Minor corrections to the .colours file editing; added .colours to the list of support...
[kugel-rb.git] / bootloader / common.c
blob7159faaa8f69c5b866b9cef8c29f84d7fb752b1e
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(ONDA_VX747)
44 bool verbose = false;
45 #else
46 bool verbose = true;
47 #endif
49 int line = 0;
50 #ifdef HAVE_REMOTE_LCD
51 int remote_line = 0;
52 #endif
54 char printfbuf[256];
56 void reset_screen(void)
58 lcd_clear_display();
59 line = 0;
60 #ifdef HAVE_REMOTE_LCD
61 lcd_remote_clear_display();
62 remote_line = 0;
63 #endif
66 void printf(const char *format, ...)
68 int len;
69 unsigned char *ptr;
70 va_list ap;
71 va_start(ap, format);
73 ptr = printfbuf;
74 len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
75 va_end(ap);
77 lcd_puts(0, line++, ptr);
78 if (verbose)
79 lcd_update();
80 if(line >= LCD_HEIGHT/SYSFONT_HEIGHT)
81 line = 0;
82 #ifdef HAVE_REMOTE_LCD
83 lcd_remote_puts(0, remote_line++, ptr);
84 if (verbose)
85 lcd_remote_update();
86 if(remote_line >= LCD_REMOTE_HEIGHT/SYSFONT_HEIGHT)
87 remote_line = 0;
88 #endif
91 char *strerror(int error)
93 switch(error)
95 case EOK:
96 return "OK";
97 case EFILE_NOT_FOUND:
98 return "File not found";
99 case EREAD_CHKSUM_FAILED:
100 return "Read failed (chksum)";
101 case EREAD_MODEL_FAILED:
102 return "Read failed (model)";
103 case EREAD_IMAGE_FAILED:
104 return "Read failed (image)";
105 case EBAD_CHKSUM:
106 return "Bad checksum";
107 case EFILE_TOO_BIG:
108 return "File too big";
109 case EINVALID_FORMAT:
110 return "Invalid file format";
111 default:
112 return "Unknown";
116 void error(int errortype, int error)
118 switch(errortype)
120 case EATA:
121 printf("ATA error: %d", error);
122 break;
124 case EDISK:
125 printf("No partition found");
126 break;
128 case EBOOTFILE:
129 printf(strerror(error));
130 break;
133 lcd_update();
134 sleep(5*HZ);
135 power_off();
138 /* Load firmware image in a format created by tools/scramble */
139 int load_firmware(unsigned char* buf, char* firmware, int buffer_size)
141 int fd;
142 int rc;
143 int len;
144 unsigned long chksum;
145 char model[5];
146 unsigned long sum;
147 int i;
148 char filename[MAX_PATH];
150 snprintf(filename,sizeof(filename), BOOTDIR "/%s",firmware);
151 fd = open(filename, O_RDONLY);
152 if(fd < 0)
154 snprintf(filename,sizeof(filename),"/%s",firmware);
155 fd = open(filename, O_RDONLY);
156 if(fd < 0)
157 return EFILE_NOT_FOUND;
160 len = filesize(fd) - 8;
162 printf("Length: %x", len);
164 if (len > buffer_size)
165 return EFILE_TOO_BIG;
167 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
169 rc = read(fd, &chksum, 4);
170 chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
171 if(rc < 4)
172 return EREAD_CHKSUM_FAILED;
174 printf("Checksum: %x", chksum);
176 rc = read(fd, model, 4);
177 if(rc < 4)
178 return EREAD_MODEL_FAILED;
180 model[4] = 0;
182 printf("Model name: %s", model);
183 printf("Loading %s", firmware);
185 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
187 rc = read(fd, buf, len);
188 if(rc < len)
189 return EREAD_IMAGE_FAILED;
191 close(fd);
193 sum = MODEL_NUMBER;
195 for(i = 0;i < len;i++) {
196 sum += buf[i];
199 printf("Sum: %x", sum);
201 if(sum != chksum)
202 return EBAD_CHKSUM;
204 return EOK;
207 /* Load raw binary image. */
208 int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size)
210 int fd;
211 int rc;
212 int len;
213 char filename[MAX_PATH];
215 snprintf(filename,sizeof(filename),"%s",firmware);
216 fd = open(filename, O_RDONLY);
217 if(fd < 0)
219 return EFILE_NOT_FOUND;
222 len = filesize(fd);
224 if (len > buffer_size)
225 return EFILE_TOO_BIG;
227 rc = read(fd, buf, len);
228 if(rc < len)
229 return EREAD_IMAGE_FAILED;
231 close(fd);
232 return len;
235 /* These functions are present in the firmware library, but we reimplement
236 them here because the originals do a lot more than we want */
237 int dbg_ports(void)
239 return 0;
242 void mpeg_stop(void)
246 #ifdef ROCKBOX_HAS_LOGF /* Logf display helper for the bootloader */
248 #define LINES (LCD_HEIGHT/SYSFONT_HEIGHT)
249 #define COLUMNS ((LCD_WIDTH/SYSFONT_WIDTH) > MAX_LOGF_ENTRY ? \
250 MAX_LOGF_ENTRY : (LCD_WIDTH/SYSFONT_WIDTH))
252 #ifdef ONDA_VX747
253 #define LOGF_UP BUTTON_VOL_UP
254 #define LOGF_DOWN BUTTON_VOL_DOWN
255 #define LOGF_CLEAR BUTTON_MENU
256 #else
257 #warning No keymap defined for this target
258 #endif
260 void display_logf(void) /* Doesn't return! */
262 int i, index, button, user_index=0;
263 #ifdef HAVE_TOUCHSCREEN
264 int touch, prev_y=0;
265 #endif
266 char buffer[COLUMNS+1];
268 while(1)
270 index = logfindex + user_index;
272 lcd_clear_display();
273 for(i = LINES-1; i>=0; i--)
275 if(--index < 0)
277 if(logfwrap)
278 index = MAX_LOGF_LINES-1;
279 else
280 break; /* done */
283 memcpy(buffer, logfbuffer[index], COLUMNS);
285 if (logfbuffer[index][MAX_LOGF_ENTRY] == LOGF_TERMINATE_CONTINUE_LINE)
286 buffer[MAX_LOGF_ENTRY-1] = '>';
287 else if (logfbuffer[index][MAX_LOGF_ENTRY] == LOGF_TERMINATE_MULTI_LINE)
288 buffer[MAX_LOGF_ENTRY-1] = '\0';
290 buffer[COLUMNS] = '\0';
292 lcd_puts(0, i, buffer);
295 button = button_get(false);
296 if(button == SYS_USB_CONNECTED)
297 usb_acknowledge(SYS_USB_CONNECTED_ACK);
298 else if(button == SYS_USB_DISCONNECTED)
299 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
300 else if(button & LOGF_UP)
301 user_index++;
302 else if(button & LOGF_DOWN)
303 user_index--;
304 else if(button & LOGF_CLEAR)
305 user_index = 0;
306 #ifdef HAVE_TOUCHSCREEN
307 else if(button & BUTTON_TOUCHSCREEN)
309 touch = button_get_data();
311 if(button & BUTTON_REL)
312 prev_y = 0;
314 if(prev_y != 0)
315 user_index += (prev_y - (touch & 0xFFFF)) / SYSFONT_HEIGHT;
316 prev_y = touch & 0xFFFF;
318 #endif
320 lcd_update();
321 sleep(HZ/16);
324 #endif