diff options
Diffstat (limited to 'tools/gigabeats.c')
-rw-r--r-- | tools/gigabeats.c | 158 |
1 files changed, 56 insertions, 102 deletions
diff --git a/tools/gigabeats.c b/tools/gigabeats.c index 0d60eae781..c109d6ad1c 100644 --- a/tools/gigabeats.c +++ b/tools/gigabeats.c | |||
@@ -19,6 +19,12 @@ | |||
19 | 19 | ||
20 | #include <stdio.h> | 20 | #include <stdio.h> |
21 | #include <stdlib.h> | 21 | #include <stdlib.h> |
22 | #include <inttypes.h> | ||
23 | #include <sys/stat.h> | ||
24 | |||
25 | /* Entry point (and load address) for the main Rockbox bootloader */ | ||
26 | #define BL_ENTRY_POINT 0x8a000000 | ||
27 | |||
22 | 28 | ||
23 | static FILE * openinfile( const char * filename ) | 29 | static FILE * openinfile( const char * filename ) |
24 | { | 30 | { |
@@ -44,128 +50,76 @@ static FILE * openoutfile( const char * filename ) | |||
44 | return F; | 50 | return F; |
45 | }; | 51 | }; |
46 | 52 | ||
47 | unsigned long calc_csum(const unsigned char* pb, int cb) | 53 | static uint32_t calc_csum(const unsigned char* pb, int cb) |
48 | { | 54 | { |
49 | unsigned long l = 0; | 55 | uint32_t l = 0; |
50 | while (cb--) | 56 | while (cb--) |
51 | l += *pb++; | 57 | l += *pb++; |
52 | return l; | 58 | return l; |
53 | } | 59 | } |
54 | 60 | ||
61 | static void put_uint32le(uint32_t x, unsigned char* p) | ||
62 | { | ||
63 | p[0] = x & 0xff; | ||
64 | p[1] = (x >> 8) & 0xff; | ||
65 | p[2] = (x >> 16) & 0xff; | ||
66 | p[3] = (x >> 24) & 0xff; | ||
67 | } | ||
68 | |||
55 | int gigabeat_s_code(char *infile, char *outfile) | 69 | int gigabeat_s_code(char *infile, char *outfile) |
56 | { | 70 | { |
57 | FILE *in, *out; | 71 | FILE *in, *out; |
58 | unsigned long size = 0; | 72 | unsigned int size; |
59 | unsigned long data; | 73 | unsigned int newsize; |
60 | int imagelength; | 74 | unsigned char* buf; |
61 | 75 | ||
62 | in = openinfile(infile); | 76 | in = openinfile(infile); |
63 | out = openoutfile(outfile); | 77 | out = openoutfile(outfile); |
64 | 78 | ||
79 | /* Step 1: Load the binary file into memory */ | ||
65 | fseek(in, 0, SEEK_END); | 80 | fseek(in, 0, SEEK_END); |
66 | size = ftell(in); | 81 | size = ftell(in); |
67 | fseek(in, 0, SEEK_SET); | 82 | |
68 | unsigned long *binptr = malloc(size); | 83 | /* 15 bytes for header, 16 for signature bypass, |
69 | if(binptr == NULL) { | 84 | * 12 for record header, 12 for footer */ |
85 | newsize = 15 + 16 + 12 + size + 12; | ||
86 | buf = malloc(newsize); | ||
87 | if(buf == NULL) { | ||
70 | fprintf(stderr, "Not enough memory to perform the requested operation. Aborting.\n" ); | 88 | fprintf(stderr, "Not enough memory to perform the requested operation. Aborting.\n" ); |
71 | return 0; | 89 | return 0; |
72 | } | 90 | } |
73 | fread(binptr, size/4, 4, in); | 91 | fseek(in, 0, SEEK_SET); |
74 | /* 15 bytes for header, three 12 byte headers, the data for the first three | 92 | fread(buf + 43, size, 1, in); |
75 | * records, 12 byte header for code, code and the 12 byte footer | 93 | fclose(in); |
76 | * However, the original nk.bin's length doesn't correspond with | 94 | |
77 | * the length of the file, so I don't know what's up... | 95 | /* Step 2: Create the file header */ |
78 | */ | 96 | sprintf(buf, "B000FF\n"); |
79 | 97 | put_uint32le(0x88200000, buf+7); | |
80 | unsigned long header[2]; | 98 | /* If the value below is too small, the update will attempt to flash. |
81 | header[0] = 0x88200000; | 99 | * Be careful when changing this (leaving it as is won't cause issues) */ |
82 | /* header[1] = 15 + 12 + 4 + 12 + 8 + 12 + 4 + 12 + size + 12; */ | 100 | put_uint32le(0xCC0CD8, buf+11); |
83 | header[1] = 0xCC0CD8; /* The bootloader checks this value and compares */ | 101 | |
84 | fwrite("B000FF\n", 7, 1, out); | 102 | /* Step 3: Add the signature bypass record */ |
85 | fwrite(header, sizeof(header), 1, out); | 103 | put_uint32le(0x88065A10, buf+15); |
86 | 104 | put_uint32le(4, buf+19); | |
87 | unsigned long record[4]; | 105 | put_uint32le(0xE3A00001, buf+27); |
88 | unsigned long extra; | 106 | put_uint32le(calc_csum(buf+27,4), buf+23); |
89 | 107 | ||
90 | /*First record*/ | 108 | /* Step 4: Create a record for the actual code */ |
91 | record[0] = 0x88200000; | 109 | put_uint32le(BL_ENTRY_POINT, buf+31); |
92 | record[1] = 4; | 110 | put_uint32le(size, buf+35); |
93 | record[2] = 0x1eb; | 111 | put_uint32le(calc_csum(buf + 43, size), buf+39); |
94 | record[3] = 0xEA0003FE; | 112 | |
95 | fwrite(record, sizeof(record), 1, out); | 113 | /* Step 5: Write the footer */ |
96 | 114 | put_uint32le(0, buf+newsize-12); | |
97 | /*Second record*/ | 115 | put_uint32le(BL_ENTRY_POINT, buf+newsize-8); |
98 | record[0] = 0x88200040; | 116 | put_uint32le(0, buf+newsize-4); |
99 | record[1] = 8; | 117 | |
100 | record[2] = 0x3e9; | 118 | /* Step 6: Write the resulting file */ |
101 | record[3] = 0x43454345; | 119 | fwrite(buf, newsize, 1, out); |
102 | extra = 0x88EBF274; | 120 | fclose(out); |
103 | fwrite(record, sizeof(record), 1, out); | ||
104 | fwrite(&extra, sizeof(extra), 1, out); | ||
105 | |||
106 | /*Third record*/ | ||
107 | record[0] = 0x88200048; | ||
108 | record[1] = 4; | ||
109 | record[2] = 0x231; | ||
110 | record[3] = 0x00CBF274; | ||
111 | fwrite(record, sizeof(record), 1, out); | ||
112 | |||
113 | /*Signature bypass record*/ | ||
114 | unsigned long magic = 0xE3A00001; | ||
115 | record[0] = 0x88065A10; | ||
116 | record[1] = 4; | ||
117 | record[2] = calc_csum((unsigned char*)&magic,4); | ||
118 | record[3] = magic; | ||
119 | fwrite(record, sizeof(record), 1, out); | ||
120 | |||
121 | /*The actual code*/ | ||
122 | header[0] = 0x88201000; | ||
123 | header[1] = size; | ||
124 | extra = calc_csum((unsigned char*)binptr, size); | ||
125 | fwrite(header, sizeof(header), 1, out); | ||
126 | fwrite(&extra, sizeof(extra), 1, out); | ||
127 | fwrite(binptr, size, 1, out); | ||
128 | |||
129 | /* Table of contents. It's a start, but it still won't boot. | ||
130 | * Looks like it needs the file/module info as well... */ | ||
131 | binptr[0] = 0x02000000; | ||
132 | binptr[1] = 0x02000000; | ||
133 | binptr[2] = 0x88040000; | ||
134 | binptr[3] = 0x88076338; | ||
135 | binptr[4] = 0x1; | ||
136 | binptr[5] = 0x88080000; | ||
137 | binptr[6] = 0x8809C000; | ||
138 | binptr[7] = 0x88100000; | ||
139 | binptr[8] = 0x0; | ||
140 | binptr[9] = 0x0; | ||
141 | binptr[10] = 0x0; | ||
142 | binptr[11] = 0x0; | ||
143 | binptr[12] = 0x0; | ||
144 | binptr[13] = 0x0; | ||
145 | binptr[14] = 0x80808080; | ||
146 | binptr[15] = 0x0; | ||
147 | binptr[16] = 0x0; | ||
148 | binptr[17] = 0x201C2; | ||
149 | binptr[18] = 0x88050940; | ||
150 | binptr[19] = 0x0; | ||
151 | binptr[20] = 0x0; | ||
152 | header[0] = 0x88EBF274; | ||
153 | header[1] = 0x54; | ||
154 | extra = calc_csum((unsigned char*)binptr, 84); | ||
155 | fwrite(header, sizeof(header), 1, out); | ||
156 | fwrite(&extra, sizeof(extra), 1, out); | ||
157 | fwrite(binptr, 84, 1, out); | ||
158 | |||
159 | /*The footer*/ | ||
160 | header[0] = 0; | ||
161 | header[1] = 0x88201000; | ||
162 | extra = 0; | ||
163 | fwrite(header, sizeof(header), 1, out); | ||
164 | fwrite(&extra, sizeof(extra), 1, out); | ||
165 | 121 | ||
166 | fprintf(stderr, "File processed successfully\n" ); | 122 | fprintf(stderr, "File processed successfully\n" ); |
167 | 123 | ||
168 | fclose(in); | ||
169 | fclose(out); | ||
170 | return(0); | 124 | return(0); |
171 | } | 125 | } |