summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Riebeling <Dominik.Riebeling@gmail.com>2009-09-17 21:03:40 +0000
committerDominik Riebeling <Dominik.Riebeling@gmail.com>2009-09-17 21:03:40 +0000
commitd2704524022c3c730463c1f566bb7b4039b866c4 (patch)
tree1940016ab0d2b510be7c0e058128aa13e7247bbd
parentbe3c62f9ab8940de9027dd3e7f86a30ca22dea2a (diff)
downloadrockbox-d2704524022c3c730463c1f566bb7b4039b866c4.tar.gz
rockbox-d2704524022c3c730463c1f566bb7b4039b866c4.zip
Refactor mknkboot in preparation for beastpatcher integration.
- separate patching from main function. - improve cleaning up open files / allocated memory on errors. - correct check for number of arguments. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@22723 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--tools/mknkboot.c58
1 files changed, 40 insertions, 18 deletions
diff --git a/tools/mknkboot.c b/tools/mknkboot.c
index db006d1978..87372500e5 100644
--- a/tools/mknkboot.c
+++ b/tools/mknkboot.c
@@ -140,40 +140,36 @@ static off_t filesize(int fd) {
140} 140}
141 141
142 142
143int main(int argc, char *argv[]) 143int mknkboot(const char* infile, const char* bootfile, const char* outfile)
144{ 144{
145 char *infile, *bootfile, *outfile; 145 int fdin, fdboot = -1, fdout = -1;
146 int fdin, fdboot,fdout;
147 int i,n; 146 int i,n;
148 int inlength,bootlength,newlength; 147 int inlength,bootlength,newlength;
149 unsigned char* buf; 148 unsigned char* buf = NULL;
150 unsigned char* boot; 149 unsigned char* boot;
151 unsigned char* boot2; 150 unsigned char* boot2;
152 unsigned char* disable; 151 unsigned char* disable;
153 uint32_t sum; 152 uint32_t sum;
154 153 int result = 0;
155 if(argc < 3) {
156 usage();
157 }
158
159 infile = argv[1];
160 bootfile = argv[2];
161 outfile = argv[3];
162 154
163 fdin = open(infile, O_RDONLY|O_BINARY); 155 fdin = open(infile, O_RDONLY|O_BINARY);
164 if (fdin < 0) 156 if (fdin < 0)
165 { 157 {
166 perror(infile); 158 perror(infile);
159 result = 1;
160 goto quit;
167 } 161 }
168 162
169 fdboot = open(bootfile, O_RDONLY|O_BINARY); 163 fdboot = open(bootfile, O_RDONLY|O_BINARY);
170 if (fdboot < 0) 164 if (fdboot < 0)
171 { 165 {
172 perror(bootfile); 166 perror(bootfile);
167 close(fdin);
168 result = 2;
169 goto quit;
173 } 170 }
174 171
175 inlength = filesize(fdin); 172 inlength = filesize(fdin);
176
177 bootlength = filesize(fdboot); 173 bootlength = filesize(fdboot);
178 174
179 /* Create buffer for original nk.bin, plus our bootloader (with 12 175 /* Create buffer for original nk.bin, plus our bootloader (with 12
@@ -185,7 +181,8 @@ int main(int argc, char *argv[])
185 if (buf==NULL) 181 if (buf==NULL)
186 { 182 {
187 printf("[ERR] Could not allocate memory, aborting\n"); 183 printf("[ERR] Could not allocate memory, aborting\n");
188 return 1; 184 result = 3;
185 goto quit;
189 } 186 }
190 187
191 /****** STEP 1 - Read original nk.bin into buffer */ 188 /****** STEP 1 - Read original nk.bin into buffer */
@@ -194,7 +191,8 @@ int main(int argc, char *argv[])
194 if (n != inlength) 191 if (n != inlength)
195 { 192 {
196 printf("[ERR] Could not read from %s\n",infile); 193 printf("[ERR] Could not read from %s\n",infile);
197 return 2; 194 result = 4;
195 goto quit;
198 } 196 }
199 197
200 /****** STEP 2 - Move EOF record to the new EOF */ 198 /****** STEP 2 - Move EOF record to the new EOF */
@@ -218,7 +216,8 @@ int main(int argc, char *argv[])
218 if (n != bootlength) 216 if (n != bootlength)
219 { 217 {
220 printf("[ERR] Could not read from %s\n",bootfile); 218 printf("[ERR] Could not read from %s\n",bootfile);
221 return 3; 219 result = 5;
220 goto quit;
222 } 221 }
223 222
224 /****** STEP 5 - Create header for bootloader record */ 223 /****** STEP 5 - Create header for bootloader record */
@@ -257,18 +256,41 @@ int main(int argc, char *argv[])
257 if (fdout < 0) 256 if (fdout < 0)
258 { 257 {
259 perror(outfile); 258 perror(outfile);
259 result = 6;
260 goto quit;
260 } 261 }
261 262
262 n = write(fdout, buf, newlength); 263 n = write(fdout, buf, newlength);
263 if (n != newlength) 264 if (n != newlength)
264 { 265 {
265 printf("[ERR] Could not write output file %s\n",outfile); 266 printf("[ERR] Could not write output file %s\n",outfile);
266 return 3; 267 result = 7;
268 goto quit;
267 } 269 }
268 270
271quit:
272 if(buf != NULL)
273 free(buf);
269 close(fdin); 274 close(fdin);
270 close(fdboot); 275 close(fdboot);
271 close(fdout); 276 close(fdout);
272 277
273 return 0; 278 return result;
274} 279}
280
281
282int main(int argc, char* argv[])
283{
284 char *infile, *bootfile, *outfile;
285 if(argc < 4) {
286 usage();
287 }
288
289 infile = argv[1];
290 bootfile = argv[2];
291 outfile = argv[3];
292
293 return mknkboot(infile, bootfile, outfile);
294
295}
296