diff options
Diffstat (limited to 'bootloader/common.c')
-rw-r--r-- | bootloader/common.c | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/bootloader/common.c b/bootloader/common.c new file mode 100644 index 0000000000..410fd42cd8 --- /dev/null +++ b/bootloader/common.c | |||
@@ -0,0 +1,205 @@ | |||
1 | /*************************************************************************** | ||
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 | * | ||
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. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
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 "cpu.h" | ||
26 | #include "common.h" | ||
27 | |||
28 | int line = 0; | ||
29 | #ifdef HAVE_REMOTE_LCD | ||
30 | int remote_line = 0; | ||
31 | #endif | ||
32 | |||
33 | char printfbuf[256]; | ||
34 | |||
35 | void reset_screen(void) | ||
36 | { | ||
37 | lcd_clear_display(); | ||
38 | line = 0; | ||
39 | #ifdef HAVE_REMOTE_LCD | ||
40 | lcd_remote_clear_display(); | ||
41 | remote_line = 0; | ||
42 | #endif | ||
43 | } | ||
44 | |||
45 | void printf(const char *format, ...) | ||
46 | { | ||
47 | int len; | ||
48 | unsigned char *ptr; | ||
49 | va_list ap; | ||
50 | va_start(ap, format); | ||
51 | |||
52 | ptr = printfbuf; | ||
53 | len = vsnprintf(ptr, sizeof(printfbuf), format, ap); | ||
54 | va_end(ap); | ||
55 | |||
56 | lcd_puts(0, line++, ptr); | ||
57 | lcd_update(); | ||
58 | if(line >= LCD_HEIGHT/SYSFONT_HEIGHT) | ||
59 | line = 0; | ||
60 | #ifdef HAVE_REMOTE_LCD | ||
61 | lcd_remote_puts(0, remote_line++, ptr); | ||
62 | lcd_remote_update(); | ||
63 | if(remote_line >= LCD_REMOTE_HEIGHT/SYSFONT_HEIGHT) | ||
64 | remote_line = 0; | ||
65 | #endif | ||
66 | } | ||
67 | |||
68 | char *strerror(int error) | ||
69 | { | ||
70 | switch(error) | ||
71 | { | ||
72 | case EOK: | ||
73 | return "OK"; | ||
74 | case EFILE_NOT_FOUND: | ||
75 | return "File not found"; | ||
76 | case EREAD_CHKSUM_FAILED: | ||
77 | return "Read failed (chksum)"; | ||
78 | case EREAD_MODEL_FAILED: | ||
79 | return "Read failed (model)"; | ||
80 | case EREAD_IMAGE_FAILED: | ||
81 | return "Read failed (image)"; | ||
82 | case EBAD_CHKSUM: | ||
83 | return "Bad checksum"; | ||
84 | case EFILE_TOO_BIG: | ||
85 | return "File too big"; | ||
86 | default: | ||
87 | return "Unknown"; | ||
88 | } | ||
89 | } | ||
90 | |||
91 | /* Load firmware image in a format created by tools/scramble */ | ||
92 | int load_firmware(unsigned char* buf, char* firmware, int buffer_size) | ||
93 | { | ||
94 | int fd; | ||
95 | int rc; | ||
96 | int len; | ||
97 | unsigned long chksum; | ||
98 | char model[5]; | ||
99 | unsigned long sum; | ||
100 | int i; | ||
101 | char filename[MAX_PATH]; | ||
102 | |||
103 | snprintf(filename,sizeof(filename),"/.rockbox/%s",firmware); | ||
104 | fd = open(filename, O_RDONLY); | ||
105 | if(fd < 0) | ||
106 | { | ||
107 | snprintf(filename,sizeof(filename),"/%s",firmware); | ||
108 | fd = open(filename, O_RDONLY); | ||
109 | if(fd < 0) | ||
110 | return EFILE_NOT_FOUND; | ||
111 | } | ||
112 | |||
113 | len = filesize(fd) - 8; | ||
114 | |||
115 | printf("Length: %x", len); | ||
116 | |||
117 | if (len > buffer_size) | ||
118 | return EFILE_TOO_BIG; | ||
119 | |||
120 | lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET); | ||
121 | |||
122 | rc = read(fd, &chksum, 4); | ||
123 | chksum=betoh32(chksum); /* Rockbox checksums are big-endian */ | ||
124 | if(rc < 4) | ||
125 | return EREAD_CHKSUM_FAILED; | ||
126 | |||
127 | printf("Checksum: %x", chksum); | ||
128 | |||
129 | rc = read(fd, model, 4); | ||
130 | if(rc < 4) | ||
131 | return EREAD_MODEL_FAILED; | ||
132 | |||
133 | model[4] = 0; | ||
134 | |||
135 | printf("Model name: %s", model); | ||
136 | printf("Loading %s", firmware); | ||
137 | |||
138 | lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET); | ||
139 | |||
140 | rc = read(fd, buf, len); | ||
141 | if(rc < len) | ||
142 | return EREAD_IMAGE_FAILED; | ||
143 | |||
144 | close(fd); | ||
145 | |||
146 | sum = MODEL_NUMBER; | ||
147 | |||
148 | for(i = 0;i < len;i++) { | ||
149 | sum += buf[i]; | ||
150 | } | ||
151 | |||
152 | printf("Sum: %x", sum); | ||
153 | |||
154 | if(sum != chksum) | ||
155 | return EBAD_CHKSUM; | ||
156 | |||
157 | return len; | ||
158 | } | ||
159 | |||
160 | /* Load raw binary image. */ | ||
161 | int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size) | ||
162 | { | ||
163 | int fd; | ||
164 | int rc; | ||
165 | int len; | ||
166 | char filename[MAX_PATH]; | ||
167 | |||
168 | snprintf(filename,sizeof(filename),"%s",firmware); | ||
169 | fd = open(filename, O_RDONLY); | ||
170 | if(fd < 0) | ||
171 | { | ||
172 | return EFILE_NOT_FOUND; | ||
173 | } | ||
174 | |||
175 | len = filesize(fd); | ||
176 | |||
177 | if (len > buffer_size) | ||
178 | return EFILE_TOO_BIG; | ||
179 | |||
180 | rc = read(fd, buf, len); | ||
181 | if(rc < len) | ||
182 | return EREAD_IMAGE_FAILED; | ||
183 | |||
184 | close(fd); | ||
185 | return len; | ||
186 | } | ||
187 | |||
188 | /* These functions are present in the firmware library, but we reimplement | ||
189 | them here because the originals do a lot more than we want */ | ||
190 | void reset_poweroff_timer(void) | ||
191 | { | ||
192 | } | ||
193 | |||
194 | int dbg_ports(void) | ||
195 | { | ||
196 | return 0; | ||
197 | } | ||
198 | |||
199 | void mpeg_stop(void) | ||
200 | { | ||
201 | } | ||
202 | |||
203 | void sys_poweroff(void) | ||
204 | { | ||
205 | } | ||