1
mirror of https://github.com/topjohnwu/Magisk synced 2024-11-13 20:54:12 +01:00

Support separate ramdisk images

This commit is contained in:
topjohnwu 2018-02-10 03:34:13 +08:00
parent 10ed299c78
commit 6728445542
6 changed files with 102 additions and 114 deletions

View File

@ -24,6 +24,8 @@
#define header(b, e) (lheader(b, e,))
static void dump(void *buf, size_t size, const char *filename) {
if (size == 0)
return;
int fd = creat(filename, 0644);
xwrite(fd, buf, size);
close(fd);
@ -95,7 +97,7 @@ int parse_img(const char *image, boot_img *boot) {
for (void *head = boot->map_addr; head < boot->map_addr + boot->map_size; ++head) {
size_t pos = 0;
switch (check_fmt(head)) {
switch (check_fmt(head, boot->map_size)) {
case CHROMEOS:
// The caller should know it's chromeos, as it needs additional signing
boot->flags |= CHROMEOS_FLAG;
@ -150,17 +152,13 @@ int parse_img(const char *image, boot_img *boot) {
pos += header(boot, ramdisk_size);
pos_align();
if (header(boot, second_size)) {
boot->second = head + pos;
pos += header(boot, second_size);
pos_align();
}
boot->second = head + pos;
pos += header(boot, second_size);
pos_align();
if (header(boot, extra_size)) {
boot->extra = head + pos;
pos += header(boot, extra_size);
pos_align();
}
boot->extra = head + pos;
pos += header(boot, extra_size);
pos_align();
if (pos < boot->map_size) {
boot->tail = head + pos;
@ -185,8 +183,8 @@ int parse_img(const char *image, boot_img *boot) {
}
}
boot->k_fmt = check_fmt(boot->kernel);
boot->r_fmt = check_fmt(boot->ramdisk);
boot->k_fmt = check_fmt(boot->kernel, header(boot, kernel_size));
boot->r_fmt = check_fmt(boot->ramdisk, header(boot, ramdisk_size));
// Check MTK
if (boot->k_fmt == MTK) {
@ -198,7 +196,7 @@ int parse_img(const char *image, boot_img *boot) {
fprintf(stderr, "NAME [%s]\n", boot->k_hdr->name);
boot->kernel += 512;
lheader(boot, kernel_size, -= 512);
boot->k_fmt = check_fmt(boot->kernel);
boot->k_fmt = check_fmt(boot->kernel, header(boot, kernel_size));
}
if (boot->r_fmt == MTK) {
fprintf(stderr, "MTK_RAMDISK_HDR\n");
@ -209,7 +207,7 @@ int parse_img(const char *image, boot_img *boot) {
fprintf(stderr, "NAME [%s]\n", boot->r_hdr->name);
boot->ramdisk += 512;
lheader(boot, ramdisk_size, -= 512);
boot->r_fmt = check_fmt(boot->ramdisk);
boot->k_fmt = check_fmt(boot->ramdisk, header(boot, ramdisk_size));
}
char fmt[16];
@ -241,10 +239,8 @@ int unpack(const char *image) {
dump(boot.kernel, header(&boot, kernel_size), KERNEL_FILE);
}
if (boot.dt_size) {
// Dump dtb
dump(boot.dtb, boot.dt_size, DTB_FILE);
}
// Dump dtb
dump(boot.dtb, boot.dt_size, DTB_FILE);
// Dump ramdisk
if (COMPRESSED(boot.r_fmt)) {
@ -252,19 +248,14 @@ int unpack(const char *image) {
decomp(boot.r_fmt, fd, boot.ramdisk, header(&boot, ramdisk_size));
close(fd);
} else {
dump(boot.ramdisk, header(&boot, ramdisk_size), RAMDISK_FILE ".raw");
LOGE("Unknown ramdisk format! Dumped to %s\n", RAMDISK_FILE ".raw");
dump(boot.ramdisk, header(&boot, ramdisk_size), RAMDISK_FILE);
}
if (header(&boot, second_size)) {
// Dump second
dump(boot.second, header(&boot, second_size), SECOND_FILE);
}
// Dump second
dump(boot.second, header(&boot, second_size), SECOND_FILE);
if (header(&boot, extra_size)) {
// Dump extra
dump(boot.extra, header(&boot, extra_size), EXTRA_FILE);
}
// Dump extra
dump(boot.extra, header(&boot, extra_size), EXTRA_FILE);
clean_boot(&boot);
return ret;
@ -279,6 +270,13 @@ void repack(const char* orig_image, const char* out_image) {
// Parse original image
parse_img(orig_image, &boot);
// Reset all sizes
lheader(&boot, kernel_size, = 0);
lheader(&boot, ramdisk_size, = 0);
lheader(&boot, second_size, = 0);
lheader(&boot, extra_size, = 0);
boot.dt_size = 0;
fprintf(stderr, "Repack to boot image: [%s]\n", out_image);
// Create new image
@ -304,20 +302,22 @@ void repack(const char* orig_image, const char* out_image) {
// Skip MTK header
write_zero(fd, 512);
}
if (COMPRESSED(boot.k_fmt)) {
size_t raw_size;
void *kernel_raw;
mmap_ro(KERNEL_FILE, &kernel_raw, &raw_size);
lheader(&boot, kernel_size, = comp(boot.k_fmt, fd, kernel_raw, raw_size));
munmap(kernel_raw, raw_size);
} else {
lheader(&boot, kernel_size, = restore(KERNEL_FILE, fd));
if (access(KERNEL_FILE, R_OK) == 0) {
if (COMPRESSED(boot.k_fmt)) {
size_t raw_size;
void *kernel_raw;
mmap_ro(KERNEL_FILE, &kernel_raw, &raw_size);
lheader(&boot, kernel_size, = comp(boot.k_fmt, fd, kernel_raw, raw_size));
munmap(kernel_raw, raw_size);
} else {
lheader(&boot, kernel_size, = restore(KERNEL_FILE, fd));
}
// dtb
if (access(DTB_FILE, R_OK) == 0) {
lheader(&boot, kernel_size, += restore(DTB_FILE, fd));
}
file_align();
}
// dtb
if (boot.dt_size && access(DTB_FILE, R_OK) == 0) {
lheader(&boot, kernel_size, += restore(DTB_FILE, fd));
}
file_align();
// ramdisk
ramdisk_off = lseek(fd, 0, SEEK_CUR);
@ -326,39 +326,28 @@ void repack(const char* orig_image, const char* out_image) {
write_zero(fd, 512);
}
if (access(RAMDISK_FILE, R_OK) == 0) {
// If we found raw cpio, compress to original format
size_t cpio_size;
void *cpio;
mmap_ro(RAMDISK_FILE, &cpio, &cpio_size);
lheader(&boot, ramdisk_size, = comp(boot.r_fmt, fd, cpio, cpio_size));
munmap(cpio, cpio_size);
} else {
// Find compressed ramdisk
char name[PATH_MAX];
int found = 0;
for (int i = 0; SUP_EXT_LIST[i]; ++i) {
sprintf(name, "%s.%s", RAMDISK_FILE, SUP_EXT_LIST[i]);
if (access(name, R_OK) == 0) {
found = 1;
break;
}
if (COMPRESSED(boot.r_fmt)) {
size_t cpio_size;
void *cpio;
mmap_ro(RAMDISK_FILE, &cpio, &cpio_size);
lheader(&boot, ramdisk_size, = comp(boot.r_fmt, fd, cpio, cpio_size));
munmap(cpio, cpio_size);
} else {
lheader(&boot, kernel_size, = restore(KERNEL_FILE, fd));
}
if (!found)
LOGE("No ramdisk exists!\n");
lheader(&boot, ramdisk_size, = restore(name, fd));
file_align();
}
file_align();
// second
second_off = lseek(fd, 0, SEEK_CUR);
if (header(&boot, second_size) && access(SECOND_FILE, R_OK) == 0) {
if (access(SECOND_FILE, R_OK) == 0) {
lheader(&boot, second_size, = restore(SECOND_FILE, fd));
file_align();
}
// extra
extra_off = lseek(fd, 0, SEEK_CUR);
if (header(&boot, extra_size) && access(EXTRA_FILE, R_OK) == 0) {
if (access(EXTRA_FILE, R_OK) == 0) {
lheader(&boot, extra_size, = restore(EXTRA_FILE, fd));
file_align();
}

View File

@ -395,7 +395,7 @@ void decomp_file(char *from, const char *to) {
stream_full_read(STDIN_FILENO, &file, &size);
else
mmap_ro(from, &file, &size);
format_t type = check_fmt(file);
format_t type = check_fmt(file, size);
char *ext;
ext = strrchr(from, '.');
if (to == NULL)

View File

@ -3,37 +3,39 @@
#include "bootimg.h"
#include "format.h"
format_t check_fmt(const void *buf) {
if (memcmp(buf, CHROMEOS_MAGIC, 8) == 0) {
#define MATCH(s) (len >= (sizeof(s) - 1) && memcmp(buf, s, sizeof(s) - 1) == 0)
format_t check_fmt(const void *buf, size_t len) {
if (MATCH(CHROMEOS_MAGIC)) {
return CHROMEOS;
} else if (memcmp(buf, BOOT_MAGIC, 8) == 0) {
} else if (MATCH(BOOT_MAGIC)) {
return AOSP;
} else if (memcmp(buf, ELF32_MAGIC, 5) == 0) {
} else if (MATCH(ELF32_MAGIC)) {
return ELF32;
} else if (memcmp(buf, ELF64_MAGIC, 5) == 0) {
} else if (MATCH(ELF64_MAGIC)) {
return ELF64;
} else if (memcmp(buf, GZIP_MAGIC, 4) == 0) {
} else if (MATCH(GZIP_MAGIC)) {
return GZIP;
} else if (memcmp(buf, LZOP_MAGIC, 9) == 0) {
} else if (MATCH(LZOP_MAGIC)) {
return LZOP;
} else if (memcmp(buf, XZ_MAGIC, 6) == 0) {
} else if (MATCH(XZ_MAGIC)) {
return XZ;
} else if (memcmp(buf, "\x5d\x00\x00", 3) == 0
} else if (len >= 13 && memcmp(buf, "\x5d\x00\x00", 3) == 0
&& (((char *)buf)[12] == '\xff' || ((char *)buf)[12] == '\x00')) {
return LZMA;
} else if (memcmp(buf, BZIP_MAGIC, 3) == 0) {
} else if (MATCH(BZIP_MAGIC)) {
return BZIP2;
} else if (memcmp(buf, LZ4_MAGIC, 4) == 0) {
} else if (MATCH(LZ4_MAGIC)) {
return LZ4;
} else if (memcmp(buf, LZ4_LEG_MAGIC, 4) == 0) {
} else if (MATCH(LZ4_LEG_MAGIC)) {
return LZ4_LEGACY;
} else if (memcmp(buf, MTK_MAGIC, 4) == 0) {
} else if (MATCH(MTK_MAGIC)) {
return MTK;
} else if (memcmp(buf, DTB_MAGIC, 4) == 0) {
} else if (MATCH(DTB_MAGIC)) {
return DTB;
} else if (memcmp(buf, DHTB_MAGIC, 8) == 0) {
} else if (MATCH(DHTB_MAGIC)) {
return DHTB;
} else if (memcmp(buf, TEGRABLOB_MAGIC, 20) == 0) {
} else if (MATCH(TEGRABLOB_MAGIC)) {
return BLOB;
} else {
return UNKNOWN;

View File

@ -44,7 +44,7 @@ typedef enum {
#define SUP_LIST ((char *[]) { "gzip", "xz", "lzma", "bzip2", "lz4", "lz4_legacy", NULL })
#define SUP_EXT_LIST ((char *[]) { "gz", "xz", "lzma", "bz2", "lz4", "lz4", NULL })
format_t check_fmt(const void *buf);
format_t check_fmt(const void *buf, size_t len);
void get_fmt_name(format_t fmt, char *name);
#endif

View File

@ -53,8 +53,6 @@ if [ -z $SOURCEDMODE ]; then
cd "`dirname_wrap "${BASH_SOURCE:-$0}"`"
# Load utility functions
. ./util_functions.sh
# Detect current status
mount_partitions
fi
BOOTIMAGE="$1"
@ -68,7 +66,7 @@ BOOTIMAGE="$1"
if [ -z $KEEPFORCEENCRYPT ]; then
if [ "`getprop ro.crypto.state`" = "encrypted" ]; then
KEEPFORCEENCRYPT=true
ui_print "- Encrypted data detected"
ui_print "- Encrypted data detected, keep forceencrypt"
else
KEEPFORCEENCRYPT=false
fi
@ -159,15 +157,17 @@ if ! $KEEPVERITY && [ -f dtb ]; then
./magiskboot --dtb-patch dtb && ui_print "- Patching fstab in dtb to remove dm-verity"
fi
# Remove Samsung RKP in stock kernel
./magiskboot --hexpatch kernel \
49010054011440B93FA00F71E9000054010840B93FA00F7189000054001840B91FA00F7188010054 \
A1020054011440B93FA00F7140020054010840B93FA00F71E0010054001840B91FA00F7181010054
if [ -f kernel ]; then
# Remove Samsung RKP in stock kernel
./magiskboot --hexpatch kernel \
49010054011440B93FA00F71E9000054010840B93FA00F7189000054001840B91FA00F7188010054 \
A1020054011440B93FA00F7140020054010840B93FA00F71E0010054001840B91FA00F7181010054
# skip_initramfs -> want_initramfs
./magiskboot --hexpatch kernel \
736B69705F696E697472616D6673 \
77616E745F696E697472616D6673
# skip_initramfs -> want_initramfs
./magiskboot --hexpatch kernel \
736B69705F696E697472616D6673 \
77616E745F696E697472616D6673
fi
##########################################################################################
# Repack and flash

View File

@ -94,21 +94,12 @@ grep_prop() {
getvar() {
local VARNAME=$1
local VALUE=$(eval echo \$$VARNAME)
[ ! -z $VALUE ] && return
local VALUE=
for DIR in /.backup /dev /data /cache /system; do
VALUE=`grep_prop $VARNAME $DIR/.magisk`
[ ! -z $VALUE ] && break;
done
eval $VARNAME=\$VALUE
}
resolve_link() {
RESOLVED="$1"
while RESOLVE=`readlink $RESOLVED`; do
RESOLVED=$RESOLVE
done
echo $RESOLVED
[ ! -z $VALUE ] && eval $VARNAME=\$VALUE
}
find_boot_image() {
@ -116,22 +107,22 @@ find_boot_image() {
if [ ! -z $SLOT ]; then
BOOTIMAGE=`find /dev/block -iname boot$SLOT | head -n 1` 2>/dev/null
fi
if [ -z "$BOOTIMAGE" ]; then
if [ -z $BOOTIMAGE ]; then
# The slot info is incorrect...
SLOT=
for BLOCK in boot_a kern-a android_boot kernel boot lnx bootimg; do
for BLOCK in ramdisk boot_a kern-a android_boot kernel boot lnx bootimg; do
BOOTIMAGE=`find /dev/block -iname $BLOCK | head -n 1` 2>/dev/null
[ ! -z $BOOTIMAGE ] && break
done
fi
# Recovery fallback
if [ -z "$BOOTIMAGE" ]; then
if [ -z $BOOTIMAGE ]; then
for FSTAB in /etc/*fstab*; do
BOOTIMAGE=`grep -v '#' $FSTAB | grep -E '/boot[^a-zA-Z]' | grep -oE '/dev/[a-zA-Z0-9_./-]*'`
[ ! -z $BOOTIMAGE ] && break
done
fi
[ ! -z "$BOOTIMAGE" ] && BOOTIMAGE=`resolve_link $BOOTIMAGE`
[ ! -z $BOOTIMAGE ] && BOOTIMAGE=`readlink -f $BOOTIMAGE`
}
run_migrations() {
@ -158,6 +149,7 @@ run_migrations() {
fi
# Remove old dbs
rm -f /data/user*/*/magisk.db
[ -L /data/magisk.img ] || mv /data/magisk.img /data/adb/magisk.img 2>/dev/null
}
flash_boot_image() {
@ -167,7 +159,12 @@ flash_boot_image() {
*.gz) COMMAND="gzip -d < '$1'";;
*) COMMAND="cat '$1'";;
esac
$BOOTSIGNED && SIGNCOM="$BOOTSIGNER -sign" || SIGNCOM="cat -"
if $BOOTSIGNED; then
SIGNCOM="$BOOTSIGNER -sign"
ui_print "- Sign boot image with test keys"
else
SIGNCOM="cat -"
fi
case "$2" in
/dev/block/*)
ui_print "- Flashing new boot image"
@ -182,7 +179,7 @@ flash_boot_image() {
find_dtbo_image() {
DTBOIMAGE=`find /dev/block -iname dtbo$SLOT | head -n 1` 2>/dev/null
[ ! -z $DTBOIMAGE ] && DTBOIMAGE=`resolve_link $DTBOIMAGE`
[ ! -z $DTBOIMAGE ] && DTBOIMAGE=`readlink -f $DTBOIMAGE`
}
patch_dtbo_image() {
@ -232,7 +229,7 @@ sign_chromeos() {
}
is_mounted() {
TARGET="`resolve_link $1`"
TARGET="`readlink -f $1`"
cat /proc/mounts | grep " $TARGET " >/dev/null
return $?
}
@ -321,9 +318,9 @@ abort() {
}
set_perm() {
chown $2:$3 $1 || exit 1
chmod $4 $1 || exit 1
[ -z $5 ] && chcon 'u:object_r:system_file:s0' $1 || chcon $5 $1
chown $2:$3 $1 || return 1
chmod $4 $1 || return 1
[ -z $5 ] && chcon 'u:object_r:system_file:s0' $1 || chcon $5 $1 || return 1
}
set_perm_recursive() {