2017-09-14 15:11:56 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2017-11-09 17:51:41 +00:00
|
|
|
#include <fcntl.h>
|
2017-09-14 15:11:56 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2017-07-12 18:14:10 +00:00
|
|
|
#include "bootimg.h"
|
|
|
|
#include "magiskboot.h"
|
2017-09-14 15:11:56 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "logging.h"
|
2017-07-12 18:14:10 +00:00
|
|
|
|
2017-12-06 04:51:16 +00:00
|
|
|
#define INSUF_BLOCK_RET 2
|
|
|
|
#define CHROMEOS_RET 3
|
|
|
|
#define ELF32_RET 4
|
|
|
|
#define ELF64_RET 5
|
|
|
|
|
2017-09-12 07:27:28 +00:00
|
|
|
static void dump(void *buf, size_t size, const char *filename) {
|
2017-11-09 17:51:41 +00:00
|
|
|
int fd = creat(filename, 0644);
|
2017-07-12 18:14:10 +00:00
|
|
|
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);
|
|
|
|
xsendfile(fd, ifd, NULL, size);
|
|
|
|
close(ifd);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void restore_buf(int fd, const void *buf, size_t size) {
|
|
|
|
xwrite(fd, buf, size);
|
|
|
|
}
|
|
|
|
|
2017-09-12 14:09:10 +00:00
|
|
|
static void print_hdr(const boot_img_hdr *hdr) {
|
|
|
|
fprintf(stderr, "KERNEL [%d] @ 0x%08x\n", hdr->kernel_size, hdr->kernel_addr);
|
|
|
|
fprintf(stderr, "RAMDISK [%d] @ 0x%08x\n", hdr->ramdisk_size, hdr->ramdisk_addr);
|
|
|
|
fprintf(stderr, "SECOND [%d] @ 0x%08x\n", hdr->second_size, hdr->second_addr);
|
2017-10-07 14:08:10 +00:00
|
|
|
fprintf(stderr, "EXTRA [%d] @ 0x%08x\n", hdr->extra_size, hdr->tags_addr);
|
2017-09-12 14:09:10 +00:00
|
|
|
fprintf(stderr, "PAGESIZE [%d]\n", hdr->page_size);
|
|
|
|
if (hdr->os_version != 0) {
|
2017-07-12 18:14:10 +00:00
|
|
|
int a,b,c,y,m = 0;
|
|
|
|
int os_version, os_patch_level;
|
2017-09-12 14:09:10 +00:00
|
|
|
os_version = hdr->os_version >> 11;
|
|
|
|
os_patch_level = hdr->os_version & 0x7ff;
|
2017-09-12 07:27:28 +00:00
|
|
|
|
2017-07-12 18:14:10 +00:00
|
|
|
a = (os_version >> 14) & 0x7f;
|
|
|
|
b = (os_version >> 7) & 0x7f;
|
|
|
|
c = os_version & 0x7f;
|
2017-07-18 03:53:28 +00:00
|
|
|
fprintf(stderr, "OS_VERSION [%d.%d.%d]\n", a, b, c);
|
2017-09-12 07:27:28 +00:00
|
|
|
|
2017-07-12 18:14:10 +00:00
|
|
|
y = (os_patch_level >> 4) + 2000;
|
|
|
|
m = os_patch_level & 0xf;
|
2017-07-18 03:53:28 +00:00
|
|
|
fprintf(stderr, "PATCH_LEVEL [%d-%02d]\n", y, m);
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
2017-09-12 14:09:10 +00:00
|
|
|
fprintf(stderr, "NAME [%s]\n", hdr->name);
|
|
|
|
fprintf(stderr, "CMDLINE [%s]\n", hdr->cmdline);
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 04:51:16 +00:00
|
|
|
int parse_img(const char *image, boot_img *boot) {
|
2017-09-12 14:09:10 +00:00
|
|
|
memset(boot, 0, sizeof(*boot));
|
2017-12-06 04:51:16 +00:00
|
|
|
int is_blk = mmap_ro(image, &boot->map_addr, &boot->map_size);
|
|
|
|
|
|
|
|
// Parse image
|
2017-12-20 19:36:18 +00:00
|
|
|
fprintf(stderr, "Parsing boot image: [%s]\n", image);
|
2018-01-12 21:48:39 +00:00
|
|
|
for (void *head = boot->map_addr; head < boot->map_addr + boot->map_size; head += 256) {
|
|
|
|
size_t pos = 0;
|
|
|
|
|
|
|
|
switch (check_type(head)) {
|
2017-07-12 18:14:10 +00:00
|
|
|
case CHROMEOS:
|
|
|
|
// The caller should know it's chromeos, as it needs additional signing
|
2017-12-06 04:51:16 +00:00
|
|
|
boot->flags |= CHROMEOS_FLAG;
|
2017-07-12 18:14:10 +00:00
|
|
|
continue;
|
|
|
|
case ELF32:
|
2017-12-06 04:51:16 +00:00
|
|
|
exit(ELF32_RET);
|
2017-07-12 18:14:10 +00:00
|
|
|
case ELF64:
|
2017-12-06 04:51:16 +00:00
|
|
|
exit(ELF64_RET);
|
2017-07-12 18:14:10 +00:00
|
|
|
case AOSP:
|
|
|
|
// Read the header
|
2018-01-12 21:48:39 +00:00
|
|
|
memcpy(&boot->hdr, head + pos, sizeof(boot->hdr));
|
2017-09-12 14:09:10 +00:00
|
|
|
pos += boot->hdr.page_size;
|
|
|
|
|
|
|
|
print_hdr(&boot->hdr);
|
|
|
|
|
2018-01-12 21:48:39 +00:00
|
|
|
boot->kernel = head + pos;
|
2017-09-12 14:09:10 +00:00
|
|
|
pos += boot->hdr.kernel_size;
|
|
|
|
mem_align(&pos, boot->hdr.page_size);
|
|
|
|
|
2018-01-12 21:48:39 +00:00
|
|
|
boot->ramdisk = head + pos;
|
2017-09-12 14:09:10 +00:00
|
|
|
pos += boot->hdr.ramdisk_size;
|
|
|
|
mem_align(&pos, boot->hdr.page_size);
|
|
|
|
|
|
|
|
if (boot->hdr.second_size) {
|
2018-01-12 21:48:39 +00:00
|
|
|
boot->second = head + pos;
|
2017-09-12 14:09:10 +00:00
|
|
|
pos += boot->hdr.second_size;
|
|
|
|
mem_align(&pos, boot->hdr.page_size);
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 14:08:10 +00:00
|
|
|
if (boot->hdr.extra_size) {
|
2018-01-12 21:48:39 +00:00
|
|
|
boot->extra = head + pos;
|
2017-10-07 14:08:10 +00:00
|
|
|
pos += boot->hdr.extra_size;
|
2017-09-12 14:09:10 +00:00
|
|
|
mem_align(&pos, boot->hdr.page_size);
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 04:51:16 +00:00
|
|
|
if (pos < boot->map_size) {
|
2018-01-12 21:48:39 +00:00
|
|
|
boot->tail = head + pos;
|
2017-12-06 04:51:16 +00:00
|
|
|
boot->tail_size = boot->map_size - pos;
|
2017-09-12 14:09:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 14:08:10 +00:00
|
|
|
// Search for dtb in kernel
|
2017-12-06 04:51:16 +00:00
|
|
|
for (uint32_t i = 0; i < boot->hdr.kernel_size; ++i) {
|
2017-10-07 14:08:10 +00:00
|
|
|
if (memcmp(boot->kernel + i, DTB_MAGIC, 4) == 0) {
|
|
|
|
boot->dtb = boot->kernel + i;
|
|
|
|
boot->dt_size = boot->hdr.kernel_size - i;
|
|
|
|
boot->hdr.kernel_size = i;
|
2017-12-06 04:51:16 +00:00
|
|
|
fprintf(stderr, "DTB [%u]\n", boot->dt_size);
|
2017-09-12 14:09:10 +00:00
|
|
|
}
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 14:09:10 +00:00
|
|
|
boot->ramdisk_type = check_type(boot->ramdisk);
|
|
|
|
boot->kernel_type = check_type(boot->kernel);
|
2017-07-12 18:14:10 +00:00
|
|
|
|
|
|
|
// Check MTK
|
2017-09-12 14:09:10 +00:00
|
|
|
if (boot->kernel_type == MTK) {
|
|
|
|
fprintf(stderr, "MTK_KERNEL_HDR [512]\n");
|
|
|
|
boot->flags |= MTK_KERNEL;
|
|
|
|
memcpy(&boot->mtk_kernel_hdr, boot->kernel, sizeof(mtk_hdr));
|
|
|
|
boot->kernel += 512;
|
|
|
|
boot->hdr.kernel_size -= 512;
|
|
|
|
boot->kernel_type = check_type(boot->kernel);
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
2017-09-12 14:09:10 +00:00
|
|
|
if (boot->ramdisk_type == MTK) {
|
|
|
|
fprintf(stderr, "MTK_RAMDISK_HDR [512]\n");
|
|
|
|
boot->flags |= MTK_RAMDISK;
|
|
|
|
memcpy(&boot->mtk_ramdisk_hdr, boot->ramdisk, sizeof(mtk_hdr));
|
|
|
|
boot->ramdisk += 512;
|
|
|
|
boot->hdr.ramdisk_size -= 512;
|
2017-11-16 14:26:28 +00:00
|
|
|
boot->ramdisk_type = check_type(boot->ramdisk);
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 14:09:10 +00:00
|
|
|
char fmt[16];
|
|
|
|
|
|
|
|
get_type_name(boot->kernel_type, fmt);
|
|
|
|
fprintf(stderr, "KERNEL_FMT [%s]\n", fmt);
|
|
|
|
get_type_name(boot->ramdisk_type, fmt);
|
|
|
|
fprintf(stderr, "RAMDISK_FMT [%s]\n", fmt);
|
|
|
|
|
2017-12-06 04:51:16 +00:00
|
|
|
return boot->flags & CHROMEOS_FLAG ? CHROMEOS_RET :
|
|
|
|
((is_blk && boot->tail_size < 500 * 1024) ? INSUF_BLOCK_RET : 0);
|
2017-07-12 18:14:10 +00:00
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2017-09-14 15:11:56 +00:00
|
|
|
LOGE("No boot image magic found!\n");
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void unpack(const char* image) {
|
2017-09-12 14:09:10 +00:00
|
|
|
boot_img boot;
|
2017-12-06 04:51:16 +00:00
|
|
|
int ret = parse_img(image, &boot);
|
|
|
|
int fd;
|
2017-07-12 18:14:10 +00:00
|
|
|
|
|
|
|
// Dump kernel
|
2017-10-07 14:08:10 +00:00
|
|
|
if (COMPRESSED(boot.kernel_type)) {
|
2017-11-09 17:51:41 +00:00
|
|
|
fd = creat(KERNEL_FILE, 0644);
|
2017-09-12 14:09:10 +00:00
|
|
|
decomp(boot.kernel_type, fd, boot.kernel, boot.hdr.kernel_size);
|
|
|
|
close(fd);
|
2017-10-07 14:08:10 +00:00
|
|
|
} else {
|
|
|
|
dump(boot.kernel, boot.hdr.kernel_size, KERNEL_FILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (boot.dt_size) {
|
|
|
|
// Dump dtb
|
|
|
|
dump(boot.dtb, boot.dt_size, DTB_FILE);
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dump ramdisk
|
2017-10-07 14:08:10 +00:00
|
|
|
if (COMPRESSED(boot.ramdisk_type)) {
|
2017-11-09 17:51:41 +00:00
|
|
|
fd = creat(RAMDISK_FILE, 0644);
|
2017-09-12 14:09:10 +00:00
|
|
|
decomp(boot.ramdisk_type, fd, boot.ramdisk, boot.hdr.ramdisk_size);
|
|
|
|
close(fd);
|
2017-10-07 14:08:10 +00:00
|
|
|
} else {
|
|
|
|
dump(boot.ramdisk, boot.hdr.ramdisk_size, RAMDISK_FILE ".raw");
|
|
|
|
LOGE("Unknown ramdisk format! Dumped to %s\n", RAMDISK_FILE ".raw");
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 14:09:10 +00:00
|
|
|
if (boot.hdr.second_size) {
|
2017-07-12 18:14:10 +00:00
|
|
|
// Dump second
|
2017-09-12 14:09:10 +00:00
|
|
|
dump(boot.second, boot.hdr.second_size, SECOND_FILE);
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 14:08:10 +00:00
|
|
|
if (boot.hdr.extra_size) {
|
|
|
|
// Dump extra
|
|
|
|
dump(boot.extra, boot.hdr.extra_size, EXTRA_FILE);
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 04:51:16 +00:00
|
|
|
munmap(boot.map_addr, boot.map_size);
|
2017-07-12 18:14:10 +00:00
|
|
|
exit(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void repack(const char* orig_image, const char* out_image) {
|
2017-09-12 14:09:10 +00:00
|
|
|
boot_img boot;
|
2017-07-12 18:14:10 +00:00
|
|
|
|
|
|
|
// There are possible two MTK headers
|
|
|
|
size_t mtk_kernel_off, mtk_ramdisk_off;
|
|
|
|
|
|
|
|
// Parse original image
|
2017-12-06 04:51:16 +00:00
|
|
|
parse_img(orig_image, &boot);
|
2017-07-12 18:14:10 +00:00
|
|
|
|
2017-12-20 19:36:18 +00:00
|
|
|
fprintf(stderr, "Repack to boot image: [%s]\n", out_image);
|
2017-07-12 18:14:10 +00:00
|
|
|
|
|
|
|
// Create new image
|
2017-11-09 17:51:41 +00:00
|
|
|
int fd = creat(out_image, 0644);
|
2017-07-12 18:14:10 +00:00
|
|
|
|
|
|
|
// Skip a page for header
|
2017-09-12 14:09:10 +00:00
|
|
|
write_zero(fd, boot.hdr.page_size);
|
2017-07-12 18:14:10 +00:00
|
|
|
|
2017-09-12 14:09:10 +00:00
|
|
|
if (boot.flags & MTK_KERNEL) {
|
|
|
|
// Record position and skip MTK header
|
2017-07-12 18:14:10 +00:00
|
|
|
mtk_kernel_off = lseek(fd, 0, SEEK_CUR);
|
2017-09-12 09:00:45 +00:00
|
|
|
write_zero(fd, 512);
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
2017-10-07 14:08:10 +00:00
|
|
|
if (COMPRESSED(boot.kernel_type)) {
|
2017-09-12 14:09:10 +00:00
|
|
|
size_t raw_size;
|
|
|
|
void *kernel_raw;
|
|
|
|
mmap_ro(KERNEL_FILE, &kernel_raw, &raw_size);
|
|
|
|
boot.hdr.kernel_size = comp(boot.kernel_type, fd, kernel_raw, raw_size);
|
|
|
|
munmap(kernel_raw, raw_size);
|
2017-10-07 14:08:10 +00:00
|
|
|
} else {
|
|
|
|
boot.hdr.kernel_size = restore(KERNEL_FILE, fd);
|
2017-09-12 14:09:10 +00:00
|
|
|
}
|
2017-10-07 14:08:10 +00:00
|
|
|
// Restore dtb
|
|
|
|
if (boot.dt_size && access(DTB_FILE, R_OK) == 0) {
|
2017-09-12 14:09:10 +00:00
|
|
|
boot.hdr.kernel_size += restore(DTB_FILE, fd);
|
|
|
|
}
|
|
|
|
file_align(fd, boot.hdr.page_size, 1);
|
2017-07-12 18:14:10 +00:00
|
|
|
|
2017-09-12 14:09:10 +00:00
|
|
|
if (boot.flags & MTK_RAMDISK) {
|
|
|
|
// Record position and skip MTK header
|
2017-07-12 18:14:10 +00:00
|
|
|
mtk_ramdisk_off = lseek(fd, 0, SEEK_CUR);
|
2017-09-12 09:00:45 +00:00
|
|
|
write_zero(fd, 512);
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
|
|
|
if (access(RAMDISK_FILE, R_OK) == 0) {
|
|
|
|
// If we found raw cpio, compress to original format
|
|
|
|
size_t cpio_size;
|
2017-09-12 07:27:28 +00:00
|
|
|
void *cpio;
|
2017-07-12 18:14:10 +00:00
|
|
|
mmap_ro(RAMDISK_FILE, &cpio, &cpio_size);
|
2017-09-12 14:09:10 +00:00
|
|
|
boot.hdr.ramdisk_size = comp(boot.ramdisk_type, fd, cpio, cpio_size);
|
2017-07-12 18:14:10 +00:00
|
|
|
munmap(cpio, cpio_size);
|
2017-09-12 09:00:45 +00:00
|
|
|
} else {
|
|
|
|
// Find compressed ramdisk
|
|
|
|
char name[PATH_MAX];
|
|
|
|
int found = 0;
|
|
|
|
for (int i = 0; SUP_EXT_LIST[i]; ++i) {
|
|
|
|
sprintf(name, "%s.%s", RAMDISK_FILE, SUP_EXT_LIST[i]);
|
|
|
|
if (access(name, R_OK) == 0) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
2017-09-12 09:00:45 +00:00
|
|
|
if (!found)
|
2017-09-14 15:11:56 +00:00
|
|
|
LOGE("No ramdisk exists!\n");
|
2017-09-12 14:09:10 +00:00
|
|
|
boot.hdr.ramdisk_size = restore(name, fd);
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
2017-09-12 14:09:10 +00:00
|
|
|
file_align(fd, boot.hdr.page_size, 1);
|
2017-07-12 18:14:10 +00:00
|
|
|
|
|
|
|
// Restore second
|
2017-09-12 14:09:10 +00:00
|
|
|
if (boot.hdr.second_size && access(SECOND_FILE, R_OK) == 0) {
|
|
|
|
boot.hdr.second_size = restore(SECOND_FILE, fd);
|
|
|
|
file_align(fd, boot.hdr.page_size, 1);
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 14:08:10 +00:00
|
|
|
// Restore extra
|
|
|
|
if (boot.hdr.extra_size && access(EXTRA_FILE, R_OK) == 0) {
|
|
|
|
boot.hdr.extra_size = restore(EXTRA_FILE, fd);
|
2017-09-12 14:09:10 +00:00
|
|
|
file_align(fd, boot.hdr.page_size, 1);
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 14:08:10 +00:00
|
|
|
// Check tail info, currently only for LG Bump and Samsung SEANDROIDENFORCE
|
|
|
|
if (boot.tail_size >= 16) {
|
|
|
|
if (memcmp(boot.tail, "SEANDROIDENFORCE", 16) == 0 ||
|
|
|
|
memcmp(boot.tail, LG_BUMP_MAGIC, 16) == 0 ) {
|
|
|
|
restore_buf(fd, boot.tail, 16);
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 09:00:45 +00:00
|
|
|
// Write MTK headers back
|
2017-09-12 14:09:10 +00:00
|
|
|
if (boot.flags & MTK_KERNEL) {
|
2017-07-12 18:14:10 +00:00
|
|
|
lseek(fd, mtk_kernel_off, SEEK_SET);
|
2017-09-12 14:09:10 +00:00
|
|
|
boot.mtk_kernel_hdr.size = boot.hdr.kernel_size;
|
|
|
|
boot.hdr.kernel_size += 512;
|
|
|
|
restore_buf(fd, &boot.mtk_kernel_hdr, sizeof(mtk_hdr));
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
2017-09-12 14:09:10 +00:00
|
|
|
if (boot.flags & MTK_RAMDISK) {
|
2017-07-12 18:14:10 +00:00
|
|
|
lseek(fd, mtk_ramdisk_off, SEEK_SET);
|
2017-09-12 14:09:10 +00:00
|
|
|
boot.mtk_ramdisk_hdr.size = boot.hdr.ramdisk_size;
|
|
|
|
boot.hdr.ramdisk_size += 512;
|
|
|
|
restore_buf(fd, &boot.mtk_ramdisk_hdr, sizeof(mtk_hdr));
|
2017-07-12 18:14:10 +00:00
|
|
|
}
|
|
|
|
// Main header
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
2017-09-12 14:09:10 +00:00
|
|
|
restore_buf(fd, &boot.hdr, sizeof(boot.hdr));
|
2017-07-12 18:14:10 +00:00
|
|
|
|
|
|
|
// Print new image info
|
2017-09-12 14:09:10 +00:00
|
|
|
print_hdr(&boot.hdr);
|
2017-07-12 18:14:10 +00:00
|
|
|
|
2017-12-06 04:51:16 +00:00
|
|
|
munmap(boot.map_addr, boot.map_size);
|
2017-07-12 18:14:10 +00:00
|
|
|
close(fd);
|
|
|
|
}
|