stupid yellow.... I'm going to bed
[kugel-rb.git] / bootloader / common.c
blobc9b2bfb7a33b6058f076c8c1ffdb9d760e628453
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: main.c 11997 2007-01-13 09:08:18Z miipekk $
10 * Copyright (C) 2005 by Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "lcd.h"
22 #include "lcd-remote.h"
23 #include "font.h"
24 #include "system.h"
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdbool.h>
28 #include "cpu.h"
29 #include "common.h"
30 #include "power.h"
31 #include "kernel.h"
32 #include "config.h"
33 #include "logf.h"
34 #include "button.h"
35 #include "string.h"
36 #include "usb.h"
37 #include "file.h"
39 /* TODO: Other bootloaders need to be adjusted to set this variable to true
40 on a button press - currently only the ipod, H10 and Sansa versions do. */
41 #if defined(IPOD_ARCH) || defined(IRIVER_H10) || defined(IRIVER_H10_5GB) \
42 || defined(SANSA_E200) || defined(SANSA_C200) || defined(GIGABEAT_F) \
43 || defined(PHILIPS_SA9200) || (CONFIG_CPU == AS3525) || defined(COWON_D2) \
44 || defined(MROBE_100) || defined(PHILIPS_HDD1630) || defined(MROBE_500) \
45 || defined(ONDA_VX747)
46 bool verbose = false;
47 #else
48 bool verbose = true;
49 #endif
51 int line = 0;
52 #ifdef HAVE_REMOTE_LCD
53 int remote_line = 0;
54 #endif
56 char printfbuf[256];
58 void reset_screen(void)
60 lcd_clear_display();
61 line = 0;
62 #ifdef HAVE_REMOTE_LCD
63 lcd_remote_clear_display();
64 remote_line = 0;
65 #endif
68 void printf(const char *format, ...)
70 int len;
71 unsigned char *ptr;
72 va_list ap;
73 va_start(ap, format);
75 ptr = printfbuf;
76 len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
77 va_end(ap);
79 lcd_puts(0, line++, ptr);
80 if (verbose)
81 lcd_update();
82 if(line >= LCD_HEIGHT/SYSFONT_HEIGHT)
83 line = 0;
84 #ifdef HAVE_REMOTE_LCD
85 lcd_remote_puts(0, remote_line++, ptr);
86 if (verbose)
87 lcd_remote_update();
88 if(remote_line >= LCD_REMOTE_HEIGHT/SYSFONT_HEIGHT)
89 remote_line = 0;
90 #endif
93 char *strerror(int error)
95 switch(error)
97 case EOK:
98 return "OK";
99 case EFILE_NOT_FOUND:
100 return "File not found";
101 case EREAD_CHKSUM_FAILED:
102 return "Read failed (chksum)";
103 case EREAD_MODEL_FAILED:
104 return "Read failed (model)";
105 case EREAD_IMAGE_FAILED:
106 return "Read failed (image)";
107 case EBAD_CHKSUM:
108 return "Bad checksum";
109 case EFILE_TOO_BIG:
110 return "File too big";
111 case EINVALID_FORMAT:
112 return "Invalid file format";
113 default:
114 return "Unknown";
118 void error(int errortype, int error)
120 switch(errortype)
122 case EATA:
123 printf("ATA error: %d", error);
124 break;
126 case EDISK:
127 printf("No partition found");
128 break;
130 case EBOOTFILE:
131 printf(strerror(error));
132 break;
135 lcd_update();
136 sleep(5*HZ);
137 power_off();
140 /* Load firmware image in a format created by tools/scramble */
141 int load_firmware(unsigned char* buf, char* firmware, int buffer_size)
143 int fd;
144 int rc;
145 int len;
146 unsigned long chksum;
147 char model[5];
148 unsigned long sum;
149 int i;
150 char filename[MAX_PATH];
152 snprintf(filename,sizeof(filename), BOOTDIR "/%s",firmware);
153 fd = open(filename, O_RDONLY);
154 if(fd < 0)
156 snprintf(filename,sizeof(filename),"/%s",firmware);
157 fd = open(filename, O_RDONLY);
158 if(fd < 0)
159 return EFILE_NOT_FOUND;
162 len = filesize(fd) - 8;
164 printf("Length: %x", len);
166 if (len > buffer_size)
167 return EFILE_TOO_BIG;
169 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
171 rc = read(fd, &chksum, 4);
172 chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
173 if(rc < 4)
174 return EREAD_CHKSUM_FAILED;
176 printf("Checksum: %x", chksum);
178 rc = read(fd, model, 4);
179 if(rc < 4)
180 return EREAD_MODEL_FAILED;
182 model[4] = 0;
184 printf("Model name: %s", model);
185 printf("Loading %s", firmware);
187 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
189 rc = read(fd, buf, len);
190 if(rc < len)
191 return EREAD_IMAGE_FAILED;
193 close(fd);
195 sum = MODEL_NUMBER;
197 for(i = 0;i < len;i++) {
198 sum += buf[i];
201 printf("Sum: %x", sum);
203 if(sum != chksum)
204 return EBAD_CHKSUM;
206 return EOK;
209 /* Load raw binary image. */
210 int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size)
212 int fd;
213 int rc;
214 int len;
215 char filename[MAX_PATH];
217 snprintf(filename,sizeof(filename),"%s",firmware);
218 fd = open(filename, O_RDONLY);
219 if(fd < 0)
221 return EFILE_NOT_FOUND;
224 len = filesize(fd);
226 if (len > buffer_size)
227 return EFILE_TOO_BIG;
229 rc = read(fd, buf, len);
230 if(rc < len)
231 return EREAD_IMAGE_FAILED;
233 close(fd);
234 return len;
237 /* These functions are present in the firmware library, but we reimplement
238 them here because the originals do a lot more than we want */
239 int dbg_ports(void)
241 return 0;
244 void mpeg_stop(void)
248 #ifdef ROCKBOX_HAS_LOGF /* Logf display helper for the bootloader */
250 #define LINES (LCD_HEIGHT/SYSFONT_HEIGHT)
251 #define COLUMNS ((LCD_WIDTH/SYSFONT_WIDTH) > MAX_LOGF_ENTRY ? \
252 MAX_LOGF_ENTRY : (LCD_WIDTH/SYSFONT_WIDTH))
254 #ifdef ONDA_VX747
255 #define LOGF_UP BUTTON_VOL_UP
256 #define LOGF_DOWN BUTTON_VOL_DOWN
257 #define LOGF_CLEAR BUTTON_MENU
258 #else
259 #warning No keymap defined for this target
260 #endif
262 void display_logf(void) /* Doesn't return! */
264 int i, index, button, user_index=0;
265 #ifdef HAVE_TOUCHSCREEN
266 int touch, prev_y=0;
267 #endif
268 char buffer[COLUMNS+1];
270 while(1)
272 index = logfindex + user_index;
274 lcd_clear_display();
275 for(i = LINES-1; i>=0; i--)
277 if(--index < 0)
279 if(logfwrap)
280 index = MAX_LOGF_LINES-1;
281 else
282 break; /* done */
285 memcpy(buffer, logfbuffer[index], COLUMNS);
287 if (logfbuffer[index][MAX_LOGF_ENTRY] == LOGF_TERMINATE_CONTINUE_LINE)
288 buffer[MAX_LOGF_ENTRY-1] = '>';
289 else if (logfbuffer[index][MAX_LOGF_ENTRY] == LOGF_TERMINATE_MULTI_LINE)
290 buffer[MAX_LOGF_ENTRY-1] = '\0';
292 buffer[COLUMNS] = '\0';
294 lcd_puts(0, i, buffer);
297 button = button_get(false);
298 if(button == SYS_USB_CONNECTED)
299 usb_acknowledge(SYS_USB_CONNECTED_ACK);
300 else if(button == SYS_USB_DISCONNECTED)
301 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
302 else if(button & LOGF_UP)
303 user_index++;
304 else if(button & LOGF_DOWN)
305 user_index--;
306 else if(button & LOGF_CLEAR)
307 user_index = 0;
308 #ifdef HAVE_TOUCHSCREEN
309 else if(button & BUTTON_TOUCHSCREEN)
311 touch = button_get_data();
313 if(button & BUTTON_REL)
314 prev_y = 0;
316 if(prev_y != 0)
317 user_index += (prev_y - (touch & 0xFFFF)) / SYSFONT_HEIGHT;
318 prev_y = touch & 0xFFFF;
320 #endif
322 lcd_update();
323 sleep(HZ/16);
326 #endif