Fix Pixel C installation

This commit is contained in:
topjohnwu 2017-10-07 22:08:10 +08:00
parent 89330b89d8
commit c215447405
10 changed files with 103 additions and 77 deletions

View File

@ -8,7 +8,7 @@ void mmap_ro(const char *filename, void **buf, size_t *size) {
int fd = xopen(filename, O_RDONLY); int fd = xopen(filename, O_RDONLY);
*size = lseek(fd, 0, SEEK_END); *size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET); lseek(fd, 0, SEEK_SET);
*buf = xmmap(NULL, *size, PROT_READ, MAP_SHARED, fd, 0); *buf = *size > 0 ? xmmap(NULL, *size, PROT_READ, MAP_SHARED, fd, 0) : NULL;
close(fd); close(fd);
} }
@ -16,7 +16,7 @@ void mmap_rw(const char *filename, void **buf, size_t *size) {
int fd = xopen(filename, O_RDWR); int fd = xopen(filename, O_RDWR);
*size = lseek(fd, 0, SEEK_END); *size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET); lseek(fd, 0, SEEK_SET);
*buf = xmmap(NULL, *size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); *buf = *size > 0 ? xmmap(NULL, *size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0) : NULL;
close(fd); close(fd);
} }

View File

@ -30,7 +30,7 @@ static void print_hdr(const boot_img_hdr *hdr) {
fprintf(stderr, "KERNEL [%d] @ 0x%08x\n", hdr->kernel_size, hdr->kernel_addr); 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, "RAMDISK [%d] @ 0x%08x\n", hdr->ramdisk_size, hdr->ramdisk_addr);
fprintf(stderr, "SECOND [%d] @ 0x%08x\n", hdr->second_size, hdr->second_addr); fprintf(stderr, "SECOND [%d] @ 0x%08x\n", hdr->second_size, hdr->second_addr);
fprintf(stderr, "DTB [%d] @ 0x%08x\n", hdr->dt_size, hdr->tags_addr); fprintf(stderr, "EXTRA [%d] @ 0x%08x\n", hdr->extra_size, hdr->tags_addr);
fprintf(stderr, "PAGESIZE [%d]\n", hdr->page_size); fprintf(stderr, "PAGESIZE [%d]\n", hdr->page_size);
if (hdr->os_version != 0) { if (hdr->os_version != 0) {
int a,b,c,y,m = 0; int a,b,c,y,m = 0;
@ -88,26 +88,24 @@ int parse_img(void *orig, size_t size, boot_img *boot) {
mem_align(&pos, boot->hdr.page_size); mem_align(&pos, boot->hdr.page_size);
} }
if (boot->hdr.dt_size) { if (boot->hdr.extra_size) {
boot->dtb = base + pos; boot->extra = base + pos;
pos += boot->hdr.dt_size; pos += boot->hdr.extra_size;
mem_align(&pos, boot->hdr.page_size); mem_align(&pos, boot->hdr.page_size);
} }
if (pos < size) { if (pos < size) {
boot->extra = base + pos; boot->tail = base + pos;
boot->tail_size = end - base - pos;
} }
// Search for dtb in kernel if not found // Search for dtb in kernel
if (boot->hdr.dt_size == 0) { for (int i = 0; i < boot->hdr.kernel_size; ++i) {
for (int i = 0; i < boot->hdr.kernel_size; ++i) { if (memcmp(boot->kernel + i, DTB_MAGIC, 4) == 0) {
if (memcmp(boot->kernel + i, DTB_MAGIC, 4) == 0) { boot->dtb = boot->kernel + i;
boot->flags |= APPEND_DTB; boot->dt_size = boot->hdr.kernel_size - i;
boot->dtb = boot->kernel + i; boot->hdr.kernel_size = i;
boot->hdr.dt_size = boot->hdr.kernel_size - i; fprintf(stderr, "DTB [%d]\n", boot->dt_size);
boot->hdr.kernel_size = i;
fprintf(stderr, "APPEND_DTB [%d]\n", boot->hdr.dt_size);
}
} }
} }
@ -146,6 +144,7 @@ int parse_img(void *orig, size_t size, boot_img *boot) {
} }
} }
LOGE("No boot image magic found!\n"); LOGE("No boot image magic found!\n");
return 1;
} }
void unpack(const char* image) { void unpack(const char* image) {
@ -160,22 +159,27 @@ void unpack(const char* image) {
int ret = parse_img(orig, size, &boot); int ret = parse_img(orig, size, &boot);
// Dump kernel // Dump kernel
if (boot.kernel_type == UNKNOWN) { if (COMPRESSED(boot.kernel_type)) {
dump(boot.kernel, boot.hdr.kernel_size, KERNEL_FILE);
} else {
fd = open_new(KERNEL_FILE); fd = open_new(KERNEL_FILE);
decomp(boot.kernel_type, fd, boot.kernel, boot.hdr.kernel_size); decomp(boot.kernel_type, fd, boot.kernel, boot.hdr.kernel_size);
close(fd); close(fd);
} else {
dump(boot.kernel, boot.hdr.kernel_size, KERNEL_FILE);
}
if (boot.dt_size) {
// Dump dtb
dump(boot.dtb, boot.dt_size, DTB_FILE);
} }
// Dump ramdisk // Dump ramdisk
if (boot.ramdisk_type == UNKNOWN) { if (COMPRESSED(boot.ramdisk_type)) {
dump(boot.ramdisk, boot.hdr.ramdisk_size, RAMDISK_FILE ".raw");
LOGE("Unknown ramdisk format! Dumped to %s\n", RAMDISK_FILE ".raw");
} else {
fd = open_new(RAMDISK_FILE); fd = open_new(RAMDISK_FILE);
decomp(boot.ramdisk_type, fd, boot.ramdisk, boot.hdr.ramdisk_size); decomp(boot.ramdisk_type, fd, boot.ramdisk, boot.hdr.ramdisk_size);
close(fd); close(fd);
} else {
dump(boot.ramdisk, boot.hdr.ramdisk_size, RAMDISK_FILE ".raw");
LOGE("Unknown ramdisk format! Dumped to %s\n", RAMDISK_FILE ".raw");
} }
if (boot.hdr.second_size) { if (boot.hdr.second_size) {
@ -183,9 +187,9 @@ void unpack(const char* image) {
dump(boot.second, boot.hdr.second_size, SECOND_FILE); dump(boot.second, boot.hdr.second_size, SECOND_FILE);
} }
if (boot.hdr.dt_size) { if (boot.hdr.extra_size) {
// Dump dtb // Dump extra
dump(boot.dtb, boot.hdr.dt_size, DTB_FILE); dump(boot.extra, boot.hdr.extra_size, EXTRA_FILE);
} }
munmap(orig, size); munmap(orig, size);
@ -220,18 +224,18 @@ void repack(const char* orig_image, const char* out_image) {
mtk_kernel_off = lseek(fd, 0, SEEK_CUR); mtk_kernel_off = lseek(fd, 0, SEEK_CUR);
write_zero(fd, 512); write_zero(fd, 512);
} }
if (boot.kernel_type == UNKNOWN) { if (COMPRESSED(boot.kernel_type)) {
boot.hdr.kernel_size = restore(KERNEL_FILE, fd);
} else {
size_t raw_size; size_t raw_size;
void *kernel_raw; void *kernel_raw;
mmap_ro(KERNEL_FILE, &kernel_raw, &raw_size); mmap_ro(KERNEL_FILE, &kernel_raw, &raw_size);
boot.hdr.kernel_size = comp(boot.kernel_type, fd, kernel_raw, raw_size); boot.hdr.kernel_size = comp(boot.kernel_type, fd, kernel_raw, raw_size);
munmap(kernel_raw, raw_size); munmap(kernel_raw, raw_size);
} else {
boot.hdr.kernel_size = restore(KERNEL_FILE, fd);
} }
if (boot.flags & APPEND_DTB) { // Restore dtb
if (boot.dt_size && access(DTB_FILE, R_OK) == 0) {
boot.hdr.kernel_size += restore(DTB_FILE, fd); boot.hdr.kernel_size += restore(DTB_FILE, fd);
boot.hdr.dt_size = 0;
} }
file_align(fd, boot.hdr.page_size, 1); file_align(fd, boot.hdr.page_size, 1);
@ -270,18 +274,17 @@ void repack(const char* orig_image, const char* out_image) {
file_align(fd, boot.hdr.page_size, 1); file_align(fd, boot.hdr.page_size, 1);
} }
// Restore dtb // Restore extra
if (boot.hdr.dt_size && access(DTB_FILE, R_OK) == 0) { if (boot.hdr.extra_size && access(EXTRA_FILE, R_OK) == 0) {
printf("Here\n"); boot.hdr.extra_size = restore(EXTRA_FILE, fd);
boot.hdr.dt_size = restore(DTB_FILE, fd);
file_align(fd, boot.hdr.page_size, 1); file_align(fd, boot.hdr.page_size, 1);
} }
// Check extra info, currently only for LG Bump and Samsung SEANDROIDENFORCE // Check tail info, currently only for LG Bump and Samsung SEANDROIDENFORCE
if (boot.extra) { if (boot.tail_size >= 16) {
if (memcmp(boot.extra, "SEANDROIDENFORCE", 16) == 0 || if (memcmp(boot.tail, "SEANDROIDENFORCE", 16) == 0 ||
memcmp(boot.extra, LG_BUMP_MAGIC, 16) == 0 ) { memcmp(boot.tail, LG_BUMP_MAGIC, 16) == 0 ) {
restore_buf(fd, boot.extra, 16); restore_buf(fd, boot.tail, 16);
} }
} }

View File

@ -44,7 +44,7 @@ struct boot_img_hdr
uint32_t tags_addr; /* physical addr for kernel tags */ uint32_t tags_addr; /* physical addr for kernel tags */
uint32_t page_size; /* flash page size we assume */ uint32_t page_size; /* flash page size we assume */
uint32_t dt_size; /* device tree in bytes */ uint32_t extra_size; /* extra blob size in bytes */
/* operating system version and security patch level; for /* operating system version and security patch level; for
* version "A.B.C" and patch level "Y-M-D": * version "A.B.C" and patch level "Y-M-D":
@ -74,13 +74,13 @@ struct boot_img_hdr
** +-----------------+ ** +-----------------+
** | second stage | o pages ** | second stage | o pages
** +-----------------+ ** +-----------------+
** | device tree | p pages ** | extra blobs | p pages
** +-----------------+ ** +-----------------+
** **
** n = (kernel_size + page_size - 1) / page_size ** n = (kernel_size + page_size - 1) / page_size
** m = (ramdisk_size + page_size - 1) / page_size ** m = (ramdisk_size + page_size - 1) / page_size
** o = (second_size + page_size - 1) / page_size ** o = (second_size + page_size - 1) / page_size
** p = (dt_size + page_size - 1) / page_size ** p = (extra_size + page_size - 1) / page_size
** **
** 0. all entities are page_size aligned in flash ** 0. all entities are page_size aligned in flash
** 1. kernel and ramdisk are required (size != 0) ** 1. kernel and ramdisk are required (size != 0)
@ -103,16 +103,18 @@ typedef struct mtk_hdr {
// Flags // Flags
#define MTK_KERNEL 0x1 #define MTK_KERNEL 0x1
#define MTK_RAMDISK 0x2 #define MTK_RAMDISK 0x2
#define APPEND_DTB 0x4
typedef struct boot_img { typedef struct boot_img {
boot_img_hdr hdr; boot_img_hdr hdr;
void *kernel; void *kernel;
void *dtb;
uint32_t dt_size;
void *ramdisk; void *ramdisk;
void *second; void *second;
void *dtb;
void *extra; void *extra;
int flags; void *tail;
uint32_t tail_size;
uint32_t flags;
file_t kernel_type, ramdisk_type; file_t kernel_type, ramdisk_type;
mtk_hdr mtk_kernel_hdr, mtk_ramdisk_hdr; mtk_hdr mtk_kernel_hdr, mtk_ramdisk_hdr;
} boot_img; } boot_img;

View File

@ -5,16 +5,14 @@
#include "magiskboot.h" #include "magiskboot.h"
#include "utils.h" #include "utils.h"
/* Left here for debugging */ static void print_subnode(const void *fdt, int parent, int depth) {
int node;
// static void print_subnode(const void *fdt, int parent, int depth) { fdt_for_each_subnode(node, fdt, parent) {
// int node; for (int i = 0; i < depth; ++i) printf(" ");
// fdt_for_each_subnode(node, fdt, parent) { printf("%d: %s\n", node, fdt_get_name(fdt, node, NULL));
// for (int i = 0; i < depth; ++i) printf(" "); print_subnode(fdt, node, depth + 1);
// printf("%d: %s\n", node, fdt_get_name(fdt, node, NULL)); }
// print_subnode(fdt, node, depth + 1); }
// }
// }
static int find_fstab(const void *fdt, int parent) { static int find_fstab(const void *fdt, int parent) {
int node, fstab; int node, fstab;
@ -28,6 +26,25 @@ static int find_fstab(const void *fdt, int parent) {
return -1; return -1;
} }
void dtb_print(const char *file) {
size_t size ;
void *dtb, *fdt;
fprintf(stderr, "Loading dtbs from [%s]\n", file);
mmap_ro(file, &dtb, &size);
// Loop through all the dtbs
int dtb_num = 0;
for (int i = 0; i < size; ++i) {
if (memcmp(dtb + i, DTB_MAGIC, 4) == 0) {
fdt = dtb + i;
fprintf(stderr, "\nPrinting dtb.%04d\n\n", dtb_num++);
print_subnode(fdt, 0, 0);
}
}
fprintf(stderr, "\n");
munmap(dtb, size);
exit(0);
}
void dtb_patch(const char *file) { void dtb_patch(const char *file) {
size_t size ; size_t size ;
void *dtb, *fdt; void *dtb, *fdt;

View File

@ -24,7 +24,7 @@ void hexpatch(const char *image, const char *from, const char *to) {
patch = xmalloc(patchsize); patch = xmalloc(patchsize);
hex2byte(from, pattern); hex2byte(from, pattern);
hex2byte(to, patch); hex2byte(to, patch);
for (size_t i = 0; i < filesize - patternsize; ++i) { for (size_t i = 0; filesize > 0 && i < filesize - patternsize; ++i) {
if (memcmp(file + i, pattern, patternsize) == 0) { if (memcmp(file + i, pattern, patternsize) == 0) {
fprintf(stderr, "Pattern %s found!\nPatching to %s\n", from, to); fprintf(stderr, "Pattern %s found!\nPatching to %s\n", from, to);
memset(file + i, 0, patternsize); memset(file + i, 0, patternsize);

View File

@ -8,6 +8,7 @@
#define KERNEL_FILE "kernel" #define KERNEL_FILE "kernel"
#define RAMDISK_FILE "ramdisk.cpio" #define RAMDISK_FILE "ramdisk.cpio"
#define SECOND_FILE "second" #define SECOND_FILE "second"
#define EXTRA_FILE "extra"
#define DTB_FILE "dtb" #define DTB_FILE "dtb"
#define NEW_BOOT "new-boot.img" #define NEW_BOOT "new-boot.img"
@ -22,6 +23,7 @@ int parse_img(void *orig, size_t size, boot_img *boot);
int cpio_commands(const char *command, int argc, char *argv[]); int cpio_commands(const char *command, int argc, char *argv[]);
void comp_file(const char *method, const char *from, const char *to); void comp_file(const char *method, const char *from, const char *to);
void decomp_file(char *from, const char *to); void decomp_file(char *from, const char *to);
void dtb_print(const char *file);
void dtb_patch(const char *file); void dtb_patch(const char *file);
// Compressions // Compressions

View File

@ -54,6 +54,9 @@ static void usage(char *arg0) {
" -stocksha1\n" " -stocksha1\n"
" Get stock boot SHA1 recorded within <incpio>\n" " Get stock boot SHA1 recorded within <incpio>\n"
"\n" "\n"
" --dtb-print <dtb>\n"
" Print all nodes in <dtb>, for debugging\n"
"\n"
" --dtb-patch <dtb>\n" " --dtb-patch <dtb>\n"
" Search for fstab in <dtb> and remove verity checks\n" " Search for fstab in <dtb> and remove verity checks\n"
"\n" "\n"
@ -94,6 +97,7 @@ int main(int argc, char *argv[]) {
unlink(RAMDISK_FILE ".raw"); unlink(RAMDISK_FILE ".raw");
unlink(SECOND_FILE); unlink(SECOND_FILE);
unlink(DTB_FILE); unlink(DTB_FILE);
unlink(EXTRA_FILE);
for (int i = 0; SUP_EXT_LIST[i]; ++i) { for (int i = 0; SUP_EXT_LIST[i]; ++i) {
sprintf(name, "%s.%s", RAMDISK_FILE, SUP_EXT_LIST[i]); sprintf(name, "%s.%s", RAMDISK_FILE, SUP_EXT_LIST[i]);
unlink(name); unlink(name);
@ -113,6 +117,8 @@ int main(int argc, char *argv[]) {
repack(argv[2], argc > 3 ? argv[3] : NEW_BOOT); repack(argv[2], argc > 3 ? argv[3] : NEW_BOOT);
} else if (argc > 2 && strcmp(argv[1], "--decompress") == 0) { } else if (argc > 2 && strcmp(argv[1], "--decompress") == 0) {
decomp_file(argv[2], argc > 3 ? argv[3] : NULL); decomp_file(argv[2], argc > 3 ? argv[3] : NULL);
} else if (argc > 2 && strcmp(argv[1], "--dtb-print") == 0) {
dtb_print(argv[2]);
} else if (argc > 2 && strcmp(argv[1], "--dtb-patch") == 0) { } else if (argc > 2 && strcmp(argv[1], "--dtb-patch") == 0) {
dtb_patch(argv[2]); dtb_patch(argv[2]);
} else if (argc > 2 && strncmp(argv[1], "--compress", 10) == 0) { } else if (argc > 2 && strncmp(argv[1], "--compress", 10) == 0) {

View File

@ -18,6 +18,8 @@ typedef enum {
DTB DTB
} file_t; } file_t;
#define COMPRESSED(type) (type >= GZIP && type <= LZ4_LEGACY)
#define CHROMEOS_MAGIC "CHROMEOS" #define CHROMEOS_MAGIC "CHROMEOS"
#define ELF32_MAGIC "\x7f""ELF\x01" #define ELF32_MAGIC "\x7f""ELF\x01"
#define ELF64_MAGIC "\x7f""ELF\x02" #define ELF64_MAGIC "\x7f""ELF\x02"

View File

@ -66,17 +66,6 @@ cpio_mkdir() {
# Initialization # Initialization
########################################################################################## ##########################################################################################
BOOTIMAGE="$1"
[ -e "$BOOTIMAGE" ] || (echo "$BOOTIMAGE does not exist!" && exit 1)
# Presets
[ -z $KEEPVERITY ] && KEEPVERITY=false
[ -z $KEEPFORCEENCRYPT ] && KEEPFORCEENCRYPT=false
# Detect whether running as root
id | grep "uid=0" >/dev/null 2>&1 && ROOT=true || ROOT=false
if [ -z $SOURCEDMODE ]; then if [ -z $SOURCEDMODE ]; then
# Switch to the location of the script file # Switch to the location of the script file
cd "`dirname_wrap "${BASH_SOURCE:-$0}"`" cd "`dirname_wrap "${BASH_SOURCE:-$0}"`"
@ -86,6 +75,14 @@ if [ -z $SOURCEDMODE ]; then
mount_partitions mount_partitions
fi fi
BOOTIMAGE="$1"
[ -e "$BOOTIMAGE" ] || abort "$BOOTIMAGE does not exist!"
# Presets
[ -z $KEEPVERITY ] && KEEPVERITY=false
[ -z $KEEPFORCEENCRYPT ] && KEEPFORCEENCRYPT=false
chmod -R 755 . chmod -R 755 .
########################################################################################## ##########################################################################################
@ -104,6 +101,7 @@ case $? in
abort "! Unable to unpack boot image" abort "! Unable to unpack boot image"
;; ;;
2 ) 2 )
ui_print "- ChromeOS boot image detected"
CHROMEOS=true CHROMEOS=true
;; ;;
3 ) 3 )
@ -145,7 +143,7 @@ case $? in
# Restore failed # Restore failed
ui_print "! Cannot restore from internal backup" ui_print "! Cannot restore from internal backup"
# If we are root and SHA1 known, we try to find the stock backup # If we are root and SHA1 known, we try to find the stock backup
if $ROOT && [ ! -z $SHA1 ]; then if [ ! -z $SHA1 ]; then
STOCKDUMP=/data/stock_boot_${SHA1}.img STOCKDUMP=/data/stock_boot_${SHA1}.img
if [ -f ${STOCKDUMP}.gz ]; then if [ -f ${STOCKDUMP}.gz ]; then
ui_print "- Stock boot image backup found" ui_print "- Stock boot image backup found"

View File

@ -32,12 +32,7 @@ get_outfd() {
} }
ui_print() { ui_print() {
if $BOOTMODE; then $BOOTMODE && echo "$1" || echo -e "ui_print $1\nui_print" >> /proc/self/fd/$OUTFD
echo "$1"
else
echo -n -e "ui_print $1\n" >> /proc/self/fd/$OUTFD
echo -n -e "ui_print\n" >> /proc/self/fd/$OUTFD
fi
} }
mount_partitions() { mount_partitions() {
@ -155,8 +150,9 @@ flash_boot_image() {
} }
sign_chromeos() { sign_chromeos() {
echo > empty ui_print "- Signing ChromeOS boot image"
echo > empty
./chromeos/futility vbutil_kernel --pack new-boot.img.signed \ ./chromeos/futility vbutil_kernel --pack new-boot.img.signed \
--keyblock ./chromeos/kernel.keyblock --signprivate ./chromeos/kernel_data_key.vbprivk \ --keyblock ./chromeos/kernel.keyblock --signprivate ./chromeos/kernel_data_key.vbprivk \
--version 1 --vmlinuz new-boot.img --config empty --arch arm --bootloader empty --flags 0x1 --version 1 --vmlinuz new-boot.img --config empty --arch arm --bootloader empty --flags 0x1