static bool initialize_adb() { bool ret = true; const char *version = get_mbtool_version(); ret = write_file("/sys/class/android_usb/android0/enable", "0", 1) && ret; ret = write_file("/sys/class/android_usb/android0/idVendor", "18D1", 4) && ret; ret = write_file("/sys/class/android_usb/android0/idProduct", "4EE7", 4) && ret; ret = write_file("/sys/class/android_usb/android0/f_ffs/aliases", "adb", 3) && ret; ret = write_file("/sys/class/android_usb/android0/functions", "adb", 3) && ret; ret = write_file("/sys/class/android_usb/android0/iManufacturer", "mbtool", 6) && ret; ret = write_file("/sys/class/android_usb/android0/iProduct", "miniadbd", 8) && ret; ret = write_file("/sys/class/android_usb/android0/iSerial", version, strlen(version)) && ret; ret = write_file("/sys/class/android_usb/android0/enable", "1", 1) && ret; // Create functionfs paths if (!util::mkdir_recursive(USB_FFS_ADB_PATH, 0770)) { LOGW("%s: Failed to create directory: %s", USB_FFS_ADB_PATH, strerror(errno)); ret = false; } if (!util::mount("adb", USB_FFS_ADB_PATH, "functionfs", 0, "")) { LOGW("%s: Failed to mount functionfs: %s", USB_FFS_ADB_PATH, strerror(errno)); ret = false; } // Set environment variables ret = setenv("PATH", "/system/bin", 1) == 0 && ret; ret = setenv("LD_LIBRARY_PATH", "/system/lib", 1) == 0 && ret; ret = setenv("ANDROID_DATA", "/data", 1) == 0 && ret; ret = setenv("ANDROID_ROOT", "/system", 1) == 0 && ret; // Use TWRP's legacy property workspace hack. TouchWiz's sh binary segfaults // if there's no property service (i.e. no /dev/__properties__ and no valid // ANDROID_PROPERTY_WORKSPACE environment variable). This doesn't actually // start a "real" properties service (getprop and setprop will never work), // but it's enough to prevent the segfaults. char tmp[32]; int propfd, propsz; legacy_properties_init(); // Load /default.prop std::unordered_map<std::string, std::string> props; util::file_get_all_properties("/default.prop", &props); for (auto const &pair : props) { legacy_property_set(pair.first.c_str(), pair.second.c_str()); } // Load /system/build.prop props.clear(); util::file_get_all_properties("/system/build.prop", &props); for (auto const &pair : props) { legacy_property_set(pair.first.c_str(), pair.second.c_str()); } legacy_get_property_workspace(&propfd, &propsz); snprintf(tmp, sizeof(tmp), "%d,%d", dup(propfd), propsz); char *orig_prop_env = getenv("ANDROID_PROPERTY_WORKSPACE"); LOGD("Original properties environment: %s", orig_prop_env ? orig_prop_env : "null"); setenv("ANDROID_PROPERTY_WORKSPACE", tmp, 1); LOGD("Switched to legacy properties environment: %s", tmp); return ret; }
static void copy_property_to_legacy(const char *key, const char *value, void *cookie) { legacy_property_set(key, value); }