set the s5l8700 i2c define to a unique number
[Rockbox.git] / bootloader / common.c
blob66a60c5f6cfc3b9744bd3c2c9dccf21657fd11ae
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 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "lcd.h"
20 #include "lcd-remote.h"
21 #include "font.h"
22 #include "system.h"
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdbool.h>
26 #include "cpu.h"
27 #include "common.h"
28 #include "power.h"
29 #include "kernel.h"
31 /* TODO: Other bootloaders need to be adjusted to set this variable to true
32 on a button press - currently only the ipod, H10 and Sansa versions do. */
33 #if defined(IPOD_ARCH) || defined(IRIVER_H10) || defined(IRIVER_H10_5GB) \
34 || defined(SANSA_E200) || defined(SANSA_C200) || defined(GIGABEAT_F) \
35 || defined(PHILIPS_SA9200)
36 bool verbose = false;
37 #else
38 bool verbose = true;
39 #endif
41 int line = 0;
42 #ifdef HAVE_REMOTE_LCD
43 int remote_line = 0;
44 #endif
46 char printfbuf[256];
48 void reset_screen(void)
50 lcd_clear_display();
51 line = 0;
52 #ifdef HAVE_REMOTE_LCD
53 lcd_remote_clear_display();
54 remote_line = 0;
55 #endif
58 void printf(const char *format, ...)
60 int len;
61 unsigned char *ptr;
62 va_list ap;
63 va_start(ap, format);
65 ptr = printfbuf;
66 len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
67 va_end(ap);
69 lcd_puts(0, line++, ptr);
70 if (verbose)
71 lcd_update();
72 if(line >= LCD_HEIGHT/SYSFONT_HEIGHT)
73 line = 0;
74 #ifdef HAVE_REMOTE_LCD
75 lcd_remote_puts(0, remote_line++, ptr);
76 if (verbose)
77 lcd_remote_update();
78 if(remote_line >= LCD_REMOTE_HEIGHT/SYSFONT_HEIGHT)
79 remote_line = 0;
80 #endif
83 char *strerror(int error)
85 switch(error)
87 case EOK:
88 return "OK";
89 case EFILE_NOT_FOUND:
90 return "File not found";
91 case EREAD_CHKSUM_FAILED:
92 return "Read failed (chksum)";
93 case EREAD_MODEL_FAILED:
94 return "Read failed (model)";
95 case EREAD_IMAGE_FAILED:
96 return "Read failed (image)";
97 case EBAD_CHKSUM:
98 return "Bad checksum";
99 case EFILE_TOO_BIG:
100 return "File too big";
101 case EINVALID_FORMAT:
102 return "Invalid file format";
103 default:
104 return "Unknown";
108 void error(int errortype, int error)
110 switch(errortype)
112 case EATA:
113 printf("ATA error: %d", error);
114 break;
116 case EDISK:
117 printf("No partition found");
118 break;
120 case EBOOTFILE:
121 printf(strerror(error));
122 break;
125 lcd_update();
126 sleep(5*HZ);
127 power_off();
130 /* Load firmware image in a format created by tools/scramble */
131 int load_firmware(unsigned char* buf, char* firmware, int buffer_size)
133 int fd;
134 int rc;
135 int len;
136 unsigned long chksum;
137 char model[5];
138 unsigned long sum;
139 int i;
140 char filename[MAX_PATH];
142 snprintf(filename,sizeof(filename),"/.rockbox/%s",firmware);
143 fd = open(filename, O_RDONLY);
144 if(fd < 0)
146 snprintf(filename,sizeof(filename),"/%s",firmware);
147 fd = open(filename, O_RDONLY);
148 if(fd < 0)
149 return EFILE_NOT_FOUND;
152 len = filesize(fd) - 8;
154 printf("Length: %x", len);
156 if (len > buffer_size)
157 return EFILE_TOO_BIG;
159 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
161 rc = read(fd, &chksum, 4);
162 chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
163 if(rc < 4)
164 return EREAD_CHKSUM_FAILED;
166 printf("Checksum: %x", chksum);
168 rc = read(fd, model, 4);
169 if(rc < 4)
170 return EREAD_MODEL_FAILED;
172 model[4] = 0;
174 printf("Model name: %s", model);
175 printf("Loading %s", firmware);
177 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
179 rc = read(fd, buf, len);
180 if(rc < len)
181 return EREAD_IMAGE_FAILED;
183 close(fd);
185 sum = MODEL_NUMBER;
187 for(i = 0;i < len;i++) {
188 sum += buf[i];
191 printf("Sum: %x", sum);
193 if(sum != chksum)
194 return EBAD_CHKSUM;
196 return EOK;
199 /* Load raw binary image. */
200 int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size)
202 int fd;
203 int rc;
204 int len;
205 char filename[MAX_PATH];
207 snprintf(filename,sizeof(filename),"%s",firmware);
208 fd = open(filename, O_RDONLY);
209 if(fd < 0)
211 return EFILE_NOT_FOUND;
214 len = filesize(fd);
216 if (len > buffer_size)
217 return EFILE_TOO_BIG;
219 rc = read(fd, buf, len);
220 if(rc < len)
221 return EREAD_IMAGE_FAILED;
223 close(fd);
224 return len;
227 /* These functions are present in the firmware library, but we reimplement
228 them here because the originals do a lot more than we want */
229 void reset_poweroff_timer(void)
233 int dbg_ports(void)
235 return 0;
238 void mpeg_stop(void)
242 void sys_poweroff(void)