Merge branch 'master' into android-test-plugins
[kugel-rb.git] / bootloader / common.c
blob8fb7cdef36483e76914addac0389c3a3428168b2
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) || (CONFIG_CPU == AS3525v2) || 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(PBELL_VIBE500) \
49 || defined(TOSHIBA_GIGABEAT_S)
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 int 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
95 return len;
98 char *strerror(int error)
100 switch(error)
102 case EOK:
103 return "OK";
104 case EFILE_NOT_FOUND:
105 return "File not found";
106 case EREAD_CHKSUM_FAILED:
107 return "Read failed (chksum)";
108 case EREAD_MODEL_FAILED:
109 return "Read failed (model)";
110 case EREAD_IMAGE_FAILED:
111 return "Read failed (image)";
112 case EBAD_CHKSUM:
113 return "Bad checksum";
114 case EFILE_TOO_BIG:
115 return "File too big";
116 case EINVALID_FORMAT:
117 return "Invalid file format";
118 default:
119 return "Unknown";
123 void error(int errortype, int error, bool shutdown)
125 switch(errortype)
127 case EATA:
128 printf("ATA error: %d", error);
129 break;
131 case EDISK:
132 printf("No partition found");
133 break;
135 case EBOOTFILE:
136 printf(strerror(error));
137 break;
140 lcd_update();
141 sleep(5*HZ);
142 if(shutdown)
143 power_off();
146 /* Load firmware image in a format created by tools/scramble */
147 int load_firmware(unsigned char* buf, char* firmware, int buffer_size)
149 int fd;
150 int rc;
151 int len;
152 unsigned long chksum;
153 char model[5];
154 unsigned long sum;
155 int i;
156 char filename[MAX_PATH];
158 snprintf(filename,sizeof(filename), BOOTDIR "/%s",firmware);
159 fd = open(filename, O_RDONLY);
160 if(fd < 0)
162 snprintf(filename,sizeof(filename),"/%s",firmware);
163 fd = open(filename, O_RDONLY);
164 if(fd < 0)
165 return EFILE_NOT_FOUND;
168 len = filesize(fd) - 8;
170 printf("Length: %x", len);
172 if (len > buffer_size)
173 return EFILE_TOO_BIG;
175 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
177 rc = read(fd, &chksum, 4);
178 chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
179 if(rc < 4)
180 return EREAD_CHKSUM_FAILED;
182 printf("Checksum: %x", chksum);
184 rc = read(fd, model, 4);
185 if(rc < 4)
186 return EREAD_MODEL_FAILED;
188 model[4] = 0;
190 printf("Model name: %s", model);
191 printf("Loading %s", firmware);
193 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
195 rc = read(fd, buf, len);
196 if(rc < len)
197 return EREAD_IMAGE_FAILED;
199 close(fd);
201 sum = MODEL_NUMBER;
203 for(i = 0;i < len;i++) {
204 sum += buf[i];
207 printf("Sum: %x", sum);
209 if(sum != chksum)
210 return EBAD_CHKSUM;
212 return EOK;
215 /* Load raw binary image. */
216 int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size)
218 int fd;
219 int rc;
220 int len;
221 char filename[MAX_PATH];
223 snprintf(filename,sizeof(filename),"%s",firmware);
224 fd = open(filename, O_RDONLY);
225 if(fd < 0)
227 return EFILE_NOT_FOUND;
230 len = filesize(fd);
232 if (len > buffer_size)
233 return EFILE_TOO_BIG;
235 rc = read(fd, buf, len);
236 if(rc < len)
237 return EREAD_IMAGE_FAILED;
239 close(fd);
240 return len;
243 /* These functions are present in the firmware library, but we reimplement
244 them here because the originals do a lot more than we want */
245 int dbg_ports(void)
247 return 0;
250 void mpeg_stop(void)
254 #ifdef ROCKBOX_HAS_LOGF /* Logf display helper for the bootloader */
256 #define LINES (LCD_HEIGHT/SYSFONT_HEIGHT)
257 #define COLUMNS ((LCD_WIDTH/SYSFONT_WIDTH) > MAX_LOGF_ENTRY ? \
258 MAX_LOGF_ENTRY : (LCD_WIDTH/SYSFONT_WIDTH))
260 #ifdef ONDA_VX747
261 #define LOGF_UP BUTTON_VOL_UP
262 #define LOGF_DOWN BUTTON_VOL_DOWN
263 #define LOGF_CLEAR BUTTON_MENU
264 #else
265 #warning No keymap defined for this target
266 #endif
268 void display_logf(void) /* Doesn't return! */
270 int i, index, button, user_index=0;
271 #ifdef HAVE_TOUCHSCREEN
272 int touch, prev_y=0;
273 #endif
274 char buffer[COLUMNS+1];
276 while(1)
278 index = logfindex + user_index;
280 lcd_clear_display();
281 for(i = LINES-1; i>=0; i--)
283 if(--index < 0)
285 if(logfwrap)
286 index = MAX_LOGF_LINES-1;
287 else
288 break; /* done */
291 memcpy(buffer, logfbuffer[index], COLUMNS);
293 if (logfbuffer[index][MAX_LOGF_ENTRY] == LOGF_TERMINATE_CONTINUE_LINE)
294 buffer[MAX_LOGF_ENTRY-1] = '>';
295 else if (logfbuffer[index][MAX_LOGF_ENTRY] == LOGF_TERMINATE_MULTI_LINE)
296 buffer[MAX_LOGF_ENTRY-1] = '\0';
298 buffer[COLUMNS] = '\0';
300 lcd_puts(0, i, buffer);
303 button = button_get(false);
304 if(button == SYS_USB_CONNECTED)
305 usb_acknowledge(SYS_USB_CONNECTED_ACK);
306 else if(button == SYS_USB_DISCONNECTED)
308 else if(button & LOGF_UP)
309 user_index++;
310 else if(button & LOGF_DOWN)
311 user_index--;
312 else if(button & LOGF_CLEAR)
313 user_index = 0;
314 #ifdef HAVE_TOUCHSCREEN
315 else if(button & BUTTON_TOUCHSCREEN)
317 touch = button_get_data();
319 if(button & BUTTON_REL)
320 prev_y = 0;
322 if(prev_y != 0)
323 user_index += (prev_y - (touch & 0xFFFF)) / SYSFONT_HEIGHT;
324 prev_y = touch & 0xFFFF;
326 #endif
328 lcd_update();
329 sleep(HZ/16);
332 #endif