Commit a3c49de6 authored by topjohnwu's avatar topjohnwu

Refactor magiskboot

parent e8dd1b29
...@@ -266,7 +266,7 @@ def zip_main(args): ...@@ -266,7 +266,7 @@ def zip_main(args):
with open(source, 'r') as script: with open(source, 'r') as script:
# Add version info util_functions.sh # Add version info util_functions.sh
util_func = script.read().replace( util_func = script.read().replace(
'MAGISK_VERSION_STUB', 'MAGISK_VER="{}"\nMAGISK_VER_CODE={}'.format(args.versionString, args.versionCode)) '#MAGISK_VERSION_STUB', 'MAGISK_VER="{}"\nMAGISK_VER_CODE={}'.format(args.versionString, args.versionCode))
target = os.path.join('common', 'util_functions.sh') target = os.path.join('common', 'util_functions.sh')
print('zip: ' + source + ' -> ' + target) print('zip: ' + source + ' -> ' + target)
zipf.writestr(target, util_func) zipf.writestr(target, util_func)
...@@ -316,11 +316,9 @@ def zip_uninstaller(args): ...@@ -316,11 +316,9 @@ def zip_uninstaller(args):
source = os.path.join('scripts', 'util_functions.sh') source = os.path.join('scripts', 'util_functions.sh')
with open(source, 'r') as script: with open(source, 'r') as script:
# Remove the stub # Remove the stub
util_func = script.read().replace(
'MAGISK_VERSION_STUB', '')
target = os.path.join('util_functions.sh') target = os.path.join('util_functions.sh')
print('zip: ' + source + ' -> ' + target) print('zip: ' + source + ' -> ' + target)
zipf.writestr(target, util_func) zipf.writestr(target, script.read())
# Prebuilts # Prebuilts
for chromeos in ['futility', 'kernel_data_key.vbprivk', 'kernel.keyblock']: for chromeos in ['futility', 'kernel_data_key.vbprivk', 'kernel.keyblock']:
......
...@@ -182,22 +182,24 @@ static void patch_ramdisk(int root) { ...@@ -182,22 +182,24 @@ static void patch_ramdisk(int root) {
close(fd); close(fd);
free(addr); free(addr);
char *key, *value; /* Disabled for now */
full_read("/.backup/.magisk", &addr, &size);
for (char *tok = strtok(addr, "\n"); tok; tok = strtok(NULL, "\n")) { // char *key, *value;
key = tok; // full_read("/.backup/.magisk", &addr, &size);
value = strchr(tok, '=') + 1; // for (char *tok = strtok(addr, "\n"); tok; tok = strtok(NULL, "\n")) {
value[-1] = '\0'; // key = tok;
if (strcmp(key, "KEEPVERITY") == 0) // value = strchr(tok, '=') + 1;
keepverity = strcmp(value, "true") == 0; // value[-1] = '\0';
else if (strcmp(key, "KEEPFORCEENCRYPT") == 0) // if (strcmp(key, "KEEPVERITY") == 0)
keepencrypt = strcmp(value, "true") == 0; // keepverity = strcmp(value, "true") == 0;
} // else if (strcmp(key, "KEEPFORCEENCRYPT") == 0)
// keepencrypt = strcmp(value, "true") == 0;
excl_list = (char *[]) { "system_root", "system", "vendor", NULL }; // }
in_order_walk(root, fstab_patch_cb);
if (!keepverity) // excl_list = (char *[]) { "system_root", "system", "vendor", NULL };
unlink("/verity_key"); // in_order_walk(root, fstab_patch_cb);
// if (!keepverity)
// unlink("/verity_key");
} }
static int strend(const char *s1, const char *s2) { static int strend(const char *s1, const char *s2) {
...@@ -472,17 +474,11 @@ int main(int argc, char *argv[]) { ...@@ -472,17 +474,11 @@ int main(int argc, char *argv[]) {
close(system_root); close(system_root);
} else { } else {
char *ramdisk_xz = NULL; if (access("/ramdisk.cpio.xz", R_OK) == 0) {
if (access("/ramdisk-recovery.xz", R_OK) == 0) // High compression mode
ramdisk_xz = "/ramdisk-recovery.xz";
else if (access("/ramdisk.cpio.xz", R_OK) == 0)
ramdisk_xz = "/ramdisk.cpio.xz";
if (ramdisk_xz) {
// High compress mode
void *addr; void *addr;
size_t size; size_t size;
mmap_ro(ramdisk_xz, &addr, &size); mmap_ro("/ramdisk.cpio.xz", &addr, &size);
int fd = creat("/ramdisk.cpio", 0); int fd = creat("/ramdisk.cpio", 0);
unxz(addr, size, fd); unxz(addr, size, fd);
munmap(addr, size); munmap(addr, size);
......
...@@ -42,6 +42,8 @@ typedef struct cpio_newc_header { ...@@ -42,6 +42,8 @@ typedef struct cpio_newc_header {
} cpio_newc_header; } cpio_newc_header;
// Basic cpio functions // Basic cpio functions
void cpio_free(cpio_entry *e);
int cpio_find(struct vector *v, const char *entry);
int cpio_cmp(const void *a, const void *b); int cpio_cmp(const void *a, const void *b);
void parse_cpio(struct vector *v, const char *filename); void parse_cpio(struct vector *v, const char *filename);
void dump_cpio(struct vector *v, const char *filename); void dump_cpio(struct vector *v, const char *filename);
......
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
#include <sys/types.h> #include <sys/types.h>
struct vector { struct vector {
size_t size; unsigned size;
size_t cap; unsigned cap;
void **data; void **data;
}; };
...@@ -26,11 +26,11 @@ struct vector *vec_dup(struct vector *v); ...@@ -26,11 +26,11 @@ struct vector *vec_dup(struct vector *v);
/* Usage: vec_for_each(vector *v, void *e) */ /* Usage: vec_for_each(vector *v, void *e) */
#define vec_for_each(v, e) \ #define vec_for_each(v, e) \
e = v ? (v)->data[0] : NULL; \ e = v ? (v)->data[0] : NULL; \
for (size_t _ = 0; v && _ < (v)->size; ++_, e = (v)->data[_]) for (int _ = 0; v && _ < (v)->size; ++_, e = (v)->data[_])
#define vec_for_each_r(v, e) \ #define vec_for_each_r(v, e) \
e = v ? (v)->data[(v)->size - 1] : NULL; \ e = v ? (v)->data[(v)->size - 1] : NULL; \
for (size_t _ = (v)->size; v && _ > 0; --_, e = (v)->data[_ - 1]) for (int _ = ((int) (v)->size) - 1; v && _ >= 0; --_, e = (v)->data[_])
#define vec_cur(v) vec_entry(v)[_] #define vec_cur(v) vec_entry(v)[_]
......
...@@ -55,7 +55,6 @@ static void print_hdr(const boot_img_hdr *hdr) { ...@@ -55,7 +55,6 @@ static void print_hdr(const boot_img_hdr *hdr) {
} }
fprintf(stderr, "NAME [%s]\n", hdr->name); fprintf(stderr, "NAME [%s]\n", hdr->name);
fprintf(stderr, "CMDLINE [%s]\n", hdr->cmdline); fprintf(stderr, "CMDLINE [%s]\n", hdr->cmdline);
fprintf(stderr, "\n");
} }
int parse_img(const char *image, boot_img *boot) { int parse_img(const char *image, boot_img *boot) {
...@@ -63,7 +62,7 @@ int parse_img(const char *image, boot_img *boot) { ...@@ -63,7 +62,7 @@ int parse_img(const char *image, boot_img *boot) {
int is_blk = mmap_ro(image, &boot->map_addr, &boot->map_size); int is_blk = mmap_ro(image, &boot->map_addr, &boot->map_size);
// Parse image // Parse image
fprintf(stderr, "Parsing boot image: [%s]\n\n", image); fprintf(stderr, "Parsing boot image: [%s]\n", image);
for (size_t pos = 0; pos < boot->map_size; pos += 256) { for (size_t pos = 0; pos < boot->map_size; pos += 256) {
switch (check_type(boot->map_addr + pos)) { switch (check_type(boot->map_addr + pos)) {
case CHROMEOS: case CHROMEOS:
...@@ -143,7 +142,6 @@ int parse_img(const char *image, boot_img *boot) { ...@@ -143,7 +142,6 @@ int parse_img(const char *image, boot_img *boot) {
fprintf(stderr, "KERNEL_FMT [%s]\n", fmt); fprintf(stderr, "KERNEL_FMT [%s]\n", fmt);
get_type_name(boot->ramdisk_type, fmt); get_type_name(boot->ramdisk_type, fmt);
fprintf(stderr, "RAMDISK_FMT [%s]\n", fmt); fprintf(stderr, "RAMDISK_FMT [%s]\n", fmt);
fprintf(stderr, "\n");
return boot->flags & CHROMEOS_FLAG ? CHROMEOS_RET : return boot->flags & CHROMEOS_FLAG ? CHROMEOS_RET :
((is_blk && boot->tail_size < 500 * 1024) ? INSUF_BLOCK_RET : 0); ((is_blk && boot->tail_size < 500 * 1024) ? INSUF_BLOCK_RET : 0);
...@@ -206,7 +204,7 @@ void repack(const char* orig_image, const char* out_image) { ...@@ -206,7 +204,7 @@ void repack(const char* orig_image, const char* out_image) {
// Parse original image // Parse original image
parse_img(orig_image, &boot); parse_img(orig_image, &boot);
fprintf(stderr, "Repack to boot image: [%s]\n\n", out_image); fprintf(stderr, "Repack to boot image: [%s]\n", out_image);
// Create new image // Create new image
int fd = creat(out_image, 0644); int fd = creat(out_image, 0644);
......
...@@ -437,7 +437,7 @@ void decomp_file(char *from, const char *to) { ...@@ -437,7 +437,7 @@ void decomp_file(char *from, const char *to) {
fd = STDOUT_FILENO; fd = STDOUT_FILENO;
} else { } else {
fd = creat(to, 0644); fd = creat(to, 0644);
fprintf(stderr, "Decompressing to [%s]\n\n", to); fprintf(stderr, "Decompressing to [%s]\n", to);
} }
decomp(type, fd, file, size); decomp(type, fd, file, size);
...@@ -498,7 +498,7 @@ void comp_file(const char *method, const char *from, const char *to) { ...@@ -498,7 +498,7 @@ void comp_file(const char *method, const char *from, const char *to) {
fd = STDOUT_FILENO; fd = STDOUT_FILENO;
} else { } else {
fd = creat(dest, 0644); fd = creat(dest, 0644);
fprintf(stderr, "Compressing to [%s]\n\n", dest); fprintf(stderr, "Compressing to [%s]\n", dest);
} }
comp(type, fd, file, size); comp(type, fd, file, size);
close(fd); close(fd);
......
...@@ -26,7 +26,7 @@ void hexpatch(const char *image, const char *from, const char *to) { ...@@ -26,7 +26,7 @@ void hexpatch(const char *image, const char *from, const char *to) {
hex2byte(to, patch); hex2byte(to, patch);
for (size_t i = 0; filesize > 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, "Patch @ %08X [%s]->[%s]\n", (unsigned) i, from, to);
memset(file + i, 0, patternsize); memset(file + i, 0, patternsize);
memcpy(file + i, patch, patchsize); memcpy(file + i, patch, patchsize);
i += patternsize - 1; i += patternsize - 1;
......
...@@ -18,7 +18,7 @@ void unpack(const char *image); ...@@ -18,7 +18,7 @@ void unpack(const char *image);
void repack(const char* orig_image, const char* out_image); void repack(const char* orig_image, const char* out_image);
void hexpatch(const char *image, const char *from, const char *to); void hexpatch(const char *image, const char *from, const char *to);
int parse_img(const char *image, boot_img *boot); int parse_img(const char *image, boot_img *boot);
int cpio_commands(const char *cmd, int argc, char *argv[]); int cpio_commands(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);
int dtb_commands(const char *cmd, int argc, char *argv[]); int dtb_commands(const char *cmd, int argc, char *argv[]);
......
...@@ -36,34 +36,39 @@ static void usage(char *arg0) { ...@@ -36,34 +36,39 @@ static void usage(char *arg0) {
" --hexpatch <file> <hexpattern1> <hexpattern2>\n" " --hexpatch <file> <hexpattern1> <hexpattern2>\n"
" Search <hexpattern1> in <file>, and replace with <hexpattern2>\n" " Search <hexpattern1> in <file>, and replace with <hexpattern2>\n"
"\n" "\n"
" --cpio-<cmd> <incpio> [flags...] [args...]\n" " --cpio <incpio> [commands...]\n"
" Do cpio related cmds to <incpio> (modifications are done directly)\n" " Do cpio commands to <incpio> (modifications are done directly)\n"
" Each command is a single argument, use quotes if necessary\n"
" Supported commands:\n" " Supported commands:\n"
" -rm [-r] <entry>\n" " rm [-r] ENTRY\n"
" Remove entry from <incpio>, flag [-r] to remove recursively\n" " Remove ENTRY, specify [-r] to remove recursively\n"
" -mkdir <mode> <entry>\n" " mkdir MODE ENTRY\n"
" Create directory as an <entry>\n" " Create directory ENTRY in permissions MODE\n"
" -ln <target> <entry>\n" " ln TARGET ENTRY\n"
" Create symlink <entry> to point to <target>\n" " Create a symlink to TARGET with the name ENTRY\n"
" -mv <from-entry> <to-entry>\n" " mv SOURCE DEST\n"
" Move <from-entry> to <to-entry>\n" " Move SOURCE to DEST\n"
" -add <mode> <entry> <infile>\n" " add MODE ENTRY INFILE\n"
" Add <infile> as an <entry>; replaces <entry> if already exists\n" " Add INFILE as ENTRY in permissions MODE; replaces ENTRY if exists\n"
" -extract [<entry> <outfile>]\n" " extract [ENTRY OUT]\n"
" Extract <entry> to <outfile>, or extract all to current directory\n" " Extract ENTRY to OUT, or extract all entries to current directory\n"
" -test\n" " test\n"
" Test the current cpio's patch status\n"
" Return value: 0/stock 1/Magisk 2/other (phh, SuperSU, Xposed)\n" " Return value: 0/stock 1/Magisk 2/other (phh, SuperSU, Xposed)\n"
" -patch <KEEPVERITY> <KEEPFORCEENCRYPT>\n" " patch KEEPVERITY KEEPFORCEENCRYPT\n"
" Ramdisk patches. KEEP**** are boolean values\n" " Ramdisk patches. KEEP**** are boolean values\n"
" This command is no longer used in Magisk installations\n" " backup ORIG [SHA1]\n"
" -backup <origcpio> <HIGHCOMP> <KEEPVERITY> <KEEPFORCEENCRYPT> [SHA1]\n" " Create ramdisk backups from ORIG\n"
" Create ramdisk backups into <incpio> from <origcpio>\n" " SHA1 of stock boot image is optional\n"
" restore\n"
" Restore ramdisk from ramdisk backup stored within incpio\n"
" magisk ORIG HIGHCOMP KEEPVERITY KEEPFORCEENCRYPT [SHA1]\n"
" Do Magisk patches and backups all in one step\n"
" Create ramdisk backups from ORIG\n"
" HIGHCOMP, KEEP**** are boolean values\n" " HIGHCOMP, KEEP**** are boolean values\n"
" SHA1 of stock boot image is optional\n" " SHA1 of stock boot image is optional\n"
" -restore\n" " sha1\n"
" Restore ramdisk from ramdisk backup within <incpio>\n" " Print stock boot SHA1 if previously stored\n"
" -stocksha1\n"
" Get stock boot SHA1 recorded within <incpio>\n"
"\n" "\n"
" --dtb-<cmd> <dtb>\n" " --dtb-<cmd> <dtb>\n"
" Do dtb related cmds to <dtb> (modifications are done directly)\n" " Do dtb related cmds to <dtb> (modifications are done directly)\n"
...@@ -104,11 +109,11 @@ static void usage(char *arg0) { ...@@ -104,11 +109,11 @@ static void usage(char *arg0) {
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
fprintf(stderr, "MagiskBoot v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu) - Boot Image Modification Tool\n\n"); fprintf(stderr, "MagiskBoot v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu) - Boot Image Modification Tool\n");
umask(0); umask(0);
if (argc > 1 && strcmp(argv[1], "--cleanup") == 0) { if (argc > 1 && strcmp(argv[1], "--cleanup") == 0) {
fprintf(stderr, "Cleaning up...\n\n"); fprintf(stderr, "Cleaning up...\n");
char name[PATH_MAX]; char name[PATH_MAX];
unlink(KERNEL_FILE); unlink(KERNEL_FILE);
unlink(RAMDISK_FILE); unlink(RAMDISK_FILE);
...@@ -146,11 +151,8 @@ int main(int argc, char *argv[]) { ...@@ -146,11 +151,8 @@ int main(int argc, char *argv[]) {
comp_file(method, argv[2], argc > 3 ? argv[3] : NULL); comp_file(method, argv[2], argc > 3 ? argv[3] : NULL);
} else if (argc > 4 && strcmp(argv[1], "--hexpatch") == 0) { } else if (argc > 4 && strcmp(argv[1], "--hexpatch") == 0) {
hexpatch(argv[2], argv[3], argv[4]); hexpatch(argv[2], argv[3], argv[4]);
} else if (argc > 2 && strncmp(argv[1], "--cpio", 6) == 0) { } else if (argc > 2 && strcmp(argv[1], "--cpio") == 0) {
char *cmd = argv[1] + 6; if (cpio_commands(argc - 2, argv + 2)) usage(argv[0]);
if (*cmd == '\0') usage(argv[0]);
else ++cmd;
if (cpio_commands(cmd, argc - 2, argv + 2)) usage(argv[0]);
} else if (argc > 2 && strncmp(argv[1], "--dtb", 5) == 0) { } else if (argc > 2 && strncmp(argv[1], "--dtb", 5) == 0) {
char *cmd = argv[1] + 5; char *cmd = argv[1] + 5;
if (*cmd == '\0') usage(argv[0]); if (*cmd == '\0') usage(argv[0]);
......
This diff is collapsed.
...@@ -209,7 +209,7 @@ public: ...@@ -209,7 +209,7 @@ public:
vector prop_list; vector prop_list;
static int prop_cmp(const void *p1, const void *p2) { static int prop_cmp(const void *p1, const void *p2) {
return strcmp((*((property **) p1))->name, (*((property **) p2))->name); return strcmp(((property *) p1)->name, ((property *) p2)->name);
} }
static void print_all_props_cb(const char *name, const char *value) { static void print_all_props_cb(const char *name, const char *value) {
......
...@@ -22,34 +22,42 @@ static uint32_t x8u(char *hex) { ...@@ -22,34 +22,42 @@ static uint32_t x8u(char *hex) {
return val; return val;
} }
static void cpio_free(cpio_entry *f) { void cpio_free(cpio_entry *e) {
if (f) { if (e) {
free(f->filename); free(e->filename);
free(f->data); free(e->data);
free(f); free(e);
} }
} }
int cpio_find(struct vector *v, const char *entry) {
cpio_entry *e;
vec_for_each(v, e) {
if (!e) continue;
if (strcmp(e->filename, entry) == 0)
return _;
}
return -1;
}
int cpio_cmp(const void *a, const void *b) { int cpio_cmp(const void *a, const void *b) {
return strcmp((*(cpio_entry **) a)->filename, (*(cpio_entry **) b)->filename); return strcmp(((cpio_entry *) a)->filename, ((cpio_entry *) b)->filename);
} }
void cpio_vec_insert(struct vector *v, cpio_entry *n) { void cpio_vec_insert(struct vector *v, cpio_entry *n) {
cpio_entry *f; int i = cpio_find(v, n->filename);
vec_for_each(v, f) { if (i > 0) {
if (strcmp(f->filename, n->filename) == 0) { // Replace, then all is done
// Replace, then all is done cpio_free(vec_entry(v)[i]);
cpio_free(f); vec_entry(v)[i] = n;
vec_cur(v) = n; return;
return;
}
} }
vec_push_back(v, n); vec_push_back(v, n);
} }
// Parse cpio file to a vector of cpio_entry // Parse cpio file to a vector of cpio_entry
void parse_cpio(struct vector *v, const char *filename) { void parse_cpio(struct vector *v, const char *filename) {
fprintf(stderr, "Loading cpio: [%s]\n\n", filename); fprintf(stderr, "Loading cpio: [%s]\n", filename);
int fd = open(filename, O_RDONLY); int fd = open(filename, O_RDONLY);
if (fd < 0) return; if (fd < 0) return;
cpio_newc_header header; cpio_newc_header header;
...@@ -91,35 +99,34 @@ void parse_cpio(struct vector *v, const char *filename) { ...@@ -91,35 +99,34 @@ void parse_cpio(struct vector *v, const char *filename) {
} }
void dump_cpio(struct vector *v, const char *filename) { void dump_cpio(struct vector *v, const char *filename) {
fprintf(stderr, "\nDump cpio: [%s]\n\n", filename); fprintf(stderr, "Dump cpio: [%s]\n", filename);
int fd = creat(filename, 0644);
unsigned inode = 300000; unsigned inode = 300000;
char header[111]; char header[111];
// Sort by name // Sort by name
vec_sort(v, cpio_cmp); vec_sort(v, cpio_cmp);
cpio_entry *f; cpio_entry *e;
vec_for_each(v, f) { int fd = creat(filename, 0644);
if (f->remove) continue; vec_for_each(v, e) {
sprintf(header, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x", sprintf(header, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
inode++, // f->ino inode++, // e->ino
f->mode, e->mode,
f->uid, e->uid,
f->gid, e->gid,
1, // f->nlink 1, // e->nlink
0, // f->mtime 0, // e->mtime
f->filesize, e->filesize,
0, // f->devmajor 0, // e->devmajor
0, // f->devminor 0, // e->devminor
0, // f->rdevmajor 0, // e->rdevmajor
0, // f->rdevminor 0, // e->rdevminor
(uint32_t) strlen(f->filename) + 1, (uint32_t) strlen(e->filename) + 1,
0 // f->check 0 // e->check
); );
xwrite(fd, header, 110); xwrite(fd, header, 110);
xwrite(fd, f->filename, strlen(f->filename) + 1); xwrite(fd, e->filename, strlen(e->filename) + 1);
file_align(fd, 4, 1); file_align(fd, 4, 1);
if (f->filesize) { if (e->filesize) {
xwrite(fd, f->data, f->filesize); xwrite(fd, e->data, e->filesize);
file_align(fd, 4, 1); file_align(fd, 4, 1);
} }
} }
...@@ -133,23 +140,22 @@ void dump_cpio(struct vector *v, const char *filename) { ...@@ -133,23 +140,22 @@ void dump_cpio(struct vector *v, const char *filename) {
void cpio_vec_destroy(struct vector *v) { void cpio_vec_destroy(struct vector *v) {
// Free each cpio_entry // Free each cpio_entry
cpio_entry *f; cpio_entry *e;
vec_for_each(v, f) { vec_for_each(v, e)
cpio_free(f); cpio_free(e);
}
vec_destroy(v); vec_destroy(v);
} }
void cpio_rm(struct vector *v, int recursive, const char *entry) { void cpio_rm(struct vector *v, int recursive, const char *entry) {
cpio_entry *f; cpio_entry *e;
vec_for_each(v, f) { size_t len = strlen(entry);
if (strncmp(f->filename, entry, strlen(entry)) == 0) { vec_for_each(v, e) {
char next = f->filename[strlen(entry)]; if (!e) continue;
if ((recursive && next == '/') || next == '\0') { if (strncmp(e->filename, entry, len) == 0) {
if (!f->remove) { if ((recursive && e->filename[len] == '/') || e->filename[len] == '\0') {
fprintf(stderr, "Remove [%s]\n", f->filename); fprintf(stderr, "Remove [%s]\n", e->filename);
f->remove = 1; cpio_free(e);
} vec_cur(v) = NULL;
if (!recursive) return; if (!recursive) return;
} }
} }
...@@ -157,97 +163,94 @@ void cpio_rm(struct vector *v, int recursive, const char *entry) { ...@@ -157,97 +163,94 @@ void cpio_rm(struct vector *v, int recursive, const char *entry) {
} }
void cpio_mkdir(struct vector *v, mode_t mode, const char *entry) { void cpio_mkdir(struct vector *v, mode_t mode, const char *entry) {
cpio_entry *f = xcalloc(sizeof(*f), 1); cpio_entry *e = xcalloc(sizeof(*e), 1);
f->mode = S_IFDIR | mode; e->mode = S_IFDIR | mode;
f->filename = strdup(entry); e->filename = strdup(entry);
cpio_vec_insert(v, f); cpio_vec_insert(v, e);
fprintf(stderr, "Create directory [%s] (%04o)\n",entry, mode); fprintf(stderr, "Create directory [%s] (%04o)\n",entry, mode);
} }
void cpio_ln(struct vector *v, const char *target, const char *entry) { void cpio_ln(struct vector *v, const char *target, const char *entry) {
cpio_entry *f = xcalloc(sizeof(*f), 1); cpio_entry *e = xcalloc(sizeof(*e), 1);
f->mode = S_IFLNK; e->mode = S_IFLNK;
f->filename = strdup(entry); e->filename = strdup(entry);
f->filesize = strlen(target); e->filesize = strlen(target);
f->data = strdup(target); e->data = strdup(target);
cpio_vec_insert(v, f); cpio_vec_insert(v, e);
fprintf(stderr, "Create symlink [%s] -> [%s]\n", entry, target); fprintf(stderr, "Create symlink [%s] -> [%s]\n", entry, target);
} }
void cpio_add(struct vector *v, mode_t mode, const char *entry, const char *filename) { void cpio_add(struct vector *v, mode_t mode, const char *entry, const char *filename) {
int fd = xopen(filename, O_RDONLY); int fd = xopen(filename, O_RDONLY);
cpio_entry *f = xcalloc(sizeof(*f), 1); cpio_entry *e = xcalloc(sizeof(*e), 1);
f->mode = S_IFREG | mode; e->mode = S_IFREG | mode;
f->filename = strdup(entry); e->filename = strdup(entry);
f->filesize = lseek(fd, 0, SEEK_END); e->filesize = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET); lseek(fd, 0, SEEK_SET);
f->data = xmalloc(f->filesize); e->data = xmalloc(e->filesize);
xxread(fd, f->data, f->filesize); xxread(fd, e->data, e->filesize);
close(fd); close(fd);
cpio_vec_insert(v, f); cpio_vec_insert(v, e);
fprintf(stderr, "Add entry [%s] (%04o)\n", entry, mode); fprintf(stderr, "Add entry [%s] (%04o)\n", entry, mode);
} }
int cpio_mv(struct vector *v, const char *from, const char *to) { int cpio_mv(struct vector *v, const char *from, const char *to) {
struct cpio_entry *f, *t; struct cpio_entry *e;
vec_for_each(v, f) { int f = cpio_find(v, from), t = cpio_find(v, to);
if (strcmp(f->filename, from) == 0) { if (f > 0) {
fprintf(stderr, "Move [%s] -> [%s]\n", from, to); if (t > 0) {
vec_for_each(v, t) { cpio_free(vec_entry(v)[t]);
if (strcmp(t->filename, to) == 0) { vec_entry(v)[t] = NULL;
t->remove = 1;
break;
}
}
free(f->filename);
f->filename = strdup(to);
return 0;
} }
e = vec_entry(v)[f];
free(e->filename);
e->filename = strdup(to);
return 0;
} }
fprintf(stderr, "Cannot find entry %s\n", from); fprintf(stderr, "Cannot find entry %s\n", from);
return 1; return 1;
} }
int cpio_extract(struct vector *v, const char *entry, const char *filename) { int cpio_extract(struct vector *v, const char *entry, const char *filename) {
cpio_entry *f; int i = cpio_find(v, entry);
vec_for_each(v, f) { if (i > 0) {
if (strcmp(f->filename, entry) == 0) { cpio_entry *e = vec_entry(v)[i];
fprintf(stderr, "Extracting [%s] to [%s]\n\n", entry, filename); fprintf(stderr, "Extracting [%s] to [%s]\n", entry, filename);
if (S_ISREG(f->mode)) { if (S_ISREG(e->mode)) {
int fd = creat(filename, f->mode & 0777); int fd = creat(filename, e->mode & 0777);
xwrite(fd, f->data, f->filesize); xwrite(fd, e->data, e->filesize);
fchown(fd, f->uid, f->gid); fchown(fd, e->uid, e->gid);
close(fd); close(fd);
} else if (S_ISLNK(f->mode)) { } else if (S_ISLNK(e->mode)) {
char *target = xcalloc(f->filesize + 1, 1); char *target = xcalloc(e->filesize + 1, 1);
memcpy(target, f->data, f->filesize); memcpy(target, e->data, e->filesize);
unlink(filename); unlink(filename);
symlink(target, filename); symlink(target, filename);
}
return 0;
} }
return 0;
} }
fprintf(stderr, "Cannot find the file entry [%s]\n", entry); fprintf(stderr, "Cannot find the file entry [%s]\n", entry);
return 1; return 1;
} }
void cpio_extract_all(struct vector *v) { void cpio_extract_all(struct vector *v) {
cpio_entry *f; cpio_entry *e;
vec_for_each(v, f) { vec_for_each(v, e) {
fprintf(stderr, "Extracting [%s]\n", f->filename); if (!e) continue;
unlink(f->filename); fprintf(stderr, "Extracting [%s]\n", e->filename);
rmdir(f->filename); unlink(e->filename);
if (S_ISDIR(f->mode)) { rmdir(e->filename);
mkdir(f->filename, f->mode & 0777); if (S_ISDIR(e->mode)) {
} else if (S_ISREG(f->mode)) { mkdir(e->filename, e->mode & 0777);
int fd = creat(f->filename, f->mode & 0777); } else if (S_ISREG(e->mode)) {
xwrite(fd, f->data, f->filesize); int fd = creat(e->filename, e->mode & 0777);
fchown(fd, f->uid, f->gid); xwrite(fd, e->data, e->filesize);
fchown(fd, e->uid, e->gid);
close(fd); close(fd);
} else if (S_ISLNK(f->mode)) { } else if (S_ISLNK(e->mode)) {
char *target = xcalloc(f->filesize + 1, 1); char *target = xcalloc(e->filesize + 1, 1);
memcpy(target, f->data, f->filesize); memcpy(target, e->data, e->filesize);
symlink(target, f->filename); symlink(target, e->filename);
} }
} }
} }
...@@ -29,9 +29,25 @@ void *vec_pop_back(struct vector *v) { ...@@ -29,9 +29,25 @@ void *vec_pop_back(struct vector *v) {
return ret; return ret;
} }
static int (*cmp)(const void *, const void *);
static int vec_comp(const void *a, const void *b) {
void *aa = *((void **)a), *bb = *((void **)b);
if (aa == NULL && bb == NULL) return 0;
else if (aa == NULL) return 1;
else if (bb == NULL) return -1;
else return cmp ? cmp(aa, bb) : 0;
}
void vec_sort(struct vector *v, int (*compar)(const void *, const void *)) { void vec_sort(struct vector *v, int (*compar)(const void *, const void *)) {
if (v == NULL) return; if (v == NULL) return;
qsort(vec_entry(v), vec_size(v), sizeof(void*), compar); cmp = compar;
qsort(vec_entry(v), vec_size(v), sizeof(void*), vec_comp);
void *e;
vec_for_each_r(v, e) {
if (e) break;
--vec_size(v);
}
} }
/* Will cleanup only the vector itself /* Will cleanup only the vector itself
......
...@@ -89,6 +89,7 @@ case $? in ...@@ -89,6 +89,7 @@ case $? in
2 ) 2 )
ui_print "! Insufficient boot partition size detected" ui_print "! Insufficient boot partition size detected"
HIGHCOMP=true HIGHCOMP=true
ui_print "- Enable high compression mode"
;; ;;
3 ) 3 )
ui_print "- ChromeOS boot image detected" ui_print "- ChromeOS boot image detected"
...@@ -103,20 +104,13 @@ case $? in ...@@ -103,20 +104,13 @@ case $? in
abort "! Stock kernel cannot be patched, please use a custom kernel" abort "! Stock kernel cannot be patched, please use a custom kernel"
esac esac
if [ -f /sdcard/ramdisk-recovery.img ]; then
HIGHCOMP=true
ui_print "- Detected ramdisk-recovery.img"
fi
$HIGHCOMP && ui_print "- Enable high compression mode"
########################################################################################## ##########################################################################################
# Ramdisk restores # Ramdisk restores
########################################################################################## ##########################################################################################
# Test patch status and do restore, after this section, ramdisk.cpio.orig is guaranteed to exist # Test patch status and do restore, after this section, ramdisk.cpio.orig is guaranteed to exist
ui_print "- Checking ramdisk status" ui_print "- Checking ramdisk status"
./magiskboot --cpio-test ramdisk.cpio ./magiskboot --cpio ramdisk.cpio test
case $? in case $? in
0 ) # Stock boot 0 ) # Stock boot
ui_print "- Stock boot image detected!" ui_print "- Stock boot image detected!"
...@@ -129,8 +123,8 @@ case $? in ...@@ -129,8 +123,8 @@ case $? in
1 ) # Magisk patched 1 ) # Magisk patched
ui_print "- Magisk patched image detected!" ui_print "- Magisk patched image detected!"
# Find SHA1 of stock boot image # Find SHA1 of stock boot image
[ -z $SHA1 ] && SHA1=`./magiskboot --cpio-stocksha1 ramdisk.cpio 2>/dev/null` [ -z $SHA1 ] && SHA1=`./magiskboot --cpio ramdisk.cpio sha1 2>/dev/null`
./magiskboot --cpio-restore ramdisk.cpio ./magiskboot --cpio ramdisk.cpio restore
cp -af ramdisk.cpio ramdisk.cpio.orig cp -af ramdisk.cpio ramdisk.cpio.orig
;; ;;
2 ) # Other patched 2 ) # Other patched
...@@ -145,18 +139,12 @@ esac ...@@ -145,18 +139,12 @@ esac
ui_print "- Patching ramdisk" ui_print "- Patching ramdisk"
./magiskboot --cpio-add ramdisk.cpio 750 init magiskinit ./magiskboot --cpio ramdisk.cpio \
./magiskboot --cpio-backup ramdisk.cpio ramdisk.cpio.orig $HIGHCOMP $KEEPVERITY $KEEPFORCEENCRYPT $SHA1 'add 750 init magiskinit' \
"magisk ramdisk.cpio.orig $HIGHCOMP $KEEPVERITY $KEEPFORCEENCRYPT $SHA1"
rm -f ramdisk.cpio.orig rm -f ramdisk.cpio.orig
if [ -f /sdcard/ramdisk-recovery.img ]; then
ui_print "- Adding ramdisk-recovery.img"
./magiskboot --decompress - < /sdcard/ramdisk-recovery.img | ./magiskboot --compress=xz - ramdisk-recovery.xz
./magiskboot --cpio-add ramdisk.cpio 0 ramdisk-recovery.xz ramdisk-recovery.xz
rm ramdisk-recovery.xz
fi
########################################################################################## ##########################################################################################
# Binary patches # Binary patches
########################################################################################## ##########################################################################################
......
...@@ -71,7 +71,7 @@ esac ...@@ -71,7 +71,7 @@ esac
# Detect boot image state # Detect boot image state
ui_print "- Checking ramdisk status" ui_print "- Checking ramdisk status"
./magiskboot --cpio-test ramdisk.cpio ./magiskboot --cpio ramdisk.cpio test
case $? in case $? in
0 ) # Stock boot 0 ) # Stock boot
ui_print "- Stock boot image detected!" ui_print "- Stock boot image detected!"
...@@ -80,13 +80,13 @@ case $? in ...@@ -80,13 +80,13 @@ case $? in
1 ) # Magisk patched 1 ) # Magisk patched
ui_print "- Magisk patched image detected!" ui_print "- Magisk patched image detected!"
# Find SHA1 of stock boot image # Find SHA1 of stock boot image
[ -z $SHA1 ] && SHA1=`./magiskboot --cpio-stocksha1 ramdisk.cpio 2>/dev/null` [ -z $SHA1 ] && SHA1=`./magiskboot --cpio ramdisk.cpio sha1 2>/dev/null`
OK=false OK=false
[ ! -z $SHA1 ] && restore_imgs $SHA1 && OK=true [ ! -z $SHA1 ] && restore_imgs $SHA1 && OK=true
if ! $OK; then if ! $OK; then
ui_print "! Boot image backup unavailable" ui_print "! Boot image backup unavailable"
ui_print "- Restoring ramdisk with internal backup" ui_print "- Restoring ramdisk with internal backup"
./magiskboot --cpio-restore ramdisk.cpio ./magiskboot --cpio ramdisk.cpio restore
./magiskboot --repack $BOOTIMAGE ./magiskboot --repack $BOOTIMAGE
# Sign chromeos boot # Sign chromeos boot
$CHROMEOS && sign_chromeos $CHROMEOS && sign_chromeos
...@@ -104,6 +104,7 @@ cd / ...@@ -104,6 +104,7 @@ cd /
ui_print "- Removing Magisk files" ui_print "- Removing Magisk files"
rm -rf /cache/*magisk* /cache/unblock /data/*magisk* /data/cache/*magisk* /data/property/*magisk* \ rm -rf /cache/*magisk* /cache/unblock /data/*magisk* /data/cache/*magisk* /data/property/*magisk* \
/data/Magisk.apk /data/busybox /data/custom_ramdisk_patch.sh /data/app/com.topjohnwu.magisk* \ /data/Magisk.apk /data/busybox /data/custom_ramdisk_patch.sh /data/app/com.topjohnwu.magisk* \
/data/user*/*/magisk.db /data/user*/*/com.topjohnwu.magisk /data/adb/*magisk* 2>/dev/null /data/user*/*/magisk.db /data/user*/*/com.topjohnwu.magisk /data/user*/*/.tmp.magisk.config \
/data/adb/*magisk* 2>/dev/null
$BOOTMODE && reboot $BOOTMODE && reboot
...@@ -7,11 +7,11 @@ ...@@ -7,11 +7,11 @@
# #
########################################################################################## ##########################################################################################
MAGISK_VERSION_STUB #MAGISK_VERSION_STUB
SCRIPT_VERSION=$MAGISK_VER_CODE SCRIPT_VERSION=$MAGISK_VER_CODE
# Default location, will override if needed # Default location, will override if needed
MAGISKBIN=/data/adb/magisk [ -d /data/adb/magisk ] && MAGISKBIN=/data/adb/magisk || MAGISKBIN=/data/magisk
BOOTSIGNER="/system/bin/dalvikvm -Xnodex2oat -Xnoimage-dex2oat -cp \$APK com.topjohnwu.magisk.utils.BootSigner" BOOTSIGNER="/system/bin/dalvikvm -Xnodex2oat -Xnoimage-dex2oat -cp \$APK com.topjohnwu.magisk.utils.BootSigner"
BOOTSIGNED=false BOOTSIGNED=false
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment