summaryrefslogtreecommitdiff
path: root/rbutil/mkamsboot/dualboot
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil/mkamsboot/dualboot')
-rw-r--r--rbutil/mkamsboot/dualboot/Makefile54
-rw-r--r--rbutil/mkamsboot/dualboot/bin2c.c131
-rw-r--r--rbutil/mkamsboot/dualboot/dualboot.S220
-rw-r--r--rbutil/mkamsboot/dualboot/nrv2e_d8.S193
4 files changed, 598 insertions, 0 deletions
diff --git a/rbutil/mkamsboot/dualboot/Makefile b/rbutil/mkamsboot/dualboot/Makefile
new file mode 100644
index 0000000000..06b90c7fb9
--- /dev/null
+++ b/rbutil/mkamsboot/dualboot/Makefile
@@ -0,0 +1,54 @@
1CC=gcc
2
3# Edit the following variables (plus copy/paste another set of rules) when
4# adding a new target. mkamsboot.c also needs to be edited to refer to these
5# new images.
6
7BOOTOBJS = nrv2e_d8.o dualboot_clip.o dualboot_e200v2.o dualboot_c200v2.o dualboot_m200v4.o dualboot_fuze.o dualboot_clipv2.o
8BOOTBINS = 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
9
10all: dualboot.h
11
12dualboot.h: $(BOOTBINS)
13
14# Dualboot bootloaders
15
16dualboot_clip.o: dualboot.S
17 arm-elf-gcc -DSANSA_CLIP -c -o dualboot_clip.o dualboot.S
18
19dualboot_fuze.o: dualboot.S
20 arm-elf-gcc -DSANSA_FUZE -c -o dualboot_fuze.o dualboot.S
21
22dualboot_e200v2.o: dualboot.S
23 arm-elf-gcc -DSANSA_E200V2 -c -o dualboot_e200v2.o dualboot.S
24
25dualboot_m200v4.o: dualboot.S
26 arm-elf-gcc -DSANSA_M200V4 -c -o dualboot_m200v4.o dualboot.S
27
28dualboot_c200v2.o: dualboot.S
29 arm-elf-gcc -DSANSA_C200V2 -c -o dualboot_c200v2.o dualboot.S
30
31dualboot_clipv2.o: dualboot.S
32 arm-elf-gcc -DSANSA_CLIPV2 -c -o dualboot_clipv2.o dualboot.S
33
34# Rules for the ucl unpack function
35nrv2e_d8.o: nrv2e_d8.S
36 arm-elf-gcc -DPURE_THUMB -c -o nrv2e_d8.o nrv2e_d8.S
37
38# Rules for the ARM code embedded in mkamsboot - assemble, link, then extract
39# the binary code and finally convert to .h for building in mkamsboot
40
41%.arm-elf: %.o
42 arm-elf-ld -e 0 -Ttext=0 -o $@ $<
43
44%.arm-bin: %.arm-elf
45 arm-elf-objcopy -O binary $< $@
46
47dualboot.c dualboot.h: $(BOOTBINS) bin2c
48 ./bin2c dualboot $(BOOTBINS)
49
50bin2c: bin2c.c
51 $(CC) -o bin2c bin2c.c
52
53clean:
54 rm -f *~ bin2c $(BOOTBINS) $(BOOTOBJS) dualboot.c dualboot.h
diff --git a/rbutil/mkamsboot/dualboot/bin2c.c b/rbutil/mkamsboot/dualboot/bin2c.c
new file mode 100644
index 0000000000..e8619eae87
--- /dev/null
+++ b/rbutil/mkamsboot/dualboot/bin2c.c
@@ -0,0 +1,131 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2007 Dave Chapman
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include <stdio.h>
23#include <string.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <unistd.h>
27#include <fcntl.h>
28#include <stdlib.h>
29
30#ifndef O_BINARY
31#define O_BINARY 0
32#endif
33
34static off_t filesize(int fd)
35{
36 struct stat buf;
37
38 fstat(fd,&buf);
39 return buf.st_size;
40}
41
42static void write_cfile(const unsigned char* buf, off_t len, FILE* fp, const char *name)
43{
44 int i;
45
46 fprintf(fp,"unsigned char %s[%ld] = {",name,len);
47
48 for (i=0;i<len;i++) {
49 if ((i % 16) == 0) {
50 fprintf(fp,"\n ");
51 }
52 if (i == (len-1)) {
53 fprintf(fp,"0x%02x",buf[i]);
54 } else if ((i % 16) == 15) {
55 fprintf(fp,"0x%02x,",buf[i]);
56 } else {
57 fprintf(fp,"0x%02x, ",buf[i]);
58 }
59 }
60 fprintf(fp,"\n};\n");
61}
62
63int main (int argc, char* argv[])
64{
65 char* cname;
66 int i;
67 FILE *cfile, *hfile;
68 char cfilename[256], hfilename[256];
69
70 if (argc < 3) {
71 fprintf(stderr,"Usage: bin2c cname file1 [file2 [file3 ...]]\n");
72 return 1;
73 }
74
75 cname=argv[1];
76
77 snprintf(cfilename,256,"%s.c",cname);
78 cfile = fopen(cfilename,"w+");
79 if (cfile == NULL) {
80 fprintf(stderr,"Couldn't open %s\n",cfilename);
81 return 2;
82 }
83
84 snprintf(hfilename,256,"%s.h",cname);
85 hfile = fopen(hfilename,"w+");
86 if (hfile == NULL) {
87 fprintf(stderr,"Couldn't open %s\n",hfilename);
88 fclose(cfile);
89 return 3;
90 }
91
92 fprintf(cfile,"/* Generated by bin2c */\n\n");
93 fprintf(hfile,"/* Generated by bin2c */\n\n");
94
95 for(i=0; i < argc - 2; i++) {
96 unsigned char* buf;
97 off_t len;
98 char *ext;
99 char *array = argv[2+i];
100
101 int fd = open(array,O_RDONLY|O_BINARY);
102 if (fd < 0) {
103 fprintf(stderr,"Can not open %s\n",argv[2+i]);
104 fclose(cfile);
105 fclose(hfile);
106 return 4;
107 }
108
109 len = filesize(fd);
110
111 buf = malloc(len);
112 if (read(fd,buf,len) < len) {
113 fprintf(stderr,"Short read, aborting\n");
114 return 5;
115 }
116
117 /* remove file extension */
118 ext = strchr (array, '.');
119 if (ext != NULL)
120 *ext = '\0';
121 write_cfile (buf, len, cfile, array);
122 fprintf(hfile,"extern unsigned char %s[%ld];\n",array,len);
123
124 close(fd);
125 }
126
127 fclose(cfile);
128 fclose(hfile);
129
130 return 0;
131}
diff --git a/rbutil/mkamsboot/dualboot/dualboot.S b/rbutil/mkamsboot/dualboot/dualboot.S
new file mode 100644
index 0000000000..5210ba5160
--- /dev/null
+++ b/rbutil/mkamsboot/dualboot/dualboot.S
@@ -0,0 +1,220 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2008 Rafaël Carré
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22.text
23
24 #if defined(SANSA_CLIPV2)
25.set RAM_SIZE, 0x100000 /* Use 1MB of SDRAM on v2 firmwares (bigger firmware) */
26#else
27.set RAM_SIZE, 0x50000 /* Use full IRAM on v1 firmwares */
28#endif
29
30/* AS3525 hardware registers */
31.set GPIOA, 0xC80B0000
32.set GPIOB, 0xC80C0000
33.set GPIOC, 0xC80D0000
34.set GPIOD, 0xC80E0000
35.set CGU_PERI, 0xC80F0014
36
37
38/* Vectors */
39
40 ldr pc, =start /* reset vector */
41 /* next vectors are unused */
42 .word 0
43 .word 0
44 .word 0
45 .word 0
46 .word 0
47 .word 0
48 .word 0
49
50/* These values are filled in by mkamsboot - don't move them from offset 0x20 */
51
52uclunpack_end: .word 0 /* End of the ucl_unpack function */
53uclunpack_size: .word 0 /* Size in bytes of the ucl_unpack function */
54
55ucl_of_end: .word 0 /* End of the ucl-compressed OF image */
56ucl_of_size: .word 0 /* Size in bytes of the compressed OF image */
57
58ucl_rb_end: .word 0 /* End of the ucl-compressed RB image */
59ucl_rb_size: .word 0 /* Size in bytes of the compressed RB image */
60
61
62start:
63 /* First copy the UCL unpack function to the end of RAM */
64 ldr r0, uclunpack_end /* Source */
65 ldr r1, uclunpack_size /* Source length */
66 sub r2, r0, r1 /* Source start - 1*/
67
68 ldr r3, =(RAM_SIZE-1) /* Destination end */
69
70uclcopy:
71 ldrb r4, [r0], #-1
72 strb r4, [r3], #-1
73 cmp r2, r0
74 bne uclcopy
75
76 add r5, r3, #2 /* r5 is entry point of copy of uclunpack */
77 /* function, plus one (for thumb mode */
78
79 /* enable gpio clock */
80 ldr r0, =CGU_PERI
81 ldr r1, [r0]
82 orr r1, r1, #(1<<16)
83 str r1, [r0]
84
85
86/* TODO : M200V4 */
87#if defined(SANSA_C200V2)
88#define USB_PIN 1
89#elif defined(SANSA_CLIP) || defined(SANSA_CLIPV2)
90#define USB_PIN 6
91#elif defined(SANSA_FUZE) || defined(SANSA_E200V2)
92#define USB_PIN 3
93#endif
94
95#ifdef USB_PIN /* TODO : remove this check when we'll have an USB driver */
96 ldr r0, =GPIOA
97 mov r1, #0
98 str r1, [r0, #0x400]
99 ldr r1, [r0, #(4*(1<<USB_PIN))]
100 cmp r1, #0
101 bne boot_of
102#endif
103
104 /* Here are model specific tests, for dual boot without a computer */
105 /* All models use left button */
106 /* /!\ Right button for c200v2 (left button is unkwown) */
107
108#ifdef SANSA_CLIP
109.set row, (1<<5) /* enable output on C5 */
110.set col, (1<<0) /* read keyscan column B0 */
111
112 ldr r0, =GPIOC
113 mov r1, #row
114 str r1, [r0, #0x400]
115 str r1, [r0, #(4*row)]
116
117 ldr r0, =GPIOB
118 mov r1, #0
119 str r1, [r0, #0x400]
120 ldr r1, [r0, #(4*col)]
121
122 cmp r1, #0
123 bne boot_of
124#elif defined(SANSA_CLIPV2)
125.set row, (1<<4) /* enable output on D4 */
126.set col, (1<<0) /* read keyscan column D0 */
127
128 ldr r0, =GPIOD
129 mov r1, #((1<<5)|(1<<4)|(1<<3)) /* all rows as output */
130 str r1, [r0, #0x400]
131
132 /* all rows high */
133 mov r1, #(1<<3)
134 str r1, [r0, #(4*(1<<3))]
135 mov r1, #(1<<4)
136 str r1, [r0, #(4*(1<<4))]
137 mov r1, #(1<<5)
138 str r1, [r0, #(4*(1<<5))]
139
140 mov r1, #0 /* button row low */
141 str r1, [r0, #(4*row)]
142
143 mov r1, #5 /* small delay */
1441: subs r1, r1, #1
145 bne 1b
146
147 ldr r1, [r0, #(4*col)]
148
149 cmp r1, #0
150 beq boot_of
151#elif defined(SANSA_E200V2) || defined(SANSA_FUZE)
152 ldr r0, =GPIOC
153 mov r1, #0
154 str r1, [r0, #0x400]
155 ldr r1, [r0, #0x20] /* read pin C3 */
156
157 cmp r1, #0 /* C3 = #0 means button pressed */
158 beq boot_of
159#elif defined(SANSA_C200V2)
160 /* check for RIGHT on C6, should changed to LEFT as soon as it
161 * known in which pin that is in order for consistency */
162 ldr r0, =GPIOC
163 mov r1, #0
164 str r1, [r0, #0x400] /* set pin to output */
165
166 ldr r1, [r0, #256] /* 1<<(6+2) */
167 cmp r1, #0 /* C6 low means button pressed */
168 beq boot_of
169#elif defined(SANSA_M200V4)
170.set row, (1<<5) /* enable output on A5 */
171.set col, (1<<0) /* read keyscan column A0 */
172
173 ldr r0, =GPIOA
174 mov r1, #row
175 str r1, [r0, #0x400]
176 str r1, [r0, #(4*row)]
177
178 ldr r2, [r0, #(4*col)]
179
180 /* check value read (1 means button pressed) */
181 cmp r2, #0
182 bne boot_of
183#else
184 #error No target-specific key check defined!
185#endif
186
187
188 /* The dualboot button was not held, so we boot rockbox */
189 ldr r0, ucl_rb_end /* Address of compressed image */
190 ldr r1, ucl_rb_size /* Compressed size */
191 b decompress
192
193boot_of:
194 ldr r0, ucl_of_end /* Address of compressed image */
195 ldr r1, ucl_of_size /* Compressed size */
196
197
198decompress:
199 /* At this point: */
200 /* r5 = entry point (plus one for thumb) of uclunpack function */
201 /* r3 = destination_end for copy of UCL image */
202 /* r0 = source_end for UCL image to copy */
203 /* r1 = size of UCL image to copy */
204
205 sub r4, r3, r1 /* r4 := destination_start - 1 */
206
207fw_copy:
208 ldrb r2, [r0], #-1
209 strb r2, [r3], #-1
210 cmp r3, r4 /* Stop when we reached dest_start-1 */
211 bne fw_copy
212
213 /* Call the ucl decompress function, which will branch to 0x0 */
214 /* on completion */
215 add r0, r3, #1 /* r0 := Start of compressed image */
216 /* r1 already contains compressed size */
217 mov r2, #0 /* r2 := Destination for unpacking */
218 bx r5 /* Branch to uclunpack, switching to thumb */
219
220 /* never reached : uclunpack will branch to the reset vector (0x0) */
diff --git a/rbutil/mkamsboot/dualboot/nrv2e_d8.S b/rbutil/mkamsboot/dualboot/nrv2e_d8.S
new file mode 100644
index 0000000000..b112ab2936
--- /dev/null
+++ b/rbutil/mkamsboot/dualboot/nrv2e_d8.S
@@ -0,0 +1,193 @@
1/* arm_nrv2e_d8.S -- ARM decompressor for NRV2E
2
3 This file is part of the UPX executable compressor.
4
5 Copyright (C) 1996-2008 Markus Franz Xaver Johannes Oberhumer
6 Copyright (C) 1996-2008 Laszlo Molnar
7 Copyright (C) 2000-2008 John F. Reiser
8 All Rights Reserved.
9
10 UPX and the UCL library are free software; you can redistribute them
11 and/or modify them under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; see the file COPYING.
22 If not, write to the Free Software Foundation, Inc.,
23 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25 Markus F.X.J. Oberhumer Laszlo Molnar
26 <markus@oberhumer.com> <ml1050@users.sourceforge.net>
27
28 John F. Reiser
29 <jreiser@users.sourceforge.net>
30*/
31#define SAFE 0 /* 1 for src+dst bounds checking: cost 40 bytes */
32
33#define src r0
34#define len r1 /* overlaps 'cnt' */
35#define dst r2
36#define tmp r3
37#define bits r4
38#define off r5
39#define wrnk r6 /* 0x500 M2_MAX_OFFSET before "wrinkle" */
40#define srclim r7
41#if 1==SAFE /*{*/
42#define dstlim r12
43#endif /*}*/
44
45#define cnt r1 /* overlaps 'len' while reading an offset */
46
47#if 1==SAFE /*{*/
48#define CHECK_SRC cmp src,srclim; bhs bad_src_n2e
49#define CHECK_DST cmp dst,dstlim; bhs bad_dst_n2e
50#else /*}{*/
51#define CHECK_SRC /*empty*/
52#define CHECK_DST /*empty*/
53#endif /*}*/
54
55#if 0 /*{ DEBUG only: check newly-decompressed against original dst */
56#define CHECK_BYTE \
57 push {wrnk}; \
58 ldrb wrnk,[dst]; \
59 cmp wrnk,tmp; beq 0f; bkpt; \
600: pop {wrnk}
61#else /*}{*/
62#define CHECK_BYTE /*empty*/
63#endif /*}*/
64
65/* "mov lr,pc; bxx ..." implements conditional subroutine call */
66#define GETBIT add bits,bits; mov lr,pc; beq get1_n2e
67
68#define getnextb(reg) GETBIT; adc reg,reg
69#define jnextb0 GETBIT; bcc
70#define jnextb1 GETBIT; bcs
71
72#ifndef PURE_THUMB
73ucl_nrv2e_decompress_8: .globl ucl_nrv2e_decompress_8 @ ARM mode
74 .type ucl_nrv2e_decompress_8, %function
75/* error = (*)(char const *src, int len_src, char *dst, int *plen_dst)
76 Actual decompressed length is stored through plen_dst.
77 For SAFE mode: at call, *plen_dst must be allowed length of output buffer.
78*/
79 adr r12,1+.thumb_nrv2e_d8; bx r12 @ enter THUMB mode
80#endif
81 .code 16 @ THUMB mode
82 .thumb_func
83
84.thumb_nrv2e_d8:
85#if 0
86 push {r2,r3, r4,r5,r6,r7, lr}
87#define sp_DST0 0 /* stack offset of original dst */
88#endif
89 add srclim,len,src @ srclim= eof_src;
90#if 1==SAFE /*{*/
91 ldr tmp,[r3] @ len_dst
92 add tmp,dst
93 mov dstlim,tmp
94#endif /*}*/
95 mov bits,#1; neg off,bits @ off= -1 initial condition
96 lsl bits,#31 @ 1<<31: refill next time
97 mov wrnk,#5
98 lsl wrnk,#8 @ 0x500 @ nrv2e M2_MAX_OFFSET
99 b top_n2e
100
101#if 1==SAFE /*{*/
102bad_dst_n2e: # return value will be 2
103 add src,srclim,#1
104bad_src_n2e: # return value will be 1
105 add src,#1
106#endif /*}*/
107eof_n2e:
108#if 0
109 pop {r3,r4} @ r3= orig_dst; r4= plen_dst
110 sub src,srclim @ 0 if actual src length equals expected length
111 sub dst,r3 @ actual dst length
112 str dst,[r4]
113 pop {r4,r5,r6,r7 /*,pc*/}
114 pop {r1}; bx r1 @ "pop {,pc}" fails return to ARM mode on ARMv4T
115#else
116 mov r0, #0
117 bx r0 /* Branch to 0x0, switch to ARM mode */
118#endif
119
120get1_n2e: @ In: Carry set [from adding 0x80000000 (1<<31) to itself]
121 ldrb bits,[src] @ zero-extend next byte
122 adc bits,bits @ double and insert CarryIn as low bit
123 CHECK_SRC
124 add src,#1
125 lsl bits,#24 @ move to top byte, and set CarryOut from old bit 8
126 mov pc,lr @ return, stay in current (THUMB) mode
127
128lit_n2e:
129 CHECK_SRC; ldrb tmp,[src]; add src,#1
130 CHECK_BYTE
131 CHECK_DST; strb tmp,[dst]; add dst,#1
132top_n2e:
133 jnextb1 lit_n2e
134 mov cnt,#1; b getoff_n2e
135
136off_n2e:
137 sub cnt,#1
138 getnextb(cnt)
139getoff_n2e:
140 getnextb(cnt)
141 jnextb0 off_n2e
142
143 sub tmp,cnt,#3 @ set Carry
144 mov len,#0 @ Carry unaffected
145 blo offprev_n2e @ cnt was 2; tests Carry only
146 lsl tmp,#8
147 CHECK_SRC; ldrb off,[src]; add src,#1 @ low 7+1 bits
148 orr off,tmp
149 mvn off,off; beq eof_n2e @ off= ~off
150 asr off,#1; bcs lenlast_n2e
151 b lenmore_n2e
152
153offprev_n2e:
154 jnextb1 lenlast_n2e
155lenmore_n2e:
156 mov len,#1
157 jnextb1 lenlast_n2e
158len_n2e:
159 getnextb(len)
160 jnextb0 len_n2e
161 add len,#6-2
162 b gotlen_n2e
163
164lenlast_n2e:
165 getnextb(len) @ 0,1,2,3
166 add len,#2
167gotlen_n2e: @ 'cmn': add the inputs, set condition codes, discard the sum
168 cmn wrnk,off; bcs near_n2e @ within M2_MAX_OFFSET
169 add len,#1 @ too far away, so minimum match length is 3
170near_n2e:
171#if 1==SAFE /*{*/
172 ldr tmp,[sp,#sp_DST0]
173 sub tmp,dst
174 sub tmp,off; bhi bad_dst_n2e @ reaching back too far
175
176 add tmp,dst,cnt
177 cmp tmp,dstlim; bhi bad_dst_n2e @ too much output
178#endif /*}*/
179 ldrb tmp,[dst] @ force cacheline allocate
180copy_n2e:
181 ldrb tmp,[dst,off]
182 CHECK_BYTE
183 strb tmp,[dst]; add dst,#1
184 sub len,#1; bne copy_n2e
185 b top_n2e
186
187#ifndef PURE_THUMB
188 .size ucl_nrv2e_decompress_8, .-ucl_nrv2e_decompress_8
189#endif
190
191/*
192vi:ts=8:et:nowrap
193 */