summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThe Android Open Source Project2009-01-20 16:04:01 -0600
committerThe Android Open Source Project2009-01-20 16:04:01 -0600
commit8ac3a138168f79b47356fb5aea2f6d95fc3147c6 (patch)
tree97e97f6ce4dbd0461e52b50192420b4a70b1b988 /mkbootimg
parent2eef60297a0ca1433d0824d6d662efd402709cfd (diff)
downloadplatform-system-core-8ac3a138168f79b47356fb5aea2f6d95fc3147c6.tar.gz
platform-system-core-8ac3a138168f79b47356fb5aea2f6d95fc3147c6.tar.xz
platform-system-core-8ac3a138168f79b47356fb5aea2f6d95fc3147c6.zip
auto import from //branches/cupcake/...@127101
Diffstat (limited to 'mkbootimg')
-rw-r--r--mkbootimg/Android.mk1
-rw-r--r--mkbootimg/mkbootimg.c74
2 files changed, 35 insertions, 40 deletions
diff --git a/mkbootimg/Android.mk b/mkbootimg/Android.mk
index a579de0b6..18f0ff306 100644
--- a/mkbootimg/Android.mk
+++ b/mkbootimg/Android.mk
@@ -3,6 +3,7 @@ LOCAL_PATH:= $(call my-dir)
3include $(CLEAR_VARS) 3include $(CLEAR_VARS)
4 4
5LOCAL_SRC_FILES := mkbootimg.c 5LOCAL_SRC_FILES := mkbootimg.c
6LOCAL_STATIC_LIBRARIES := libmincrypt
6 7
7LOCAL_MODULE := mkbootimg 8LOCAL_MODULE := mkbootimg
8 9
diff --git a/mkbootimg/mkbootimg.c b/mkbootimg/mkbootimg.c
index fa650cfca..d803cf66d 100644
--- a/mkbootimg/mkbootimg.c
+++ b/mkbootimg/mkbootimg.c
@@ -2,16 +2,16 @@
2** 2**
3** Copyright 2007, The Android Open Source Project 3** Copyright 2007, The Android Open Source Project
4** 4**
5** Licensed under the Apache License, Version 2.0 (the "License"); 5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License. 6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at 7** You may obtain a copy of the License at
8** 8**
9** http://www.apache.org/licenses/LICENSE-2.0 9** http://www.apache.org/licenses/LICENSE-2.0
10** 10**
11** Unless required by applicable law or agreed to in writing, software 11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS, 12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and 14** See the License for the specific language governing permissions and
15** limitations under the License. 15** limitations under the License.
16*/ 16*/
17 17
@@ -21,8 +21,8 @@
21#include <unistd.h> 21#include <unistd.h>
22#include <fcntl.h> 22#include <fcntl.h>
23#include <errno.h> 23#include <errno.h>
24#include <time.h>
25 24
25#include "mincrypt/sha.h"
26#include "bootimg.h" 26#include "bootimg.h"
27 27
28static void *load_file(const char *fn, unsigned *_sz) 28static void *load_file(const char *fn, unsigned *_sz)
@@ -82,7 +82,7 @@ int write_padding(int fd, unsigned pagesize, unsigned itemsize)
82 } 82 }
83 83
84 count = pagesize - (itemsize & pagemask); 84 count = pagesize - (itemsize & pagemask);
85 85
86 if(write(fd, padding, count) != count) { 86 if(write(fd, padding, count) != count) {
87 return -1; 87 return -1;
88 } else { 88 } else {
@@ -90,21 +90,10 @@ int write_padding(int fd, unsigned pagesize, unsigned itemsize)
90 } 90 }
91} 91}
92 92
93unsigned checksum(void *_ptr, unsigned len)
94{
95 unsigned chk = 0;
96 unsigned char *ptr = _ptr;
97 while (len > 0) {
98 chk += *ptr++;
99 len--;
100 }
101 return chk;
102}
103
104int main(int argc, char **argv) 93int main(int argc, char **argv)
105{ 94{
106 boot_img_hdr hdr; 95 boot_img_hdr hdr;
107 96
108 char *kernel_fn = 0; 97 char *kernel_fn = 0;
109 void *kernel_data = 0; 98 void *kernel_data = 0;
110 char *ramdisk_fn = 0; 99 char *ramdisk_fn = 0;
@@ -117,12 +106,14 @@ int main(int argc, char **argv)
117 unsigned pagesize = 2048; 106 unsigned pagesize = 2048;
118 unsigned saddr = 0; 107 unsigned saddr = 0;
119 int fd; 108 int fd;
120 109 SHA_CTX ctx;
110 uint8_t* sha;
111
121 argc--; 112 argc--;
122 argv++; 113 argv++;
123 114
124 memset(&hdr, 0, sizeof(hdr)); 115 memset(&hdr, 0, sizeof(hdr));
125 116
126 while(argc > 0){ 117 while(argc > 0){
127 char *arg = argv[0]; 118 char *arg = argv[0];
128 char *val = argv[1]; 119 char *val = argv[1];
@@ -171,7 +162,7 @@ int main(int argc, char **argv)
171 } 162 }
172 163
173 strcpy(hdr.name, board); 164 strcpy(hdr.name, board);
174 165
175 hdr.kernel_addr = 0x10008000; 166 hdr.kernel_addr = 0x10008000;
176 hdr.ramdisk_addr = 0x11000000; 167 hdr.ramdisk_addr = 0x11000000;
177 if(saddr) { 168 if(saddr) {
@@ -181,21 +172,21 @@ int main(int argc, char **argv)
181 } 172 }
182 hdr.tags_addr = 0x10000100; 173 hdr.tags_addr = 0x10000100;
183 hdr.page_size = pagesize; 174 hdr.page_size = pagesize;
184 175
185 memcpy(hdr.magic, BOOT_MAGIC, BOOT_MAGIC_SIZE); 176 memcpy(hdr.magic, BOOT_MAGIC, BOOT_MAGIC_SIZE);
186 177
187 if(strlen(cmdline) > (BOOT_ARGS_SIZE - 1)) { 178 if(strlen(cmdline) > (BOOT_ARGS_SIZE - 1)) {
188 fprintf(stderr,"error: kernel commandline too large\n"); 179 fprintf(stderr,"error: kernel commandline too large\n");
189 return 1; 180 return 1;
190 } 181 }
191 strcpy((char*)hdr.cmdline, cmdline); 182 strcpy((char*)hdr.cmdline, cmdline);
192 183
193 kernel_data = load_file(kernel_fn, &hdr.kernel_size); 184 kernel_data = load_file(kernel_fn, &hdr.kernel_size);
194 if(kernel_data == 0) { 185 if(kernel_data == 0) {
195 fprintf(stderr,"error: could not load kernel '%s'\n", kernel_fn); 186 fprintf(stderr,"error: could not load kernel '%s'\n", kernel_fn);
196 return 1; 187 return 1;
197 } 188 }
198 189
199 if(!strcmp(ramdisk_fn,"NONE")) { 190 if(!strcmp(ramdisk_fn,"NONE")) {
200 ramdisk_data = 0; 191 ramdisk_data = 0;
201 hdr.ramdisk_size = 0; 192 hdr.ramdisk_size = 0;
@@ -215,15 +206,19 @@ int main(int argc, char **argv)
215 } 206 }
216 } 207 }
217 208
218 /* put some stuff in the header to differentiate between 209 /* put a hash of the contents in the header so boot images can be
219 * different boot images. SHA1 would be nicer, but this 210 * differentiated based on their first 2k.
220 * isn't for crypto grade anything, just to have a quick 211 */
221 * way to compare boot.imgs based on their first 2k 212 SHA_init(&ctx);
222 */ 213 SHA_update(&ctx, kernel_data, hdr.kernel_size);
223 hdr.id[0] = (unsigned) time(0); 214 SHA_update(&ctx, &hdr.kernel_size, sizeof(hdr.kernel_size));
224 hdr.id[1] = checksum(kernel_data, hdr.kernel_size); 215 SHA_update(&ctx, ramdisk_data, hdr.ramdisk_size);
225 hdr.id[2] = checksum(ramdisk_data, hdr.ramdisk_size); 216 SHA_update(&ctx, &hdr.ramdisk_size, sizeof(hdr.ramdisk_size));
226 hdr.id[3] = checksum(second_data, hdr.second_size); 217 SHA_update(&ctx, second_data, hdr.second_size);
218 SHA_update(&ctx, &hdr.second_size, sizeof(hdr.second_size));
219 sha = SHA_final(&ctx);
220 memcpy(hdr.id, sha,
221 SHA_DIGEST_SIZE > sizeof(hdr.id) ? sizeof(hdr.id) : SHA_DIGEST_SIZE);
227 222
228 fd = open(bootimg, O_CREAT | O_TRUNC | O_WRONLY, 0644); 223 fd = open(bootimg, O_CREAT | O_TRUNC | O_WRONLY, 0644);
229 if(fd < 0) { 224 if(fd < 0) {
@@ -246,7 +241,7 @@ int main(int argc, char **argv)
246 } 241 }
247 242
248 return 0; 243 return 0;
249 244
250fail: 245fail:
251 unlink(bootimg); 246 unlink(bootimg);
252 close(fd); 247 close(fd);
@@ -254,4 +249,3 @@ fail:
254 strerror(errno)); 249 strerror(errno));
255 return 1; 250 return 1;
256} 251}
257