move handling of shared manuals (like h100 series) to a new setting in rbutil.ini...
[Rockbox.git] / firmware / rolo.c
blob0b8a4f28bae212dd60cbcb725ee2fbd8991c97ec
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
20 #include "config.h"
21 #include "lcd.h"
22 #include "lcd-remote.h"
23 #include "kernel.h"
24 #include "sprintf.h"
25 #include "button.h"
26 #include "file.h"
27 #include "audio.h"
28 #include "system.h"
29 #include "i2c.h"
30 #include "string.h"
31 #include "buffer.h"
33 #ifdef MI4_FORMAT
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
39 #endif
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
47 #ifdef CPU_PP
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 */
60 invalidate_icache();
62 /* Disable cache */
63 CACHE_CTL = CACHE_DISABLE;
65 /* Tell the main core that we're ready to reload */
66 cpu_reply = 2;
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 */
73 cpu_reply = 1;
75 asm volatile(
76 "mov r0, #0x10000000 \n"
77 "mov pc, r0 \n"
80 #endif
82 static void rolo_error(const char *text)
84 lcd_clear_display();
85 lcd_puts(0, 0, "ROLO error:");
86 lcd_puts_scroll(0, 1, text);
87 lcd_update();
88 button_get(true);
89 button_get(true);
90 button_get(true);
91 lcd_stop_scroll();
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,
99 int length);
100 #else
101 void rolo_restart(const unsigned char* source, unsigned char* dest,
102 long length) __attribute__ ((section (".icode")));
103 void rolo_restart(const unsigned char* source, unsigned char* dest,
104 long length)
106 long i;
107 unsigned char* localdest = dest;
108 #if (CONFIG_CPU==PP5020) || (CONFIG_CPU==PP5024)
109 unsigned long* memmapregs = (unsigned long*)0xf000f000;
110 #endif
112 for(i = 0;i < length;i++)
113 *localdest++ = *source++;
115 #if defined(CPU_COLDFIRE)
116 asm (
117 "movec.l %0,%%vbr \n"
118 "move.l (%0)+,%%sp \n"
119 "move.l (%0),%0 \n"
120 "jmp (%0) \n"
121 : : "a"(dest)
123 #elif (CONFIG_CPU==PP5020) || (CONFIG_CPU==PP5024)
125 /* Tell the COP that we've finished loading and started rebooting */
126 cpu_message = 0;
128 /* Flush cache */
129 flush_icache();
131 /* Disable cache */
132 CACHE_CTL = CACHE_DISABLE;
134 /* Reset the memory mapping registers to zero */
135 for (i=0;i<8;i++)
136 memmapregs[i]=0;
138 /* Wait for the COP to tell us it is rebooting */
139 while(cpu_reply != 1) {}
141 asm volatile(
142 "mov r0, #0x10000000 \n"
143 "mov pc, r0 \n"
145 #endif
147 #endif
149 /* This is assigned in the linker control file */
150 extern unsigned long loadaddress;
152 /***************************************************************************
154 * Name: rolo_load_app(char *filename,int scrambled)
155 * Filename must be a fully defined filename including the path and extension
157 ***************************************************************************/
158 int rolo_load(const char* filename)
160 int fd;
161 long length;
162 #if defined(CPU_COLDFIRE) || defined(CPU_PP)
163 #if !defined(MI4_FORMAT)
164 int i;
165 #endif
166 unsigned long checksum,file_checksum;
167 #else
168 long file_length;
169 unsigned short checksum,file_checksum;
170 #endif
171 unsigned char* ramstart = (void*)&loadaddress;
173 lcd_clear_display();
174 lcd_puts(0, 0, "ROLO...");
175 lcd_puts(0, 1, "Loading");
176 lcd_update();
177 #ifdef HAVE_REMOTE_LCD
178 lcd_remote_clear_display();
179 lcd_remote_puts(0, 0, "ROLO...");
180 lcd_remote_puts(0, 1, "Loading");
181 lcd_remote_update();
182 #endif
184 audio_stop();
186 fd = open(filename, O_RDONLY);
187 if(-1 == fd) {
188 rolo_error("File not found");
189 return -1;
192 length = filesize(fd) - FIRMWARE_OFFSET_FILE_DATA;
194 #if defined(CPU_COLDFIRE) || defined(CPU_PP)
195 /* Read and save checksum */
196 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
197 if (read(fd, &file_checksum, 4) != 4) {
198 rolo_error("Error Reading checksum");
199 return -1;
202 #if !defined(MI4_FORMAT)
203 /* Rockbox checksums are big-endian */
204 file_checksum = betoh32(file_checksum);
205 #endif
207 #ifdef CPU_PP
208 cpu_message = COP_REBOOT;
209 COP_CTL = PROC_WAKE;
210 lcd_puts(0, 2, "Waiting for coprocessor...");
211 lcd_update();
212 while(cpu_reply != 2) {}
213 lcd_puts(0, 2, " ");
214 lcd_update();
215 #endif
217 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
219 if (read(fd, audiobuf, length) != length) {
220 rolo_error("Error Reading File");
221 return -1;
224 #ifdef MI4_FORMAT
225 /* Check CRC32 to see if we have a valid file */
226 chksum_crc32gentab();
227 checksum = chksum_crc32 (audiobuf, length);
228 #else
229 checksum = MODEL_NUMBER;
231 for(i = 0;i < length;i++) {
232 checksum += audiobuf[i];
234 #endif
236 /* Verify checksum against file header */
237 if (checksum != file_checksum) {
238 rolo_error("Checksum Error");
239 return -1;
242 lcd_puts(0, 1, "Executing");
243 lcd_update();
244 #ifdef HAVE_REMOTE_LCD
245 lcd_remote_puts(0, 1, "Executing");
246 lcd_remote_update();
247 #endif
249 set_irq_level(HIGHEST_IRQ_LEVEL);
250 #elif CONFIG_CPU == SH7034
251 /* Read file length from header and compare to real file length */
252 lseek(fd, FIRMWARE_OFFSET_FILE_LENGTH, SEEK_SET);
253 if(read(fd, &file_length, 4) != 4) {
254 rolo_error("Error Reading File Length");
255 return -1;
257 if (length != file_length) {
258 rolo_error("File length mismatch");
259 return -1;
262 /* Read and save checksum */
263 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
264 if (read(fd, &file_checksum, 2) != 2) {
265 rolo_error("Error Reading checksum");
266 return -1;
268 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
270 /* verify that file can be read and descrambled */
271 if ((audiobuf + (2*length)+4) >= audiobufend) {
272 rolo_error("Not enough room to load file");
273 return -1;
276 if (read(fd, &audiobuf[length], length) != (int)length) {
277 rolo_error("Error Reading File");
278 return -1;
281 lcd_puts(0, 1, "Descramble");
282 lcd_update();
284 checksum = descramble(audiobuf + length, audiobuf, length);
286 /* Verify checksum against file header */
287 if (checksum != file_checksum) {
288 rolo_error("Checksum Error");
289 return -1;
292 lcd_puts(0, 1, "Executing ");
293 lcd_update();
295 set_irq_level(HIGHEST_IRQ_LEVEL);
297 /* Calling these 2 initialization routines was necessary to get the
298 the origional Archos version of the firmware to load and execute. */
299 system_init(); /* Initialize system for restart */
300 i2c_init(); /* Init i2c bus - it seems like a good idea */
301 ICR = IRQ0_EDGE_TRIGGER; /* Make IRQ0 edge triggered */
302 TSTR = 0xE0; /* disable all timers */
303 /* model-specific de-init, needed when flashed */
304 /* Especially the Archos software is picky about this */
305 #if defined(ARCHOS_RECORDER) || defined(ARCHOS_RECORDERV2) || \
306 defined(ARCHOS_FMRECORDER)
307 PAIOR = 0x0FA0;
308 #endif
309 #endif
310 rolo_restart(audiobuf, ramstart, length);
312 return 0; /* this is never reached */
314 #else /* !defined(IRIVER_IFP7XX_SERIES) */
315 int rolo_load(const char* filename)
317 /* dummy */
318 (void)filename;
319 return 0;
322 #endif /* !defined(IRIVER_IFP7XX_SERIES) */