2016-09-08 12:59:48 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/sendfile.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-12-09 07:57:10 +00:00
|
|
|
#include "bootimg.h"
|
2017-02-24 19:29:12 +00:00
|
|
|
#include "elf.h"
|
2016-09-08 12:59:48 +00:00
|
|
|
|
2017-02-23 23:45:48 +00:00
|
|
|
// Global pointer of current positions
|
2017-02-24 06:58:44 +00:00
|
|
|
static int ofd, opos;
|
2016-09-08 12:59:48 +00:00
|
|
|
|
2017-02-24 19:29:12 +00:00
|
|
|
static size_t restore(const char *filename) {
|
2016-09-08 12:59:48 +00:00
|
|
|
int fd = open(filename, O_RDONLY);
|
2017-02-23 23:45:48 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "Cannot open %s\n", filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
size_t size = lseek(fd, 0, SEEK_END);
|
2016-09-08 12:59:48 +00:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
2017-02-23 23:45:48 +00:00
|
|
|
if (sendfile(ofd, fd, NULL, size) < 0) {
|
|
|
|
fprintf(stderr, "Cannot write %s\n", filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-09-08 12:59:48 +00:00
|
|
|
close(fd);
|
2017-02-23 23:45:48 +00:00
|
|
|
opos += size;
|
2016-09-08 12:59:48 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2017-02-24 19:29:12 +00:00
|
|
|
static void restore_buf(size_t size, const void *buf) {
|
|
|
|
if (write(ofd, buf, size) != size) {
|
2017-02-23 23:45:48 +00:00
|
|
|
fprintf(stderr, "Cannot dump from input file\n");
|
|
|
|
exit(1);
|
2016-09-08 12:59:48 +00:00
|
|
|
}
|
2017-02-23 23:45:48 +00:00
|
|
|
opos += size;
|
2016-09-08 12:59:48 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 19:29:12 +00:00
|
|
|
static void page_align() {
|
|
|
|
uint32_t pagemask = hdr.page_size - 1L;
|
2017-02-23 23:45:48 +00:00
|
|
|
if (opos & pagemask) {
|
2017-02-24 19:29:12 +00:00
|
|
|
opos += hdr.page_size - (opos & pagemask);
|
2017-02-23 23:45:48 +00:00
|
|
|
}
|
|
|
|
ftruncate(ofd, opos);
|
|
|
|
lseek(ofd, 0, SEEK_END);
|
|
|
|
}
|
2016-09-08 12:59:48 +00:00
|
|
|
|
2017-02-24 19:29:12 +00:00
|
|
|
int repack(const char* image) {
|
|
|
|
// Load original boot
|
|
|
|
int ifd = open(image, O_RDONLY), ret = -1;
|
|
|
|
if (ifd < 0)
|
|
|
|
error(1, "Cannot open %s", image);
|
2017-02-23 23:45:48 +00:00
|
|
|
|
2017-02-24 19:29:12 +00:00
|
|
|
size_t isize = lseek(ifd, 0, SEEK_END);
|
|
|
|
lseek(ifd, 0, SEEK_SET);
|
|
|
|
unsigned char *orig = mmap(NULL, isize, PROT_READ, MAP_SHARED, ifd, 0);
|
|
|
|
|
|
|
|
// Create new boot image
|
|
|
|
unlink("new-boot.img");
|
|
|
|
ofd = open("new-boot.img", O_RDWR | O_CREAT, 0644);
|
|
|
|
|
|
|
|
// Parse images
|
|
|
|
for(base = orig; base < (orig + isize); base += 256) {
|
|
|
|
if (memcmp(base, BOOT_MAGIC, BOOT_MAGIC_SIZE) == 0) {
|
|
|
|
parse_aosp();
|
|
|
|
break;
|
|
|
|
} else if (memcmp(base, ELF_MAGIC, ELF_MAGIC_SIZE) == 0) {
|
|
|
|
parse_elf();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-02-23 23:45:48 +00:00
|
|
|
|
2017-02-24 19:29:12 +00:00
|
|
|
printf("\n");
|
2017-02-23 23:45:48 +00:00
|
|
|
|
2017-02-24 19:29:12 +00:00
|
|
|
char *name;
|
|
|
|
|
2017-02-23 23:45:48 +00:00
|
|
|
// Set all sizes to 0
|
|
|
|
hdr.kernel_size = 0;
|
|
|
|
hdr.ramdisk_size = 0;
|
|
|
|
hdr.second_size = 0;
|
|
|
|
hdr.dt_size = 0;
|
|
|
|
|
2017-02-24 19:29:12 +00:00
|
|
|
// Skip a page for header
|
2017-02-23 23:45:48 +00:00
|
|
|
ftruncate(ofd, hdr.page_size);
|
|
|
|
lseek(ofd, 0, SEEK_END);
|
|
|
|
opos += hdr.page_size;
|
|
|
|
|
2017-02-24 19:29:12 +00:00
|
|
|
// Restore kernel
|
|
|
|
if (memcmp(kernel, "\x88\x16\x88\x58", 4) == 0) {
|
|
|
|
printf("Dumping MTK header back to kernel\n");
|
|
|
|
restore_buf(512, kernel);
|
2017-02-23 23:45:48 +00:00
|
|
|
hdr.kernel_size += 512;
|
|
|
|
}
|
2017-02-24 19:29:12 +00:00
|
|
|
hdr.kernel_size += restore("kernel");
|
|
|
|
page_align();
|
2017-02-23 23:45:48 +00:00
|
|
|
|
|
|
|
// Dump ramdisk
|
2017-02-24 19:29:12 +00:00
|
|
|
if (memcmp(ramdisk, "\x88\x16\x88\x58", 4) == 0) {
|
2017-02-23 23:45:48 +00:00
|
|
|
printf("Dumping MTK header back to ramdisk\n");
|
2017-02-24 19:29:12 +00:00
|
|
|
restore_buf(512, ramdisk);
|
2017-02-23 23:45:48 +00:00
|
|
|
hdr.ramdisk_size += 512;
|
|
|
|
}
|
|
|
|
if (access("ramdisk.gz", R_OK) == 0) {
|
|
|
|
name = "ramdisk.gz";
|
|
|
|
} else if (access("ramdisk.lzo", R_OK) == 0) {
|
|
|
|
name = "ramdisk.lzo";
|
|
|
|
} else if (access("ramdisk.xz", R_OK) == 0) {
|
|
|
|
name = "ramdisk.xz";
|
|
|
|
} else if (access("ramdisk.lzma", R_OK) == 0) {
|
|
|
|
name = "ramdisk.lzma";
|
|
|
|
} else if (access("ramdisk.bz2", R_OK) == 0) {
|
|
|
|
name = "ramdisk.bz2";
|
|
|
|
} else if (access("ramdisk.lz4", R_OK) == 0) {
|
|
|
|
name = "ramdisk.lz4";
|
|
|
|
} else {
|
2017-02-24 19:29:12 +00:00
|
|
|
error(1, "Ramdisk file doesn't exist!");
|
2016-09-08 12:59:48 +00:00
|
|
|
}
|
2017-02-24 19:29:12 +00:00
|
|
|
hdr.ramdisk_size += restore(name);
|
|
|
|
page_align();
|
2016-09-08 12:59:48 +00:00
|
|
|
|
2017-02-23 23:45:48 +00:00
|
|
|
// Dump second
|
|
|
|
if (access("second", R_OK) == 0) {
|
2017-02-24 19:29:12 +00:00
|
|
|
hdr.second_size += restore("second");
|
|
|
|
page_align();
|
2016-09-08 12:59:48 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 23:45:48 +00:00
|
|
|
// Dump dtb
|
|
|
|
if (access("dtb", R_OK) == 0) {
|
2017-02-24 19:29:12 +00:00
|
|
|
hdr.dt_size += restore("dtb");
|
|
|
|
page_align();
|
2016-09-08 12:59:48 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 19:29:12 +00:00
|
|
|
print_header();
|
|
|
|
|
2017-02-23 23:45:48 +00:00
|
|
|
// Write header back
|
|
|
|
lseek(ofd, 0, SEEK_SET);
|
|
|
|
write(ofd, &hdr, sizeof(hdr));
|
2016-09-08 12:59:48 +00:00
|
|
|
|
2017-02-23 23:45:48 +00:00
|
|
|
munmap(orig, isize);
|
|
|
|
close(ifd);
|
2017-02-24 19:29:12 +00:00
|
|
|
close(ofd);
|
2017-02-23 23:45:48 +00:00
|
|
|
return ret;
|
|
|
|
}
|