summaryrefslogtreecommitdiff
path: root/tools/mkboot.c
diff options
context:
space:
mode:
authorDominik Riebeling <Dominik.Riebeling@gmail.com>2008-06-18 22:30:59 +0000
committerDominik Riebeling <Dominik.Riebeling@gmail.com>2008-06-18 22:30:59 +0000
commitc47988034fbd5d7de8fcda2a87224bdb0b5dcfe6 (patch)
treedeb96279c6952871b5c0f47ec25879bb73240afd /tools/mkboot.c
parentefbd2b8d7a9295f1a5544717c1b86527cc27ac7b (diff)
downloadrockbox-c47988034fbd5d7de8fcda2a87224bdb0b5dcfe6.tar.gz
rockbox-c47988034fbd5d7de8fcda2a87224bdb0b5dcfe6.zip
Factor out scramble / mkboot functions to allow easier reuse (for rbutil).
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@17732 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'tools/mkboot.c')
-rw-r--r--tools/mkboot.c43
1 files changed, 29 insertions, 14 deletions
diff --git a/tools/mkboot.c b/tools/mkboot.c
index ce743e9414..d86a4fc177 100644
--- a/tools/mkboot.c
+++ b/tools/mkboot.c
@@ -19,25 +19,25 @@
19#include <stdio.h> 19#include <stdio.h>
20#include <stdlib.h> 20#include <stdlib.h>
21#include <string.h> 21#include <string.h>
22#include "mkboot.h"
22 23
23void usage(void) 24#ifndef RBUTIL
25static void usage(void)
24{ 26{
25 printf("usage: mkboot [-h300] <firmware file> <boot file> <output file>\n"); 27 printf("usage: mkboot [-h300] <firmware file> <boot file> <output file>\n");
26 28
27 exit(1); 29 exit(1);
28} 30}
31#endif
29 32
30unsigned char image[0x400000 + 0x220 + 0x400000/0x200]; 33static unsigned char image[0x400000 + 0x220 + 0x400000/0x200];
31 34
35#ifndef RBUTIL
32int main(int argc, char *argv[]) 36int main(int argc, char *argv[])
33{ 37{
34 char *infile, *bootfile, *outfile; 38 char *infile, *bootfile, *outfile;
35 FILE *f;
36 int i;
37 int len;
38 int actual_length, total_length, binary_length, num_chksums;
39 int origin = 0x1f0000; /* H1x0 bootloader address */ 39 int origin = 0x1f0000; /* H1x0 bootloader address */
40 40
41 if(argc < 3) { 41 if(argc < 3) {
42 usage(); 42 usage();
43 } 43 }
@@ -55,6 +55,16 @@ int main(int argc, char *argv[])
55 bootfile = argv[2]; 55 bootfile = argv[2];
56 outfile = argv[3]; 56 outfile = argv[3];
57 } 57 }
58 return mkboot(infile, bootfile, outfile, origin);
59}
60#endif
61
62int mkboot(const char* infile, const char* bootfile, const char* outfile, int origin)
63{
64 FILE *f;
65 int i;
66 int len;
67 int actual_length, total_length, binary_length, num_chksums;
58 68
59 memset(image, 0xff, sizeof(image)); 69 memset(image, 0xff, sizeof(image));
60 70
@@ -62,13 +72,14 @@ int main(int argc, char *argv[])
62 f = fopen(infile, "rb"); 72 f = fopen(infile, "rb");
63 if(!f) { 73 if(!f) {
64 perror(infile); 74 perror(infile);
65 exit(1); 75 return -1;
66 } 76 }
67 77
68 i = fread(image, 1, 16, f); 78 i = fread(image, 1, 16, f);
69 if(i < 16) { 79 if(i < 16) {
70 perror(infile); 80 perror(infile);
71 exit(1); 81 fclose(f);
82 return -2;
72 } 83 }
73 84
74 /* This is the length of the binary image without the scrambling 85 /* This is the length of the binary image without the scrambling
@@ -81,7 +92,8 @@ int main(int argc, char *argv[])
81 i = fread(image+16, 1, len, f); 92 i = fread(image+16, 1, len, f);
82 if(i < len) { 93 if(i < len) {
83 perror(infile); 94 perror(infile);
84 exit(1); 95 fclose(f);
96 return -3;
85 } 97 }
86 98
87 fclose(f); 99 fclose(f);
@@ -90,7 +102,8 @@ int main(int argc, char *argv[])
90 f = fopen(bootfile, "rb"); 102 f = fopen(bootfile, "rb");
91 if(!f) { 103 if(!f) {
92 perror(bootfile); 104 perror(bootfile);
93 exit(1); 105 fclose(f);
106 return -4;
94 } 107 }
95 108
96 fseek(f, 0, SEEK_END); 109 fseek(f, 0, SEEK_END);
@@ -101,7 +114,8 @@ int main(int argc, char *argv[])
101 i = fread(image+0x220 + origin, 1, len, f); 114 i = fread(image+0x220 + origin, 1, len, f);
102 if(i < len) { 115 if(i < len) {
103 perror(bootfile); 116 perror(bootfile);
104 exit(1); 117 fclose(f);
118 return -5;
105 } 119 }
106 120
107 fclose(f); 121 fclose(f);
@@ -109,7 +123,7 @@ int main(int argc, char *argv[])
109 f = fopen(outfile, "wb"); 123 f = fopen(outfile, "wb");
110 if(!f) { 124 if(!f) {
111 perror(outfile); 125 perror(outfile);
112 exit(1); 126 return -6;
113 } 127 }
114 128
115 /* Patch the reset vector to start the boot loader */ 129 /* Patch the reset vector to start the boot loader */
@@ -161,7 +175,8 @@ int main(int argc, char *argv[])
161 i = fwrite(image, 1, total_length, f); 175 i = fwrite(image, 1, total_length, f);
162 if(i < total_length) { 176 if(i < total_length) {
163 perror(outfile); 177 perror(outfile);
164 exit(1); 178 fclose(f);
179 return -7;
165 } 180 }
166 181
167 printf("Wrote 0x%x bytes in %s\n", total_length, outfile); 182 printf("Wrote 0x%x bytes in %s\n", total_length, outfile);