Magisk/native/src/boot/bootimg.cpp

1019 lines
35 KiB
C++
Raw Normal View History

2023-09-19 00:12:49 -07:00
#include <bit>
#include <functional>
#include <memory>
#include <span>
2022-05-12 02:03:42 -07:00
#include <base.hpp>
2023-06-25 03:50:21 +08:00
#include "boot-rs.hpp"
2020-03-09 01:50:30 -07:00
#include "bootimg.hpp"
#include "magiskboot.hpp"
#include "compress.hpp"
using namespace std;
2020-10-12 01:55:33 -07:00
#define PADDING 15
2023-06-25 03:50:21 +08:00
#define SHA256_DIGEST_SIZE 32
#define SHA_DIGEST_SIZE 20
2020-10-12 01:55:33 -07:00
static void decompress(format_t type, int fd, const void *in, size_t size) {
2024-02-28 11:07:53 -08:00
auto ptr = get_decoder(type, make_unique<fd_stream>(fd));
ptr->write(in, size);
}
2019-11-23 04:57:52 -05:00
static off_t compress(format_t type, int fd, const void *in, size_t size) {
auto prev = lseek(fd, 0, SEEK_CUR);
{
2024-02-28 11:07:53 -08:00
auto strm = get_encoder(type, make_unique<fd_stream>(fd));
strm->write(in, size);
}
auto now = lseek(fd, 0, SEEK_CUR);
return now - prev;
}
static void dump(const void *buf, size_t size, const char *filename) {
if (size == 0)
return;
int fd = creat(filename, 0644);
xwrite(fd, buf, size);
close(fd);
}
2019-10-09 16:01:21 -04:00
static size_t restore(int fd, const char *filename) {
int ifd = xopen(filename, O_RDONLY);
size_t size = lseek(ifd, 0, SEEK_END);
lseek(ifd, 0, SEEK_SET);
xsendfile(fd, ifd, nullptr, size);
close(ifd);
return size;
}
2023-06-03 05:10:22 -07:00
void dyn_img_hdr::print() const {
uint32_t ver = header_version();
fprintf(stderr, "%-*s [%u]\n", PADDING, "HEADER_VER", ver);
2023-06-03 05:10:22 -07:00
if (!is_vendor())
2021-11-21 05:55:20 -08:00
fprintf(stderr, "%-*s [%u]\n", PADDING, "KERNEL_SZ", kernel_size());
fprintf(stderr, "%-*s [%u]\n", PADDING, "RAMDISK_SZ", ramdisk_size());
if (ver < 3)
fprintf(stderr, "%-*s [%u]\n", PADDING, "SECOND_SZ", second_size());
if (ver == 0)
fprintf(stderr, "%-*s [%u]\n", PADDING, "EXTRA_SZ", extra_size());
if (ver == 1 || ver == 2)
fprintf(stderr, "%-*s [%u]\n", PADDING, "RECOV_DTBO_SZ", recovery_dtbo_size());
2023-06-03 05:10:22 -07:00
if (ver == 2 || is_vendor())
fprintf(stderr, "%-*s [%u]\n", PADDING, "DTB_SZ", dtb_size());
if (ver == 4 && is_vendor())
fprintf(stderr, "%-*s [%u]\n", PADDING, "BOOTCONFIG_SZ", bootconfig_size());
if (uint32_t os_ver = os_version()) {
int a,b,c,y,m = 0;
int version = os_ver >> 11;
int patch_level = os_ver & 0x7ff;
a = (version >> 14) & 0x7f;
b = (version >> 7) & 0x7f;
c = version & 0x7f;
fprintf(stderr, "%-*s [%d.%d.%d]\n", PADDING, "OS_VERSION", a, b, c);
y = (patch_level >> 4) + 2000;
m = patch_level & 0xf;
fprintf(stderr, "%-*s [%d-%02d]\n", PADDING, "OS_PATCH_LEVEL", y, m);
}
fprintf(stderr, "%-*s [%u]\n", PADDING, "PAGESIZE", page_size());
2023-06-03 05:10:22 -07:00
if (const char *n = name()) {
2021-10-31 10:52:12 -07:00
fprintf(stderr, "%-*s [%s]\n", PADDING, "NAME", n);
}
fprintf(stderr, "%-*s [%.*s%.*s]\n", PADDING, "CMDLINE",
BOOT_ARGS_SIZE, cmdline(), BOOT_EXTRA_ARGS_SIZE, extra_cmdline());
2023-06-03 05:10:22 -07:00
if (const char *checksum = id()) {
fprintf(stderr, "%-*s [", PADDING, "CHECKSUM");
for (int i = 0; i < SHA256_DIGEST_SIZE; ++i)
2021-10-31 10:52:12 -07:00
fprintf(stderr, "%02hhx", checksum[i]);
fprintf(stderr, "]\n");
}
}
2023-06-03 05:10:22 -07:00
void dyn_img_hdr::dump_hdr_file() const {
FILE *fp = xfopen(HEADER_FILE, "w");
if (name())
fprintf(fp, "name=%s\n", name());
fprintf(fp, "cmdline=%.*s%.*s\n", BOOT_ARGS_SIZE, cmdline(), BOOT_EXTRA_ARGS_SIZE, extra_cmdline());
uint32_t ver = os_version();
if (ver) {
int a, b, c, y, m;
int version, patch_level;
version = ver >> 11;
patch_level = ver & 0x7ff;
a = (version >> 14) & 0x7f;
b = (version >> 7) & 0x7f;
c = version & 0x7f;
fprintf(fp, "os_version=%d.%d.%d\n", a, b, c);
y = (patch_level >> 4) + 2000;
m = patch_level & 0xf;
fprintf(fp, "os_patch_level=%d-%02d\n", y, m);
}
fclose(fp);
2019-10-09 16:01:21 -04:00
}
void dyn_img_hdr::load_hdr_file() {
2023-08-15 17:10:20 -07:00
parse_prop_file(HEADER_FILE, [=, this](string_view key, string_view value) -> bool {
2021-11-20 22:51:22 -08:00
if (key == "name" && name()) {
memset(name(), 0, 16);
memcpy(name(), value.data(), value.length() > 15 ? 15 : value.length());
} else if (key == "cmdline") {
memset(cmdline(), 0, BOOT_ARGS_SIZE);
memset(extra_cmdline(), 0, BOOT_EXTRA_ARGS_SIZE);
if (value.length() > BOOT_ARGS_SIZE) {
memcpy(cmdline(), value.data(), BOOT_ARGS_SIZE);
auto len = std::min(value.length() - BOOT_ARGS_SIZE, (size_t) BOOT_EXTRA_ARGS_SIZE);
memcpy(extra_cmdline(), &value[BOOT_ARGS_SIZE], len);
} else {
memcpy(cmdline(), value.data(), value.length());
}
} else if (key == "os_version") {
int patch_level = os_version() & 0x7ff;
int a, b, c;
sscanf(value.data(), "%d.%d.%d", &a, &b, &c);
os_version() = (((a << 14) | (b << 7) | c) << 11) | patch_level;
} else if (key == "os_patch_level") {
int os_ver = os_version() >> 11;
int y, m;
sscanf(value.data(), "%d-%d", &y, &m);
y -= 2000;
os_version() = (os_ver << 11) | (y << 4) | m;
}
return true;
});
2019-10-09 16:01:21 -04:00
}
2021-11-30 01:50:55 -08:00
boot_img::boot_img(const char *image) : map(image) {
fprintf(stderr, "Parsing boot image: [%s]\n", image);
2023-06-03 03:16:03 -07:00
for (const uint8_t *addr = map.buf(); addr < map.buf() + map.sz(); ++addr) {
format_t fmt = check_fmt(addr, map.sz());
switch (fmt) {
case CHROMEOS:
// chromeos require external signing
2021-02-28 12:56:23 -08:00
flags[CHROMEOS_FLAG] = true;
addr += 65535;
break;
case DHTB:
2021-02-28 12:56:23 -08:00
flags[DHTB_FLAG] = true;
flags[SEANDROID_FLAG] = true;
fprintf(stderr, "DHTB_HDR\n");
addr += sizeof(dhtb_hdr) - 1;
break;
case BLOB:
2021-02-28 12:56:23 -08:00
flags[BLOB_FLAG] = true;
fprintf(stderr, "TEGRA_BLOB\n");
addr += sizeof(blob_hdr) - 1;
break;
case AOSP:
case AOSP_VENDOR:
if (parse_image(addr, fmt))
return;
// fallthrough
default:
break;
}
}
exit(1);
}
2019-10-09 16:01:21 -04:00
boot_img::~boot_img() {
delete hdr;
2019-10-09 16:01:21 -04:00
}
2023-09-19 00:12:49 -07:00
struct [[gnu::packed]] fdt_header {
struct fdt32_t {
uint32_t byte0: 8;
uint32_t byte1: 8;
uint32_t byte2: 8;
uint32_t byte3: 8;
constexpr operator uint32_t() const {
return bit_cast<uint32_t>(fdt32_t {
.byte0 = byte3,
.byte1 = byte2,
.byte2 = byte1,
.byte3 = byte0
});
}
};
struct node_header {
fdt32_t tag;
char name[0];
};
fdt32_t magic; /* magic word FDT_MAGIC */
fdt32_t totalsize; /* total size of DT block */
fdt32_t off_dt_struct; /* offset to structure */
fdt32_t off_dt_strings; /* offset to strings */
fdt32_t off_mem_rsvmap; /* offset to memory reserve map */
fdt32_t version; /* format version */
fdt32_t last_comp_version; /* last compatible version */
/* version 2 fields below */
fdt32_t boot_cpuid_phys; /* Which physical CPU id we're
booting on */
/* version 3 fields below */
fdt32_t size_dt_strings; /* size of the strings block */
/* version 17 fields below */
fdt32_t size_dt_struct; /* size of the structure block */
};
static int find_dtb_offset(const uint8_t *buf, unsigned sz) {
const uint8_t * const end = buf + sz;
2021-02-28 14:36:48 -08:00
for (auto curr = buf; curr < end; curr += sizeof(fdt_header)) {
2023-09-19 00:12:49 -07:00
curr = static_cast<uint8_t*>(memmem(curr, end - curr, DTB_MAGIC, sizeof(fdt_header::fdt32_t)));
2021-02-28 14:36:48 -08:00
if (curr == nullptr)
return -1;
auto fdt_hdr = reinterpret_cast<const fdt_header *>(curr);
// Check that fdt_header.totalsize does not overflow kernel image size
2023-09-19 00:12:49 -07:00
uint32_t totalsize = fdt_hdr->totalsize;
if (totalsize > end - curr)
continue;
// Check that fdt_header.off_dt_struct does not overflow kernel image size
2023-09-19 00:12:49 -07:00
uint32_t off_dt_struct = fdt_hdr->off_dt_struct;
if (off_dt_struct > end - curr)
continue;
// Check that fdt_node_header.tag of first node is FDT_BEGIN_NODE
2023-09-19 00:12:49 -07:00
auto fdt_node_hdr = reinterpret_cast<const fdt_header::node_header *>(curr + off_dt_struct);
if (fdt_node_hdr->tag != 0x1u)
continue;
2021-02-28 14:36:48 -08:00
return curr - buf;
}
return -1;
2020-10-15 00:19:11 -07:00
}
static format_t check_fmt_lg(const uint8_t *buf, unsigned sz) {
format_t fmt = check_fmt(buf, sz);
if (fmt == LZ4_LEGACY) {
// We need to check if it is LZ4_LG
2021-11-21 05:55:20 -08:00
uint32_t off = 4;
uint32_t block_sz;
while (off + sizeof(block_sz) <= sz) {
memcpy(&block_sz, buf + off, sizeof(block_sz));
off += sizeof(block_sz);
if (off + block_sz > sz)
return LZ4_LG;
off += block_sz;
}
}
return fmt;
2020-10-14 23:45:06 -07:00
}
2021-10-31 10:52:12 -07:00
#define CMD_MATCH(s) BUFFER_MATCH(h->cmdline, s)
pair<const uint8_t *, dyn_img_hdr *> boot_img::create_hdr(const uint8_t *addr, format_t type) {
if (type == AOSP_VENDOR) {
fprintf(stderr, "VENDOR_BOOT_HDR\n");
auto h = reinterpret_cast<const boot_img_hdr_vnd_v3*>(addr);
2021-10-31 10:52:12 -07:00
switch (h->header_version) {
2021-05-08 22:32:51 +08:00
case 4:
return make_pair(addr, new dyn_img_vnd_v4(addr));
2021-05-08 22:32:51 +08:00
default:
return make_pair(addr, new dyn_img_vnd_v3(addr));
2021-05-08 22:32:51 +08:00
}
2021-11-20 22:44:38 -08:00
}
auto h = reinterpret_cast<const boot_img_hdr_v0*>(addr);
2021-11-20 22:44:38 -08:00
if (h->page_size >= 0x02000000) {
fprintf(stderr, "PXA_BOOT_HDR\n");
return make_pair(addr, new dyn_img_pxa(addr));
2021-11-20 22:44:38 -08:00
}
auto make_hdr = [](const uint8_t *ptr) -> dyn_img_hdr * {
auto h = reinterpret_cast<const boot_img_hdr_v0*>(ptr);
if (memcmp(h->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE) != 0)
return nullptr;
switch (h->header_version) {
case 1:
return new dyn_img_v1(ptr);
case 2:
return new dyn_img_v2(ptr);
case 3:
return new dyn_img_v3(ptr);
case 4:
return new dyn_img_v4(ptr);
default:
return new dyn_img_v0(ptr);
}
};
// For NOOKHD and ACCLAIM, the entire boot image is shifted by a fixed offset.
// For AMONET, only the header is internally shifted by a fixed offset.
if (BUFFER_CONTAIN(addr, AMONET_MICROLOADER_SZ, AMONET_MICROLOADER_MAGIC) &&
BUFFER_MATCH(addr + AMONET_MICROLOADER_SZ, BOOT_MAGIC)) {
flags[AMONET_FLAG] = true;
fprintf(stderr, "AMONET_MICROLOADER\n");
// The real header is shifted, copy to temporary buffer
h = reinterpret_cast<const boot_img_hdr_v0*>(addr + AMONET_MICROLOADER_SZ);
if (memcmp(h->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE) != 0)
return make_pair(addr, nullptr);
auto real_hdr_sz = h->page_size - AMONET_MICROLOADER_SZ;
heap_data copy(h->page_size);
memcpy(copy.buf(), h, real_hdr_sz);
return make_pair(addr, make_hdr(copy.buf()));
}
2021-11-20 22:44:38 -08:00
if (CMD_MATCH(NOOKHD_RL_MAGIC) ||
CMD_MATCH(NOOKHD_GL_MAGIC) ||
CMD_MATCH(NOOKHD_GR_MAGIC) ||
CMD_MATCH(NOOKHD_EB_MAGIC) ||
CMD_MATCH(NOOKHD_ER_MAGIC)) {
flags[NOOKHD_FLAG] = true;
fprintf(stderr, "NOOKHD_LOADER\n");
addr += NOOKHD_PRE_HEADER_SZ;
} else if (BUFFER_MATCH(h->name, ACCLAIM_MAGIC)) {
2021-11-20 22:44:38 -08:00
flags[ACCLAIM_FLAG] = true;
fprintf(stderr, "ACCLAIM_LOADER\n");
addr += ACCLAIM_PRE_HEADER_SZ;
}
return make_pair(addr, make_hdr(addr));
}
static const char *vendor_ramdisk_type(int type) {
switch (type) {
case VENDOR_RAMDISK_TYPE_PLATFORM:
return "platform";
case VENDOR_RAMDISK_TYPE_RECOVERY:
return "recovery";
case VENDOR_RAMDISK_TYPE_DLKM:
return "dlkm";
case VENDOR_RAMDISK_TYPE_NONE:
default:
return "none";
}
}
#define assert_off() \
if ((base_addr + off) > (map.buf() + map.sz())) { \
fprintf(stderr, "Corrupted boot image!\n"); \
return false; \
2021-11-20 22:44:38 -08:00
}
#define get_block(name) \
name = base_addr + off; \
2021-11-20 22:44:38 -08:00
off += hdr->name##_size(); \
off = align_to(off, hdr->page_size()); \
assert_off();
2021-11-20 22:44:38 -08:00
bool boot_img::parse_image(const uint8_t *p, format_t type) {
auto [base_addr, hdr] = create_hdr(p, type);
if (hdr == nullptr) {
fprintf(stderr, "Invalid boot image header!\n");
return false;
}
if (const char *id = hdr->id()) {
for (int i = SHA_DIGEST_SIZE + 4; i < SHA256_DIGEST_SIZE; ++i) {
if (id[i]) {
2021-02-28 12:56:23 -08:00
flags[SHA256_FLAG] = true;
break;
}
}
}
hdr->print();
size_t off = hdr->hdr_space();
get_block(kernel);
get_block(ramdisk);
get_block(second);
get_block(extra);
get_block(recovery_dtbo);
get_block(dtb);
get_block(signature);
get_block(vendor_ramdisk_table);
get_block(bootconfig);
2021-11-21 05:55:20 -08:00
payload = byte_view(base_addr, off);
auto tail_addr = base_addr + off;
tail = byte_view(tail_addr, map.buf() + map.sz() - tail_addr);
if (auto size = hdr->kernel_size()) {
2021-11-23 13:39:15 -08:00
if (int dtb_off = find_dtb_offset(kernel, size); dtb_off > 0) {
kernel_dtb = byte_view(kernel + dtb_off, size - dtb_off);
2021-11-23 13:39:15 -08:00
hdr->kernel_size() = dtb_off;
fprintf(stderr, "%-*s [%zu]\n", PADDING, "KERNEL_DTB_SZ", kernel_dtb.sz());
2021-11-23 13:39:15 -08:00
}
k_fmt = check_fmt_lg(kernel, hdr->kernel_size());
if (k_fmt == MTK) {
fprintf(stderr, "MTK_KERNEL_HDR\n");
2021-02-28 12:56:23 -08:00
flags[MTK_KERNEL] = true;
k_hdr = reinterpret_cast<const mtk_hdr *>(kernel);
fprintf(stderr, "%-*s [%u]\n", PADDING, "SIZE", k_hdr->size);
fprintf(stderr, "%-*s [%s]\n", PADDING, "NAME", k_hdr->name);
kernel += sizeof(mtk_hdr);
hdr->kernel_size() -= sizeof(mtk_hdr);
k_fmt = check_fmt_lg(kernel, hdr->kernel_size());
}
2021-07-24 23:04:44 +02:00
if (k_fmt == ZIMAGE) {
z_hdr = reinterpret_cast<const zimage_hdr *>(kernel);
if (const void *gzip = memmem(kernel, hdr->kernel_size(), GZIP1_MAGIC "\x08\x00", 4)) {
2021-08-28 14:07:34 -07:00
fprintf(stderr, "ZIMAGE_KERNEL\n");
z_info.hdr_sz = (const uint8_t *) gzip - kernel;
2021-11-23 13:39:15 -08:00
// Find end of piggy
uint32_t zImage_size = z_hdr->end - z_hdr->start;
uint32_t piggy_end = zImage_size;
uint32_t offsets[16];
memcpy(offsets, kernel + zImage_size - sizeof(offsets), sizeof(offsets));
for (int i = 15; i >= 0; --i) {
if (offsets[i] > (zImage_size - 0xFF) && offsets[i] < zImage_size) {
piggy_end = offsets[i];
break;
2021-07-24 23:04:44 +02:00
}
}
2021-11-23 13:39:15 -08:00
if (piggy_end == zImage_size) {
fprintf(stderr, "! Could not find end of zImage piggy, keeping raw kernel\n");
2021-08-28 14:07:34 -07:00
} else {
flags[ZIMAGE_KERNEL] = true;
z_info.tail = byte_view(kernel + piggy_end, hdr->kernel_size() - piggy_end);
2021-08-28 14:07:34 -07:00
kernel += z_info.hdr_sz;
2021-11-23 13:39:15 -08:00
hdr->kernel_size() = piggy_end - z_info.hdr_sz;
2021-08-28 14:07:34 -07:00
k_fmt = check_fmt_lg(kernel, hdr->kernel_size());
}
} else {
2021-11-23 13:39:15 -08:00
fprintf(stderr, "! Could not find zImage gzip piggy, keeping raw kernel\n");
2021-07-24 23:04:44 +02:00
}
}
fprintf(stderr, "%-*s [%s]\n", PADDING, "KERNEL_FMT", fmt2name[k_fmt]);
}
if (auto size = hdr->ramdisk_size()) {
2024-07-31 13:52:30 +08:00
if (hdr->vendor_ramdisk_table_size()) {
2021-11-21 05:55:20 -08:00
// v4 vendor boot contains multiple ramdisks
using table_entry = const vendor_ramdisk_table_entry_v4;
if (hdr->vendor_ramdisk_table_entry_size() != sizeof(table_entry)) {
fprintf(stderr,
"! Invalid vendor image: vendor_ramdisk_table_entry_size != %zu\n",
sizeof(table_entry));
exit(1);
}
span<table_entry> table(
reinterpret_cast<table_entry *>(vendor_ramdisk_table),
hdr->vendor_ramdisk_table_entry_num());
for (auto &it : table) {
format_t fmt = check_fmt_lg(ramdisk + it.ramdisk_offset, it.ramdisk_size);
fprintf(stderr,
"%-*s name=[%s] type=[%s] size=[%u] fmt=[%s]\n", PADDING, "VND_RAMDISK",
it.ramdisk_name, vendor_ramdisk_type(it.ramdisk_type),
it.ramdisk_size, fmt2name[fmt]);
}
2021-11-21 05:55:20 -08:00
} else {
r_fmt = check_fmt_lg(ramdisk, size);
if (r_fmt == MTK) {
fprintf(stderr, "MTK_RAMDISK_HDR\n");
flags[MTK_RAMDISK] = true;
r_hdr = reinterpret_cast<const mtk_hdr *>(ramdisk);
fprintf(stderr, "%-*s [%u]\n", PADDING, "SIZE", r_hdr->size);
fprintf(stderr, "%-*s [%s]\n", PADDING, "NAME", r_hdr->name);
ramdisk += sizeof(mtk_hdr);
hdr->ramdisk_size() -= sizeof(mtk_hdr);
r_fmt = check_fmt_lg(ramdisk, hdr->ramdisk_size());
}
fprintf(stderr, "%-*s [%s]\n", PADDING, "RAMDISK_FMT", fmt2name[r_fmt]);
2021-11-21 05:55:20 -08:00
}
}
if (auto size = hdr->extra_size()) {
e_fmt = check_fmt_lg(extra, size);
fprintf(stderr, "%-*s [%s]\n", PADDING, "EXTRA_FMT", fmt2name[e_fmt]);
}
if (tail.sz()) {
// Check special flags
if (tail.sz() >= 16 && BUFFER_MATCH(tail.buf(), SEANDROID_MAGIC)) {
fprintf(stderr, "SAMSUNG_SEANDROID\n");
flags[SEANDROID_FLAG] = true;
} else if (tail.sz() >= 16 && BUFFER_MATCH(tail.buf(), LG_BUMP_MAGIC)) {
fprintf(stderr, "LG_BUMP_IMAGE\n");
flags[LG_BUMP_FLAG] = true;
}
// Check if the image is signed
if (verify()) {
fprintf(stderr, "AVB1_SIGNED\n");
flags[AVB1_SIGNED_FLAG] = true;
}
// Find AVB footer
const void *footer = tail.buf() + tail.sz() - sizeof(AvbFooter);
if (BUFFER_MATCH(footer, AVB_FOOTER_MAGIC)) {
avb_footer = reinterpret_cast<const AvbFooter*>(footer);
// Double check if meta header exists
const void *meta = base_addr + __builtin_bswap64(avb_footer->vbmeta_offset);
if (BUFFER_MATCH(meta, AVB_MAGIC)) {
fprintf(stderr, "VBMETA\n");
flags[AVB_FLAG] = true;
vbmeta = reinterpret_cast<const AvbVBMetaImageHeader*>(meta);
}
}
}
2023-06-03 05:10:22 -07:00
this->hdr = hdr;
return true;
}
bool boot_img::verify(const char *cert) const {
return rust::verify_boot_image(*this, cert);
}
int split_image_dtb(const char *filename, bool skip_decomp) {
2023-06-03 03:16:03 -07:00
mmap_data img(filename);
2023-06-03 03:16:03 -07:00
if (int off = find_dtb_offset(img.buf(), img.sz()); off > 0) {
format_t fmt = check_fmt_lg(img.buf(), img.sz());
if (!skip_decomp && COMPRESSED(fmt)) {
int fd = creat(KERNEL_FILE, 0644);
2023-06-03 03:16:03 -07:00
decompress(fmt, fd, img.buf(), off);
close(fd);
} else {
2023-06-03 03:16:03 -07:00
dump(img.buf(), off, KERNEL_FILE);
}
2023-06-03 03:16:03 -07:00
dump(img.buf() + off, img.sz() - off, KER_DTB_FILE);
return 0;
} else {
fprintf(stderr, "Cannot find DTB in %s\n", filename);
return 1;
}
}
2020-10-12 01:06:42 -07:00
int unpack(const char *image, bool skip_decomp, bool hdr) {
2023-06-03 05:10:22 -07:00
const boot_img boot(image);
if (hdr)
boot.hdr->dump_hdr_file();
// Dump kernel
if (!skip_decomp && COMPRESSED(boot.k_fmt)) {
if (boot.hdr->kernel_size() != 0) {
int fd = creat(KERNEL_FILE, 0644);
decompress(boot.k_fmt, fd, boot.kernel, boot.hdr->kernel_size());
close(fd);
}
} else {
dump(boot.kernel, boot.hdr->kernel_size(), KERNEL_FILE);
}
// Dump kernel_dtb
dump(boot.kernel_dtb.buf(), boot.kernel_dtb.sz(), KER_DTB_FILE);
// Dump ramdisk
2024-07-31 13:52:30 +08:00
if (boot.hdr->vendor_ramdisk_table_size()) {
using table_entry = const vendor_ramdisk_table_entry_v4;
span<table_entry> table(
reinterpret_cast<table_entry *>(boot.vendor_ramdisk_table),
boot.hdr->vendor_ramdisk_table_entry_num());
xmkdir(VND_RAMDISK_DIR, 0755);
owned_fd dirfd = xopen(VND_RAMDISK_DIR, O_RDONLY | O_CLOEXEC);
for (auto &it : table) {
char file_name[40];
if (it.ramdisk_name[0] == '\0') {
strscpy(file_name, RAMDISK_FILE, sizeof(file_name));
} else {
ssprintf(file_name, sizeof(file_name), "%s.cpio", it.ramdisk_name);
}
owned_fd fd = xopenat(dirfd, file_name, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0644);
format_t fmt = check_fmt_lg(boot.ramdisk + it.ramdisk_offset, it.ramdisk_size);
if (!skip_decomp && COMPRESSED(fmt)) {
decompress(fmt, fd, boot.ramdisk + it.ramdisk_offset, it.ramdisk_size);
} else {
xwrite(fd, boot.ramdisk + it.ramdisk_offset, it.ramdisk_size);
}
}
} else if (!skip_decomp && COMPRESSED(boot.r_fmt)) {
if (boot.hdr->ramdisk_size() != 0) {
int fd = creat(RAMDISK_FILE, 0644);
decompress(boot.r_fmt, fd, boot.ramdisk, boot.hdr->ramdisk_size());
close(fd);
}
} else {
dump(boot.ramdisk, boot.hdr->ramdisk_size(), RAMDISK_FILE);
}
// Dump second
dump(boot.second, boot.hdr->second_size(), SECOND_FILE);
// Dump extra
if (!skip_decomp && COMPRESSED(boot.e_fmt)) {
if (boot.hdr->extra_size() != 0) {
int fd = creat(EXTRA_FILE, 0644);
decompress(boot.e_fmt, fd, boot.extra, boot.hdr->extra_size());
close(fd);
}
} else {
dump(boot.extra, boot.hdr->extra_size(), EXTRA_FILE);
}
// Dump recovery_dtbo
dump(boot.recovery_dtbo, boot.hdr->recovery_dtbo_size(), RECV_DTBO_FILE);
// Dump dtb
dump(boot.dtb, boot.hdr->dtb_size(), DTB_FILE);
// Dump bootconfig
dump(boot.bootconfig, boot.hdr->bootconfig_size(), BOOTCONFIG_FILE);
2021-02-28 12:56:23 -08:00
return boot.flags[CHROMEOS_FLAG] ? 2 : 0;
}
#define file_align_with(page_size) \
2021-11-29 19:56:37 -08:00
write_zero(fd, align_padding(lseek(fd, 0, SEEK_CUR) - off.header, page_size))
#define file_align() file_align_with(boot.hdr->page_size())
2021-02-28 12:56:23 -08:00
void repack(const char *src_img, const char *out_img, bool skip_comp) {
const boot_img boot(src_img);
fprintf(stderr, "Repack to boot image: [%s]\n", out_img);
struct {
uint32_t header;
uint32_t kernel;
uint32_t ramdisk;
uint32_t second;
uint32_t extra;
uint32_t dtb;
2021-01-14 03:55:27 -08:00
uint32_t total;
uint32_t vbmeta;
2021-02-28 12:56:23 -08:00
} off{};
2021-02-28 12:56:23 -08:00
// Create a new boot header and reset sizes
auto hdr = boot.hdr->clone();
hdr->kernel_size() = 0;
hdr->ramdisk_size() = 0;
hdr->second_size() = 0;
hdr->dtb_size() = 0;
hdr->bootconfig_size() = 0;
if (access(HEADER_FILE, R_OK) == 0)
2021-02-28 12:56:23 -08:00
hdr->load_hdr_file();
2021-02-28 12:56:23 -08:00
/***************
* Write blocks
***************/
// Create new image
int fd = creat(out_img, 0644);
2021-02-28 12:56:23 -08:00
if (boot.flags[DHTB_FLAG]) {
// Skip DHTB header
write_zero(fd, sizeof(dhtb_hdr));
2021-02-28 12:56:23 -08:00
} else if (boot.flags[BLOB_FLAG]) {
2023-06-03 03:16:03 -07:00
xwrite(fd, boot.map.buf(), sizeof(blob_hdr));
2021-02-28 12:56:23 -08:00
} else if (boot.flags[NOOKHD_FLAG]) {
2023-06-03 03:16:03 -07:00
xwrite(fd, boot.map.buf(), NOOKHD_PRE_HEADER_SZ);
2021-02-28 12:56:23 -08:00
} else if (boot.flags[ACCLAIM_FLAG]) {
2023-06-03 03:16:03 -07:00
xwrite(fd, boot.map.buf(), ACCLAIM_PRE_HEADER_SZ);
}
2021-02-28 12:56:23 -08:00
// Copy raw header
off.header = lseek(fd, 0, SEEK_CUR);
xwrite(fd, boot.payload.buf(), hdr->hdr_space());
// kernel
off.kernel = lseek(fd, 0, SEEK_CUR);
2021-02-28 12:56:23 -08:00
if (boot.flags[MTK_KERNEL]) {
// Copy MTK headers
xwrite(fd, boot.k_hdr, sizeof(mtk_hdr));
}
2021-07-24 23:04:44 +02:00
if (boot.flags[ZIMAGE_KERNEL]) {
2021-08-28 14:07:34 -07:00
// Copy zImage headers
xwrite(fd, boot.z_hdr, boot.z_info.hdr_sz);
2021-07-24 23:04:44 +02:00
}
if (access(KERNEL_FILE, R_OK) == 0) {
2023-06-03 03:16:03 -07:00
mmap_data m(KERNEL_FILE);
if (!skip_comp && !COMPRESSED_ANY(check_fmt(m.buf(), m.sz())) && COMPRESSED(boot.k_fmt)) {
// Always use zopfli for zImage compression
auto fmt = (boot.flags[ZIMAGE_KERNEL] && boot.k_fmt == GZIP) ? ZOPFLI : boot.k_fmt;
2023-06-03 03:16:03 -07:00
hdr->kernel_size() = compress(fmt, fd, m.buf(), m.sz());
} else {
2023-06-03 03:16:03 -07:00
hdr->kernel_size() = xwrite(fd, m.buf(), m.sz());
}
2021-08-28 17:16:20 -07:00
2021-11-23 13:39:15 -08:00
if (boot.flags[ZIMAGE_KERNEL]) {
if (hdr->kernel_size() > boot.hdr->kernel_size()) {
fprintf(stderr, "! Recompressed kernel is too large, using original kernel\n");
ftruncate64(fd, lseek64(fd, - (off64_t) hdr->kernel_size(), SEEK_CUR));
2021-11-23 13:39:15 -08:00
xwrite(fd, boot.kernel, boot.hdr->kernel_size());
} else if (!skip_comp) {
2021-11-23 13:39:15 -08:00
// Pad zeros to make sure the zImage file size does not change
// Also ensure the last 4 bytes are the uncompressed vmlinux size
2023-06-03 03:16:03 -07:00
uint32_t sz = m.sz();
2021-11-23 13:39:15 -08:00
write_zero(fd, boot.hdr->kernel_size() - hdr->kernel_size() - sizeof(sz));
xwrite(fd, &sz, sizeof(sz));
}
// zImage size shall remain the same
hdr->kernel_size() = boot.hdr->kernel_size();
2021-08-28 17:16:20 -07:00
}
} else if (boot.hdr->kernel_size() != 0) {
xwrite(fd, boot.kernel, boot.hdr->kernel_size());
hdr->kernel_size() = boot.hdr->kernel_size();
}
2021-07-24 23:04:44 +02:00
if (boot.flags[ZIMAGE_KERNEL]) {
2021-11-23 13:39:15 -08:00
// Copy zImage tail and adjust size accordingly
2021-08-28 14:07:34 -07:00
hdr->kernel_size() += boot.z_info.hdr_sz;
hdr->kernel_size() += xwrite(fd, boot.z_info.tail.buf(), boot.z_info.tail.sz());
2021-07-24 23:04:44 +02:00
}
// kernel dtb
if (access(KER_DTB_FILE, R_OK) == 0)
2021-02-28 12:56:23 -08:00
hdr->kernel_size() += restore(fd, KER_DTB_FILE);
file_align();
// ramdisk
off.ramdisk = lseek(fd, 0, SEEK_CUR);
2021-02-28 12:56:23 -08:00
if (boot.flags[MTK_RAMDISK]) {
// Copy MTK headers
xwrite(fd, boot.r_hdr, sizeof(mtk_hdr));
}
using table_entry = vendor_ramdisk_table_entry_v4;
vector<table_entry> ramdisk_table;
2024-07-31 13:52:30 +08:00
if (boot.hdr->vendor_ramdisk_table_size()) {
// Create a copy so we can modify it
auto entry_start = reinterpret_cast<const table_entry *>(boot.vendor_ramdisk_table);
ramdisk_table.insert(
ramdisk_table.begin(),
entry_start, entry_start + boot.hdr->vendor_ramdisk_table_entry_num());
owned_fd dirfd = xopen(VND_RAMDISK_DIR, O_RDONLY | O_CLOEXEC);
uint32_t ramdisk_offset = 0;
for (auto &it : ramdisk_table) {
char file_name[64];
if (it.ramdisk_name[0] == '\0') {
strscpy(file_name, RAMDISK_FILE, sizeof(file_name));
} else {
ssprintf(file_name, sizeof(file_name), "%s.cpio", it.ramdisk_name);
}
mmap_data m(dirfd, file_name);
format_t fmt = check_fmt_lg(boot.ramdisk + it.ramdisk_offset, it.ramdisk_size);
it.ramdisk_offset = ramdisk_offset;
if (!skip_comp && !COMPRESSED_ANY(check_fmt(m.buf(), m.sz())) && COMPRESSED(fmt)) {
it.ramdisk_size = compress(fmt, fd, m.buf(), m.sz());
} else {
it.ramdisk_size = xwrite(fd, m.buf(), m.sz());
}
ramdisk_offset += it.ramdisk_size;
}
hdr->ramdisk_size() = ramdisk_offset;
file_align();
} else if (access(RAMDISK_FILE, R_OK) == 0) {
2023-06-03 03:16:03 -07:00
mmap_data m(RAMDISK_FILE);
auto r_fmt = boot.r_fmt;
2023-06-03 05:10:22 -07:00
if (!skip_comp && !hdr->is_vendor() && hdr->header_version() == 4 && r_fmt != LZ4_LEGACY) {
// A v4 boot image ramdisk will have to be merged with other vendor ramdisks,
// and they have to use the exact same compression method. v4 GKIs are required to
// use lz4 (legacy), so hardcode the format here.
fprintf(stderr, "RAMDISK_FMT: [%s] -> [%s]\n", fmt2name[r_fmt], fmt2name[LZ4_LEGACY]);
r_fmt = LZ4_LEGACY;
}
2023-06-03 03:16:03 -07:00
if (!skip_comp && !COMPRESSED_ANY(check_fmt(m.buf(), m.sz())) && COMPRESSED(r_fmt)) {
hdr->ramdisk_size() = compress(r_fmt, fd, m.buf(), m.sz());
} else {
2023-06-03 03:16:03 -07:00
hdr->ramdisk_size() = xwrite(fd, m.buf(), m.sz());
}
file_align();
}
// second
off.second = lseek(fd, 0, SEEK_CUR);
if (access(SECOND_FILE, R_OK) == 0) {
2021-02-28 12:56:23 -08:00
hdr->second_size() = restore(fd, SECOND_FILE);
file_align();
}
// extra
off.extra = lseek(fd, 0, SEEK_CUR);
if (access(EXTRA_FILE, R_OK) == 0) {
2023-06-03 03:16:03 -07:00
mmap_data m(EXTRA_FILE);
if (!skip_comp && !COMPRESSED_ANY(check_fmt(m.buf(), m.sz())) && COMPRESSED(boot.e_fmt)) {
hdr->extra_size() = compress(boot.e_fmt, fd, m.buf(), m.sz());
} else {
2023-06-03 03:16:03 -07:00
hdr->extra_size() = xwrite(fd, m.buf(), m.sz());
}
file_align();
}
// recovery_dtbo
if (access(RECV_DTBO_FILE, R_OK) == 0) {
2021-02-28 12:56:23 -08:00
hdr->recovery_dtbo_offset() = lseek(fd, 0, SEEK_CUR);
hdr->recovery_dtbo_size() = restore(fd, RECV_DTBO_FILE);
file_align();
}
// dtb
off.dtb = lseek(fd, 0, SEEK_CUR);
if (access(DTB_FILE, R_OK) == 0) {
2021-02-28 12:56:23 -08:00
hdr->dtb_size() = restore(fd, DTB_FILE);
file_align();
}
// Copy boot signature
if (boot.hdr->signature_size()) {
xwrite(fd, boot.signature, boot.hdr->signature_size());
file_align();
}
// vendor ramdisk table
if (!ramdisk_table.empty()) {
xwrite(fd, ramdisk_table.data(), sizeof(table_entry) * ramdisk_table.size());
file_align();
}
// bootconfig
if (access(BOOTCONFIG_FILE, R_OK) == 0) {
hdr->bootconfig_size() = restore(fd, BOOTCONFIG_FILE);
file_align();
2021-11-21 05:55:20 -08:00
}
// Proprietary stuffs
2021-02-28 12:56:23 -08:00
if (boot.flags[SEANDROID_FLAG]) {
xwrite(fd, SEANDROID_MAGIC, 16);
if (boot.flags[DHTB_FLAG]) {
xwrite(fd, "\xFF\xFF\xFF\xFF", 4);
}
} else if (boot.flags[LG_BUMP_FLAG]) {
xwrite(fd, LG_BUMP_MAGIC, 16);
}
2021-01-14 03:55:27 -08:00
off.total = lseek(fd, 0, SEEK_CUR);
file_align();
// vbmeta
if (boot.flags[AVB_FLAG]) {
// According to avbtool.py, if the input is not an Android sparse image
// (which boot images are not), the default block size is 4096
file_align_with(4096);
off.vbmeta = lseek(fd, 0, SEEK_CUR);
uint64_t vbmeta_size = __builtin_bswap64(boot.avb_footer->vbmeta_size);
xwrite(fd, boot.vbmeta, vbmeta_size);
}
2021-01-14 03:55:27 -08:00
// Pad image to original size if not chromeos (as it requires post processing)
2021-02-28 12:56:23 -08:00
if (!boot.flags[CHROMEOS_FLAG]) {
off_t current = lseek(fd, 0, SEEK_CUR);
2023-06-03 03:16:03 -07:00
if (current < boot.map.sz()) {
write_zero(fd, boot.map.sz() - current);
}
}
2021-02-28 12:56:23 -08:00
/******************
* Patch the image
******************/
// Map output image as rw
2023-06-03 03:16:03 -07:00
mmap_data out(out_img, true);
// MTK headers
2021-02-28 12:56:23 -08:00
if (boot.flags[MTK_KERNEL]) {
2023-06-03 03:16:03 -07:00
auto m_hdr = reinterpret_cast<mtk_hdr *>(out.buf() + off.kernel);
2021-02-28 12:56:23 -08:00
m_hdr->size = hdr->kernel_size();
hdr->kernel_size() += sizeof(mtk_hdr);
}
2021-02-28 12:56:23 -08:00
if (boot.flags[MTK_RAMDISK]) {
2023-06-03 03:16:03 -07:00
auto m_hdr = reinterpret_cast<mtk_hdr *>(out.buf() + off.ramdisk);
2021-02-28 12:56:23 -08:00
m_hdr->size = hdr->ramdisk_size();
hdr->ramdisk_size() += sizeof(mtk_hdr);
}
// Make sure header size matches
2021-02-28 12:56:23 -08:00
hdr->header_size() = hdr->hdr_size();
// Update checksum
2021-02-28 12:56:23 -08:00
if (char *id = hdr->id()) {
2023-06-25 03:50:21 +08:00
auto ctx = get_sha(!boot.flags[SHA256_FLAG]);
2021-02-28 12:56:23 -08:00
uint32_t size = hdr->kernel_size();
2023-06-25 03:50:21 +08:00
ctx->update(byte_view(out.buf() + off.kernel, size));
ctx->update(byte_view(&size, sizeof(size)));
2021-02-28 12:56:23 -08:00
size = hdr->ramdisk_size();
2023-06-25 03:50:21 +08:00
ctx->update(byte_view(out.buf() + off.ramdisk, size));
ctx->update(byte_view(&size, sizeof(size)));
2021-02-28 12:56:23 -08:00
size = hdr->second_size();
2023-06-25 03:50:21 +08:00
ctx->update(byte_view(out.buf() + off.second, size));
ctx->update(byte_view(&size, sizeof(size)));
2021-02-28 12:56:23 -08:00
size = hdr->extra_size();
if (size) {
2023-06-25 03:50:21 +08:00
ctx->update(byte_view(out.buf() + off.extra, size));
ctx->update(byte_view(&size, sizeof(size)));
}
2021-02-28 12:56:23 -08:00
uint32_t ver = hdr->header_version();
if (ver == 1 || ver == 2) {
2021-02-28 12:56:23 -08:00
size = hdr->recovery_dtbo_size();
2023-06-25 03:50:21 +08:00
ctx->update(byte_view(out.buf() + hdr->recovery_dtbo_offset(), size));
ctx->update(byte_view(&size, sizeof(size)));
}
if (ver == 2) {
2021-02-28 12:56:23 -08:00
size = hdr->dtb_size();
2023-06-25 03:50:21 +08:00
ctx->update(byte_view(out.buf() + off.dtb, size));
ctx->update(byte_view(&size, sizeof(size)));
}
memset(id, 0, BOOT_ID_SIZE);
2023-06-30 15:50:52 -07:00
ctx->finalize_into(byte_data(id, ctx->output_size()));
}
2021-02-28 12:56:23 -08:00
// Print new header info
hdr->print();
2021-02-28 12:56:23 -08:00
// Copy main header
if (boot.flags[AMONET_FLAG]) {
auto real_hdr_sz = std::min(hdr->hdr_space() - AMONET_MICROLOADER_SZ, hdr->hdr_size());
2023-06-03 03:16:03 -07:00
memcpy(out.buf() + off.header + AMONET_MICROLOADER_SZ, hdr->raw_hdr(), real_hdr_sz);
} else {
2023-06-03 03:16:03 -07:00
memcpy(out.buf() + off.header, hdr->raw_hdr(), hdr->hdr_size());
}
if (boot.flags[AVB_FLAG]) {
// Copy and patch AVB structures
2023-06-03 03:16:03 -07:00
auto footer = reinterpret_cast<AvbFooter*>(out.buf() + out.sz() - sizeof(AvbFooter));
memcpy(footer, boot.avb_footer, sizeof(AvbFooter));
footer->original_image_size = __builtin_bswap64(off.total);
footer->vbmeta_offset = __builtin_bswap64(off.vbmeta);
if (check_env("PATCHVBMETAFLAG")) {
auto vbmeta = reinterpret_cast<AvbVBMetaImageHeader*>(out.buf() + off.vbmeta);
vbmeta->flags = __builtin_bswap32(3);
}
}
2021-02-28 12:56:23 -08:00
if (boot.flags[DHTB_FLAG]) {
// DHTB header
2023-06-03 03:16:03 -07:00
auto d_hdr = reinterpret_cast<dhtb_hdr *>(out.buf());
2021-02-28 12:56:23 -08:00
memcpy(d_hdr, DHTB_MAGIC, 8);
d_hdr->size = off.total - sizeof(dhtb_hdr);
2023-06-30 15:50:52 -07:00
sha256_hash(byte_view(out.buf() + sizeof(dhtb_hdr), d_hdr->size),
byte_data(d_hdr->checksum, 32));
2021-02-28 12:56:23 -08:00
} else if (boot.flags[BLOB_FLAG]) {
// Blob header
2023-06-03 03:16:03 -07:00
auto b_hdr = reinterpret_cast<blob_hdr *>(out.buf());
2021-02-28 12:56:23 -08:00
b_hdr->size = off.total - sizeof(blob_hdr);
}
// Sign the image after we finish patching the boot image
if (boot.flags[AVB1_SIGNED_FLAG]) {
byte_view payload(out.buf() + off.header, off.total - off.header);
auto sig = rust::sign_boot_image(payload, "/boot", nullptr, nullptr);
if (!sig.empty()) {
lseek(fd, off.total, SEEK_SET);
xwrite(fd, sig.data(), sig.size());
}
}
close(fd);
}
int verify(const char *image, const char *cert) {
const boot_img boot(image);
if (cert == nullptr) {
// Boot image parsing already checks if the image is signed
return boot.flags[AVB1_SIGNED_FLAG] ? 0 : 1;
} else {
// Provide a custom certificate and re-verify
return boot.verify(cert) ? 0 : 1;
}
}
int sign(const char *image, const char *name, const char *cert, const char *key) {
const boot_img boot(image);
auto sig = rust::sign_boot_image(boot.payload, name, cert, key);
if (sig.empty())
return 1;
auto eof = boot.tail.buf() - boot.map.buf();
int fd = xopen(image, O_WRONLY | O_CLOEXEC);
if (lseek(fd, eof, SEEK_SET) != eof || xwrite(fd, sig.data(), sig.size()) != sig.size()) {
close(fd);
return 1;
}
if (auto off = lseek(fd, 0, SEEK_CUR); off < boot.map.sz()) {
// Wipe out rest of tail
write_zero(fd, boot.map.sz() - off);
}
close(fd);
return 0;
}