summaryrefslogtreecommitdiff
path: root/rbutil/ipodpatcher/ipodpatcher.c
diff options
context:
space:
mode:
authorDominik Riebeling <Dominik.Riebeling@gmail.com>2008-06-17 17:52:13 +0000
committerDominik Riebeling <Dominik.Riebeling@gmail.com>2008-06-17 17:52:13 +0000
commitd131a312668f2503546d7e1047c4a6be47ca0589 (patch)
tree9bd6582b61be4fd4af074d39a0b0c347dc8e8b6c /rbutil/ipodpatcher/ipodpatcher.c
parent8436e29e7e618db15ae28d9ff4c16c560f18deef (diff)
downloadrockbox-d131a312668f2503546d7e1047c4a6be47ca0589.tar.gz
rockbox-d131a312668f2503546d7e1047c4a6be47ca0589.zip
Rename variables sectorbuf and verbose to avoid clashes in rbutil. Cleanup exports a bit.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@17730 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'rbutil/ipodpatcher/ipodpatcher.c')
-rw-r--r--rbutil/ipodpatcher/ipodpatcher.c178
1 files changed, 89 insertions, 89 deletions
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;