as3525v1 use an as3515 audio codec/PMU, not an as3517 as previously thought
[kugel-rb.git] / bootloader / common.c
blob67b581669439a1056b90bb6b59c5d7d929f678ad
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 bool verbose = false;
50 #else
51 bool verbose = true;
52 #endif
54 int line = 0;
55 #ifdef HAVE_REMOTE_LCD
56 int remote_line = 0;
57 #endif
59 char printfbuf[256];
61 void reset_screen(void)
63 lcd_clear_display();
64 line = 0;
65 #ifdef HAVE_REMOTE_LCD
66 lcd_remote_clear_display();
67 remote_line = 0;
68 #endif
71 void printf(const char *format, ...)
73 int len;
74 unsigned char *ptr;
75 va_list ap;
76 va_start(ap, format);
78 ptr = printfbuf;
79 len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
80 va_end(ap);
82 lcd_puts(0, line++, ptr);
83 if (verbose)
84 lcd_update();
85 if(line >= LCD_HEIGHT/SYSFONT_HEIGHT)
86 line = 0;
87 #ifdef HAVE_REMOTE_LCD
88 lcd_remote_puts(0, remote_line++, ptr);
89 if (verbose)
90 lcd_remote_update();
91 if(remote_line >= LCD_REMOTE_HEIGHT/SYSFONT_HEIGHT)
92 remote_line = 0;
93 #endif
96 char *strerror(int error)
98 switch(error)
100 case EOK:
101 return "OK";
102 case EFILE_NOT_FOUND:
103 return "File not found";
104 case EREAD_CHKSUM_FAILED:
105 return "Read failed (chksum)";
106 case EREAD_MODEL_FAILED:
107 return "Read failed (model)";
108 case EREAD_IMAGE_FAILED:
109 return "Read failed (image)";
110 case EBAD_CHKSUM:
111 return "Bad checksum";
112 case EFILE_TOO_BIG:
113 return "File too big";
114 case EINVALID_FORMAT:
115 return "Invalid file format";
116 default:
117 return "Unknown";
121 void error(int errortype, int error)
123 switch(errortype)
125 case EATA:
126 printf("ATA error: %d", error);
127 break;
129 case EDISK:
130 printf("No partition found");
131 break;
133 case EBOOTFILE:
134 printf(strerror(error));
135 break;
138 lcd_update();
139 sleep(5*HZ);
140 power_off();
143 /* Load firmware image in a format created by tools/scramble */
144 int load_firmware(unsigned char* buf, char* firmware, int buffer_size)
146 int fd;
147 int rc;
148 int len;
149 unsigned long chksum;
150 char model[5];
151 unsigned long sum;
152 int i;
153 char filename[MAX_PATH];
155 snprintf(filename,sizeof(filename), BOOTDIR "/%s",firmware);
156 fd = open(filename, O_RDONLY);
157 if(fd < 0)
159 snprintf(filename,sizeof(filename),"/%s",firmware);
160 fd = open(filename, O_RDONLY);
161 if(fd < 0)
162 return EFILE_NOT_FOUND;
165 len = filesize(fd) - 8;
167 printf("Length: %x", len);
169 if (len > buffer_size)
170 return EFILE_TOO_BIG;
172 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
174 rc = read(fd, &chksum, 4);
175 chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
176 if(rc < 4)
177 return EREAD_CHKSUM_FAILED;
179 printf("Checksum: %x", chksum);
181 rc = read(fd, model, 4);
182 if(rc < 4)
183 return EREAD_MODEL_FAILED;
185 model[4] = 0;
187 printf("Model name: %s", model);
188 printf("Loading %s", firmware);
190 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
192 rc = read(fd, buf, len);
193 if(rc < len)
194 return EREAD_IMAGE_FAILED;
196 close(fd);
198 sum = MODEL_NUMBER;
200 for(i = 0;i < len;i++) {
201 sum += buf[i];
204 printf("Sum: %x", sum);
206 if(sum != chksum)
207 return EBAD_CHKSUM;
209 return EOK;
212 /* Load raw binary image. */
213 int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size)
215 int fd;
216 int rc;
217 int len;
218 char filename[MAX_PATH];
220 snprintf(filename,sizeof(filename),"%s",firmware);
221 fd = open(filename, O_RDONLY);
222 if(fd < 0)
224 return EFILE_NOT_FOUND;
227 len = filesize(fd);
229 if (len > buffer_size)
230 return EFILE_TOO_BIG;
232 rc = read(fd, buf, len);
233 if(rc < len)
234 return EREAD_IMAGE_FAILED;
236 close(fd);
237 return len;
240 /* These functions are present in the firmware library, but we reimplement
241 them here because the originals do a lot more than we want */
242 int dbg_ports(void)
244 return 0;
247 void mpeg_stop(void)
251 #ifdef ROCKBOX_HAS_LOGF /* Logf display helper for the bootloader */
253 #define LINES (LCD_HEIGHT/SYSFONT_HEIGHT)
254 #define COLUMNS ((LCD_WIDTH/SYSFONT_WIDTH) > MAX_LOGF_ENTRY ? \
255 MAX_LOGF_ENTRY : (LCD_WIDTH/SYSFONT_WIDTH))
257 #ifdef ONDA_VX747
258 #define LOGF_UP BUTTON_VOL_UP
259 #define LOGF_DOWN BUTTON_VOL_DOWN
260 #define LOGF_CLEAR BUTTON_MENU
261 #else
262 #warning No keymap defined for this target
263 #endif
265 void display_logf(void) /* Doesn't return! */
267 int i, index, button, user_index=0;
268 #ifdef HAVE_TOUCHSCREEN
269 int touch, prev_y=0;
270 #endif
271 char buffer[COLUMNS+1];
273 while(1)
275 index = logfindex + user_index;
277 lcd_clear_display();
278 for(i = LINES-1; i>=0; i--)
280 if(--index < 0)
282 if(logfwrap)
283 index = MAX_LOGF_LINES-1;
284 else
285 break; /* done */
288 memcpy(buffer, logfbuffer[index], COLUMNS);
290 if (logfbuffer[index][MAX_LOGF_ENTRY] == LOGF_TERMINATE_CONTINUE_LINE)
291 buffer[MAX_LOGF_ENTRY-1] = '>';
292 else if (logfbuffer[index][MAX_LOGF_ENTRY] == LOGF_TERMINATE_MULTI_LINE)
293 buffer[MAX_LOGF_ENTRY-1] = '\0';
295 buffer[COLUMNS] = '\0';
297 lcd_puts(0, i, buffer);
300 button = button_get(false);
301 if(button == SYS_USB_CONNECTED)
302 usb_acknowledge(SYS_USB_CONNECTED_ACK);
303 else if(button == SYS_USB_DISCONNECTED)
304 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
305 else if(button & LOGF_UP)
306 user_index++;
307 else if(button & LOGF_DOWN)
308 user_index--;
309 else if(button & LOGF_CLEAR)
310 user_index = 0;
311 #ifdef HAVE_TOUCHSCREEN
312 else if(button & BUTTON_TOUCHSCREEN)
314 touch = button_get_data();
316 if(button & BUTTON_REL)
317 prev_y = 0;
319 if(prev_y != 0)
320 user_index += (prev_y - (touch & 0xFFFF)) / SYSFONT_HEIGHT;
321 prev_y = touch & 0xFFFF;
323 #endif
325 lcd_update();
326 sleep(HZ/16);
329 #endif