1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.security;
18 
19 import android.os.SystemProperties;
20 import android.text.TextUtils;
21 
22 import com.android.internal.os.Zygote;
23 import com.android.settings.R;
24 import com.android.settings.core.BasePreferenceController;
25 
26 import java.util.Arrays;
27 
28 public class MemtagHelper {
29     public static final String DEVICE_CONFIG_PROP =
30             "persist.device_config.runtime_native_boot.bootloader_override";
31 
isForcedOff()32     public static boolean isForcedOff() {
33         return TextUtils.equals("force_off", SystemProperties.get(DEVICE_CONFIG_PROP));
34     }
35 
isForcedOn()36     public static boolean isForcedOn() {
37         return TextUtils.equals("force_on", SystemProperties.get(DEVICE_CONFIG_PROP));
38     }
39 
isChecked()40     public static boolean isChecked() {
41         String modes[] = SystemProperties.get("arm64.memtag.bootctl", "").split(",");
42         return Arrays.asList(modes).contains("memtag");
43     }
44 
setChecked(boolean isChecked)45     public static void setChecked(boolean isChecked) {
46         String newString = isChecked ? "memtag" : "none";
47         SystemProperties.set("arm64.memtag.bootctl", newString);
48     }
49 
getAvailabilityStatus()50     public static int getAvailabilityStatus() {
51         if (MemtagHelper.isForcedOff() || MemtagHelper.isForcedOn()) {
52             return BasePreferenceController.DISABLED_DEPENDENT_SETTING;
53         }
54         return SystemProperties.getBoolean("ro.arm64.memtag.bootctl_settings_toggle", false)
55                 ? BasePreferenceController.AVAILABLE
56                 : BasePreferenceController.UNSUPPORTED_ON_DEVICE;
57     }
58 
59     /**
60      * Returns whether MTE is currently active on this device. We use this to determine whether we
61      * need to reboot the device to apply the user choice.
62      *
63      * @return boolean whether MTE is currently active
64      */
isOn()65     public static boolean isOn() {
66         return Zygote.nativeSupportsMemoryTagging();
67     }
68 
getSummary()69     public static int getSummary() {
70         if (isForcedOff()) {
71             return R.string.memtag_force_off;
72         }
73         if (isForcedOn()) {
74             return R.string.memtag_force_on;
75         }
76         if (isOn()) {
77             if (isChecked()) {
78                 return R.string.memtag_on;
79             }
80             return R.string.memtag_off_pending;
81         }
82         if (isChecked()) {
83             return R.string.memtag_on_pending;
84         }
85         return R.string.memtag_off;
86     }
87 }
88