From dfee9954e027639a8123b389e532122bfba145ad Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 29 Jan 2018 03:12:35 +0800 Subject: [PATCH] Small refactor of magiskboot --- native/jni/Android.mk | 2 +- native/jni/magiskboot/bootimg.c | 33 ++++++++++----------- native/jni/magiskboot/bootimg.h | 25 ++-------------- native/jni/magiskboot/compress.c | 8 ++--- native/jni/magiskboot/{types.c => format.c} | 8 ++--- native/jni/magiskboot/{types.h => format.h} | 13 ++++---- native/jni/magiskboot/magiskboot.h | 4 +-- 7 files changed, 37 insertions(+), 56 deletions(-) rename native/jni/magiskboot/{types.c => format.c} (92%) rename native/jni/magiskboot/{types.h => format.h} (80%) diff --git a/native/jni/Android.mk b/native/jni/Android.mk index 4f7dfa396..69598b64d 100644 --- a/native/jni/Android.mk +++ b/native/jni/Android.mk @@ -102,7 +102,7 @@ LOCAL_SRC_FILES := \ magiskboot/bootimg.c \ magiskboot/hexpatch.c \ magiskboot/compress.c \ - magiskboot/types.c \ + magiskboot/format.c \ magiskboot/dtb.c \ magiskboot/ramdisk.c \ $(UTIL_SRC) diff --git a/native/jni/magiskboot/bootimg.c b/native/jni/magiskboot/bootimg.c index eb970e0e1..b63ebe8da 100644 --- a/native/jni/magiskboot/bootimg.c +++ b/native/jni/magiskboot/bootimg.c @@ -87,7 +87,7 @@ int parse_img(const char *image, boot_img *boot) { for (void *head = boot->map_addr; head < boot->map_addr + boot->map_size; head += 256) { size_t pos = 0; - switch (check_type(head)) { + switch (check_fmt(head)) { case CHROMEOS: // The caller should know it's chromeos, as it needs additional signing boot->flags |= CHROMEOS_FLAG; @@ -147,11 +147,11 @@ int parse_img(const char *image, boot_img *boot) { } } - boot->kernel_type = check_type(boot->kernel); - boot->ramdisk_type = check_type(boot->ramdisk); + boot->k_fmt = check_fmt(boot->kernel); + boot->r_fmt = check_fmt(boot->ramdisk); // Check MTK - if (boot->kernel_type == MTK) { + if (boot->k_fmt == MTK) { fprintf(stderr, "MTK_KERNEL_HDR\n"); boot->flags |= MTK_KERNEL; boot->k_hdr = malloc(sizeof(mtk_hdr)); @@ -160,9 +160,9 @@ 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->kernel_type = check_type(boot->kernel); + boot->k_fmt = check_fmt(boot->kernel); } - if (boot->ramdisk_type == MTK) { + if (boot->r_fmt == MTK) { fprintf(stderr, "MTK_RAMDISK_HDR\n"); boot->flags |= MTK_RAMDISK; boot->r_hdr = malloc(sizeof(mtk_hdr)); @@ -171,14 +171,13 @@ 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->ramdisk_type = check_type(boot->ramdisk); + boot->r_fmt = check_fmt(boot->ramdisk); } char fmt[16]; - - get_type_name(boot->kernel_type, fmt); + get_fmt_name(boot->k_fmt, fmt); fprintf(stderr, "KERNEL_FMT [%s]\n", fmt); - get_type_name(boot->ramdisk_type, fmt); + get_fmt_name(boot->r_fmt, fmt); fprintf(stderr, "RAMDISK_FMT [%s]\n", fmt); return boot->flags & CHROMEOS_FLAG ? CHROMEOS_RET : @@ -196,9 +195,9 @@ int unpack(const char *image) { int fd; // Dump kernel - if (COMPRESSED(boot.kernel_type)) { + if (COMPRESSED(boot.k_fmt)) { fd = creat(KERNEL_FILE, 0644); - decomp(boot.kernel_type, fd, boot.kernel, header(&boot, kernel_size)); + decomp(boot.k_fmt, fd, boot.kernel, header(&boot, kernel_size)); close(fd); } else { dump(boot.kernel, header(&boot, kernel_size), KERNEL_FILE); @@ -210,9 +209,9 @@ int unpack(const char *image) { } // Dump ramdisk - if (COMPRESSED(boot.ramdisk_type)) { + if (COMPRESSED(boot.r_fmt)) { fd = creat(RAMDISK_FILE, 0644); - decomp(boot.ramdisk_type, fd, boot.ramdisk, header(&boot, ramdisk_size)); + decomp(boot.r_fmt, fd, boot.ramdisk, header(&boot, ramdisk_size)); close(fd); } else { dump(boot.ramdisk, header(&boot, ramdisk_size), RAMDISK_FILE ".raw"); @@ -255,11 +254,11 @@ void repack(const char* orig_image, const char* out_image) { mtk_kernel_off = lseek(fd, 0, SEEK_CUR); write_zero(fd, 512); } - if (COMPRESSED(boot.kernel_type)) { + 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.kernel_type, fd, 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)); @@ -280,7 +279,7 @@ void repack(const char* orig_image, const char* out_image) { size_t cpio_size; void *cpio; mmap_ro(RAMDISK_FILE, &cpio, &cpio_size); - lheader(&boot, ramdisk_size, = comp(boot.ramdisk_type, fd, cpio, cpio_size)); + lheader(&boot, ramdisk_size, = comp(boot.r_fmt, fd, cpio, cpio_size)); munmap(cpio, cpio_size); } else { // Find compressed ramdisk diff --git a/native/jni/magiskboot/bootimg.h b/native/jni/magiskboot/bootimg.h index 98c91d4ec..9d279204b 100644 --- a/native/jni/magiskboot/bootimg.h +++ b/native/jni/magiskboot/bootimg.h @@ -1,28 +1,9 @@ -/* tools/mkbootimg/bootimg.h -** -** Copyright 2007, The Android Open Source Project -** -** Licensed under the Apache License, Version 2.0 (the "License"); -** you may not use this file except in compliance with the License. -** You may obtain a copy of the License at -** -** http://www.apache.org/licenses/LICENSE-2.0 -** -** Unless required by applicable law or agreed to in writing, software -** distributed under the License is distributed on an "AS IS" BASIS, -** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -** See the License for the specific language governing permissions and -** limitations under the License. -*/ - #include -#include "types.h" +#include "format.h" #ifndef _BOOT_IMAGE_H_ #define _BOOT_IMAGE_H_ -#define BOOT_MAGIC "ANDROID!" - typedef struct boot_img_hdr { char magic[8]; @@ -141,8 +122,8 @@ typedef struct boot_img { uint8_t flags; // The format of kernel and ramdisk - file_t kernel_type; - file_t ramdisk_type; + format_t k_fmt; + format_t r_fmt; // Pointer to dtb that is appended after kernel void *dtb; diff --git a/native/jni/magiskboot/compress.c b/native/jni/magiskboot/compress.c index e82f27250..f4b7ea6b8 100644 --- a/native/jni/magiskboot/compress.c +++ b/native/jni/magiskboot/compress.c @@ -343,7 +343,7 @@ done: return total; } -long long decomp(file_t type, int to, const void *from, size_t size) { +long long decomp(format_t type, int to, const void *from, size_t size) { switch (type) { case GZIP: return gzip(0, to, from, size); @@ -363,7 +363,7 @@ long long decomp(file_t type, int to, const void *from, size_t size) { } } -long long comp(file_t type, int to, const void *from, size_t size) { +long long comp(format_t type, int to, const void *from, size_t size) { switch (type) { case GZIP: return gzip(1, to, from, size); @@ -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); - file_t type = check_type(file); + format_t type = check_fmt(file); char *ext; ext = strrchr(from, '.'); if (to == NULL) @@ -453,7 +453,7 @@ void decomp_file(char *from, const char *to) { } void comp_file(const char *method, const char *from, const char *to) { - file_t type; + format_t type; char *ext, dest[PATH_MAX]; if (strcmp(method, "gzip") == 0) { type = GZIP; diff --git a/native/jni/magiskboot/types.c b/native/jni/magiskboot/format.c similarity index 92% rename from native/jni/magiskboot/types.c rename to native/jni/magiskboot/format.c index 87e33865e..9c32c3531 100644 --- a/native/jni/magiskboot/types.c +++ b/native/jni/magiskboot/format.c @@ -1,9 +1,9 @@ #include #include "bootimg.h" -#include "types.h" +#include "format.h" -file_t check_type(const void *buf) { +format_t check_fmt(const void *buf) { if (memcmp(buf, CHROMEOS_MAGIC, 8) == 0) { return CHROMEOS; } else if (memcmp(buf, BOOT_MAGIC, 8) == 0) { @@ -36,9 +36,9 @@ file_t check_type(const void *buf) { } } -void get_type_name(file_t type, char *name) { +void get_fmt_name(format_t fmt, char *name) { char *s; - switch (type) { + switch (fmt) { case CHROMEOS: s = "chromeos"; break; diff --git a/native/jni/magiskboot/types.h b/native/jni/magiskboot/format.h similarity index 80% rename from native/jni/magiskboot/types.h rename to native/jni/magiskboot/format.h index c65183c97..ca30c34ee 100644 --- a/native/jni/magiskboot/types.h +++ b/native/jni/magiskboot/format.h @@ -1,5 +1,5 @@ -#ifndef _TYPES_H_ -#define _TYPES_H_ +#ifndef _FORMAT_H_ +#define _FORMAT_H_ typedef enum { UNKNOWN, @@ -16,10 +16,11 @@ typedef enum { LZ4_LEGACY, MTK, DTB -} file_t; +} format_t; -#define COMPRESSED(type) (type >= GZIP && type <= LZ4_LEGACY) +#define COMPRESSED(fmt) (fmt >= GZIP && fmt <= LZ4_LEGACY) +#define BOOT_MAGIC "ANDROID!" #define CHROMEOS_MAGIC "CHROMEOS" #define ELF32_MAGIC "\x7f""ELF\x01" #define ELF64_MAGIC "\x7f""ELF\x02" @@ -36,7 +37,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 }) -file_t check_type(const void *buf); -void get_type_name(file_t type, char *name); +format_t check_fmt(const void *buf); +void get_fmt_name(format_t fmt, char *name); #endif diff --git a/native/jni/magiskboot/magiskboot.h b/native/jni/magiskboot/magiskboot.h index 2839993d4..8b3bb3284 100644 --- a/native/jni/magiskboot/magiskboot.h +++ b/native/jni/magiskboot/magiskboot.h @@ -29,7 +29,7 @@ size_t lzma(int mode, int fd, const void *buf, size_t size); size_t lz4(int mode, int fd, const void *buf, size_t size); size_t bzip2(int mode, int fd, const void *buf, size_t size); size_t lz4_legacy(int mode, int fd, const void *buf, size_t size); -long long comp(file_t type, int to, const void *from, size_t size); -long long decomp(file_t type, int to, const void *from, size_t size); +long long comp(format_t type, int to, const void *from, size_t size); +long long decomp(format_t type, int to, const void *from, size_t size); #endif