mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-12-24 20:37:39 +00:00
Use external functions in init
Don't reinvent the wheel
This commit is contained in:
parent
e5e34797a8
commit
8c8a63ebfb
@ -94,7 +94,11 @@ include $(BUILD_EXECUTABLE)
|
|||||||
ifeq ($(TARGET_ARCH_ABI), arm64-v8a)
|
ifeq ($(TARGET_ARCH_ABI), arm64-v8a)
|
||||||
include $(CLEAR_VARS)
|
include $(CLEAR_VARS)
|
||||||
LOCAL_MODULE := magiskinit
|
LOCAL_MODULE := magiskinit
|
||||||
LOCAL_SRC_FILES := magiskinit.c
|
LOCAL_C_INCLUDES := jni/include
|
||||||
|
LOCAL_SRC_FILES := \
|
||||||
|
magiskinit.c \
|
||||||
|
magiskboot/boot_utils.c \
|
||||||
|
utils/xwrap.c
|
||||||
LOCAL_LDFLAGS := -static
|
LOCAL_LDFLAGS := -static
|
||||||
include $(BUILD_EXECUTABLE)
|
include $(BUILD_EXECUTABLE)
|
||||||
endif
|
endif
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
@ -34,21 +35,9 @@ struct device {
|
|||||||
char path[64];
|
char path[64];
|
||||||
};
|
};
|
||||||
|
|
||||||
static void mmap_ro(const char *filename, void **buf, size_t *size) {
|
extern void mmap_ro(const char *filename, void **buf, size_t *size);
|
||||||
int fd = open(filename, O_RDONLY);
|
extern void mmap_rw(const char *filename, void **buf, size_t *size);
|
||||||
*size = lseek(fd, 0, SEEK_END);
|
extern void *patch_init_rc(char *data, uint32_t *size);
|
||||||
lseek(fd, 0, SEEK_SET);
|
|
||||||
*buf = mmap(NULL, *size, PROT_READ, MAP_SHARED, fd, 0);
|
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mmap_rw(const char *filename, void **buf, size_t *size) {
|
|
||||||
int fd = open(filename, O_RDWR);
|
|
||||||
*size = lseek(fd, 0, SEEK_END);
|
|
||||||
lseek(fd, 0, SEEK_SET);
|
|
||||||
*buf = mmap(NULL, *size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void clone_dir(int src, int dest) {
|
static void clone_dir(int src, int dest) {
|
||||||
struct dirent *entry;
|
struct dirent *entry;
|
||||||
@ -245,19 +234,14 @@ static void patch_ramdisk() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
munmap(addr, size);
|
munmap(addr, size);
|
||||||
mmap_ro("/init.rc", &addr, &size);
|
mmap_rw("/init.rc", &addr, &size);
|
||||||
int fd = open("/init.rc.patch", O_WRONLY | O_CREAT | O_CLOEXEC, 0750);
|
uint32_t new_size = size;
|
||||||
for (int i = 0; i < size; ++i) {
|
void *init_rc = patch_init_rc(addr, &new_size);
|
||||||
if (memcmp(addr + i, "import", 6) == 0) {
|
|
||||||
write(fd, addr, i);
|
|
||||||
write(fd, "import /init.magisk.rc\n", 23);
|
|
||||||
write(fd, addr + i, size -i);
|
|
||||||
close(fd);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
munmap(addr, size);
|
munmap(addr, size);
|
||||||
rename("/init.rc.patch", "/init.rc");
|
int fd = open("/init.rc", O_WRONLY | O_TRUNC | O_CLOEXEC);
|
||||||
|
write(fd, init_rc, new_size);
|
||||||
|
close(fd);
|
||||||
|
free(init_rc);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user