Packard Bell Vibe 500: remove the old bootloader code (actually a lack of code) and...
[kugel-rb.git] / bootloader / common.c
blob2a80f987ad33dc111d8db667209b237b4062f3eb
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, Vibe 500 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 || (CONFIG_CPU == AS3525) || defined(COWON_D2) \
44 || defined(MROBE_100) || defined(MROBE_500) \
45 || defined(SAMSUNG_YH925) || defined(SAMSUNG_YH920) \
46 || defined(SAMSUNG_YH820) || defined(PHILIPS_SA9200) \
47 || defined(PHILIPS_HDD1630) || defined(PHILIPS_HDD6330) \
48 || defined(ONDA_VX747) || defined(SANSA_CLIPPLUS) \
49 || defined(PBELL_VIBE500)
50 bool verbose = false;
51 #else
52 bool verbose = true;
53 #endif
55 int line = 0;
56 #ifdef HAVE_REMOTE_LCD
57 int remote_line = 0;
58 #endif
60 char printfbuf[256];
62 void reset_screen(void)
64 lcd_clear_display();
65 line = 0;
66 #ifdef HAVE_REMOTE_LCD
67 lcd_remote_clear_display();
68 remote_line = 0;
69 #endif
72 void printf(const char *format, ...)
74 int len;
75 unsigned char *ptr;
76 va_list ap;
77 va_start(ap, format);
79 ptr = printfbuf;
80 len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
81 va_end(ap);
83 lcd_puts(0, line++, ptr);
84 if (verbose)
85 lcd_update();
86 if(line >= LCD_HEIGHT/SYSFONT_HEIGHT)
87 line = 0;
88 #ifdef HAVE_REMOTE_LCD
89 lcd_remote_puts(0, remote_line++, ptr);
90 if (verbose)
91 lcd_remote_update();
92 if(remote_line >= LCD_REMOTE_HEIGHT/SYSFONT_HEIGHT)
93 remote_line = 0;
94 #endif
97 char *strerror(int error)
99 switch(error)
101 case EOK:
102 return "OK";
103 case EFILE_NOT_FOUND:
104 return "File not found";
105 case EREAD_CHKSUM_FAILED:
106 return "Read failed (chksum)";
107 case EREAD_MODEL_FAILED:
108 return "Read failed (model)";
109 case EREAD_IMAGE_FAILED:
110 return "Read failed (image)";
111 case EBAD_CHKSUM:
112 return "Bad checksum";
113 case EFILE_TOO_BIG:
114 return "File too big";
115 case EINVALID_FORMAT:
116 return "Invalid file format";
117 default:
118 return "Unknown";
122 void error(int errortype, int error)
124 switch(errortype)
126 case EATA:
127 printf("ATA error: %d", error);
128 break;
130 case EDISK:
131 printf("No partition found");
132 break;
134 case EBOOTFILE:
135 printf(strerror(error));
136 break;
139 lcd_update();
140 sleep(5*HZ);
141 power_off();
144 /* Load firmware image in a format created by tools/scramble */
145 int load_firmware(unsigned char* buf, char* firmware, int buffer_size)
147 int fd;
148 int rc;
149 int len;
150 unsigned long chksum;
151 char model[5];
152 unsigned long sum;
153 int i;
154 char filename[MAX_PATH];
156 snprintf(filename,sizeof(filename), BOOTDIR "/%s",firmware);
157 fd = open(filename, O_RDONLY);
158 if(fd < 0)
160 snprintf(filename,sizeof(filename),"/%s",firmware);
161 fd = open(filename, O_RDONLY);
162 if(fd < 0)
163 return EFILE_NOT_FOUND;
166 len = filesize(fd) - 8;
168 printf("Length: %x", len);
170 if (len > buffer_size)
171 return EFILE_TOO_BIG;
173 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
175 rc = read(fd, &chksum, 4);
176 chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
177 if(rc < 4)
178 return EREAD_CHKSUM_FAILED;
180 printf("Checksum: %x", chksum);
182 rc = read(fd, model, 4);
183 if(rc < 4)
184 return EREAD_MODEL_FAILED;
186 model[4] = 0;
188 printf("Model name: %s", model);
189 printf("Loading %s", firmware);
191 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
193 rc = read(fd, buf, len);
194 if(rc < len)
195 return EREAD_IMAGE_FAILED;
197 close(fd);
199 sum = MODEL_NUMBER;
201 for(i = 0;i < len;i++) {
202 sum += buf[i];
205 printf("Sum: %x", sum);
207 if(sum != chksum)
208 return EBAD_CHKSUM;
210 return EOK;
213 /* Load raw binary image. */
214 int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size)
216 int fd;
217 int rc;
218 int len;
219 char filename[MAX_PATH];
221 snprintf(filename,sizeof(filename),"%s",firmware);
222 fd = open(filename, O_RDONLY);
223 if(fd < 0)
225 return EFILE_NOT_FOUND;
228 len = filesize(fd);
230 if (len > buffer_size)
231 return EFILE_TOO_BIG;
233 rc = read(fd, buf, len);
234 if(rc < len)
235 return EREAD_IMAGE_FAILED;
237 close(fd);
238 return len;
241 /* These functions are present in the firmware library, but we reimplement
242 them here because the originals do a lot more than we want */
243 int dbg_ports(void)
245 return 0;
248 void mpeg_stop(void)
252 #ifdef ROCKBOX_HAS_LOGF /* Logf display helper for the bootloader */
254 #define LINES (LCD_HEIGHT/SYSFONT_HEIGHT)
255 #define COLUMNS ((LCD_WIDTH/SYSFONT_WIDTH) > MAX_LOGF_ENTRY ? \
256 MAX_LOGF_ENTRY : (LCD_WIDTH/SYSFONT_WIDTH))
258 #ifdef ONDA_VX747
259 #define LOGF_UP BUTTON_VOL_UP
260 #define LOGF_DOWN BUTTON_VOL_DOWN
261 #define LOGF_CLEAR BUTTON_MENU
262 #else
263 #warning No keymap defined for this target
264 #endif
266 void display_logf(void) /* Doesn't return! */
268 int i, index, button, user_index=0;
269 #ifdef HAVE_TOUCHSCREEN
270 int touch, prev_y=0;
271 #endif
272 char buffer[COLUMNS+1];
274 while(1)
276 index = logfindex + user_index;
278 lcd_clear_display();
279 for(i = LINES-1; i>=0; i--)
281 if(--index < 0)
283 if(logfwrap)
284 index = MAX_LOGF_LINES-1;
285 else
286 break; /* done */
289 memcpy(buffer, logfbuffer[index], COLUMNS);
291 if (logfbuffer[index][MAX_LOGF_ENTRY] == LOGF_TERMINATE_CONTINUE_LINE)
292 buffer[MAX_LOGF_ENTRY-1] = '>';
293 else if (logfbuffer[index][MAX_LOGF_ENTRY] == LOGF_TERMINATE_MULTI_LINE)
294 buffer[MAX_LOGF_ENTRY-1] = '\0';
296 buffer[COLUMNS] = '\0';
298 lcd_puts(0, i, buffer);
301 button = button_get(false);
302 if(button == SYS_USB_CONNECTED)
303 usb_acknowledge(SYS_USB_CONNECTED_ACK);
304 else if(button == SYS_USB_DISCONNECTED)
305 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
306 else if(button & LOGF_UP)
307 user_index++;
308 else if(button & LOGF_DOWN)
309 user_index--;
310 else if(button & LOGF_CLEAR)
311 user_index = 0;
312 #ifdef HAVE_TOUCHSCREEN
313 else if(button & BUTTON_TOUCHSCREEN)
315 touch = button_get_data();
317 if(button & BUTTON_REL)
318 prev_y = 0;
320 if(prev_y != 0)
321 user_index += (prev_y - (touch & 0xFFFF)) / SYSFONT_HEIGHT;
322 prev_y = touch & 0xFFFF;
324 #endif
326 lcd_update();
327 sleep(HZ/16);
330 #endif