1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 Randy D. Wood
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
24 #include "lcd-remote.h"
40 #include "crc32-mi4.h"
41 #undef FIRMWARE_OFFSET_FILE_CRC
42 #undef FIRMWARE_OFFSET_FILE_DATA
43 #define FIRMWARE_OFFSET_FILE_CRC 0xC
44 #define FIRMWARE_OFFSET_FILE_DATA 0x200
47 #if !defined(IRIVER_IFP7XX_SERIES)
48 /* FIX: this doesn't work on iFP */
50 #define IRQ0_EDGE_TRIGGER 0x80
53 /* Handle the COP properly - it needs to jump to a function outside SDRAM while
54 * the new firmware is being loaded, and then jump to the start of SDRAM
55 * TODO: Use the mailboxes built into the PP processor for this
59 volatile unsigned char IDATA_ATTR cpu_message
= 0;
60 volatile unsigned char IDATA_ATTR cpu_reply
= 0;
61 extern int cop_idlestackbegin
[];
63 void rolo_restart_cop(void) ICODE_ATTR
;
64 void rolo_restart_cop(void)
66 if (CURRENT_CORE
== CPU
)
68 /* There should be free thread slots aplenty */
69 create_thread(rolo_restart_cop
, cop_idlestackbegin
, IDLE_STACK_SIZE
,
70 0, "rolo COP" IF_PRIO(, PRIORITY_REALTIME
)
77 /* Invalidate cache */
78 cpucache_invalidate();
81 CACHE_CTL
= CACHE_CTL_DISABLE
;
83 /* Tell the main core that we're ready to reload */
86 /* Wait while RoLo loads the image into SDRAM */
87 /* TODO: Accept checksum failure gracefully */
88 while(cpu_message
!= 1);
90 /* Acknowledge the CPU and then reload */
99 #endif /* NUM_CORES > 1 */
102 static void rolo_error(const char *text
)
105 lcd_puts(0, 0, "ROLO error:");
106 lcd_puts_scroll(0, 1, text
);
114 #if CONFIG_CPU == SH7034 || CONFIG_CPU == IMX31L
115 /* these are in assembler file "descramble.S" for SH7034 */
116 extern unsigned short descramble(const unsigned char* source
,
117 unsigned char* dest
, int length
);
118 /* this is in firmware/target/arm/imx31/rolo_restart.S for IMX31 */
119 extern void rolo_restart(const unsigned char* source
, unsigned char* dest
,
123 /* explicitly put this code in iram, ICODE_ATTR is defined to be null for some
124 targets that are low on iram, like the gigabeat F/X */
125 void rolo_restart(const unsigned char* source
, unsigned char* dest
,
126 long length
) __attribute__ ((section(".icode")));
127 void rolo_restart(const unsigned char* source
, unsigned char* dest
,
131 unsigned char* localdest
= dest
;
133 /* This is the equivalent of a call to memcpy() but this must be done from
134 iram to avoid overwriting itself and we don't want to depend on memcpy()
135 always being in iram */
136 for(i
= 0;i
< length
;i
++)
137 *localdest
++ = *source
++;
139 #if defined(CPU_COLDFIRE)
141 "movec.l %0,%%vbr \n"
142 "move.l (%0)+,%%sp \n"
147 #elif defined(CPU_PP)
154 CACHE_CTL
= CACHE_CTL_DISABLE
;
156 /* Reset the memory mapping registers to zero */
158 volatile unsigned long *mmap_reg
;
159 for (mmap_reg
= &MMAP_FIRST
; mmap_reg
<= &MMAP_LAST
; mmap_reg
++)
164 /* Tell the COP it's safe to continue rebooting */
167 /* Wait for the COP to tell us it is rebooting */
168 while(cpu_reply
!= 2);
177 #elif defined(CPU_ARM)
178 #ifdef HAVE_CPUCACHE_INVALIDATE
179 /* Flush and invalidate caches */
180 cpucache_invalidate();
186 #elif defined(CPU_MIPS)
187 __dcache_writeback_all();
196 /* This is assigned in the linker control file */
197 extern unsigned long loadaddress
;
199 /***************************************************************************
201 * Name: rolo_load_app(char *filename,int scrambled)
202 * Filename must be a fully defined filename including the path and extension
204 ***************************************************************************/
205 int rolo_load(const char* filename
)
209 #if defined(CPU_COLDFIRE) || defined(CPU_ARM) || defined(CPU_MIPS)
210 #if !defined(MI4_FORMAT)
213 unsigned long checksum
,file_checksum
;
216 unsigned short checksum
,file_checksum
;
218 unsigned char* ramstart
= (void*)&loadaddress
;
221 lcd_puts(0, 0, "ROLO...");
222 lcd_puts(0, 1, "Loading");
224 #ifdef HAVE_REMOTE_LCD
225 lcd_remote_clear_display();
226 lcd_remote_puts(0, 0, "ROLO...");
227 lcd_remote_puts(0, 1, "Loading");
233 fd
= open(filename
, O_RDONLY
);
235 rolo_error("File not found");
239 length
= filesize(fd
) - FIRMWARE_OFFSET_FILE_DATA
;
241 #if CONFIG_CPU != SH7034
242 /* Read and save checksum */
243 lseek(fd
, FIRMWARE_OFFSET_FILE_CRC
, SEEK_SET
);
244 if (read(fd
, &file_checksum
, 4) != 4) {
245 rolo_error("Error Reading checksum");
249 #if !defined(MI4_FORMAT)
250 /* Rockbox checksums are big-endian */
251 file_checksum
= betoh32(file_checksum
);
254 #if defined(CPU_PP) && NUM_CORES > 1
255 lcd_puts(0, 2, "Waiting for coprocessor...");
258 /* Wait for COP to be in safe code */
259 while(cpu_reply
!= 1);
264 lseek(fd
, FIRMWARE_OFFSET_FILE_DATA
, SEEK_SET
);
266 if (read(fd
, audiobuf
, length
) != length
) {
267 rolo_error("Error Reading File");
272 /* Check CRC32 to see if we have a valid file */
273 chksum_crc32gentab();
274 checksum
= chksum_crc32 (audiobuf
, length
);
276 checksum
= MODEL_NUMBER
;
278 for(i
= 0;i
< length
;i
++) {
279 checksum
+= audiobuf
[i
];
283 /* Verify checksum against file header */
284 if (checksum
!= file_checksum
) {
285 rolo_error("Checksum Error");
289 #ifdef HAVE_STORAGE_FLUSH
290 lcd_puts(0, 1, "Flushing storage buffers");
295 lcd_puts(0, 1, "Executing");
297 #ifdef HAVE_REMOTE_LCD
298 lcd_remote_puts(0, 1, "Executing");
304 /* Should do these together since some ARM version should never have
305 * FIQ disabled and not IRQ (imx31 errata). */
306 disable_interrupt(IRQ_FIQ_STATUS
);
308 /* Some targets have a higher disable level than HIGEST_IRQ_LEVEL */
309 set_irq_level(DISABLE_INTERRUPTS
);
312 #else /* CONFIG_CPU == SH7034 */
313 /* Read file length from header and compare to real file length */
314 lseek(fd
, FIRMWARE_OFFSET_FILE_LENGTH
, SEEK_SET
);
315 if(read(fd
, &file_length
, 4) != 4) {
316 rolo_error("Error Reading File Length");
319 if (length
!= file_length
) {
320 rolo_error("File length mismatch");
324 /* Read and save checksum */
325 lseek(fd
, FIRMWARE_OFFSET_FILE_CRC
, SEEK_SET
);
326 if (read(fd
, &file_checksum
, 2) != 2) {
327 rolo_error("Error Reading checksum");
330 lseek(fd
, FIRMWARE_OFFSET_FILE_DATA
, SEEK_SET
);
332 /* verify that file can be read and descrambled */
333 if ((audiobuf
+ (2*length
)+4) >= audiobufend
) {
334 rolo_error("Not enough room to load file");
338 if (read(fd
, &audiobuf
[length
], length
) != (int)length
) {
339 rolo_error("Error Reading File");
343 lcd_puts(0, 1, "Descramble");
346 checksum
= descramble(audiobuf
+ length
, audiobuf
, length
);
348 /* Verify checksum against file header */
349 if (checksum
!= file_checksum
) {
350 rolo_error("Checksum Error");
354 #ifdef HAVE_STORAGE_FLUSH
355 lcd_puts(0, 1, "Flushing ");
360 lcd_puts(0, 1, "Executing ");
363 set_irq_level(HIGHEST_IRQ_LEVEL
);
365 /* Calling these 2 initialization routines was necessary to get the
366 the origional Archos version of the firmware to load and execute. */
367 system_init(); /* Initialize system for restart */
368 i2c_init(); /* Init i2c bus - it seems like a good idea */
369 ICR
= IRQ0_EDGE_TRIGGER
; /* Make IRQ0 edge triggered */
370 TSTR
= 0xE0; /* disable all timers */
371 /* model-specific de-init, needed when flashed */
372 /* Especially the Archos software is picky about this */
373 #if defined(ARCHOS_RECORDER) || defined(ARCHOS_RECORDERV2) || \
374 defined(ARCHOS_FMRECORDER)
378 rolo_restart(audiobuf
, ramstart
, length
);
380 return 0; /* this is never reached */
381 (void)checksum
; (void)file_checksum
;
383 #else /* !defined(IRIVER_IFP7XX_SERIES) */
384 int rolo_load(const char* filename
)
391 #endif /* !defined(IRIVER_IFP7XX_SERIES) */