New committer!
[kugel-rb.git] / bootloader / common.c
blobf7ab661ca214773ad974168a3f5721cd6ee97704
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"
38 /* TODO: Other bootloaders need to be adjusted to set this variable to true
39 on a button press - currently only the ipod, H10 and Sansa versions do. */
40 #if defined(IPOD_ARCH) || defined(IRIVER_H10) || defined(IRIVER_H10_5GB) \
41 || defined(SANSA_E200) || defined(SANSA_C200) || defined(GIGABEAT_F) \
42 || defined(PHILIPS_SA9200) || (CONFIG_CPU == AS3525) || defined(COWON_D2) \
43 || defined(MROBE_100) || defined(PHILIPS_HDD1630) || defined(MROBE_500) \
44 || defined(ONDA_VX747)
45 bool verbose = false;
46 #else
47 bool verbose = true;
48 #endif
50 int line = 0;
51 #ifdef HAVE_REMOTE_LCD
52 int remote_line = 0;
53 #endif
55 char printfbuf[256];
57 void reset_screen(void)
59 lcd_clear_display();
60 line = 0;
61 #ifdef HAVE_REMOTE_LCD
62 lcd_remote_clear_display();
63 remote_line = 0;
64 #endif
67 void printf(const char *format, ...)
69 int len;
70 unsigned char *ptr;
71 va_list ap;
72 va_start(ap, format);
74 ptr = printfbuf;
75 len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
76 va_end(ap);
78 lcd_puts(0, line++, ptr);
79 if (verbose)
80 lcd_update();
81 if(line >= LCD_HEIGHT/SYSFONT_HEIGHT)
82 line = 0;
83 #ifdef HAVE_REMOTE_LCD
84 lcd_remote_puts(0, remote_line++, ptr);
85 if (verbose)
86 lcd_remote_update();
87 if(remote_line >= LCD_REMOTE_HEIGHT/SYSFONT_HEIGHT)
88 remote_line = 0;
89 #endif
92 char *strerror(int error)
94 switch(error)
96 case EOK:
97 return "OK";
98 case EFILE_NOT_FOUND:
99 return "File not found";
100 case EREAD_CHKSUM_FAILED:
101 return "Read failed (chksum)";
102 case EREAD_MODEL_FAILED:
103 return "Read failed (model)";
104 case EREAD_IMAGE_FAILED:
105 return "Read failed (image)";
106 case EBAD_CHKSUM:
107 return "Bad checksum";
108 case EFILE_TOO_BIG:
109 return "File too big";
110 case EINVALID_FORMAT:
111 return "Invalid file format";
112 default:
113 return "Unknown";
117 void error(int errortype, int error)
119 switch(errortype)
121 case EATA:
122 printf("ATA error: %d", error);
123 break;
125 case EDISK:
126 printf("No partition found");
127 break;
129 case EBOOTFILE:
130 printf(strerror(error));
131 break;
134 lcd_update();
135 sleep(5*HZ);
136 power_off();
139 /* Load firmware image in a format created by tools/scramble */
140 int load_firmware(unsigned char* buf, char* firmware, int buffer_size)
142 int fd;
143 int rc;
144 int len;
145 unsigned long chksum;
146 char model[5];
147 unsigned long sum;
148 int i;
149 char filename[MAX_PATH];
151 snprintf(filename,sizeof(filename), BOOTDIR "/%s",firmware);
152 fd = open(filename, O_RDONLY);
153 if(fd < 0)
155 snprintf(filename,sizeof(filename),"/%s",firmware);
156 fd = open(filename, O_RDONLY);
157 if(fd < 0)
158 return EFILE_NOT_FOUND;
161 len = filesize(fd) - 8;
163 printf("Length: %x", len);
165 if (len > buffer_size)
166 return EFILE_TOO_BIG;
168 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
170 rc = read(fd, &chksum, 4);
171 chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
172 if(rc < 4)
173 return EREAD_CHKSUM_FAILED;
175 printf("Checksum: %x", chksum);
177 rc = read(fd, model, 4);
178 if(rc < 4)
179 return EREAD_MODEL_FAILED;
181 model[4] = 0;
183 printf("Model name: %s", model);
184 printf("Loading %s", firmware);
186 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
188 rc = read(fd, buf, len);
189 if(rc < len)
190 return EREAD_IMAGE_FAILED;
192 close(fd);
194 sum = MODEL_NUMBER;
196 for(i = 0;i < len;i++) {
197 sum += buf[i];
200 printf("Sum: %x", sum);
202 if(sum != chksum)
203 return EBAD_CHKSUM;
205 return EOK;
208 /* Load raw binary image. */
209 int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size)
211 int fd;
212 int rc;
213 int len;
214 char filename[MAX_PATH];
216 snprintf(filename,sizeof(filename),"%s",firmware);
217 fd = open(filename, O_RDONLY);
218 if(fd < 0)
220 return EFILE_NOT_FOUND;
223 len = filesize(fd);
225 if (len > buffer_size)
226 return EFILE_TOO_BIG;
228 rc = read(fd, buf, len);
229 if(rc < len)
230 return EREAD_IMAGE_FAILED;
232 close(fd);
233 return len;
236 /* These functions are present in the firmware library, but we reimplement
237 them here because the originals do a lot more than we want */
238 int dbg_ports(void)
240 return 0;
243 void mpeg_stop(void)
247 #ifdef ROCKBOX_HAS_LOGF /* Logf display helper for the bootloader */
249 #define LINES (LCD_HEIGHT/SYSFONT_HEIGHT)
250 #define COLUMNS ((LCD_WIDTH/SYSFONT_WIDTH) > MAX_LOGF_ENTRY ? \
251 MAX_LOGF_ENTRY : (LCD_WIDTH/SYSFONT_WIDTH))
253 #ifdef ONDA_VX747
254 #define LOGF_UP BUTTON_VOL_UP
255 #define LOGF_DOWN BUTTON_VOL_DOWN
256 #define LOGF_CLEAR BUTTON_MENU
257 #else
258 #warning No keymap defined for this target
259 #endif
261 void display_logf(void) /* Doesn't return! */
263 int i, index, button, user_index=0;
264 #ifdef HAVE_TOUCHSCREEN
265 int touch, prev_y=0;
266 #endif
267 char buffer[COLUMNS+1];
269 while(1)
271 index = logfindex + user_index;
273 lcd_clear_display();
274 for(i = LINES-1; i>=0; i--)
276 if(--index < 0)
278 if(logfwrap)
279 index = MAX_LOGF_LINES-1;
280 else
281 break; /* done */
284 memcpy(buffer, logfbuffer[index], COLUMNS);
286 if (logfbuffer[index][MAX_LOGF_ENTRY] == LOGF_TERMINATE_CONTINUE_LINE)
287 buffer[MAX_LOGF_ENTRY-1] = '>';
288 else if (logfbuffer[index][MAX_LOGF_ENTRY] == LOGF_TERMINATE_MULTI_LINE)
289 buffer[MAX_LOGF_ENTRY-1] = '\0';
291 buffer[COLUMNS] = '\0';
293 lcd_puts(0, i, buffer);
296 button = button_get(false);
297 if(button == SYS_USB_CONNECTED)
298 usb_acknowledge(SYS_USB_CONNECTED_ACK);
299 else if(button == SYS_USB_DISCONNECTED)
300 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
301 else if(button & LOGF_UP)
302 user_index++;
303 else if(button & LOGF_DOWN)
304 user_index--;
305 else if(button & LOGF_CLEAR)
306 user_index = 0;
307 #ifdef HAVE_TOUCHSCREEN
308 else if(button & BUTTON_TOUCHSCREEN)
310 touch = button_get_data();
312 if(button & BUTTON_REL)
313 prev_y = 0;
315 if(prev_y != 0)
316 user_index += (prev_y - (touch & 0xFFFF)) / SYSFONT_HEIGHT;
317 prev_y = touch & 0xFFFF;
319 #endif
321 lcd_update();
322 sleep(HZ/16);
325 #endif