summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThe Android Open Source Project2009-03-03 20:29:04 -0600
committerThe Android Open Source Project2009-03-03 20:29:04 -0600
commite54eebbf1a908d65ee8cf80bab62821c05666d70 (patch)
tree4b825dc642cb6eb9a060e54bf8d69288fbee4904 /mkbootimg
parenta1e1c1b106423de09bc918502e7a51d4ffe5a4ae (diff)
downloadplatform-system-core-e54eebbf1a908d65ee8cf80bab62821c05666d70.tar.gz
platform-system-core-e54eebbf1a908d65ee8cf80bab62821c05666d70.tar.xz
platform-system-core-e54eebbf1a908d65ee8cf80bab62821c05666d70.zip
auto import from //depot/cupcake/@135843
Diffstat (limited to 'mkbootimg')
-rw-r--r--mkbootimg/Android.mk12
-rw-r--r--mkbootimg/bootimg.h97
-rw-r--r--mkbootimg/mkbootimg.c251
3 files changed, 0 insertions, 360 deletions
diff --git a/mkbootimg/Android.mk b/mkbootimg/Android.mk
deleted file mode 100644
index 18f0ff306..000000000
--- a/mkbootimg/Android.mk
+++ /dev/null
@@ -1,12 +0,0 @@
1
2LOCAL_PATH:= $(call my-dir)
3include $(CLEAR_VARS)
4
5LOCAL_SRC_FILES := mkbootimg.c
6LOCAL_STATIC_LIBRARIES := libmincrypt
7
8LOCAL_MODULE := mkbootimg
9
10include $(BUILD_HOST_EXECUTABLE)
11
12$(call dist-for-goals,droid,$(LOCAL_BUILT_MODULE))
diff --git a/mkbootimg/bootimg.h b/mkbootimg/bootimg.h
deleted file mode 100644
index 242ab35db..000000000
--- a/mkbootimg/bootimg.h
+++ /dev/null
@@ -1,97 +0,0 @@
1/* tools/mkbootimg/bootimg.h
2**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (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
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#ifndef _BOOT_IMAGE_H_
19#define _BOOT_IMAGE_H_
20
21typedef struct boot_img_hdr boot_img_hdr;
22
23#define BOOT_MAGIC "ANDROID!"
24#define BOOT_MAGIC_SIZE 8
25#define BOOT_NAME_SIZE 16
26#define BOOT_ARGS_SIZE 512
27
28struct boot_img_hdr
29{
30 unsigned char magic[BOOT_MAGIC_SIZE];
31
32 unsigned kernel_size; /* size in bytes */
33 unsigned kernel_addr; /* physical load addr */
34
35 unsigned ramdisk_size; /* size in bytes */
36 unsigned ramdisk_addr; /* physical load addr */
37
38 unsigned second_size; /* size in bytes */
39 unsigned second_addr; /* physical load addr */
40
41 unsigned tags_addr; /* physical addr for kernel tags */
42 unsigned page_size; /* flash page size we assume */
43 unsigned unused[2]; /* future expansion: should be 0 */
44
45 unsigned char name[BOOT_NAME_SIZE]; /* asciiz product name */
46
47 unsigned char cmdline[BOOT_ARGS_SIZE];
48
49 unsigned id[8]; /* timestamp / checksum / sha1 / etc */
50};
51
52/*
53** +-----------------+
54** | boot header | 1 page
55** +-----------------+
56** | kernel | n pages
57** +-----------------+
58** | ramdisk | m pages
59** +-----------------+
60** | second stage | o pages
61** +-----------------+
62**
63** n = (kernel_size + page_size - 1) / page_size
64** m = (ramdisk_size + page_size - 1) / page_size
65** o = (second_size + page_size - 1) / page_size
66**
67** 0. all entities are page_size aligned in flash
68** 1. kernel and ramdisk are required (size != 0)
69** 2. second is optional (second_size == 0 -> no second)
70** 3. load each element (kernel, ramdisk, second) at
71** the specified physical address (kernel_addr, etc)
72** 4. prepare tags at tag_addr. kernel_args[] is
73** appended to the kernel commandline in the tags.
74** 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
75** 6. if second_size != 0: jump to second_addr
76** else: jump to kernel_addr
77*/
78
79#if 0
80typedef struct ptentry ptentry;
81
82struct ptentry {
83 char name[16]; /* asciiz partition name */
84 unsigned start; /* starting block number */
85 unsigned length; /* length in blocks */
86 unsigned flags; /* set to zero */
87};
88
89/* MSM Partition Table ATAG
90**
91** length: 2 + 7 * n
92** atag: 0x4d534d70
93** <ptentry> x n
94*/
95#endif
96
97#endif
diff --git a/mkbootimg/mkbootimg.c b/mkbootimg/mkbootimg.c
deleted file mode 100644
index d803cf66d..000000000
--- a/mkbootimg/mkbootimg.c
+++ /dev/null
@@ -1,251 +0,0 @@
1/* tools/mkbootimg/mkbootimg.c
2**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (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
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <unistd.h>
22#include <fcntl.h>
23#include <errno.h>
24
25#include "mincrypt/sha.h"
26#include "bootimg.h"
27
28static void *load_file(const char *fn, unsigned *_sz)
29{
30 char *data;
31 int sz;
32 int fd;
33
34 data = 0;
35 fd = open(fn, O_RDONLY);
36 if(fd < 0) return 0;
37
38 sz = lseek(fd, 0, SEEK_END);
39 if(sz < 0) goto oops;
40
41 if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
42
43 data = (char*) malloc(sz);
44 if(data == 0) goto oops;
45
46 if(read(fd, data, sz) != sz) goto oops;
47 close(fd);
48
49 if(_sz) *_sz = sz;
50 return data;
51
52oops:
53 close(fd);
54 if(data != 0) free(data);
55 return 0;
56}
57
58int usage(void)
59{
60 fprintf(stderr,"usage: mkbootimg\n"
61 " --kernel <filename>\n"
62 " --ramdisk <filename>\n"
63 " [ --second <2ndbootloader-filename> ]\n"
64 " [ --cmdline <kernel-commandline> ]\n"
65 " [ --board <boardname> ]\n"
66 " -o|--output <filename>\n"
67 );
68 return 1;
69}
70
71
72
73static unsigned char padding[2048] = { 0, };
74
75int write_padding(int fd, unsigned pagesize, unsigned itemsize)
76{
77 unsigned pagemask = pagesize - 1;
78 unsigned count;
79
80 if((itemsize & pagemask) == 0) {
81 return 0;
82 }
83
84 count = pagesize - (itemsize & pagemask);
85
86 if(write(fd, padding, count) != count) {
87 return -1;
88 } else {
89 return 0;
90 }
91}
92
93int main(int argc, char **argv)
94{
95 boot_img_hdr hdr;
96
97 char *kernel_fn = 0;
98 void *kernel_data = 0;
99 char *ramdisk_fn = 0;
100 void *ramdisk_data = 0;
101 char *second_fn = 0;
102 void *second_data = 0;
103 char *cmdline = "";
104 char *bootimg = 0;
105 char *board = "";
106 unsigned pagesize = 2048;
107 unsigned saddr = 0;
108 int fd;
109 SHA_CTX ctx;
110 uint8_t* sha;
111
112 argc--;
113 argv++;
114
115 memset(&hdr, 0, sizeof(hdr));
116
117 while(argc > 0){
118 char *arg = argv[0];
119 char *val = argv[1];
120 if(argc < 2) {
121 return usage();
122 }
123 argc -= 2;
124 argv += 2;
125 if(!strcmp(arg, "--output") || !strcmp(arg, "-o")) {
126 bootimg = val;
127 } else if(!strcmp(arg, "--kernel")) {
128 kernel_fn = val;
129 } else if(!strcmp(arg, "--ramdisk")) {
130 ramdisk_fn = val;
131 } else if(!strcmp(arg, "--second")) {
132 second_fn = val;
133 } else if(!strcmp(arg, "--cmdline")) {
134 cmdline = val;
135 } else if(!strcmp(arg, "--saddr")) {
136 saddr = strtoul(val, 0, 16);
137 } else if(!strcmp(arg, "--board")) {
138 board = val;
139 } else {
140 return usage();
141 }
142 }
143
144 if(bootimg == 0) {
145 fprintf(stderr,"error: no output filename specified\n");
146 return usage();
147 }
148
149 if(kernel_fn == 0) {
150 fprintf(stderr,"error: no kernel image specified\n");
151 return usage();
152 }
153
154 if(ramdisk_fn == 0) {
155 fprintf(stderr,"error: no ramdisk image specified\n");
156 return usage();
157 }
158
159 if(strlen(board) >= BOOT_NAME_SIZE) {
160 fprintf(stderr,"error: board name too large\n");
161 return usage();
162 }
163
164 strcpy(hdr.name, board);
165
166 hdr.kernel_addr = 0x10008000;
167 hdr.ramdisk_addr = 0x11000000;
168 if(saddr) {
169 hdr.second_addr = 0x00300000;
170 } else {
171 hdr.second_addr = 0x10F00000;
172 }
173 hdr.tags_addr = 0x10000100;
174 hdr.page_size = pagesize;
175
176 memcpy(hdr.magic, BOOT_MAGIC, BOOT_MAGIC_SIZE);
177
178 if(strlen(cmdline) > (BOOT_ARGS_SIZE - 1)) {
179 fprintf(stderr,"error: kernel commandline too large\n");
180 return 1;
181 }
182 strcpy((char*)hdr.cmdline, cmdline);
183
184 kernel_data = load_file(kernel_fn, &hdr.kernel_size);
185 if(kernel_data == 0) {
186 fprintf(stderr,"error: could not load kernel '%s'\n", kernel_fn);
187 return 1;
188 }
189
190 if(!strcmp(ramdisk_fn,"NONE")) {
191 ramdisk_data = 0;
192 hdr.ramdisk_size = 0;
193 } else {
194 ramdisk_data = load_file(ramdisk_fn, &hdr.ramdisk_size);
195 if(ramdisk_data == 0) {
196 fprintf(stderr,"error: could not load ramdisk '%s'\n", ramdisk_fn);
197 return 1;
198 }
199 }
200
201 if(second_fn) {
202 second_data = load_file(second_fn, &hdr.second_size);
203 if(second_data == 0) {
204 fprintf(stderr,"error: could not load secondstage '%s'\n", second_fn);
205 return 1;
206 }
207 }
208
209 /* put a hash of the contents in the header so boot images can be
210 * differentiated based on their first 2k.
211 */
212 SHA_init(&ctx);
213 SHA_update(&ctx, kernel_data, hdr.kernel_size);
214 SHA_update(&ctx, &hdr.kernel_size, sizeof(hdr.kernel_size));
215 SHA_update(&ctx, ramdisk_data, hdr.ramdisk_size);
216 SHA_update(&ctx, &hdr.ramdisk_size, sizeof(hdr.ramdisk_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);
222
223 fd = open(bootimg, O_CREAT | O_TRUNC | O_WRONLY, 0644);
224 if(fd < 0) {
225 fprintf(stderr,"error: could not create '%s'\n", bootimg);
226 return 1;
227 }
228
229 if(write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) goto fail;
230 if(write_padding(fd, pagesize, sizeof(hdr))) goto fail;
231
232 if(write(fd, kernel_data, hdr.kernel_size) != hdr.kernel_size) goto fail;
233 if(write_padding(fd, pagesize, hdr.kernel_size)) goto fail;
234
235 if(write(fd, ramdisk_data, hdr.ramdisk_size) != hdr.ramdisk_size) goto fail;
236 if(write_padding(fd, pagesize, hdr.ramdisk_size)) goto fail;
237
238 if(second_data) {
239 if(write(fd, second_data, hdr.second_size) != hdr.second_size) goto fail;
240 if(write_padding(fd, pagesize, hdr.ramdisk_size)) goto fail;
241 }
242
243 return 0;
244
245fail:
246 unlink(bootimg);
247 close(fd);
248 fprintf(stderr,"error: failed writing '%s': %s\n", bootimg,
249 strerror(errno));
250 return 1;
251}