diff --git a/fastboot/device/flashing.cpp b/fastboot/device/flashing.cpp index e3d84e098..66b90bf7d 100644 --- a/fastboot/device/flashing.cpp +++ b/fastboot/device/flashing.cpp @@ -21,10 +21,15 @@ #include #include +#include +#include +#include #include #include #include +#include +#include #include #include #include @@ -32,13 +37,35 @@ #include "fastboot_device.h" #include "utility.h" +using namespace android::fs_mgr; +using namespace std::literals; + namespace { constexpr uint32_t SPARSE_HEADER_MAGIC = 0xed26ff3a; -} // namespace +void WipeOverlayfsForPartition(FastbootDevice* device, const std::string& partition_name) { + // May be called, in the case of sparse data, multiple times so cache/skip. + static std::set wiped; + if (wiped.find(partition_name) != wiped.end()) return; + wiped.insert(partition_name); + // Following appears to have a first time 2% impact on flashing speeds. -using namespace android::fs_mgr; + // Convert partition_name to a validated mount point and wipe. + std::unique_ptr fstab(fs_mgr_read_fstab_default(), + fs_mgr_free_fstab); + for (auto i = 0; i < fstab->num_entries; i++) { + const auto mount_point = fstab->recs[i].mount_point; + if (!mount_point) continue; + auto partition = android::base::Basename(mount_point); + if ("/"s == mount_point) partition = "system"; + if ((partition + device->GetCurrentSlot()) == partition_name) { + fs_mgr_overlayfs_teardown(mount_point); + } + } +} + +} // namespace int FlashRawDataChunk(int fd, const char* data, size_t len) { size_t ret = 0; @@ -101,6 +128,7 @@ int Flash(FastbootDevice* device, const std::string& partition_name) { } else if (data.size() > get_block_device_size(handle.fd())) { return -EOVERFLOW; } + WipeOverlayfsForPartition(device, partition_name); return FlashBlockDevice(handle.fd(), data); } diff --git a/fs_mgr/tests/adb-remount-test.sh b/fs_mgr/tests/adb-remount-test.sh index d4ca57496..5ef479452 100755 --- a/fs_mgr/tests/adb-remount-test.sh +++ b/fs_mgr/tests/adb-remount-test.sh @@ -251,8 +251,33 @@ adb_root && B="`adb_cat /vendor/hello`" || die "re-read vendor hello after reboot" check_eq "${A}" "${B}" vendor after reboot + +adb reboot-fastboot && + fastboot flash vendor && + fastboot reboot || + die "fastbootd flash vendor" +adb_wait && + adb_root && + adb_wait && + adb_sh df -k /dev/null || + die "overlay system takeover after flash vendor" +adb_sh df -k /dev/null && + die "overlay minus vendor takeover after flash vendor" +B="`adb_cat /system/hello`" || + die "re-read system hello after flash vendor" +check_eq "${A}" "${B}" system after flash vendor +adb_root && + adb_wait || + die "adb root" +B="`adb_cat /vendor/hello`" && + die "re-read vendor hello after flash vendor" +check_eq "cat: /vendor/hello: No such file or directory" "${B}" vendor after flash vendor + adb remount && - adb_sh rm /system/hello /vendor/hello /dev/null || true ) && + adb_sh rm /system/hello