1
0
Fork 0
forked from len0rd/rockbox

FS#10253 : mkamsboot v1.0

- Bump version to 1.0
- Add Clipv2 target
- Make mkamsboot work as a library (work by domonoky : FS#10185, with a few modifications by me)
  . Use a macro with variadic arguments for error cases in functions which might error.
  . Add detailed descriptions to functions exported by the library (in the header file)
- modify bin2c.c to produce only one pair of .c/.h files with several files embedded in it
- move files needing to be built by an ARM cross compiler into dualboot/
- commit produced .c/.h files (containing nrv2e_d8.S and dualboot.S built for Clip, Fuze, e200v2, c200v2, m200v4, Clipv2)
- Write a real README file
- cosmetics: indent dualboot.S properly, remove trailing spaces, limit lines to 80 characters
- comments: add/correct comments in dualboot.S and mkamsboot.c
- move back extract_fw.c to utils/AMS/hacking

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21118 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Rafaël Carré 2009-05-28 18:27:08 +00:00
parent bebc8587cf
commit 96165abec2
14 changed files with 1054 additions and 685 deletions

View file

@ -0,0 +1,54 @@
CC=gcc
# 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.
BOOTOBJS = nrv2e_d8.o dualboot_clip.o dualboot_e200v2.o dualboot_c200v2.o dualboot_m200v4.o dualboot_fuze.o dualboot_clipv2.o
BOOTBINS = nrv2e_d8.arm-bin dualboot_clip.arm-bin dualboot_e200v2.arm-bin dualboot_c200v2.arm-bin dualboot_m200v4.arm-bin dualboot_fuze.arm-bin dualboot_clipv2.arm-bin
all: dualboot.h
dualboot.h: $(BOOTBINS)
# Dualboot bootloaders
dualboot_clip.o: dualboot.S
arm-elf-gcc -DSANSA_CLIP -c -o dualboot_clip.o dualboot.S
dualboot_fuze.o: dualboot.S
arm-elf-gcc -DSANSA_FUZE -c -o dualboot_fuze.o dualboot.S
dualboot_e200v2.o: dualboot.S
arm-elf-gcc -DSANSA_E200V2 -c -o dualboot_e200v2.o dualboot.S
dualboot_m200v4.o: dualboot.S
arm-elf-gcc -DSANSA_M200V4 -c -o dualboot_m200v4.o dualboot.S
dualboot_c200v2.o: dualboot.S
arm-elf-gcc -DSANSA_C200V2 -c -o dualboot_c200v2.o dualboot.S
dualboot_clipv2.o: dualboot.S
arm-elf-gcc -DSANSA_CLIPV2 -c -o dualboot_clipv2.o dualboot.S
# Rules for the ucl unpack function
nrv2e_d8.o: nrv2e_d8.S
arm-elf-gcc -DPURE_THUMB -c -o nrv2e_d8.o nrv2e_d8.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
arm-elf-ld -e 0 -Ttext=0 -o $@ $<
%.arm-bin: %.arm-elf
arm-elf-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) dualboot.c dualboot.h

View file

@ -0,0 +1,131 @@
/***************************************************************************
* __________ __ ___.
* 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 <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#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<len;i++) {
if ((i % 16) == 0) {
fprintf(fp,"\n ");
}
if (i == (len-1)) {
fprintf(fp,"0x%02x",buf[i]);
} else if ((i % 16) == 15) {
fprintf(fp,"0x%02x,",buf[i]);
} else {
fprintf(fp,"0x%02x, ",buf[i]);
}
}
fprintf(fp,"\n};\n");
}
int main (int argc, char* argv[])
{
char* cname;
int i;
FILE *cfile, *hfile;
char cfilename[256], hfilename[256];
if (argc < 3) {
fprintf(stderr,"Usage: bin2c cname file1 [file2 [file3 ...]]\n");
return 1;
}
cname=argv[1];
snprintf(cfilename,256,"%s.c",cname);
cfile = fopen(cfilename,"w+");
if (cfile == NULL) {
fprintf(stderr,"Couldn't open %s\n",cfilename);
return 2;
}
snprintf(hfilename,256,"%s.h",cname);
hfile = fopen(hfilename,"w+");
if (hfile == NULL) {
fprintf(stderr,"Couldn't open %s\n",hfilename);
fclose(cfile);
return 3;
}
fprintf(cfile,"/* Generated by bin2c */\n\n");
fprintf(hfile,"/* Generated by bin2c */\n\n");
for(i=0; i < argc - 2; i++) {
unsigned char* buf;
off_t len;
char *ext;
char *array = argv[2+i];
int fd = open(array,O_RDONLY|O_BINARY);
if (fd < 0) {
fprintf(stderr,"Can not open %s\n",argv[2+i]);
fclose(cfile);
fclose(hfile);
return 4;
}
len = filesize(fd);
buf = malloc(len);
if (read(fd,buf,len) < len) {
fprintf(stderr,"Short read, aborting\n");
return 5;
}
/* 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;
}

View file

@ -0,0 +1,220 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* 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
#if defined(SANSA_CLIPV2)
.set RAM_SIZE, 0x100000 /* Use 1MB of SDRAM on v2 firmwares (bigger firmware) */
#else
.set RAM_SIZE, 0x50000 /* Use full IRAM on v1 firmwares */
#endif
/* AS3525 hardware registers */
.set GPIOA, 0xC80B0000
.set GPIOB, 0xC80C0000
.set GPIOC, 0xC80D0000
.set GPIOD, 0xC80E0000
.set CGU_PERI, 0xC80F0014
/* Vectors */
ldr pc, =start /* reset vector */
/* next vectors are unused */
.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, =(RAM_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]
/* TODO : M200V4 */
#if defined(SANSA_C200V2)
#define USB_PIN 1
#elif defined(SANSA_CLIP) || defined(SANSA_CLIPV2)
#define USB_PIN 6
#elif defined(SANSA_FUZE) || defined(SANSA_E200V2)
#define USB_PIN 3
#endif
#ifdef USB_PIN /* TODO : remove this check when we'll have an USB driver */
ldr r0, =GPIOA
mov r1, #0
str r1, [r0, #0x400]
ldr r1, [r0, #(4*(1<<USB_PIN))]
cmp r1, #0
bne boot_of
#endif
/* Here are model specific tests, for dual boot without a computer */
/* All models use left button */
/* /!\ Right button for c200v2 (left button is unkwown) */
#ifdef SANSA_CLIP
.set row, (1<<5) /* enable output on C5 */
.set col, (1<<0) /* read keyscan column B0 */
ldr r0, =GPIOC
mov r1, #row
str r1, [r0, #0x400]
str r1, [r0, #(4*row)]
ldr r0, =GPIOB
mov r1, #0
str r1, [r0, #0x400]
ldr r1, [r0, #(4*col)]
cmp r1, #0
bne boot_of
#elif defined(SANSA_CLIPV2)
.set row, (1<<4) /* enable output on D4 */
.set col, (1<<0) /* read keyscan column D0 */
ldr r0, =GPIOD
mov r1, #((1<<5)|(1<<4)|(1<<3)) /* all rows as output */
str r1, [r0, #0x400]
/* all rows high */
mov r1, #(1<<3)
str r1, [r0, #(4*(1<<3))]
mov r1, #(1<<4)
str r1, [r0, #(4*(1<<4))]
mov r1, #(1<<5)
str r1, [r0, #(4*(1<<5))]
mov r1, #0 /* button row low */
str r1, [r0, #(4*row)]
mov r1, #5 /* small delay */
1: subs r1, r1, #1
bne 1b
ldr r1, [r0, #(4*col)]
cmp r1, #0
beq boot_of
#elif defined(SANSA_E200V2) || defined(SANSA_FUZE)
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 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)
.set row, (1<<5) /* enable output on A5 */
.set col, (1<<0) /* read keyscan column A0 */
ldr r0, =GPIOA
mov r1, #row
str r1, [r0, #0x400]
str r1, [r0, #(4*row)]
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
/* The dualboot button was not 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 : uclunpack will branch to the reset vector (0x0) */

View file

@ -0,0 +1,193 @@
/* 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
<markus@oberhumer.com> <ml1050@users.sourceforge.net>
John F. Reiser
<jreiser@users.sourceforge.net>
*/
#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
*/