Skip to content

Instantly share code, notes, and snippets.

@phhusson
Created January 5, 2024 16:28
Show Gist options
  • Save phhusson/e36d47beedd8bb9eeac01d1258ae18d7 to your computer and use it in GitHub Desktop.
Save phhusson/e36d47beedd8bb9eeac01d1258ae18d7 to your computer and use it in GitHub Desktop.
Booting Android over USB
From e3e5afef1f357a86c1c5bc1386910ed3ed095d7b Mon Sep 17 00:00:00 2001
From: Pierre-Hugues Husson <phh@phh.me>
Date: Tue, 18 Jul 2023 15:01:17 +0200
Subject: [PATCH] Allow booting USB
This fixes boot USB (assuming androidboot.boot_device=ff500000.dwc3 in cmdline):
- Do prefix match rather than full match, because full path includes USB path which might be problematic
- Only take boot_devices' list of partitions to confirm that partitions are ready
(without it internal emmc's partitions will be enough to tell system that it's ready to boot, even though it isn't)
Change-Id: I7b7aa0eff1442054eef19679e05e7a0808dcfd9d
---
init/block_dev_initializer.cpp | 17 +++++++++++++----
init/block_dev_initializer.h | 1 +
init/devices.cpp | 6 +++++-
3 files changed, 19 insertions(+), 5 deletions(-)
diff --git a/init/block_dev_initializer.cpp b/init/block_dev_initializer.cpp
index 05e00edfda..1688bbf768 100644
--- a/init/block_dev_initializer.cpp
+++ b/init/block_dev_initializer.cpp
@@ -29,11 +29,10 @@ namespace init {
using android::base::Timer;
using namespace std::chrono_literals;
-BlockDevInitializer::BlockDevInitializer() : uevent_listener_(16 * 1024 * 1024) {
- auto boot_devices = android::fs_mgr::GetBootDevices();
+BlockDevInitializer::BlockDevInitializer() : uevent_listener_(16 * 1024 * 1024), boot_devices_(android::fs_mgr::GetBootDevices()) {
device_handler_ = std::make_unique<DeviceHandler>(
std::vector<Permissions>{}, std::vector<SysfsPermissions>{}, std::vector<Subsystem>{},
- std::move(boot_devices), false);
+ boot_devices_, false);
}
bool BlockDevInitializer::InitDeviceMapper() {
@@ -98,6 +97,16 @@ ListenerAction BlockDevInitializer::HandleUevent(const Uevent& uevent,
LOG(VERBOSE) << __PRETTY_FUNCTION__ << ": found partition: " << name;
+ std::string device;
+ bool is_boot_device = false;
+ for(const auto& b: boot_devices_) {
+ if(uevent.path.find(b) != std::string::npos) {
+ is_boot_device = true;
+ }
+ }
+
+ if(!is_boot_device) return ListenerAction::kContinue;
+
devices->erase(iter);
device_handler_->HandleUevent(uevent);
return devices->empty() ? ListenerAction::kStop : ListenerAction::kContinue;
@@ -116,7 +125,7 @@ bool BlockDevInitializer::InitDevices(std::set<std::string> devices) {
<< ": partition(s) not found in /sys, waiting for their uevent(s): "
<< android::base::Join(devices, ", ");
Timer t;
- uevent_listener_.Poll(uevent_callback, 10s);
+ uevent_listener_.Poll(uevent_callback, 100s);
LOG(INFO) << "Wait for partitions returned after " << t;
}
diff --git a/init/block_dev_initializer.h b/init/block_dev_initializer.h
index ec39ce084d..7732ce6598 100644
--- a/init/block_dev_initializer.h
+++ b/init/block_dev_initializer.h
@@ -40,6 +40,7 @@ class BlockDevInitializer final {
std::unique_ptr<DeviceHandler> device_handler_;
UeventListener uevent_listener_;
+ std::set<std::string> boot_devices_;
};
} // namespace init
diff --git a/init/devices.cpp b/init/devices.cpp
index d4a3cb9d35..d81c6a4216 100644
--- a/init/devices.cpp
+++ b/init/devices.cpp
@@ -403,7 +403,11 @@ std::vector<std::string> DeviceHandler::GetBlockDeviceSymlinks(const Uevent& uev
auto link_path = "/dev/block/" + type + "/" + device;
- bool is_boot_device = boot_devices_.find(device) != boot_devices_.end();
+ bool is_boot_device = false;
+ for(const auto& b: boot_devices_) {
+ if(device.find(b) == 0)
+ is_boot_device = true;
+ }
if (!uevent.partition_name.empty()) {
std::string partition_name_sanitized(uevent.partition_name);
SanitizePartitionName(&partition_name_sanitized);
--
2.41.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment