summaryrefslogtreecommitdiff
path: root/rbutil
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil')
-rw-r--r--rbutil/ipodpatcher/fat32format.c18
-rw-r--r--rbutil/ipodpatcher/ipodpatcher.c178
-rw-r--r--rbutil/ipodpatcher/ipodpatcher.h3
-rw-r--r--rbutil/ipodpatcher/main.c6
-rw-r--r--rbutil/rbutilqt/installbootloader.cpp4
-rw-r--r--rbutil/sansapatcher/main.c6
-rw-r--r--rbutil/sansapatcher/sansapatcher.c112
-rw-r--r--rbutil/sansapatcher/sansapatcher.h3
8 files changed, 163 insertions, 167 deletions
diff --git a/rbutil/ipodpatcher/fat32format.c b/rbutil/ipodpatcher/fat32format.c
index a9417b22a2..573d3bf88a 100644
--- a/rbutil/ipodpatcher/fat32format.c
+++ b/rbutil/ipodpatcher/fat32format.c
@@ -82,7 +82,7 @@ uint32_t htole32(uint32_t x)
82 82
83 83
84/* A large aligned buffer for disk I/O */ 84/* A large aligned buffer for disk I/O */
85extern unsigned char* sectorbuf; 85extern unsigned char* ipod_sectorbuf;
86 86
87/* TODO: Pass these as parameters to the various create_ functions */ 87/* TODO: Pass these as parameters to the various create_ functions */
88 88
@@ -160,7 +160,7 @@ static int zero_sectors(struct ipod_t* ipod, uint64_t sector, int count)
160 return -1; 160 return -1;
161 } 161 }
162 162
163 memset(sectorbuf, 0, 128 * ipod->sector_size); 163 memset(ipod_sectorbuf, 0, 128 * ipod->sector_size);
164 164
165 /* Write 128 sectors at a time */ 165 /* Write 128 sectors at a time */
166 while (count) { 166 while (count) {
@@ -169,7 +169,7 @@ static int zero_sectors(struct ipod_t* ipod, uint64_t sector, int count)
169 else 169 else
170 n = count; 170 n = count;
171 171
172 if (ipod_write(ipod,sectorbuf,n * ipod->sector_size) < 0) { 172 if (ipod_write(ipod,ipod_sectorbuf,n * ipod->sector_size) < 0) {
173 perror("[ERR] Write failed in zero_sectors\n"); 173 perror("[ERR] Write failed in zero_sectors\n");
174 return -1; 174 return -1;
175 } 175 }
@@ -478,15 +478,15 @@ int format_partition(struct ipod_t* ipod, int partition)
478 fprintf(stderr,"[INFO] Initialising reserved sectors and FATs...\n" ); 478 fprintf(stderr,"[INFO] Initialising reserved sectors and FATs...\n" );
479 479
480 /* Create the boot sector structure */ 480 /* Create the boot sector structure */
481 create_boot_sector(sectorbuf, ipod, partition); 481 create_boot_sector(ipod_sectorbuf, ipod, partition);
482 create_fsinfo(sectorbuf + 512); 482 create_fsinfo(ipod_sectorbuf + 512);
483 483
484 /* Write boot sector and fsinfo at start of partition */ 484 /* Write boot sector and fsinfo at start of partition */
485 if (ipod_seek(ipod, ipod->pinfo[partition].start * ipod->sector_size) < 0) { 485 if (ipod_seek(ipod, ipod->pinfo[partition].start * ipod->sector_size) < 0) {
486 fprintf(stderr,"[ERR] Seek failed\n"); 486 fprintf(stderr,"[ERR] Seek failed\n");
487 return -1; 487 return -1;
488 } 488 }
489 if (ipod_write(ipod,sectorbuf,512 * 2) < 0) { 489 if (ipod_write(ipod,ipod_sectorbuf,512 * 2) < 0) {
490 perror("[ERR] Write failed (first copy of bootsect/fsinfo)\n"); 490 perror("[ERR] Write failed (first copy of bootsect/fsinfo)\n");
491 return -1; 491 return -1;
492 } 492 }
@@ -496,13 +496,13 @@ int format_partition(struct ipod_t* ipod, int partition)
496 fprintf(stderr,"[ERR] Seek failed\n"); 496 fprintf(stderr,"[ERR] Seek failed\n");
497 return -1; 497 return -1;
498 } 498 }
499 if (ipod_write(ipod,sectorbuf,512 * 2) < 0) { 499 if (ipod_write(ipod,ipod_sectorbuf,512 * 2) < 0) {
500 perror("[ERR] Write failed (first copy of bootsect/fsinfo)\n"); 500 perror("[ERR] Write failed (first copy of bootsect/fsinfo)\n");
501 return -1; 501 return -1;
502 } 502 }
503 503
504 /* Create the first FAT sector */ 504 /* Create the first FAT sector */
505 create_firstfatsector(sectorbuf); 505 create_firstfatsector(ipod_sectorbuf);
506 506
507 /* Write the first fat sector in the right places */ 507 /* Write the first fat sector in the right places */
508 for ( i=0; i<NumFATs; i++ ) { 508 for ( i=0; i<NumFATs; i++ ) {
@@ -513,7 +513,7 @@ int format_partition(struct ipod_t* ipod, int partition)
513 return -1; 513 return -1;
514 } 514 }
515 515
516 if (ipod_write(ipod,sectorbuf,512) < 0) { 516 if (ipod_write(ipod,ipod_sectorbuf,512) < 0) {
517 perror("[ERR] Write failed (first copy of bootsect/fsinfo)\n"); 517 perror("[ERR] Write failed (first copy of bootsect/fsinfo)\n");
518 return -1; 518 return -1;
519 } 519 }
diff --git a/rbutil/ipodpatcher/ipodpatcher.c b/rbutil/ipodpatcher/ipodpatcher.c
index d9048f24cc..981a223e73 100644
--- a/rbutil/ipodpatcher/ipodpatcher.c
+++ b/rbutil/ipodpatcher/ipodpatcher.c
@@ -46,9 +46,9 @@
46#include "arc4.h" 46#include "arc4.h"
47#endif 47#endif
48 48
49extern int verbose; 49int ipod_verbose = 0;
50 50
51unsigned char* sectorbuf; 51unsigned char* ipod_sectorbuf;
52 52
53/* The following string appears at the start of the firmware partition */ 53/* The following string appears at the start of the firmware partition */
54static const char *apple_stop_sign = "{{~~ /-----\\ "\ 54static const char *apple_stop_sign = "{{~~ /-----\\ "\
@@ -169,7 +169,7 @@ int read_partinfo(struct ipod_t* ipod, int silent)
169 int i; 169 int i;
170 unsigned long count; 170 unsigned long count;
171 171
172 count = ipod_read(ipod,sectorbuf, ipod->sector_size); 172 count = ipod_read(ipod,ipod_sectorbuf, ipod->sector_size);
173 173
174 if (count <= 0) { 174 if (count <= 0) {
175 print_error(" Error reading from disk: "); 175 print_error(" Error reading from disk: ");
@@ -178,12 +178,12 @@ int read_partinfo(struct ipod_t* ipod, int silent)
178 178
179 memset(ipod->pinfo, 0, sizeof(ipod->pinfo)); 179 memset(ipod->pinfo, 0, sizeof(ipod->pinfo));
180 180
181 if ((sectorbuf[510] == 0x55) && (sectorbuf[511] == 0xaa)) { 181 if ((ipod_sectorbuf[510] == 0x55) && (ipod_sectorbuf[511] == 0xaa)) {
182 /* DOS partition table */ 182 /* DOS partition table */
183 ipod->macpod = 0; 183 ipod->macpod = 0;
184 /* parse partitions */ 184 /* parse partitions */
185 for ( i = 0; i < 4; i++ ) { 185 for ( i = 0; i < 4; i++ ) {
186 unsigned char* ptr = sectorbuf + 0x1be + 16*i; 186 unsigned char* ptr = ipod_sectorbuf + 0x1be + 16*i;
187 ipod->pinfo[i].type = ptr[4]; 187 ipod->pinfo[i].type = ptr[4];
188 ipod->pinfo[i].start = BYTES2INT32(ptr, 8); 188 ipod->pinfo[i].start = BYTES2INT32(ptr, 8);
189 ipod->pinfo[i].size = BYTES2INT32(ptr, 12); 189 ipod->pinfo[i].size = BYTES2INT32(ptr, 12);
@@ -193,7 +193,7 @@ int read_partinfo(struct ipod_t* ipod, int silent)
193 /* not handled yet */ 193 /* not handled yet */
194 } 194 }
195 } 195 }
196 } else if ((sectorbuf[0] == 'E') && (sectorbuf[1] == 'R')) { 196 } else if ((ipod_sectorbuf[0] == 'E') && (ipod_sectorbuf[1] == 'R')) {
197 /* Apple Partition Map */ 197 /* Apple Partition Map */
198 198
199 /* APM parsing code based on the check_mac_partitions() function in 199 /* APM parsing code based on the check_mac_partitions() function in
@@ -202,7 +202,7 @@ int read_partinfo(struct ipod_t* ipod, int silent)
202 202
203 int blkNo = 1; 203 int blkNo = 1;
204 int partBlkCount = 1; 204 int partBlkCount = 1;
205 int partBlkSizMul = sectorbuf[2] / 2; 205 int partBlkSizMul = ipod_sectorbuf[2] / 2;
206 206
207 int pmMapBlkCnt; /* # of blks in partition map */ 207 int pmMapBlkCnt; /* # of blks in partition map */
208 int pmPyPartStart; /* physical start blk of partition */ 208 int pmPyPartStart; /* physical start blk of partition */
@@ -219,7 +219,7 @@ int read_partinfo(struct ipod_t* ipod, int silent)
219 return -1; 219 return -1;
220 } 220 }
221 221
222 count = ipod_read(ipod, sectorbuf, ipod->sector_size); 222 count = ipod_read(ipod, ipod_sectorbuf, ipod->sector_size);
223 223
224 if (count <= 0) { 224 if (count <= 0) {
225 print_error(" Error reading from disk: "); 225 print_error(" Error reading from disk: ");
@@ -227,26 +227,26 @@ int read_partinfo(struct ipod_t* ipod, int silent)
227 } 227 }
228 228
229 /* see if it's a partition entry */ 229 /* see if it's a partition entry */
230 if ((sectorbuf[0] != 'P') || (sectorbuf[1] != 'M')) { 230 if ((ipod_sectorbuf[0] != 'P') || (ipod_sectorbuf[1] != 'M')) {
231 /* end of partition table -> leave the loop */ 231 /* end of partition table -> leave the loop */
232 break; 232 break;
233 } 233 }
234 234
235 /* Extract the interesting entries */ 235 /* Extract the interesting entries */
236 pmMapBlkCnt = be2int(sectorbuf + 4); 236 pmMapBlkCnt = be2int(ipod_sectorbuf + 4);
237 pmPyPartStart = be2int(sectorbuf + 8); 237 pmPyPartStart = be2int(ipod_sectorbuf + 8);
238 pmPartBlkCnt = be2int(sectorbuf + 12); 238 pmPartBlkCnt = be2int(ipod_sectorbuf + 12);
239 239
240 /* update the number of part map blocks */ 240 /* update the number of part map blocks */
241 partBlkCount = pmMapBlkCnt; 241 partBlkCount = pmMapBlkCnt;
242 242
243 if (strncmp((char*)(sectorbuf + 48), "Apple_MDFW", 32)==0) { 243 if (strncmp((char*)(ipod_sectorbuf + 48), "Apple_MDFW", 32)==0) {
244 /* A Firmware partition */ 244 /* A Firmware partition */
245 ipod->pinfo[i].start = pmPyPartStart; 245 ipod->pinfo[i].start = pmPyPartStart;
246 ipod->pinfo[i].size = pmPartBlkCnt; 246 ipod->pinfo[i].size = pmPartBlkCnt;
247 ipod->pinfo[i].type = 0; 247 ipod->pinfo[i].type = 0;
248 i++; 248 i++;
249 } else if (strncmp((char*)(sectorbuf + 48), "Apple_HFS", 32)==0) { 249 } else if (strncmp((char*)(ipod_sectorbuf + 48), "Apple_HFS", 32)==0) {
250 /* A HFS partition */ 250 /* A HFS partition */
251 ipod->pinfo[i].start = pmPyPartStart; 251 ipod->pinfo[i].start = pmPyPartStart;
252 ipod->pinfo[i].size = pmPartBlkCnt; 252 ipod->pinfo[i].size = pmPartBlkCnt;
@@ -298,7 +298,7 @@ int read_partition(struct ipod_t* ipod, int outfile)
298 chunksize = bytesleft; 298 chunksize = bytesleft;
299 } 299 }
300 300
301 n = ipod_read(ipod, sectorbuf, chunksize); 301 n = ipod_read(ipod, ipod_sectorbuf, chunksize);
302 302
303 if (n < 0) { 303 if (n < 0) {
304 return -1; 304 return -1;
@@ -313,7 +313,7 @@ int read_partition(struct ipod_t* ipod, int outfile)
313 313
314 bytesleft -= n; 314 bytesleft -= n;
315 315
316 res = write(outfile,sectorbuf,n); 316 res = write(outfile,ipod_sectorbuf,n);
317 317
318 if (res < 0) { 318 if (res < 0) {
319 perror("[ERR] write in disk_read"); 319 perror("[ERR] write in disk_read");
@@ -348,7 +348,7 @@ int write_partition(struct ipod_t* ipod, int infile)
348 bytesread = 0; 348 bytesread = 0;
349 eof = 0; 349 eof = 0;
350 while (!eof) { 350 while (!eof) {
351 n = read(infile,sectorbuf,BUFFER_SIZE); 351 n = read(infile,ipod_sectorbuf,BUFFER_SIZE);
352 352
353 if (n < 0) { 353 if (n < 0) {
354 perror("[ERR] read in disk_write"); 354 perror("[ERR] read in disk_write");
@@ -366,7 +366,7 @@ int write_partition(struct ipod_t* ipod, int infile)
366 366
367 bytesread += n; 367 bytesread += n;
368 368
369 res = ipod_write(ipod, sectorbuf, n); 369 res = ipod_write(ipod, ipod_sectorbuf, n);
370 370
371 if (res < 0) { 371 if (res < 0) {
372 print_error(" Error writing to disk: "); 372 print_error(" Error writing to disk: ");
@@ -403,7 +403,7 @@ int diskmove(struct ipod_t* ipod, int delta)
403 (ipod->sector_size-1)) & ~(ipod->sector_size-1); 403 (ipod->sector_size-1)) & ~(ipod->sector_size-1);
404 bytesleft = src_end - src_start; 404 bytesleft = src_end - src_start;
405 405
406 if (verbose) { 406 if (ipod_verbose) {
407 fprintf(stderr,"[INFO] Need to move images 2-%d forward %08x bytes\n", ipod->nimages,delta); 407 fprintf(stderr,"[INFO] Need to move images 2-%d forward %08x bytes\n", ipod->nimages,delta);
408 fprintf(stderr,"[VERB] src_start = %08x\n",src_start); 408 fprintf(stderr,"[VERB] src_start = %08x\n",src_start);
409 fprintf(stderr,"[VERB] src_end = %08x\n",src_end); 409 fprintf(stderr,"[VERB] src_end = %08x\n",src_end);
@@ -419,7 +419,7 @@ int diskmove(struct ipod_t* ipod, int delta)
419 chunksize = BUFFER_SIZE; 419 chunksize = BUFFER_SIZE;
420 } 420 }
421 421
422 if (verbose) { 422 if (ipod_verbose) {
423 fprintf(stderr,"[VERB] Copying %08x bytes from %08x to %08x (absolute %08x to %08x)\n", 423 fprintf(stderr,"[VERB] Copying %08x bytes from %08x to %08x (absolute %08x to %08x)\n",
424 chunksize, 424 chunksize,
425 src_end-chunksize, 425 src_end-chunksize,
@@ -434,7 +434,7 @@ int diskmove(struct ipod_t* ipod, int delta)
434 return -1; 434 return -1;
435 } 435 }
436 436
437 if ((n = ipod_read(ipod,sectorbuf,chunksize)) < 0) { 437 if ((n = ipod_read(ipod,ipod_sectorbuf,chunksize)) < 0) {
438 perror("[ERR] Write failed\n"); 438 perror("[ERR] Write failed\n");
439 return -1; 439 return -1;
440 } 440 }
@@ -450,7 +450,7 @@ int diskmove(struct ipod_t* ipod, int delta)
450 return -1; 450 return -1;
451 } 451 }
452 452
453 if ((n = ipod_write(ipod,sectorbuf,chunksize)) < 0) { 453 if ((n = ipod_write(ipod,ipod_sectorbuf,chunksize)) < 0) {
454 perror("[ERR] Write failed\n"); 454 perror("[ERR] Write failed\n");
455 return -1; 455 return -1;
456 } 456 }
@@ -494,7 +494,7 @@ int add_bootloader(struct ipod_t* ipod, char* filename, int type)
494#ifdef WITH_BOOTOBJS 494#ifdef WITH_BOOTOBJS
495 if (type == FILETYPE_INTERNAL) { 495 if (type == FILETYPE_INTERNAL) {
496 fprintf(stderr,"[INFO] Using internal bootloader - %d bytes\n",ipod->bootloader_len); 496 fprintf(stderr,"[INFO] Using internal bootloader - %d bytes\n",ipod->bootloader_len);
497 memcpy(sectorbuf+entryOffset,ipod->bootloader,ipod->bootloader_len); 497 memcpy(ipod_sectorbuf+entryOffset,ipod->bootloader,ipod->bootloader_len);
498 length = ipod->bootloader_len; 498 length = ipod->bootloader_len;
499 paddedlength=(ipod->bootloader_len+ipod->sector_size-1)&~(ipod->sector_size-1); 499 paddedlength=(ipod->bootloader_len+ipod->sector_size-1)&~(ipod->sector_size-1);
500 } 500 }
@@ -567,7 +567,7 @@ int add_bootloader(struct ipod_t* ipod, char* filename, int type)
567 return -1; 567 return -1;
568 } 568 }
569 569
570 if (verbose) { 570 if (ipod_verbose) {
571 fprintf(stderr,"[VERB] Original firmware begins at 0x%08x\n", ipod->ipod_directory[0].devOffset + ipod->sector_size); 571 fprintf(stderr,"[VERB] Original firmware begins at 0x%08x\n", ipod->ipod_directory[0].devOffset + ipod->sector_size);
572 fprintf(stderr,"[VERB] New entryOffset will be 0x%08x\n",entryOffset); 572 fprintf(stderr,"[VERB] New entryOffset will be 0x%08x\n",entryOffset);
573 fprintf(stderr,"[VERB] End of bootloader will be at 0x%08x\n",entryOffset+paddedlength); 573 fprintf(stderr,"[VERB] End of bootloader will be at 0x%08x\n",entryOffset+paddedlength);
@@ -592,14 +592,14 @@ int add_bootloader(struct ipod_t* ipod, char* filename, int type)
592 592
593 /* We have moved the partitions, now we can write our bootloader */ 593 /* We have moved the partitions, now we can write our bootloader */
594 594
595 /* Firstly read the original firmware into sectorbuf */ 595 /* Firstly read the original firmware into ipod_sectorbuf */
596 fprintf(stderr,"[INFO] Reading original firmware...\n"); 596 fprintf(stderr,"[INFO] Reading original firmware...\n");
597 if (ipod_seek(ipod, ipod->fwoffset+ipod->ipod_directory[0].devOffset) < 0) { 597 if (ipod_seek(ipod, ipod->fwoffset+ipod->ipod_directory[0].devOffset) < 0) {
598 fprintf(stderr,"[ERR] Seek failed\n"); 598 fprintf(stderr,"[ERR] Seek failed\n");
599 return -1; 599 return -1;
600 } 600 }
601 601
602 if ((n = ipod_read(ipod,sectorbuf,entryOffset)) < 0) { 602 if ((n = ipod_read(ipod,ipod_sectorbuf,entryOffset)) < 0) {
603 perror("[ERR] Read failed\n"); 603 perror("[ERR] Read failed\n");
604 return -1; 604 return -1;
605 } 605 }
@@ -612,19 +612,19 @@ int add_bootloader(struct ipod_t* ipod, char* filename, int type)
612 612
613#ifdef WITH_BOOTOBJS 613#ifdef WITH_BOOTOBJS
614 if (type == FILETYPE_INTERNAL) { 614 if (type == FILETYPE_INTERNAL) {
615 memcpy(sectorbuf+entryOffset,ipod->bootloader,ipod->bootloader_len); 615 memcpy(ipod_sectorbuf+entryOffset,ipod->bootloader,ipod->bootloader_len);
616 } 616 }
617 else 617 else
618#endif 618#endif
619 { 619 {
620 memcpy(sectorbuf+entryOffset,bootloader_buf,length); 620 memcpy(ipod_sectorbuf+entryOffset,bootloader_buf,length);
621 free(bootloader_buf); 621 free(bootloader_buf);
622 } 622 }
623 623
624 /* Calculate new checksum for combined image */ 624 /* Calculate new checksum for combined image */
625 chksum = 0; 625 chksum = 0;
626 for (i=0;i<entryOffset + length; i++) { 626 for (i=0;i<entryOffset + length; i++) {
627 chksum += sectorbuf[i]; 627 chksum += ipod_sectorbuf[i];
628 } 628 }
629 629
630 /* Now write the combined firmware image to the disk */ 630 /* Now write the combined firmware image to the disk */
@@ -634,7 +634,7 @@ int add_bootloader(struct ipod_t* ipod, char* filename, int type)
634 return -1; 634 return -1;
635 } 635 }
636 636
637 if ((n = ipod_write(ipod,sectorbuf,entryOffset+paddedlength)) < 0) { 637 if ((n = ipod_write(ipod,ipod_sectorbuf,entryOffset+paddedlength)) < 0) {
638 perror("[ERR] Write failed\n"); 638 perror("[ERR] Write failed\n");
639 return -1; 639 return -1;
640 } 640 }
@@ -655,22 +655,22 @@ int add_bootloader(struct ipod_t* ipod, char* filename, int type)
655 return -1; 655 return -1;
656 } 656 }
657 657
658 n=ipod_read(ipod, sectorbuf, ipod->sector_size); 658 n=ipod_read(ipod, ipod_sectorbuf, ipod->sector_size);
659 if (n < 0) { 659 if (n < 0) {
660 fprintf(stderr,"[ERR] Directory read failed\n"); 660 fprintf(stderr,"[ERR] Directory read failed\n");
661 return -1; 661 return -1;
662 } 662 }
663 663
664 /* Update entries for image 0 */ 664 /* Update entries for image 0 */
665 int2le(entryOffset+length,sectorbuf+x+16); 665 int2le(entryOffset+length,ipod_sectorbuf+x+16);
666 int2le(entryOffset,sectorbuf+x+24); 666 int2le(entryOffset,ipod_sectorbuf+x+24);
667 int2le(chksum,sectorbuf+x+28); 667 int2le(chksum,ipod_sectorbuf+x+28);
668 int2le(0xffffffff,sectorbuf+x+36); /* loadAddr */ 668 int2le(0xffffffff,ipod_sectorbuf+x+36); /* loadAddr */
669 669
670 /* Update devOffset entries for other images, if we have moved them */ 670 /* Update devOffset entries for other images, if we have moved them */
671 if (delta > 0) { 671 if (delta > 0) {
672 for (i=1;i<ipod->nimages;i++) { 672 for (i=1;i<ipod->nimages;i++) {
673 int2le(le2int(sectorbuf+x+i*40+12)+delta,sectorbuf+x+i*40+12); 673 int2le(le2int(ipod_sectorbuf+x+i*40+12)+delta,ipod_sectorbuf+x+i*40+12);
674 } 674 }
675 } 675 }
676 676
@@ -679,7 +679,7 @@ int add_bootloader(struct ipod_t* ipod, char* filename, int type)
679 fprintf(stderr,"[ERR] Seek to %d failed\n", (int)(ipod->start+ipod->diroffset-x)); 679 fprintf(stderr,"[ERR] Seek to %d failed\n", (int)(ipod->start+ipod->diroffset-x));
680 return -1; 680 return -1;
681 } 681 }
682 n=ipod_write(ipod, sectorbuf, ipod->sector_size); 682 n=ipod_write(ipod, ipod_sectorbuf, ipod->sector_size);
683 if (n < 0) { 683 if (n < 0) {
684 fprintf(stderr,"[ERR] Directory write failed\n"); 684 fprintf(stderr,"[ERR] Directory write failed\n");
685 return -1; 685 return -1;
@@ -720,7 +720,7 @@ int delete_bootloader(struct ipod_t* ipod)
720 fprintf(stderr,"[INFO] Padding read from 0x%08x to 0x%08x bytes\n", 720 fprintf(stderr,"[INFO] Padding read from 0x%08x to 0x%08x bytes\n",
721 length,i); 721 length,i);
722 722
723 if ((n = ipod_read(ipod,sectorbuf,i)) < 0) { 723 if ((n = ipod_read(ipod,ipod_sectorbuf,i)) < 0) {
724 return -1; 724 return -1;
725 } 725 }
726 726
@@ -733,7 +733,7 @@ int delete_bootloader(struct ipod_t* ipod)
733 chksum = 0; 733 chksum = 0;
734 for (i = 0; i < length; i++) { 734 for (i = 0; i < length; i++) {
735 /* add 8 unsigned bits but keep a 32 bit sum */ 735 /* add 8 unsigned bits but keep a 32 bit sum */
736 chksum += sectorbuf[i]; 736 chksum += ipod_sectorbuf[i];
737 } 737 }
738 738
739 /* Now write back the updated directory entry */ 739 /* Now write back the updated directory entry */
@@ -745,17 +745,17 @@ int delete_bootloader(struct ipod_t* ipod)
745 /* Read directory */ 745 /* Read directory */
746 if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) { return -1; } 746 if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) { return -1; }
747 747
748 n=ipod_read(ipod, sectorbuf, ipod->sector_size); 748 n=ipod_read(ipod, ipod_sectorbuf, ipod->sector_size);
749 if (n < 0) { return -1; } 749 if (n < 0) { return -1; }
750 750
751 /* Update entries for image 0 */ 751 /* Update entries for image 0 */
752 int2le(length,sectorbuf+x+16); 752 int2le(length,ipod_sectorbuf+x+16);
753 int2le(0,sectorbuf+x+24); 753 int2le(0,ipod_sectorbuf+x+24);
754 int2le(chksum,sectorbuf+x+28); 754 int2le(chksum,ipod_sectorbuf+x+28);
755 755
756 /* Write directory */ 756 /* Write directory */
757 if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) { return -1; } 757 if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) { return -1; }
758 n=ipod_write(ipod, sectorbuf, ipod->sector_size); 758 n=ipod_write(ipod, ipod_sectorbuf, ipod->sector_size);
759 if (n < 0) { return -1; } 759 if (n < 0) { return -1; }
760 760
761 return 0; 761 return 0;
@@ -840,14 +840,14 @@ int write_firmware(struct ipod_t* ipod, char* filename, int type)
840 840
841#ifdef WITH_BOOTOBJS 841#ifdef WITH_BOOTOBJS
842 if (type == FILETYPE_INTERNAL) { 842 if (type == FILETYPE_INTERNAL) {
843 memcpy(sectorbuf,ipod->bootloader,ipod->bootloader_len); 843 memcpy(ipod_sectorbuf,ipod->bootloader,ipod->bootloader_len);
844 } 844 }
845 else 845 else
846#endif 846#endif
847 { 847 {
848 fprintf(stderr,"[INFO] Reading input file...\n"); 848 fprintf(stderr,"[INFO] Reading input file...\n");
849 /* We now know we have enough space, so write it. */ 849 /* We now know we have enough space, so write it. */
850 n = read(infile,sectorbuf,length); 850 n = read(infile,ipod_sectorbuf,length);
851 if (n < 0) { 851 if (n < 0) {
852 fprintf(stderr,"[ERR] Couldn't read input file\n"); 852 fprintf(stderr,"[ERR] Couldn't read input file\n");
853 close(infile); 853 close(infile);
@@ -857,13 +857,13 @@ int write_firmware(struct ipod_t* ipod, char* filename, int type)
857 } 857 }
858 858
859 /* Pad the data with zeros */ 859 /* Pad the data with zeros */
860 memset(sectorbuf+length,0,newsize-length); 860 memset(ipod_sectorbuf+length,0,newsize-length);
861 861
862 if (type==FILETYPE_DOT_IPOD) { 862 if (type==FILETYPE_DOT_IPOD) {
863 chksum = ipod->modelnum; 863 chksum = ipod->modelnum;
864 for (i = 0; i < length; i++) { 864 for (i = 0; i < length; i++) {
865 /* add 8 unsigned bits but keep a 32 bit sum */ 865 /* add 8 unsigned bits but keep a 32 bit sum */
866 chksum += sectorbuf[i]; 866 chksum += ipod_sectorbuf[i];
867 } 867 }
868 868
869 if (chksum == filechksum) { 869 if (chksum == filechksum) {
@@ -879,7 +879,7 @@ int write_firmware(struct ipod_t* ipod, char* filename, int type)
879 return -1; 879 return -1;
880 } 880 }
881 881
882 if ((n = ipod_write(ipod,sectorbuf,newsize)) < 0) { 882 if ((n = ipod_write(ipod,ipod_sectorbuf,newsize)) < 0) {
883 perror("[ERR] Write failed\n"); 883 perror("[ERR] Write failed\n");
884 return -1; 884 return -1;
885 } 885 }
@@ -895,7 +895,7 @@ int write_firmware(struct ipod_t* ipod, char* filename, int type)
895 chksum = 0; 895 chksum = 0;
896 for (i = 0; i < length; i++) { 896 for (i = 0; i < length; i++) {
897 /* add 8 unsigned bits but keep a 32 bit sum */ 897 /* add 8 unsigned bits but keep a 32 bit sum */
898 chksum += sectorbuf[i]; 898 chksum += ipod_sectorbuf[i];
899 } 899 }
900 900
901 x = ipod->diroffset % ipod->sector_size; 901 x = ipod->diroffset % ipod->sector_size;
@@ -903,17 +903,17 @@ int write_firmware(struct ipod_t* ipod, char* filename, int type)
903 /* Read directory */ 903 /* Read directory */
904 if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) { return -1; } 904 if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) { return -1; }
905 905
906 n=ipod_read(ipod, sectorbuf, ipod->sector_size); 906 n=ipod_read(ipod, ipod_sectorbuf, ipod->sector_size);
907 if (n < 0) { return -1; } 907 if (n < 0) { return -1; }
908 908
909 /* Update entries for image 0 */ 909 /* Update entries for image 0 */
910 int2le(length,sectorbuf+x+16); 910 int2le(length,ipod_sectorbuf+x+16);
911 int2le(0,sectorbuf+x+24); 911 int2le(0,ipod_sectorbuf+x+24);
912 int2le(chksum,sectorbuf+x+28); 912 int2le(chksum,ipod_sectorbuf+x+28);
913 913
914 /* Write directory */ 914 /* Write directory */
915 if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) { return -1; } 915 if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) { return -1; }
916 n=ipod_write(ipod, sectorbuf, ipod->sector_size); 916 n=ipod_write(ipod, ipod_sectorbuf, ipod->sector_size);
917 if (n < 0) { return -1; } 917 if (n < 0) { return -1; }
918 918
919 return 0; 919 return 0;
@@ -945,7 +945,7 @@ int read_firmware(struct ipod_t* ipod, char* filename, int type)
945 fprintf(stderr,"[INFO] Padding read from 0x%08x to 0x%08x bytes\n", 945 fprintf(stderr,"[INFO] Padding read from 0x%08x to 0x%08x bytes\n",
946 length,i); 946 length,i);
947 947
948 if ((n = ipod_read(ipod,sectorbuf,i)) < 0) { 948 if ((n = ipod_read(ipod,ipod_sectorbuf,i)) < 0) {
949 return -1; 949 return -1;
950 } 950 }
951 951
@@ -965,7 +965,7 @@ int read_firmware(struct ipod_t* ipod, char* filename, int type)
965 chksum = ipod->modelnum; 965 chksum = ipod->modelnum;
966 for (i = 0; i < length; i++) { 966 for (i = 0; i < length; i++) {
967 /* add 8 unsigned bits but keep a 32 bit sum */ 967 /* add 8 unsigned bits but keep a 32 bit sum */
968 chksum += sectorbuf[i]; 968 chksum += ipod_sectorbuf[i];
969 } 969 }
970 970
971 int2be(chksum,header); 971 int2be(chksum,header);
@@ -977,7 +977,7 @@ int read_firmware(struct ipod_t* ipod, char* filename, int type)
977 } 977 }
978 } 978 }
979 979
980 n = write(outfile,sectorbuf,length); 980 n = write(outfile,ipod_sectorbuf,length);
981 if (n != length) { 981 if (n != length) {
982 fprintf(stderr,"[ERR] Write error - %d\n",n); 982 fprintf(stderr,"[ERR] Write error - %d\n",n);
983 } 983 }
@@ -1004,28 +1004,28 @@ int read_directory(struct ipod_t* ipod)
1004 return -1; 1004 return -1;
1005 } 1005 }
1006 1006
1007 n=ipod_read(ipod, sectorbuf, ipod->sector_size); 1007 n=ipod_read(ipod, ipod_sectorbuf, ipod->sector_size);
1008 if (n < 0) { 1008 if (n < 0) {
1009 fprintf(stderr,"[ERR] ipod_read(ipod,buf,0x%08x) failed in read_directory()\n", ipod->sector_size); 1009 fprintf(stderr,"[ERR] ipod_read(ipod,buf,0x%08x) failed in read_directory()\n", ipod->sector_size);
1010 return -1; 1010 return -1;
1011 } 1011 }
1012 1012
1013 if (memcmp(sectorbuf,apple_stop_sign,sizeof(apple_stop_sign))!=0) { 1013 if (memcmp(ipod_sectorbuf,apple_stop_sign,sizeof(apple_stop_sign))!=0) {
1014 fprintf(stderr,"[ERR] Firmware partition doesn't contain Apple copyright, aborting.\n"); 1014 fprintf(stderr,"[ERR] Firmware partition doesn't contain Apple copyright, aborting.\n");
1015 return -1; 1015 return -1;
1016 } 1016 }
1017 1017
1018 if (memcmp(sectorbuf+0x100,"]ih[",4)!=0) { 1018 if (memcmp(ipod_sectorbuf+0x100,"]ih[",4)!=0) {
1019 fprintf(stderr,"[ERR] Bad firmware directory\n"); 1019 fprintf(stderr,"[ERR] Bad firmware directory\n");
1020 return -1; 1020 return -1;
1021 } 1021 }
1022 1022
1023 version = le2ushort(sectorbuf+0x10a); 1023 version = le2ushort(ipod_sectorbuf+0x10a);
1024 if ((version != 2) && (version != 3)) { 1024 if ((version != 2) && (version != 3)) {
1025 fprintf(stderr,"[ERR] Unknown firmware format version %04x\n", 1025 fprintf(stderr,"[ERR] Unknown firmware format version %04x\n",
1026 version); 1026 version);
1027 } 1027 }
1028 ipod->diroffset=le2int(sectorbuf+0x104) + 0x200; 1028 ipod->diroffset=le2int(ipod_sectorbuf+0x104) + 0x200;
1029 1029
1030 /* diroffset may not be sector-aligned */ 1030 /* diroffset may not be sector-aligned */
1031 x = ipod->diroffset % ipod->sector_size; 1031 x = ipod->diroffset % ipod->sector_size;
@@ -1036,26 +1036,26 @@ int read_directory(struct ipod_t* ipod)
1036 return -1; 1036 return -1;
1037 } 1037 }
1038 1038
1039 n=ipod_read(ipod, sectorbuf, ipod->sector_size); 1039 n=ipod_read(ipod, ipod_sectorbuf, ipod->sector_size);
1040 if (n < 0) { 1040 if (n < 0) {
1041 fprintf(stderr,"[ERR] Read of directory failed.\n"); 1041 fprintf(stderr,"[ERR] Read of directory failed.\n");
1042 return -1; 1042 return -1;
1043 } 1043 }
1044 1044
1045 p = sectorbuf + x; 1045 p = ipod_sectorbuf + x;
1046 1046
1047 /* A hack to detect 2nd gen Nanos - maybe there is a better way? */ 1047 /* A hack to detect 2nd gen Nanos - maybe there is a better way? */
1048 if (p[0] == 0) 1048 if (p[0] == 0)
1049 { 1049 {
1050 n=ipod_read(ipod, sectorbuf, ipod->sector_size); 1050 n=ipod_read(ipod, ipod_sectorbuf, ipod->sector_size);
1051 if (n < 0) { 1051 if (n < 0) {
1052 fprintf(stderr,"[ERR] Read of directory failed.\n"); 1052 fprintf(stderr,"[ERR] Read of directory failed.\n");
1053 return -1; 1053 return -1;
1054 } 1054 }
1055 p = sectorbuf; 1055 p = ipod_sectorbuf;
1056 } 1056 }
1057 1057
1058 while ((ipod->nimages < MAX_IMAGES) && (p < (sectorbuf + x + 400)) && 1058 while ((ipod->nimages < MAX_IMAGES) && (p < (ipod_sectorbuf + x + 400)) &&
1059 ((memcmp(p,"!ATA",4)==0) || (memcmp(p,"DNAN",4)==0))) { 1059 ((memcmp(p,"!ATA",4)==0) || (memcmp(p,"DNAN",4)==0))) {
1060 p+=4; 1060 p+=4;
1061 if (memcmp(p,"soso",4)==0) { 1061 if (memcmp(p,"soso",4)==0) {
@@ -1109,7 +1109,7 @@ int list_images(struct ipod_t* ipod)
1109{ 1109{
1110 int i; 1110 int i;
1111 1111
1112 if (verbose) { 1112 if (ipod_verbose) {
1113 printf(" Type id devOffset len addr entryOffset chksum vers loadAddr devOffset+len\n"); 1113 printf(" Type id devOffset len addr entryOffset chksum vers loadAddr devOffset+len\n");
1114 for (i = 0 ; i < ipod->nimages; i++) { 1114 for (i = 0 ; i < ipod->nimages; i++) {
1115 printf("%d - %s 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",i, 1115 printf("%d - %s 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",i,
@@ -1359,12 +1359,12 @@ int write_dos_partition_table(struct ipod_t* ipod)
1359 } 1359 }
1360 1360
1361 /* Firstly zero the entire MBR */ 1361 /* Firstly zero the entire MBR */
1362 memset(sectorbuf, 0, ipod->sector_size); 1362 memset(ipod_sectorbuf, 0, ipod->sector_size);
1363 1363
1364 /* Now add the partition info */ 1364 /* Now add the partition info */
1365 for (i=0; i < 4 ; i++) 1365 for (i=0; i < 4 ; i++)
1366 { 1366 {
1367 p = sectorbuf + 0x1be + i*16; 1367 p = ipod_sectorbuf + 0x1be + i*16;
1368 1368
1369 /* Ensure first partition is type 0, and second is 0xb */ 1369 /* Ensure first partition is type 0, and second is 0xb */
1370 if (i==0) { type = 0; } 1370 if (i==0) { type = 0; }
@@ -1377,8 +1377,8 @@ int write_dos_partition_table(struct ipod_t* ipod)
1377 } 1377 }
1378 1378
1379 /* Finally add the magic */ 1379 /* Finally add the magic */
1380 sectorbuf[0x1fe] = 0x55; 1380 ipod_sectorbuf[0x1fe] = 0x55;
1381 sectorbuf[0x1ff] = 0xaa; 1381 ipod_sectorbuf[0x1ff] = 0xaa;
1382 1382
1383 if (ipod_seek(ipod, 0) < 0) { 1383 if (ipod_seek(ipod, 0) < 0) {
1384 fprintf(stderr,"[ERR] Seek failed writing MBR\n"); 1384 fprintf(stderr,"[ERR] Seek failed writing MBR\n");
@@ -1386,7 +1386,7 @@ int write_dos_partition_table(struct ipod_t* ipod)
1386 } 1386 }
1387 1387
1388 /* Write MBR */ 1388 /* Write MBR */
1389 if ((n = ipod_write(ipod, sectorbuf, ipod->sector_size)) < 0) { 1389 if ((n = ipod_write(ipod, ipod_sectorbuf, ipod->sector_size)) < 0) {
1390 perror("[ERR] Write failed\n"); 1390 perror("[ERR] Write failed\n");
1391 return -1; 1391 return -1;
1392 } 1392 }
@@ -1514,11 +1514,11 @@ static int find_key(struct ipod_t* ipod, int aupd, unsigned char* key)
1514 return -1; 1514 return -1;
1515 } 1515 }
1516 1516
1517 if ((n = ipod_read(ipod, sectorbuf, 512)) < 0) { 1517 if ((n = ipod_read(ipod, ipod_sectorbuf, 512)) < 0) {
1518 return -1; 1518 return -1;
1519 } 1519 }
1520 1520
1521 n = GetSecurityBlockKey(sectorbuf, key); 1521 n = GetSecurityBlockKey(ipod_sectorbuf, key);
1522 1522
1523 if (n != 1) 1523 if (n != 1)
1524 { 1524 {
@@ -1569,7 +1569,7 @@ int read_aupd(struct ipod_t* ipod, char* filename)
1569 1569
1570 i = (length+ipod->sector_size-1) & ~(ipod->sector_size-1); 1570 i = (length+ipod->sector_size-1) & ~(ipod->sector_size-1);
1571 1571
1572 if ((n = ipod_read(ipod,sectorbuf,i)) < 0) { 1572 if ((n = ipod_read(ipod,ipod_sectorbuf,i)) < 0) {
1573 return -1; 1573 return -1;
1574 } 1574 }
1575 1575
@@ -1581,12 +1581,12 @@ int read_aupd(struct ipod_t* ipod, char* filename)
1581 1581
1582 /* Perform the decryption - this is standard (A)RC4 */ 1582 /* Perform the decryption - this is standard (A)RC4 */
1583 matrixArc4Init(&rc4, key, 4); 1583 matrixArc4Init(&rc4, key, 4);
1584 matrixArc4(&rc4, sectorbuf, sectorbuf, length); 1584 matrixArc4(&rc4, ipod_sectorbuf, ipod_sectorbuf, length);
1585 1585
1586 chksum = 0; 1586 chksum = 0;
1587 for (i = 0; i < (int)length; i++) { 1587 for (i = 0; i < (int)length; i++) {
1588 /* add 8 unsigned bits but keep a 32 bit sum */ 1588 /* add 8 unsigned bits but keep a 32 bit sum */
1589 chksum += sectorbuf[i]; 1589 chksum += ipod_sectorbuf[i];
1590 } 1590 }
1591 1591
1592 if (chksum != ipod->ipod_directory[aupd].chksum) 1592 if (chksum != ipod->ipod_directory[aupd].chksum)
@@ -1602,7 +1602,7 @@ int read_aupd(struct ipod_t* ipod, char* filename)
1602 return -1; 1602 return -1;
1603 } 1603 }
1604 1604
1605 n = write(outfile,sectorbuf,length); 1605 n = write(outfile,ipod_sectorbuf,length);
1606 if (n != length) { 1606 if (n != length) {
1607 fprintf(stderr,"[ERR] Write error - %d\n",n); 1607 fprintf(stderr,"[ERR] Write error - %d\n",n);
1608 } 1608 }
@@ -1673,7 +1673,7 @@ int write_aupd(struct ipod_t* ipod, char* filename)
1673 /* We now know we have enough space, so write it. */ 1673 /* We now know we have enough space, so write it. */
1674 1674
1675 fprintf(stderr,"[INFO] Reading input file...\n"); 1675 fprintf(stderr,"[INFO] Reading input file...\n");
1676 n = read(infile,sectorbuf,length); 1676 n = read(infile,ipod_sectorbuf,length);
1677 if (n < 0) { 1677 if (n < 0) {
1678 fprintf(stderr,"[ERR] Couldn't read input file\n"); 1678 fprintf(stderr,"[ERR] Couldn't read input file\n");
1679 close(infile); 1679 close(infile);
@@ -1682,25 +1682,25 @@ int write_aupd(struct ipod_t* ipod, char* filename)
1682 close(infile); 1682 close(infile);
1683 1683
1684 /* Pad the data with zeros */ 1684 /* Pad the data with zeros */
1685 memset(sectorbuf+length,0,newsize-length); 1685 memset(ipod_sectorbuf+length,0,newsize-length);
1686 1686
1687 /* Calculate the new checksum (before we encrypt) */ 1687 /* Calculate the new checksum (before we encrypt) */
1688 chksum = 0; 1688 chksum = 0;
1689 for (i = 0; i < (int)length; i++) { 1689 for (i = 0; i < (int)length; i++) {
1690 /* add 8 unsigned bits but keep a 32 bit sum */ 1690 /* add 8 unsigned bits but keep a 32 bit sum */
1691 chksum += sectorbuf[i]; 1691 chksum += ipod_sectorbuf[i];
1692 } 1692 }
1693 1693
1694 /* Perform the encryption - this is standard (A)RC4 */ 1694 /* Perform the encryption - this is standard (A)RC4 */
1695 matrixArc4Init(&rc4, key, 4); 1695 matrixArc4Init(&rc4, key, 4);
1696 matrixArc4(&rc4, sectorbuf, sectorbuf, length); 1696 matrixArc4(&rc4, ipod_sectorbuf, ipod_sectorbuf, length);
1697 1697
1698 if (ipod_seek(ipod, ipod->fwoffset+ipod->ipod_directory[aupd].devOffset) < 0) { 1698 if (ipod_seek(ipod, ipod->fwoffset+ipod->ipod_directory[aupd].devOffset) < 0) {
1699 fprintf(stderr,"[ERR] Seek failed\n"); 1699 fprintf(stderr,"[ERR] Seek failed\n");
1700 return -1; 1700 return -1;
1701 } 1701 }
1702 1702
1703 if ((n = ipod_write(ipod,sectorbuf,newsize)) < 0) { 1703 if ((n = ipod_write(ipod,ipod_sectorbuf,newsize)) < 0) {
1704 perror("[ERR] Write failed\n"); 1704 perror("[ERR] Write failed\n");
1705 return -1; 1705 return -1;
1706 } 1706 }
@@ -1717,16 +1717,16 @@ int write_aupd(struct ipod_t* ipod, char* filename)
1717 /* Read directory */ 1717 /* Read directory */
1718 if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) { return -1; } 1718 if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) { return -1; }
1719 1719
1720 n=ipod_read(ipod, sectorbuf, ipod->sector_size); 1720 n=ipod_read(ipod, ipod_sectorbuf, ipod->sector_size);
1721 if (n < 0) { return -1; } 1721 if (n < 0) { return -1; }
1722 1722
1723 /* Update checksum */ 1723 /* Update checksum */
1724 fprintf(stderr,"[INFO] Updating checksum to 0x%08x (was 0x%08x)\n",(unsigned int)chksum,le2int(sectorbuf + x + aupd*40 + 28)); 1724 fprintf(stderr,"[INFO] Updating checksum to 0x%08x (was 0x%08x)\n",(unsigned int)chksum,le2int(ipod_sectorbuf + x + aupd*40 + 28));
1725 int2le(chksum,sectorbuf+x+aupd*40+28); 1725 int2le(chksum,ipod_sectorbuf+x+aupd*40+28);
1726 1726
1727 /* Write directory */ 1727 /* Write directory */
1728 if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) { return -1; } 1728 if (ipod_seek(ipod, ipod->start + ipod->diroffset - x) < 0) { return -1; }
1729 n=ipod_write(ipod, sectorbuf, ipod->sector_size); 1729 n=ipod_write(ipod, ipod_sectorbuf, ipod->sector_size);
1730 if (n < 0) { return -1; } 1730 if (n < 0) { return -1; }
1731 1731
1732 return 0; 1732 return 0;
diff --git a/rbutil/ipodpatcher/ipodpatcher.h b/rbutil/ipodpatcher/ipodpatcher.h
index 91a28ecea9..54f4f6b7e3 100644
--- a/rbutil/ipodpatcher/ipodpatcher.h
+++ b/rbutil/ipodpatcher/ipodpatcher.h
@@ -29,7 +29,8 @@ extern "C" {
29/* Size of buffer for disk I/O - 8MB is large enough for any version 29/* Size of buffer for disk I/O - 8MB is large enough for any version
30 of the Apple firmware, but not the Nano's RSRC image. */ 30 of the Apple firmware, but not the Nano's RSRC image. */
31#define BUFFER_SIZE 8*1024*1024 31#define BUFFER_SIZE 8*1024*1024
32extern unsigned char* sectorbuf; 32extern unsigned char* ipod_sectorbuf;
33extern int ipod_verbose;
33 34
34#define FILETYPE_DOT_IPOD 0 35#define FILETYPE_DOT_IPOD 0
35#define FILETYPE_DOT_BIN 1 36#define FILETYPE_DOT_BIN 1
diff --git a/rbutil/ipodpatcher/main.c b/rbutil/ipodpatcher/main.c
index 6b3e8ddda6..585f7277dd 100644
--- a/rbutil/ipodpatcher/main.c
+++ b/rbutil/ipodpatcher/main.c
@@ -31,8 +31,6 @@
31 31
32#define VERSION "2.0 with v2.0 bootloaders" 32#define VERSION "2.0 with v2.0 bootloaders"
33 33
34int verbose = 0;
35
36enum { 34enum {
37 NONE, 35 NONE,
38#ifdef WITH_BOOTOBJS 36#ifdef WITH_BOOTOBJS
@@ -155,7 +153,7 @@ int main(int argc, char* argv[])
155 return 1; 153 return 1;
156 } 154 }
157 155
158 if (ipod_alloc_buffer(&sectorbuf,BUFFER_SIZE) < 0) { 156 if (ipod_alloc_buffer(&ipod_sectorbuf,BUFFER_SIZE) < 0) {
159 fprintf(stderr,"Failed to allocate memory buffer\n"); 157 fprintf(stderr,"Failed to allocate memory buffer\n");
160 } 158 }
161 159
@@ -297,7 +295,7 @@ int main(int argc, char* argv[])
297 i++; 295 i++;
298 } else if ((strcmp(argv[i],"-v")==0) || 296 } else if ((strcmp(argv[i],"-v")==0) ||
299 (strcmp(argv[i],"--verbose")==0)) { 297 (strcmp(argv[i],"--verbose")==0)) {
300 verbose++; 298 ipod_verbose++;
301 i++; 299 i++;
302 } else if ((strcmp(argv[i],"-f")==0) || 300 } else if ((strcmp(argv[i],"-f")==0) ||
303 (strcmp(argv[i],"--format")==0)) { 301 (strcmp(argv[i],"--format")==0)) {
diff --git a/rbutil/rbutilqt/installbootloader.cpp b/rbutil/rbutilqt/installbootloader.cpp
index dae82b6fd2..ea9bf15ad1 100644
--- a/rbutil/rbutilqt/installbootloader.cpp
+++ b/rbutil/rbutilqt/installbootloader.cpp
@@ -807,7 +807,7 @@ int verbose =0;
807// reserves memory for ipodpatcher 807// reserves memory for ipodpatcher
808bool initIpodpatcher() 808bool initIpodpatcher()
809{ 809{
810 if (ipod_alloc_buffer(&sectorbuf,BUFFER_SIZE) < 0) return true; 810 if (ipod_alloc_buffer(&ipod_sectorbuf,BUFFER_SIZE) < 0) return true;
811 else return false; 811 else return false;
812} 812}
813 813
@@ -1050,7 +1050,7 @@ void BootloaderInstaller::ipodFinish()
1050// reserves memory for sansapatcher 1050// reserves memory for sansapatcher
1051bool initSansapatcher() 1051bool initSansapatcher()
1052{ 1052{
1053 if (sansa_alloc_buffer(&sectorbuf,BUFFER_SIZE) < 0) return true; 1053 if (sansa_alloc_buffer(&sansa_sectorbuf,BUFFER_SIZE) < 0) return true;
1054 else return false; 1054 else return false;
1055} 1055}
1056 1056
diff --git a/rbutil/sansapatcher/main.c b/rbutil/sansapatcher/main.c
index b813fadbe6..83eb8f80db 100644
--- a/rbutil/sansapatcher/main.c
+++ b/rbutil/sansapatcher/main.c
@@ -32,10 +32,6 @@
32 32
33#define VERSION "0.6 with v4.0 bootloaders" 33#define VERSION "0.6 with v4.0 bootloaders"
34 34
35unsigned char* sectorbuf;
36
37int verbose = 0;
38
39enum { 35enum {
40 NONE, 36 NONE,
41 INSTALL, 37 INSTALL,
@@ -150,7 +146,7 @@ int main(int argc, char* argv[])
150 return 1; 146 return 1;
151 } 147 }
152 148
153 if (sansa_alloc_buffer(&sectorbuf,BUFFER_SIZE) < 0) { 149 if (sansa_alloc_buffer(&sansa_sectorbuf,BUFFER_SIZE) < 0) {
154 fprintf(stderr,"Failed to allocate memory buffer\n"); 150 fprintf(stderr,"Failed to allocate memory buffer\n");
155 } 151 }
156 152
diff --git a/rbutil/sansapatcher/sansapatcher.c b/rbutil/sansapatcher/sansapatcher.c
index ee15a16ca9..b251ee5959 100644
--- a/rbutil/sansapatcher/sansapatcher.c
+++ b/rbutil/sansapatcher/sansapatcher.c
@@ -38,14 +38,14 @@
38#define NVPARAMS_OFFSET 0x780000 38#define NVPARAMS_OFFSET 0x780000
39#define NVPARAMS_SIZE (0x80000-0x200) 39#define NVPARAMS_SIZE (0x80000-0x200)
40 40
41extern int verbose; 41int sansa_verbose = 0;
42 42
43/* Windows requires the buffer for disk I/O to be aligned in memory on a 43/* Windows requires the buffer for disk I/O to be aligned in memory on a
44 multiple of the disk volume size - so we use a single global variable 44 multiple of the disk volume size - so we use a single global variable
45 and initialise it with sansa_alloc_buf() in main(). 45 and initialise it with sansa_alloc_buf() in main().
46*/ 46*/
47 47
48extern unsigned char* sectorbuf; 48unsigned char* sansa_sectorbuf;
49 49
50static off_t filesize(int fd) { 50static off_t filesize(int fd) {
51 struct stat buf; 51 struct stat buf;
@@ -94,17 +94,17 @@ int sansa_read_partinfo(struct sansa_t* sansa, int silent)
94 int i; 94 int i;
95 unsigned long count; 95 unsigned long count;
96 96
97 count = sansa_read(sansa,sectorbuf, sansa->sector_size); 97 count = sansa_read(sansa,sansa_sectorbuf, sansa->sector_size);
98 98
99 if (count <= 0) { 99 if (count <= 0) {
100 print_error(" Error reading from disk: "); 100 print_error(" Error reading from disk: ");
101 return -1; 101 return -1;
102 } 102 }
103 103
104 if ((sectorbuf[510] == 0x55) && (sectorbuf[511] == 0xaa)) { 104 if ((sansa_sectorbuf[510] == 0x55) && (sansa_sectorbuf[511] == 0xaa)) {
105 /* parse partitions */ 105 /* parse partitions */
106 for ( i = 0; i < 4; i++ ) { 106 for ( i = 0; i < 4; i++ ) {
107 unsigned char* ptr = sectorbuf + 0x1be + 16*i; 107 unsigned char* ptr = sansa_sectorbuf + 0x1be + 16*i;
108 sansa->pinfo[i].type = ptr[4]; 108 sansa->pinfo[i].type = ptr[4];
109 sansa->pinfo[i].start = BYTES2INT32(ptr, 8); 109 sansa->pinfo[i].start = BYTES2INT32(ptr, 8);
110 sansa->pinfo[i].size = BYTES2INT32(ptr, 12); 110 sansa->pinfo[i].size = BYTES2INT32(ptr, 12);
@@ -114,7 +114,7 @@ int sansa_read_partinfo(struct sansa_t* sansa, int silent)
114 /* not handled yet */ 114 /* not handled yet */
115 } 115 }
116 } 116 }
117 } else if ((sectorbuf[0] == 'E') && (sectorbuf[1] == 'R')) { 117 } else if ((sansa_sectorbuf[0] == 'E') && (sansa_sectorbuf[1] == 'R')) {
118 if (!silent) fprintf(stderr,"[ERR] Bad boot sector signature\n"); 118 if (!silent) fprintf(stderr,"[ERR] Bad boot sector signature\n");
119 return -1; 119 return -1;
120 } 120 }
@@ -405,14 +405,14 @@ int is_sansa(struct sansa_t* sansa)
405 } 405 }
406 406
407 /* Check Bootloader header */ 407 /* Check Bootloader header */
408 if (sansa_seek_and_read(sansa, sansa->start, sectorbuf, 0x200) < 0) { 408 if (sansa_seek_and_read(sansa, sansa->start, sansa_sectorbuf, 0x200) < 0) {
409 return -2; 409 return -2;
410 } 410 }
411 if (memcmp(sectorbuf,"PPBL",4)!=0) { 411 if (memcmp(sansa_sectorbuf,"PPBL",4)!=0) {
412 /* No bootloader header, abort */ 412 /* No bootloader header, abort */
413 return -4; 413 return -4;
414 } 414 }
415 ppbl_length = (le2int(sectorbuf+4) + 0x1ff) & ~0x1ff; 415 ppbl_length = (le2int(sansa_sectorbuf+4) + 0x1ff) & ~0x1ff;
416 416
417 /* Sanity/safety check - the bootloader can't be larger than PPMI_OFFSET */ 417 /* Sanity/safety check - the bootloader can't be larger than PPMI_OFFSET */
418 if (ppbl_length > PPMI_OFFSET) 418 if (ppbl_length > PPMI_OFFSET)
@@ -421,12 +421,12 @@ int is_sansa(struct sansa_t* sansa)
421 } 421 }
422 422
423 /* Load Sansa bootloader and check for "Sansa C200" magic string */ 423 /* Load Sansa bootloader and check for "Sansa C200" magic string */
424 if (sansa_seek_and_read(sansa, sansa->start + 0x200, sectorbuf, ppbl_length) < 0) { 424 if (sansa_seek_and_read(sansa, sansa->start + 0x200, sansa_sectorbuf, ppbl_length) < 0) {
425 fprintf(stderr,"[ERR] Seek and read to 0x%08llx in is_sansa failed.\n", 425 fprintf(stderr,"[ERR] Seek and read to 0x%08llx in is_sansa failed.\n",
426 sansa->start+0x200); 426 sansa->start+0x200);
427 return -6; 427 return -6;
428 } 428 }
429 if (sansa_memmem(sectorbuf, ppbl_length, "Sansa C200", 10) != NULL) { 429 if (sansa_memmem(sansa_sectorbuf, ppbl_length, "Sansa C200", 10) != NULL) {
430 /* C200 */ 430 /* C200 */
431 sansa->targetname="c200"; 431 sansa->targetname="c200";
432 } else { 432 } else {
@@ -435,25 +435,25 @@ int is_sansa(struct sansa_t* sansa)
435 } 435 }
436 436
437 /* Check Main firmware header */ 437 /* Check Main firmware header */
438 if (sansa_seek_and_read(sansa, sansa->start+PPMI_OFFSET, sectorbuf, 0x200) < 0) { 438 if (sansa_seek_and_read(sansa, sansa->start+PPMI_OFFSET, sansa_sectorbuf, 0x200) < 0) {
439 fprintf(stderr,"[ERR] Seek to 0x%08llx in is_sansa failed.\n", 439 fprintf(stderr,"[ERR] Seek to 0x%08llx in is_sansa failed.\n",
440 sansa->start+PPMI_OFFSET); 440 sansa->start+PPMI_OFFSET);
441 return -5; 441 return -5;
442 } 442 }
443 if (memcmp(sectorbuf,"PPMI",4)!=0) { 443 if (memcmp(sansa_sectorbuf,"PPMI",4)!=0) {
444 /* No bootloader header, abort */ 444 /* No bootloader header, abort */
445 return -7; 445 return -7;
446 } 446 }
447 ppmi_length = le2int(sectorbuf+4); 447 ppmi_length = le2int(sansa_sectorbuf+4);
448 448
449 /* Check main mi4 file header */ 449 /* Check main mi4 file header */
450 if (sansa_seek_and_read(sansa, sansa->start+PPMI_OFFSET+0x200, sectorbuf, 0x200) < 0) { 450 if (sansa_seek_and_read(sansa, sansa->start+PPMI_OFFSET+0x200, sansa_sectorbuf, 0x200) < 0) {
451 fprintf(stderr,"[ERR] Seek to 0x%08llx in is_sansa failed.\n", 451 fprintf(stderr,"[ERR] Seek to 0x%08llx in is_sansa failed.\n",
452 sansa->start+PPMI_OFFSET+0x200); 452 sansa->start+PPMI_OFFSET+0x200);
453 return -5; 453 return -5;
454 } 454 }
455 455
456 if (get_mi4header(sectorbuf,&mi4header) < 0) { 456 if (get_mi4header(sansa_sectorbuf,&mi4header) < 0) {
457 fprintf(stderr,"[ERR] Invalid mi4header\n"); 457 fprintf(stderr,"[ERR] Invalid mi4header\n");
458 return -6; 458 return -6;
459 } 459 }
@@ -465,15 +465,15 @@ int is_sansa(struct sansa_t* sansa)
465 */ 465 */
466 466
467 sansa->hasoldbootloader = 0; 467 sansa->hasoldbootloader = 0;
468 if (memcmp(sectorbuf+0x1f8,"RBBL",4)==0) { 468 if (memcmp(sansa_sectorbuf+0x1f8,"RBBL",4)==0) {
469 /* Look for an original firmware after the first image */ 469 /* Look for an original firmware after the first image */
470 if (sansa_seek_and_read(sansa, 470 if (sansa_seek_and_read(sansa,
471 sansa->start + PPMI_OFFSET + 0x200 + ppmi_length, 471 sansa->start + PPMI_OFFSET + 0x200 + ppmi_length,
472 sectorbuf, 512) < 0) { 472 sansa_sectorbuf, 512) < 0) {
473 return -7; 473 return -7;
474 } 474 }
475 475
476 if (get_mi4header(sectorbuf,&mi4header)!=0) { 476 if (get_mi4header(sansa_sectorbuf,&mi4header)!=0) {
477 fprintf(stderr,"[ERR] No original firmware found\n"); 477 fprintf(stderr,"[ERR] No original firmware found\n");
478 sansa->hasoldbootloader = 1; 478 sansa->hasoldbootloader = 1;
479 } 479 }
@@ -654,7 +654,7 @@ int sansa_read_firmware(struct sansa_t* sansa, char* filename)
654 int outfile; 654 int outfile;
655 struct mi4header_t mi4header; 655 struct mi4header_t mi4header;
656 656
657 res = load_original_firmware(sansa,sectorbuf,&mi4header); 657 res = load_original_firmware(sansa,sansa_sectorbuf,&mi4header);
658 if (res < 0) 658 if (res < 0)
659 return res; 659 return res;
660 660
@@ -664,7 +664,7 @@ int sansa_read_firmware(struct sansa_t* sansa, char* filename)
664 return -1; 664 return -1;
665 } 665 }
666 666
667 res = write(outfile,sectorbuf,mi4header.mi4size); 667 res = write(outfile,sansa_sectorbuf,mi4header.mi4size);
668 if (res != (int)mi4header.mi4size) { 668 if (res != (int)mi4header.mi4size) {
669 fprintf(stderr,"[ERR] Write error - %d\n", res); 669 fprintf(stderr,"[ERR] Write error - %d\n", res);
670 return -1; 670 return -1;
@@ -704,14 +704,14 @@ int sansa_add_bootloader(struct sansa_t* sansa, char* filename, int type)
704 } 704 }
705 705
706 /* Create PPMI header */ 706 /* Create PPMI header */
707 memset(sectorbuf,0,0x200); 707 memset(sansa_sectorbuf,0,0x200);
708 memcpy(sectorbuf,"PPMI",4); 708 memcpy(sansa_sectorbuf,"PPMI",4);
709 int2le(bl_length, sectorbuf+4); 709 int2le(bl_length, sansa_sectorbuf+4);
710 int2le(0x00020000, sectorbuf+8); 710 int2le(0x00020000, sansa_sectorbuf+8);
711 711
712 if (type==FILETYPE_MI4) { 712 if (type==FILETYPE_MI4) {
713 /* Read bootloader into sectorbuf+0x200 */ 713 /* Read bootloader into sansa_sectorbuf+0x200 */
714 n = read(infile,sectorbuf+0x200,bl_length); 714 n = read(infile,sansa_sectorbuf+0x200,bl_length);
715 close(infile); 715 close(infile);
716 if (n < bl_length) { 716 if (n < bl_length) {
717 fprintf(stderr,"[ERR] Short read - requested %d bytes, received %d\n" 717 fprintf(stderr,"[ERR] Short read - requested %d bytes, received %d\n"
@@ -719,7 +719,7 @@ int sansa_add_bootloader(struct sansa_t* sansa, char* filename, int type)
719 return -1; 719 return -1;
720 } 720 }
721 721
722 if (memcmp(sectorbuf+0x200+0x1f8,"RBBL",4)!=0) { 722 if (memcmp(sansa_sectorbuf+0x200+0x1f8,"RBBL",4)!=0) {
723 fprintf(stderr,"[ERR] %s is not a Rockbox bootloader, aborting.\n", 723 fprintf(stderr,"[ERR] %s is not a Rockbox bootloader, aborting.\n",
724 filename); 724 filename);
725 return -1; 725 return -1;
@@ -727,15 +727,15 @@ int sansa_add_bootloader(struct sansa_t* sansa, char* filename, int type)
727 } else { 727 } else {
728 #ifndef RBUTIL 728 #ifndef RBUTIL
729 if (strcmp(sansa->targetname,"c200") == 0) { 729 if (strcmp(sansa->targetname,"c200") == 0) {
730 memcpy(sectorbuf+0x200,bootimg_c200,LEN_bootimg_c200); 730 memcpy(sansa_sectorbuf+0x200,bootimg_c200,LEN_bootimg_c200);
731 } else { 731 } else {
732 memcpy(sectorbuf+0x200,bootimg_e200,LEN_bootimg_e200); 732 memcpy(sansa_sectorbuf+0x200,bootimg_e200,LEN_bootimg_e200);
733 } 733 }
734 #endif 734 #endif
735 } 735 }
736 736
737 /* Load original firmware from Sansa to the space after the bootloader */ 737 /* Load original firmware from Sansa to the space after the bootloader */
738 res = load_original_firmware(sansa,sectorbuf+0x200+bl_length,&mi4header); 738 res = load_original_firmware(sansa,sansa_sectorbuf+0x200+bl_length,&mi4header);
739 if (res < 0) 739 if (res < 0)
740 return res; 740 return res;
741 741
@@ -749,7 +749,7 @@ int sansa_add_bootloader(struct sansa_t* sansa, char* filename, int type)
749 749
750 length = 0x200 + bl_length + mi4header.mi4size; 750 length = 0x200 + bl_length + mi4header.mi4size;
751 751
752 n=sansa_write(sansa, sectorbuf, length); 752 n=sansa_write(sansa, sansa_sectorbuf, length);
753 if (n < length) { 753 if (n < length) {
754 fprintf(stderr,"[ERR] Short write in add_bootloader\n"); 754 fprintf(stderr,"[ERR] Short write in add_bootloader\n");
755 return -6; 755 return -6;
@@ -765,16 +765,16 @@ int sansa_delete_bootloader(struct sansa_t* sansa)
765 int n; 765 int n;
766 int length; 766 int length;
767 767
768 /* Load original firmware from Sansa to sectorbuf+0x200 */ 768 /* Load original firmware from Sansa to sansa_sectorbuf+0x200 */
769 res = load_original_firmware(sansa,sectorbuf+0x200,&mi4header); 769 res = load_original_firmware(sansa,sansa_sectorbuf+0x200,&mi4header);
770 if (res < 0) 770 if (res < 0)
771 return res; 771 return res;
772 772
773 /* Create PPMI header */ 773 /* Create PPMI header */
774 memset(sectorbuf,0,0x200); 774 memset(sansa_sectorbuf,0,0x200);
775 memcpy(sectorbuf,"PPMI",4); 775 memcpy(sansa_sectorbuf,"PPMI",4);
776 int2le(mi4header.mi4size, sectorbuf+4); 776 int2le(mi4header.mi4size, sansa_sectorbuf+4);
777 int2le(0x00020000, sectorbuf+8); 777 int2le(0x00020000, sansa_sectorbuf+8);
778 778
779 /* Now write the whole thing back to the Sansa */ 779 /* Now write the whole thing back to the Sansa */
780 780
@@ -786,7 +786,7 @@ int sansa_delete_bootloader(struct sansa_t* sansa)
786 786
787 length = 0x200 + mi4header.mi4size; 787 length = 0x200 + mi4header.mi4size;
788 788
789 n=sansa_write(sansa, sectorbuf, length); 789 n=sansa_write(sansa, sansa_sectorbuf, length);
790 if (n < length) { 790 if (n < length) {
791 fprintf(stderr,"[ERR] Short write in delete_bootloader\n"); 791 fprintf(stderr,"[ERR] Short write in delete_bootloader\n");
792 return -6; 792 return -6;
@@ -801,20 +801,20 @@ void sansa_list_images(struct sansa_t* sansa)
801 loff_t ppmi_length; 801 loff_t ppmi_length;
802 802
803 /* Check Main firmware header */ 803 /* Check Main firmware header */
804 if (sansa_seek_and_read(sansa, sansa->start+PPMI_OFFSET, sectorbuf, 0x200) < 0) { 804 if (sansa_seek_and_read(sansa, sansa->start+PPMI_OFFSET, sansa_sectorbuf, 0x200) < 0) {
805 return; 805 return;
806 } 806 }
807 807
808 ppmi_length = le2int(sectorbuf+4); 808 ppmi_length = le2int(sansa_sectorbuf+4);
809 809
810 printf("[INFO] Image 1 - %llu bytes\n",ppmi_length); 810 printf("[INFO] Image 1 - %llu bytes\n",ppmi_length);
811 811
812 /* Look for an original firmware after the first image */ 812 /* Look for an original firmware after the first image */
813 if (sansa_seek_and_read(sansa, sansa->start + PPMI_OFFSET + 0x200 + ppmi_length, sectorbuf, 512) < 0) { 813 if (sansa_seek_and_read(sansa, sansa->start + PPMI_OFFSET + 0x200 + ppmi_length, sansa_sectorbuf, 512) < 0) {
814 return; 814 return;
815 } 815 }
816 816
817 if (get_mi4header(sectorbuf,&mi4header)==0) { 817 if (get_mi4header(sansa_sectorbuf,&mi4header)==0) {
818 printf("[INFO] Image 2 - %d bytes\n",mi4header.mi4size); 818 printf("[INFO] Image 2 - %d bytes\n",mi4header.mi4size);
819 } 819 }
820} 820}
@@ -864,8 +864,8 @@ int sansa_update_of(struct sansa_t* sansa, char* filename)
864 of_length = filesize(infile); 864 of_length = filesize(infile);
865 865
866 /* Load original firmware from file */ 866 /* Load original firmware from file */
867 memset(sectorbuf,0,0x200); 867 memset(sansa_sectorbuf,0,0x200);
868 n = read(infile,sectorbuf,of_length); 868 n = read(infile,sansa_sectorbuf,of_length);
869 close(infile); 869 close(infile);
870 if (n < of_length) { 870 if (n < of_length) {
871 fprintf(stderr,"[ERR] Short read - requested %d bytes, received %d\n" 871 fprintf(stderr,"[ERR] Short read - requested %d bytes, received %d\n"
@@ -874,13 +874,13 @@ int sansa_update_of(struct sansa_t* sansa, char* filename)
874 } 874 }
875 875
876 /* Check we have a valid MI4 file. */ 876 /* Check we have a valid MI4 file. */
877 if (get_mi4header(sectorbuf,&mi4header)!=0) { 877 if (get_mi4header(sansa_sectorbuf,&mi4header)!=0) {
878 fprintf(stderr,"[ERR] %s is not a valid mi4 file\n",filename); 878 fprintf(stderr,"[ERR] %s is not a valid mi4 file\n",filename);
879 return -1; 879 return -1;
880 } 880 }
881 881
882 /* Decrypt and build the header */ 882 /* Decrypt and build the header */
883 if(prepare_original_firmware(sectorbuf, &mi4header)!=0){ 883 if(prepare_original_firmware(sansa_sectorbuf, &mi4header)!=0){
884 fprintf(stderr,"[ERR] Unable to build decrypted mi4 from %s\n" 884 fprintf(stderr,"[ERR] Unable to build decrypted mi4 from %s\n"
885 ,filename); 885 ,filename);
886 return -1; 886 return -1;
@@ -893,7 +893,7 @@ int sansa_update_of(struct sansa_t* sansa, char* filename)
893 return -1; 893 return -1;
894 } 894 }
895 895
896 n=sansa_write(sansa, sectorbuf, of_length); 896 n=sansa_write(sansa, sansa_sectorbuf, of_length);
897 if (n < of_length) { 897 if (n < of_length) {
898 fprintf(stderr,"[ERR] Short write in sansa_update_of\n"); 898 fprintf(stderr,"[ERR] Short write in sansa_update_of\n");
899 return -1; 899 return -1;
@@ -910,8 +910,8 @@ int sansa_update_of(struct sansa_t* sansa, char* filename)
910 return -1; 910 return -1;
911 } 911 }
912 912
913 memset(sectorbuf,0,NVPARAMS_SIZE); 913 memset(sansa_sectorbuf,0,NVPARAMS_SIZE);
914 n=sansa_write(sansa, sectorbuf, NVPARAMS_SIZE); 914 n=sansa_write(sansa, sansa_sectorbuf, NVPARAMS_SIZE);
915 if (n < NVPARAMS_SIZE) { 915 if (n < NVPARAMS_SIZE) {
916 fprintf(stderr,"[ERR] Short write in sansa_update_of\n"); 916 fprintf(stderr,"[ERR] Short write in sansa_update_of\n");
917 return -1; 917 return -1;
@@ -937,7 +937,7 @@ int sansa_update_ppbl(struct sansa_t* sansa, char* filename)
937 937
938 ppbl_length = filesize(infile); 938 ppbl_length = filesize(infile);
939 939
940 n = read(infile,sectorbuf+0x200,ppbl_length); 940 n = read(infile,sansa_sectorbuf+0x200,ppbl_length);
941 close(infile); 941 close(infile);
942 if (n < ppbl_length) { 942 if (n < ppbl_length) {
943 fprintf(stderr,"[ERR] Short read - requested %d bytes, received %d\n", ppbl_length, n); 943 fprintf(stderr,"[ERR] Short read - requested %d bytes, received %d\n", ppbl_length, n);
@@ -945,10 +945,10 @@ int sansa_update_ppbl(struct sansa_t* sansa, char* filename)
945 } 945 }
946 946
947 /* Step 2 - Build the header */ 947 /* Step 2 - Build the header */
948 memset(sectorbuf,0,0x200); 948 memset(sansa_sectorbuf,0,0x200);
949 memcpy(sectorbuf,"PPBL",4); 949 memcpy(sansa_sectorbuf,"PPBL",4);
950 int2le(ppbl_length, sectorbuf+4); 950 int2le(ppbl_length, sansa_sectorbuf+4);
951 int2le(0x00010000, sectorbuf+8); 951 int2le(0x00010000, sansa_sectorbuf+8);
952 952
953 /* Step 3 - write the bootloader to the Sansa */ 953 /* Step 3 - write the bootloader to the Sansa */
954 if (sansa_seek(sansa, sansa->start) < 0) { 954 if (sansa_seek(sansa, sansa->start) < 0) {
@@ -956,7 +956,7 @@ int sansa_update_ppbl(struct sansa_t* sansa, char* filename)
956 return -1; 956 return -1;
957 } 957 }
958 958
959 n=sansa_write(sansa, sectorbuf, ppbl_length + 0x200); 959 n=sansa_write(sansa, sansa_sectorbuf, ppbl_length + 0x200);
960 if (n < (ppbl_length+0x200)) { 960 if (n < (ppbl_length+0x200)) {
961 fprintf(stderr,"[ERR] Short write in sansa_update_ppbl\n"); 961 fprintf(stderr,"[ERR] Short write in sansa_update_ppbl\n");
962 return -1; 962 return -1;
diff --git a/rbutil/sansapatcher/sansapatcher.h b/rbutil/sansapatcher/sansapatcher.h
index d082e6b08b..73abf6f9de 100644
--- a/rbutil/sansapatcher/sansapatcher.h
+++ b/rbutil/sansapatcher/sansapatcher.h
@@ -26,10 +26,11 @@ extern "C" {
26 26
27#include "sansaio.h" 27#include "sansaio.h"
28 28
29extern int sansa_verbose;
29/* Size of buffer for disk I/O - 8MB is large enough for any version 30/* Size of buffer for disk I/O - 8MB is large enough for any version
30 of the Apple firmware, but not the Nano's RSRC image. */ 31 of the Apple firmware, but not the Nano's RSRC image. */
31#define BUFFER_SIZE 8*1024*1024 32#define BUFFER_SIZE 8*1024*1024
32extern unsigned char* sectorbuf; 33extern unsigned char* sansa_sectorbuf;
33 34
34#define FILETYPE_MI4 0 35#define FILETYPE_MI4 0
35#define FILETYPE_INTERNAL 1 36#define FILETYPE_INTERNAL 1