2019-03-07 13:24:06 +01:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <string.h>
|
2018-10-20 05:10:47 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libfdt.h>
|
2019-03-07 13:24:06 +01:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
2018-10-20 05:10:47 +02:00
|
|
|
|
|
|
|
#include <mincrypt/sha.h>
|
|
|
|
#include <mincrypt/sha256.h>
|
2019-02-10 09:57:51 +01:00
|
|
|
#include <utils.h>
|
|
|
|
#include <logging.h>
|
2018-10-20 05:10:47 +02:00
|
|
|
|
|
|
|
#include "bootimg.h"
|
|
|
|
#include "magiskboot.h"
|
2019-02-21 02:49:26 +01:00
|
|
|
#include "compress.h"
|
2018-10-20 05:10:47 +02:00
|
|
|
|
2019-03-07 13:24:06 +01:00
|
|
|
using namespace std;
|
|
|
|
|
2019-02-21 10:14:52 +01:00
|
|
|
uint32_t dyn_img_hdr::j32 = 0;
|
|
|
|
uint64_t dyn_img_hdr::j64 = 0;
|
|
|
|
|
2019-03-07 13:24:06 +01:00
|
|
|
static int64_t one_step(unique_ptr<Compression> &&ptr, int fd, const void *in, size_t size) {
|
2019-09-26 05:55:39 +02:00
|
|
|
ptr->setOut(make_unique<FDOutStream>(fd));
|
2019-03-07 13:24:06 +01:00
|
|
|
if (!ptr->write(in, size))
|
|
|
|
return -1;
|
|
|
|
return ptr->finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t decompress(format_t type, int fd, const void *in, size_t size) {
|
|
|
|
return one_step(unique_ptr<Compression>(get_decoder(type)), fd, in, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t compress(format_t type, int fd, const void *in, size_t size) {
|
|
|
|
return one_step(unique_ptr<Compression>(get_encoder(type)), fd, in, size);
|
|
|
|
}
|
|
|
|
|
2018-10-20 05:10:47 +02:00
|
|
|
static void dump(void *buf, size_t size, const char *filename) {
|
|
|
|
if (size == 0)
|
|
|
|
return;
|
|
|
|
int fd = creat(filename, 0644);
|
|
|
|
xwrite(fd, buf, size);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t restore(const char *filename, int fd) {
|
|
|
|
int ifd = xopen(filename, O_RDONLY);
|
|
|
|
size_t size = lseek(ifd, 0, SEEK_END);
|
|
|
|
lseek(ifd, 0, SEEK_SET);
|
2019-02-21 08:54:37 +01:00
|
|
|
xsendfile(fd, ifd, nullptr, size);
|
2018-10-20 05:10:47 +02:00
|
|
|
close(ifd);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void restore_buf(int fd, const void *buf, size_t size) {
|
|
|
|
xwrite(fd, buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_img::~boot_img() {
|
|
|
|
munmap(map_addr, map_size);
|
2019-09-17 11:01:04 +02:00
|
|
|
delete hdr;
|
2018-10-20 05:10:47 +02:00
|
|
|
delete k_hdr;
|
|
|
|
delete r_hdr;
|
|
|
|
delete b_hdr;
|
|
|
|
}
|
|
|
|
|
2019-02-24 08:30:04 +01:00
|
|
|
#define UNSUPP_RET 1
|
|
|
|
#define CHROME_RET 2
|
2019-02-21 11:08:38 +01:00
|
|
|
int boot_img::parse_file(const char *image) {
|
2019-02-25 05:09:34 +01:00
|
|
|
mmap_ro(image, map_addr, map_size);
|
2018-10-20 05:10:47 +02:00
|
|
|
fprintf(stderr, "Parsing boot image: [%s]\n", image);
|
|
|
|
for (uint8_t *head = map_addr; head < map_addr + map_size; ++head) {
|
|
|
|
switch (check_fmt(head, map_size)) {
|
|
|
|
case CHROMEOS:
|
|
|
|
// The caller should know it's chromeos, as it needs additional signing
|
|
|
|
flags |= CHROMEOS_FLAG;
|
|
|
|
break;
|
|
|
|
case DHTB:
|
|
|
|
flags |= DHTB_FLAG;
|
|
|
|
flags |= SEANDROID_FLAG;
|
|
|
|
fprintf(stderr, "DHTB_HDR\n");
|
|
|
|
break;
|
|
|
|
case BLOB:
|
|
|
|
flags |= BLOB_FLAG;
|
|
|
|
fprintf(stderr, "TEGRA_BLOB\n");
|
|
|
|
b_hdr = new blob_hdr();
|
|
|
|
memcpy(b_hdr, head, sizeof(blob_hdr));
|
2019-02-21 11:08:38 +01:00
|
|
|
head += sizeof(blob_hdr) - 1;
|
2018-10-20 05:10:47 +02:00
|
|
|
break;
|
|
|
|
case AOSP:
|
2019-02-21 11:08:38 +01:00
|
|
|
return parse_image(head);
|
|
|
|
|
2019-02-21 11:24:05 +01:00
|
|
|
default:
|
|
|
|
break;
|
2018-10-20 05:10:47 +02:00
|
|
|
}
|
|
|
|
}
|
2019-02-24 08:30:04 +01:00
|
|
|
exit(UNSUPP_RET);
|
2018-10-20 05:10:47 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 11:01:04 +02:00
|
|
|
#define pos_align() pos = do_align(pos, hdr->page_size())
|
2019-02-21 11:08:38 +01:00
|
|
|
int boot_img::parse_image(uint8_t *head) {
|
2019-09-17 11:01:04 +02:00
|
|
|
auto hp = reinterpret_cast<boot_img_hdr*>(head);
|
2019-02-21 11:08:38 +01:00
|
|
|
if (hp->page_size >= 0x02000000) {
|
|
|
|
fprintf(stderr, "PXA_BOOT_HDR\n");
|
2019-09-17 11:01:04 +02:00
|
|
|
hdr = new dyn_img_pxa(head);
|
2019-02-21 11:08:38 +01:00
|
|
|
} else {
|
2019-05-20 02:18:18 +02:00
|
|
|
if (memcmp(hp->cmdline, NOOKHD_RL_MAGIC, 10) == 0 ||
|
|
|
|
memcmp(hp->cmdline, NOOKHD_GL_MAGIC, 12) == 0 ||
|
|
|
|
memcmp(hp->cmdline, NOOKHD_GR_MAGIC, 14) == 0 ||
|
|
|
|
memcmp(hp->cmdline, NOOKHD_EB_MAGIC, 26) == 0 ||
|
|
|
|
memcmp(hp->cmdline, NOOKHD_ER_MAGIC, 30) == 0) {
|
2019-02-21 11:08:38 +01:00
|
|
|
flags |= NOOKHD_FLAG;
|
2019-05-20 02:18:18 +02:00
|
|
|
fprintf(stderr, "NOOKHD_LOADER\n");
|
2019-02-21 11:08:38 +01:00
|
|
|
head += NOOKHD_PRE_HEADER_SZ;
|
|
|
|
} else if (memcmp(hp->name, ACCLAIM_MAGIC, 10) == 0) {
|
|
|
|
flags |= ACCLAIM_FLAG;
|
2019-05-20 02:18:18 +02:00
|
|
|
fprintf(stderr, "ACCLAIM_LOADER\n");
|
2019-02-21 11:08:38 +01:00
|
|
|
head += ACCLAIM_PRE_HEADER_SZ;
|
|
|
|
}
|
2019-09-17 11:01:04 +02:00
|
|
|
|
|
|
|
if (hp->header_version == 1)
|
|
|
|
hdr = new dyn_img_v1(head);
|
|
|
|
else if (hp->header_version == 2)
|
|
|
|
hdr = new dyn_img_v2(head);
|
|
|
|
else
|
|
|
|
hdr = new dyn_img_v0(head);
|
2019-02-21 11:08:38 +01:00
|
|
|
}
|
|
|
|
|
2019-09-17 11:01:04 +02:00
|
|
|
size_t pos = hdr->page_size();
|
2019-02-21 11:08:38 +01:00
|
|
|
|
2019-09-17 11:01:04 +02:00
|
|
|
flags |= hdr->id()[SHA_DIGEST_SIZE] ? SHA256_FLAG : 0;
|
2019-02-21 11:08:38 +01:00
|
|
|
|
|
|
|
print_hdr();
|
|
|
|
|
|
|
|
kernel = head + pos;
|
2019-09-17 11:01:04 +02:00
|
|
|
pos += hdr->kernel_size();
|
2019-02-21 11:08:38 +01:00
|
|
|
pos_align();
|
|
|
|
|
|
|
|
ramdisk = head + pos;
|
2019-09-17 11:01:04 +02:00
|
|
|
pos += hdr->ramdisk_size();
|
2019-02-21 11:08:38 +01:00
|
|
|
pos_align();
|
|
|
|
|
|
|
|
second = head + pos;
|
2019-09-17 11:01:04 +02:00
|
|
|
pos += hdr->second_size();
|
2019-02-21 11:08:38 +01:00
|
|
|
pos_align();
|
|
|
|
|
|
|
|
extra = head + pos;
|
2019-09-17 11:01:04 +02:00
|
|
|
pos += hdr->extra_size();
|
2019-02-21 11:08:38 +01:00
|
|
|
pos_align();
|
|
|
|
|
|
|
|
recov_dtbo = head + pos;
|
2019-09-17 11:01:04 +02:00
|
|
|
pos += hdr->recovery_dtbo_size();
|
2019-02-21 11:08:38 +01:00
|
|
|
pos_align();
|
|
|
|
|
2019-03-13 21:51:22 +01:00
|
|
|
dtb = head + pos;
|
2019-09-17 11:01:04 +02:00
|
|
|
pos += hdr->dtb_size();
|
2019-03-13 21:51:22 +01:00
|
|
|
pos_align();
|
|
|
|
|
2019-02-21 11:08:38 +01:00
|
|
|
if (head + pos < map_addr + map_size) {
|
|
|
|
tail = head + pos;
|
|
|
|
tail_size = map_size - (tail - map_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check tail info, currently only for LG Bump and Samsung SEANDROIDENFORCE
|
|
|
|
if (tail_size >= 16 && memcmp(tail, SEANDROID_MAGIC, 16) == 0) {
|
|
|
|
flags |= SEANDROID_FLAG;
|
|
|
|
} else if (tail_size >= 16 && memcmp(tail, LG_BUMP_MAGIC, 16) == 0) {
|
|
|
|
flags |= LG_BUMP_FLAG;
|
|
|
|
}
|
|
|
|
|
|
|
|
find_dtb();
|
|
|
|
|
2019-09-17 11:01:04 +02:00
|
|
|
k_fmt = check_fmt(kernel, hdr->kernel_size());
|
|
|
|
r_fmt = check_fmt(ramdisk, hdr->ramdisk_size());
|
2019-02-21 11:08:38 +01:00
|
|
|
|
|
|
|
// Check MTK
|
|
|
|
if (k_fmt == MTK) {
|
|
|
|
fprintf(stderr, "MTK_KERNEL_HDR\n");
|
|
|
|
flags |= MTK_KERNEL;
|
|
|
|
k_hdr = new mtk_hdr();
|
|
|
|
memcpy(k_hdr, kernel, sizeof(mtk_hdr));
|
|
|
|
fprintf(stderr, "KERNEL [%u]\n", k_hdr->size);
|
|
|
|
fprintf(stderr, "NAME [%s]\n", k_hdr->name);
|
|
|
|
kernel += 512;
|
2019-09-17 11:01:04 +02:00
|
|
|
hdr->kernel_size() -= 512;
|
|
|
|
k_fmt = check_fmt(kernel, hdr->kernel_size());
|
2019-02-21 11:08:38 +01:00
|
|
|
}
|
|
|
|
if (r_fmt == MTK) {
|
|
|
|
fprintf(stderr, "MTK_RAMDISK_HDR\n");
|
|
|
|
flags |= MTK_RAMDISK;
|
|
|
|
r_hdr = new mtk_hdr();
|
|
|
|
memcpy(r_hdr, ramdisk, sizeof(mtk_hdr));
|
|
|
|
fprintf(stderr, "RAMDISK [%u]\n", r_hdr->size);
|
|
|
|
fprintf(stderr, "NAME [%s]\n", r_hdr->name);
|
|
|
|
ramdisk += 512;
|
2019-09-17 11:01:04 +02:00
|
|
|
hdr->ramdisk_size() -= 512;
|
|
|
|
r_fmt = check_fmt(ramdisk, hdr->ramdisk_size());
|
2019-02-21 11:08:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "KERNEL_FMT [%s]\n", fmt2name[k_fmt]);
|
|
|
|
fprintf(stderr, "RAMDISK_FMT [%s]\n", fmt2name[r_fmt]);
|
|
|
|
|
2019-02-24 08:30:04 +01:00
|
|
|
return (flags & CHROMEOS_FLAG) ? CHROME_RET : 0;
|
2019-02-21 11:08:38 +01:00
|
|
|
}
|
|
|
|
|
2018-10-20 05:10:47 +02:00
|
|
|
void boot_img::find_dtb() {
|
2019-09-17 11:01:04 +02:00
|
|
|
for (uint32_t i = 0; i < hdr->kernel_size(); ++i) {
|
2019-02-21 11:24:05 +01:00
|
|
|
auto fdt_hdr = reinterpret_cast<fdt_header *>(kernel + i);
|
|
|
|
if (fdt32_to_cpu(fdt_hdr->magic) != FDT_MAGIC)
|
2018-10-20 05:10:47 +02:00
|
|
|
continue;
|
2019-02-21 11:24:05 +01:00
|
|
|
|
2018-10-20 05:10:47 +02:00
|
|
|
// Check that fdt_header.totalsize does not overflow kernel image size
|
2019-02-21 11:24:05 +01:00
|
|
|
uint32_t totalsize = fdt32_to_cpu(fdt_hdr->totalsize);
|
2019-09-17 11:01:04 +02:00
|
|
|
if (totalsize > hdr->kernel_size() - i) {
|
2018-10-20 05:10:47 +02:00
|
|
|
fprintf(stderr, "Invalid DTB detection at 0x%x: size (%u) > remaining (%u)\n",
|
2019-09-17 11:01:04 +02:00
|
|
|
i, totalsize, hdr->kernel_size() - i);
|
2018-10-20 05:10:47 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that fdt_header.off_dt_struct does not overflow kernel image size
|
2019-02-21 11:24:05 +01:00
|
|
|
uint32_t off_dt_struct = fdt32_to_cpu(fdt_hdr->off_dt_struct);
|
2019-09-17 11:01:04 +02:00
|
|
|
if (off_dt_struct > hdr->kernel_size() - i) {
|
2018-10-20 05:10:47 +02:00
|
|
|
fprintf(stderr, "Invalid DTB detection at 0x%x: "
|
|
|
|
"struct offset (%u) > remaining (%u)\n",
|
2019-09-17 11:01:04 +02:00
|
|
|
i, off_dt_struct, hdr->kernel_size() - i);
|
2018-10-20 05:10:47 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that fdt_node_header.tag of first node is FDT_BEGIN_NODE
|
2019-02-21 11:24:05 +01:00
|
|
|
auto fdt_node_hdr = reinterpret_cast<fdt_node_header *>(kernel + i + off_dt_struct);
|
|
|
|
if (fdt32_to_cpu(fdt_node_hdr->tag) != FDT_BEGIN_NODE) {
|
2018-10-20 05:10:47 +02:00
|
|
|
fprintf(stderr, "Invalid DTB detection at 0x%x: "
|
|
|
|
"header tag of first node != FDT_BEGIN_NODE\n", i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-03-13 21:51:22 +01:00
|
|
|
kernel_dtb = kernel + i;
|
2019-09-17 11:01:04 +02:00
|
|
|
kernel_dt_size = hdr->kernel_size() - i;
|
|
|
|
hdr->kernel_size() = i;
|
2019-03-13 21:51:22 +01:00
|
|
|
fprintf(stderr, "KERNEL_DTB [%u]\n", kernel_dt_size);
|
2018-10-20 05:10:47 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void boot_img::print_hdr() {
|
2019-09-17 11:11:09 +02:00
|
|
|
uint32_t ver = hdr->header_version();
|
|
|
|
fprintf(stderr, "HEADER_VER [%u]\n", ver);
|
2019-09-17 11:01:04 +02:00
|
|
|
fprintf(stderr, "KERNEL_SZ [%u]\n", hdr->kernel_size());
|
|
|
|
fprintf(stderr, "RAMDISK_SZ [%u]\n", hdr->ramdisk_size());
|
|
|
|
fprintf(stderr, "SECOND_SZ [%u]\n", hdr->second_size());
|
2019-09-17 11:11:09 +02:00
|
|
|
if (ver) {
|
|
|
|
fprintf(stderr, "RECOV_DTBO_SZ [%u]\n", hdr->recovery_dtbo_size());
|
|
|
|
fprintf(stderr, "DTB [%u]\n", hdr->dtb_size());
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "EXTRA_SZ [%u]\n", hdr->extra_size());
|
|
|
|
}
|
2019-09-17 11:01:04 +02:00
|
|
|
|
2019-09-17 11:11:09 +02:00
|
|
|
ver = hdr->os_version();
|
2018-10-20 05:10:47 +02:00
|
|
|
if (ver) {
|
|
|
|
int a,b,c,y,m = 0;
|
|
|
|
int version, patch_level;
|
|
|
|
version = ver >> 11;
|
|
|
|
patch_level = ver & 0x7ff;
|
|
|
|
|
|
|
|
a = (version >> 14) & 0x7f;
|
|
|
|
b = (version >> 7) & 0x7f;
|
|
|
|
c = version & 0x7f;
|
2018-11-01 06:16:15 +01:00
|
|
|
fprintf(stderr, "OS_VERSION [%d.%d.%d]\n", a, b, c);
|
2018-10-20 05:10:47 +02:00
|
|
|
|
|
|
|
y = (patch_level >> 4) + 2000;
|
|
|
|
m = patch_level & 0xf;
|
2019-03-08 03:59:03 +01:00
|
|
|
fprintf(stderr, "OS_PATCH_LEVEL [%d-%02d]\n", y, m);
|
2018-10-20 05:10:47 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 11:01:04 +02:00
|
|
|
fprintf(stderr, "PAGESIZE [%u]\n", hdr->page_size());
|
|
|
|
fprintf(stderr, "NAME [%s]\n", hdr->name());
|
|
|
|
fprintf(stderr, "CMDLINE [%.512s%.1024s]\n", hdr->cmdline(), hdr->extra_cmdline());
|
2018-11-01 06:16:15 +01:00
|
|
|
fprintf(stderr, "CHECKSUM [");
|
2019-09-17 11:01:04 +02:00
|
|
|
for (int i = 0; hdr->id()[i]; ++i)
|
|
|
|
fprintf(stderr, "%02x", hdr->id()[i]);
|
2018-10-20 05:10:47 +02:00
|
|
|
fprintf(stderr, "]\n");
|
|
|
|
}
|
|
|
|
|
2019-03-08 03:32:01 +01:00
|
|
|
int unpack(const char *image, bool hdr) {
|
2018-11-06 11:02:30 +01:00
|
|
|
boot_img boot {};
|
2019-02-21 11:08:38 +01:00
|
|
|
int ret = boot.parse_file(image);
|
2018-10-20 05:10:47 +02:00
|
|
|
int fd;
|
|
|
|
|
2019-03-08 03:32:01 +01:00
|
|
|
if (hdr) {
|
|
|
|
FILE *fp = xfopen(HEADER_FILE, "w");
|
2019-09-17 11:01:04 +02:00
|
|
|
fprintf(fp, "pagesize=%u\n", boot.hdr->page_size());
|
|
|
|
fprintf(fp, "name=%s\n", boot.hdr->name());
|
|
|
|
fprintf(fp, "cmdline=%.512s%.1024s\n", boot.hdr->cmdline(), boot.hdr->extra_cmdline());
|
|
|
|
uint32_t ver = boot.hdr->os_version();
|
2019-03-08 03:32:01 +01:00
|
|
|
if (ver) {
|
|
|
|
int a, b, c, y, m = 0;
|
|
|
|
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;
|
2019-03-08 03:59:03 +01:00
|
|
|
fprintf(fp, "os_patch_level=%d-%02d\n", y, m);
|
2019-03-08 03:32:01 +01:00
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2018-10-20 05:10:47 +02:00
|
|
|
// Dump kernel
|
|
|
|
if (COMPRESSED(boot.k_fmt)) {
|
|
|
|
fd = creat(KERNEL_FILE, 0644);
|
2019-09-17 11:01:04 +02:00
|
|
|
decompress(boot.k_fmt, fd, boot.kernel, boot.hdr->kernel_size());
|
2018-10-20 05:10:47 +02:00
|
|
|
close(fd);
|
|
|
|
} else {
|
2019-05-28 17:47:52 +02:00
|
|
|
fprintf(stderr, "Kernel is uncompressed or not a supported compressed type!\n");
|
2019-09-17 11:01:04 +02:00
|
|
|
dump(boot.kernel, boot.hdr->kernel_size(), KERNEL_FILE);
|
2018-10-20 05:10:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dump dtb
|
2019-03-13 21:51:22 +01:00
|
|
|
dump(boot.kernel_dtb, boot.kernel_dt_size, KER_DTB_FILE);
|
2018-10-20 05:10:47 +02:00
|
|
|
|
|
|
|
// Dump ramdisk
|
|
|
|
if (COMPRESSED(boot.r_fmt)) {
|
|
|
|
fd = creat(RAMDISK_FILE, 0644);
|
2019-09-17 11:01:04 +02:00
|
|
|
decompress(boot.r_fmt, fd, boot.ramdisk, boot.hdr->ramdisk_size());
|
2018-10-20 05:10:47 +02:00
|
|
|
close(fd);
|
|
|
|
} else {
|
2019-05-28 17:47:52 +02:00
|
|
|
fprintf(stderr, "Ramdisk is uncompressed or not a supported compressed type!\n");
|
2019-09-17 11:01:04 +02:00
|
|
|
dump(boot.ramdisk, boot.hdr->ramdisk_size(), RAMDISK_FILE);
|
2018-10-20 05:10:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dump second
|
2019-09-17 11:01:04 +02:00
|
|
|
dump(boot.second, boot.hdr->second_size(), SECOND_FILE);
|
2018-10-20 05:10:47 +02:00
|
|
|
|
|
|
|
// Dump extra
|
2019-09-17 11:01:04 +02:00
|
|
|
dump(boot.extra, boot.hdr->extra_size(), EXTRA_FILE);
|
2018-10-20 05:10:47 +02:00
|
|
|
|
|
|
|
// Dump recovery_dtbo
|
2019-09-17 11:01:04 +02:00
|
|
|
dump(boot.recov_dtbo, boot.hdr->recovery_dtbo_size(), RECV_DTBO_FILE);
|
2019-03-13 21:51:22 +01:00
|
|
|
|
|
|
|
// Dump dtb
|
2019-09-17 11:01:04 +02:00
|
|
|
dump(boot.dtb, boot.hdr->dtb_size(), DTB_FILE);
|
2018-10-20 05:10:47 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-09-17 11:01:04 +02:00
|
|
|
#define file_align() \
|
|
|
|
write_zero(fd, align_off(lseek(fd, 0, SEEK_CUR) - header_off, boot.hdr->page_size()))
|
|
|
|
|
2019-06-07 20:56:20 +02:00
|
|
|
void repack(const char* orig_image, const char* out_image, bool force_nocomp) {
|
2018-11-06 11:02:30 +01:00
|
|
|
boot_img boot {};
|
2018-10-20 05:10:47 +02:00
|
|
|
|
2019-05-21 01:57:49 +02:00
|
|
|
off_t header_off, kernel_off, ramdisk_off, second_off, extra_off, dtb_off;
|
2018-10-20 05:10:47 +02:00
|
|
|
|
|
|
|
// Parse original image
|
2019-02-21 11:08:38 +01:00
|
|
|
boot.parse_file(orig_image);
|
2018-10-20 05:10:47 +02:00
|
|
|
|
|
|
|
// Reset sizes
|
2019-09-17 11:01:04 +02:00
|
|
|
boot.hdr->kernel_size() = 0;
|
|
|
|
boot.hdr->ramdisk_size() = 0;
|
|
|
|
boot.hdr->second_size() = 0;
|
|
|
|
boot.hdr->dtb_size() = 0;
|
2019-03-13 21:51:22 +01:00
|
|
|
boot.kernel_dt_size = 0;
|
2018-10-20 05:10:47 +02:00
|
|
|
|
|
|
|
fprintf(stderr, "Repack to boot image: [%s]\n", out_image);
|
|
|
|
|
|
|
|
// Create new image
|
|
|
|
int fd = creat(out_image, 0644);
|
|
|
|
|
|
|
|
if (boot.flags & DHTB_FLAG) {
|
|
|
|
// Skip DHTB header
|
|
|
|
write_zero(fd, 512);
|
|
|
|
} else if (boot.flags & BLOB_FLAG) {
|
|
|
|
// Skip blob header
|
|
|
|
write_zero(fd, sizeof(blob_hdr));
|
|
|
|
} else if (boot.flags & NOOKHD_FLAG) {
|
|
|
|
restore_buf(fd, boot.map_addr, NOOKHD_PRE_HEADER_SZ);
|
|
|
|
} else if (boot.flags & ACCLAIM_FLAG) {
|
|
|
|
restore_buf(fd, boot.map_addr, ACCLAIM_PRE_HEADER_SZ);
|
|
|
|
}
|
|
|
|
|
2019-03-08 03:32:01 +01:00
|
|
|
// header
|
|
|
|
if (access(HEADER_FILE, R_OK) == 0) {
|
|
|
|
parse_prop_file(HEADER_FILE, [&](string_view key, string_view value) -> bool {
|
|
|
|
if (key == "page_size") {
|
2019-09-17 11:01:04 +02:00
|
|
|
boot.hdr->page_size() = parse_int(value);
|
2019-03-08 03:32:01 +01:00
|
|
|
} else if (key == "name") {
|
2019-09-17 11:01:04 +02:00
|
|
|
memset(boot.hdr->name(), 0, 16);
|
|
|
|
memcpy(boot.hdr->name(), value.data(), value.length() > 15 ? 15 : value.length());
|
2019-03-08 03:32:01 +01:00
|
|
|
} else if (key == "cmdline") {
|
2019-09-17 11:01:04 +02:00
|
|
|
memset(boot.hdr->cmdline(), 0, 512);
|
|
|
|
memset(boot.hdr->extra_cmdline(), 0, 1024);
|
2019-03-08 03:32:01 +01:00
|
|
|
if (value.length() > 512) {
|
2019-09-17 11:01:04 +02:00
|
|
|
memcpy(boot.hdr->cmdline(), value.data(), 512);
|
|
|
|
memcpy(boot.hdr->extra_cmdline(), &value[512], value.length() - 511);
|
2019-03-08 03:32:01 +01:00
|
|
|
} else {
|
2019-09-17 11:01:04 +02:00
|
|
|
memcpy(boot.hdr->cmdline(), value.data(), value.length());
|
2019-03-08 03:32:01 +01:00
|
|
|
}
|
|
|
|
} else if (key == "os_version") {
|
2019-09-17 11:01:04 +02:00
|
|
|
int patch_level = boot.hdr->os_version() & 0x7ff;
|
2019-03-08 03:32:01 +01:00
|
|
|
int a, b, c;
|
|
|
|
sscanf(value.data(), "%d.%d.%d", &a, &b, &c);
|
2019-09-17 11:01:04 +02:00
|
|
|
boot.hdr->os_version() = (((a << 14) | (b << 7) | c) << 11) | patch_level;
|
2019-03-08 03:59:03 +01:00
|
|
|
} else if (key == "os_patch_level") {
|
2019-09-17 11:01:04 +02:00
|
|
|
int os_version = boot.hdr->os_version() >> 11;
|
2019-03-08 03:32:01 +01:00
|
|
|
int y, m;
|
|
|
|
sscanf(value.data(), "%d-%d", &y, &m);
|
|
|
|
y -= 2000;
|
2019-09-17 11:01:04 +02:00
|
|
|
boot.hdr->os_version() = (os_version << 11) | (y << 4) | m;
|
2019-03-08 03:32:01 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-20 05:10:47 +02:00
|
|
|
// Skip a page for header
|
|
|
|
header_off = lseek(fd, 0, SEEK_CUR);
|
2019-09-17 11:01:04 +02:00
|
|
|
write_zero(fd, boot.hdr->page_size());
|
2018-10-20 05:10:47 +02:00
|
|
|
|
|
|
|
// kernel
|
|
|
|
kernel_off = lseek(fd, 0, SEEK_CUR);
|
|
|
|
if (boot.flags & MTK_KERNEL) {
|
|
|
|
// Skip MTK header
|
|
|
|
write_zero(fd, 512);
|
|
|
|
}
|
|
|
|
if (access(KERNEL_FILE, R_OK) == 0) {
|
2019-03-07 13:24:06 +01:00
|
|
|
size_t raw_size;
|
|
|
|
void *raw_buf;
|
|
|
|
mmap_ro(KERNEL_FILE, raw_buf, raw_size);
|
2019-06-06 21:56:17 +02:00
|
|
|
if (!COMPRESSED_ANY(check_fmt(raw_buf, raw_size)) && COMPRESSED(boot.k_fmt)) {
|
2019-09-17 11:01:04 +02:00
|
|
|
boot.hdr->kernel_size() = compress(boot.k_fmt, fd, raw_buf, raw_size);
|
2018-10-20 05:10:47 +02:00
|
|
|
} else {
|
2019-09-17 11:01:04 +02:00
|
|
|
boot.hdr->kernel_size() = write(fd, raw_buf, raw_size);
|
2018-10-20 05:10:47 +02:00
|
|
|
}
|
2019-03-07 13:24:06 +01:00
|
|
|
munmap(raw_buf, raw_size);
|
2018-10-20 05:10:47 +02:00
|
|
|
}
|
|
|
|
|
2019-03-13 21:51:22 +01:00
|
|
|
// kernel dtb
|
|
|
|
if (access(KER_DTB_FILE, R_OK) == 0)
|
2019-09-17 11:01:04 +02:00
|
|
|
boot.hdr->kernel_size() += restore(KER_DTB_FILE, fd);
|
2018-10-20 05:10:47 +02:00
|
|
|
file_align();
|
|
|
|
|
|
|
|
// ramdisk
|
|
|
|
ramdisk_off = lseek(fd, 0, SEEK_CUR);
|
|
|
|
if (boot.flags & MTK_RAMDISK) {
|
|
|
|
// Skip MTK header
|
|
|
|
write_zero(fd, 512);
|
|
|
|
}
|
|
|
|
if (access(RAMDISK_FILE, R_OK) == 0) {
|
2019-03-07 13:24:06 +01:00
|
|
|
size_t raw_size;
|
|
|
|
void *raw_buf;
|
|
|
|
mmap_ro(RAMDISK_FILE, raw_buf, raw_size);
|
2019-06-07 20:56:20 +02:00
|
|
|
if (!COMPRESSED_ANY(check_fmt(raw_buf, raw_size)) && COMPRESSED(boot.r_fmt) && !force_nocomp) {
|
2019-09-17 11:01:04 +02:00
|
|
|
boot.hdr->ramdisk_size() = compress(boot.r_fmt, fd, raw_buf, raw_size);
|
2018-10-20 05:10:47 +02:00
|
|
|
} else {
|
2019-09-17 11:01:04 +02:00
|
|
|
boot.hdr->ramdisk_size() = write(fd, raw_buf, raw_size);
|
2018-10-20 05:10:47 +02:00
|
|
|
}
|
2019-03-07 13:24:06 +01:00
|
|
|
munmap(raw_buf, raw_size);
|
2018-10-20 05:10:47 +02:00
|
|
|
file_align();
|
|
|
|
}
|
|
|
|
|
|
|
|
// second
|
|
|
|
second_off = lseek(fd, 0, SEEK_CUR);
|
|
|
|
if (access(SECOND_FILE, R_OK) == 0) {
|
2019-09-17 11:01:04 +02:00
|
|
|
boot.hdr->second_size() = restore(SECOND_FILE, fd);
|
2018-10-20 05:10:47 +02:00
|
|
|
file_align();
|
|
|
|
}
|
|
|
|
|
|
|
|
// extra
|
|
|
|
extra_off = lseek(fd, 0, SEEK_CUR);
|
|
|
|
if (access(EXTRA_FILE, R_OK) == 0) {
|
2019-09-17 11:01:04 +02:00
|
|
|
boot.hdr->extra_size() = restore(EXTRA_FILE, fd);
|
2018-10-20 05:10:47 +02:00
|
|
|
file_align();
|
|
|
|
}
|
|
|
|
|
|
|
|
// recovery_dtbo
|
|
|
|
if (access(RECV_DTBO_FILE, R_OK) == 0) {
|
2019-09-17 11:01:04 +02:00
|
|
|
boot.hdr->recovery_dtbo_offset() = lseek(fd, 0, SEEK_CUR);
|
|
|
|
boot.hdr->recovery_dtbo_size() = restore(RECV_DTBO_FILE, fd);
|
2018-10-20 05:10:47 +02:00
|
|
|
file_align();
|
|
|
|
}
|
|
|
|
|
2019-03-13 21:51:22 +01:00
|
|
|
// dtb
|
2019-05-21 01:57:49 +02:00
|
|
|
dtb_off = lseek(fd, 0, SEEK_CUR);
|
2019-03-13 21:51:22 +01:00
|
|
|
if (access(DTB_FILE, R_OK) == 0) {
|
2019-09-17 11:01:04 +02:00
|
|
|
boot.hdr->dtb_size() = restore(DTB_FILE, fd);
|
2019-03-13 21:51:22 +01:00
|
|
|
file_align();
|
|
|
|
}
|
|
|
|
|
2018-10-20 05:10:47 +02:00
|
|
|
// Append tail info
|
|
|
|
if (boot.flags & SEANDROID_FLAG) {
|
|
|
|
restore_buf(fd, SEANDROID_MAGIC "\xFF\xFF\xFF\xFF", 20);
|
|
|
|
}
|
|
|
|
if (boot.flags & LG_BUMP_FLAG) {
|
|
|
|
restore_buf(fd, LG_BUMP_MAGIC, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
// Map output image as rw
|
|
|
|
munmap(boot.map_addr, boot.map_size);
|
2019-02-25 05:09:34 +01:00
|
|
|
mmap_rw(out_image, boot.map_addr, boot.map_size);
|
2018-10-20 05:10:47 +02:00
|
|
|
|
|
|
|
// MTK headers
|
|
|
|
if (boot.flags & MTK_KERNEL) {
|
2019-09-17 11:01:04 +02:00
|
|
|
boot.k_hdr->size = boot.hdr->kernel_size();
|
|
|
|
boot.hdr->kernel_size() += 512;
|
2018-10-20 05:10:47 +02:00
|
|
|
memcpy(boot.map_addr + kernel_off, boot.k_hdr, sizeof(mtk_hdr));
|
|
|
|
}
|
|
|
|
if (boot.flags & MTK_RAMDISK) {
|
2019-09-17 11:01:04 +02:00
|
|
|
boot.r_hdr->size = boot.hdr->ramdisk_size();
|
|
|
|
boot.hdr->ramdisk_size() += 512;
|
2018-10-20 05:10:47 +02:00
|
|
|
memcpy(boot.map_addr + ramdisk_off, boot.r_hdr, sizeof(mtk_hdr));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update checksum
|
|
|
|
HASH_CTX ctx;
|
|
|
|
(boot.flags & SHA256_FLAG) ? SHA256_init(&ctx) : SHA_init(&ctx);
|
2019-09-17 11:01:04 +02:00
|
|
|
uint32_t size = boot.hdr->kernel_size();
|
2018-10-20 05:10:47 +02:00
|
|
|
HASH_update(&ctx, boot.map_addr + kernel_off, size);
|
|
|
|
HASH_update(&ctx, &size, sizeof(size));
|
2019-09-17 11:01:04 +02:00
|
|
|
size = boot.hdr->ramdisk_size();
|
2018-10-20 05:10:47 +02:00
|
|
|
HASH_update(&ctx, boot.map_addr + ramdisk_off, size);
|
|
|
|
HASH_update(&ctx, &size, sizeof(size));
|
2019-09-17 11:01:04 +02:00
|
|
|
size = boot.hdr->second_size();
|
2018-10-20 05:10:47 +02:00
|
|
|
HASH_update(&ctx, boot.map_addr + second_off, size);
|
|
|
|
HASH_update(&ctx, &size, sizeof(size));
|
2019-09-17 11:01:04 +02:00
|
|
|
size = boot.hdr->extra_size();
|
2018-10-20 05:10:47 +02:00
|
|
|
if (size) {
|
|
|
|
HASH_update(&ctx, boot.map_addr + extra_off, size);
|
|
|
|
HASH_update(&ctx, &size, sizeof(size));
|
|
|
|
}
|
2019-09-17 11:01:04 +02:00
|
|
|
if (boot.hdr->header_version()) {
|
|
|
|
size = boot.hdr->recovery_dtbo_size();
|
|
|
|
HASH_update(&ctx, boot.map_addr + boot.hdr->recovery_dtbo_offset(), size);
|
2018-10-20 05:10:47 +02:00
|
|
|
HASH_update(&ctx, &size, sizeof(size));
|
2019-09-17 11:01:04 +02:00
|
|
|
size = boot.hdr->dtb_size();
|
2019-05-21 01:57:49 +02:00
|
|
|
if (size) {
|
|
|
|
HASH_update(&ctx, boot.map_addr + dtb_off, size);
|
|
|
|
HASH_update(&ctx, &size, sizeof(size));
|
|
|
|
}
|
2018-10-20 05:10:47 +02:00
|
|
|
}
|
2019-09-17 11:01:04 +02:00
|
|
|
memset(boot.hdr->id(), 0, 32);
|
|
|
|
memcpy(boot.hdr->id(), HASH_final(&ctx),
|
2018-10-20 05:10:47 +02:00
|
|
|
(boot.flags & SHA256_FLAG) ? SHA256_DIGEST_SIZE : SHA_DIGEST_SIZE);
|
|
|
|
|
|
|
|
// Print new image info
|
|
|
|
boot.print_hdr();
|
|
|
|
|
2018-12-04 09:30:43 +01:00
|
|
|
// Try to fix the header
|
2019-09-17 11:01:04 +02:00
|
|
|
if (boot.hdr->header_version() && boot.hdr->header_size() == 0)
|
|
|
|
boot.hdr->header_size() = sizeof(boot_img_hdr);
|
2018-12-04 09:30:43 +01:00
|
|
|
|
2018-10-20 05:10:47 +02:00
|
|
|
// Main header
|
2019-09-17 11:01:04 +02:00
|
|
|
memcpy(boot.map_addr + header_off, **boot.hdr, boot.hdr->hdr_size());
|
2018-10-20 05:10:47 +02:00
|
|
|
|
|
|
|
if (boot.flags & DHTB_FLAG) {
|
|
|
|
// DHTB header
|
|
|
|
dhtb_hdr *hdr = reinterpret_cast<dhtb_hdr *>(boot.map_addr);
|
|
|
|
memcpy(hdr, DHTB_MAGIC, 8);
|
|
|
|
hdr->size = boot.map_size - 512;
|
|
|
|
SHA256_hash(boot.map_addr + 512, hdr->size, hdr->checksum);
|
|
|
|
} else if (boot.flags & BLOB_FLAG) {
|
|
|
|
// Blob headers
|
|
|
|
boot.b_hdr->size = boot.map_size - sizeof(blob_hdr);
|
|
|
|
memcpy(boot.map_addr, boot.b_hdr, sizeof(blob_hdr));
|
|
|
|
}
|
|
|
|
}
|