Use BOOTDIR and BOOTFILE defines instead of hardcoded path and file. FS#9559
[kugel-rb.git] / bootloader / common.c
blob40e460bed9d57f5d91989474713e71b3cf51fb58
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"
34 /* TODO: Other bootloaders need to be adjusted to set this variable to true
35 on a button press - currently only the ipod, H10 and Sansa versions do. */
36 #if defined(IPOD_ARCH) || defined(IRIVER_H10) || defined(IRIVER_H10_5GB) \
37 || defined(SANSA_E200) || defined(SANSA_C200) || defined(GIGABEAT_F) \
38 || defined(PHILIPS_SA9200) || (CONFIG_CPU == AS3525) || defined(COWON_D2)
39 bool verbose = false;
40 #else
41 bool verbose = true;
42 #endif
44 int line = 0;
45 #ifdef HAVE_REMOTE_LCD
46 int remote_line = 0;
47 #endif
49 char printfbuf[256];
51 void reset_screen(void)
53 lcd_clear_display();
54 line = 0;
55 #ifdef HAVE_REMOTE_LCD
56 lcd_remote_clear_display();
57 remote_line = 0;
58 #endif
61 void printf(const char *format, ...)
63 int len;
64 unsigned char *ptr;
65 va_list ap;
66 va_start(ap, format);
68 ptr = printfbuf;
69 len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
70 va_end(ap);
72 lcd_puts(0, line++, ptr);
73 if (verbose)
74 lcd_update();
75 if(line >= LCD_HEIGHT/SYSFONT_HEIGHT)
76 line = 0;
77 #ifdef HAVE_REMOTE_LCD
78 lcd_remote_puts(0, remote_line++, ptr);
79 if (verbose)
80 lcd_remote_update();
81 if(remote_line >= LCD_REMOTE_HEIGHT/SYSFONT_HEIGHT)
82 remote_line = 0;
83 #endif
86 char *strerror(int error)
88 switch(error)
90 case EOK:
91 return "OK";
92 case EFILE_NOT_FOUND:
93 return "File not found";
94 case EREAD_CHKSUM_FAILED:
95 return "Read failed (chksum)";
96 case EREAD_MODEL_FAILED:
97 return "Read failed (model)";
98 case EREAD_IMAGE_FAILED:
99 return "Read failed (image)";
100 case EBAD_CHKSUM:
101 return "Bad checksum";
102 case EFILE_TOO_BIG:
103 return "File too big";
104 case EINVALID_FORMAT:
105 return "Invalid file format";
106 default:
107 return "Unknown";
111 void error(int errortype, int error)
113 switch(errortype)
115 case EATA:
116 printf("ATA error: %d", error);
117 break;
119 case EDISK:
120 printf("No partition found");
121 break;
123 case EBOOTFILE:
124 printf(strerror(error));
125 break;
128 lcd_update();
129 sleep(5*HZ);
130 power_off();
133 /* Load firmware image in a format created by tools/scramble */
134 int load_firmware(unsigned char* buf, char* firmware, int buffer_size)
136 int fd;
137 int rc;
138 int len;
139 unsigned long chksum;
140 char model[5];
141 unsigned long sum;
142 int i;
143 char filename[MAX_PATH];
145 snprintf(filename,sizeof(filename), BOOTDIR "/%s",firmware);
146 fd = open(filename, O_RDONLY);
147 if(fd < 0)
149 snprintf(filename,sizeof(filename),"/%s",firmware);
150 fd = open(filename, O_RDONLY);
151 if(fd < 0)
152 return EFILE_NOT_FOUND;
155 len = filesize(fd) - 8;
157 printf("Length: %x", len);
159 if (len > buffer_size)
160 return EFILE_TOO_BIG;
162 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
164 rc = read(fd, &chksum, 4);
165 chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
166 if(rc < 4)
167 return EREAD_CHKSUM_FAILED;
169 printf("Checksum: %x", chksum);
171 rc = read(fd, model, 4);
172 if(rc < 4)
173 return EREAD_MODEL_FAILED;
175 model[4] = 0;
177 printf("Model name: %s", model);
178 printf("Loading %s", firmware);
180 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
182 rc = read(fd, buf, len);
183 if(rc < len)
184 return EREAD_IMAGE_FAILED;
186 close(fd);
188 sum = MODEL_NUMBER;
190 for(i = 0;i < len;i++) {
191 sum += buf[i];
194 printf("Sum: %x", sum);
196 if(sum != chksum)
197 return EBAD_CHKSUM;
199 return EOK;
202 /* Load raw binary image. */
203 int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size)
205 int fd;
206 int rc;
207 int len;
208 char filename[MAX_PATH];
210 snprintf(filename,sizeof(filename),"%s",firmware);
211 fd = open(filename, O_RDONLY);
212 if(fd < 0)
214 return EFILE_NOT_FOUND;
217 len = filesize(fd);
219 if (len > buffer_size)
220 return EFILE_TOO_BIG;
222 rc = read(fd, buf, len);
223 if(rc < len)
224 return EREAD_IMAGE_FAILED;
226 close(fd);
227 return len;
230 /* These functions are present in the firmware library, but we reimplement
231 them here because the originals do a lot more than we want */
232 void reset_poweroff_timer(void)
236 int dbg_ports(void)
238 return 0;
241 void mpeg_stop(void)
245 void sys_poweroff(void)