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"
171 #elif defined(CPU_TCC780X) || (CONFIG_CPU==IMX31L)
172 /* Flush and invalidate caches */
183 /* This is assigned in the linker control file */
184 extern unsigned long loadaddress
;
186 /***************************************************************************
188 * Name: rolo_load_app(char *filename,int scrambled)
189 * Filename must be a fully defined filename including the path and extension
191 ***************************************************************************/
192 int rolo_load(const char* filename
)
196 #if defined(CPU_COLDFIRE) || defined(CPU_ARM)
197 #if !defined(MI4_FORMAT)
200 unsigned long checksum
,file_checksum
;
203 unsigned short checksum
,file_checksum
;
205 unsigned char* ramstart
= (void*)&loadaddress
;
208 lcd_puts(0, 0, "ROLO...");
209 lcd_puts(0, 1, "Loading");
211 #ifdef HAVE_REMOTE_LCD
212 lcd_remote_clear_display();
213 lcd_remote_puts(0, 0, "ROLO...");
214 lcd_remote_puts(0, 1, "Loading");
220 fd
= open(filename
, O_RDONLY
);
222 rolo_error("File not found");
226 length
= filesize(fd
) - FIRMWARE_OFFSET_FILE_DATA
;
228 #if defined(CPU_COLDFIRE) || defined(CPU_PP) || (CONFIG_CPU==DM320) \
229 || defined(CPU_TCC780X) || (CONFIG_CPU==IMX31L)
230 /* Read and save checksum */
231 lseek(fd
, FIRMWARE_OFFSET_FILE_CRC
, SEEK_SET
);
232 if (read(fd
, &file_checksum
, 4) != 4) {
233 rolo_error("Error Reading checksum");
237 #if !defined(MI4_FORMAT)
238 /* Rockbox checksums are big-endian */
239 file_checksum
= betoh32(file_checksum
);
242 #if defined(CPU_PP) && NUM_CORES > 1
243 lcd_puts(0, 2, "Waiting for coprocessor...");
246 /* Wait for COP to be in safe code */
247 while(cpu_reply
!= 1);
252 lseek(fd
, FIRMWARE_OFFSET_FILE_DATA
, SEEK_SET
);
254 if (read(fd
, audiobuf
, length
) != length
) {
255 rolo_error("Error Reading File");
260 /* Check CRC32 to see if we have a valid file */
261 chksum_crc32gentab();
262 checksum
= chksum_crc32 (audiobuf
, length
);
264 checksum
= MODEL_NUMBER
;
266 for(i
= 0;i
< length
;i
++) {
267 checksum
+= audiobuf
[i
];
271 /* Verify checksum against file header */
272 if (checksum
!= file_checksum
) {
273 rolo_error("Checksum Error");
277 lcd_puts(0, 1, "Executing");
279 #ifdef HAVE_REMOTE_LCD
280 lcd_remote_puts(0, 1, "Executing");
288 set_irq_level(DISABLE_INTERRUPTS
);
290 #elif CONFIG_CPU == SH7034
291 /* Read file length from header and compare to real file length */
292 lseek(fd
, FIRMWARE_OFFSET_FILE_LENGTH
, SEEK_SET
);
293 if(read(fd
, &file_length
, 4) != 4) {
294 rolo_error("Error Reading File Length");
297 if (length
!= file_length
) {
298 rolo_error("File length mismatch");
302 /* Read and save checksum */
303 lseek(fd
, FIRMWARE_OFFSET_FILE_CRC
, SEEK_SET
);
304 if (read(fd
, &file_checksum
, 2) != 2) {
305 rolo_error("Error Reading checksum");
308 lseek(fd
, FIRMWARE_OFFSET_FILE_DATA
, SEEK_SET
);
310 /* verify that file can be read and descrambled */
311 if ((audiobuf
+ (2*length
)+4) >= audiobufend
) {
312 rolo_error("Not enough room to load file");
316 if (read(fd
, &audiobuf
[length
], length
) != (int)length
) {
317 rolo_error("Error Reading File");
321 lcd_puts(0, 1, "Descramble");
324 checksum
= descramble(audiobuf
+ length
, audiobuf
, length
);
326 /* Verify checksum against file header */
327 if (checksum
!= file_checksum
) {
328 rolo_error("Checksum Error");
332 lcd_puts(0, 1, "Executing ");
335 set_irq_level(HIGHEST_IRQ_LEVEL
);
337 /* Calling these 2 initialization routines was necessary to get the
338 the origional Archos version of the firmware to load and execute. */
339 system_init(); /* Initialize system for restart */
340 i2c_init(); /* Init i2c bus - it seems like a good idea */
341 ICR
= IRQ0_EDGE_TRIGGER
; /* Make IRQ0 edge triggered */
342 TSTR
= 0xE0; /* disable all timers */
343 /* model-specific de-init, needed when flashed */
344 /* Especially the Archos software is picky about this */
345 #if defined(ARCHOS_RECORDER) || defined(ARCHOS_RECORDERV2) || \
346 defined(ARCHOS_FMRECORDER)
350 rolo_restart(audiobuf
, ramstart
, length
);
352 return 0; /* this is never reached */
353 (void)checksum
; (void)file_checksum
;
355 #else /* !defined(IRIVER_IFP7XX_SERIES) */
356 int rolo_load(const char* filename
)
363 #endif /* !defined(IRIVER_IFP7XX_SERIES) */