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"
34 #include "crc32-mi4.h"
35 #undef FIRMWARE_OFFSET_FILE_CRC
36 #undef FIRMWARE_OFFSET_FILE_DATA
37 #define FIRMWARE_OFFSET_FILE_CRC 0xC
38 #define FIRMWARE_OFFSET_FILE_DATA 0x200
41 #if !defined(IRIVER_IFP7XX_SERIES) && \
42 (CONFIG_CPU != PP5002) && (CONFIG_CPU != S3C2440)
43 /* FIX: this doesn't work on iFP, 3rd Gen ipods */
45 #define IRQ0_EDGE_TRIGGER 0x80
48 /* Handle the COP properly - it needs to jump to a function outside SDRAM while
49 * the new firmware is being loaded, and then jump to the start of SDRAM
50 * TODO: Use the mailboxes built into the PP processor for this
53 volatile unsigned char IDATA_ATTR cpu_message
= 0;
54 volatile unsigned char IDATA_ATTR cpu_reply
= 0;
56 void rolo_restart_cop(void) ICODE_ATTR
;
57 void rolo_restart_cop(void)
59 /* Invalidate cache */
63 CACHE_CTL
= CACHE_DISABLE
;
65 /* Tell the main core that we're ready to reload */
68 /* Wait while RoLo loads the image into SDRAM */
69 /* TODO: Accept checksum failure gracefully */
70 while(cpu_message
== 1) {}
72 /* Acknowledge the CPU and then reload */
76 "mov r0, #0x10000000 \n"
82 static void rolo_error(const char *text
)
85 lcd_puts(0, 0, "ROLO error:");
86 lcd_puts_scroll(0, 1, text
);
94 #if CONFIG_CPU == SH7034
95 /* these are in assembler file "descramble.S" */
96 extern unsigned short descramble(const unsigned char* source
,
97 unsigned char* dest
, int length
);
98 extern void rolo_restart(const unsigned char* source
, unsigned char* dest
,
102 /* explicitly put this code in iram, ICODE_ATTR is defined to be null for some
103 targets that are low on iram, like the gigabeat F/X */
104 void rolo_restart(const unsigned char* source
, unsigned char* dest
,
105 long length
) __attribute__ ((section(".icode")));
106 void rolo_restart(const unsigned char* source
, unsigned char* dest
,
110 unsigned char* localdest
= dest
;
112 unsigned long* memmapregs
= (unsigned long*)0xf000f000;
115 /* This is the equivalent of a call to memcpy() but this must be done from
116 iram to avoid overwriting itself and we don't want to depend on memcpy()
117 always being in iram */
118 for(i
= 0;i
< length
;i
++)
119 *localdest
++ = *source
++;
121 #if defined(CPU_COLDFIRE)
123 "movec.l %0,%%vbr \n"
124 "move.l (%0)+,%%sp \n"
129 #elif defined(CPU_PP502x)
131 /* Tell the COP that we've finished loading and started rebooting */
138 CACHE_CTL
= CACHE_DISABLE
;
140 /* Reset the memory mapping registers to zero */
144 /* Wait for the COP to tell us it is rebooting */
145 while(cpu_reply
!= 1) {}
148 "mov r0, #0x10000000 \n"
155 /* This is assigned in the linker control file */
156 extern unsigned long loadaddress
;
158 /***************************************************************************
160 * Name: rolo_load_app(char *filename,int scrambled)
161 * Filename must be a fully defined filename including the path and extension
163 ***************************************************************************/
164 int rolo_load(const char* filename
)
168 #if defined(CPU_COLDFIRE) || defined(CPU_PP)
169 #if !defined(MI4_FORMAT)
172 unsigned long checksum
,file_checksum
;
175 unsigned short checksum
,file_checksum
;
177 unsigned char* ramstart
= (void*)&loadaddress
;
180 lcd_puts(0, 0, "ROLO...");
181 lcd_puts(0, 1, "Loading");
183 #ifdef HAVE_REMOTE_LCD
184 lcd_remote_clear_display();
185 lcd_remote_puts(0, 0, "ROLO...");
186 lcd_remote_puts(0, 1, "Loading");
192 fd
= open(filename
, O_RDONLY
);
194 rolo_error("File not found");
198 length
= filesize(fd
) - FIRMWARE_OFFSET_FILE_DATA
;
200 #if defined(CPU_COLDFIRE) || defined(CPU_PP)
201 /* Read and save checksum */
202 lseek(fd
, FIRMWARE_OFFSET_FILE_CRC
, SEEK_SET
);
203 if (read(fd
, &file_checksum
, 4) != 4) {
204 rolo_error("Error Reading checksum");
208 #if !defined(MI4_FORMAT)
209 /* Rockbox checksums are big-endian */
210 file_checksum
= betoh32(file_checksum
);
214 cpu_message
= COP_REBOOT
;
216 lcd_puts(0, 2, "Waiting for coprocessor...");
218 while(cpu_reply
!= 2) {}
223 lseek(fd
, FIRMWARE_OFFSET_FILE_DATA
, SEEK_SET
);
225 if (read(fd
, audiobuf
, length
) != length
) {
226 rolo_error("Error Reading File");
231 /* Check CRC32 to see if we have a valid file */
232 chksum_crc32gentab();
233 checksum
= chksum_crc32 (audiobuf
, length
);
235 checksum
= MODEL_NUMBER
;
237 for(i
= 0;i
< length
;i
++) {
238 checksum
+= audiobuf
[i
];
242 /* Verify checksum against file header */
243 if (checksum
!= file_checksum
) {
244 rolo_error("Checksum Error");
248 lcd_puts(0, 1, "Executing");
250 #ifdef HAVE_REMOTE_LCD
251 lcd_remote_puts(0, 1, "Executing");
255 set_irq_level(HIGHEST_IRQ_LEVEL
);
256 #elif CONFIG_CPU == SH7034
257 /* Read file length from header and compare to real file length */
258 lseek(fd
, FIRMWARE_OFFSET_FILE_LENGTH
, SEEK_SET
);
259 if(read(fd
, &file_length
, 4) != 4) {
260 rolo_error("Error Reading File Length");
263 if (length
!= file_length
) {
264 rolo_error("File length mismatch");
268 /* Read and save checksum */
269 lseek(fd
, FIRMWARE_OFFSET_FILE_CRC
, SEEK_SET
);
270 if (read(fd
, &file_checksum
, 2) != 2) {
271 rolo_error("Error Reading checksum");
274 lseek(fd
, FIRMWARE_OFFSET_FILE_DATA
, SEEK_SET
);
276 /* verify that file can be read and descrambled */
277 if ((audiobuf
+ (2*length
)+4) >= audiobufend
) {
278 rolo_error("Not enough room to load file");
282 if (read(fd
, &audiobuf
[length
], length
) != (int)length
) {
283 rolo_error("Error Reading File");
287 lcd_puts(0, 1, "Descramble");
290 checksum
= descramble(audiobuf
+ length
, audiobuf
, length
);
292 /* Verify checksum against file header */
293 if (checksum
!= file_checksum
) {
294 rolo_error("Checksum Error");
298 lcd_puts(0, 1, "Executing ");
301 set_irq_level(HIGHEST_IRQ_LEVEL
);
303 /* Calling these 2 initialization routines was necessary to get the
304 the origional Archos version of the firmware to load and execute. */
305 system_init(); /* Initialize system for restart */
306 i2c_init(); /* Init i2c bus - it seems like a good idea */
307 ICR
= IRQ0_EDGE_TRIGGER
; /* Make IRQ0 edge triggered */
308 TSTR
= 0xE0; /* disable all timers */
309 /* model-specific de-init, needed when flashed */
310 /* Especially the Archos software is picky about this */
311 #if defined(ARCHOS_RECORDER) || defined(ARCHOS_RECORDERV2) || \
312 defined(ARCHOS_FMRECORDER)
316 rolo_restart(audiobuf
, ramstart
, length
);
318 return 0; /* this is never reached */
320 #else /* !defined(IRIVER_IFP7XX_SERIES) */
321 int rolo_load(const char* filename
)
328 #endif /* !defined(IRIVER_IFP7XX_SERIES) */