Magisk/native/jni/magiskboot/pattern.cpp
topjohnwu 027ec70262 Patch AVB structures
Disable vbmeta verification in flags
2021-03-02 20:37:37 -08:00

64 lines
1.7 KiB
C++

#include <malloc.h>
#include <string.h>
#include <utils.hpp>
#include "magiskboot.hpp"
#define CHECKED_MATCH(p) else if (strncmp(s + skip, p, sizeof(p) - 1) == 0) skip += (sizeof(p) - 1)
static int check_verity_pattern(const char *s) {
int skip = s[0] == ',';
if (0) {}
CHECKED_MATCH("verifyatboot");
CHECKED_MATCH("verify");
CHECKED_MATCH("avb_keys");
CHECKED_MATCH("avb");
CHECKED_MATCH("support_scfs");
CHECKED_MATCH("fsverity");
else return -1;
if (s[skip] == '=') {
while (s[skip] != '\0' && s[skip] != ' ' && s[skip] != '\n' && s[skip] != ',')
++skip;
}
return skip;
}
#undef CHECKED_MATCH
#define CHECKED_MATCH(p) else if (strncmp(s, p, sizeof(p) - 1) == 0) return (sizeof(p) - 1)
static int check_encryption_pattern(const char *s) {
if (0) {}
CHECKED_MATCH("forceencrypt");
CHECKED_MATCH("forcefdeorfbe");
CHECKED_MATCH("fileencryption");
else return -1;
}
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 < 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;
}
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);
}