From 5c988510b350736841cd4e3c2351457c361a8e06 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 1 Feb 2020 00:48:21 +0800 Subject: [PATCH] Preserve fdt paddings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some Motorola devices (Qualcomm kernel with CONFIG_MMI_DEVICE_DTBS configuration enabled) need 1k of padding to the DTBs to allow for environment variables to be runtime added by the bootloader. Those extra paddings will be removed during the process of dtb patch, devices won’t be able to boot-up and return to fastboot mode immediately after flashed the flawed boot.img. Credits to @shakalaca, close #2273 --- native/jni/magiskboot/dtb.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/native/jni/magiskboot/dtb.cpp b/native/jni/magiskboot/dtb.cpp index f830d900b..7c5e343e7 100644 --- a/native/jni/magiskboot/dtb.cpp +++ b/native/jni/magiskboot/dtb.cpp @@ -283,24 +283,35 @@ static int dt_table_patch(const Header *hdr, const char *out) { static int blob_patch(uint8_t *dtb, size_t dtb_sz, const char *out) { vector fdt_list; + vector padding_list; for (int i = 0; i < dtb_sz; ++i) { if (memcmp(dtb + i, FDT_MAGIC_STR, 4) == 0) { - int len = fdt_totalsize(dtb + i); + auto len = fdt_totalsize(dtb + i); auto fdt = static_cast(xmalloc(len + 256)); memcpy(fdt, dtb + i, len); + fdt_pack(fdt); + uint32_t padding = len - fdt_totalsize(fdt); + padding_list.push_back(padding); fdt_open_into(fdt, fdt, len + 256); fdt_list.push_back(fdt); i += len - 1; } } + if (!fdt_patch(fdt_list.begin(), fdt_list.end())) return 1; unlink(out); int fd = xopen(out, O_WRONLY | O_CREAT | O_CLOEXEC, 0644); - for (auto fdt : fdt_list) { + for (int i = 0; i < fdt_list.size(); ++i) { + auto fdt = fdt_list[i]; fdt_pack(fdt); + // Only add padding back if it is anything meaningful + if (padding_list[i] > 4) { + auto len = fdt_totalsize(fdt); + fdt_set_totalsize(fdt, len + padding_list[i]); + } xwrite(fd, fdt, fdt_totalsize(fdt)); free(fdt); }