2020-03-09 01:50:30 -07:00
|
|
|
#include <utils.hpp>
|
2019-02-10 03:57:51 -05:00
|
|
|
|
2020-03-09 01:50:30 -07:00
|
|
|
#include "magiskboot.hpp"
|
2017-12-07 01:30:48 +08:00
|
|
|
|
2021-03-09 04:08:16 -08:00
|
|
|
#define MATCH(p) else if (strncmp(s + skip, p, sizeof(p) - 1) == 0) skip += (sizeof(p) - 1)
|
2019-12-03 05:39:39 -05:00
|
|
|
|
2021-03-09 04:08:16 -08:00
|
|
|
static int skip_verity_pattern(const char *s) {
|
2020-12-30 22:11:24 -08:00
|
|
|
int skip = s[0] == ',';
|
|
|
|
|
|
|
|
if (0) {}
|
2021-03-09 04:08:16 -08:00
|
|
|
MATCH("verifyatboot");
|
|
|
|
MATCH("verify");
|
|
|
|
MATCH("avb_keys");
|
|
|
|
MATCH("avb");
|
|
|
|
MATCH("support_scfs");
|
|
|
|
MATCH("fsverity");
|
2020-12-30 22:11:24 -08:00
|
|
|
else return -1;
|
|
|
|
|
|
|
|
if (s[skip] == '=') {
|
2021-03-09 04:08:16 -08:00
|
|
|
while (!strchr(" \n,", s[skip]))
|
2020-12-30 22:11:24 -08:00
|
|
|
++skip;
|
|
|
|
}
|
|
|
|
return skip;
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
|
2021-03-09 04:08:16 -08:00
|
|
|
static int skip_encryption_pattern(const char *s) {
|
|
|
|
int skip = s[0] == ',';
|
2019-12-03 05:39:39 -05:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
if (0) {}
|
2021-03-09 04:08:16 -08:00
|
|
|
MATCH("forceencrypt");
|
|
|
|
MATCH("forcefdeorfbe");
|
|
|
|
MATCH("fileencryption");
|
2020-12-30 22:11:24 -08:00
|
|
|
else return -1;
|
2021-03-09 04:08:16 -08:00
|
|
|
|
|
|
|
if (s[skip] == '=') {
|
|
|
|
while (!strchr(" \n,", s[skip]))
|
|
|
|
++skip;
|
|
|
|
}
|
|
|
|
return skip;
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
|
2021-03-09 04:08:16 -08:00
|
|
|
static uint32_t remove_pattern(char *src, uint32_t size, int(*pattern_skip)(const char *)) {
|
2020-12-30 22:11:24 -08:00
|
|
|
int orig_sz = size;
|
|
|
|
int write = 0;
|
|
|
|
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;
|
|
|
|
} else {
|
|
|
|
src[write++] = src[read++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memset(src + write, 0, orig_sz - write);
|
|
|
|
return size;
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
|
2020-05-05 01:03:09 -07:00
|
|
|
uint32_t patch_verity(void *buf, uint32_t size) {
|
2021-03-09 04:08:16 -08:00
|
|
|
return remove_pattern(static_cast<char *>(buf), size, skip_verity_pattern);
|
2020-05-05 01:03:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t patch_encryption(void *buf, uint32_t size) {
|
2021-03-09 04:08:16 -08:00
|
|
|
return remove_pattern(static_cast<char *>(buf), size, skip_encryption_pattern);
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|