summaryrefslogtreecommitdiff
path: root/rbutil/sansapatcher/sansapatcher.c
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil/sansapatcher/sansapatcher.c')
-rw-r--r--rbutil/sansapatcher/sansapatcher.c101
1 files changed, 93 insertions, 8 deletions
diff --git a/rbutil/sansapatcher/sansapatcher.c b/rbutil/sansapatcher/sansapatcher.c
index d1c12852a7..b3a0289c15 100644
--- a/rbutil/sansapatcher/sansapatcher.c
+++ b/rbutil/sansapatcher/sansapatcher.c
@@ -30,7 +30,8 @@
30#include "sansapatcher.h" 30#include "sansapatcher.h"
31 31
32#ifndef RBUTIL 32#ifndef RBUTIL
33 #include "bootimg.h" 33 #include "bootimg_c200.h"
34 #include "bootimg_e200.h"
34#endif 35#endif
35/* The offset of the MI4 image header in the firmware partition */ 36/* The offset of the MI4 image header in the firmware partition */
36#define PPMI_OFFSET 0x80000 37#define PPMI_OFFSET 0x80000
@@ -121,6 +122,60 @@ int sansa_read_partinfo(struct sansa_t* sansa, int silent)
121 return 0; 122 return 0;
122} 123}
123 124
125/* NOTE: memmem implementation copied from glibc-2.2.4 - it's a GNU
126 extension and is not universally. In addition, early versions of
127 memmem had a serious bug - the meaning of needle and haystack were
128 reversed. */
129
130/* Copyright (C) 1991,92,93,94,96,97,98,2000 Free Software Foundation, Inc.
131 This file is part of the GNU C Library.
132
133 The GNU C Library is free software; you can redistribute it and/or
134 modify it under the terms of the GNU Lesser General Public
135 License as published by the Free Software Foundation; either
136 version 2.1 of the License, or (at your option) any later version.
137
138 The GNU C Library is distributed in the hope that it will be useful,
139 but WITHOUT ANY WARRANTY; without even the implied warranty of
140 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
141 Lesser General Public License for more details.
142
143 You should have received a copy of the GNU Lesser General Public
144 License along with the GNU C Library; if not, write to the Free
145 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
146 02111-1307 USA. */
147
148/* Return the first occurrence of NEEDLE in HAYSTACK. */
149static void *
150sansa_memmem (haystack, haystack_len, needle, needle_len)
151 const void *haystack;
152 size_t haystack_len;
153 const void *needle;
154 size_t needle_len;
155{
156 const char *begin;
157 const char *const last_possible
158 = (const char *) haystack + haystack_len - needle_len;
159
160 if (needle_len == 0)
161 /* The first occurrence of the empty string is deemed to occur at
162 the beginning of the string. */
163 return (void *) haystack;
164
165 /* Sanity check, otherwise the loop might search through the whole
166 memory. */
167 if (__builtin_expect (haystack_len < needle_len, 0))
168 return NULL;
169
170 for (begin = (const char *) haystack; begin <= last_possible; ++begin)
171 if (begin[0] == ((const char *) needle)[0] &&
172 !memcmp ((const void *) &begin[1],
173 (const void *) ((const char *) needle + 1),
174 needle_len - 1))
175 return (void *) begin;
176
177 return NULL;
178}
124 179
125/* 180/*
126 * CRC32 implementation taken from: 181 * CRC32 implementation taken from:
@@ -191,7 +246,7 @@ static void chksum_crc32gentab (void)
191} 246}
192 247
193/* Known keys for Sansa E200 and C200 firmwares: */ 248/* Known keys for Sansa E200 and C200 firmwares: */
194#define NUM_KEYS (sizeof(keys)/sizeof(keys[0])) 249#define NUM_KEYS ((int)(sizeof(keys)/sizeof(keys[0])))
195static uint32_t keys[][4] = { 250static uint32_t keys[][4] = {
196 { 0xe494e96e, 0x3ee32966, 0x6f48512b, 0xa93fbb42 }, /* "sansa" */ 251 { 0xe494e96e, 0x3ee32966, 0x6f48512b, 0xa93fbb42 }, /* "sansa" */
197 { 0xd7b10538, 0xc662945b, 0x1b3fce68, 0xf389c0e6 }, /* "sansa_gh" */ 252 { 0xd7b10538, 0xc662945b, 0x1b3fce68, 0xf389c0e6 }, /* "sansa_gh" */
@@ -328,10 +383,11 @@ static int sansa_seek_and_read(struct sansa_t* sansa, loff_t pos, unsigned char*
328 5) The "PPMI" string appears at offset PPMI_OFFSET in the 2nd partition. 383 5) The "PPMI" string appears at offset PPMI_OFFSET in the 2nd partition.
329*/ 384*/
330 385
331int is_e200(struct sansa_t* sansa) 386int is_sansa(struct sansa_t* sansa)
332{ 387{
333 struct mi4header_t mi4header; 388 struct mi4header_t mi4header;
334 int ppmi_length; 389 int ppmi_length;
390 int ppbl_length;
335 391
336 /* Check partition layout */ 392 /* Check partition layout */
337 393
@@ -354,10 +410,31 @@ int is_e200(struct sansa_t* sansa)
354 /* No bootloader header, abort */ 410 /* No bootloader header, abort */
355 return -4; 411 return -4;
356 } 412 }
413 ppbl_length = (le2int(sectorbuf+4) + 0x1ff) & ~0x1ff;
357 414
415 /* Sanity/safety check - the bootloader can't be larger than PPMI_OFFSET */
416 if (ppbl_length > PPMI_OFFSET)
417 {
418 return -5;
419 }
420
421 /* Load Sansa bootloader and check for "Sansa C200" magic string */
422 if (sansa_seek_and_read(sansa, sansa->start + 0x200, sectorbuf, ppbl_length) < 0) {
423 fprintf(stderr,"[ERR] Seek and read to 0x%08llx in is_sansa failed.\n",
424 sansa->start+0x200);
425 return -6;
426 }
427 if (sansa_memmem(sectorbuf, ppbl_length, "Sansa C200", 10) != NULL) {
428 /* C200 */
429 sansa->targetname="c200";
430 } else {
431 /* E200 */
432 sansa->targetname="e200";
433 }
434
358 /* Check Main firmware header */ 435 /* Check Main firmware header */
359 if (sansa_seek_and_read(sansa, sansa->start+PPMI_OFFSET, sectorbuf, 0x200) < 0) { 436 if (sansa_seek_and_read(sansa, sansa->start+PPMI_OFFSET, sectorbuf, 0x200) < 0) {
360 fprintf(stderr,"[ERR] Seek to 0x%08llx in is_e200 failed.\n", 437 fprintf(stderr,"[ERR] Seek to 0x%08llx in is_sansa failed.\n",
361 sansa->start+PPMI_OFFSET); 438 sansa->start+PPMI_OFFSET);
362 return -5; 439 return -5;
363 } 440 }
@@ -369,7 +446,7 @@ int is_e200(struct sansa_t* sansa)
369 446
370 /* Check main mi4 file header */ 447 /* Check main mi4 file header */
371 if (sansa_seek_and_read(sansa, sansa->start+PPMI_OFFSET+0x200, sectorbuf, 0x200) < 0) { 448 if (sansa_seek_and_read(sansa, sansa->start+PPMI_OFFSET+0x200, sectorbuf, 0x200) < 0) {
372 fprintf(stderr,"[ERR] Seek to 0x%08llx in is_e200 failed.\n", 449 fprintf(stderr,"[ERR] Seek to 0x%08llx in is_sansa failed.\n",
373 sansa->start+PPMI_OFFSET+0x200); 450 sansa->start+PPMI_OFFSET+0x200);
374 return -5; 451 return -5;
375 } 452 }
@@ -435,7 +512,7 @@ int sansa_scan(struct sansa_t* sansa)
435 continue; 512 continue;
436 } 513 }
437 514
438 if (is_e200(sansa) < 0) { 515 if (is_sansa(sansa) < 0) {
439 continue; 516 continue;
440 } 517 }
441 518
@@ -602,7 +679,11 @@ int sansa_add_bootloader(struct sansa_t* sansa, char* filename, int type)
602 bl_length = filesize(infile); 679 bl_length = filesize(infile);
603 } else { 680 } else {
604 #ifndef RBUTIL 681 #ifndef RBUTIL
605 bl_length = LEN_bootimg; 682 if (strcmp(sansa->targetname,"c200") == 0) {
683 bl_length = LEN_bootimg_c200;
684 } else {
685 bl_length = LEN_bootimg_e200;
686 }
606 #endif 687 #endif
607 } 688 }
608 689
@@ -629,7 +710,11 @@ int sansa_add_bootloader(struct sansa_t* sansa, char* filename, int type)
629 } 710 }
630 } else { 711 } else {
631 #ifndef RBUTIL 712 #ifndef RBUTIL
632 memcpy(sectorbuf+0x200,bootimg,LEN_bootimg); 713 if (strcmp(sansa->targetname,"c200") == 0) {
714 memcpy(sectorbuf+0x200,bootimg_c200,LEN_bootimg_c200);
715 } else {
716 memcpy(sectorbuf+0x200,bootimg_e200,LEN_bootimg_e200);
717 }
633 #endif 718 #endif
634 } 719 }
635 720