1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2003 by Jörg Hohensohn
12 * Second-level bootloader, with dual-boot feature by holding F1/Menu
13 * This is the image being descrambled and executed by the boot ROM.
14 * It's task is to copy Rockbox from Flash to DRAM.
15 * The image(s) in flash may optionally be compressed with UCL 2e
17 * All files in this archive are subject to the GNU General Public License.
18 * See the file COPYING in the source tree root for full license agreement.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
26 #include "bootloader.h"
30 /* start with the vector table */
31 UINT32 vectors
[] __attribute__ ((section (".vectors"))) =
33 (UINT32
)_main
, /* entry point, the copy routine */
34 (UINT32
)(end_stack
- 1), /* initial stack pointer */
35 FLASH_BASE
+ 0x200, /* source of image in flash */
36 (UINT32
)total_size
, /* size of image */
37 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,
38 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,
39 0x03020080 /* mask and version (just as a suggestion) */
42 /* our binary has to start with a vector to the entry point */
43 tpMain start_vector
[] __attribute__ ((section (".startvector"))) = {main
};
46 #ifdef NO_ROM /* some code which is only needed for the romless variant */
53 asm volatile ("ldc %0,sr" : : "r"(0xF0)); // disable interrupts
54 asm volatile ("mov.l @%0,r15" : : "r"(4)); // load stack
55 asm volatile ("ldc %0,vbr" : : "r"(0)); // load vector base
57 /* copy everything to IRAM and continue there */
58 pSrc
= begin_iramcopy
;
60 pEnd
= pDest
+ (begin_stack
- begin_text
);
68 main(); /* jump to the real main() */
74 /* inits from the boot ROM, whether they make sense or not */
75 PBDR
&= 0xFFBF; /* LED off (0x131E) */
76 PBCR2
= 0; /* all GPIO */
77 PBIOR
|= 0x0040; /* LED output */
78 PBIOR
&= 0xFFF1; /* LCD lines input */
80 /* init DRAM like the boot ROM does */
91 #endif /* #ifdef NO_ROM */
98 PlatformInit(); /* model-specific inits */
100 nButton
= ButtonPressed();
103 { /* F3 means start monitor */
109 pImage
= GetStartImage(nButton
); /* which image */
110 DecompressStart(pImage
); /* move into place and start it */
113 return 0; /* I guess we won't return ;-) */
117 /* init code that is specific to certain platform */
118 void PlatformInit(void)
121 BootInit(); /* if not started by boot ROM, we need to init what it did */
124 #if defined PLATFORM_PLAYER
125 BRR1
= 0x19; /* 14400 Baud for monitor */
126 PBDRL
|= 0x10; /* set PB4 to 1 to power the hd early (and prepare for
127 * probing in case the charger is connected) */
128 PBIORL
|= 0x10; /* make PB4 an output */
129 PACR2
&= 0xFFFC; /* GPIO for PA0 (charger detection, input by default) */
130 if (!(PADRL
& 0x01)) /* charger plugged? */
131 { /* we need to probe whether the box is able to control hd power */
134 PBIORL
&= ~0x10; /* set PB4 to input */
135 /* wait whether it goes low, max. ~1 ms */
136 for (i
= 0; (PBDRL
& 0x10) && i
< 1000; i
++);
138 if (~(PBDRL
& 0x10)) /* pulled low -> power controllable */
139 PBDRL
&= 0x10; /* set PB4 low */
140 else /* still floating high -> not controllable */
141 PBDRL
|= 0x10; /* set PB4 high */
142 PBIORL
|= 0x10; /* ..and output again */
144 #elif defined PLATFORM_RECORDER
145 BRR1
= 0x02; /* 115200 Baud for monitor */
146 if (ReadADC(7) > 0x100) /* charger plugged? */
147 { /* switch off the HD, else a flat battery may not start */
148 PACR2
&= 0xFBFF; /* GPIO for PA5 */
149 PAIOR
|= 0x0020; /* make PA5 an output (low by default) */
151 #elif defined PLATFORM_FM
152 BRR1
= 0x02; /* 115200 Baud for monitor */
153 PBDR
|= 0x0020; /* set PB5 to keep power (fixes the ON-holding problem) */
154 PBIOR
|= 0x0020; /* make PB5 an output */
155 if (ReadADC(0) < 0x1FF) /* charger plugged? */
156 { /* switch off the HD, else a flat battery may not start */
157 PACR2
&= 0xFBFF; /* GPIO for PA5 */
158 PAIOR
|= 0x0020; /* make PA5 an output (low by default) */
160 #elif defined PLATFORM_ONDIO
161 BRR1
= 0x19; /* 14400 Baud for monitor */
162 PBDR
|= 0x0020; /* set PB5 to keep power (fixes the ON-holding problem) */
163 PBIOR
|= 0x0020; /* make PB5 an output */
166 /* platform-independent inits */
167 DCR
|= 0x1000; /* enable burst mode on DRAM */
168 BCR
|= 0x2000; /* activate Warp mode (simultaneous internal and external
173 /* Thinned out version of the UCL 2e decompression sourcecode
174 * Original (C) Markus F.X.J Oberhumer under GNU GPL license */
175 #define GETBIT(bb, src, ilen) \
176 (((bb = bb & 0x7f ? bb*2 : ((unsigned)src[ilen++]*2+1)) >> 8) & 1)
178 int ucl_nrv2e_decompress_8(
179 const UINT8
*src
, UINT8
*dst
, UINT32
* dst_len
)
182 unsigned ilen
= 0, olen
= 0, last_m_off
= 1;
186 unsigned m_off
, m_len
;
188 while (GETBIT(bb
,src
,ilen
))
190 dst
[olen
++] = src
[ilen
++];
195 m_off
= m_off
*2 + GETBIT(bb
,src
,ilen
);
196 if (GETBIT(bb
,src
,ilen
)) break;
197 m_off
= (m_off
-1)*2 + GETBIT(bb
,src
,ilen
);
202 m_len
= GETBIT(bb
,src
,ilen
);
206 m_off
= (m_off
-3)*256 + src
[ilen
++];
207 if (m_off
== 0xffffffff)
209 m_len
= (m_off
^ 0xffffffff) & 1;
211 last_m_off
= ++m_off
;
214 m_len
= 1 + GETBIT(bb
,src
,ilen
);
215 else if (GETBIT(bb
,src
,ilen
))
216 m_len
= 3 + GETBIT(bb
,src
,ilen
);
221 m_len
= m_len
*2 + GETBIT(bb
,src
,ilen
);
222 } while (!GETBIT(bb
,src
,ilen
));
225 m_len
+= (m_off
> 0x500);
228 m_pos
= dst
+ olen
- m_off
;
229 dst
[olen
++] = *m_pos
++;
230 do dst
[olen
++] = *m_pos
++; while (--m_len
> 0);
239 /* move the image into place and start it */
240 void DecompressStart(tImage
* pImage
)
245 pSrc
= pImage
->image
;
246 pDest
= pImage
->pDestination
;
248 if (pSrc
!= pDest
) /* if not linked to that flash address */
250 if (pImage
->flags
& IF_UCL_2E
)
251 { /* UCL compressed, algorithm 2e */
252 UINT32 dst_len
; /* dummy */
253 ucl_nrv2e_decompress_8((UINT8
*)pSrc
, (UINT8
*)pDest
, &dst_len
);
256 { /* uncompressed, copy it */
257 UINT32 size
= pImage
->size
;
259 size
= (size
+ 3) / 4; /* round up to 32bit-words */
266 while (pDest
< pEnd
);
274 int ReadADC(int channel
)
276 /* after channel 3, the ports wrap and get re-used */
277 volatile UINT16
* pResult
= (UINT16
*)(ADDRAH_ADDR
+ 2 * (channel
& 0x03));
278 int timeout
= 266; /* conversion takes 266 clock cycles */
280 ADCSR
= 0x20 | channel
; /* start single conversion */
281 while (((ADCSR
& 0x80) == 0) && (--timeout
)); /* 6 instructions per round*/
283 return (timeout
== 0) ? -1 : *pResult
>>6;
288 /* This function is platform-dependent,
289 * until I figure out how to distinguish at runtime. */
290 int ButtonPressed(void) /* return 1,2,3 for F1,F2,F3, 0 if none pressed */
293 int value
= ReadADC(CHANNEL
);
295 if (value
>= F1_LOWER
&& value
<= F1_UPPER
) /* in range */
297 else if (value
>= F2_LOWER
&& value
<= F2_UPPER
) /* in range */
299 else if (value
>= F3_LOWER
&& value
<= F3_UPPER
) /* in range */
304 if (!(value
& F1_MASK
))
306 else if (!(value
& F2_MASK
))
308 else if (!(value
& F3_MASK
))
316 /* Determine the image to be started */
317 tImage
* GetStartImage(int nPreferred
)
320 tImage
* pImage2
= NULL
; /* default to not present */
322 UINT32
* pFlash
= (UINT32
*)FLASH_BASE
;
324 /* determine the first image position */
325 pos
= pFlash
[2] + pFlash
[3]; /* position + size of the bootloader
327 pos
= (pos
+ 3) & ~3; /* be sure it's 32 bit aligned */
329 pImage1
= (tImage
*)pos
;
331 if (pImage1
->size
!= 0)
332 { /* check for second image */
333 pos
= (UINT32
)(&pImage1
->image
) + pImage1
->size
;
334 pImage2
= (tImage
*)pos
;
336 /* does it make sense? (not in FF or 00 erazed space) */
337 if (pImage2
->pDestination
== (void*)0xFFFFFFFF
338 || pImage2
->size
== 0xFFFFFFFF
339 || pImage2
->pExecute
== (void*)0xFFFFFFFF
340 || pImage2
->flags
== 0xFFFFFFFF
341 || pImage2
->pDestination
== NULL
)
342 /* size, execute and flags can legally be 0 */
344 pImage2
= NULL
; /* invalidate */
348 if (pImage2
== NULL
|| nPreferred
== 1)
349 { /* no second image or overridden: return the first */
353 return pImage2
; /* return second image */
356 /* diagnostic functions */
358 void SetLed(BOOL bOn
)
369 PBIOR
&= 0xFBFF; /* input: RXD1 remote pin */
370 PBCR1
|= 0x00A0; /* set PB11+PB10 to UART */
371 PBCR1
&= 0xFFAF; /* clear bits 6, 4 -> UART */
372 SMR1
= 0x00; /* async format 8N1, baud generator input is CPU clock */
373 SCR1
= 0x30; /* transmit+receive enable */
374 PBCR1
&= 0x00FF; /* set bit 12...15 as GPIO */
375 SSR1
&= 0xBF; /* clear bit 6 (RDRF, receive data register full) */
382 while (!(SSR1
& SCI_RDRF
)); /* wait for char to be available */
389 void UartWrite(UINT8 byte
)
391 while (!(SSR1
& SCI_TDRE
)); /* wait for transmit buffer empty */
397 /* include the mini monitor as a rescue feature, started with F3 */
404 volatile UINT8
* paddr
= NULL
;
405 volatile UINT8
* pflash
= NULL
; /* flash base address */
415 content
= UartRead();
416 UartWrite(cmd
); /* acknowledge by returning the command value */
417 while (!(SSR1
& SCI_TEND
)); /* wait for empty shift register,
418 * before changing baudrate */
423 addr
= (UartRead() << 24) | (UartRead() << 16)
424 | (UartRead() << 8) | UartRead();
425 paddr
= (UINT8
*)addr
;
426 pflash
= (UINT8
*)(addr
& 0xFFF80000); /* round down to 512k align*/
427 UartWrite(cmd
); /* acknowledge by returning the command value */
432 UartWrite(content
); /* the content is the ack */
436 content
= UartRead();
438 UartWrite(cmd
); /* acknowledge by returning the command value */
446 UartWrite(content
); /* the content is the ack */
454 content
= UartRead();
457 UartWrite(cmd
); /* acknowledge by returning the command value */
461 content
= UartRead();
462 pflash
[0x5555] = 0xAA; /* set flash to command mode */
463 pflash
[0x2AAA] = 0x55;
464 pflash
[0x5555] = 0xA0; /* byte program command */
466 UartWrite(cmd
); /* acknowledge by returning the command value */
473 content
= UartRead();
474 pflash
[0x5555] = 0xAA; /* set flash to command mode */
475 pflash
[0x2AAA] = 0x55;
476 pflash
[0x5555] = 0xA0; /* byte program command */
479 UartWrite(cmd
); /* acknowledge by returning the command value */
483 content
= *(UINT16
*)paddr
;
485 UartWrite(content
>> 8); /* highbyte */
486 UartWrite(content
& 0xFF); /* lowbyte */
490 content
= UartRead() << 8 | UartRead();
491 *(UINT16
*)paddr
= content
;
493 UartWrite(cmd
); /* acknowledge by returning the command value */
498 tpFunc pFunc
= (tpFunc
)paddr
;
500 UartWrite(cmd
); /* acknowledge by returning the command value*/
505 UartWrite(1); /* return our version number */
511 UartWrite(~cmd
); /* error acknowledge */