Commit 02dc1172 authored by topjohnwu's avatar topjohnwu

Revert DTB patches to in-place binary patches

Since we no longer need to add new properties in the device tree, and
all the patches we do removes strings, we can just directly patch
the flat device tree in-place, ignoring basically all the higher level
DTB structure and format to accomplish 100% compatibility.
parent dbf8c412
This diff is collapsed.
......@@ -19,6 +19,6 @@ int hexpatch(const char *image, const char *from, const char *to);
int cpio_commands(int argc, char *argv[]);
int dtb_commands(int argc, char *argv[]);
char *patch_verity(const void *buf, uint32_t &size, bool inplace = false);
void patch_encryption(void *buf, uint32_t &size);
uint32_t patch_verity(void *buf, uint32_t size);
uint32_t patch_encryption(void *buf, uint32_t size);
bool check_env(const char *name);
......@@ -42,7 +42,7 @@ Supported actions:
Search <hexpattern1> in <file>, and replace with <hexpattern2>
cpio <incpio> [commands...]
Do cpio commands to <incpio> (modifications are done directly)
Do cpio commands to <incpio> (modifications are done in-place)
Each command is a single argument, add quotes for each command
Supported commands:
exists ENTRY
......@@ -64,8 +64,8 @@ Supported actions:
Return values:
0:stock 1:Magisk 2:unsupported (phh, SuperSU, Xposed)
patch
Apply ramdisk patches. Configure settings with env variables:
KEEPVERITY KEEPFORCEENCRYPT
Apply ramdisk patches
Configure with env variables: KEEPVERITY KEEPFORCEENCRYPT
backup ORIG
Create ramdisk backups from ORIG
restore
......@@ -79,10 +79,10 @@ Supported actions:
print [-f]
Print all contents of dtb for debugging
Specify [-f] to only print fstab nodes
patch [OUT]
patch
Search for fstab and remove verity/avb
If [OUT] is not specified, it will directly output to <input>
Configure with env variables: KEEPVERITY TWOSTAGEINIT
Modifications are done directly to the file in-place
Configure with env variables: KEEPVERITY
split <input>
Split image.*-dtb into kernel + kernel_dtb
......@@ -186,7 +186,7 @@ int main(int argc, char *argv[]) {
} else if (argc > 2 && action == "cpio"sv) {
if (cpio_commands(argc - 2, argv + 2))
usage(argv[0]);
} else if (argc > 2 && action == "dtb") {
} else if (argc > 3 && action == "dtb") {
if (dtb_commands(argc - 2, argv + 2))
usage(argv[0]);
} else {
......
......@@ -37,42 +37,27 @@ static int check_encryption_pattern(const char *s) {
else return -1;
}
char *patch_verity(const void *buf, uint32_t &size, bool inplace) {
auto src = static_cast<const char *>(buf);
auto dest = (char *)(inplace ? buf : xmalloc(size));
int src_size = size;
bool found = false;
static uint32_t remove_pattern(void *buf, uint32_t size, int(*pattern_skip)(const char *)) {
auto src = static_cast<char *>(buf);
int orig_sz = size;
int write = 0;
for (int read = 0; read < src_size;) {
if (int skip; (skip = check_verity_pattern(src + read)) > 0) {
fprintf(stderr, "Found pattern [%.*s]\n", skip, src + read);
for (int read = 0; read < orig_sz;) {
if (int skip = pattern_skip(src + read); skip > 0) {
fprintf(stderr, "Remove pattern [%.*s]\n", skip, src + read);
size -= skip;
read += skip;
found = true;
} else {
dest[write++] = src[read++];
src[write++] = src[read++];
}
}
dest[write] = '\0';
if (!found) {
if (!inplace)
free(dest);
return nullptr;
}
return dest;
memset(src + write, 0, orig_sz - write);
return size;
}
void patch_encryption(void *buf, uint32_t &size) {
auto src = static_cast<char *>(buf);
int src_size = size;
int write = 0;
for (int read = 0; read < src_size; ++read, ++write) {
if (int skip; (skip = check_encryption_pattern(src + read)) > 0) {
fprintf(stderr, "Found pattern [%.*s]\n", skip, src + read);
size -= skip;
read += skip;
}
src[write] = src[read];
}
src[write] = '\0';
uint32_t patch_verity(void *buf, uint32_t size) {
return remove_pattern(buf, size, check_verity_pattern);
}
uint32_t patch_encryption(void *buf, uint32_t size) {
return remove_pattern(buf, size, check_encryption_pattern);
}
......@@ -55,7 +55,7 @@ void magisk_cpio::patch() {
if (!keepverity) {
if (fstab) {
fprintf(stderr, "Found fstab file [%s]\n", cur->first.data());
patch_verity(cur->second->data, cur->second->filesize, true);
cur->second->filesize = patch_verity(cur->second->data, cur->second->filesize);
} else if (cur->first == "verity_key") {
rm(cur);
continue;
......@@ -63,7 +63,7 @@ void magisk_cpio::patch() {
}
if (!keepforceencrypt) {
if (fstab) {
patch_encryption(cur->second->data, cur->second->filesize);
cur->second->filesize = patch_encryption(cur->second->data, cur->second->filesize);
}
}
}
......
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