From ea6178065c6df158db18793c72669393a37198e2 Mon Sep 17 00:00:00 2001 From: Dominik Riebeling Date: Wed, 4 Nov 2009 20:58:40 +0000 Subject: Clean up mkamsboot building. No functional changes. - split out standalone functions to a separate file. - adjust and clean up Makefile. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@23520 a1c6a512-1295-4272-9138-f99709370657 --- rbutil/mkamsboot/Makefile | 45 ++++++------ rbutil/mkamsboot/main.c | 168 +++++++++++++++++++++++++++++++++++++++++++ rbutil/mkamsboot/mkamsboot.c | 132 ++-------------------------------- rbutil/mkamsboot/mkamsboot.h | 5 ++ 4 files changed, 199 insertions(+), 151 deletions(-) create mode 100644 rbutil/mkamsboot/main.c (limited to 'rbutil') diff --git a/rbutil/mkamsboot/Makefile b/rbutil/mkamsboot/Makefile index 98a027ffb5..6cf74ee2b5 100644 --- a/rbutil/mkamsboot/Makefile +++ b/rbutil/mkamsboot/Makefile @@ -1,12 +1,13 @@ -# We use the UCL code available in the Rockbox tools/ directory -CFLAGS=-I../../tools/ucl/include -Wall -CC = gcc #change for releases ifndef APPVERSION APPVERSION=`../../tools/version.sh` endif +# We use the UCL code available in the Rockbox tools/ directory +CFLAGS=-I../../tools/ucl/include -Wall -DVERSION=\"$(APPVERSION)\" +CC = gcc + ifndef V SILENT = @ endif @@ -34,38 +35,36 @@ OUT = $(TARGET_DIR)build$(RBARCH) all: $(OUTPUT) -# Dependant modules +# additional link dependencies for the standalone executable LIBUCL=../../tools/ucl/src/libucl$(RBARCH).a $(LIBUCL): make -C ../../tools/ucl/src $(TARGET_DIR)libucl$(RBARCH).a -# This file can be generated in the dualboot/ directory -$(OUT)/dualboot.o: dualboot.[ch] - @echo CC $< - $(SILENT)$(CC) $(CFLAGS) -c -o $(OUT)/dualboot.o dualboot.c +# inputs +LIBSOURCES := dualboot.c md5.c mkamsboot.c +SOURCES := $(LIBSOURCES) main.c +OBJS := $(patsubst %.c,%.o,$(addprefix $(OUT)/,$(SOURCES))) +LIBOBJS := $(patsubst %.c,%.o,$(addprefix $(OUT)/,$(LIBSOURCES))) +EXTRADEPS := $(LIBUCL) -$(OUT)/md5.o: md5.[ch] - @echo CC $< - $(SILENT)$(CC) $(CFLAGS) -c -o $(OUT)/md5.o -W -Wall md5.c - -DEPENDANT_OBJS=$(LIBUCL) $(OUT)/dualboot.o $(OUT)/md5.o - -$(OUT)/mkamsboot.o: mkamsboot.[ch] $(DEPENDANT_OBJS) - @echo CC $< - $(SILENT)$(CC) $(CFLAGS) -c -o $(OUT)/mkamsboot.o -W -Wall mkamsboot.c -DVERSION=\"$(APPVERSION)\" - -$(OUTPUT): $(OUT) $(OUT)/mkamsboot.o - @echo CC $< - $(SILENT)$(CC) $(CFLAGS) -o $(OUTPUT) $(OUT)/mkamsboot.o $(DEPENDANT_OBJS) +$(OUT)/%.o: %.c $(OUT) + @echo CC $< $ + $(SILENT)$(CC) $(CFLAGS) -c -o $@ $< +# building the library archive $(OUT)/libmkamsboot.o: $(OUT)/mkamsboot.o @echo CC $< $(SILENT)$(CC) $(CFLAGS) -DLIB -c -o $(OUT)/libmkamsboot.o -W -Wall mkamsboot.c -libmkamsboot$(RBARCH).a: $(OUT) $(OUT)/libmkamsboot.o +libmkamsboot$(RBARCH).a: $(LIBOBJS) @echo AR $@ - $(SILENT)$(AR) ruc $(TARGET_DIR)libmkamsboot$(RBARCH).a $(OUT)/libmkamsboot.o $(OUT)/md5.o $(OUT)/dualboot.o + $(SILENT)$(AR) ruc $(TARGET_DIR)$@ $^ + +# building the standalone executable +$(OUTPUT): $(OBJS) $(EXTRADEPS) + @echo LD $@ + $(SILENT)$(CC) $(CFLAGS) -o$(OUTPUT) $(OBJS) $(EXTRADEPS) # some trickery to build ppc and i386 from a single call ifeq ($(RBARCH),) diff --git a/rbutil/mkamsboot/main.c b/rbutil/mkamsboot/main.c new file mode 100644 index 0000000000..5715248685 --- /dev/null +++ b/rbutil/mkamsboot/main.c @@ -0,0 +1,168 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * mkamsboot - a tool for merging bootloader code into an Sansa V2 + * (AMS) firmware file + * + * Copyright (C) 2008 Dave Chapman + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "mkamsboot.h" + +/* Header for ARM code binaries */ +#include "dualboot.h" + +/* Win32 compatibility */ +#ifndef O_BINARY +#define O_BINARY 0 +#endif + +/* standalone executable */ +int main(int argc, char* argv[]) +{ + char *infile, *bootfile, *outfile; + int fdout; + off_t len; + uint32_t n; + unsigned char* buf; + int firmware_size; + int bootloader_size; + unsigned char* of_packed; + int of_packedsize; + unsigned char* rb_packed; + int rb_packedsize; + int totalsize; + char errstr[200]; + struct md5sums sum; + char md5sum[33]; /* 32 digits + \0 */ + + sum.md5 = md5sum; + +/* VERSION comes frome the Makefile */ + fprintf(stderr, +"mkamsboot Version " VERSION "\n" +"This is free software; see the source for copying conditions. There is NO\n" +"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" +"\n"); + + if(argc != 4) { + printf("Usage: mkamsboot \n"); + return 1; + } + + infile = argv[1]; + bootfile = argv[2]; + outfile = argv[3]; + + /* Load original firmware file */ + buf = load_of_file(infile, &len, &sum, + &firmware_size, &of_packed, &of_packedsize, errstr, sizeof(errstr)); + + if (buf == NULL) { + fprintf(stderr, "%s", errstr); + fprintf(stderr, "[ERR] Could not load %s\n", infile); + return 1; + } + + fprintf(stderr, "[INFO] Original firmware MD5 checksum match\n"); + fprintf(stderr, "[INFO] Model: Sansa %s v%d - Firmware version: %s\n", + model_names[sum.model], hw_revisions[sum.model], sum.version); + + + /* Load bootloader file */ + rb_packed = load_rockbox_file(bootfile, sum.model, &bootloader_size, + &rb_packedsize, errstr, sizeof(errstr)); + if (rb_packed == NULL) { + fprintf(stderr, "%s", errstr); + fprintf(stderr, "[ERR] Could not load %s\n", bootfile); + free(buf); + free(of_packed); + return 1; + } + + printf("[INFO] Firmware patching has begun !\n\n"); + + fprintf(stderr, "[INFO] Original firmware size: %d bytes\n", + firmware_size); + fprintf(stderr, "[INFO] Packed OF size: %d bytes\n", + of_packedsize); + fprintf(stderr, "[INFO] Bootloader size: %d bytes\n", + (int)bootloader_size); + fprintf(stderr, "[INFO] Packed bootloader size: %d bytes\n", + rb_packedsize); + fprintf(stderr, "[INFO] Dual-boot function size: %d bytes\n", + bootloader_sizes[sum.model]); + fprintf(stderr, "[INFO] UCL unpack function size: %u bytes\n", + (unsigned int)sizeof(nrv2e_d8)); + + totalsize = total_size(sum.model, of_packedsize, rb_packedsize); + + fprintf(stderr, "[INFO] Total size of new image: %d bytes\n", totalsize); + + if (totalsize > firmware_size) { + fprintf(stderr, "[ERR] No room to insert bootloader, aborting\n"); + free(buf); + free(of_packed); + free(rb_packed); + return 1; + } + + patch_firmware(sum.model, fw_revisions[sum.model], firmware_size, buf, len, + of_packed, of_packedsize, rb_packed, rb_packedsize); + + /* Write the new firmware */ + fdout = open(outfile, O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0666); + + if (fdout < 0) { + fprintf(stderr, "[ERR] Could not open %s for writing\n", outfile); + free(buf); + free(of_packed); + free(rb_packed); + return 1; + } + + n = write(fdout, buf, len); + + if (n != (unsigned)len) { + fprintf(stderr, "[ERR] Could not write firmware file\n"); + free(buf); + free(of_packed); + free(rb_packed); + return 1; + } + + close(fdout); + free(buf); + free(of_packed); + free(rb_packed); + fprintf(stderr, "\n[INFO] Patching succeeded!\n"); + + return 0; +} + diff --git a/rbutil/mkamsboot/mkamsboot.c b/rbutil/mkamsboot/mkamsboot.c index 9d0ad9cd9a..8d40a1966c 100644 --- a/rbutil/mkamsboot/mkamsboot.c +++ b/rbutil/mkamsboot/mkamsboot.c @@ -110,7 +110,7 @@ execution to the uncompressed firmware. #endif /* 4 for m200, 2 for e200/c200, 1 or 2 for fuze/clop */ -static const unsigned short hw_revisions[] = { +const unsigned short hw_revisions[] = { [MODEL_FUZE] = 1, [MODEL_CLIP] = 1, [MODEL_CLIPV2] = 2, @@ -120,7 +120,7 @@ static const unsigned short hw_revisions[] = { }; /* version 2 is used in Clipv2 and Fuzev2 firmwares */ -static const unsigned short fw_revisions[] = { +const unsigned short fw_revisions[] = { [MODEL_FUZE] = 1, [MODEL_CLIP] = 1, [MODEL_CLIPV2] = 2, @@ -130,7 +130,7 @@ static const unsigned short fw_revisions[] = { }; /* Descriptive name of these models */ -static const char* model_names[] = { +const char* model_names[] = { [MODEL_FUZE] = "Fuze", [MODEL_CLIP] = "Clip", [MODEL_CLIPV2] = "Clip", @@ -150,7 +150,7 @@ static const unsigned char* bootloaders[] = { }; /* Size of dualboot functions for these models */ -static const int bootloader_sizes[] = { +const int bootloader_sizes[] = { [MODEL_FUZE] = sizeof(dualboot_fuze), [MODEL_CLIP] = sizeof(dualboot_clip), [MODEL_CLIPV2] = sizeof(dualboot_clipv2), @@ -574,127 +574,3 @@ int total_size(int model, int rb_packedsize, int of_packedsize) rb_packedsize; } -#ifndef LIB -/* standalone executable */ -int main(int argc, char* argv[]) -{ - char *infile, *bootfile, *outfile; - int fdout; - off_t len; - uint32_t n; - unsigned char* buf; - int firmware_size; - int bootloader_size; - unsigned char* of_packed; - int of_packedsize; - unsigned char* rb_packed; - int rb_packedsize; - int totalsize; - char errstr[200]; - struct md5sums sum; - char md5sum[33]; /* 32 digits + \0 */ - - sum.md5 = md5sum; - -/* VERSION comes frome the Makefile */ - fprintf(stderr, -"mkamsboot Version " VERSION "\n" -"This is free software; see the source for copying conditions. There is NO\n" -"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" -"\n"); - - if(argc != 4) { - printf("Usage: mkamsboot \n"); - return 1; - } - - infile = argv[1]; - bootfile = argv[2]; - outfile = argv[3]; - - /* Load original firmware file */ - buf = load_of_file(infile, &len, &sum, - &firmware_size, &of_packed, &of_packedsize, errstr, sizeof(errstr)); - - if (buf == NULL) { - fprintf(stderr, "%s", errstr); - fprintf(stderr, "[ERR] Could not load %s\n", infile); - return 1; - } - - fprintf(stderr, "[INFO] Original firmware MD5 checksum match\n"); - fprintf(stderr, "[INFO] Model: Sansa %s v%d - Firmware version: %s\n", - model_names[sum.model], hw_revisions[sum.model], sum.version); - - - /* Load bootloader file */ - rb_packed = load_rockbox_file(bootfile, sum.model, &bootloader_size, - &rb_packedsize, errstr, sizeof(errstr)); - if (rb_packed == NULL) { - fprintf(stderr, "%s", errstr); - fprintf(stderr, "[ERR] Could not load %s\n", bootfile); - free(buf); - free(of_packed); - return 1; - } - - printf("[INFO] Firmware patching has begun !\n\n"); - - fprintf(stderr, "[INFO] Original firmware size: %d bytes\n", - firmware_size); - fprintf(stderr, "[INFO] Packed OF size: %d bytes\n", - of_packedsize); - fprintf(stderr, "[INFO] Bootloader size: %d bytes\n", - (int)bootloader_size); - fprintf(stderr, "[INFO] Packed bootloader size: %d bytes\n", - rb_packedsize); - fprintf(stderr, "[INFO] Dual-boot function size: %d bytes\n", - bootloader_sizes[sum.model]); - fprintf(stderr, "[INFO] UCL unpack function size: %u bytes\n", - (unsigned int)sizeof(nrv2e_d8)); - - totalsize = total_size(sum.model, of_packedsize, rb_packedsize); - - fprintf(stderr, "[INFO] Total size of new image: %d bytes\n", totalsize); - - if (totalsize > firmware_size) { - fprintf(stderr, "[ERR] No room to insert bootloader, aborting\n"); - free(buf); - free(of_packed); - free(rb_packed); - return 1; - } - - patch_firmware(sum.model, fw_revisions[sum.model], firmware_size, buf, len, - of_packed, of_packedsize, rb_packed, rb_packedsize); - - /* Write the new firmware */ - fdout = open(outfile, O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0666); - - if (fdout < 0) { - fprintf(stderr, "[ERR] Could not open %s for writing\n", outfile); - free(buf); - free(of_packed); - free(rb_packed); - return 1; - } - - n = write(fdout, buf, len); - - if (n != (unsigned)len) { - fprintf(stderr, "[ERR] Could not write firmware file\n"); - free(buf); - free(of_packed); - free(rb_packed); - return 1; - } - - close(fdout); - free(buf); - free(of_packed); - free(rb_packed); - fprintf(stderr, "\n[INFO] Patching succeeded!\n"); - - return 0; -} -#endif diff --git a/rbutil/mkamsboot/mkamsboot.h b/rbutil/mkamsboot/mkamsboot.h index 51f5b2f699..647770ff46 100644 --- a/rbutil/mkamsboot/mkamsboot.h +++ b/rbutil/mkamsboot/mkamsboot.h @@ -51,6 +51,11 @@ struct md5sums { char *md5; }; +extern const unsigned short hw_revisions[]; +extern const unsigned short fw_revisions[]; +extern const char* model_names[]; +extern const int bootloader_sizes[]; + /* load_rockbox_file() * * Loads a rockbox bootloader file into memory -- cgit v1.2.3