summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/plugins/iriver_flash.c61
1 files changed, 46 insertions, 15 deletions
diff --git a/apps/plugins/iriver_flash.c b/apps/plugins/iriver_flash.c
index e33e422d01..449bf349a8 100644
--- a/apps/plugins/iriver_flash.c
+++ b/apps/plugins/iriver_flash.c
@@ -31,7 +31,7 @@
31 31
32/* All CFI flash routines are copied and ported from firmware_flash.c */ 32/* All CFI flash routines are copied and ported from firmware_flash.c */
33 33
34unsigned char *audiobuf; 34uint8_t* audiobuf;
35ssize_t audiobuf_size; 35ssize_t audiobuf_size;
36 36
37#if !defined(IRIVER_H100_SERIES) && !defined(IRIVER_H300_SERIES) 37#if !defined(IRIVER_H100_SERIES) && !defined(IRIVER_H300_SERIES)
@@ -66,6 +66,14 @@ enum sections {
66static volatile uint16_t* FB = (uint16_t*)0x00000000; /* Flash base address */ 66static volatile uint16_t* FB = (uint16_t*)0x00000000; /* Flash base address */
67#endif 67#endif
68 68
69#ifdef IRIVER_H100
70#define MODEL "h100"
71#elif defined(IRIVER_H120)
72#define MODEL "h120"
73#elif defined(IRIVER_H300)
74#define MODEL "h300"
75#endif
76
69/* read the manufacturer and device ID */ 77/* read the manufacturer and device ID */
70static void cfi_read_id(struct flash_info* pInfo) 78static void cfi_read_id(struct flash_info* pInfo)
71{ 79{
@@ -230,19 +238,19 @@ bool confirm(const char *msg)
230 return ret; 238 return ret;
231} 239}
232 240
233int load_firmware_file(const char *filename, uint32_t *checksum) 241static off_t load_firmware_file(const char* filename, uint32_t* out_checksum)
234{ 242{
235 int fd; 243 int fd;
236 int len, rc; 244 off_t len;
237 int i; 245 uint32_t checksum;
246 char model[4];
238 uint32_t sum; 247 uint32_t sum;
239 248
240 fd = rb->open(filename, O_RDONLY); 249 fd = rb->open(filename, O_RDONLY);
241 if (fd < 0) 250 if (fd < 0)
242 return -1; 251 return -1;
243 252
244 len = rb->filesize(fd); 253 len = rb->filesize(fd);
245
246 if (audiobuf_size < len) 254 if (audiobuf_size < len)
247 { 255 {
248 rb->splash(HZ*3, "Aborting: Out of memory!"); 256 rb->splash(HZ*3, "Aborting: Out of memory!");
@@ -250,29 +258,52 @@ int load_firmware_file(const char *filename, uint32_t *checksum)
250 return -2; 258 return -2;
251 } 259 }
252 260
253 rb->read(fd, checksum, 4); 261 if (rb->read(fd, &checksum, sizeof(checksum)) != sizeof(checksum))
254 rb->lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET); 262 {
263 rb->splash(HZ*3, "Aborting: Read failure");
264 rb->close(fd);
265 return -3;
266 }
267
268 if (rb->read(fd, model, sizeof(model)) != sizeof(model))
269 {
270 rb->splash(HZ*3, "Aborting: Read failure");
271 rb->close(fd);
272 return -4;
273 }
274
255 len -= FIRMWARE_OFFSET_FILE_DATA; 275 len -= FIRMWARE_OFFSET_FILE_DATA;
256 276
257 rc = rb->read(fd, audiobuf, len); 277 if (rb->read(fd, audiobuf, len) != len)
258 rb->close(fd);
259 if (rc != len)
260 { 278 {
261 rb->splash(HZ*3, "Aborting: Read failure"); 279 rb->splash(HZ*3, "Aborting: Read failure");
262 return -3; 280 rb->close(fd);
281 return -5;
263 } 282 }
264 283
284 rb->close(fd);
285
265 /* Verify the checksum */ 286 /* Verify the checksum */
266 sum = MODEL_NUMBER; 287 sum = MODEL_NUMBER;
267 for (i = 0; i < len; i++) 288 for (off_t i = 0; i < len; i++)
268 sum += audiobuf[i]; 289 sum += audiobuf[i];
269 290
270 if (sum != *checksum) 291 if (sum != checksum)
271 { 292 {
272 rb->splash(HZ*3, "Aborting: Checksums mismatch!"); 293 rb->splash(HZ*3, "Aborting: Checksums mismatch!");
273 return -4; 294 return -6;
274 } 295 }
275 296
297 /* Verify the model */
298 if (rb->memcmp(model, MODEL, sizeof(model)) != 0)
299 {
300 rb->splash(HZ*3, "Aborting: Models mismatch!");
301 return -7;
302 }
303
304 if (out_checksum != NULL)
305 *out_checksum = checksum;
306
276 return len; 307 return len;
277} 308}
278 309