Android12 persist.sys.usb.config值更新

adb的相关控制

在android 4.0 之后,adb 的控制统一使用了persist.sys.usb.config来控制,而这个persist.sys.usb.config 中的adb是根据ro.debuggable = 1 or 0 来设置,1 就是开启adb, 0 即关闭adb debug.

有两个地方影响

  • build/tools/post_process_props.py
java 复制代码
def mangle_build_prop(prop_list):
  # If ro.debuggable is 1, then enable adb on USB by default
  # (this is for userdebug builds)
  if prop_list.get_value("ro.debuggable") == "1":
    val = prop_list.get_value("persist.sys.usb.config")
    if "adb" not in val:
      if val == "":
        val = "adb"
      else:
        val = val + ",adb"
      prop_list.put("persist.sys.usb.config", val)
  # UsbDeviceManager expects a value here.  If it doesn't get it, it will
  # default to "adb". That might not the right policy there, but it's better
  # to be explicit.
  if not prop_list.get_value("persist.sys.usb.config"):
    prop_list.put("persist.sys.usb.config", "none")

在system.prop中添加的会被post_process_props.py中的覆盖,build.prop中有显示

java 复制代码
# Value overridden by post_process_props.py. Original value: mtp
persist.sys.usb.config=mtp,adb
  • system/core/init/property_service.cpp
java 复制代码
static void update_sys_usb_config() {
    bool is_debuggable = android::base::GetBoolProperty("ro.debuggable", false);
    std::string config = android::base::GetProperty("persist.sys.usb.config", "");
    // b/150130503, add (config == "none") condition here to prevent appending
    // ",adb" if "none" is explicitly defined in default prop.
    if (config.empty() || config == "none") {
        InitPropertySet("persist.sys.usb.config", is_debuggable ? "adb" : "none");
    } else if (is_debuggable && config.find("adb") == std::string::npos &&
               config.length() + 4 < PROP_VALUE_MAX) {
        config.append(",adb");
        InitPropertySet("persist.sys.usb.config", config);
    }
}

...

void PropertyLoadBootDefaults() {
	...
	 // Order matters here. The more the partition is specific to a product, the higher its
    // precedence is.
    LoadPropertiesFromSecondStageRes(&properties);
    load_properties_from_file("/system/build.prop", nullptr, &properties);
    load_properties_from_partition("system_ext", /* support_legacy_path_until */ 30);
    // TODO(b/117892318): uncomment the following condition when vendor.imgs for aosp_* targets are
    // all updated.
    // if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_R__) {
    load_properties_from_file("/vendor/default.prop", nullptr, &properties);
    // }
    load_properties_from_file("/vendor/build.prop", nullptr, &properties);
    load_properties_from_file("/vendor_dlkm/etc/build.prop", nullptr, &properties);
    load_properties_from_file("/odm_dlkm/etc/build.prop", nullptr, &properties);
    load_properties_from_partition("odm", /* support_legacy_path_until */ 28);
    load_properties_from_partition("product", /* support_legacy_path_until */ 30);

    if (access(kDebugRamdiskProp, R_OK) == 0) {
        LOG(INFO) << "Loading " << kDebugRamdiskProp;
        load_properties_from_file(kDebugRamdiskProp, nullptr, &properties);
    }

    for (const auto& [name, value] : properties) {
        std::string error;
        if (PropertySet(name, value, &error) != PROP_SUCCESS) {
            LOG(ERROR) << "Could not set '" << name << "' to '" << value
                       << "' while loading .prop files" << error;
        }
    }

    property_initialize_ro_product_props();
    property_initialize_build_id();
    property_derive_build_fingerprint();
    property_derive_legacy_build_fingerprint();
    property_initialize_ro_cpu_abilist();
	
	// 获取完所有默认属性值,再次更新
    update_sys_usb_config();
}

post_process_props.py中的值编译完是在/system/build.prop,所以也可能会在property_service.cpp中被修改。

保存persist.sys.usb.config属性值

update_sys_usb_config中更新属性值-->InitPropertySe()-->HandlePropertySet()-->PropertySet(),在PropertySet方法中将属性值写入磁盘中。

  • system/core/init/property_service.cpp
java 复制代码
static uint32_t PropertySet(const std::string& name, const std::string& value, std::string* error) {
    size_t valuelen = value.size();

    if (!IsLegalPropertyName(name)) {
        *error = "Illegal property name";
        return PROP_ERROR_INVALID_NAME;
    }

    if (auto result = IsLegalPropertyValue(name, value); !result.ok()) {
        *error = result.error().message();
        return PROP_ERROR_INVALID_VALUE;
    }
#ifdef MTK_LOG
    SnapshotPropertyFlowTraceLog("_spf");
#endif
    prop_info* pi = (prop_info*) __system_property_find(name.c_str());
    if (pi != nullptr) {
        // ro.* properties are actually "write-once".
        if (StartsWith(name, "ro.")) {
            *error = "Read-only property was already set";
            return PROP_ERROR_READ_ONLY_PROPERTY;
        }

#ifdef MTK_LOG
        SnapshotPropertyFlowTraceLog("_spu");
#endif
        __system_property_update(pi, value.c_str(), valuelen);
    } else {
#ifdef MTK_LOG
        SnapshotPropertyFlowTraceLog("_spa");
#endif
        int rc = __system_property_add(name.c_str(), name.size(), value.c_str(), valuelen);
        if (rc < 0) {
            *error = "__system_property_add failed";
            return PROP_ERROR_SET_FAILED;
        }
    }

	// 写入磁盘
    // Don't write properties to disk until after we have read all default
    // properties to prevent them from being overwritten by default values.
    if (persistent_properties_loaded && StartsWith(name, "persist.")) {
        WritePersistentProperty(name, value);
    }
    // If init hasn't started its main loop, then it won't be handling property changed messages
    // anyway, so there's no need to try to send them.
    auto lock = std::lock_guard{accept_messages_lock};
    if (accept_messages) {
#ifdef MTK_LOG
        SnapshotPropertyFlowTraceLog("SPC");
#endif
		// 通知属性值变化
        PropertyChanged(name, value);
    }

#ifdef MTK_LOG
    if (GetMTKLOGDISABLERATELIMIT()) // MTK add log
        PropSetLog(name, value, error);
#endif

    return PROP_SUCCESS;
}
属性值更新
  • system/core/init/init.cpp
java 复制代码
void PropertyChanged(const std::string& name, const std::string& value) {
#ifdef G1122717
    if (!ActionManager::GetInstance().WatchingPropertyCount(name))
        return;
#endif

    // If the property is sys.powerctl, we bypass the event queue and immediately handle it.
    // This is to ensure that init will always and immediately shutdown/reboot, regardless of
    // if there are other pending events to process or if init is waiting on an exec service or
    // waiting on a property.
    // In non-thermal-shutdown case, 'shutdown' trigger will be fired to let device specific
    // commands to be executed.
    if (name == "sys.powerctl") {
        trigger_shutdown(value);
    }

    if (property_triggers_enabled) {
        ActionManager::GetInstance().QueuePropertyChange(name, value);
        WakeMainInitThread();
    }

    prop_waiter_state.CheckAndResetWait(name, value);
}
相关推荐
framework-coder4 个月前
Android12 禁用adb
adb·sys.usb.config