Fixed crash when changing between id3 and file mode in wps.
[kugel-rb.git] / firmware / rolo.c
blob5b69c3679f4660312ab51f2f0902765241dbc861
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 "lcd.h"
21 #include "kernel.h"
22 #include "sprintf.h"
23 #include "button.h"
24 #include "file.h"
25 #include "mpeg.h"
26 #include "system.h"
27 #include "i2c.h"
28 #include "string.h"
29 #include "buffer.h"
31 #define IRQ0_EDGE_TRIGGER 0x80
33 static void rolo_error(const char *text)
35 lcd_clear_display();
36 lcd_puts(0, 0, "ROLO error:");
37 lcd_puts_scroll(0, 1, text);
38 lcd_update();
39 button_get(true);
40 button_get(true);
41 button_get(true);
42 lcd_stop_scroll();
45 /* these are in assembler file "descramble.S" */
46 extern unsigned short descramble(const unsigned char* source,
47 unsigned char* dest, int length);
48 extern void rolo_restart(const unsigned char* source, unsigned char* dest,
49 int length);
51 /***************************************************************************
53 * Name: rolo_load_app(char *filename,int scrambled)
54 * Filename must be a fully defined filename including the path and extension
56 ***************************************************************************/
57 int rolo_load(const char* filename)
59 int fd;
60 unsigned long length;
61 unsigned long file_length;
62 unsigned short checksum,file_checksum;
63 unsigned char* ramstart = (void*)0x09000000;
65 lcd_clear_display();
66 lcd_puts(0, 0, "ROLO...");
67 lcd_puts(0, 1, "Loading");
68 lcd_update();
70 mpeg_stop();
72 fd = open(filename, O_RDONLY);
73 if(-1 == fd) {
74 rolo_error("File not found");
75 return -1;
78 /* Read file length from header and compare to real file length */
79 length=lseek(fd,0,SEEK_END)-FIRMWARE_OFFSET_FILE_DATA;
80 lseek(fd, FIRMWARE_OFFSET_FILE_LENGTH, SEEK_SET);
81 if(read(fd, &file_length, 4) != 4) {
82 rolo_error("Error Reading File Length");
83 return -1;
85 if (length != file_length) {
86 rolo_error("File length mismatch");
87 return -1;
90 /* Read and save checksum */
91 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
92 if (read(fd, &file_checksum, 2) != 2) {
93 rolo_error("Error Reading checksum");
94 return -1;
96 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
98 /* verify that file can be read and descrambled */
99 if ((mp3buf + (2*length)+4) >= mp3end) {
100 rolo_error("Not enough room to load file");
101 return -1;
104 if (read(fd, &mp3buf[length], length) != (int)length) {
105 rolo_error("Error Reading File");
106 return -1;
109 lcd_puts(0, 1, "Descramble");
110 lcd_update();
112 checksum = descramble(mp3buf + length, mp3buf, length);
114 /* Verify checksum against file header */
116 if (checksum != file_checksum) {
117 rolo_error("Checksum Error");
118 return -1;
121 lcd_puts(0, 1, "Executing ");
122 lcd_update();
124 /* Disable interrupts */
125 asm("mov #15<<4,r6\n"
126 "ldc r6,sr");
128 /* Calling these 2 initialization routines was necessary to get the
129 the origional Archos version of the firmware to load and execute. */
130 system_init(); /* Initialize system for restart */
131 i2c_init(); /* Init i2c bus - it seems like a good idea */
132 ICR = IRQ0_EDGE_TRIGGER; /* Make IRQ0 edge triggered */
133 TSTR = 0xE0; /* disable all timers */
134 /* model-specific de-init, needed when flashed */
135 /* Especially the Archos software is picky about this */
136 #if defined(ARCHOS_RECORDER) || defined(ARCHOS_RECORDERV2) || defined(ARCHOS_FMRECORDER)
137 PAIOR = 0x0FA0;
138 #endif
140 rolo_restart(mp3buf, ramstart, length);
142 return 0; /* this is never reached */