Throw in the explicit global inits that were supposed to be there too. Important...
[kugel-rb.git] / bootloader / common.c
blob5f824cf1b80a0f2e489c2688ead4b1e1ca3745ab
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"
29 /* TODO: Other bootloaders need to be adjusted to set this variable to true
30 on a button press - currently only the ipod version does. */
31 #ifdef IPOD_ARCH
32 bool verbose = false;
33 #else
34 bool verbose = true;
35 #endif
37 int line = 0;
38 #ifdef HAVE_REMOTE_LCD
39 int remote_line = 0;
40 #endif
42 char printfbuf[256];
44 void reset_screen(void)
46 lcd_clear_display();
47 line = 0;
48 #ifdef HAVE_REMOTE_LCD
49 lcd_remote_clear_display();
50 remote_line = 0;
51 #endif
54 void printf(const char *format, ...)
56 int len;
57 unsigned char *ptr;
58 va_list ap;
59 va_start(ap, format);
61 ptr = printfbuf;
62 len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
63 va_end(ap);
65 lcd_puts(0, line++, ptr);
66 if (verbose)
67 lcd_update();
68 if(line >= LCD_HEIGHT/SYSFONT_HEIGHT)
69 line = 0;
70 #ifdef HAVE_REMOTE_LCD
71 lcd_remote_puts(0, remote_line++, ptr);
72 if (verbose)
73 lcd_remote_update();
74 if(remote_line >= LCD_REMOTE_HEIGHT/SYSFONT_HEIGHT)
75 remote_line = 0;
76 #endif
79 char *strerror(int error)
81 switch(error)
83 case EOK:
84 return "OK";
85 case EFILE_NOT_FOUND:
86 return "File not found";
87 case EREAD_CHKSUM_FAILED:
88 return "Read failed (chksum)";
89 case EREAD_MODEL_FAILED:
90 return "Read failed (model)";
91 case EREAD_IMAGE_FAILED:
92 return "Read failed (image)";
93 case EBAD_CHKSUM:
94 return "Bad checksum";
95 case EFILE_TOO_BIG:
96 return "File too big";
97 default:
98 return "Unknown";
102 /* Load firmware image in a format created by tools/scramble */
103 int load_firmware(unsigned char* buf, char* firmware, int buffer_size)
105 int fd;
106 int rc;
107 int len;
108 unsigned long chksum;
109 char model[5];
110 unsigned long sum;
111 int i;
112 char filename[MAX_PATH];
114 snprintf(filename,sizeof(filename),"/.rockbox/%s",firmware);
115 fd = open(filename, O_RDONLY);
116 if(fd < 0)
118 snprintf(filename,sizeof(filename),"/%s",firmware);
119 fd = open(filename, O_RDONLY);
120 if(fd < 0)
121 return EFILE_NOT_FOUND;
124 len = filesize(fd) - 8;
126 printf("Length: %x", len);
128 if (len > buffer_size)
129 return EFILE_TOO_BIG;
131 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
133 rc = read(fd, &chksum, 4);
134 chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
135 if(rc < 4)
136 return EREAD_CHKSUM_FAILED;
138 printf("Checksum: %x", chksum);
140 rc = read(fd, model, 4);
141 if(rc < 4)
142 return EREAD_MODEL_FAILED;
144 model[4] = 0;
146 printf("Model name: %s", model);
147 printf("Loading %s", firmware);
149 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
151 rc = read(fd, buf, len);
152 if(rc < len)
153 return EREAD_IMAGE_FAILED;
155 close(fd);
157 sum = MODEL_NUMBER;
159 for(i = 0;i < len;i++) {
160 sum += buf[i];
163 printf("Sum: %x", sum);
165 if(sum != chksum)
166 return EBAD_CHKSUM;
168 return EOK;
171 /* Load raw binary image. */
172 int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size)
174 int fd;
175 int rc;
176 int len;
177 char filename[MAX_PATH];
179 snprintf(filename,sizeof(filename),"%s",firmware);
180 fd = open(filename, O_RDONLY);
181 if(fd < 0)
183 return EFILE_NOT_FOUND;
186 len = filesize(fd);
188 if (len > buffer_size)
189 return EFILE_TOO_BIG;
191 rc = read(fd, buf, len);
192 if(rc < len)
193 return EREAD_IMAGE_FAILED;
195 close(fd);
196 return len;
199 /* These functions are present in the firmware library, but we reimplement
200 them here because the originals do a lot more than we want */
201 void reset_poweroff_timer(void)
205 int dbg_ports(void)
207 return 0;
210 void mpeg_stop(void)
214 void sys_poweroff(void)