diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/mktccboot.c | 40 |
1 files changed, 29 insertions, 11 deletions
diff --git a/tools/mktccboot.c b/tools/mktccboot.c index ad7247419a..8c2e73d5f2 100644 --- a/tools/mktccboot.c +++ b/tools/mktccboot.c | |||
@@ -96,12 +96,13 @@ off_t filesize(int fd) { | |||
96 | int main(int argc, char *argv[]) | 96 | int main(int argc, char *argv[]) |
97 | { | 97 | { |
98 | char *infile, *bootfile, *outfile; | 98 | char *infile, *bootfile, *outfile; |
99 | int fdin, fdboot,fdout; | 99 | int fdin = -1, fdboot = -1, fdout = -1; |
100 | int n; | 100 | int n; |
101 | int inlength,bootlength; | 101 | int inlength,bootlength; |
102 | uint32_t ldr; | 102 | uint32_t ldr; |
103 | unsigned char* image; | 103 | unsigned char* image; |
104 | int origoffset; | 104 | int origoffset; |
105 | int ret = 0; | ||
105 | 106 | ||
106 | if(argc < 3) { | 107 | if(argc < 3) { |
107 | usage(); | 108 | usage(); |
@@ -115,37 +116,44 @@ int main(int argc, char *argv[]) | |||
115 | if (fdin < 0) | 116 | if (fdin < 0) |
116 | { | 117 | { |
117 | perror(infile); | 118 | perror(infile); |
119 | ret = 1; | ||
120 | goto error_exit; | ||
118 | } | 121 | } |
119 | 122 | ||
120 | fdboot = open(bootfile, O_RDONLY|O_BINARY); | 123 | fdboot = open(bootfile, O_RDONLY|O_BINARY); |
121 | if (fdboot < 0) | 124 | if (fdboot < 0) |
122 | { | 125 | { |
123 | perror(bootfile); | 126 | perror(bootfile); |
127 | ret = 2; | ||
128 | goto error_exit; | ||
124 | } | 129 | } |
125 | 130 | ||
126 | inlength = filesize(fdin); | 131 | inlength = filesize(fdin); |
127 | bootlength = filesize(fdboot); | 132 | bootlength = filesize(fdboot); |
128 | 133 | ||
129 | image = malloc(inlength + bootlength); | 134 | image = malloc(inlength + bootlength); |
130 | 135 | ||
131 | if (image==NULL) | 136 | if (image==NULL) |
132 | { | 137 | { |
133 | printf("[ERR] Could not allocate memory, aborting\n"); | 138 | printf("[ERR] Could not allocate memory, aborting\n"); |
134 | return 1; | 139 | ret = 3; |
140 | goto error_exit; | ||
135 | } | 141 | } |
136 | 142 | ||
137 | n = read(fdin, image, inlength); | 143 | n = read(fdin, image, inlength); |
138 | if (n != inlength) | 144 | if (n != inlength) |
139 | { | 145 | { |
140 | printf("[ERR] Could not read from %s\n",infile); | 146 | printf("[ERR] Could not read from %s\n",infile); |
141 | return 2; | 147 | ret = 4; |
148 | goto error_exit; | ||
142 | } | 149 | } |
143 | 150 | ||
144 | n = read(fdboot, image + inlength, bootlength); | 151 | n = read(fdboot, image + inlength, bootlength); |
145 | if (n != bootlength) | 152 | if (n != bootlength) |
146 | { | 153 | { |
147 | printf("[ERR] Could not read from %s\n",bootfile); | 154 | printf("[ERR] Could not read from %s\n",bootfile); |
148 | return 3; | 155 | ret = 5; |
156 | goto error_exit; | ||
149 | } | 157 | } |
150 | 158 | ||
151 | ldr = get_uint32le(image); | 159 | ldr = get_uint32le(image); |
@@ -170,18 +178,28 @@ int main(int argc, char *argv[]) | |||
170 | if (fdout < 0) | 178 | if (fdout < 0) |
171 | { | 179 | { |
172 | perror(bootfile); | 180 | perror(bootfile); |
181 | ret = 6; | ||
182 | goto error_exit; | ||
173 | } | 183 | } |
174 | 184 | ||
175 | n = write(fdout, image, inlength + bootlength); | 185 | n = write(fdout, image, inlength + bootlength); |
176 | if (n != inlength + bootlength) | 186 | if (n != inlength + bootlength) |
177 | { | 187 | { |
178 | printf("[ERR] Could not write output file %s\n",outfile); | 188 | printf("[ERR] Could not write output file %s\n",outfile); |
179 | return 3; | 189 | ret = 7; |
190 | goto error_exit; | ||
180 | } | 191 | } |
181 | 192 | ||
182 | close(fdin); | 193 | error_exit: |
183 | close(fdboot); | 194 | |
184 | close(fdout); | 195 | if (fdin >= 0) |
185 | 196 | close(fdin); | |
186 | return 0; | 197 | |
198 | if (fdboot >= 0) | ||
199 | close(fdboot); | ||
200 | |||
201 | if (fdout >= 0) | ||
202 | close(fdout); | ||
203 | |||
204 | return ret; | ||
187 | } | 205 | } |