From 303c486f4f7d557c35e7b6543fa9827fb94846b9 Mon Sep 17 00:00:00 2001 From: Amaury Pouly Date: Sun, 6 Nov 2011 19:44:03 +0000 Subject: Introduce mkimxboot to build bootloader images for the i.MX (only the fuze+ currently). Still not integrated in rbutil git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30920 a1c6a512-1295-4272-9138-f99709370657 --- rbutil/mkimxboot/Makefile | 112 +++++++++ rbutil/mkimxboot/dualboot.c | 9 + rbutil/mkimxboot/dualboot.h | 3 + rbutil/mkimxboot/dualboot/Makefile | 33 +++ rbutil/mkimxboot/dualboot/bin2c.c | 140 +++++++++++ rbutil/mkimxboot/dualboot/dualboot.S | 39 ++++ rbutil/mkimxboot/dualboot/dualboot.lds | 16 ++ rbutil/mkimxboot/main.c | 132 +++++++++++ rbutil/mkimxboot/md5.c | 246 ++++++++++++++++++++ rbutil/mkimxboot/md5.h | 25 ++ rbutil/mkimxboot/mkimxboot.c | 409 +++++++++++++++++++++++++++++++++ rbutil/mkimxboot/mkimxboot.h | 55 +++++ 12 files changed, 1219 insertions(+) create mode 100644 rbutil/mkimxboot/Makefile create mode 100644 rbutil/mkimxboot/dualboot.c create mode 100644 rbutil/mkimxboot/dualboot.h create mode 100644 rbutil/mkimxboot/dualboot/Makefile create mode 100644 rbutil/mkimxboot/dualboot/bin2c.c create mode 100644 rbutil/mkimxboot/dualboot/dualboot.S create mode 100644 rbutil/mkimxboot/dualboot/dualboot.lds create mode 100644 rbutil/mkimxboot/main.c create mode 100644 rbutil/mkimxboot/md5.c create mode 100644 rbutil/mkimxboot/md5.h create mode 100644 rbutil/mkimxboot/mkimxboot.c create mode 100644 rbutil/mkimxboot/mkimxboot.h (limited to 'rbutil/mkimxboot') diff --git a/rbutil/mkimxboot/Makefile b/rbutil/mkimxboot/Makefile new file mode 100644 index 0000000000..08cd887fce --- /dev/null +++ b/rbutil/mkimxboot/Makefile @@ -0,0 +1,112 @@ +# __________ __ ___. +# Open \______ \ ____ ____ | | _\_ |__ _______ ___ +# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / +# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < +# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ +# \/ \/ \/ \/ \/ + +#change for releases +ifndef APPVERSION +APPVERSION=$(shell ../../tools/version.sh ../../) +endif +TARGET_DIR ?= $(shell pwd)/ +# We use the SB code available in the Rockbox utils/sbtools directory +IMXTOOLS_DIR=../../utils/imxtools/ +CFLAGS=-I$(IMXTOOLS_DIR) -Wall -DVERSION=\"$(APPVERSION)\" + +ifndef V +SILENT = @ +endif + +ifeq ($(findstring CYGWIN,$(shell uname)),CYGWIN) +OUTPUT=mkimxboot.exe +CFLAGS+=-mno-cygwin +else +ifeq ($(findstring MINGW,$(shell uname)),MINGW) +OUTPUT=mkimxboot.exe +else +ifeq ($(findstring mingw,$(CC)),mingw) +OUTPUT=mkimxboot.exe +else +OUTPUT=mkimxboot +endif +endif +endif + +ifdef RBARCH +CFLAGS += -arch $(RBARCH) +OBJDIR = $(TARGET_DIR)build/$(RBARCH)/ +else +OBJDIR = $(TARGET_DIR)build/ +endif + +CFLAGS += -std=c99 + +all: $(OUTPUT) + +# inputs +LIBIMXSOURCES=misc.c sb.c crypto.c crc.c aes128.c sha1.c +LIBSOURCES := dualboot.c mkimxboot.c md5.c $(LIBIMXSOURCES) +SOURCES := $(LIBSOURCES) main.c +OBJS := $(patsubst %.c,%.o,$(addprefix $(OBJDIR),$(SOURCES))) +LIBOBJS := $(patsubst %.c,%.o,$(addprefix $(OBJDIR),$(LIBSOURCES))) +EXTRADEPS := + +# explicit dependencies on dualboot.{c,h} and mkimxboot.h +$(OBJDIR)mkimxboot.o: dualboot.h dualboot.c mkimxboot.c mkimxboot.h +$(OBJDIR)main.o: dualboot.h dualboot.c main.c mkimxboot.h + +$(OBJDIR)%.o: %.c + @echo CC $< + $(SILENT)mkdir -p $(dir $@) + $(SILENT)$(CC) $(CFLAGS) -c -o $@ $< + +$(OBJDIR)%.o: $(IMXTOOLS_DIR)%.c + @echo CC $< + $(SILENT)mkdir -p $(dir $@) + $(SILENT)$(CC) $(CFLAGS) -c -o $@ $< + +libmkimxboot$(RBARCH).a: $(TARGET_DIR)libmkimxboot$(RBARCH).a + +$(TARGET_DIR)libmkimxboot$(RBARCH).a: $(LIBOBJS) + @echo AR $(notdir $@) + $(SILENT)$(AR) rucs $@ $^ + +# 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),) +$(TARGET_DIR)libmkimxbooti386.a: + make RBARCH=i386 TARGET_DIR=$(TARGET_DIR) libmkimxbooti386.a + +$(TARGET_DIR)libmkimxbootppc.a: + make RBARCH=ppc TARGET_DIR=$(TARGET_DIR) libmkimxbootppc.a +endif + +libmkimxboot-universal: $(TARGET_DIR)libmkimxbooti386.a $(TARGET_DIR)libmkimxbootppc.a + @echo lipo $(TARGET_DIR)libmkimxboot.a + $(SILENT) rm -f $(TARGET_DIR)libmkimxboot.a + $(SILENT)lipo -create $(TARGET_DIR)libmkimxbootppc.a $(TARGET_DIR)libmkimxbooti386.a -output $(TARGET_DIR)libmkimxboot.a + +clean: + rm -f $(OBJS) $(OUTPUT) libmkimxboot.o $(TARGET_DIR)libmkimxboot*.a mkimxboot.dmg + rm -rf mkimxboot-* i386 ppc $(OBJDIR) + +mkimxboot-i386: + $(MAKE) RBARCH=i386 + mv mkimxboot mkimxboot-i386 + +mkimxboot-ppc: + $(MAKE) RBARCH=ppc + mv mkimxboot mkimxboot-ppc + +mkimxboot-mac: mkimxboot-i386 mkimxboot-ppc + $(SILENT)lipo -create mkimxboot-ppc mkimxboot-i386 -output mkimxboot-mac + +mkimxboot.dmg: mkimxboot-mac + mkdir -p mkimxboot-dmg + cp -p mkimxboot-mac mkimxboot-dmg + hdiutil create -srcfolder mkimxboot-dmg mkimxboot.dmg diff --git a/rbutil/mkimxboot/dualboot.c b/rbutil/mkimxboot/dualboot.c new file mode 100644 index 0000000000..95bee11cf8 --- /dev/null +++ b/rbutil/mkimxboot/dualboot.c @@ -0,0 +1,9 @@ +/* Generated by bin2c */ + +#include "dualboot.h" + +unsigned char dualboot_fuzeplus[36] = { + 0x18, 0x20, 0x9f, 0xe5, 0x00, 0x20, 0x92, 0xe5, 0x01, 0x01, 0x12, 0xe3, 0x00, 0x00, 0xa0, 0x03, + 0x1e, 0xff, 0x2f, 0x01, 0x00, 0x00, 0x81, 0xe5, 0x01, 0x00, 0xa0, 0xe3, 0x1e, 0xff, 0x2f, 0xe1, + 0x10, 0x86, 0x01, 0x80 +}; diff --git a/rbutil/mkimxboot/dualboot.h b/rbutil/mkimxboot/dualboot.h new file mode 100644 index 0000000000..e14f3582a8 --- /dev/null +++ b/rbutil/mkimxboot/dualboot.h @@ -0,0 +1,3 @@ +/* Generated by bin2c */ + +extern unsigned char dualboot_fuzeplus[36]; diff --git a/rbutil/mkimxboot/dualboot/Makefile b/rbutil/mkimxboot/dualboot/Makefile new file mode 100644 index 0000000000..fa5f341b6d --- /dev/null +++ b/rbutil/mkimxboot/dualboot/Makefile @@ -0,0 +1,33 @@ +CC=gcc +CROSS_PREFIX=arm-elf-eabi +# Edit the following variables (plus copy/paste another set of rules) when +# adding a new target. mkimxboot.c also needs to be edited to refer to these +# new images. + +BOOTOBJS = dualboot_fuzeplus.o +BOOTBINS = dualboot_fuzeplus.arm-bin + +all: ../dualboot.h ../dualboot.c + +# Dualboot bootloaders + +dualboot_fuzeplus.o: dualboot.S + $(CROSS_PREFIX)-$(CC) -mcpu=arm926ej-s -DSANSA_FUZEPLUS -c -o dualboot_fuzeplus.o dualboot.S + +# Rules for the ARM code embedded in mkamsboot - assemble, link, then extract +# the binary code and finally convert to .h for building in mkamsboot + +%.arm-elf: %.o + $(CROSS_PREFIX)-ld -Tdualboot.lds -o $@ $< + +%.arm-bin: %.arm-elf + $(CROSS_PREFIX)-objcopy -O binary $< $@ + +../dualboot.c ../dualboot.h: $(BOOTBINS) bin2c + ./bin2c ../dualboot $(BOOTBINS) + +bin2c: bin2c.c + $(CC) -o bin2c bin2c.c + +clean: + rm -f *~ bin2c $(BOOTBINS) $(BOOTOBJS) diff --git a/rbutil/mkimxboot/dualboot/bin2c.c b/rbutil/mkimxboot/dualboot/bin2c.c new file mode 100644 index 0000000000..b02af88a4d --- /dev/null +++ b/rbutil/mkimxboot/dualboot/bin2c.c @@ -0,0 +1,140 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2007 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 + +#ifndef O_BINARY +#define O_BINARY 0 +#endif + +static off_t filesize(int fd) +{ + struct stat buf; + + fstat(fd,&buf); + return buf.st_size; +} + +static void write_cfile(const unsigned char* buf, off_t len, FILE* fp, const char *name) +{ + int i; + + fprintf(fp,"unsigned char %s[%ld] = {",name,len); + + for (i=0;i orig_len) + memset(buf+orig_len, 0, len-orig_len); + + /* remove file extension */ + ext = strchr (array, '.'); + if (ext != NULL) + *ext = '\0'; + write_cfile (buf, len, cfile, array); + fprintf(hfile,"extern unsigned char %s[%ld];\n",array,len); + + close(fd); + } + + fclose(cfile); + fclose(hfile); + + return 0; +} diff --git a/rbutil/mkimxboot/dualboot/dualboot.S b/rbutil/mkimxboot/dualboot/dualboot.S new file mode 100644 index 0000000000..8302829a81 --- /dev/null +++ b/rbutil/mkimxboot/dualboot/dualboot.S @@ -0,0 +1,39 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2011 by Amaury Pouly + * + * 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. + * + ****************************************************************************/ + +.text +.global start +@ int start(uint32_t arg, uint32_t *result_id) +start: +#if defined(SANSA_FUZEPLUS) + /* If volume down key is hold, return so that the OF can boot */ + ldr r2, =0x80018610 @ HW_PINCTRL_DIN1 + ldr r2, [r2] + tst r2, #0x40000000 @ bit 30, active low + moveq r0, #0 @ return 0, continue boot + bxeq lr + /* otherwise jump to section given as argument */ + str r0, [r1] + mov r0, #1 + bx lr +#else +#error No target defined ! +#endif diff --git a/rbutil/mkimxboot/dualboot/dualboot.lds b/rbutil/mkimxboot/dualboot/dualboot.lds new file mode 100644 index 0000000000..caaff1a41d --- /dev/null +++ b/rbutil/mkimxboot/dualboot/dualboot.lds @@ -0,0 +1,16 @@ +ENTRY(start) +OUTPUT_FORMAT(elf32-littlearm) +OUTPUT_ARCH(arm) + +MEMORY +{ + OCRAM : ORIGIN = 0, LENGTH = 0x8000 +} + +SECTIONS +{ + .text 0 : + { + *(.text*) + } > OCRAM +} diff --git a/rbutil/mkimxboot/main.c b/rbutil/mkimxboot/main.c new file mode 100644 index 0000000000..ec2a42380d --- /dev/null +++ b/rbutil/mkimxboot/main.c @@ -0,0 +1,132 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2011 by Amaury Pouly + * + * 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 "mkimxboot.h" + +static void usage(void) +{ + printf("Usage: elftosb [options | file]...\n"); + printf("Options:\n"); + printf(" -?/--help\tDisplay this message\n"); + printf(" -o \tSet output file\n"); + printf(" -i \tSet input file\n"); + printf(" -b \tSet boot file\n"); + printf(" -d/--debug\tEnable debug output\n"); + printf(" -t \tSet type (dualboot, singleboot, recovery)\n"); + printf("By default a dualboot image is built\n"); + exit(1); +} + +int main(int argc, char *argv[]) +{ + char *infile = NULL; + char *outfile = NULL; + char *bootfile = NULL; + enum imx_output_type_t type = IMX_DUALBOOT; + bool debug = false; + + if(argc == 1) + usage(); + + while(1) + { + static struct option long_options[] = + { + {"help", no_argument, 0, '?'}, + {"in-file", no_argument, 0, 'i'}, + {"out-file", required_argument, 0, 'o'}, + {"boot-file", required_argument, 0, 'b'}, + {"debug", no_argument, 0, 'd'}, + {"type", required_argument, 0, 't'}, + {0, 0, 0, 0} + }; + + int c = getopt_long(argc, argv, "?di:o:b:t:", long_options, NULL); + if(c == -1) + break; + switch(c) + { + case 'd': + debug = true; + break; + case '?': + usage(); + break; + case 'o': + outfile = optarg; + break; + case 'i': + infile = optarg; + break; + case 'b': + { + bootfile = optarg; + break; + } + case 't': + if(strcmp(optarg, "dualboot") == 0) + type = IMX_DUALBOOT; + else if(strcmp(optarg, "singleboot") == 0) + type = IMX_SINGLEBOOT; + else if(strcmp(optarg, "recovery") == 0) + type = IMX_RECOVERY; + else + { + printf("Invalid boot type '%s'\n", optarg); + return 1; + } + break; + default: + abort(); + } + } + + if(!infile) + { + printf("You must specify an input file\n"); + return 1; + } + if(!outfile) + { + printf("You must specify an output file\n"); + return 1; + } + if(!bootfile) + { + printf("You must specify an boot file\n"); + return 1; + } + if(optind != argc) + { + printf("Extra arguments on command line\n"); + return 1; + } + + struct imx_option_t opt; + opt.debug = debug; + opt.output = type; + enum imx_error_t err = mkimxboot(infile, bootfile, outfile, opt); + printf("Result: %d\n", err); + return 0; +} diff --git a/rbutil/mkimxboot/md5.c b/rbutil/mkimxboot/md5.c new file mode 100644 index 0000000000..530d8df15a --- /dev/null +++ b/rbutil/mkimxboot/md5.c @@ -0,0 +1,246 @@ +/* + * RFC 1321 compliant MD5 implementation + * + * Copyright (C) 2001-2003 Christophe Devine + * + * 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 program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include + +#include "md5.h" + +#define GET_UINT32(n,b,i) \ +{ \ + (n) = ( (uint32) (b)[(i) ] ) \ + | ( (uint32) (b)[(i) + 1] << 8 ) \ + | ( (uint32) (b)[(i) + 2] << 16 ) \ + | ( (uint32) (b)[(i) + 3] << 24 ); \ +} + +#define PUT_UINT32(n,b,i) \ +{ \ + (b)[(i) ] = (uint8) ( (n) ); \ + (b)[(i) + 1] = (uint8) ( (n) >> 8 ); \ + (b)[(i) + 2] = (uint8) ( (n) >> 16 ); \ + (b)[(i) + 3] = (uint8) ( (n) >> 24 ); \ +} + +void md5_starts( md5_context *ctx ) +{ + ctx->total[0] = 0; + ctx->total[1] = 0; + + ctx->state[0] = 0x67452301; + ctx->state[1] = 0xEFCDAB89; + ctx->state[2] = 0x98BADCFE; + ctx->state[3] = 0x10325476; +} + +void md5_process( md5_context *ctx, uint8 data[64] ) +{ + uint32 X[16], A, B, C, D; + + GET_UINT32( X[0], data, 0 ); + GET_UINT32( X[1], data, 4 ); + GET_UINT32( X[2], data, 8 ); + GET_UINT32( X[3], data, 12 ); + GET_UINT32( X[4], data, 16 ); + GET_UINT32( X[5], data, 20 ); + GET_UINT32( X[6], data, 24 ); + GET_UINT32( X[7], data, 28 ); + GET_UINT32( X[8], data, 32 ); + GET_UINT32( X[9], data, 36 ); + GET_UINT32( X[10], data, 40 ); + GET_UINT32( X[11], data, 44 ); + GET_UINT32( X[12], data, 48 ); + GET_UINT32( X[13], data, 52 ); + GET_UINT32( X[14], data, 56 ); + GET_UINT32( X[15], data, 60 ); + +#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) + +#define P(a,b,c,d,k,s,t) \ +{ \ + a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \ +} + + A = ctx->state[0]; + B = ctx->state[1]; + C = ctx->state[2]; + D = ctx->state[3]; + +#define F(x,y,z) (z ^ (x & (y ^ z))) + + P( A, B, C, D, 0, 7, 0xD76AA478 ); + P( D, A, B, C, 1, 12, 0xE8C7B756 ); + P( C, D, A, B, 2, 17, 0x242070DB ); + P( B, C, D, A, 3, 22, 0xC1BDCEEE ); + P( A, B, C, D, 4, 7, 0xF57C0FAF ); + P( D, A, B, C, 5, 12, 0x4787C62A ); + P( C, D, A, B, 6, 17, 0xA8304613 ); + P( B, C, D, A, 7, 22, 0xFD469501 ); + P( A, B, C, D, 8, 7, 0x698098D8 ); + P( D, A, B, C, 9, 12, 0x8B44F7AF ); + P( C, D, A, B, 10, 17, 0xFFFF5BB1 ); + P( B, C, D, A, 11, 22, 0x895CD7BE ); + P( A, B, C, D, 12, 7, 0x6B901122 ); + P( D, A, B, C, 13, 12, 0xFD987193 ); + P( C, D, A, B, 14, 17, 0xA679438E ); + P( B, C, D, A, 15, 22, 0x49B40821 ); + +#undef F + +#define F(x,y,z) (y ^ (z & (x ^ y))) + + P( A, B, C, D, 1, 5, 0xF61E2562 ); + P( D, A, B, C, 6, 9, 0xC040B340 ); + P( C, D, A, B, 11, 14, 0x265E5A51 ); + P( B, C, D, A, 0, 20, 0xE9B6C7AA ); + P( A, B, C, D, 5, 5, 0xD62F105D ); + P( D, A, B, C, 10, 9, 0x02441453 ); + P( C, D, A, B, 15, 14, 0xD8A1E681 ); + P( B, C, D, A, 4, 20, 0xE7D3FBC8 ); + P( A, B, C, D, 9, 5, 0x21E1CDE6 ); + P( D, A, B, C, 14, 9, 0xC33707D6 ); + P( C, D, A, B, 3, 14, 0xF4D50D87 ); + P( B, C, D, A, 8, 20, 0x455A14ED ); + P( A, B, C, D, 13, 5, 0xA9E3E905 ); + P( D, A, B, C, 2, 9, 0xFCEFA3F8 ); + P( C, D, A, B, 7, 14, 0x676F02D9 ); + P( B, C, D, A, 12, 20, 0x8D2A4C8A ); + +#undef F + +#define F(x,y,z) (x ^ y ^ z) + + P( A, B, C, D, 5, 4, 0xFFFA3942 ); + P( D, A, B, C, 8, 11, 0x8771F681 ); + P( C, D, A, B, 11, 16, 0x6D9D6122 ); + P( B, C, D, A, 14, 23, 0xFDE5380C ); + P( A, B, C, D, 1, 4, 0xA4BEEA44 ); + P( D, A, B, C, 4, 11, 0x4BDECFA9 ); + P( C, D, A, B, 7, 16, 0xF6BB4B60 ); + P( B, C, D, A, 10, 23, 0xBEBFBC70 ); + P( A, B, C, D, 13, 4, 0x289B7EC6 ); + P( D, A, B, C, 0, 11, 0xEAA127FA ); + P( C, D, A, B, 3, 16, 0xD4EF3085 ); + P( B, C, D, A, 6, 23, 0x04881D05 ); + P( A, B, C, D, 9, 4, 0xD9D4D039 ); + P( D, A, B, C, 12, 11, 0xE6DB99E5 ); + P( C, D, A, B, 15, 16, 0x1FA27CF8 ); + P( B, C, D, A, 2, 23, 0xC4AC5665 ); + +#undef F + +#define F(x,y,z) (y ^ (x | ~z)) + + P( A, B, C, D, 0, 6, 0xF4292244 ); + P( D, A, B, C, 7, 10, 0x432AFF97 ); + P( C, D, A, B, 14, 15, 0xAB9423A7 ); + P( B, C, D, A, 5, 21, 0xFC93A039 ); + P( A, B, C, D, 12, 6, 0x655B59C3 ); + P( D, A, B, C, 3, 10, 0x8F0CCC92 ); + P( C, D, A, B, 10, 15, 0xFFEFF47D ); + P( B, C, D, A, 1, 21, 0x85845DD1 ); + P( A, B, C, D, 8, 6, 0x6FA87E4F ); + P( D, A, B, C, 15, 10, 0xFE2CE6E0 ); + P( C, D, A, B, 6, 15, 0xA3014314 ); + P( B, C, D, A, 13, 21, 0x4E0811A1 ); + P( A, B, C, D, 4, 6, 0xF7537E82 ); + P( D, A, B, C, 11, 10, 0xBD3AF235 ); + P( C, D, A, B, 2, 15, 0x2AD7D2BB ); + P( B, C, D, A, 9, 21, 0xEB86D391 ); + +#undef F + + ctx->state[0] += A; + ctx->state[1] += B; + ctx->state[2] += C; + ctx->state[3] += D; +} + +void md5_update( md5_context *ctx, uint8 *input, uint32 length ) +{ + uint32 left, fill; + + if( ! length ) return; + + left = ctx->total[0] & 0x3F; + fill = 64 - left; + + ctx->total[0] += length; + ctx->total[0] &= 0xFFFFFFFF; + + if( ctx->total[0] < length ) + ctx->total[1]++; + + if( left && length >= fill ) + { + memcpy( (void *) (ctx->buffer + left), + (void *) input, fill ); + md5_process( ctx, ctx->buffer ); + length -= fill; + input += fill; + left = 0; + } + + while( length >= 64 ) + { + md5_process( ctx, input ); + length -= 64; + input += 64; + } + + if( length ) + { + memcpy( (void *) (ctx->buffer + left), + (void *) input, length ); + } +} + +static uint8 md5_padding[64] = +{ + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +void md5_finish( md5_context *ctx, uint8 digest[16] ) +{ + uint32 last, padn; + uint32 high, low; + uint8 msglen[8]; + + high = ( ctx->total[0] >> 29 ) + | ( ctx->total[1] << 3 ); + low = ( ctx->total[0] << 3 ); + + PUT_UINT32( low, msglen, 0 ); + PUT_UINT32( high, msglen, 4 ); + + last = ctx->total[0] & 0x3F; + padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); + + md5_update( ctx, md5_padding, padn ); + md5_update( ctx, msglen, 8 ); + + PUT_UINT32( ctx->state[0], digest, 0 ); + PUT_UINT32( ctx->state[1], digest, 4 ); + PUT_UINT32( ctx->state[2], digest, 8 ); + PUT_UINT32( ctx->state[3], digest, 12 ); +} + diff --git a/rbutil/mkimxboot/md5.h b/rbutil/mkimxboot/md5.h new file mode 100644 index 0000000000..71fa395548 --- /dev/null +++ b/rbutil/mkimxboot/md5.h @@ -0,0 +1,25 @@ +#ifndef _MD5_H +#define _MD5_H + +#ifndef uint8 +#define uint8 unsigned char +#endif + +#ifndef uint32 +#define uint32 unsigned long int +#endif + +typedef struct +{ + uint32 total[2]; + uint32 state[4]; + uint8 buffer[64]; +} +md5_context; + +void md5_starts( md5_context *ctx ); +void md5_update( md5_context *ctx, uint8 *input, uint32 length ); +void md5_finish( md5_context *ctx, uint8 digest[16] ); + +#endif /* md5.h */ + diff --git a/rbutil/mkimxboot/mkimxboot.c b/rbutil/mkimxboot/mkimxboot.c new file mode 100644 index 0000000000..f1a874b3f5 --- /dev/null +++ b/rbutil/mkimxboot/mkimxboot.c @@ -0,0 +1,409 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2011 by Amaury Pouly + * + * 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 "mkimxboot.h" +#include "sb.h" +#include "dualboot.h" +#include "md5.h" + +/* Supported models */ +enum imx_model_t +{ + MODEL_UNKNOWN = -1, + MODEL_FUZEPLUS = 0, + /* new models go here */ + + NUM_MODELS +}; + +struct imx_md5sum_t +{ + int model; + char *md5sum; +}; + +struct imx_model_desc_t +{ + /* Descriptive name of this model */ + const char *model_name; + /* Dualboot code for this model */ + const unsigned char *dualboot; + /* Size of dualboot functions for this model */ + int dualboot_size; + /* Model name used in the Rockbox header in ".sansa" files - these match the + -add parameter to the "scramble" tool */ + const char *rb_model_name; + /* Model number used to initialise the checksum in the Rockbox header in + ".sansa" files - these are the same as MODEL_NUMBER in config-target.h */ + const int rb_model_num; + /* Number of keys needed to decrypt/encrypt */ + int nr_keys; + /* Array of keys */ + struct crypto_key_t *keys; + /* Dualboot load address */ + uint32_t dualboot_addr; + /* Bootloader load address */ + uint32_t bootloader_addr; +}; + +static const struct imx_md5sum_t imx_sums[] = +{ + { MODEL_FUZEPLUS, "c3e27620a877dc6b200b97dcb3e0ecc7" }, /* Version 2.38.6 */ +}; + +static struct crypto_key_t zero_key = +{ + .method = CRYPTO_KEY, + .u.key = {0} +}; + +static const struct imx_model_desc_t imx_models[] = +{ + [MODEL_FUZEPLUS] = { "Fuze+", dualboot_fuzeplus, sizeof(dualboot_fuzeplus), "fuz+", 72, + 1, &zero_key }, +}; + +#define NR_IMX_SUMS (sizeof(imx_sums) / sizeof(imx_sums[0])) +#define NR_IMX_MODELS (sizeof(imx_models) / sizeof(imx_models[0])) + +#define MAGIC_ROCK 0x726f636b /* 'rock' */ +#define MAGIC_RECOVERY 0xfee1dead +#define MAGIC_NORMAL 0xcafebabe + +static enum imx_error_t patch_std_zero_host_play(int jump_before, int model, + enum imx_output_type_t type, struct sb_file_t *sb_file, void *boot, size_t boot_sz) +{ + /* We assume the file has three boot sections: ____, host, play and one + * resource section rsrc. + * + * Dual Boot: + * ---------- + * We patch the file by inserting the dualboot code before the th + * call in the ____ section. We give it as argument the section name 'rock' + * and add a section called 'rock' after rsrc which contains the bootloader. + * + * Single Boot & Recovery: + * ----------------------- + * We patch the file by inserting the bootloader code after the th + * call in the ____ section and get rid of everything else. In recovery mode, + * we give 0xfee1dead as argument */ + + /* Do not override real key and IV */ + sb_file->override_crypto_iv = false; + sb_file->override_real_key = false; + + /* first locate the good instruction */ + struct sb_section_t *sec = &sb_file->sections[0]; + int jump_idx = 0; + while(jump_idx < sec->nr_insts && jump_before > 0) + if(sec->insts[jump_idx++].inst == SB_INST_CALL) + jump_before--; + if(jump_idx == sec->nr_insts) + { + printf("[ERR] Cannot locate call in section ____\n"); + return IMX_DONT_KNOW_HOW_TO_PATCH; + } + + if(type == IMX_DUALBOOT) + { + /* create a new instruction array with a hole for two instructions */ + struct sb_inst_t *new_insts = xmalloc(sizeof(struct sb_inst_t) * (sec->nr_insts + 2)); + memcpy(new_insts, sec->insts, sizeof(struct sb_inst_t) * jump_idx); + memcpy(new_insts + jump_idx + 2, sec->insts + jump_idx, + sizeof(struct sb_inst_t) * (sec->nr_insts - jump_idx)); + /* first instruction is be a load */ + struct sb_inst_t *load = &new_insts[jump_idx]; + memset(load, 0, sizeof(struct sb_inst_t)); + load->inst = SB_INST_LOAD; + load->size = imx_models[model].dualboot_size; + load->addr = imx_models[model].dualboot_addr; + /* duplicate memory because it will be free'd */ + load->data = memdup(imx_models[model].dualboot, imx_models[model].dualboot_size); + /* second instruction is a call */ + struct sb_inst_t *call = &new_insts[jump_idx + 1]; + memset(call, 0, sizeof(struct sb_inst_t)); + call->inst = SB_INST_CALL; + call->addr = imx_models[model].dualboot_addr; + call->argument = MAGIC_ROCK; + /* free old instruction array */ + free(sec->insts); + sec->insts = new_insts; + sec->nr_insts += 2; + + /* create a new section */ + struct sb_section_t rock_sec; + memset(&rock_sec, 0, sizeof(rock_sec)); + /* section has two instructions: load and call */ + rock_sec.identifier = MAGIC_ROCK; + rock_sec.alignment = BLOCK_SIZE; + rock_sec.nr_insts = 2; + rock_sec.insts = xmalloc(2 * sizeof(struct sb_inst_t)); + memset(rock_sec.insts, 0, 2 * sizeof(struct sb_inst_t)); + rock_sec.insts[0].inst = SB_INST_LOAD; + rock_sec.insts[0].size = boot_sz; + rock_sec.insts[0].data = memdup(boot, boot_sz); + rock_sec.insts[0].addr = imx_models[model].bootloader_addr; + rock_sec.insts[1].inst = SB_INST_CALL; + rock_sec.insts[1].addr = imx_models[model].bootloader_addr; + rock_sec.insts[1].argument = MAGIC_NORMAL; + + sb_file->sections = augment_array(sb_file->sections, + sizeof(struct sb_section_t), sb_file->nr_sections, + &rock_sec, 1); + sb_file->nr_sections++; + + return IMX_SUCCESS; + } + else if(type == IMX_SINGLEBOOT || type == IMX_RECOVERY) + { + bool recovery = type == IMX_RECOVERY; + /* remove everything after the call and add two instructions: load and call */ + struct sb_inst_t *new_insts = xmalloc(sizeof(struct sb_inst_t) * (jump_idx + 2)); + memcpy(new_insts, sec->insts, sizeof(struct sb_inst_t) * jump_idx); + for(int i = jump_idx; i < sec->nr_insts; i++) + sb_free_instruction(sec->insts[i]); + memset(new_insts + jump_idx, 0, 2 * sizeof(struct sb_inst_t)); + new_insts[jump_idx + 0].inst = SB_INST_LOAD; + new_insts[jump_idx + 0].size = boot_sz; + new_insts[jump_idx + 0].data = memdup(boot, boot_sz); + new_insts[jump_idx + 0].addr = imx_models[model].bootloader_addr; + new_insts[jump_idx + 1].inst = SB_INST_CALL; + new_insts[jump_idx + 1].addr = imx_models[model].bootloader_addr; + new_insts[jump_idx + 1].argument = recovery ? MAGIC_RECOVERY : MAGIC_NORMAL; + + free(sec->insts); + sec->insts = new_insts; + sec->nr_insts = jump_idx + 2; + /* remove all other sections */ + for(int i = 1; i < sb_file->nr_sections; i++) + sb_free_section(sb_file->sections[i]); + struct sb_section_t *new_sec = xmalloc(sizeof(struct sb_section_t)); + memcpy(new_sec, &sb_file->sections[0], sizeof(struct sb_section_t)); + free(sb_file->sections); + sb_file->sections = new_sec; + sb_file->nr_sections = 1; + + return IMX_SUCCESS; + } + else + { + printf("[ERR] Bad output type !\n"); + return IMX_DONT_KNOW_HOW_TO_PATCH; + } +} + +static enum imx_error_t patch_firmware(int model, enum imx_output_type_t type, + struct sb_file_t *sb_file, void *boot, size_t boot_sz) +{ + switch(model) + { + case MODEL_FUZEPLUS: + /* The Fuze+ uses the standard ____, host, play sections, patch after third + * call in ____ section */ + return patch_std_zero_host_play(3, model, type, sb_file, boot, boot_sz); + default: + return IMX_DONT_KNOW_HOW_TO_PATCH; + } +} + +static void imx_printf(void *user, bool error, color_t c, const char *fmt, ...) +{ + (void) user; + (void) c; + va_list args; + va_start(args, fmt); + /* + if(error) + printf("[ERR] "); + else + printf("[INFO] "); + */ + vprintf(fmt, args); + va_end(args); +} + +static uint32_t get_uint32be(unsigned char *p) +{ + return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; +} + +enum imx_error_t mkimxboot(const char *infile, const char *bootfile, + const char *outfile, struct imx_option_t opt) +{ + /* Dump tables */ + do + { + printf("[INFO] mkimxboot models:\n"); + for(int i = 0; i < NR_IMX_MODELS; i++) + { + printf("[INFO] %s: idx=%d rb_model=%s rb_num=%d\n", + imx_models[i].model_name, i, imx_models[i].rb_model_name, + imx_models[i].rb_model_num); + } + printf("[INFO] mkimxboot mapping:\n"); + for(int i = 0; i < NR_IMX_SUMS; i++) + { + printf("[INFO] md5sum=%s -> idx=%d\n", imx_sums[i].md5sum, + imx_sums[i].model); + } + }while(0); + /* compute MD5 sum of the file */ + uint8_t file_md5sum[16]; + do + { + FILE *f = fopen(infile, "rb"); + if(f == NULL) + { + printf("[ERR] Cannot open input file\n"); + return IMX_OPEN_ERROR; + } + fseek(f, 0, SEEK_END); + size_t sz = ftell(f); + fseek(f, 0, SEEK_SET); + void *buf = xmalloc(sz); + if(fread(buf, sz, 1, f) != 1) + { + fclose(f); + free(buf); + printf("[ERR] Cannot read file\n"); + return IMX_READ_ERROR; + } + fclose(f); + md5_context ctx; + md5_starts(&ctx); + md5_update(&ctx, buf, sz); + md5_finish(&ctx, file_md5sum); + free(buf); + }while(0); + printf("[INFO] MD5 sum of the file: "); + print_hex(file_md5sum, 16, true); + /* find model */ + int model; + do + { + int i = 0; + while(i < NR_IMX_SUMS) + { + uint8_t md5[20]; + if(strlen(imx_sums[i].md5sum) != 32) + { + printf("[INFO] Invalid MD5 sum in imx_sums\n"); + return IMX_ERROR; + } + for(int j = 0; j < 16; j++) + { + byte a, b; + if(convxdigit(imx_sums[i].md5sum[2 * j], &a) || convxdigit(imx_sums[i].md5sum[2 * j + 1], &b)) + return false; + md5[j] = (a << 4) | b; + } + if(memcmp(file_md5sum, md5, 16) == 0) + break; + i++; + } + if(i == NR_IMX_SUMS) + { + printf("[ERR] MD5 sum doesn't match any known file\n"); + return IMX_NO_MATCH; + } + model = imx_sums[i].model; + }while(0); + printf("[INFO] File is for model %d (%s)\n", model, imx_models[model].model_name); + /* load rockbox file */ + uint8_t *boot; + size_t boot_size; + do + { + FILE *f = fopen(bootfile, "rb"); + if(f == NULL) + { + printf("[ERR] Cannot open boot file\n"); + return IMX_OPEN_ERROR; + } + fseek(f, 0, SEEK_END); + boot_size = ftell(f); + fseek(f, 0, SEEK_SET); + boot = xmalloc(boot_size); + if(fread(boot, boot_size, 1, f) != 1) + { + free(boot); + fclose(f); + printf("[ERR] Cannot read boot file\n"); + return IMX_READ_ERROR; + } + fclose(f); + }while(0); + /* Check boot file */ + do + { + if(boot_size < 8) + { + printf("[ERR] Bootloader file is too small to be valid\n"); + free(boot); + return IMX_BOOT_INVALID; + } + /* check model name */ + uint8_t *name = boot + 4; + if(memcmp(name, imx_models[model].rb_model_name, 4) != 0) + { + printf("[ERR] Bootloader model doesn't match found model for input file\n"); + free(boot); + return IMX_BOOT_MISMATCH; + } + /* check checksum */ + uint32_t sum = imx_models[model].rb_model_num; + for(int i = 8; i < boot_size; i++) + sum += boot[i]; + if(sum != get_uint32be(boot)) + { + printf("[ERR] Bootloader checksum mismatch\n"); + free(boot); + return IMX_BOOT_CHECKSUM_ERROR; + } + }while(0); + /* load OF file */ + struct sb_file_t *sb_file; + do + { + enum sb_error_t err; + g_debug = opt.debug; + clear_keys(); + add_keys(imx_models[model].keys, imx_models[model].nr_keys); + sb_file = sb_read_file(infile, false, NULL, &imx_printf, &err); + if(sb_file == NULL) + { + clear_keys(); + free(boot); + return IMX_FIRST_SB_ERROR + err; + } + }while(0); + /* produce file */ + enum imx_error_t ret = patch_firmware(model, opt.output, sb_file, boot + 8, boot_size - 8); + if(ret == IMX_SUCCESS) + ret = sb_write_file(sb_file, outfile); + + clear_keys(); + free(boot); + sb_free(sb_file); + return ret; +} diff --git a/rbutil/mkimxboot/mkimxboot.h b/rbutil/mkimxboot/mkimxboot.h new file mode 100644 index 0000000000..97642c5d84 --- /dev/null +++ b/rbutil/mkimxboot/mkimxboot.h @@ -0,0 +1,55 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2011 by Amaury Pouly + * + * 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 "sb.h" + +enum imx_error_t +{ + IMX_SUCCESS = 0, + IMX_ERROR = -1, + IMX_OPEN_ERROR = -2, + IMX_READ_ERROR = -3, + IMX_NO_MATCH = -4, + IMX_BOOT_INVALID = -5, + IMX_BOOT_MISMATCH = -6, + IMX_BOOT_CHECKSUM_ERROR = -7, + IMX_DONT_KNOW_HOW_TO_PATCH = -8, + IMX_FIRST_SB_ERROR = -9, +}; + +enum imx_output_type_t +{ + IMX_DUALBOOT = 0, + IMX_RECOVERY = 1, + IMX_SINGLEBOOT = 2, +}; + +struct imx_option_t +{ + bool debug; + enum imx_output_type_t output; +}; + +enum imx_error_t mkimxboot(const char *infile, const char *bootfile, + const char *outfile, struct imx_option_t opt); -- cgit v1.2.3