blob: c792267afa28b845f1c50c1ff63591ab0ac33427 [file] [log] [blame]
Barry Wardell84b509d2007-01-28 18:42:11 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: main.c 11997 2007-01-13 09:08:18Z miipekk $
9 *
10 * Copyright (C) 2005 by Linus Nielsen Feltzing
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Barry Wardell84b509d2007-01-28 18:42:11 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
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>
Dave Chapman4d25bff2007-03-05 23:56:28 +000027#include <stdbool.h>
Barry Wardell84b509d2007-01-28 18:42:11 +000028#include "cpu.h"
29#include "common.h"
Barry Wardell23709982007-03-12 22:12:20 +000030#include "power.h"
31#include "kernel.h"
Barry Wardell84b509d2007-01-28 18:42:11 +000032
Dave Chapman4d25bff2007-03-05 23:56:28 +000033/* TODO: Other bootloaders need to be adjusted to set this variable to true
Barry Wardell23709982007-03-12 22:12:20 +000034 on a button press - currently only the ipod, H10 and Sansa versions do. */
Jens Arnoldc41470d2007-09-12 22:21:03 +000035#if defined(IPOD_ARCH) || defined(IRIVER_H10) || defined(IRIVER_H10_5GB) \
Mark Arigob4275d42008-05-21 03:55:17 +000036 || defined(SANSA_E200) || defined(SANSA_C200) || defined(GIGABEAT_F) \
37 || defined(PHILIPS_SA9200)
Dave Chapman4d25bff2007-03-05 23:56:28 +000038bool verbose = false;
39#else
40bool verbose = true;
41#endif
42
Barry Wardell84b509d2007-01-28 18:42:11 +000043int line = 0;
44#ifdef HAVE_REMOTE_LCD
45int remote_line = 0;
46#endif
47
48char printfbuf[256];
49
50void reset_screen(void)
51{
52 lcd_clear_display();
53 line = 0;
54#ifdef HAVE_REMOTE_LCD
55 lcd_remote_clear_display();
56 remote_line = 0;
57#endif
58}
59
60void printf(const char *format, ...)
61{
62 int len;
63 unsigned char *ptr;
64 va_list ap;
65 va_start(ap, format);
66
67 ptr = printfbuf;
68 len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
69 va_end(ap);
70
71 lcd_puts(0, line++, ptr);
Dave Chapman4d25bff2007-03-05 23:56:28 +000072 if (verbose)
73 lcd_update();
Barry Wardell84b509d2007-01-28 18:42:11 +000074 if(line >= LCD_HEIGHT/SYSFONT_HEIGHT)
75 line = 0;
76#ifdef HAVE_REMOTE_LCD
77 lcd_remote_puts(0, remote_line++, ptr);
Dave Chapman4d25bff2007-03-05 23:56:28 +000078 if (verbose)
79 lcd_remote_update();
Barry Wardell84b509d2007-01-28 18:42:11 +000080 if(remote_line >= LCD_REMOTE_HEIGHT/SYSFONT_HEIGHT)
81 remote_line = 0;
82#endif
83}
84
85char *strerror(int error)
86{
87 switch(error)
88 {
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";
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000103 case EINVALID_FORMAT:
104 return "Invalid file format";
Barry Wardell84b509d2007-01-28 18:42:11 +0000105 default:
106 return "Unknown";
107 }
108}
109
Barry Wardell23709982007-03-12 22:12:20 +0000110void error(int errortype, int error)
111{
112 switch(errortype)
113 {
114 case EATA:
115 printf("ATA error: %d", error);
116 break;
117
118 case EDISK:
119 printf("No partition found");
120 break;
121
122 case EBOOTFILE:
123 printf(strerror(error));
124 break;
125 }
126
127 lcd_update();
128 sleep(5*HZ);
129 power_off();
130}
131
Barry Wardell84b509d2007-01-28 18:42:11 +0000132/* Load firmware image in a format created by tools/scramble */
133int load_firmware(unsigned char* buf, char* firmware, int buffer_size)
134{
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];
143
144 snprintf(filename,sizeof(filename),"/.rockbox/%s",firmware);
145 fd = open(filename, O_RDONLY);
146 if(fd < 0)
147 {
148 snprintf(filename,sizeof(filename),"/%s",firmware);
149 fd = open(filename, O_RDONLY);
150 if(fd < 0)
151 return EFILE_NOT_FOUND;
152 }
153
154 len = filesize(fd) - 8;
155
156 printf("Length: %x", len);
157
158 if (len > buffer_size)
159 return EFILE_TOO_BIG;
160
161 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
162
163 rc = read(fd, &chksum, 4);
164 chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
165 if(rc < 4)
166 return EREAD_CHKSUM_FAILED;
167
168 printf("Checksum: %x", chksum);
169
170 rc = read(fd, model, 4);
171 if(rc < 4)
172 return EREAD_MODEL_FAILED;
173
174 model[4] = 0;
175
176 printf("Model name: %s", model);
177 printf("Loading %s", firmware);
178
179 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
180
181 rc = read(fd, buf, len);
182 if(rc < len)
183 return EREAD_IMAGE_FAILED;
184
185 close(fd);
186
187 sum = MODEL_NUMBER;
188
189 for(i = 0;i < len;i++) {
190 sum += buf[i];
191 }
192
193 printf("Sum: %x", sum);
194
195 if(sum != chksum)
196 return EBAD_CHKSUM;
197
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000198 return EOK;
Barry Wardell84b509d2007-01-28 18:42:11 +0000199}
200
201/* Load raw binary image. */
202int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size)
203{
204 int fd;
205 int rc;
206 int len;
207 char filename[MAX_PATH];
208
209 snprintf(filename,sizeof(filename),"%s",firmware);
210 fd = open(filename, O_RDONLY);
211 if(fd < 0)
212 {
213 return EFILE_NOT_FOUND;
214 }
215
216 len = filesize(fd);
217
218 if (len > buffer_size)
219 return EFILE_TOO_BIG;
220
221 rc = read(fd, buf, len);
222 if(rc < len)
223 return EREAD_IMAGE_FAILED;
224
225 close(fd);
226 return len;
227}
228
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 */
231void reset_poweroff_timer(void)
232{
233}
234
235int dbg_ports(void)
236{
237 return 0;
238}
239
240void mpeg_stop(void)
241{
242}
243
244void sys_poweroff(void)
245{
246}