summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Chapman <dave@dchapman.com>2007-03-15 23:26:47 +0000
committerDave Chapman <dave@dchapman.com>2007-03-15 23:26:47 +0000
commite815601afb0eabe1ef59a2e8d1e0a746c4c33c34 (patch)
tree69ba3d9bc54042bafa03b5917f2a45dc454a1705
parent4a812912846dd5f02f3c8aa81af59b2d1cb67b65 (diff)
downloadrockbox-e815601afb0eabe1ef59a2e8d1e0a746c4c33c34.tar.gz
rockbox-e815601afb0eabe1ef59a2e8d1e0a746c4c33c34.zip
Implement the ability to embed a bootloader in sansapatcher - the --install option installs the embedded bootloader, and running without any parameters will cause a prompt to be displayed asking the user if they want to install, uninstall or cancel. sansapatcher now requres a PP5022.mi4 file (the Rockbox bootloader) in the current directory when building.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@12794 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--rbutil/sansapatcher/Makefile11
-rw-r--r--rbutil/sansapatcher/bin2c.c133
-rw-r--r--rbutil/sansapatcher/main.c53
-rw-r--r--rbutil/sansapatcher/parttypes.h109
-rw-r--r--rbutil/sansapatcher/sansapatcher.c35
-rw-r--r--rbutil/sansapatcher/sansapatcher.h4
6 files changed, 306 insertions, 39 deletions
diff --git a/rbutil/sansapatcher/Makefile b/rbutil/sansapatcher/Makefile
index 17b3fb0047..28ba1dae6a 100644
--- a/rbutil/sansapatcher/Makefile
+++ b/rbutil/sansapatcher/Makefile
@@ -33,12 +33,11 @@ sansapatcher-ppc: main.c sansapatcher.c sansaio-posix.c parttypes.h bootimg.c
33 gcc -arch ppc $(CFLAGS) -o sansapatcher-ppc main.c sansapatcher.c sansaio-posix.c bootimg.c 33 gcc -arch ppc $(CFLAGS) -o sansapatcher-ppc main.c sansapatcher.c sansaio-posix.c bootimg.c
34 strip sansapatcher-ppc 34 strip sansapatcher-ppc
35 35
36#mi42c: mi42c.c 36bin2c: bin2c.c
37# $(NATIVECC) $(CFLAGS) -o mi42c mi42c.c 37 $(NATIVECC) $(CFLAGS) -o bin2c bin2c.c
38 38
39#bootimg.c: PP5022.mi4 mi42c 39bootimg.c: PP5022.mi4 bin2c
40# ./mi42c PP5022.mi4 bootimg 40 ./bin2c PP5022.mi4 bootimg
41 41
42clean: 42clean:
43 rm -f sansapatcher.exe sansapatcher-mac sansapatcher-i386 sansapatcher-ppc sansapatcher mi42c *~ 43 rm -f sansapatcher.exe sansapatcher-mac sansapatcher-i386 sansapatcher-ppc sansapatcher bin2c bootimg.c bootimg.h *~
44#bootimg.c bootimg.h
diff --git a/rbutil/sansapatcher/bin2c.c b/rbutil/sansapatcher/bin2c.c
new file mode 100644
index 0000000000..4e9bd4a782
--- /dev/null
+++ b/rbutil/sansapatcher/bin2c.c
@@ -0,0 +1,133 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2007 Dave Chapman
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20#include <stdio.h>
21#include <string.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <unistd.h>
25#include <fcntl.h>
26#include <stdlib.h>
27
28#ifndef O_BINARY
29#define O_BINARY 0
30#endif
31
32static off_t filesize(int fd)
33{
34 struct stat buf;
35
36 fstat(fd,&buf);
37 return buf.st_size;
38}
39
40static int write_cfile(unsigned char* buf, off_t len, char* cname)
41{
42 char filename[256];
43 FILE* fp;
44 int i;
45
46 snprintf(filename,256,"%s.c",cname);
47
48 fp = fopen(filename,"w+");
49 if (fp == NULL) {
50 fprintf(stderr,"Couldn't open %s\n",filename);
51 return -1;
52 }
53
54 fprintf(fp,"/* Generated by ipod2c */\n\n");
55 fprintf(fp,"unsigned char %s[] = {",cname);
56
57 for (i=0;i<len;i++) {
58 if ((i % 16) == 0) {
59 fprintf(fp,"\n ");
60 }
61 if (i == (len-1)) {
62 fprintf(fp,"0x%02x",buf[i]);
63 } else {
64 fprintf(fp,"0x%02x, ",buf[i]);
65 }
66 }
67 fprintf(fp,"\n};\n");
68
69 fclose(fp);
70 return 0;
71}
72
73static int write_hfile(unsigned char* buf, off_t len, char* cname)
74{
75 char filename[256];
76 FILE* fp;
77
78 snprintf(filename,256,"%s.h",cname);
79 fp = fopen(filename,"w+");
80 if (fp == NULL) {
81 fprintf(stderr,"Couldn't open %s\n",filename);
82 return -1;
83 }
84
85 fprintf(fp,"/* Generated by ipod2c */\n\n");
86 fprintf(fp,"#define LEN_%s %d\n",cname,(int)len);
87 fprintf(fp,"extern unsigned char %s[];\n",cname);
88 fclose(fp);
89 return 0;
90}
91
92int main (int argc, char* argv[])
93{
94 char* infile;
95 char* cname;
96 int fd;
97 unsigned char* buf;
98 int len;
99 int n;
100
101 if (argc != 3) {
102 fprintf(stderr,"Usage: bin2c file cname\n");
103 return 0;
104 }
105
106 infile=argv[1];
107 cname=argv[2];
108
109 fd = open(infile,O_RDONLY|O_BINARY);
110 if (fd < 0) {
111 fprintf(stderr,"Can not open %s\n",infile);
112 return 0;
113 }
114
115 len = filesize(fd);
116
117 buf = malloc(len);
118 n = read(fd,buf,len);
119 if (n < len) {
120 fprintf(stderr,"Short read, aborting\n");
121 return 0;
122 }
123 close(fd);
124
125 if (write_cfile(buf,len,cname) < 0) {
126 return -1;
127 }
128 if (write_hfile(buf,len,cname) < 0) {
129 return -1;
130 }
131
132 return 0;
133}
diff --git a/rbutil/sansapatcher/main.c b/rbutil/sansapatcher/main.c
index 0828095bae..07e44a044e 100644
--- a/rbutil/sansapatcher/main.c
+++ b/rbutil/sansapatcher/main.c
@@ -35,9 +35,7 @@ int verbose = 0;
35 35
36enum { 36enum {
37 NONE, 37 NONE,
38#ifdef WITH_BOOTOBJS
39 INSTALL, 38 INSTALL,
40#endif
41 INTERACTIVE, 39 INTERACTIVE,
42 SHOW_INFO, 40 SHOW_INFO,
43 LIST_IMAGES, 41 LIST_IMAGES,
@@ -59,9 +57,7 @@ void print_usage(void)
59#endif 57#endif
60 fprintf(stderr,"\n"); 58 fprintf(stderr,"\n");
61 fprintf(stderr,"Where [action] is one of the following options:\n"); 59 fprintf(stderr,"Where [action] is one of the following options:\n");
62#ifdef WITH_BOOTOBJS
63 fprintf(stderr," --install\n"); 60 fprintf(stderr," --install\n");
64#endif
65 fprintf(stderr," -l, --list\n"); 61 fprintf(stderr," -l, --list\n");
66 fprintf(stderr," -rf, --read-firmware filename.mi4\n"); 62 fprintf(stderr," -rf, --read-firmware filename.mi4\n");
67 fprintf(stderr," -a, --add-bootloader filename.mi4\n"); 63 fprintf(stderr," -a, --add-bootloader filename.mi4\n");
@@ -109,9 +105,7 @@ void display_partinfo(struct sansa_t* sansa)
109 105
110int main(int argc, char* argv[]) 106int main(int argc, char* argv[])
111{ 107{
112#ifdef WITH_BOOTOBJS
113 char yesno[4]; 108 char yesno[4];
114#endif
115 int i; 109 int i;
116 int n; 110 int n;
117 char* filename; 111 char* filename;
@@ -167,33 +161,25 @@ int main(int argc, char* argv[])
167 } 161 }
168 162
169 if (n != 1) { 163 if (n != 1) {
170#ifdef WITH_BOOTOBJS
171 if (argc==1) { 164 if (argc==1) {
172 printf("\nPress ENTER to exit sansapatcher :"); 165 printf("\nPress ENTER to exit sansapatcher :");
173 fgets(yesno,4,stdin); 166 fgets(yesno,4,stdin);
174 } 167 }
175#endif
176 return 0; 168 return 0;
177 } 169 }
178 170
179 i = 1; 171 i = 1;
180 } 172 }
181 173
182#ifdef WITH_BOOTOBJS
183 action = INTERACTIVE; 174 action = INTERACTIVE;
184#else
185 action = NONE;
186#endif
187 175
188 while (i < argc) { 176 while (i < argc) {
189 if ((strcmp(argv[i],"-l")==0) || (strcmp(argv[i],"--list")==0)) { 177 if ((strcmp(argv[i],"-l")==0) || (strcmp(argv[i],"--list")==0)) {
190 action = LIST_IMAGES; 178 action = LIST_IMAGES;
191 i++; 179 i++;
192#ifdef WITH_BOOTOBJS
193 } else if (strcmp(argv[i],"--install")==0) { 180 } else if (strcmp(argv[i],"--install")==0) {
194 action = INSTALL; 181 action = INSTALL;
195 i++; 182 i++;
196#endif
197 } else if ((strcmp(argv[i],"-d")==0) || 183 } else if ((strcmp(argv[i],"-d")==0) ||
198 (strcmp(argv[i],"--delete-bootloader")==0)) { 184 (strcmp(argv[i],"--delete-bootloader")==0)) {
199 action = DELETE_BOOTLOADER; 185 action = DELETE_BOOTLOADER;
@@ -252,12 +238,49 @@ int main(int argc, char* argv[])
252 238
253 if (action==LIST_IMAGES) { 239 if (action==LIST_IMAGES) {
254 list_images(&sansa); 240 list_images(&sansa);
241 } else if (action==INTERACTIVE) {
242
243 printf("Enter i to install the Rockbox bootloader, u to uninstall\n or c to cancel and do nothing (i/u/c) :");
244
245 if (fgets(yesno,4,stdin)) {
246 if (yesno[0]=='i') {
247 if (sansa_reopen_rw(&sansa) < 0) {
248 return 5;
249 }
250
251 if (add_bootloader(&sansa, NULL, FILETYPE_INTERNAL)==0) {
252 fprintf(stderr,"[INFO] Bootloader installed successfully.\n");
253 } else {
254 fprintf(stderr,"[ERR] --install failed.\n");
255 }
256 } else if (yesno[0]=='u') {
257 if (sansa_reopen_rw(&sansa) < 0) {
258 return 5;
259 }
260
261 if (delete_bootloader(&sansa)==0) {
262 fprintf(stderr,"[INFO] Bootloader removed.\n");
263 } else {
264 fprintf(stderr,"[ERR] Bootloader removal failed.\n");
265 }
266 }
267 }
255 } else if (action==READ_FIRMWARE) { 268 } else if (action==READ_FIRMWARE) {
256 if (read_firmware(&sansa, filename)==0) { 269 if (read_firmware(&sansa, filename)==0) {
257 fprintf(stderr,"[INFO] Firmware read to file %s.\n",filename); 270 fprintf(stderr,"[INFO] Firmware read to file %s.\n",filename);
258 } else { 271 } else {
259 fprintf(stderr,"[ERR] --read-firmware failed.\n"); 272 fprintf(stderr,"[ERR] --read-firmware failed.\n");
260 } 273 }
274 } else if (action==INSTALL) {
275 if (sansa_reopen_rw(&sansa) < 0) {
276 return 5;
277 }
278
279 if (add_bootloader(&sansa, NULL, FILETYPE_INTERNAL)==0) {
280 fprintf(stderr,"[INFO] Bootloader installed successfully.\n");
281 } else {
282 fprintf(stderr,"[ERR] --install failed.\n");
283 }
261 } else if (action==ADD_BOOTLOADER) { 284 } else if (action==ADD_BOOTLOADER) {
262 if (sansa_reopen_rw(&sansa) < 0) { 285 if (sansa_reopen_rw(&sansa) < 0) {
263 return 5; 286 return 5;
@@ -282,12 +305,10 @@ int main(int argc, char* argv[])
282 305
283 sansa_close(&sansa); 306 sansa_close(&sansa);
284 307
285#ifdef WITH_BOOTOBJS
286 if (action==INTERACTIVE) { 308 if (action==INTERACTIVE) {
287 printf("Press ENTER to exit sansapatcher :"); 309 printf("Press ENTER to exit sansapatcher :");
288 fgets(yesno,4,stdin); 310 fgets(yesno,4,stdin);
289 } 311 }
290#endif
291 312
292 return 0; 313 return 0;
293} 314}
diff --git a/rbutil/sansapatcher/parttypes.h b/rbutil/sansapatcher/parttypes.h
new file mode 100644
index 0000000000..f8de303553
--- /dev/null
+++ b/rbutil/sansapatcher/parttypes.h
@@ -0,0 +1,109 @@
1/* DOS partition types - taken from fdisk */
2
3struct parttype {
4 unsigned char type;
5 char *name;
6};
7
8struct parttype parttypes[] = {
9 {0x00, "Empty"},
10 {0x01, "FAT12"},
11 {0x02, "XENIX root"},
12 {0x03, "XENIX usr"},
13 {0x04, "FAT16 <32M"},
14 {0x05, "Extended"}, /* DOS 3.3+ extended partition */
15 {0x06, "FAT16"}, /* DOS 16-bit >=32M */
16 {0x07, "HPFS/NTFS"}, /* OS/2 IFS, eg, HPFS or NTFS or QNX */
17 {0x08, "AIX"}, /* AIX boot (AIX -- PS/2 port) or SplitDrive */
18 {0x09, "AIX bootable"}, /* AIX data or Coherent */
19 {0x0a, "OS/2 Boot Manager"},/* OS/2 Boot Manager */
20 {0x0b, "W95 FAT32"},
21 {0x0c, "W95 FAT32 (LBA)"},/* LBA really is `Extended Int 13h' */
22 {0x0e, "W95 FAT16 (LBA)"},
23 {0x0f, "W95 Ext'd (LBA)"},
24 {0x10, "OPUS"},
25 {0x11, "Hidden FAT12"},
26 {0x12, "Compaq diagnostics"},
27 {0x14, "Hidden FAT16 <32M"},
28 {0x16, "Hidden FAT16"},
29 {0x17, "Hidden HPFS/NTFS"},
30 {0x18, "AST SmartSleep"},
31 {0x1b, "Hidden W95 FAT32"},
32 {0x1c, "Hidden W95 FAT32 (LBA)"},
33 {0x1e, "Hidden W95 FAT16 (LBA)"},
34 {0x24, "NEC DOS"},
35 {0x39, "Plan 9"},
36 {0x3c, "PartitionMagic recovery"},
37 {0x40, "Venix 80286"},
38 {0x41, "PPC PReP Boot"},
39 {0x42, "SFS"},
40 {0x4d, "QNX4.x"},
41 {0x4e, "QNX4.x 2nd part"},
42 {0x4f, "QNX4.x 3rd part"},
43 {0x50, "OnTrack DM"},
44 {0x51, "OnTrack DM6 Aux1"}, /* (or Novell) */
45 {0x52, "CP/M"}, /* CP/M or Microport SysV/AT */
46 {0x53, "OnTrack DM6 Aux3"},
47 {0x54, "OnTrackDM6"},
48 {0x55, "EZ-Drive"},
49 {0x56, "Golden Bow"},
50 {0x5c, "Priam Edisk"},
51 {0x61, "SpeedStor"},
52 {0x63, "GNU HURD or SysV"}, /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
53 {0x64, "Novell Netware 286"},
54 {0x65, "Novell Netware 386"},
55 {0x70, "DiskSecure Multi-Boot"},
56 {0x75, "PC/IX"},
57 {0x80, "Old Minix"}, /* Minix 1.4a and earlier */
58 {0x81, "Minix / old Linux"},/* Minix 1.4b and later */
59 {0x82, "Linux swap / Solaris"},
60 {0x83, "Linux"},
61 {0x84, "OS/2 hidden C: drive"},
62 {0x85, "Linux extended"},
63 {0x86, "NTFS volume set"},
64 {0x87, "NTFS volume set"},
65 {0x88, "Linux plaintext"},
66 {0x8e, "Linux LVM"},
67 {0x93, "Amoeba"},
68 {0x94, "Amoeba BBT"}, /* (bad block table) */
69 {0x9f, "BSD/OS"}, /* BSDI */
70 {0xa0, "IBM Thinkpad hibernation"},
71 {0xa5, "FreeBSD"}, /* various BSD flavours */
72 {0xa6, "OpenBSD"},
73 {0xa7, "NeXTSTEP"},
74 {0xa8, "Darwin UFS"},
75 {0xa9, "NetBSD"},
76 {0xab, "Darwin boot"},
77 {0xb7, "BSDI fs"},
78 {0xb8, "BSDI swap"},
79 {0xbb, "Boot Wizard hidden"},
80 {0xbe, "Solaris boot"},
81 {0xbf, "Solaris"},
82 {0xc1, "DRDOS/sec (FAT-12)"},
83 {0xc4, "DRDOS/sec (FAT-16 < 32M)"},
84 {0xc6, "DRDOS/sec (FAT-16)"},
85 {0xc7, "Syrinx"},
86 {0xda, "Non-FS data"},
87 {0xdb, "CP/M / CTOS / ..."},/* CP/M or Concurrent CP/M or
88 Concurrent DOS or CTOS */
89 {0xde, "Dell Utility"}, /* Dell PowerEdge Server utilities */
90 {0xdf, "BootIt"}, /* BootIt EMBRM */
91 {0xe1, "DOS access"}, /* DOS access or SpeedStor 12-bit FAT
92 extended partition */
93 {0xe3, "DOS R/O"}, /* DOS R/O or SpeedStor */
94 {0xe4, "SpeedStor"}, /* SpeedStor 16-bit FAT extended
95 partition < 1024 cyl. */
96 {0xeb, "BeOS fs"},
97 {0xee, "EFI GPT"}, /* Intel EFI GUID Partition Table */
98 {0xef, "EFI (FAT-12/16/32)"},/* Intel EFI System Partition */
99 {0xf0, "Linux/PA-RISC boot"},/* Linux/PA-RISC boot loader */
100 {0xf1, "SpeedStor"},
101 {0xf4, "SpeedStor"}, /* SpeedStor large partition */
102 {0xf2, "DOS secondary"}, /* DOS 3.3+ secondary */
103 {0xfd, "Linux raid autodetect"},/* New (2.2.x) raid partition with
104 autodetect using persistent
105 superblock */
106 {0xfe, "LANstep"}, /* SpeedStor >1024 cyl. or LANstep */
107 {0xff, "BBT"}, /* Xenix Bad Block Table */
108 { 0, 0 }
109};
diff --git a/rbutil/sansapatcher/sansapatcher.c b/rbutil/sansapatcher/sansapatcher.c
index a19c60eaa0..3c6848e6d4 100644
--- a/rbutil/sansapatcher/sansapatcher.c
+++ b/rbutil/sansapatcher/sansapatcher.c
@@ -539,7 +539,6 @@ int load_original_firmware(struct sansa_t* sansa, unsigned char* buf, struct mi4
539 } 539 }
540 540
541 if (key_found) { 541 if (key_found) {
542printf("Key found - %d\n",i);
543 memcpy(buf+(mi4header->plaintext+0x200),tmpbuf,mi4header->mi4size-(mi4header->plaintext+0x200)); 542 memcpy(buf+(mi4header->plaintext+0x200),tmpbuf,mi4header->mi4size-(mi4header->plaintext+0x200));
544 free(tmpbuf); 543 free(tmpbuf);
545 } else { 544 } else {
@@ -593,14 +592,18 @@ int add_bootloader(struct sansa_t* sansa, char* filename, int type)
593 int n; 592 int n;
594 int length; 593 int length;
595 594
596 /* Step 1 - read bootloader into RAM. */ 595 if (type==FILETYPE_MI4) {
597 infile=open(filename,O_RDONLY|O_BINARY); 596 /* Step 1 - read bootloader into RAM. */
598 if (infile < 0) { 597 infile=open(filename,O_RDONLY|O_BINARY);
599 fprintf(stderr,"[ERR] Couldn't open input file %s\n",filename); 598 if (infile < 0) {
600 return -1; 599 fprintf(stderr,"[ERR] Couldn't open input file %s\n",filename);
601 } 600 return -1;
601 }
602 602
603 bl_length = filesize(infile); 603 bl_length = filesize(infile);
604 } else {
605 bl_length = LEN_bootimg;
606 }
604 607
605 /* Create PPMI header */ 608 /* Create PPMI header */
606 memset(sectorbuf,0,0x200); 609 memset(sectorbuf,0,0x200);
@@ -608,12 +611,16 @@ int add_bootloader(struct sansa_t* sansa, char* filename, int type)
608 int2le(bl_length, sectorbuf+4); 611 int2le(bl_length, sectorbuf+4);
609 int2le(0x00020000, sectorbuf+8); 612 int2le(0x00020000, sectorbuf+8);
610 613
611 /* Read bootloader into sectorbuf+0x200 */ 614 if (type==FILETYPE_MI4) {
612 n = read(infile,sectorbuf+0x200,bl_length); 615 /* Read bootloader into sectorbuf+0x200 */
613 if (n < bl_length) { 616 n = read(infile,sectorbuf+0x200,bl_length);
614 fprintf(stderr,"[ERR] Short read - requested %d bytes, received %d\n" 617 if (n < bl_length) {
615 ,bl_length,n); 618 fprintf(stderr,"[ERR] Short read - requested %d bytes, received %d\n"
616 return -1; 619 ,bl_length,n);
620 return -1;
621 }
622 } else {
623 memcpy(sectorbuf+0x200,bootimg,LEN_bootimg);
617 } 624 }
618 625
619 /* Load original firmware from Sansa to the space after the bootloader */ 626 /* Load original firmware from Sansa to the space after the bootloader */
diff --git a/rbutil/sansapatcher/sansapatcher.h b/rbutil/sansapatcher/sansapatcher.h
index 97b73dd0b9..d422aa5932 100644
--- a/rbutil/sansapatcher/sansapatcher.h
+++ b/rbutil/sansapatcher/sansapatcher.h
@@ -28,9 +28,7 @@
28extern unsigned char* sectorbuf; 28extern unsigned char* sectorbuf;
29 29
30#define FILETYPE_MI4 0 30#define FILETYPE_MI4 0
31#ifdef WITH_BOOTOBJS 31#define FILETYPE_INTERNAL 1
32 #define FILETYPE_INTERNAL 1
33#endif
34 32
35char* get_parttype(int pt); 33char* get_parttype(int pt);
36int read_partinfo(struct sansa_t* sansa, int silent); 34int read_partinfo(struct sansa_t* sansa, int silent);