Small refactor of magiskboot

This commit is contained in:
topjohnwu 2018-01-29 03:12:35 +08:00
parent eed86c760f
commit dfee9954e0
7 changed files with 37 additions and 56 deletions

View File

@ -102,7 +102,7 @@ LOCAL_SRC_FILES := \
magiskboot/bootimg.c \ magiskboot/bootimg.c \
magiskboot/hexpatch.c \ magiskboot/hexpatch.c \
magiskboot/compress.c \ magiskboot/compress.c \
magiskboot/types.c \ magiskboot/format.c \
magiskboot/dtb.c \ magiskboot/dtb.c \
magiskboot/ramdisk.c \ magiskboot/ramdisk.c \
$(UTIL_SRC) $(UTIL_SRC)

View File

@ -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) { for (void *head = boot->map_addr; head < boot->map_addr + boot->map_size; head += 256) {
size_t pos = 0; size_t pos = 0;
switch (check_type(head)) { switch (check_fmt(head)) {
case CHROMEOS: case CHROMEOS:
// The caller should know it's chromeos, as it needs additional signing // The caller should know it's chromeos, as it needs additional signing
boot->flags |= CHROMEOS_FLAG; 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->k_fmt = check_fmt(boot->kernel);
boot->ramdisk_type = check_type(boot->ramdisk); boot->r_fmt = check_fmt(boot->ramdisk);
// Check MTK // Check MTK
if (boot->kernel_type == MTK) { if (boot->k_fmt == MTK) {
fprintf(stderr, "MTK_KERNEL_HDR\n"); fprintf(stderr, "MTK_KERNEL_HDR\n");
boot->flags |= MTK_KERNEL; boot->flags |= MTK_KERNEL;
boot->k_hdr = malloc(sizeof(mtk_hdr)); 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); fprintf(stderr, "NAME [%s]\n", boot->k_hdr->name);
boot->kernel += 512; boot->kernel += 512;
lheader(boot, kernel_size, -= 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"); fprintf(stderr, "MTK_RAMDISK_HDR\n");
boot->flags |= MTK_RAMDISK; boot->flags |= MTK_RAMDISK;
boot->r_hdr = malloc(sizeof(mtk_hdr)); 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); fprintf(stderr, "NAME [%s]\n", boot->r_hdr->name);
boot->ramdisk += 512; boot->ramdisk += 512;
lheader(boot, ramdisk_size, -= 512); lheader(boot, ramdisk_size, -= 512);
boot->ramdisk_type = check_type(boot->ramdisk); boot->r_fmt = check_fmt(boot->ramdisk);
} }
char fmt[16]; char fmt[16];
get_fmt_name(boot->k_fmt, fmt);
get_type_name(boot->kernel_type, fmt);
fprintf(stderr, "KERNEL_FMT [%s]\n", 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); fprintf(stderr, "RAMDISK_FMT [%s]\n", fmt);
return boot->flags & CHROMEOS_FLAG ? CHROMEOS_RET : return boot->flags & CHROMEOS_FLAG ? CHROMEOS_RET :
@ -196,9 +195,9 @@ int unpack(const char *image) {
int fd; int fd;
// Dump kernel // Dump kernel
if (COMPRESSED(boot.kernel_type)) { if (COMPRESSED(boot.k_fmt)) {
fd = creat(KERNEL_FILE, 0644); 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); close(fd);
} else { } else {
dump(boot.kernel, header(&boot, kernel_size), KERNEL_FILE); dump(boot.kernel, header(&boot, kernel_size), KERNEL_FILE);
@ -210,9 +209,9 @@ int unpack(const char *image) {
} }
// Dump ramdisk // Dump ramdisk
if (COMPRESSED(boot.ramdisk_type)) { if (COMPRESSED(boot.r_fmt)) {
fd = creat(RAMDISK_FILE, 0644); 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); close(fd);
} else { } else {
dump(boot.ramdisk, header(&boot, ramdisk_size), RAMDISK_FILE ".raw"); 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); mtk_kernel_off = lseek(fd, 0, SEEK_CUR);
write_zero(fd, 512); write_zero(fd, 512);
} }
if (COMPRESSED(boot.kernel_type)) { if (COMPRESSED(boot.k_fmt)) {
size_t raw_size; size_t raw_size;
void *kernel_raw; void *kernel_raw;
mmap_ro(KERNEL_FILE, &kernel_raw, &raw_size); 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); munmap(kernel_raw, raw_size);
} else { } else {
lheader(&boot, kernel_size, = restore(KERNEL_FILE, fd)); 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; size_t cpio_size;
void *cpio; void *cpio;
mmap_ro(RAMDISK_FILE, &cpio, &cpio_size); 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); munmap(cpio, cpio_size);
} else { } else {
// Find compressed ramdisk // Find compressed ramdisk

View File

@ -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 <stdint.h> #include <stdint.h>
#include "types.h" #include "format.h"
#ifndef _BOOT_IMAGE_H_ #ifndef _BOOT_IMAGE_H_
#define _BOOT_IMAGE_H_ #define _BOOT_IMAGE_H_
#define BOOT_MAGIC "ANDROID!"
typedef struct boot_img_hdr { typedef struct boot_img_hdr {
char magic[8]; char magic[8];
@ -141,8 +122,8 @@ typedef struct boot_img {
uint8_t flags; uint8_t flags;
// The format of kernel and ramdisk // The format of kernel and ramdisk
file_t kernel_type; format_t k_fmt;
file_t ramdisk_type; format_t r_fmt;
// Pointer to dtb that is appended after kernel // Pointer to dtb that is appended after kernel
void *dtb; void *dtb;

View File

@ -343,7 +343,7 @@ done:
return total; 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) { switch (type) {
case GZIP: case GZIP:
return gzip(0, to, from, size); 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) { switch (type) {
case GZIP: case GZIP:
return gzip(1, to, from, size); 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); stream_full_read(STDIN_FILENO, &file, &size);
else else
mmap_ro(from, &file, &size); mmap_ro(from, &file, &size);
file_t type = check_type(file); format_t type = check_fmt(file);
char *ext; char *ext;
ext = strrchr(from, '.'); ext = strrchr(from, '.');
if (to == NULL) 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) { void comp_file(const char *method, const char *from, const char *to) {
file_t type; format_t type;
char *ext, dest[PATH_MAX]; char *ext, dest[PATH_MAX];
if (strcmp(method, "gzip") == 0) { if (strcmp(method, "gzip") == 0) {
type = GZIP; type = GZIP;

View File

@ -1,9 +1,9 @@
#include <string.h> #include <string.h>
#include "bootimg.h" #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) { if (memcmp(buf, CHROMEOS_MAGIC, 8) == 0) {
return CHROMEOS; return CHROMEOS;
} else if (memcmp(buf, BOOT_MAGIC, 8) == 0) { } 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; char *s;
switch (type) { switch (fmt) {
case CHROMEOS: case CHROMEOS:
s = "chromeos"; s = "chromeos";
break; break;

View File

@ -1,5 +1,5 @@
#ifndef _TYPES_H_ #ifndef _FORMAT_H_
#define _TYPES_H_ #define _FORMAT_H_
typedef enum { typedef enum {
UNKNOWN, UNKNOWN,
@ -16,10 +16,11 @@ typedef enum {
LZ4_LEGACY, LZ4_LEGACY,
MTK, MTK,
DTB 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 CHROMEOS_MAGIC "CHROMEOS"
#define ELF32_MAGIC "\x7f""ELF\x01" #define ELF32_MAGIC "\x7f""ELF\x01"
#define ELF64_MAGIC "\x7f""ELF\x02" #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_LIST ((char *[]) { "gzip", "xz", "lzma", "bzip2", "lz4", "lz4_legacy", NULL })
#define SUP_EXT_LIST ((char *[]) { "gz", "xz", "lzma", "bz2", "lz4", "lz4", NULL }) #define SUP_EXT_LIST ((char *[]) { "gz", "xz", "lzma", "bz2", "lz4", "lz4", NULL })
file_t check_type(const void *buf); format_t check_fmt(const void *buf);
void get_type_name(file_t type, char *name); void get_fmt_name(format_t fmt, char *name);
#endif #endif

View File

@ -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 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 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); 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 comp(format_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 decomp(format_t type, int to, const void *from, size_t size);
#endif #endif