1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 Randy D. Wood
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
22 #include "lcd-remote.h"
36 #include "crc32-mi4.h"
37 #undef FIRMWARE_OFFSET_FILE_CRC
38 #undef FIRMWARE_OFFSET_FILE_DATA
39 #define FIRMWARE_OFFSET_FILE_CRC 0xC
40 #define FIRMWARE_OFFSET_FILE_DATA 0x200
43 #if !defined(IRIVER_IFP7XX_SERIES) && \
44 (CONFIG_CPU != PP5002) && (CONFIG_CPU != S3C2440)
45 /* FIX: this doesn't work on iFP, 3rd Gen ipods */
47 #define IRQ0_EDGE_TRIGGER 0x80
50 /* Handle the COP properly - it needs to jump to a function outside SDRAM while
51 * the new firmware is being loaded, and then jump to the start of SDRAM
52 * TODO: Use the mailboxes built into the PP processor for this
56 volatile unsigned char IDATA_ATTR cpu_message
= 0;
57 volatile unsigned char IDATA_ATTR cpu_reply
= 0;
58 extern int cop_idlestackbegin
[];
60 void rolo_restart_cop(void) ICODE_ATTR
;
61 void rolo_restart_cop(void)
63 if (CURRENT_CORE
== CPU
)
65 /* There should be free thread slots aplenty */
66 create_thread(rolo_restart_cop
, cop_idlestackbegin
, IDLE_STACK_SIZE
,
67 0, "rolo COP" IF_PRIO(, PRIORITY_REALTIME
)
74 /* Invalidate cache */
78 CACHE_CTL
= CACHE_CTL_DISABLE
;
80 /* Tell the main core that we're ready to reload */
83 /* Wait while RoLo loads the image into SDRAM */
84 /* TODO: Accept checksum failure gracefully */
85 while(cpu_message
!= 1);
87 /* Acknowledge the CPU and then reload */
91 "mov r0, #0x10000000 \n"
95 #endif /* NUM_CORES > 1 */
98 static void rolo_error(const char *text
)
101 lcd_puts(0, 0, "ROLO error:");
102 lcd_puts_scroll(0, 1, text
);
110 #if CONFIG_CPU == SH7034
111 /* these are in assembler file "descramble.S" */
112 extern unsigned short descramble(const unsigned char* source
,
113 unsigned char* dest
, int length
);
114 extern void rolo_restart(const unsigned char* source
, unsigned char* dest
,
118 /* explicitly put this code in iram, ICODE_ATTR is defined to be null for some
119 targets that are low on iram, like the gigabeat F/X */
120 void rolo_restart(const unsigned char* source
, unsigned char* dest
,
121 long length
) __attribute__ ((section(".icode")));
122 void rolo_restart(const unsigned char* source
, unsigned char* dest
,
126 unsigned char* localdest
= dest
;
128 /* This is the equivalent of a call to memcpy() but this must be done from
129 iram to avoid overwriting itself and we don't want to depend on memcpy()
130 always being in iram */
131 for(i
= 0;i
< length
;i
++)
132 *localdest
++ = *source
++;
134 #if defined(CPU_COLDFIRE)
136 "movec.l %0,%%vbr \n"
137 "move.l (%0)+,%%sp \n"
142 #elif defined(CPU_PP502x)
149 CACHE_CTL
= CACHE_CTL_DISABLE
;
151 /* Reset the memory mapping registers to zero */
153 volatile unsigned long *mmap_reg
;
154 for (mmap_reg
= &MMAP_FIRST
; mmap_reg
<= &MMAP_LAST
; mmap_reg
++)
159 /* Tell the COP it's safe to continue rebooting */
162 /* Wait for the COP to tell us it is rebooting */
163 while(cpu_reply
!= 2);
167 "mov r0, #0x10000000 \n"
174 /* This is assigned in the linker control file */
175 extern unsigned long loadaddress
;
177 /***************************************************************************
179 * Name: rolo_load_app(char *filename,int scrambled)
180 * Filename must be a fully defined filename including the path and extension
182 ***************************************************************************/
183 int rolo_load(const char* filename
)
187 #if defined(CPU_COLDFIRE) || defined(CPU_ARM)
188 #if !defined(MI4_FORMAT)
191 unsigned long checksum
,file_checksum
;
194 unsigned short checksum
,file_checksum
;
196 unsigned char* ramstart
= (void*)&loadaddress
;
199 lcd_puts(0, 0, "ROLO...");
200 lcd_puts(0, 1, "Loading");
202 #ifdef HAVE_REMOTE_LCD
203 lcd_remote_clear_display();
204 lcd_remote_puts(0, 0, "ROLO...");
205 lcd_remote_puts(0, 1, "Loading");
211 fd
= open(filename
, O_RDONLY
);
213 rolo_error("File not found");
217 length
= filesize(fd
) - FIRMWARE_OFFSET_FILE_DATA
;
219 #if defined(CPU_COLDFIRE) || defined(CPU_PP) || (CONFIG_CPU==DM320)
220 /* Read and save checksum */
221 lseek(fd
, FIRMWARE_OFFSET_FILE_CRC
, SEEK_SET
);
222 if (read(fd
, &file_checksum
, 4) != 4) {
223 rolo_error("Error Reading checksum");
227 #if !defined(MI4_FORMAT)
228 /* Rockbox checksums are big-endian */
229 file_checksum
= betoh32(file_checksum
);
232 #if defined(CPU_PP) && NUM_CORES > 1
233 lcd_puts(0, 2, "Waiting for coprocessor...");
236 /* Wait for COP to be in safe code */
237 while(cpu_reply
!= 1);
242 lseek(fd
, FIRMWARE_OFFSET_FILE_DATA
, SEEK_SET
);
244 if (read(fd
, audiobuf
, length
) != length
) {
245 rolo_error("Error Reading File");
250 /* Check CRC32 to see if we have a valid file */
251 chksum_crc32gentab();
252 checksum
= chksum_crc32 (audiobuf
, length
);
254 checksum
= MODEL_NUMBER
;
256 for(i
= 0;i
< length
;i
++) {
257 checksum
+= audiobuf
[i
];
261 /* Verify checksum against file header */
262 if (checksum
!= file_checksum
) {
263 rolo_error("Checksum Error");
267 lcd_puts(0, 1, "Executing");
269 #ifdef HAVE_REMOTE_LCD
270 lcd_remote_puts(0, 1, "Executing");
278 set_irq_level(DISABLE_INTERRUPTS
);
280 #elif CONFIG_CPU == SH7034
281 /* Read file length from header and compare to real file length */
282 lseek(fd
, FIRMWARE_OFFSET_FILE_LENGTH
, SEEK_SET
);
283 if(read(fd
, &file_length
, 4) != 4) {
284 rolo_error("Error Reading File Length");
287 if (length
!= file_length
) {
288 rolo_error("File length mismatch");
292 /* Read and save checksum */
293 lseek(fd
, FIRMWARE_OFFSET_FILE_CRC
, SEEK_SET
);
294 if (read(fd
, &file_checksum
, 2) != 2) {
295 rolo_error("Error Reading checksum");
298 lseek(fd
, FIRMWARE_OFFSET_FILE_DATA
, SEEK_SET
);
300 /* verify that file can be read and descrambled */
301 if ((audiobuf
+ (2*length
)+4) >= audiobufend
) {
302 rolo_error("Not enough room to load file");
306 if (read(fd
, &audiobuf
[length
], length
) != (int)length
) {
307 rolo_error("Error Reading File");
311 lcd_puts(0, 1, "Descramble");
314 checksum
= descramble(audiobuf
+ length
, audiobuf
, length
);
316 /* Verify checksum against file header */
317 if (checksum
!= file_checksum
) {
318 rolo_error("Checksum Error");
322 lcd_puts(0, 1, "Executing ");
325 set_irq_level(HIGHEST_IRQ_LEVEL
);
327 /* Calling these 2 initialization routines was necessary to get the
328 the origional Archos version of the firmware to load and execute. */
329 system_init(); /* Initialize system for restart */
330 i2c_init(); /* Init i2c bus - it seems like a good idea */
331 ICR
= IRQ0_EDGE_TRIGGER
; /* Make IRQ0 edge triggered */
332 TSTR
= 0xE0; /* disable all timers */
333 /* model-specific de-init, needed when flashed */
334 /* Especially the Archos software is picky about this */
335 #if defined(ARCHOS_RECORDER) || defined(ARCHOS_RECORDERV2) || \
336 defined(ARCHOS_FMRECORDER)
340 rolo_restart(audiobuf
, ramstart
, length
);
342 return 0; /* this is never reached */
344 #else /* !defined(IRIVER_IFP7XX_SERIES) */
345 int rolo_load(const char* filename
)
352 #endif /* !defined(IRIVER_IFP7XX_SERIES) */