2017-12-07 01:30:48 +08:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "utils.h"
|
2018-08-10 00:40:23 +08:00
|
|
|
#include "magiskboot.h"
|
2017-12-07 01:30:48 +08:00
|
|
|
|
|
|
|
static int check_verity_pattern(const char *s) {
|
2017-12-29 04:25:03 +08:00
|
|
|
int skip = 0;
|
|
|
|
if (s[0] == ',') ++skip;
|
|
|
|
if (strncmp(s + skip, "verify", 6) == 0)
|
|
|
|
skip += 6;
|
|
|
|
else if (strncmp(s + skip, "avb", 3) == 0)
|
|
|
|
skip += 3;
|
2017-12-07 01:30:48 +08:00
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
|
2017-12-29 04:25:03 +08:00
|
|
|
if (s[skip] == '=') {
|
|
|
|
while (s[skip] != '\0' && s[skip] != ' ' && s[skip] != '\n' && s[skip] != ',') ++skip;
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
2017-12-29 04:25:03 +08:00
|
|
|
return skip;
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int check_encryption_pattern(const char *s) {
|
|
|
|
const char *encrypt_list[] = { "forceencrypt", "forcefdeorfbe", NULL };
|
|
|
|
for (int i = 0 ; encrypt_list[i]; ++i) {
|
|
|
|
int len = strlen(encrypt_list[i]);
|
|
|
|
if (strncmp(s, encrypt_list[i], len) == 0)
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-12-07 03:21:13 +08:00
|
|
|
int patch_verity(void **buf, uint32_t *size, int patch) {
|
2018-06-10 17:57:40 +08:00
|
|
|
int skip, src_size = *size, found = 0;
|
2017-12-29 04:25:03 +08:00
|
|
|
char *src = *buf, *patched = patch ? xcalloc(src_size, 1) : NULL;
|
|
|
|
for (int read = 0, write = 0; read < src_size; ++read, ++write) {
|
|
|
|
if ((skip = check_verity_pattern(src + read)) > 0) {
|
2018-06-10 17:57:40 +08:00
|
|
|
if (patch) {
|
|
|
|
fprintf(stderr, "Remove pattern [%.*s]\n", skip, src + read);
|
|
|
|
*size -= skip;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Found pattern [%.*s]\n", skip, src + read);
|
|
|
|
}
|
2017-12-29 04:25:03 +08:00
|
|
|
read += skip;
|
2018-06-10 17:57:40 +08:00
|
|
|
found = 1;
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
2017-12-29 04:25:03 +08:00
|
|
|
if (patch)
|
|
|
|
patched[write] = src[read];
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
2017-12-31 19:30:56 +08:00
|
|
|
if (patch) {
|
|
|
|
free(*buf);
|
|
|
|
*buf = patched;
|
|
|
|
}
|
2018-06-10 17:57:40 +08:00
|
|
|
return found;
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
|
2017-12-07 03:21:13 +08:00
|
|
|
void patch_encryption(void **buf, uint32_t *size) {
|
2017-12-29 04:25:03 +08:00
|
|
|
int skip, src_size = *size;
|
|
|
|
char *src = *buf, *patched = xcalloc(src_size, 1);
|
|
|
|
for (int read = 0, write = 0; read < src_size; ++read, ++write) {
|
|
|
|
if ((skip = check_encryption_pattern(src + read)) > 0) {
|
|
|
|
fprintf(stderr, "Replace pattern [%.*s] with [encryptable]\n", skip, src + read);
|
|
|
|
memcpy(patched + read, "encryptable", 11);
|
|
|
|
read += skip;
|
|
|
|
write += 11;
|
2017-12-07 01:30:48 +08:00
|
|
|
*size -= (skip - 11);
|
|
|
|
}
|
2017-12-29 04:25:03 +08:00
|
|
|
patched[write] = src[read];
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
2017-12-29 04:25:03 +08:00
|
|
|
free(*buf);
|
|
|
|
*buf = patched;
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|