summaryrefslogtreecommitdiff
path: root/rbutil/chinachippatcher/chinachip.c
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil/chinachippatcher/chinachip.c')
-rw-r--r--rbutil/chinachippatcher/chinachip.c87
1 files changed, 42 insertions, 45 deletions
diff --git a/rbutil/chinachippatcher/chinachip.c b/rbutil/chinachippatcher/chinachip.c
index dc1d2a40bb..d9e3e75817 100644
--- a/rbutil/chinachippatcher/chinachip.c
+++ b/rbutil/chinachippatcher/chinachip.c
@@ -27,8 +27,6 @@
27#include <time.h> 27#include <time.h>
28#include "chinachip.h" 28#include "chinachip.h"
29 29
30#define tr(x) x /* Qt translation support */
31
32/* From http://www.rockbox.org/wiki/ChinaChip */ 30/* From http://www.rockbox.org/wiki/ChinaChip */
33struct header 31struct header
34{ 32{
@@ -63,22 +61,11 @@ static long int filesize(FILE* fd)
63 return len; 61 return len;
64} 62}
65 63
66#ifdef STANDALONE
67#define ERR(fmt, ...) err(userdata, "[ERR] "fmt"\n", ##__VA_ARGS__)
68#define INFO(fmt, ...) info(userdata, "[INFO] "fmt"\n", ##__VA_ARGS__)
69#define tr(x) x
70#else
71#define ERR(fmt, ...) err(userdata, fmt, ##__VA_ARGS__)
72#define INFO(fmt, ...) info(userdata, fmt, ##__VA_ARGS__)
73#endif
74#define FCLOSE(fd) fclose(fd); fd = NULL; 64#define FCLOSE(fd) fclose(fd); fd = NULL;
75#define CCPMPBIN_HEADER_SIZE (sizeof(uint32_t)*2 + sizeof(uint8_t) + 9) 65#define CCPMPBIN_HEADER_SIZE (sizeof(uint32_t)*2 + sizeof(uint8_t) + 9)
76#define TOTAL_SIZE (fsize + CCPMPBIN_HEADER_SIZE + bsize) 66#define TOTAL_SIZE (fsize + CCPMPBIN_HEADER_SIZE + bsize)
77int chinachip_patch(const char* firmware, const char* bootloader, 67enum cc_error chinachip_patch(const char* firmware, const char* bootloader,
78 const char* output, const char* ccpmp_backup, 68 const char* output, const char* ccpmp_backup)
79 void (*info)(void*, char*, ...),
80 void (*err)(void*, char*, ...),
81 void* userdata)
82{ 69{
83 char header_time[13]; 70 char header_time[13];
84 time_t cur_time; 71 time_t cur_time;
@@ -87,46 +74,52 @@ int chinachip_patch(const char* firmware, const char* bootloader,
87 FILE *fd = NULL, *bd = NULL, *od = NULL; 74 FILE *fd = NULL, *bd = NULL, *od = NULL;
88 unsigned int ccpmp_size = 0, i, fsize, bsize; 75 unsigned int ccpmp_size = 0, i, fsize, bsize;
89 signed int checksum = 0, ccpmp_pos; 76 signed int checksum = 0, ccpmp_pos;
77 int result = E_OK;
90 78
91 fd = fopen(firmware, "rb"); 79 fd = fopen(firmware, "rb");
92 if(!fd) 80 if(!fd)
93 { 81 {
94 ERR(tr("Can't open file %s!"), firmware); 82 fprintf(stderr, "[ERR] Can't open file %s!\n", firmware);
83 result = E_OPEN_FIRMWARE;
95 goto err; 84 goto err;
96 } 85 }
97 bd = fopen(bootloader, "rb"); 86 bd = fopen(bootloader, "rb");
98 if(!bd) 87 if(!bd)
99 { 88 {
100 ERR(tr("Can't open file %s!"), bootloader); 89 fprintf(stderr, "[ERR] Can't open file %s!\n", bootloader);
90 result = E_OPEN_BOOTLOADER;
101 goto err; 91 goto err;
102 } 92 }
103 93
104 bsize = filesize(bd); 94 bsize = filesize(bd);
105 INFO(tr("Bootloader size is %d bytes"), bsize); 95 fprintf(stderr, "[INFO] Bootloader size is %d bytes\n", bsize);
106 FCLOSE(bd); 96 FCLOSE(bd);
107 97
108 fsize = filesize(fd); 98 fsize = filesize(fd);
109 INFO(tr("Firmware size is %d bytes"), fsize); 99 fprintf(stderr, "[INFO] Firmware size is %d bytes\n", fsize);
110 100
111 buf = malloc(TOTAL_SIZE); 101 buf = malloc(TOTAL_SIZE);
112 if(buf == NULL) 102 if(buf == NULL)
113 { 103 {
114 ERR(tr("Can't allocate %d bytes!"), fsize); 104 fprintf(stderr, "[ERR] Can't allocate %d bytes!\n", fsize);
105 result = E_MEMALLOC;
115 goto err; 106 goto err;
116 } 107 }
117 memset(buf, 0, TOTAL_SIZE); 108 memset(buf, 0, TOTAL_SIZE);
118 109
119 INFO(tr("Reading %s into memory..."), firmware); 110 fprintf(stderr, "[INFO] Reading %s into memory...\n", firmware);
120 if(fread(buf, fsize, 1, fd) != 1) 111 if(fread(buf, fsize, 1, fd) != 1)
121 { 112 {
122 ERR(tr("Can't read file %s to memory!"), firmware); 113 fprintf(stderr, "[ERR] Can't read file %s to memory!\n", firmware);
114 result = E_LOAD_FIRMWARE;
123 goto err; 115 goto err;
124 } 116 }
125 FCLOSE(fd); 117 FCLOSE(fd);
126 118
127 if(memcmp(buf, "WADF", 4)) 119 if(memcmp(buf, "WADF", 4))
128 { 120 {
129 ERR(tr("File %s isn't a valid ChinaChip firmware!"), firmware); 121 fprintf(stderr, "[ERR] File %s isn't a valid ChinaChip firmware!\n", firmware);
122 result = E_INVALID_FILE;
130 goto err; 123 goto err;
131 } 124 }
132 125
@@ -148,10 +141,11 @@ int chinachip_patch(const char* firmware, const char* bootloader,
148 141
149 if(i >= fsize) 142 if(i >= fsize)
150 { 143 {
151 ERR(tr("Couldn't find ccpmp.bin in %s!"), firmware); 144 fprintf(stderr, "[ERR] Couldn't find ccpmp.bin in %s!\n", firmware);
145 result = E_NO_CCPMP;
152 goto err; 146 goto err;
153 } 147 }
154 INFO(tr("Found ccpmp.bin at %d bytes"), ccpmp_pos); 148 fprintf(stderr, "[INFO] Found ccpmp.bin at %d bytes\n", ccpmp_pos);
155 149
156 if(ccpmp_backup) 150 if(ccpmp_backup)
157 { 151 {
@@ -159,20 +153,22 @@ int chinachip_patch(const char* firmware, const char* bootloader,
159 bd = fopen(ccpmp_backup, "wb"); 153 bd = fopen(ccpmp_backup, "wb");
160 if(!bd) 154 if(!bd)
161 { 155 {
162 ERR(tr("Can't open file %s!"), ccpmp_backup); 156 fprintf(stderr, "[ERR] Can't open file %s!\n", ccpmp_backup);
157 result = E_OPEN_BACKUP;
163 goto err; 158 goto err;
164 } 159 }
165 160
166 INFO(tr("Writing %d bytes to %s..."), ccpmp_size, ccpmp_backup); 161 fprintf(stderr, "[INFO] Writing %d bytes to %s...\n", ccpmp_size, ccpmp_backup);
167 if(fwrite(&buf[ccpmp_data_pos], ccpmp_size, 1, bd) != 1) 162 if(fwrite(&buf[ccpmp_data_pos], ccpmp_size, 1, bd) != 1)
168 { 163 {
169 ERR(tr("Can't write to file %s!"), ccpmp_backup); 164 fprintf(stderr, "[ERR] Can't write to file %s!\n", ccpmp_backup);
165 result = E_WRITE_BACKUP;
170 goto err; 166 goto err;
171 } 167 }
172 FCLOSE(bd); 168 FCLOSE(bd);
173 } 169 }
174 170
175 INFO(tr("Renaming it to ccpmp.old...")); 171 fprintf(stderr, "[INFO] Renaming it to ccpmp.old...\n");
176 buf[ccpmp_pos + 6] = 'o'; 172 buf[ccpmp_pos + 6] = 'o';
177 buf[ccpmp_pos + 7] = 'l'; 173 buf[ccpmp_pos + 7] = 'l';
178 buf[ccpmp_pos + 8] = 'd'; 174 buf[ccpmp_pos + 8] = 'd';
@@ -180,27 +176,29 @@ int chinachip_patch(const char* firmware, const char* bootloader,
180 bd = fopen(bootloader, "rb"); 176 bd = fopen(bootloader, "rb");
181 if(!bd) 177 if(!bd)
182 { 178 {
183 ERR(tr("Can't open file %s!"), bootloader); 179 fprintf(stderr, "[ERR] Can't open file %s!\n", bootloader);
180 result = E_OPEN_BOOTLOADER;
184 goto err; 181 goto err;
185 } 182 }
186 183
187 /* Also include path size */ 184 /* Also include path size */
188 ccpmp_pos -= sizeof(uint32_t); 185 ccpmp_pos -= sizeof(uint32_t);
189 186
190 INFO(tr("Making place for ccpmp.bin...")); 187 fprintf(stderr, "[INFO] Making place for ccpmp.bin...\n");
191 memmove(&buf[ccpmp_pos + bsize + CCPMPBIN_HEADER_SIZE], 188 memmove(&buf[ccpmp_pos + bsize + CCPMPBIN_HEADER_SIZE],
192 &buf[ccpmp_pos], fsize - ccpmp_pos); 189 &buf[ccpmp_pos], fsize - ccpmp_pos);
193 190
194 INFO(tr("Reading %s into memory..."), bootloader); 191 fprintf(stderr, "[INFO] Reading %s into memory...\n", bootloader);
195 if(fread(&buf[ccpmp_pos + CCPMPBIN_HEADER_SIZE], 192 if(fread(&buf[ccpmp_pos + CCPMPBIN_HEADER_SIZE],
196 bsize, 1, bd) != 1) 193 bsize, 1, bd) != 1)
197 { 194 {
198 ERR(tr("Can't read file %s to memory!"), bootloader); 195 fprintf(stderr, "[ERR] Can't read file %s to memory!\n", bootloader);
196 result = E_LOAD_BOOTLOADER;
199 goto err; 197 goto err;
200 } 198 }
201 FCLOSE(bd); 199 FCLOSE(bd);
202 200
203 INFO(tr("Adding header to %s..."), bootloader); 201 fprintf(stderr, "[INFO] Adding header to %s...\n", bootloader);
204 int2le(&buf[ccpmp_pos ], 9); /* Pathname Size */ 202 int2le(&buf[ccpmp_pos ], 9); /* Pathname Size */
205 memcpy(&buf[ccpmp_pos + 4 ], "ccpmp.bin", 9); /* Pathname */ 203 memcpy(&buf[ccpmp_pos + 4 ], "ccpmp.bin", 9); /* Pathname */
206 memset(&buf[ccpmp_pos + 4 + 9 ], 0x20, sizeof(uint8_t)); /* File Type */ 204 memset(&buf[ccpmp_pos + 4 + 9 ], 0x20, sizeof(uint8_t)); /* File Type */
@@ -210,7 +208,8 @@ int chinachip_patch(const char* firmware, const char* bootloader,
210 time_info = localtime(&cur_time); 208 time_info = localtime(&cur_time);
211 if(time_info == NULL) 209 if(time_info == NULL)
212 { 210 {
213 ERR(tr("Can't obtain current time!")); 211 fprintf(stderr, "[ERR] Can't obtain current time!\n");
212 result = E_GET_TIME;
214 goto err; 213 goto err;
215 } 214 }
216 215
@@ -220,11 +219,11 @@ int chinachip_patch(const char* firmware, const char* bootloader,
220 time_info->tm_hour, 219 time_info->tm_hour,
221 time_info->tm_min); 220 time_info->tm_min);
222 221
223 INFO(tr("Computing checksum...")); 222 fprintf(stderr, "[INFO] Computing checksum...\n");
224 for(i = sizeof(struct header); i < TOTAL_SIZE; i+=4) 223 for(i = sizeof(struct header); i < TOTAL_SIZE; i+=4)
225 checksum += le2int(&buf[i]); 224 checksum += le2int(&buf[i]);
226 225
227 INFO(tr("Updating main header...")); 226 fprintf(stderr, "[INFO] Updating main header...\n");
228 memcpy(&buf[offsetof(struct header, timestamp)], header_time, 12); 227 memcpy(&buf[offsetof(struct header, timestamp)], header_time, 12);
229 int2le(&buf[offsetof(struct header, size) ], TOTAL_SIZE); 228 int2le(&buf[offsetof(struct header, size) ], TOTAL_SIZE);
230 int2le(&buf[offsetof(struct header, checksum) ], checksum); 229 int2le(&buf[offsetof(struct header, checksum) ], checksum);
@@ -232,20 +231,18 @@ int chinachip_patch(const char* firmware, const char* bootloader,
232 od = fopen(output, "wb"); 231 od = fopen(output, "wb");
233 if(!od) 232 if(!od)
234 { 233 {
235 ERR(tr("Can't open file %s!"), output); 234 fprintf(stderr, "[ERR] Can't open file %s!\n", output);
235 result = E_OPEN_OUTFILE;
236 goto err; 236 goto err;
237 } 237 }
238 238
239 INFO(tr("Writing output to %s..."), output); 239 fprintf(stderr, "[INFO] Writing output to %s...\n", output);
240 if(fwrite(buf, TOTAL_SIZE, 1, od) != 1) 240 if(fwrite(buf, TOTAL_SIZE, 1, od) != 1)
241 { 241 {
242 ERR(tr("Can't write to file %s!"), output); 242 fprintf(stderr, "[ERR] Can't write to file %s!\n", output);
243 result = E_WRITE_OUTFILE;
243 goto err; 244 goto err;
244 } 245 }
245 fclose(od);
246 free(buf);
247
248 return 0;
249 246
250err: 247err:
251 if(buf) 248 if(buf)
@@ -257,6 +254,6 @@ err:
257 if(od) 254 if(od)
258 fclose(od); 255 fclose(od);
259 256
260 return -1; 257 return result;
261} 258}
262 259