Remove .a files before running ar, to avoid problems with renamed files remaining...
[kugel-rb.git] / bootloader / common.c
blobae6003cddf0ab7f8f8a3e2749a791ddd6bbdb078
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"
33 /* TODO: Other bootloaders need to be adjusted to set this variable to true
34 on a button press - currently only the ipod, H10 and Sansa versions do. */
35 #if defined(IPOD_ARCH) || defined(IRIVER_H10) || defined(IRIVER_H10_5GB) \
36 || defined(SANSA_E200) || defined(SANSA_C200) || defined(GIGABEAT_F) \
37 || defined(PHILIPS_SA9200) || (CONFIG_CPU == AS3525) || defined(COWON_D2)
38 bool verbose = false;
39 #else
40 bool verbose = true;
41 #endif
43 int line = 0;
44 #ifdef HAVE_REMOTE_LCD
45 int remote_line = 0;
46 #endif
48 char printfbuf[256];
50 void reset_screen(void)
52 lcd_clear_display();
53 line = 0;
54 #ifdef HAVE_REMOTE_LCD
55 lcd_remote_clear_display();
56 remote_line = 0;
57 #endif
60 void printf(const char *format, ...)
62 int len;
63 unsigned char *ptr;
64 va_list ap;
65 va_start(ap, format);
67 ptr = printfbuf;
68 len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
69 va_end(ap);
71 lcd_puts(0, line++, ptr);
72 if (verbose)
73 lcd_update();
74 if(line >= LCD_HEIGHT/SYSFONT_HEIGHT)
75 line = 0;
76 #ifdef HAVE_REMOTE_LCD
77 lcd_remote_puts(0, remote_line++, ptr);
78 if (verbose)
79 lcd_remote_update();
80 if(remote_line >= LCD_REMOTE_HEIGHT/SYSFONT_HEIGHT)
81 remote_line = 0;
82 #endif
85 char *strerror(int error)
87 switch(error)
89 case EOK:
90 return "OK";
91 case EFILE_NOT_FOUND:
92 return "File not found";
93 case EREAD_CHKSUM_FAILED:
94 return "Read failed (chksum)";
95 case EREAD_MODEL_FAILED:
96 return "Read failed (model)";
97 case EREAD_IMAGE_FAILED:
98 return "Read failed (image)";
99 case EBAD_CHKSUM:
100 return "Bad checksum";
101 case EFILE_TOO_BIG:
102 return "File too big";
103 case EINVALID_FORMAT:
104 return "Invalid file format";
105 default:
106 return "Unknown";
110 void error(int errortype, int error)
112 switch(errortype)
114 case EATA:
115 printf("ATA error: %d", error);
116 break;
118 case EDISK:
119 printf("No partition found");
120 break;
122 case EBOOTFILE:
123 printf(strerror(error));
124 break;
127 lcd_update();
128 sleep(5*HZ);
129 power_off();
132 /* Load firmware image in a format created by tools/scramble */
133 int load_firmware(unsigned char* buf, char* firmware, int buffer_size)
135 int fd;
136 int rc;
137 int len;
138 unsigned long chksum;
139 char model[5];
140 unsigned long sum;
141 int i;
142 char filename[MAX_PATH];
144 snprintf(filename,sizeof(filename),"/.rockbox/%s",firmware);
145 fd = open(filename, O_RDONLY);
146 if(fd < 0)
148 snprintf(filename,sizeof(filename),"/%s",firmware);
149 fd = open(filename, O_RDONLY);
150 if(fd < 0)
151 return EFILE_NOT_FOUND;
154 len = filesize(fd) - 8;
156 printf("Length: %x", len);
158 if (len > buffer_size)
159 return EFILE_TOO_BIG;
161 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
163 rc = read(fd, &chksum, 4);
164 chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
165 if(rc < 4)
166 return EREAD_CHKSUM_FAILED;
168 printf("Checksum: %x", chksum);
170 rc = read(fd, model, 4);
171 if(rc < 4)
172 return EREAD_MODEL_FAILED;
174 model[4] = 0;
176 printf("Model name: %s", model);
177 printf("Loading %s", firmware);
179 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
181 rc = read(fd, buf, len);
182 if(rc < len)
183 return EREAD_IMAGE_FAILED;
185 close(fd);
187 sum = MODEL_NUMBER;
189 for(i = 0;i < len;i++) {
190 sum += buf[i];
193 printf("Sum: %x", sum);
195 if(sum != chksum)
196 return EBAD_CHKSUM;
198 return EOK;
201 /* Load raw binary image. */
202 int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size)
204 int fd;
205 int rc;
206 int len;
207 char filename[MAX_PATH];
209 snprintf(filename,sizeof(filename),"%s",firmware);
210 fd = open(filename, O_RDONLY);
211 if(fd < 0)
213 return EFILE_NOT_FOUND;
216 len = filesize(fd);
218 if (len > buffer_size)
219 return EFILE_TOO_BIG;
221 rc = read(fd, buf, len);
222 if(rc < len)
223 return EREAD_IMAGE_FAILED;
225 close(fd);
226 return len;
229 /* These functions are present in the firmware library, but we reimplement
230 them here because the originals do a lot more than we want */
231 void reset_poweroff_timer(void)
235 int dbg_ports(void)
237 return 0;
240 void mpeg_stop(void)
244 void sys_poweroff(void)