2019-03-07 07:24:06 -05:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <string.h>
|
2018-10-19 23:10:47 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libfdt.h>
|
2019-03-07 07:24:06 -05:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
2018-10-19 23:10:47 -04:00
|
|
|
|
|
|
|
#include <mincrypt/sha.h>
|
|
|
|
#include <mincrypt/sha256.h>
|
2019-02-10 03:57:51 -05:00
|
|
|
#include <utils.h>
|
|
|
|
#include <logging.h>
|
2018-10-19 23:10:47 -04:00
|
|
|
|
|
|
|
#include "bootimg.h"
|
|
|
|
#include "magiskboot.h"
|
2019-02-20 20:49:26 -05:00
|
|
|
#include "compress.h"
|
2018-10-19 23:10:47 -04:00
|
|
|
|
2019-03-07 07:24:06 -05:00
|
|
|
using namespace std;
|
|
|
|
|
2019-02-21 04:14:52 -05:00
|
|
|
uint32_t dyn_img_hdr::j32 = 0;
|
|
|
|
uint64_t dyn_img_hdr::j64 = 0;
|
|
|
|
|
2019-11-21 06:08:02 -05:00
|
|
|
static void decompress(format_t type, int fd, const void *in, size_t size) {
|
2019-12-13 00:37:06 -05:00
|
|
|
auto ptr = get_decoder(type, make_unique<fd_stream>(fd));
|
2019-11-21 06:08:02 -05:00
|
|
|
ptr->write(in, size);
|
2019-03-07 07:24:06 -05:00
|
|
|
}
|
|
|
|
|
2019-11-23 04:57:52 -05:00
|
|
|
static off_t compress(format_t type, int fd, const void *in, size_t size) {
|
2019-11-21 06:08:02 -05:00
|
|
|
auto prev = lseek(fd, 0, SEEK_CUR);
|
2019-11-22 03:01:49 -05:00
|
|
|
{
|
2019-12-13 00:37:06 -05:00
|
|
|
auto strm = get_encoder(type, make_unique<fd_stream>(fd));
|
2019-11-23 04:57:52 -05:00
|
|
|
strm->write(in, size);
|
2019-11-22 03:01:49 -05:00
|
|
|
}
|
2019-11-21 06:08:02 -05:00
|
|
|
auto now = lseek(fd, 0, SEEK_CUR);
|
|
|
|
return now - prev;
|
2019-03-07 07:24:06 -05:00
|
|
|
}
|
|
|
|
|
2018-10-19 23:10:47 -04: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);
|
|
|
|
}
|
|
|
|
|
2019-10-09 16:01:21 -04:00
|
|
|
static size_t restore(int fd, const char *filename) {
|
2018-10-19 23:10:47 -04:00
|
|
|
int ifd = xopen(filename, O_RDONLY);
|
|
|
|
size_t size = lseek(ifd, 0, SEEK_END);
|
|
|
|
lseek(ifd, 0, SEEK_SET);
|
2019-02-21 02:54:37 -05:00
|
|
|
xsendfile(fd, ifd, nullptr, size);
|
2018-10-19 23:10:47 -04:00
|
|
|
close(ifd);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void restore_buf(int fd, const void *buf, size_t size) {
|
|
|
|
xwrite(fd, buf, size);
|
|
|
|
}
|
|
|
|
|
2019-10-09 16:01:21 -04:00
|
|
|
void dyn_img_hdr::print() {
|
|
|
|
uint32_t ver = header_version();
|
|
|
|
fprintf(stderr, "HEADER_VER [%u]\n", ver);
|
|
|
|
fprintf(stderr, "KERNEL_SZ [%u]\n", kernel_size());
|
|
|
|
fprintf(stderr, "RAMDISK_SZ [%u]\n", ramdisk_size());
|
|
|
|
fprintf(stderr, "SECOND_SZ [%u]\n", second_size());
|
2019-10-15 01:46:29 -04:00
|
|
|
if (ver == 0)
|
2019-10-09 16:01:21 -04:00
|
|
|
fprintf(stderr, "EXTRA_SZ [%u]\n", extra_size());
|
2019-10-15 01:46:29 -04:00
|
|
|
if (ver >= 1)
|
|
|
|
fprintf(stderr, "RECOV_DTBO_SZ [%u]\n", recovery_dtbo_size());
|
|
|
|
if (ver >= 2)
|
|
|
|
fprintf(stderr, "DTB_SZ [%u]\n", dtb_size());
|
2019-10-09 16:01:21 -04:00
|
|
|
|
|
|
|
ver = os_version();
|
|
|
|
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(stderr, "OS_VERSION [%d.%d.%d]\n", a, b, c);
|
|
|
|
|
|
|
|
y = (patch_level >> 4) + 2000;
|
|
|
|
m = patch_level & 0xf;
|
|
|
|
fprintf(stderr, "OS_PATCH_LEVEL [%d-%02d]\n", y, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "PAGESIZE [%u]\n", page_size());
|
|
|
|
fprintf(stderr, "NAME [%s]\n", name());
|
|
|
|
fprintf(stderr, "CMDLINE [%.512s%.1024s]\n", cmdline(), extra_cmdline());
|
|
|
|
fprintf(stderr, "CHECKSUM [");
|
2019-10-15 01:46:29 -04:00
|
|
|
for (int i = 0; i < SHA256_DIGEST_SIZE; ++i)
|
|
|
|
fprintf(stderr, "%02hhx", static_cast<uint8_t>(id()[i]));
|
2019-10-09 16:01:21 -04:00
|
|
|
fprintf(stderr, "]\n");
|
2018-10-19 23:10:47 -04:00
|
|
|
}
|
|
|
|
|
2019-10-09 16:01:21 -04:00
|
|
|
void dyn_img_hdr::dump_hdr_file() {
|
|
|
|
FILE *fp = xfopen(HEADER_FILE, "w");
|
|
|
|
fprintf(fp, "pagesize=%u\n", page_size());
|
|
|
|
fprintf(fp, "name=%s\n", name());
|
|
|
|
fprintf(fp, "cmdline=%.512s%.1024s\n", cmdline(), extra_cmdline());
|
|
|
|
uint32_t ver = os_version();
|
|
|
|
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;
|
|
|
|
fprintf(fp, "os_patch_level=%d-%02d\n", y, m);
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dyn_img_hdr::load_hdr_file() {
|
|
|
|
parse_prop_file(HEADER_FILE, [=](string_view key, string_view value) -> bool {
|
|
|
|
if (key == "page_size") {
|
|
|
|
page_size() = parse_int(value);
|
|
|
|
} else if (key == "name") {
|
|
|
|
memset(name(), 0, 16);
|
|
|
|
memcpy(name(), value.data(), value.length() > 15 ? 15 : value.length());
|
|
|
|
} else if (key == "cmdline") {
|
|
|
|
memset(cmdline(), 0, 512);
|
|
|
|
memset(extra_cmdline(), 0, 1024);
|
|
|
|
if (value.length() > 512) {
|
|
|
|
memcpy(cmdline(), value.data(), 512);
|
|
|
|
memcpy(extra_cmdline(), &value[512], value.length() - 511);
|
|
|
|
} 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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_img::boot_img(const char *image) {
|
2019-02-24 23:09:34 -05:00
|
|
|
mmap_ro(image, map_addr, map_size);
|
2018-10-19 23:10:47 -04:00
|
|
|
fprintf(stderr, "Parsing boot image: [%s]\n", image);
|
2019-10-07 04:35:02 -04:00
|
|
|
for (uint8_t *addr = map_addr; addr < map_addr + map_size; ++addr) {
|
|
|
|
switch (check_fmt(addr, map_size)) {
|
2018-10-19 23:10:47 -04:00
|
|
|
case CHROMEOS:
|
2019-10-07 04:35:02 -04:00
|
|
|
// chromeos require external signing
|
2018-10-19 23:10:47 -04:00
|
|
|
flags |= CHROMEOS_FLAG;
|
2019-10-07 04:35:02 -04:00
|
|
|
addr += 65535;
|
2018-10-19 23:10:47 -04:00
|
|
|
break;
|
|
|
|
case DHTB:
|
2019-10-07 04:35:02 -04:00
|
|
|
flags |= (DHTB_FLAG | SEANDROID_FLAG);
|
2018-10-19 23:10:47 -04:00
|
|
|
fprintf(stderr, "DHTB_HDR\n");
|
2019-10-07 04:35:02 -04:00
|
|
|
addr += sizeof(dhtb_hdr) - 1;
|
2018-10-19 23:10:47 -04:00
|
|
|
break;
|
|
|
|
case BLOB:
|
|
|
|
flags |= BLOB_FLAG;
|
|
|
|
fprintf(stderr, "TEGRA_BLOB\n");
|
2019-10-07 04:35:02 -04:00
|
|
|
addr += sizeof(blob_hdr) - 1;
|
2018-10-19 23:10:47 -04:00
|
|
|
break;
|
|
|
|
case AOSP:
|
2019-10-07 04:35:02 -04:00
|
|
|
parse_image(addr);
|
|
|
|
return;
|
2019-02-21 05:24:05 -05:00
|
|
|
default:
|
|
|
|
break;
|
2018-10-19 23:10:47 -04:00
|
|
|
}
|
|
|
|
}
|
2019-10-07 04:35:02 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-10-09 16:01:21 -04:00
|
|
|
boot_img::~boot_img() {
|
|
|
|
munmap(map_addr, map_size);
|
|
|
|
delete hdr;
|
|
|
|
}
|
|
|
|
|
2019-10-07 04:35:02 -04:00
|
|
|
#define get_block(name) {\
|
|
|
|
name = addr + off; \
|
|
|
|
off += hdr->name##_size(); \
|
|
|
|
off = do_align(off, hdr->page_size()); \
|
2018-10-19 23:10:47 -04:00
|
|
|
}
|
|
|
|
|
2019-10-07 04:35:02 -04:00
|
|
|
void boot_img::parse_image(uint8_t *addr) {
|
|
|
|
auto hp = reinterpret_cast<boot_img_hdr*>(addr);
|
2019-02-21 05:08:38 -05:00
|
|
|
if (hp->page_size >= 0x02000000) {
|
|
|
|
fprintf(stderr, "PXA_BOOT_HDR\n");
|
2019-10-07 04:35:02 -04:00
|
|
|
hdr = new dyn_img_pxa(addr);
|
2019-02-21 05:08:38 -05:00
|
|
|
} else {
|
2019-05-19 21:18:18 -03: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 05:08:38 -05:00
|
|
|
flags |= NOOKHD_FLAG;
|
2019-05-19 21:18:18 -03:00
|
|
|
fprintf(stderr, "NOOKHD_LOADER\n");
|
2019-10-07 04:35:02 -04:00
|
|
|
addr += NOOKHD_PRE_HEADER_SZ;
|
2019-02-21 05:08:38 -05:00
|
|
|
} else if (memcmp(hp->name, ACCLAIM_MAGIC, 10) == 0) {
|
|
|
|
flags |= ACCLAIM_FLAG;
|
2019-05-19 21:18:18 -03:00
|
|
|
fprintf(stderr, "ACCLAIM_LOADER\n");
|
2019-10-07 04:35:02 -04:00
|
|
|
addr += ACCLAIM_PRE_HEADER_SZ;
|
2019-02-21 05:08:38 -05:00
|
|
|
}
|
2019-09-17 05:01:04 -04:00
|
|
|
|
|
|
|
if (hp->header_version == 1)
|
2019-10-07 04:35:02 -04:00
|
|
|
hdr = new dyn_img_v1(addr);
|
2019-09-17 05:01:04 -04:00
|
|
|
else if (hp->header_version == 2)
|
2019-10-07 04:35:02 -04:00
|
|
|
hdr = new dyn_img_v2(addr);
|
2019-09-17 05:01:04 -04:00
|
|
|
else
|
2019-10-07 04:35:02 -04:00
|
|
|
hdr = new dyn_img_v0(addr);
|
2019-02-21 05:08:38 -05:00
|
|
|
}
|
2019-10-15 01:46:29 -04:00
|
|
|
|
2019-11-15 11:52:58 -04:00
|
|
|
for (int i = SHA_DIGEST_SIZE + 4; i < SHA256_DIGEST_SIZE; ++i) {
|
2019-10-15 01:46:29 -04:00
|
|
|
if (hdr->id()[i]) {
|
|
|
|
flags |= SHA256_FLAG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-02-21 05:08:38 -05:00
|
|
|
|
2019-10-09 16:01:21 -04:00
|
|
|
hdr->print();
|
2019-02-21 05:08:38 -05:00
|
|
|
|
2019-10-07 04:35:02 -04:00
|
|
|
size_t off = hdr->page_size();
|
2019-10-15 01:46:29 -04:00
|
|
|
hdr_addr = addr;
|
2019-10-07 04:35:02 -04:00
|
|
|
get_block(kernel);
|
|
|
|
get_block(ramdisk);
|
|
|
|
get_block(second);
|
|
|
|
get_block(extra);
|
|
|
|
get_block(recovery_dtbo);
|
|
|
|
get_block(dtb);
|
2019-02-21 05:08:38 -05:00
|
|
|
|
2019-10-07 04:35:02 -04:00
|
|
|
if (addr + off < map_addr + map_size) {
|
|
|
|
tail = addr + off;
|
2019-02-21 05:08:38 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-10-07 04:35:02 -04:00
|
|
|
find_kernel_dtb();
|
2019-02-21 05:08:38 -05:00
|
|
|
|
2019-09-17 05:01:04 -04:00
|
|
|
k_fmt = check_fmt(kernel, hdr->kernel_size());
|
|
|
|
r_fmt = check_fmt(ramdisk, hdr->ramdisk_size());
|
2019-02-21 05:08:38 -05:00
|
|
|
|
|
|
|
// Check MTK
|
|
|
|
if (k_fmt == MTK) {
|
|
|
|
fprintf(stderr, "MTK_KERNEL_HDR\n");
|
|
|
|
flags |= MTK_KERNEL;
|
2019-10-07 04:35:02 -04:00
|
|
|
k_hdr = reinterpret_cast<mtk_hdr *>(kernel);
|
2019-02-21 05:08:38 -05:00
|
|
|
fprintf(stderr, "KERNEL [%u]\n", k_hdr->size);
|
|
|
|
fprintf(stderr, "NAME [%s]\n", k_hdr->name);
|
2019-10-07 04:35:02 -04:00
|
|
|
kernel += sizeof(mtk_hdr);
|
|
|
|
hdr->kernel_size() -= sizeof(mtk_hdr);
|
2019-09-17 05:01:04 -04:00
|
|
|
k_fmt = check_fmt(kernel, hdr->kernel_size());
|
2019-02-21 05:08:38 -05:00
|
|
|
}
|
|
|
|
if (r_fmt == MTK) {
|
|
|
|
fprintf(stderr, "MTK_RAMDISK_HDR\n");
|
|
|
|
flags |= MTK_RAMDISK;
|
2019-10-07 04:35:02 -04:00
|
|
|
r_hdr = reinterpret_cast<mtk_hdr *>(ramdisk);
|
2019-02-21 05:08:38 -05:00
|
|
|
fprintf(stderr, "RAMDISK [%u]\n", r_hdr->size);
|
|
|
|
fprintf(stderr, "NAME [%s]\n", r_hdr->name);
|
2019-10-07 04:35:02 -04:00
|
|
|
ramdisk += sizeof(mtk_hdr);
|
|
|
|
hdr->ramdisk_size() -= sizeof(mtk_hdr);
|
2019-09-17 05:01:04 -04:00
|
|
|
r_fmt = check_fmt(ramdisk, hdr->ramdisk_size());
|
2019-02-21 05:08:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "KERNEL_FMT [%s]\n", fmt2name[k_fmt]);
|
|
|
|
fprintf(stderr, "RAMDISK_FMT [%s]\n", fmt2name[r_fmt]);
|
|
|
|
}
|
|
|
|
|
2019-12-30 13:04:39 +08:00
|
|
|
static int find_dtb_offset(uint8_t *buf, int sz) {
|
|
|
|
for (int off = 0; off < sz - (int) sizeof(fdt_header); ++off) {
|
|
|
|
auto fdt_hdr = reinterpret_cast<fdt_header *>(buf + off);
|
2019-02-21 05:24:05 -05:00
|
|
|
if (fdt32_to_cpu(fdt_hdr->magic) != FDT_MAGIC)
|
2018-10-19 23:10:47 -04:00
|
|
|
continue;
|
2019-02-21 05:24:05 -05:00
|
|
|
|
2018-10-19 23:10:47 -04:00
|
|
|
// Check that fdt_header.totalsize does not overflow kernel image size
|
2019-02-21 05:24:05 -05:00
|
|
|
uint32_t totalsize = fdt32_to_cpu(fdt_hdr->totalsize);
|
2019-12-30 13:04:39 +08:00
|
|
|
if (totalsize + off > sz)
|
2018-10-19 23:10:47 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check that fdt_header.off_dt_struct does not overflow kernel image size
|
2019-02-21 05:24:05 -05:00
|
|
|
uint32_t off_dt_struct = fdt32_to_cpu(fdt_hdr->off_dt_struct);
|
2019-12-30 13:04:39 +08:00
|
|
|
if (off_dt_struct + off > sz)
|
2018-10-19 23:10:47 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check that fdt_node_header.tag of first node is FDT_BEGIN_NODE
|
2019-12-30 13:04:39 +08:00
|
|
|
auto fdt_node_hdr = reinterpret_cast<fdt_node_header *>(buf + off + off_dt_struct);
|
2019-10-07 04:35:02 -04:00
|
|
|
if (fdt32_to_cpu(fdt_node_hdr->tag) != FDT_BEGIN_NODE)
|
2018-10-19 23:10:47 -04:00
|
|
|
continue;
|
|
|
|
|
2019-12-30 13:04:39 +08:00
|
|
|
return off;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void boot_img::find_kernel_dtb() {
|
|
|
|
if (int off = find_dtb_offset(kernel, hdr->kernel_size()); off > 0) {
|
|
|
|
kernel_dtb = kernel + off;
|
|
|
|
kernel_dt_size = hdr->kernel_size() - off;
|
|
|
|
hdr->kernel_size() = off;
|
2019-03-13 16:51:22 -04:00
|
|
|
fprintf(stderr, "KERNEL_DTB [%u]\n", kernel_dt_size);
|
2019-12-30 13:04:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int split_image_dtb(const char *filename) {
|
|
|
|
uint8_t *buf;
|
|
|
|
size_t sz;
|
|
|
|
mmap_ro(filename, buf, sz);
|
|
|
|
run_finally f([=]{ munmap(buf, sz); });
|
|
|
|
|
|
|
|
if (int off = find_dtb_offset(buf, sz); off > 0) {
|
|
|
|
format_t fmt = check_fmt(buf, sz);
|
|
|
|
if (COMPRESSED(fmt)) {
|
|
|
|
int fd = creat(KERNEL_FILE, 0644);
|
|
|
|
decompress(fmt, fd, buf, off);
|
|
|
|
close(fd);
|
|
|
|
} else {
|
|
|
|
dump(buf, off, KERNEL_FILE);
|
|
|
|
}
|
|
|
|
dump(buf + off, sz - off, KER_DTB_FILE);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Cannot find DTB in %s\n", filename);
|
|
|
|
return 1;
|
2018-10-19 23:10:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 15:48:58 -03:00
|
|
|
int unpack(const char *image, bool nodecomp, bool hdr) {
|
2019-10-09 16:01:21 -04:00
|
|
|
boot_img boot(image);
|
2019-10-07 04:35:02 -04:00
|
|
|
|
|
|
|
if (hdr)
|
2019-10-09 16:01:21 -04:00
|
|
|
boot.hdr->dump_hdr_file();
|
2019-03-07 21:32:01 -05:00
|
|
|
|
2018-10-19 23:10:47 -04:00
|
|
|
// Dump kernel
|
2019-10-31 02:43:59 -04:00
|
|
|
if (!nodecomp && COMPRESSED(boot.k_fmt)) {
|
2019-10-07 04:35:02 -04:00
|
|
|
int fd = creat(KERNEL_FILE, 0644);
|
2019-09-17 05:01:04 -04:00
|
|
|
decompress(boot.k_fmt, fd, boot.kernel, boot.hdr->kernel_size());
|
2018-10-19 23:10:47 -04:00
|
|
|
close(fd);
|
|
|
|
} else {
|
2019-09-17 05:01:04 -04:00
|
|
|
dump(boot.kernel, boot.hdr->kernel_size(), KERNEL_FILE);
|
2018-10-19 23:10:47 -04:00
|
|
|
}
|
|
|
|
|
2019-10-07 04:35:02 -04:00
|
|
|
// Dump kernel_dtb
|
2019-03-13 16:51:22 -04:00
|
|
|
dump(boot.kernel_dtb, boot.kernel_dt_size, KER_DTB_FILE);
|
2018-10-19 23:10:47 -04:00
|
|
|
|
|
|
|
// Dump ramdisk
|
2019-10-31 02:43:59 -04:00
|
|
|
if (!nodecomp && COMPRESSED(boot.r_fmt)) {
|
2019-10-07 04:35:02 -04:00
|
|
|
int fd = creat(RAMDISK_FILE, 0644);
|
2019-09-17 05:01:04 -04:00
|
|
|
decompress(boot.r_fmt, fd, boot.ramdisk, boot.hdr->ramdisk_size());
|
2018-10-19 23:10:47 -04:00
|
|
|
close(fd);
|
|
|
|
} else {
|
2019-09-17 05:01:04 -04:00
|
|
|
dump(boot.ramdisk, boot.hdr->ramdisk_size(), RAMDISK_FILE);
|
2018-10-19 23:10:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dump second
|
2019-09-17 05:01:04 -04:00
|
|
|
dump(boot.second, boot.hdr->second_size(), SECOND_FILE);
|
2018-10-19 23:10:47 -04:00
|
|
|
|
|
|
|
// Dump extra
|
2019-09-17 05:01:04 -04:00
|
|
|
dump(boot.extra, boot.hdr->extra_size(), EXTRA_FILE);
|
2018-10-19 23:10:47 -04:00
|
|
|
|
|
|
|
// Dump recovery_dtbo
|
2019-10-07 04:35:02 -04:00
|
|
|
dump(boot.recovery_dtbo, boot.hdr->recovery_dtbo_size(), RECV_DTBO_FILE);
|
2019-03-13 16:51:22 -04:00
|
|
|
|
|
|
|
// Dump dtb
|
2019-09-17 05:01:04 -04:00
|
|
|
dump(boot.dtb, boot.hdr->dtb_size(), DTB_FILE);
|
2019-10-07 04:35:02 -04:00
|
|
|
|
|
|
|
return (boot.flags & CHROMEOS_FLAG) ? 2 : 0;
|
2018-10-19 23:10:47 -04:00
|
|
|
}
|
|
|
|
|
2019-09-17 05:01:04 -04:00
|
|
|
#define file_align() \
|
2019-10-07 04:35:02 -04:00
|
|
|
write_zero(fd, align_off(lseek(fd, 0, SEEK_CUR) - off.header, boot.hdr->page_size()))
|
2019-09-17 05:01:04 -04:00
|
|
|
|
2019-10-09 16:01:21 -04:00
|
|
|
void repack(const char* src_img, const char* out_img, bool nocomp) {
|
|
|
|
boot_img boot(src_img);
|
2018-10-19 23:10:47 -04:00
|
|
|
|
2019-10-07 04:35:02 -04:00
|
|
|
struct {
|
|
|
|
uint32_t header;
|
|
|
|
uint32_t kernel;
|
|
|
|
uint32_t ramdisk;
|
|
|
|
uint32_t second;
|
|
|
|
uint32_t extra;
|
|
|
|
uint32_t dtb;
|
|
|
|
} off;
|
2018-10-19 23:10:47 -04:00
|
|
|
|
2019-10-09 16:01:21 -04:00
|
|
|
fprintf(stderr, "Repack to boot image: [%s]\n", out_img);
|
2019-10-07 04:35:02 -04:00
|
|
|
|
2018-10-19 23:10:47 -04:00
|
|
|
// Reset sizes
|
2019-09-17 05:01:04 -04:00
|
|
|
boot.hdr->kernel_size() = 0;
|
|
|
|
boot.hdr->ramdisk_size() = 0;
|
|
|
|
boot.hdr->second_size() = 0;
|
|
|
|
boot.hdr->dtb_size() = 0;
|
2019-03-13 16:51:22 -04:00
|
|
|
boot.kernel_dt_size = 0;
|
2018-10-19 23:10:47 -04:00
|
|
|
|
2019-10-07 04:35:02 -04:00
|
|
|
if (access(HEADER_FILE, R_OK) == 0)
|
2019-10-09 16:01:21 -04:00
|
|
|
boot.hdr->load_hdr_file();
|
2019-10-07 04:35:02 -04:00
|
|
|
|
|
|
|
/*****************
|
|
|
|
* Writing blocks
|
|
|
|
*****************/
|
2018-10-19 23:10:47 -04:00
|
|
|
|
|
|
|
// Create new image
|
2019-10-09 16:01:21 -04:00
|
|
|
int fd = creat(out_img, 0644);
|
2018-10-19 23:10:47 -04:00
|
|
|
|
|
|
|
if (boot.flags & DHTB_FLAG) {
|
|
|
|
// Skip DHTB header
|
2019-10-07 04:35:02 -04:00
|
|
|
write_zero(fd, sizeof(dhtb_hdr));
|
2018-10-19 23:10:47 -04:00
|
|
|
} else if (boot.flags & BLOB_FLAG) {
|
2019-10-07 04:35:02 -04:00
|
|
|
restore_buf(fd, boot.map_addr, sizeof(blob_hdr));
|
2018-10-19 23:10:47 -04:00
|
|
|
} 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-10-07 04:35:02 -04:00
|
|
|
// Copy a page for header
|
|
|
|
off.header = lseek(fd, 0, SEEK_CUR);
|
2019-10-15 01:46:29 -04:00
|
|
|
restore_buf(fd, boot.hdr_addr, boot.hdr->page_size());
|
2018-10-19 23:10:47 -04:00
|
|
|
|
|
|
|
// kernel
|
2019-10-07 04:35:02 -04:00
|
|
|
off.kernel = lseek(fd, 0, SEEK_CUR);
|
2018-10-19 23:10:47 -04:00
|
|
|
if (boot.flags & MTK_KERNEL) {
|
2019-10-07 04:35:02 -04:00
|
|
|
// Copy MTK headers
|
|
|
|
restore_buf(fd, boot.k_hdr, sizeof(mtk_hdr));
|
2018-10-19 23:10:47 -04:00
|
|
|
}
|
|
|
|
if (access(KERNEL_FILE, R_OK) == 0) {
|
2019-03-07 07:24:06 -05:00
|
|
|
size_t raw_size;
|
|
|
|
void *raw_buf;
|
|
|
|
mmap_ro(KERNEL_FILE, raw_buf, raw_size);
|
2019-06-06 16:56:17 -03:00
|
|
|
if (!COMPRESSED_ANY(check_fmt(raw_buf, raw_size)) && COMPRESSED(boot.k_fmt)) {
|
2019-09-17 05:01:04 -04:00
|
|
|
boot.hdr->kernel_size() = compress(boot.k_fmt, fd, raw_buf, raw_size);
|
2018-10-19 23:10:47 -04:00
|
|
|
} else {
|
2019-10-07 04:35:02 -04:00
|
|
|
boot.hdr->kernel_size() = xwrite(fd, raw_buf, raw_size);
|
2018-10-19 23:10:47 -04:00
|
|
|
}
|
2019-03-07 07:24:06 -05:00
|
|
|
munmap(raw_buf, raw_size);
|
2018-10-19 23:10:47 -04:00
|
|
|
}
|
|
|
|
|
2019-03-13 16:51:22 -04:00
|
|
|
// kernel dtb
|
|
|
|
if (access(KER_DTB_FILE, R_OK) == 0)
|
2019-10-09 16:01:21 -04:00
|
|
|
boot.hdr->kernel_size() += restore(fd, KER_DTB_FILE);
|
2018-10-19 23:10:47 -04:00
|
|
|
file_align();
|
|
|
|
|
|
|
|
// ramdisk
|
2019-10-07 04:35:02 -04:00
|
|
|
off.ramdisk = lseek(fd, 0, SEEK_CUR);
|
2018-10-19 23:10:47 -04:00
|
|
|
if (boot.flags & MTK_RAMDISK) {
|
2019-10-07 04:35:02 -04:00
|
|
|
// Copy MTK headers
|
|
|
|
restore_buf(fd, boot.r_hdr, sizeof(mtk_hdr));
|
2018-10-19 23:10:47 -04:00
|
|
|
}
|
|
|
|
if (access(RAMDISK_FILE, R_OK) == 0) {
|
2019-03-07 07:24:06 -05:00
|
|
|
size_t raw_size;
|
|
|
|
void *raw_buf;
|
|
|
|
mmap_ro(RAMDISK_FILE, raw_buf, raw_size);
|
2019-10-31 02:43:59 -04:00
|
|
|
if (!nocomp && !COMPRESSED_ANY(check_fmt(raw_buf, raw_size)) && COMPRESSED(boot.r_fmt)) {
|
2019-09-17 05:01:04 -04:00
|
|
|
boot.hdr->ramdisk_size() = compress(boot.r_fmt, fd, raw_buf, raw_size);
|
2018-10-19 23:10:47 -04:00
|
|
|
} else {
|
2019-10-07 04:35:02 -04:00
|
|
|
boot.hdr->ramdisk_size() = xwrite(fd, raw_buf, raw_size);
|
2018-10-19 23:10:47 -04:00
|
|
|
}
|
2019-03-07 07:24:06 -05:00
|
|
|
munmap(raw_buf, raw_size);
|
2018-10-19 23:10:47 -04:00
|
|
|
file_align();
|
|
|
|
}
|
|
|
|
|
|
|
|
// second
|
2019-10-07 04:35:02 -04:00
|
|
|
off.second = lseek(fd, 0, SEEK_CUR);
|
2018-10-19 23:10:47 -04:00
|
|
|
if (access(SECOND_FILE, R_OK) == 0) {
|
2019-10-09 16:01:21 -04:00
|
|
|
boot.hdr->second_size() = restore(fd, SECOND_FILE);
|
2018-10-19 23:10:47 -04:00
|
|
|
file_align();
|
|
|
|
}
|
|
|
|
|
|
|
|
// extra
|
2019-10-07 04:35:02 -04:00
|
|
|
off.extra = lseek(fd, 0, SEEK_CUR);
|
2018-10-19 23:10:47 -04:00
|
|
|
if (access(EXTRA_FILE, R_OK) == 0) {
|
2019-10-09 16:01:21 -04:00
|
|
|
boot.hdr->extra_size() = restore(fd, EXTRA_FILE);
|
2018-10-19 23:10:47 -04:00
|
|
|
file_align();
|
|
|
|
}
|
|
|
|
|
|
|
|
// recovery_dtbo
|
|
|
|
if (access(RECV_DTBO_FILE, R_OK) == 0) {
|
2019-09-17 05:01:04 -04:00
|
|
|
boot.hdr->recovery_dtbo_offset() = lseek(fd, 0, SEEK_CUR);
|
2019-10-09 16:01:21 -04:00
|
|
|
boot.hdr->recovery_dtbo_size() = restore(fd, RECV_DTBO_FILE);
|
2018-10-19 23:10:47 -04:00
|
|
|
file_align();
|
|
|
|
}
|
|
|
|
|
2019-03-13 16:51:22 -04:00
|
|
|
// dtb
|
2019-10-07 04:35:02 -04:00
|
|
|
off.dtb = lseek(fd, 0, SEEK_CUR);
|
2019-03-13 16:51:22 -04:00
|
|
|
if (access(DTB_FILE, R_OK) == 0) {
|
2019-10-09 16:01:21 -04:00
|
|
|
boot.hdr->dtb_size() = restore(fd, DTB_FILE);
|
2019-03-13 16:51:22 -04:00
|
|
|
file_align();
|
|
|
|
}
|
|
|
|
|
2018-10-19 23:10:47 -04: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);
|
|
|
|
|
2019-10-07 04:35:02 -04:00
|
|
|
/*********************
|
|
|
|
* Patching the image
|
|
|
|
*********************/
|
|
|
|
|
2018-10-19 23:10:47 -04:00
|
|
|
// Map output image as rw
|
|
|
|
munmap(boot.map_addr, boot.map_size);
|
2019-10-09 16:01:21 -04:00
|
|
|
mmap_rw(out_img, boot.map_addr, boot.map_size);
|
2018-10-19 23:10:47 -04:00
|
|
|
|
|
|
|
// MTK headers
|
|
|
|
if (boot.flags & MTK_KERNEL) {
|
2019-10-07 04:35:02 -04:00
|
|
|
auto hdr = reinterpret_cast<mtk_hdr *>(boot.map_addr + off.kernel);
|
|
|
|
hdr->size = boot.hdr->kernel_size();
|
|
|
|
boot.hdr->kernel_size() += sizeof(*hdr);
|
2018-10-19 23:10:47 -04:00
|
|
|
}
|
|
|
|
if (boot.flags & MTK_RAMDISK) {
|
2019-10-07 04:35:02 -04:00
|
|
|
auto hdr = reinterpret_cast<mtk_hdr *>(boot.map_addr + off.ramdisk);
|
|
|
|
hdr->size = boot.hdr->ramdisk_size();
|
|
|
|
boot.hdr->ramdisk_size() += sizeof(*hdr);
|
2018-10-19 23:10:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update checksum
|
|
|
|
HASH_CTX ctx;
|
|
|
|
(boot.flags & SHA256_FLAG) ? SHA256_init(&ctx) : SHA_init(&ctx);
|
2019-09-17 05:01:04 -04:00
|
|
|
uint32_t size = boot.hdr->kernel_size();
|
2019-10-07 04:35:02 -04:00
|
|
|
HASH_update(&ctx, boot.map_addr + off.kernel, size);
|
2018-10-19 23:10:47 -04:00
|
|
|
HASH_update(&ctx, &size, sizeof(size));
|
2019-09-17 05:01:04 -04:00
|
|
|
size = boot.hdr->ramdisk_size();
|
2019-10-07 04:35:02 -04:00
|
|
|
HASH_update(&ctx, boot.map_addr + off.ramdisk, size);
|
2018-10-19 23:10:47 -04:00
|
|
|
HASH_update(&ctx, &size, sizeof(size));
|
2019-09-17 05:01:04 -04:00
|
|
|
size = boot.hdr->second_size();
|
2019-10-07 04:35:02 -04:00
|
|
|
HASH_update(&ctx, boot.map_addr + off.second, size);
|
2018-10-19 23:10:47 -04:00
|
|
|
HASH_update(&ctx, &size, sizeof(size));
|
2019-09-17 05:01:04 -04:00
|
|
|
size = boot.hdr->extra_size();
|
2018-10-19 23:10:47 -04:00
|
|
|
if (size) {
|
2019-10-07 04:35:02 -04:00
|
|
|
HASH_update(&ctx, boot.map_addr + off.extra, size);
|
2018-10-19 23:10:47 -04:00
|
|
|
HASH_update(&ctx, &size, sizeof(size));
|
|
|
|
}
|
2019-10-15 01:46:29 -04:00
|
|
|
if (boot.hdr->header_version() >= 1) {
|
2019-09-17 05:01:04 -04:00
|
|
|
size = boot.hdr->recovery_dtbo_size();
|
|
|
|
HASH_update(&ctx, boot.map_addr + boot.hdr->recovery_dtbo_offset(), size);
|
2018-10-19 23:10:47 -04:00
|
|
|
HASH_update(&ctx, &size, sizeof(size));
|
2019-10-15 01:46:29 -04:00
|
|
|
}
|
|
|
|
if (boot.hdr->header_version() >= 2) {
|
2019-09-17 05:01:04 -04:00
|
|
|
size = boot.hdr->dtb_size();
|
2019-10-15 01:46:29 -04:00
|
|
|
HASH_update(&ctx, boot.map_addr + off.dtb, size);
|
|
|
|
HASH_update(&ctx, &size, sizeof(size));
|
2018-10-19 23:10:47 -04:00
|
|
|
}
|
2019-09-17 05:01:04 -04:00
|
|
|
memset(boot.hdr->id(), 0, 32);
|
|
|
|
memcpy(boot.hdr->id(), HASH_final(&ctx),
|
2018-10-19 23:10:47 -04:00
|
|
|
(boot.flags & SHA256_FLAG) ? SHA256_DIGEST_SIZE : SHA_DIGEST_SIZE);
|
2019-10-15 01:46:29 -04:00
|
|
|
boot.hdr->header_size() = boot.hdr->hdr_size();
|
2018-10-19 23:10:47 -04:00
|
|
|
|
|
|
|
// Print new image info
|
2019-10-09 16:01:21 -04:00
|
|
|
boot.hdr->print();
|
2018-10-19 23:10:47 -04:00
|
|
|
|
|
|
|
// Main header
|
2019-10-07 04:35:02 -04:00
|
|
|
memcpy(boot.map_addr + off.header, **boot.hdr, boot.hdr->hdr_size());
|
2018-10-19 23:10:47 -04:00
|
|
|
|
|
|
|
if (boot.flags & DHTB_FLAG) {
|
|
|
|
// DHTB header
|
2019-10-07 04:35:02 -04:00
|
|
|
auto hdr = reinterpret_cast<dhtb_hdr *>(boot.map_addr);
|
2018-10-19 23:10:47 -04:00
|
|
|
memcpy(hdr, DHTB_MAGIC, 8);
|
2019-10-07 04:35:02 -04:00
|
|
|
hdr->size = boot.map_size - sizeof(dhtb_hdr);
|
|
|
|
SHA256_hash(boot.map_addr + sizeof(dhtb_hdr), hdr->size, hdr->checksum);
|
2018-10-19 23:10:47 -04:00
|
|
|
} else if (boot.flags & BLOB_FLAG) {
|
2019-10-07 04:35:02 -04:00
|
|
|
// Blob header
|
|
|
|
auto hdr = reinterpret_cast<blob_hdr *>(boot.map_addr);
|
|
|
|
hdr->size = boot.map_size - sizeof(blob_hdr);
|
2018-10-19 23:10:47 -04:00
|
|
|
}
|
|
|
|
}
|