2017-09-14 17:11:56 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2019-02-10 09:57:51 +01:00
|
|
|
#include <utils.h>
|
|
|
|
|
2017-02-27 22:37:47 +01:00
|
|
|
#include "magiskboot.h"
|
2016-09-08 14:59:48 +02:00
|
|
|
|
2018-10-25 03:08:06 +02:00
|
|
|
static void hex2byte(uint8_t *hex, uint8_t *str) {
|
2017-03-07 17:54:23 +01:00
|
|
|
char high, low;
|
2018-10-25 03:08:06 +02:00
|
|
|
for (int i = 0, length = strlen((char *) hex); i < length; i += 2) {
|
2017-03-07 17:54:23 +01:00
|
|
|
high = toupper(hex[i]) - '0';
|
|
|
|
low = toupper(hex[i + 1]) - '0';
|
|
|
|
str[i / 2] = ((high > 9 ? high - 7 : high) << 4) + (low > 9 ? low - 7 : low);
|
2016-09-08 14:59:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 14:29:30 +02:00
|
|
|
int hexpatch(const char *image, const char *from, const char *to) {
|
2017-03-04 14:16:59 +01:00
|
|
|
int patternsize = strlen(from) / 2, patchsize = strlen(to) / 2;
|
2019-06-08 14:29:30 +02:00
|
|
|
int patched = 1;
|
2017-03-04 14:16:59 +01:00
|
|
|
size_t filesize;
|
2018-10-25 03:08:06 +02:00
|
|
|
uint8_t *file, *pattern, *patch;
|
2019-02-25 05:09:34 +01:00
|
|
|
mmap_rw(image, file, filesize);
|
2018-10-25 03:08:06 +02:00
|
|
|
pattern = (uint8_t *) xmalloc(patternsize);
|
|
|
|
patch = (uint8_t *) xmalloc(patchsize);
|
|
|
|
hex2byte((uint8_t *) from, pattern);
|
|
|
|
hex2byte((uint8_t *) to, patch);
|
2017-10-07 16:08:10 +02:00
|
|
|
for (size_t i = 0; filesize > 0 && i < filesize - patternsize; ++i) {
|
2017-02-24 20:50:26 +01:00
|
|
|
if (memcmp(file + i, pattern, patternsize) == 0) {
|
2017-12-20 20:36:18 +01:00
|
|
|
fprintf(stderr, "Patch @ %08X [%s]->[%s]\n", (unsigned) i, from, to);
|
2017-02-24 20:50:26 +01:00
|
|
|
memset(file + i, 0, patternsize);
|
|
|
|
memcpy(file + i, patch, patchsize);
|
|
|
|
i += patternsize - 1;
|
2019-06-08 14:29:30 +02:00
|
|
|
patched = 0;
|
2016-09-08 14:59:48 +02:00
|
|
|
}
|
|
|
|
}
|
2017-02-24 20:50:26 +01:00
|
|
|
munmap(file, filesize);
|
2016-09-08 14:59:48 +02:00
|
|
|
free(pattern);
|
|
|
|
free(patch);
|
2019-06-08 14:29:30 +02:00
|
|
|
|
|
|
|
return patched;
|
2017-02-27 22:37:47 +01:00
|
|
|
}
|