summaryrefslogtreecommitdiff
path: root/bootloader/common.c
diff options
context:
space:
mode:
authorMarcin Bukat <marcin.bukat@gmail.com>2012-03-04 15:34:29 +0100
committerMarcin Bukat <marcin.bukat@gmail.com>2013-06-27 13:50:11 +0200
commit0b29691324e5700f15ea653592bf29f8552d47d7 (patch)
tree00e4f77f28c813a493cd8af22170f486f6e82903 /bootloader/common.c
parent46ea8bfe7c690c8db230fff3a582a69779f8e432 (diff)
downloadrockbox-0b29691324e5700f15ea653592bf29f8552d47d7.tar.gz
rockbox-0b29691324e5700f15ea653592bf29f8552d47d7.zip
Move load_firmware() to separate file
The idea is to share loading code between bootloaders and rolo(). Change-Id: I1656ed91946d7a05cb7c9fa7a16793c3c862a5cd Reviewed-on: http://gerrit.rockbox.org/190 Reviewed-by: Marcin Bukat <marcin.bukat@gmail.com>
Diffstat (limited to 'bootloader/common.c')
-rw-r--r--bootloader/common.c105
1 files changed, 10 insertions, 95 deletions
diff --git a/bootloader/common.c b/bootloader/common.c
index 207d74ae9c..21b26e881c 100644
--- a/bootloader/common.c
+++ b/bootloader/common.c
@@ -35,6 +35,14 @@
35#include "string.h" 35#include "string.h"
36#include "usb.h" 36#include "usb.h"
37#include "file.h" 37#include "file.h"
38#include "loader_strerror.h"
39#if defined(MI4_FORMAT)
40#include "mi4-loader.h"
41#elif defined(RKW_FORMAT)
42#include "rkw-loader.h"
43#else
44#include "rb-loader.h"
45#endif
38 46
39/* TODO: Other bootloaders need to be adjusted to set this variable to true 47/* TODO: Other bootloaders need to be adjusted to set this variable to true
40 on a button press - currently only the ipod, H10, Vibe 500 and Sansa versions do. */ 48 on a button press - currently only the ipod, H10, Vibe 500 and Sansa versions do. */
@@ -94,31 +102,6 @@ int printf(const char *format, ...)
94 return len; 102 return len;
95} 103}
96 104
97char *strerror(int error)
98{
99 switch(error)
100 {
101 case EOK:
102 return "OK";
103 case EFILE_NOT_FOUND:
104 return "File not found";
105 case EREAD_CHKSUM_FAILED:
106 return "Read failed (chksum)";
107 case EREAD_MODEL_FAILED:
108 return "Read failed (model)";
109 case EREAD_IMAGE_FAILED:
110 return "Read failed (image)";
111 case EBAD_CHKSUM:
112 return "Bad checksum";
113 case EFILE_TOO_BIG:
114 return "File too big";
115 case EINVALID_FORMAT:
116 return "Invalid file format";
117 default:
118 return "Unknown";
119 }
120}
121
122void error(int errortype, int error, bool shutdown) 105void error(int errortype, int error, bool shutdown)
123{ 106{
124 switch(errortype) 107 switch(errortype)
@@ -132,85 +115,17 @@ void error(int errortype, int error, bool shutdown)
132 break; 115 break;
133 116
134 case EBOOTFILE: 117 case EBOOTFILE:
135 printf(strerror(error)); 118 printf(loader_strerror(error));
136 break; 119 break;
137 } 120 }
138 121
139 lcd_update(); 122 lcd_update();
140 sleep(5*HZ); 123 sleep(5*HZ);
124
141 if(shutdown) 125 if(shutdown)
142 power_off(); 126 power_off();
143} 127}
144 128
145/* Load firmware image in a format created by tools/scramble */
146int load_firmware(unsigned char* buf, char* firmware, int buffer_size)
147{
148 int fd;
149 int rc;
150 int len;
151 unsigned long chksum;
152 char model[5];
153 unsigned long sum;
154 int i;
155 char filename[MAX_PATH];
156
157 snprintf(filename,sizeof(filename), BOOTDIR "/%s",firmware);
158 fd = open(filename, O_RDONLY);
159 if(fd < 0)
160 {
161 snprintf(filename,sizeof(filename),"/%s",firmware);
162 fd = open(filename, O_RDONLY);
163 if(fd < 0)
164 return EFILE_NOT_FOUND;
165 }
166
167 len = filesize(fd) - 8;
168
169 printf("Length: %x", len);
170
171 if (len > buffer_size)
172 return EFILE_TOO_BIG;
173
174 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
175
176 rc = read(fd, &chksum, 4);
177 chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
178 if(rc < 4)
179 return EREAD_CHKSUM_FAILED;
180
181 printf("Checksum: %x", chksum);
182
183 rc = read(fd, model, 4);
184 if(rc < 4)
185 return EREAD_MODEL_FAILED;
186
187 model[4] = '\0';
188
189 printf("Model name: %s", model);
190 printf("Loading %s", firmware);
191
192 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
193
194 rc = read(fd, buf, len);
195 if(rc < len)
196 return EREAD_IMAGE_FAILED;
197
198 close(fd);
199
200 sum = MODEL_NUMBER;
201
202 for(i = 0;i < len;i++) {
203 sum += buf[i];
204 }
205
206 printf("Sum: %x", sum);
207
208 if(sum != chksum)
209 return EBAD_CHKSUM;
210
211 return EOK;
212}
213
214/* Load raw binary image. */ 129/* Load raw binary image. */
215int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size) 130int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size)
216{ 131{