diff --git a/rbutil/mkamsboot/Makefile b/rbutil/mkamsboot/Makefile index 5a62650..9029478 100644 --- a/rbutil/mkamsboot/Makefile +++ b/rbutil/mkamsboot/Makefile @@ -1,90 +1,33 @@ -CC=gcc +CC?=gcc # We use the UCL code available in the Rockbox tools/ directory CFLAGS=-I../../tools/ucl/include LIBUCL=../../tools/ucl/src/libucl.a -# Edit the following variables (plus copy/paste another set of rules) when -# adding a new target. mkamsboot.c also needs to be edited to refer to these -# new images. - -BOOTIMAGES = dualboot_clip.o dualboot_e200v2.o dualboot_c200v2.o dualboot_m200v4.o dualboot_fuze.o -BOOTHEADERS = dualboot_clip.h dualboot_e200v2.h dualboot_c200v2.h dualboot_m200v4.h dualboot_fuze.h - -CLIPFILES = dualboot_clip.arm-o dualboot_clip.o dualboot_clip.c dualboot_clip.h - -E200V2FILES = dualboot_e200v2.arm-o dualboot_e200v2.o dualboot_e200v2.c \ - dualboot_e200v2.h - -M200V4FILES = dualboot_m200v4.arm-o dualboot_m200v4.o dualboot_m200v4.arm-bin \ - dualboot_m200v4.c dualboot_m200v4.h - -C200V2FILES = dualboot_c200v2.arm-o dualboot_c200v2.o dualboot_c200v2.c \ - dualboot_c200v2.h - -FUZEFILES = dualboot_fuze.arm-o dualboot_fuze.o dualboot_fuze.c dualboot_fuze.h - all: mkamsboot -# Dualboot bootloaders - -dualboot_clip.arm-o: dualboot.S - arm-elf-gcc -DSANSA_CLIP -c -o dualboot_clip.arm-o dualboot.S - -dualboot_fuze.arm-o: dualboot.S - arm-elf-gcc -DSANSA_FUZE -c -o dualboot_fuze.arm-o dualboot.S - -dualboot_e200v2.arm-o: dualboot.S - arm-elf-gcc -DSANSA_E200V2 -c -o dualboot_e200v2.arm-o dualboot.S - -dualboot_m200v4.arm-o: dualboot.S - arm-elf-gcc -DSANSA_M200V4 -c -o dualboot_m200v4.arm-o dualboot.S - -dualboot_c200v2.arm-o: dualboot.S - arm-elf-gcc -DSANSA_C200V2 -c -o dualboot_c200v2.arm-o dualboot.S - -# Rules for the ucl unpack function -nrv2e_d8.arm-o: nrv2e_d8.S - arm-elf-gcc -DPURE_THUMB -c -o nrv2e_d8.arm-o nrv2e_d8.S - - - $(LIBUCL): make -C ../../tools/ucl/src libucl.a +# This file can be generated in the dualboot/ directory +dualboot.o: dualboot.c + $(CC) $(CFLAGS) -c -o dualboot.o dualboot.c + md5.o: md5.c md5.h $(CC) $(CFLAGS) -c -o md5.o -W -Wall md5.c -mkamsboot.o: mkamsboot.c $(BOOTHEADERS) nrv2e_d8.h md5.h +mkamsboot.o: mkamsboot.c dualboot.h md5.h $(CC) $(CFLAGS) -c -o mkamsboot.o -W -Wall mkamsboot.c -mkamsboot: mkamsboot.o $(BOOTIMAGES) nrv2e_d8.o md5.o $(LIBUCL) - $(CC) -o mkamsboot mkamsboot.o $(BOOTIMAGES) nrv2e_d8.o md5.o $(LIBUCL) - -# Rules for the ARM code embedded in mkamsboot - assemble, link, then extract -# the binary code and finally convert to .c/.h for linking with mkamsboot - -%.arm-elf: %.arm-o - arm-elf-ld -e 0 -Ttext=0 -o $@ $< - -%.arm-bin: %.arm-elf - arm-elf-objcopy -O binary $< $@ - -%.c %.h: %.arm-bin bin2c - ./bin2c $< $* - -# Generic host rule. -%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< +mkamsboot: mkamsboot.o md5.o dualboot.o $(LIBUCL) + $(CC) $(CFLAGS) -o mkamsboot mkamsboot.o md5.o dualboot.o $(LIBUCL) -# Cancel the implicit .S -> .o rule -%.o: %.S +libmkamsboot.o: mkamsboot.c dualboot.h md5.h + $(CC) $(CFLAGS) -DLIB -c -o libmkamsboot.o -W -Wall mkamsboot.c -bin2c: bin2c.c - $(CC) -o bin2c bin2c.c +libmkamsboot.a: libmkamsboot.o md5.o dualboot.o + $(AR) ruv libmkamsboot.a libmkamsboot.o md5.o dualboot.o clean: - rm -f mkamsboot mkamsboot.o nrv2e_d8.arm-o nrv2e_d8.arm-elf \ - nrv2e_d8.arm-bin *~ bin2c nrv2e_d8.c nrv2e_d8.h nrv2e_d8.o md5.o \ - $(BOOTIMAGES) $(CLIPFILES) $(E200V2FILES) $(M200V4FILES) $(FUZEFILES) \ - $(C200V2FILES) + rm -f mkamsboot mkamsboot.o *~ md5.o dualboot.o \ + libmkamsboot.o libmkamsboot.a diff --git a/rbutil/mkamsboot/README b/rbutil/mkamsboot/README index bd0d2a3..97261e5 100644 --- a/rbutil/mkamsboot/README +++ b/rbutil/mkamsboot/README @@ -3,4 +3,61 @@ mkamsboot A tool to inject a bootloader into a Sansa V2 (AMS) firmware file. -See comments in mkamsboot.c and dualboot.S for more information. + +Usage +----- + +mkamsboot + + is an original Sansa firmware file obtained from the Sansa + forums for example : http://forums.sandisk.com/sansa/?category.id=devices + + is the code you want to execute (a rockbox bootloader), previously + scrambled with tools/scramble utility. + + is the resulting firmware file which you'll have to copy on your + Sansa player. See "Firmware filenames". + + +Supported models +---------------- + +Sansa Clip : *only* firmwares starting with "01." +Sansa Fuze : *only* firmwares starting with "01." +Sansa E200 : *only* firmwares starting with "03." +Sansa C200 : *only* firmwares starting with "03." +Sansa M200 : *only* firmwares starting with "4." + + +Firmware filenames +------------------ + +For the firmware upgrade to happen, the firmware has to be named specially: + +clip : m300a.bin +fuze : fuzea.bin +e200v2 : e200pa.bin +c200v2 : c200pa.bin +m200v4 : m200a.bin + + +Dual-Boot +--------- + + The purpose of this program is to provide dual-boot between the original +firmware and the new (rockbox) firmware. + + By default the player will boot into the new firmware. + + To boot into the Original Firmware, you need to press the Left key. + ***Note : on the c200v2 you need to press the Right key. + + The player will boot into the Original Firmware as well if it is powered up by +inserting an usb cable. + ***Note : on the m200v4 powering up by usb will cause booting into the new +firmware + +Hacking +------- + +See comments in mkamsboot.c and dualboot/dualboot.S for more information. diff --git a/rbutil/mkamsboot/bin2c.c b/rbutil/mkamsboot/bin2c.c deleted file mode 100644 index dce8013..0000000 --- a/rbutil/mkamsboot/bin2c.c +++ /dev/null @@ -1,134 +0,0 @@ -/*************************************************************************** - * __________ __ ___. - * 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 - -#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 int write_cfile(const unsigned char* buf, off_t len, const char* cname) -{ - char filename[256]; - FILE* fp; - int i; - - snprintf(filename,256,"%s.c",cname); - - fp = fopen(filename,"w+"); - if (fp == NULL) { - fprintf(stderr,"Couldn't open %s\n",filename); - return -1; - } - - fprintf(fp,"/* Generated by bin2c */\n\n"); - fprintf(fp,"unsigned char %s[%d] = {",cname,len); - - for (i=0;i ) \___| < | \_\ ( <_> > < < - * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ - * \/ \/ \/ \/ \/ - * $Id$ - * - * Copyright (C) 2008 Rafaël Carré - * - * 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 - -.set IRAM_SIZE, 0x50000 - -.set GPIOA, 0xC80B0000 -.set GPIOB, 0xC80C0000 -.set GPIOC, 0xC80D0000 -.set GPIOD, 0xC80E0000 -.set CGU_PERI, 0xC80F0014 - - -/* Vectors */ - ldr pc, =start -.word 0 -.word 0 -.word 0 -.word 0 -.word 0 -.word 0 -.word 0 - -/* These values are filled in by mkamsboot - don't move them from offset 0x20 */ -uclunpack_end: .word 0 /* End of the ucl_unpack function */ -uclunpack_size: .word 0 /* Size in bytes of the ucl_unpack function */ - -ucl_of_end: .word 0 /* End of the ucl-compressed OF image */ -ucl_of_size: .word 0 /* Size in bytes of the compressed OF image */ - -ucl_rb_end: .word 0 /* End of the ucl-compressed RB image */ -ucl_rb_size: .word 0 /* Size in bytes of the compressed RB image */ - - -start: - /* First copy the UCL unpack function to the end of RAM */ - ldr r0, uclunpack_end /* Source */ - ldr r1, uclunpack_size /* Source length */ - sub r2, r0, r1 /* Source start - 1*/ - - ldr r3, =(IRAM_SIZE-1) /* Destination end */ - -uclcopy: - ldrb r4, [r0], #-1 - strb r4, [r3], #-1 - cmp r2, r0 - bne uclcopy - - add r5, r3, #2 /* r5 is entry point of copy of uclunpack */ - /* function, plus one (for thumb mode */ - - /* enable gpio clock */ - ldr r0, =CGU_PERI - ldr r1, [r0] - orr r1, r1, #(1<<16) - str r1, [r0] - - -#ifndef SANSA_M200V4 /* this doesnt work for m200 */ - /* we check A3 unconditionally of the model because it seems to be */ - /* either hold, either usb on every model */ - /* TODO: make it USB on all AMS Sansas for consistency, USB is safer too */ - - ldr r0, =GPIOA - mov r1, #0 - str r1, [r0, #0x400] -#ifdef SANSA_C200V2 - ldr r1, [r0, #0x8] /* USB is A1 on C200 */ -#elif defined(SANSA_CLIP) - ldr r1, [r0, #0x100] /* USB is A6 on Clip */ -#else - ldr r1, [r0, #0x20] /* read pin A3 */ -#endif - cmp r1, #0 - bne boot_of -#endif - - /* here are model specific tests, for dual boot without a computer */ - -#ifdef SANSA_CLIP - /* LEFT button */ -.set row, (1<<5) /* enable output on C5 */ - ldr r0, =GPIOC - mov r1, #row - str r1, [r0, #0x400] - str r1, [r0, #(4*row)] - -.set col, (1<<0) /* read keyscan column B0 */ - ldr r0, =GPIOB - mov r1, #0 - str r1, [r0, #0x400] - ldr r1, [r0, #(4*col)] - - cmp r1, #0 - bne boot_of -#elif defined(SANSA_E200V2) || defined(SANSA_FUZE) - /* LEFT button */ - ldr r0, =GPIOC - mov r1, #0 - str r1, [r0, #0x400] - ldr r1, [r0, #0x20] /* read pin C3 */ - - cmp r1, #0 /* C3 = #0 means button pressed */ - beq boot_of -#elif defined(SANSA_C200V2) - /* check for RIGHT on C6, should maybe changed to LEFT as soon as it - * known in which pin that is in order for consistency */ - ldr r0, =GPIOC - mov r1, #0 - str r1, [r0, #0x400] /* set pin to output */ - - ldr r1, [r0, #256] /* 1<<(6+2) */ - cmp r1, #0 /* C6 low means button pressed */ - beq boot_of -#elif defined(SANSA_M200V4) - /* LEFT button */ -.set row, (1<<5) /* enable output on A5 */ - ldr r0, =GPIOA - mov r1, #row - str r1, [r0, #0x400] - str r1, [r0, #(4*row)] - -.set col, (1<<0) /* read keyscan column A0 */ - ldr r2, [r0, #(4*col)] - - /* check value read (1 means button pressed) */ - cmp r2, #0 - bne boot_of -#else - #error No target-specific key check defined! -#endif - - /* No button was held, so we boot rockbox */ - ldr r0, ucl_rb_end /* Address of compressed image */ - ldr r1, ucl_rb_size /* Compressed size */ - b decompress - -boot_of: - ldr r0, ucl_of_end /* Address of compressed image */ - ldr r1, ucl_of_size /* Compressed size */ - - -decompress: - /* At this point: */ - /* r5 = entry point (plus one for thumb) of uclunpack function */ - /* r3 = destination_end for copy of UCL image */ - /* r0 = source_end for UCL image to copy */ - /* r1 = size of UCL image to copy */ - - sub r4, r3, r1 /* r4 := destination_start - 1 */ - -fw_copy: - ldrb r2, [r0], #-1 - strb r2, [r3], #-1 - cmp r3, r4 /* Stop when we reached dest_start-1 */ - bne fw_copy - - /* Call the ucl decompress function, which will branch to 0x0 */ - /* on completion */ - add r0, r3, #1 /* r0 := Start of compressed image */ - /* r1 already contains compressed size */ - mov r2, #0 /* r2 := Destination for unpacking */ - bx r5 /* Branch to uclunpack, switching to thumb */ - - /* never reached */ diff --git a/rbutil/mkamsboot/extract_fw.c b/rbutil/mkamsboot/extract_fw.c deleted file mode 100644 index e91d1f8..0000000 --- a/rbutil/mkamsboot/extract_fw.c +++ /dev/null @@ -1,129 +0,0 @@ -/* - -extract_fw.c - extract the main firmware image from a Sansa V2 (AMS) firmware - file - -Copyright (C) Dave Chapman 2008 - -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., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA - -*/ - - -#include -#include -#include -#include -#include -#include -#include -#include - - -/* Win32 compatibility */ -#ifndef O_BINARY -#define O_BINARY 0 -#endif - - -static off_t filesize(int fd) { - struct stat buf; - - if (fstat(fd,&buf) < 0) { - perror("[ERR] Checking filesize of input file"); - return -1; - } else { - return(buf.st_size); - } -} - -static uint32_t get_uint32le(unsigned char* p) -{ - return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); -} - -void usage(void) -{ - printf("Usage: extract_fw \n"); - - exit(1); -} - -int main(int argc, char* argv[]) -{ - char *infile, *outfile; - int fdin, fdout; - off_t len; - uint32_t n; - unsigned char* buf; - uint32_t firmware_size; - - if(argc != 3) { - usage(); - } - - infile = argv[1]; - outfile = argv[2]; - - /* Open the firmware file */ - fdin = open(infile,O_RDONLY|O_BINARY); - - if (fdin < 0) { - fprintf(stderr,"[ERR] Could not open %s for reading\n",infile); - return 1; - } - - if ((len = filesize(fdin)) < 0) - return 1; - - /* We will need no more memory than the total size plus the bootloader size - padded to a boundary */ - if ((buf = malloc(len)) == NULL) { - fprintf(stderr,"[ERR] Could not allocate buffer for input file (%d bytes)\n",(int)len); - return 1; - } - - n = read(fdin, buf, len); - - if (n != (uint32_t)len) { - fprintf(stderr,"[ERR] Could not read firmware file\n"); - return 1; - } - - close(fdin); - - /* Get the firmware size */ - firmware_size = get_uint32le(&buf[0x0c]); - - 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); - return 1; - } - - n = write(fdout, buf + 0x400, firmware_size); - - if (n != (uint32_t)firmware_size) { - fprintf(stderr,"[ERR] Could not write firmware block\n"); - return 1; - } - - /* Clean up */ - close(fdout); - free(buf); - - return 0; -} diff --git a/rbutil/mkamsboot/md5.c b/rbutil/mkamsboot/md5.c index 6c5e812..530d8df 100644 --- a/rbutil/mkamsboot/md5.c +++ b/rbutil/mkamsboot/md5.c @@ -123,7 +123,7 @@ void md5_process( md5_context *ctx, uint8 data[64] ) 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 ); diff --git a/rbutil/mkamsboot/mkamsboot.c b/rbutil/mkamsboot/mkamsboot.c index 65b88af..0514dfa 100644 --- a/rbutil/mkamsboot/mkamsboot.c +++ b/rbutil/mkamsboot/mkamsboot.c @@ -24,7 +24,17 @@ /* -Insert a Rockbox bootloader into an AMS original firmware file. +Insert a Rockbox bootloader into a Sansa AMS original firmware file. + +Layout of a Sansa AMS original firmware file: + + ---------------------- 0x0 +| HEADER | +|----------------------| 0x400 +| FIRMWARE BLOCK | +|----------------------| 0x400 + firmware block size +| LIBRARIES/DATA | + ---------------------- END We replace the main firmware block (bytes 0x400..0x400+firmware_size) as follows: @@ -65,7 +75,7 @@ end of RAM. Then, depending on the detection of the dual-boot keypress, either the OF image or the Rockbox image is copied to the end of RAM (just before -the ucl unpack function) and uncompress it to the start of RAM. +the ucl unpack function) and uncompressed to the start of RAM. Finally, the ucl unpack function branches to address 0x0, passing execution to the uncompressed firmware. @@ -73,7 +83,6 @@ execution to the uncompressed firmware. */ - #include #include #include @@ -85,27 +94,22 @@ execution to the uncompressed firmware. #include -/* Headers for ARM code binaries */ -#include "nrv2e_d8.h" +#include "mkamsboot.h" + #include "md5.h" -#include "dualboot_clip.h" -#include "dualboot_e200v2.h" -#include "dualboot_fuze.h" -#include "dualboot_m200v4.h" -#include "dualboot_c200v2.h" +/* Header for ARM code binaries */ +#include "dualboot.h" /* Win32 compatibility */ #ifndef O_BINARY #define O_BINARY 0 #endif -#ifndef VERSION -#define VERSION "0.1" -#endif +#define VERSION "1.0" -enum -{ +/* Supported models */ +enum { MODEL_UNKNOWN = -1, MODEL_FUZE = 0, MODEL_CLIP, @@ -115,8 +119,8 @@ enum MODEL_C200V2, }; -static const char* model_names[] = -{ +/* Descriptive name of these models */ +static const char* model_names[] = { "Fuze", "Clip", "Clip V2", @@ -125,18 +129,18 @@ static const char* model_names[] = "c200 v2" }; -static const unsigned char* bootloaders[] = -{ +/* Dualboot functions for these models */ +static const unsigned char* bootloaders[] = { dualboot_fuze, dualboot_clip, - NULL, + NULL, /* Clipv2 not supported (yet) */ dualboot_e200v2, dualboot_m200v4, dualboot_c200v2, }; -static const int bootloader_sizes[] = -{ +/* Size of dualboot functions for these models */ +static const int bootloader_sizes[] = { sizeof(dualboot_fuze), sizeof(dualboot_clip), 0, @@ -147,8 +151,7 @@ static const int bootloader_sizes[] = /* Model names used in the Rockbox header in ".sansa" files - these match the -add parameter to the "scramble" tool */ -static const char* rb_model_names[] = -{ +static const char* rb_model_names[] = { "fuze", "clip", NULL, @@ -159,8 +162,7 @@ static const char* rb_model_names[] = /* Model numbers used to initialise the checksum in the Rockbox header in ".sansa" files - these are the same as MODEL_NUMBER in config-target.h */ -static const int rb_model_num[] = -{ +static const int rb_model_num[] = { 43, 40, 0, @@ -172,7 +174,7 @@ static const int rb_model_num[] = struct md5sums { int model; char *version; - int fw_version; + int fw_version; /* version 2 is used in Clipv2 and Fuzev2 firmwares */ char *md5; }; @@ -181,27 +183,29 @@ struct md5sums { static struct md5sums sansasums[] = { /* NOTE: Different regional versions of the firmware normally only differ in the filename - the md5sums are identical */ - { MODEL_E200V2, "3.01.11", 1, "e622ca8cb6df423f54b8b39628a1f0a3" }, - { MODEL_E200V2, "3.01.14", 1, "2c1d0383fc3584b2cc83ba8cc2243af6" }, - { MODEL_E200V2, "3.01.16", 1, "12563ad71b25a1034cf2092d1e0218c4" }, - - { MODEL_FUZE, "1.01.11", 1, "cac8ffa03c599330ac02c4d41de66166" }, - { MODEL_FUZE, "1.01.15", 1, "df0e2c1612727f722c19a3c764cff7f2" }, - { MODEL_FUZE, "1.01.22", 1, "5aff5486fe8dd64239cc71eac470af98" }, - { MODEL_FUZE, "1.02.26", 1, "7c632c479461c48c8833baed74eb5e4f" }, - - { MODEL_C200V2, "3.02.05", 1, "b6378ebd720b0ade3fad4dc7ab61c1a5" }, - - { MODEL_M200V4, "4.00.45", 1, "82e3194310d1514e3bbcd06e84c4add3" }, - { MODEL_M200V4, "4.01.08-A", 1, "fc9dd6116001b3e6a150b898f1b091f0" }, - { MODEL_M200V4, "4.01.08-E", 1, "d3fb7d8ec8624ee65bc99f8dab0e2369" }, - - { MODEL_CLIP, "1.01.17", 1, "12caad785d506219d73f538772afd99e" }, - { MODEL_CLIP, "1.01.18", 1, "d720b266bd5afa38a198986ef0508a45" }, - { MODEL_CLIP, "1.01.20", 1, "236d8f75189f468462c03f6d292cf2ac" }, - { MODEL_CLIP, "1.01.29", 1, "c12711342169c66e209540cd1f27cd26" }, - { MODEL_CLIP, "1.01.30", 1, "f2974d47c536549c9d8259170f1dbe4d" }, - { MODEL_CLIP, "1.01.32", 1, "d835d12342500732ffb9c4ee54abec15" }, + + /* model version fw_version md5 */ + { MODEL_E200V2, "3.01.11", 1, "e622ca8cb6df423f54b8b39628a1f0a3" }, + { MODEL_E200V2, "3.01.14", 1, "2c1d0383fc3584b2cc83ba8cc2243af6" }, + { MODEL_E200V2, "3.01.16", 1, "12563ad71b25a1034cf2092d1e0218c4" }, + + { MODEL_FUZE, "1.01.11", 1, "cac8ffa03c599330ac02c4d41de66166" }, + { MODEL_FUZE, "1.01.15", 1, "df0e2c1612727f722c19a3c764cff7f2" }, + { MODEL_FUZE, "1.01.22", 1, "5aff5486fe8dd64239cc71eac470af98" }, + { MODEL_FUZE, "1.02.26", 1, "7c632c479461c48c8833baed74eb5e4f" }, + + { MODEL_C200V2, "3.02.05", 1, "b6378ebd720b0ade3fad4dc7ab61c1a5" }, + + { MODEL_M200V4, "4.00.45", 1, "82e3194310d1514e3bbcd06e84c4add3" }, + { MODEL_M200V4, "4.01.08-A", 1, "fc9dd6116001b3e6a150b898f1b091f0" }, + { MODEL_M200V4, "4.01.08-E", 1, "d3fb7d8ec8624ee65bc99f8dab0e2369" }, + + { MODEL_CLIP, "1.01.17", 1, "12caad785d506219d73f538772afd99e" }, + { MODEL_CLIP, "1.01.18", 1, "d720b266bd5afa38a198986ef0508a45" }, + { MODEL_CLIP, "1.01.20", 1, "236d8f75189f468462c03f6d292cf2ac" }, + { MODEL_CLIP, "1.01.29", 1, "c12711342169c66e209540cd1f27cd26" }, + { MODEL_CLIP, "1.01.30", 1, "f2974d47c536549c9d8259170f1dbe4d" }, + { MODEL_CLIP, "1.01.32", 1, "d835d12342500732ffb9c4ee54abec15" }, }; #define NUM_MD5S (sizeof(sansasums)/sizeof(sansasums[0])) @@ -209,7 +213,7 @@ static struct md5sums sansasums[] = { static off_t filesize(int fd) { struct stat buf; - if (fstat(fd,&buf) < 0) { + if (fstat(fd, &buf) < 0) { perror("[ERR] Checking filesize of input file"); return -1; } else { @@ -217,30 +221,26 @@ static off_t filesize(int fd) { } } -static uint32_t get_uint32le(unsigned char* p) -{ +static uint32_t get_uint32le(unsigned char* p) { return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); } -static uint32_t get_uint32be(unsigned char* p) -{ +static uint32_t get_uint32be(unsigned char* p) { return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; } -static void put_uint32le(unsigned char* p, uint32_t x) -{ +static void put_uint32le(unsigned char* p, uint32_t x) { p[0] = x & 0xff; p[1] = (x >> 8) & 0xff; p[2] = (x >> 16) & 0xff; p[3] = (x >> 24) & 0xff; } -void calc_MD5(unsigned char* buf, int len, char *md5str) -{ +void calc_MD5(unsigned char* buf, int len, char *md5str) { int i; md5_context ctx; unsigned char md5sum[16]; - + md5_starts(&ctx); md5_update(&ctx, buf, len); md5_finish(&ctx, md5sum); @@ -249,9 +249,8 @@ void calc_MD5(unsigned char* buf, int len, char *md5str) sprintf(md5str + 2*i, "%02x", md5sum[i]); } - -static uint32_t calc_checksum(unsigned char* buf, uint32_t n) -{ +/* Calculate a simple checksum used in Sansa Original Firmwares */ +static uint32_t calc_checksum(unsigned char* buf, uint32_t n) { uint32_t sum = 0; uint32_t i; @@ -261,10 +260,8 @@ static uint32_t calc_checksum(unsigned char* buf, uint32_t n) return sum; } -static int get_model(int model_id) -{ - switch(model_id) - { +static int get_model(int model_id) { + switch(model_id) { case 0x1e: return MODEL_FUZE; case 0x22: @@ -282,9 +279,8 @@ static int get_model(int model_id) return MODEL_UNKNOWN; } - -static unsigned char* uclpack(unsigned char* inbuf, int insize, int* outsize) -{ +/* Compress using nrv2e algorithm : Thumb decompressor fits in 168 bytes ! */ +static unsigned char* uclpack(unsigned char* inbuf, int insize, int* outsize) { int maxsize; unsigned char* outbuf; int r; @@ -295,10 +291,9 @@ static unsigned char* uclpack(unsigned char* inbuf, int insize, int* outsize) /* Allocate some memory for the output buffer */ outbuf = malloc(maxsize); - if (outbuf == NULL) { + if (outbuf == NULL) return NULL; - } - + r = ucl_nrv2e_99_compress( (const ucl_bytep) inbuf, (ucl_uint) insize, @@ -306,8 +301,7 @@ static unsigned char* uclpack(unsigned char* inbuf, int insize, int* outsize) (ucl_uintp) outsize, 0, 10, NULL, NULL); - if (r != UCL_E_OK || *outsize > maxsize) - { + if (r != UCL_E_OK || *outsize > maxsize) { /* this should NEVER happen, and implies memory corruption */ fprintf(stderr, "internal error - compression failed: %d\n", r); free(outbuf); @@ -317,42 +311,102 @@ static unsigned char* uclpack(unsigned char* inbuf, int insize, int* outsize) return outbuf; } -static unsigned char* load_file(char* filename, off_t* bufsize) -{ +#define ERROR(format, ...) \ + do { \ + snprintf(errstr, errstrsize, format, __VA_ARGS__); \ + goto error; \ + } while(0) + +/* Loads a Sansa AMS Original Firmware file into memory */ +unsigned char* load_of_file( + char* filename, off_t* bufsize, char* md5sum, int* model, + int* fw_version, int* firmware_size, unsigned char** of_packed, + int* of_packedsize, char* errstr, int errstrsize +) { int fd; - unsigned char* buf; + unsigned char* buf =NULL; off_t n; + unsigned int i=0; + uint32_t checksum; + int model_id; + unsigned int last_word; fd = open(filename, O_RDONLY|O_BINARY); if (fd < 0) - { - fprintf(stderr,"[ERR] Could not open %s for reading\n",filename); - return NULL; - } + ERROR("[ERR] Could not open %s for reading\n", filename); *bufsize = filesize(fd); buf = malloc(*bufsize); - if (buf == NULL) { - fprintf(stderr,"[ERR] Could not allocate memory for %s\n",filename); - return NULL; - } + if (buf == NULL) + ERROR("[ERR] Could not allocate memory for %s\n", filename); n = read(fd, buf, *bufsize); - if (n != *bufsize) { - fprintf(stderr,"[ERR] Could not read file %s\n",filename); - return NULL; + if (n != *bufsize) + ERROR("[ERR] Could not read file %s\n", filename); + + /* check the file */ + + /* Calculate MD5 checksum of OF */ + calc_MD5(buf, *bufsize, md5sum); + + while ((i < NUM_MD5S) && (strcmp(sansasums[i].md5, md5sum) != 0)) + i++; + + if (i < NUM_MD5S) { + *model = sansasums[i].model; + *fw_version = sansasums[i].fw_version; + } else { + fprintf(stderr, "[WARN] ****** Original firmware unknown ******\n"); + if (get_uint32le(&buf[0x204])==0x0000f000) { + *fw_version = 2; + model_id = buf[0x219]; + } else { + *fw_version = 1; + model_id = buf[0x215]; + } + + *model = get_model(model_id); + + if (*model == MODEL_UNKNOWN) + ERROR("[ERR] Unknown firmware - model id 0x%02x\n", model_id); } + /* TODO: Do some more sanity checks on the OF image. Some images (like + m200v4) dont have a checksum at the end, only padding (0xdeadbeef). */ + last_word = *bufsize - 4; + checksum = get_uint32le(buf + last_word); + if (checksum != 0xefbeadde && checksum != calc_checksum(buf, last_word)) + ERROR("%s", "[ERR] Whole file checksum failed\n"); + + if (bootloaders[*model] == NULL) + ERROR("[ERR] Unsupported model - \"%s\"\n", model_names[*model]); + + + /* Get the firmware size */ + *firmware_size = get_uint32le(&buf[0x0c]); + + /* Compress the original firmware image */ + *of_packed = uclpack(buf + 0x400, *firmware_size, of_packedsize); + if (*of_packed == NULL) + ERROR("[ERR] Could not compress %s\n", filename); + return buf; -} +error: + free(buf); + return NULL; +} -static unsigned char* load_rockbox_file(char* filename, int model, off_t* bufsize) -{ +/* Loads a rockbox bootloader file into memory */ +unsigned char* load_rockbox_file( + char* filename, int model, int* bufsize, int* rb_packedsize, + char* errstr, int errstrsize +) { int fd; - unsigned char* buf; + unsigned char* buf = NULL; + unsigned char* packed = NULL; unsigned char header[8]; uint32_t sum; off_t n; @@ -360,39 +414,28 @@ static unsigned char* load_rockbox_file(char* filename, int model, off_t* bufsiz fd = open(filename, O_RDONLY|O_BINARY); if (fd < 0) - { - fprintf(stderr,"[ERR] Could not open %s for reading\n",filename); - return NULL; - } + ERROR("[ERR] Could not open %s for reading\n", filename); /* Read Rockbox header */ n = read(fd, header, sizeof(header)); - if (n != sizeof(header)) { - fprintf(stderr,"[ERR] Could not read file %s\n",filename); - return NULL; - } + if (n != sizeof(header)) + ERROR("[ERR] Could not read file %s\n", filename); /* Check for correct model string */ - if (memcmp(rb_model_names[model],header + 4,4)!=0) { - fprintf(stderr,"[ERR] Model name \"%s\" not found in %s\n", - rb_model_names[model],filename); - return NULL; - } + if (memcmp(rb_model_names[model], header + 4, 4)!=0) + ERROR("[ERR] Model name \"%s\" not found in %s\n", + rb_model_names[model], filename); *bufsize = filesize(fd) - sizeof(header); buf = malloc(*bufsize); - if (buf == NULL) { - fprintf(stderr,"[ERR] Could not allocate memory for %s\n",filename); - return NULL; - } + if (buf == NULL) + ERROR("[ERR] Could not allocate memory for %s\n", filename); n = read(fd, buf, *bufsize); - if (n != *bufsize) { - fprintf(stderr,"[ERR] Could not read file %s\n",filename); - return NULL; - } + if (n != *bufsize) + ERROR("[ERR] Could not read file %s\n", filename); /* Check checksum */ sum = rb_model_num[model]; @@ -401,176 +444,36 @@ static unsigned char* load_rockbox_file(char* filename, int model, off_t* bufsiz sum += buf[i]; } - if (sum != get_uint32be(header)) { - fprintf(stderr,"[ERR] Checksum mismatch in %s\n",filename); - return NULL; - } - return buf; -} + if (sum != get_uint32be(header)) + ERROR("[ERR] Checksum mismatch in %s\n", filename); + packed = uclpack(buf, *bufsize, rb_packedsize); + if(packed == NULL) + ERROR("[ERR] Could not compress %s\n", filename); -int main(int argc, char* argv[]) -{ - char *infile, *bootfile, *outfile; - int fdout; - off_t len; - uint32_t n; - unsigned char* buf; - int firmware_size; - off_t bootloader_size; - uint32_t sum,filesum; - uint8_t model_id; - int model; - uint32_t i; - unsigned char* of_packed; - int of_packedsize; - unsigned char* rb_unpacked; - unsigned char* rb_packed; - int rb_packedsize; - int fw_version; - int totalsize; - unsigned char* p; - uint32_t checksum; - char md5sum[33]; /* 32 hex digits, plus terminating zero */ - - fprintf(stderr,"mkamsboot v" VERSION " - (C) Dave Chapman and Rafaël Carré 2008\n"); - fprintf(stderr,"This is free software; see the source for copying conditions. There is NO\n"); - fprintf(stderr,"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"); - - if(argc != 4) { - printf("Usage: mkamsboot \n\n"); - return 1; - } - - infile = argv[1]; - bootfile = argv[2]; - outfile = argv[3]; + free(buf); + return packed; - /* Load original firmware file */ - buf = load_file(infile, &len); - - if (buf == NULL) { - fprintf(stderr,"[ERR] Could not load %s\n",infile); - return 1; - } - - /* Calculate MD5 checksum of OF */ - calc_MD5(buf, len, md5sum); - - fprintf(stderr,"[INFO] MD5 sum - %s\n",md5sum); - - i = 0; - while ((i < NUM_MD5S) && (strcmp(sansasums[i].md5, md5sum) != 0)) - i++; - - if (i < NUM_MD5S) { - model = sansasums[i].model; - fw_version = sansasums[i].fw_version; - fprintf(stderr,"[INFO] Original firmware MD5 checksum match - %s %s\n", - model_names[model], sansasums[i].version); - } else { - if (get_uint32le(&buf[0x204])==0x0000f000) { - fw_version = 2; - model_id = buf[0x219]; - } else { - fw_version = 1; - model_id = buf[0x215]; - } - - model = get_model(model_id); - -#if 0 - /* if you are a tester this info might help */ - fprintf(stderr,"[WARN] ****** Original firmware unknown ******\n"); - if (model == MODEL_UNKNOWN) { - fprintf(stderr,"[ERR] Unknown firmware - model id 0x%02x\n", - model_id); - free(buf); - return 1; - } -#else - /* else you don't want to brick your player */ - fprintf(stderr, "[ERR] Original firmware untested - aborting\n"); - free(buf); - return 1; -#endif - } - - - /* TODO: Do some more sanity checks on the OF image. Some images (like - m200v4) dont have a checksum at the end, only padding (0xdeadbeef). */ - checksum = get_uint32le(buf + len - 4); - if (checksum != 0xefbeadde && checksum != calc_checksum(buf, len - 4)) { - - fprintf(stderr,"[ERR] Whole file checksum failed - %s\n",infile); - free(buf); - return 1; - } - - if (bootloaders[model] == NULL) { - fprintf(stderr,"[ERR] Unsupported model - \"%s\"\n",model_names[model]); - free(buf); - return 1; - } - - /* Load bootloader file */ - rb_unpacked = load_rockbox_file(bootfile, model, &bootloader_size); - if (rb_unpacked == NULL) { - fprintf(stderr,"[ERR] Could not load %s\n",bootfile); - free(buf); - return 1; - } - - printf("[INFO] Patching %s firmware\n",model_names[model]); - - /* Get the firmware size */ - firmware_size = get_uint32le(&buf[0x0c]); - - /* Compress the original firmware image */ - of_packed = uclpack(buf + 0x400, firmware_size, &of_packedsize); - if (of_packed == NULL) { - fprintf(stderr,"[ERR] Could not compress original firmware\n"); - free(buf); - free(rb_unpacked); - return 1; - } - - rb_packed = uclpack(rb_unpacked, bootloader_size, &rb_packedsize); - if (rb_packed == NULL) { - fprintf(stderr,"[ERR] Could not compress %s\n",bootfile); - free(buf); - free(rb_unpacked); - free(of_packed); - return 1; - } - - /* We are finished with the unpacked version of the bootloader */ - free(rb_unpacked); - - 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[model]); - fprintf(stderr,"[INFO] UCL unpack function size: %d bytes\n",sizeof(nrv2e_d8)); - - totalsize = bootloader_sizes[model] + sizeof(nrv2e_d8) + of_packedsize + - rb_packedsize; +error: + free(buf); + return NULL; +} - fprintf(stderr,"[INFO] Total size of new image: %d bytes\n",totalsize); +#undef ERROR - if (totalsize > firmware_size) { - fprintf(stderr,"[ERR] No room to insert bootloader, aborting\n"); - free(buf); - free(rb_unpacked); - free(of_packed); - return 1; - } +/* Patches a Sansa AMS Original Firmware file */ +void patch_firmware( + int model, int fw_version, int firmware_size, unsigned char* buf, + int len, unsigned char* of_packed, int of_packedsize, + unsigned char* rb_packed, int rb_packedsize +) { + unsigned char *p; + uint32_t sum, filesum; + unsigned int i; /* Zero the original firmware area - not needed, but helps debugging */ memset(buf + 0x400, 0, firmware_size); - /* Insert dual-boot bootloader at offset 0 */ memcpy(buf + 0x400, bootloaders[model], bootloader_sizes[model]); @@ -589,7 +492,7 @@ int main(int argc, char* argv[]) p -= rb_packedsize; memcpy(p, rb_packed, rb_packedsize); - /* Write the locations of the various images to the variables at the + /* Write the locations of the various images to the variables at the start of the dualboot image - we save the location of the last byte in each image, along with the size in bytes */ @@ -602,12 +505,13 @@ int main(int argc, char* argv[]) put_uint32le(&buf[0x42c], of_packedsize); /* Compressed Rockbox image */ - put_uint32le(&buf[0x430], firmware_size - sizeof(nrv2e_d8) - of_packedsize - 1); + put_uint32le(&buf[0x430], firmware_size - sizeof(nrv2e_d8) - of_packedsize + - 1); put_uint32le(&buf[0x434], rb_packedsize); /* Update the firmware block checksum */ - sum = calc_checksum(buf + 0x400,firmware_size); + sum = calc_checksum(buf + 0x400, firmware_size); if (fw_version == 1) { put_uint32le(&buf[0x04], sum); @@ -629,29 +533,130 @@ int main(int argc, char* argv[]) filesum += get_uint32le(&buf[i]); put_uint32le(buf + len - 4, filesum); +} + +/* returns size of new firmware block */ +int total_size(int model, int rb_packedsize, int of_packedsize) { + return bootloader_sizes[model] + sizeof(nrv2e_d8) + 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; + int model; + unsigned char* of_packed; + int of_packedsize; + unsigned char* rb_packed; + int rb_packedsize; + int fw_version; + int totalsize; + char md5sum[33]; /* 32 hex digits, plus terminating zero */ + char errstr[200]; + fprintf(stderr, +"mkamsboot v" VERSION " - (C) Dave Chapman and Rafaël Carré 2008\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, md5sum, &model, &fw_version, + &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 - %s\n", + model_names[model]); + + + /* Load bootloader file */ + rb_packed = load_rockbox_file(bootfile, 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] Patching %s firmware\n", model_names[model]); + + 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[model]); + fprintf(stderr, "[INFO] UCL unpack function size: %d bytes\n", + sizeof(nrv2e_d8)); + + totalsize = total_size(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(model, fw_version, 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); + 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); + 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"); + fprintf(stderr, "[ERR] Could not write firmware file\n"); + free(buf); + free(of_packed); + free(rb_packed); return 1; } close(fdout); - - fprintf(stderr," *****************************************************************************\n"); - fprintf(stderr," *** THIS CODE IS UNTESTED - DO NOT USE IF YOU CAN NOT RECOVER YOUR DEVICE ***\n"); - fprintf(stderr," *****************************************************************************\n"); + free(buf); + free(of_packed); + free(rb_packed); return 0; - } +#endif diff --git a/rbutil/mkamsboot/nrv2e_d8.S b/rbutil/mkamsboot/nrv2e_d8.S deleted file mode 100644 index 89cb76d..0000000 --- a/rbutil/mkamsboot/nrv2e_d8.S +++ /dev/null @@ -1,194 +0,0 @@ -/* arm_nrv2e_d8.S -- ARM decompressor for NRV2E - - This file is part of the UPX executable compressor. - - Copyright (C) 1996-2008 Markus Franz Xaver Johannes Oberhumer - Copyright (C) 1996-2008 Laszlo Molnar - Copyright (C) 2000-2008 John F. Reiser - All Rights Reserved. - - UPX and the UCL library are free software; you can redistribute them - and/or modify them 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; see the file COPYING. - If not, write to the Free Software Foundation, Inc., - 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - - Markus F.X.J. Oberhumer Laszlo Molnar - - - John F. Reiser - -*/ -#define SAFE 0 /* 1 for src+dst bounds checking: cost 40 bytes */ - -#define src r0 -#define len r1 /* overlaps 'cnt' */ -#define dst r2 -#define tmp r3 -#define bits r4 -#define off r5 -#define wrnk r6 /* 0x500 M2_MAX_OFFSET before "wrinkle" */ -#define srclim r7 -#if 1==SAFE /*{*/ -#define dstlim r12 -#endif /*}*/ - -#define cnt r1 /* overlaps 'len' while reading an offset */ - -#if 1==SAFE /*{*/ -#define CHECK_SRC cmp src,srclim; bhs bad_src_n2e -#define CHECK_DST cmp dst,dstlim; bhs bad_dst_n2e -#else /*}{*/ -#define CHECK_SRC /*empty*/ -#define CHECK_DST /*empty*/ -#endif /*}*/ - -#if 0 /*{ DEBUG only: check newly-decompressed against original dst */ -#define CHECK_BYTE \ - push {wrnk}; \ - ldrb wrnk,[dst]; \ - cmp wrnk,tmp; beq 0f; bkpt; \ -0: pop {wrnk} -#else /*}{*/ -#define CHECK_BYTE /*empty*/ -#endif /*}*/ - -/* "mov lr,pc; bxx ..." implements conditional subroutine call */ -#define GETBIT add bits,bits; mov lr,pc; beq get1_n2e - -#define getnextb(reg) GETBIT; adc reg,reg -#define jnextb0 GETBIT; bcc -#define jnextb1 GETBIT; bcs - -#ifndef PURE_THUMB -ucl_nrv2e_decompress_8: .globl ucl_nrv2e_decompress_8 @ ARM mode - .type ucl_nrv2e_decompress_8, %function -/* error = (*)(char const *src, int len_src, char *dst, int *plen_dst) - Actual decompressed length is stored through plen_dst. - For SAFE mode: at call, *plen_dst must be allowed length of output buffer. -*/ - adr r12,1+.thumb_nrv2e_d8; bx r12 @ enter THUMB mode -#endif - .code 16 @ THUMB mode - .thumb_func - -.thumb_nrv2e_d8: -#if 0 - push {r2,r3, r4,r5,r6,r7, lr} -#define sp_DST0 0 /* stack offset of original dst */ -#endif - add srclim,len,src @ srclim= eof_src; -#if 1==SAFE /*{*/ - ldr tmp,[r3] @ len_dst - add tmp,dst - mov dstlim,tmp -#endif /*}*/ - mov bits,#1; neg off,bits @ off= -1 initial condition - lsl bits,#31 @ 1<<31: refill next time - mov wrnk,#5 - lsl wrnk,#8 @ 0x500 @ nrv2e M2_MAX_OFFSET - b top_n2e - -#if 1==SAFE /*{*/ -bad_dst_n2e: # return value will be 2 - add src,srclim,#1 -bad_src_n2e: # return value will be 1 - add src,#1 -#endif /*}*/ -eof_n2e: -#if 0 - pop {r3,r4} @ r3= orig_dst; r4= plen_dst - sub src,srclim @ 0 if actual src length equals expected length - sub dst,r3 @ actual dst length - str dst,[r4] - pop {r4,r5,r6,r7 /*,pc*/} - pop {r1}; bx r1 @ "pop {,pc}" fails return to ARM mode on ARMv4T -#else - mov r0, #0 - bx r0 /* Branch to 0x0, switch to ARM mode */ -#endif - -get1_n2e: @ In: Carry set [from adding 0x80000000 (1<<31) to itself] - ldrb bits,[src] @ zero-extend next byte - adc bits,bits @ double and insert CarryIn as low bit - CHECK_SRC - add src,#1 - lsl bits,#24 @ move to top byte, and set CarryOut from old bit 8 - mov pc,lr @ return, stay in current (THUMB) mode - -lit_n2e: - CHECK_SRC; ldrb tmp,[src]; add src,#1 - CHECK_BYTE - CHECK_DST; strb tmp,[dst]; add dst,#1 -top_n2e: - jnextb1 lit_n2e - mov cnt,#1; b getoff_n2e - -off_n2e: - sub cnt,#1 - getnextb(cnt) -getoff_n2e: - getnextb(cnt) - jnextb0 off_n2e - - sub tmp,cnt,#3 @ set Carry - mov len,#0 @ Carry unaffected - blo offprev_n2e @ cnt was 2; tests Carry only - lsl tmp,#8 - CHECK_SRC; ldrb off,[src]; add src,#1 @ low 7+1 bits - orr off,tmp - mvn off,off; beq eof_n2e @ off= ~off - asr off,#1; bcs lenlast_n2e - b lenmore_n2e - -offprev_n2e: - jnextb1 lenlast_n2e -lenmore_n2e: - mov len,#1 - jnextb1 lenlast_n2e -len_n2e: - getnextb(len) - jnextb0 len_n2e - add len,#6-2 - b gotlen_n2e - -lenlast_n2e: - getnextb(len) @ 0,1,2,3 - add len,#2 -gotlen_n2e: @ 'cmn': add the inputs, set condition codes, discard the sum - cmn wrnk,off; bcs near_n2e @ within M2_MAX_OFFSET - add len,#1 @ too far away, so minimum match length is 3 -near_n2e: -#if 1==SAFE /*{*/ - ldr tmp,[sp,#sp_DST0] - sub tmp,dst - sub tmp,off; bhi bad_dst_n2e @ reaching back too far - - add tmp,dst,cnt - cmp tmp,dstlim; bhi bad_dst_n2e @ too much output -#endif /*}*/ - ldrb tmp,[dst] @ force cacheline allocate -copy_n2e: - ldrb tmp,[dst,off] - CHECK_BYTE - strb tmp,[dst]; add dst,#1 - sub len,#1; bne copy_n2e - b top_n2e - -#ifndef PURE_THUMB - .size ucl_nrv2e_decompress_8, .-ucl_nrv2e_decompress_8 -#endif - -/* -vi:ts=8:et:nowrap - */ -