keyboard: some random cleanup.
[kugel-rb.git] / bootloader / common.c
blobbfe44a2687a0e4e11b38db029087a3b28642a0c1
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(SAMSUNG_YH925) || defined(SAMSUNG_YH920) || defined(SAMSUNG_YH820) \
46 || defined(ONDA_VX747)
47 bool verbose = false;
48 #else
49 bool verbose = true;
50 #endif
52 int line = 0;
53 #ifdef HAVE_REMOTE_LCD
54 int remote_line = 0;
55 #endif
57 char printfbuf[256];
59 void reset_screen(void)
61 lcd_clear_display();
62 line = 0;
63 #ifdef HAVE_REMOTE_LCD
64 lcd_remote_clear_display();
65 remote_line = 0;
66 #endif
69 void printf(const char *format, ...)
71 int len;
72 unsigned char *ptr;
73 va_list ap;
74 va_start(ap, format);
76 ptr = printfbuf;
77 len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
78 va_end(ap);
80 lcd_puts(0, line++, ptr);
81 if (verbose)
82 lcd_update();
83 if(line >= LCD_HEIGHT/SYSFONT_HEIGHT)
84 line = 0;
85 #ifdef HAVE_REMOTE_LCD
86 lcd_remote_puts(0, remote_line++, ptr);
87 if (verbose)
88 lcd_remote_update();
89 if(remote_line >= LCD_REMOTE_HEIGHT/SYSFONT_HEIGHT)
90 remote_line = 0;
91 #endif
94 char *strerror(int error)
96 switch(error)
98 case EOK:
99 return "OK";
100 case EFILE_NOT_FOUND:
101 return "File not found";
102 case EREAD_CHKSUM_FAILED:
103 return "Read failed (chksum)";
104 case EREAD_MODEL_FAILED:
105 return "Read failed (model)";
106 case EREAD_IMAGE_FAILED:
107 return "Read failed (image)";
108 case EBAD_CHKSUM:
109 return "Bad checksum";
110 case EFILE_TOO_BIG:
111 return "File too big";
112 case EINVALID_FORMAT:
113 return "Invalid file format";
114 default:
115 return "Unknown";
119 void error(int errortype, int error)
121 switch(errortype)
123 case EATA:
124 printf("ATA error: %d", error);
125 break;
127 case EDISK:
128 printf("No partition found");
129 break;
131 case EBOOTFILE:
132 printf(strerror(error));
133 break;
136 lcd_update();
137 sleep(5*HZ);
138 power_off();
141 /* Load firmware image in a format created by tools/scramble */
142 int load_firmware(unsigned char* buf, char* firmware, int buffer_size)
144 int fd;
145 int rc;
146 int len;
147 unsigned long chksum;
148 char model[5];
149 unsigned long sum;
150 int i;
151 char filename[MAX_PATH];
153 snprintf(filename,sizeof(filename), BOOTDIR "/%s",firmware);
154 fd = open(filename, O_RDONLY);
155 if(fd < 0)
157 snprintf(filename,sizeof(filename),"/%s",firmware);
158 fd = open(filename, O_RDONLY);
159 if(fd < 0)
160 return EFILE_NOT_FOUND;
163 len = filesize(fd) - 8;
165 printf("Length: %x", len);
167 if (len > buffer_size)
168 return EFILE_TOO_BIG;
170 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
172 rc = read(fd, &chksum, 4);
173 chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
174 if(rc < 4)
175 return EREAD_CHKSUM_FAILED;
177 printf("Checksum: %x", chksum);
179 rc = read(fd, model, 4);
180 if(rc < 4)
181 return EREAD_MODEL_FAILED;
183 model[4] = 0;
185 printf("Model name: %s", model);
186 printf("Loading %s", firmware);
188 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
190 rc = read(fd, buf, len);
191 if(rc < len)
192 return EREAD_IMAGE_FAILED;
194 close(fd);
196 sum = MODEL_NUMBER;
198 for(i = 0;i < len;i++) {
199 sum += buf[i];
202 printf("Sum: %x", sum);
204 if(sum != chksum)
205 return EBAD_CHKSUM;
207 return EOK;
210 /* Load raw binary image. */
211 int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size)
213 int fd;
214 int rc;
215 int len;
216 char filename[MAX_PATH];
218 snprintf(filename,sizeof(filename),"%s",firmware);
219 fd = open(filename, O_RDONLY);
220 if(fd < 0)
222 return EFILE_NOT_FOUND;
225 len = filesize(fd);
227 if (len > buffer_size)
228 return EFILE_TOO_BIG;
230 rc = read(fd, buf, len);
231 if(rc < len)
232 return EREAD_IMAGE_FAILED;
234 close(fd);
235 return len;
238 /* These functions are present in the firmware library, but we reimplement
239 them here because the originals do a lot more than we want */
240 int dbg_ports(void)
242 return 0;
245 void mpeg_stop(void)
249 #ifdef ROCKBOX_HAS_LOGF /* Logf display helper for the bootloader */
251 #define LINES (LCD_HEIGHT/SYSFONT_HEIGHT)
252 #define COLUMNS ((LCD_WIDTH/SYSFONT_WIDTH) > MAX_LOGF_ENTRY ? \
253 MAX_LOGF_ENTRY : (LCD_WIDTH/SYSFONT_WIDTH))
255 #ifdef ONDA_VX747
256 #define LOGF_UP BUTTON_VOL_UP
257 #define LOGF_DOWN BUTTON_VOL_DOWN
258 #define LOGF_CLEAR BUTTON_MENU
259 #else
260 #warning No keymap defined for this target
261 #endif
263 void display_logf(void) /* Doesn't return! */
265 int i, index, button, user_index=0;
266 #ifdef HAVE_TOUCHSCREEN
267 int touch, prev_y=0;
268 #endif
269 char buffer[COLUMNS+1];
271 while(1)
273 index = logfindex + user_index;
275 lcd_clear_display();
276 for(i = LINES-1; i>=0; i--)
278 if(--index < 0)
280 if(logfwrap)
281 index = MAX_LOGF_LINES-1;
282 else
283 break; /* done */
286 memcpy(buffer, logfbuffer[index], COLUMNS);
288 if (logfbuffer[index][MAX_LOGF_ENTRY] == LOGF_TERMINATE_CONTINUE_LINE)
289 buffer[MAX_LOGF_ENTRY-1] = '>';
290 else if (logfbuffer[index][MAX_LOGF_ENTRY] == LOGF_TERMINATE_MULTI_LINE)
291 buffer[MAX_LOGF_ENTRY-1] = '\0';
293 buffer[COLUMNS] = '\0';
295 lcd_puts(0, i, buffer);
298 button = button_get(false);
299 if(button == SYS_USB_CONNECTED)
300 usb_acknowledge(SYS_USB_CONNECTED_ACK);
301 else if(button == SYS_USB_DISCONNECTED)
302 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
303 else if(button & LOGF_UP)
304 user_index++;
305 else if(button & LOGF_DOWN)
306 user_index--;
307 else if(button & LOGF_CLEAR)
308 user_index = 0;
309 #ifdef HAVE_TOUCHSCREEN
310 else if(button & BUTTON_TOUCHSCREEN)
312 touch = button_get_data();
314 if(button & BUTTON_REL)
315 prev_y = 0;
317 if(prev_y != 0)
318 user_index += (prev_y - (touch & 0xFFFF)) / SYSFONT_HEIGHT;
319 prev_y = touch & 0xFFFF;
321 #endif
323 lcd_update();
324 sleep(HZ/16);
327 #endif