Several improvements

This commit is contained in:
topjohnwu 2017-02-24 14:58:44 +08:00
parent ee2a30470a
commit 6f609f0dd7
6 changed files with 18 additions and 19 deletions

View File

@ -3,6 +3,6 @@ LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := bootimgtools
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := main.c extract.c repack.c hexpatch.c
LOCAL_SRC_FILES := main.c unpack.c repack.c hexpatch.c
LOCAL_CFLAGS += -std=gnu11
include $(BUILD_EXECUTABLE)

View File

@ -93,7 +93,7 @@ struct boot_img_hdr
** else: jump to kernel_addr
*/
int extract(const char *image);
int unpack(const char *image);
int repack(const char *image);
int hexpatch(char *image, char *from, char *to);

View File

@ -9,7 +9,7 @@
#include "bootimg.h"
int hex2int(char c) {
static int hex2int(char c) {
int first = c / 16 - 3;
int second = c % 16;
int result = first * 10 + second;
@ -17,13 +17,13 @@ int hex2int(char c) {
return result;
}
int hex2ascii(char c, char d) {
static int hex2ascii(char c, char d) {
int high = hex2int(c) * 16;
int low = hex2int(d);
return high+low;
}
void hexstr2str(char *hex, char *str) {
static void hexstr2str(char *hex, char *str) {
char buf = 0;
for(int i = 0, length = strlen(hex); i < length; ++i){
if(i % 2){

View File

@ -7,22 +7,21 @@
Patch Boot Image
*********************/
int usage(char *arg0) {
static int usage(char *arg0) {
fprintf(stderr, "Boot Image Unpack/Repack Tool\n");
fprintf(stderr, "%s --extract <bootimage>\n", arg0);
fprintf(stderr, "%s --unpack <bootimage>\n", arg0);
fprintf(stderr, " Unpack <bootimage> into current directory\n\n");
fprintf(stderr, "%s --repack <bootimage>\n", arg0);
fprintf(stderr, " Repack kernel, dt, ramdisk... from current directory to new-image.img\n <bootimage> is the image you've just unpacked\n\n");
fprintf(stderr, " Repack kernel, dtb, ramdisk... from current directory to new-image.img\n <bootimage> is the image you've just unpacked\n\n");
fprintf(stderr, "%s --hexpatch <bootimage> <hexpattern1> <hexpattern2>\n", arg0);
fprintf(stderr, " Search <hexpattern1> in <bootimage>, and replace with <hexpattern2>\n\n");
return 1;
}
int main(int argc, char *argv[])
{
int main(int argc, char *argv[]) {
char ch;
struct option long_options[] = {
{"extract", required_argument, NULL, 'e'},
{"unpack", required_argument, NULL, 'u'},
{"repack", required_argument, NULL, 'r'},
{"hexpatch", required_argument, NULL, 'p'},
{NULL, 0, NULL, 0}
@ -30,7 +29,7 @@ int main(int argc, char *argv[])
while ((ch = getopt_long(argc, argv, "e:r:p:", long_options, NULL)) != -1) {
switch (ch) {
case 'e':
return extract(optarg);
return unpack(optarg);
case 'r':
return repack(optarg);
case 'p':

View File

@ -12,8 +12,8 @@
#include "bootimg.h"
// Global pointer of current positions
void *ibase, *ipos;
int ofd, opos;
static void *ibase, *ipos;
static int ofd, opos;
static size_t dump(const char *filename) {
int fd = open(filename, O_RDONLY);
@ -60,7 +60,7 @@ static int aosp() {
printf("AOSP Boot Image Detected\n");
char *name;
struct boot_img_hdr hdr, ihdr;
boot_img_hdr hdr, ihdr;
// Read the original header
memcpy(&ihdr, ibase, sizeof(ihdr));

View File

@ -12,7 +12,7 @@
#include "bootimg.h"
// Global pointer of current positions
unsigned char *base, *pos;
static unsigned char *base, *pos;
static void dump(size_t size, const char *filename) {
unlink(filename);
@ -37,7 +37,7 @@ static int aosp() {
char name[PATH_MAX], *ext;
// Read the header
struct boot_img_hdr hdr;
boot_img_hdr hdr;
memcpy(&hdr, base, sizeof(hdr));
pos = base + hdr.page_size;
@ -108,7 +108,7 @@ static int aosp() {
return 0;
}
int extract(const char* image) {
int unpack(const char* image) {
int fd = open(image, O_RDONLY), ret = 0;
size_t size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);