mkubootimage.c revision 1.1 1 1.1 jmcneill /* $NetBSD: mkubootimage.c,v 1.1 2010/06/18 18:55:48 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. The name of the author may not be used to endorse or promote products
13 1.1 jmcneill * derived from this software without specific prior written permission.
14 1.1 jmcneill *
15 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 jmcneill * SUCH DAMAGE.
26 1.1 jmcneill */
27 1.1 jmcneill
28 1.1 jmcneill #include <sys/cdefs.h>
29 1.1 jmcneill __RCSID("$NetBSD: mkubootimage.c,v 1.1 2010/06/18 18:55:48 jmcneill Exp $");
30 1.1 jmcneill
31 1.1 jmcneill #include <sys/mman.h>
32 1.1 jmcneill #include <sys/stat.h>
33 1.1 jmcneill #include <err.h>
34 1.1 jmcneill #include <errno.h>
35 1.1 jmcneill #include <fcntl.h>
36 1.1 jmcneill #include <limits.h>
37 1.1 jmcneill #include <stdint.h>
38 1.1 jmcneill #include <stdio.h>
39 1.1 jmcneill #include <stdlib.h>
40 1.1 jmcneill #include <string.h>
41 1.1 jmcneill #include <time.h>
42 1.1 jmcneill #include <unistd.h>
43 1.1 jmcneill
44 1.1 jmcneill #include "uboot.h"
45 1.1 jmcneill
46 1.1 jmcneill #ifndef __arraycount
47 1.1 jmcneill #define __arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
48 1.1 jmcneill #endif
49 1.1 jmcneill
50 1.1 jmcneill extern uint32_t crc32(const void *, size_t);
51 1.1 jmcneill
52 1.1 jmcneill static enum uboot_image_arch image_arch = IH_ARCH_UNKNOWN;
53 1.1 jmcneill static enum uboot_image_type image_type = IH_TYPE_UNKNOWN;
54 1.1 jmcneill static enum uboot_image_comp image_comp = IH_COMP_NONE;
55 1.1 jmcneill static uint32_t image_loadaddr = 0;
56 1.1 jmcneill static uint32_t image_entrypoint = 0;
57 1.1 jmcneill static char *image_name;
58 1.1 jmcneill
59 1.1 jmcneill struct uboot_arch {
60 1.1 jmcneill enum uboot_image_arch arch;
61 1.1 jmcneill const char *name;
62 1.1 jmcneill } uboot_arch[] = {
63 1.1 jmcneill { IH_ARCH_ARM, "arm" },
64 1.1 jmcneill { IH_ARCH_PPC, "powerpc" },
65 1.1 jmcneill };
66 1.1 jmcneill
67 1.1 jmcneill static enum uboot_image_arch
68 1.1 jmcneill get_arch(const char *name)
69 1.1 jmcneill {
70 1.1 jmcneill unsigned int i;
71 1.1 jmcneill
72 1.1 jmcneill for (i = 0; i < __arraycount(uboot_arch); i++) {
73 1.1 jmcneill if (strcmp(uboot_arch[i].name, name) == 0)
74 1.1 jmcneill return uboot_arch[i].arch;
75 1.1 jmcneill }
76 1.1 jmcneill
77 1.1 jmcneill return IH_ARCH_UNKNOWN;
78 1.1 jmcneill }
79 1.1 jmcneill
80 1.1 jmcneill struct uboot_type {
81 1.1 jmcneill enum uboot_image_type type;
82 1.1 jmcneill const char *name;
83 1.1 jmcneill } uboot_type[] = {
84 1.1 jmcneill { IH_TYPE_KERNEL, "kernel" },
85 1.1 jmcneill { IH_TYPE_RAMDISK, "ramdisk" },
86 1.1 jmcneill { IH_TYPE_FILESYSTEM, "fs" },
87 1.1 jmcneill };
88 1.1 jmcneill
89 1.1 jmcneill static enum uboot_image_type
90 1.1 jmcneill get_type(const char *name)
91 1.1 jmcneill {
92 1.1 jmcneill unsigned int i;
93 1.1 jmcneill
94 1.1 jmcneill for (i = 0; i < __arraycount(uboot_type); i++) {
95 1.1 jmcneill if (strcmp(uboot_type[i].name, name) == 0)
96 1.1 jmcneill return uboot_type[i].type;
97 1.1 jmcneill }
98 1.1 jmcneill
99 1.1 jmcneill return IH_TYPE_UNKNOWN;
100 1.1 jmcneill }
101 1.1 jmcneill
102 1.1 jmcneill struct uboot_comp {
103 1.1 jmcneill enum uboot_image_comp comp;
104 1.1 jmcneill const char *name;
105 1.1 jmcneill } uboot_comp[] = {
106 1.1 jmcneill { IH_COMP_NONE, "none" },
107 1.1 jmcneill { IH_COMP_GZIP, "gz" },
108 1.1 jmcneill { IH_COMP_BZIP2, "bz2" },
109 1.1 jmcneill };
110 1.1 jmcneill
111 1.1 jmcneill static enum uboot_image_comp
112 1.1 jmcneill get_comp(const char *name)
113 1.1 jmcneill {
114 1.1 jmcneill unsigned int i;
115 1.1 jmcneill
116 1.1 jmcneill for (i = 0; i < __arraycount(uboot_comp); i++) {
117 1.1 jmcneill if (strcmp(uboot_comp[i].name, name) == 0)
118 1.1 jmcneill return uboot_comp[i].comp;
119 1.1 jmcneill }
120 1.1 jmcneill
121 1.1 jmcneill return IH_TYPE_UNKNOWN;
122 1.1 jmcneill }
123 1.1 jmcneill
124 1.1 jmcneill static void
125 1.1 jmcneill usage(void)
126 1.1 jmcneill {
127 1.1 jmcneill fprintf(stderr, "usage: mkuboot -A <arm|powerpc>");
128 1.1 jmcneill fprintf(stderr, " -T <kernel|ramdisk|fs>");
129 1.1 jmcneill fprintf(stderr, " -C <none|gz|bz2>");
130 1.1 jmcneill fprintf(stderr, " -a <addr> -e <ep> -n <name>");
131 1.1 jmcneill fprintf(stderr, " <srcfile> <dstfile>\n");
132 1.1 jmcneill
133 1.1 jmcneill exit(EXIT_FAILURE);
134 1.1 jmcneill }
135 1.1 jmcneill
136 1.1 jmcneill static void
137 1.1 jmcneill dump_header(struct uboot_image_header *hdr)
138 1.1 jmcneill {
139 1.1 jmcneill time_t tm = ntohl(hdr->ih_time);
140 1.1 jmcneill
141 1.1 jmcneill printf(" magic: 0x%08x\n", ntohl(hdr->ih_magic));
142 1.1 jmcneill printf(" time: %s", ctime(&tm));
143 1.1 jmcneill printf(" size: %u\n", ntohl(hdr->ih_size));
144 1.1 jmcneill printf(" load addr: 0x%08x\n", ntohl(hdr->ih_load));
145 1.1 jmcneill printf(" entry point: 0x%08x\n", ntohl(hdr->ih_ep));
146 1.1 jmcneill printf(" data crc: 0x%08x\n", ntohl(hdr->ih_dcrc));
147 1.1 jmcneill printf(" os: %d\n", hdr->ih_os);
148 1.1 jmcneill printf(" arch: %d\n", hdr->ih_arch);
149 1.1 jmcneill printf(" type: %d\n", hdr->ih_type);
150 1.1 jmcneill printf(" comp: %d\n", hdr->ih_comp);
151 1.1 jmcneill printf(" name: %s\n", hdr->ih_name);
152 1.1 jmcneill printf(" header crc: 0x%08x\n", hdr->ih_hcrc);
153 1.1 jmcneill }
154 1.1 jmcneill
155 1.1 jmcneill static int
156 1.1 jmcneill generate_header(struct uboot_image_header *hdr, int kernel_fd)
157 1.1 jmcneill {
158 1.1 jmcneill uint8_t *p;
159 1.1 jmcneill struct stat st;
160 1.1 jmcneill uint32_t crc;
161 1.1 jmcneill int error;
162 1.1 jmcneill
163 1.1 jmcneill error = fstat(kernel_fd, &st);
164 1.1 jmcneill if (error == -1) {
165 1.1 jmcneill perror("stat");
166 1.1 jmcneill return errno;
167 1.1 jmcneill }
168 1.1 jmcneill
169 1.1 jmcneill if (st.st_size + sizeof(*hdr) > UINT32_MAX) {
170 1.1 jmcneill fprintf(stderr, "fatal: kernel too big\n");
171 1.1 jmcneill return EINVAL;
172 1.1 jmcneill }
173 1.1 jmcneill
174 1.1 jmcneill p = mmap(0, st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, kernel_fd, 0);
175 1.1 jmcneill if (p == MAP_FAILED) {
176 1.1 jmcneill perror("mmap kernel");
177 1.1 jmcneill return EINVAL;
178 1.1 jmcneill }
179 1.1 jmcneill crc = crc32(p, st.st_size);
180 1.1 jmcneill munmap(p, st.st_size);
181 1.1 jmcneill
182 1.1 jmcneill memset(hdr, 0, sizeof(*hdr));
183 1.1 jmcneill hdr->ih_magic = htonl(IH_MAGIC);
184 1.1 jmcneill hdr->ih_time = htonl(st.st_mtime);
185 1.1 jmcneill hdr->ih_size = htonl(st.st_size);
186 1.1 jmcneill hdr->ih_load = htonl(image_loadaddr);
187 1.1 jmcneill hdr->ih_ep = htonl(image_entrypoint);
188 1.1 jmcneill hdr->ih_dcrc = htonl(crc);
189 1.1 jmcneill hdr->ih_os = IH_OS_NETBSD;
190 1.1 jmcneill hdr->ih_arch = image_arch;
191 1.1 jmcneill hdr->ih_type = image_type;
192 1.1 jmcneill hdr->ih_comp = image_comp;
193 1.1 jmcneill strncpy((char *)hdr->ih_name, image_name, sizeof(hdr->ih_name));
194 1.1 jmcneill crc = crc32((void *)hdr, sizeof(*hdr));
195 1.1 jmcneill hdr->ih_hcrc = htonl(crc);
196 1.1 jmcneill
197 1.1 jmcneill dump_header(hdr);
198 1.1 jmcneill
199 1.1 jmcneill return 0;
200 1.1 jmcneill }
201 1.1 jmcneill
202 1.1 jmcneill static int
203 1.1 jmcneill write_image(struct uboot_image_header *hdr, int kernel_fd, int image_fd)
204 1.1 jmcneill {
205 1.1 jmcneill uint8_t buf[4096];
206 1.1 jmcneill ssize_t rlen, wlen;
207 1.1 jmcneill
208 1.1 jmcneill wlen = write(image_fd, hdr, sizeof(*hdr));
209 1.1 jmcneill if (wlen != sizeof(*hdr)) {
210 1.1 jmcneill perror("short write");
211 1.1 jmcneill return errno;
212 1.1 jmcneill }
213 1.1 jmcneill
214 1.1 jmcneill while ((rlen = read(kernel_fd, buf, sizeof(buf))) > 0) {
215 1.1 jmcneill wlen = write(image_fd, buf, rlen);
216 1.1 jmcneill if (wlen != rlen) {
217 1.1 jmcneill perror("short write");
218 1.1 jmcneill return errno;
219 1.1 jmcneill }
220 1.1 jmcneill }
221 1.1 jmcneill
222 1.1 jmcneill return 0;
223 1.1 jmcneill }
224 1.1 jmcneill
225 1.1 jmcneill int
226 1.1 jmcneill main(int argc, char *argv[])
227 1.1 jmcneill {
228 1.1 jmcneill struct uboot_image_header hdr;
229 1.1 jmcneill const char *src, *dest;
230 1.1 jmcneill char *ep;
231 1.1 jmcneill int kernel_fd, image_fd;
232 1.1 jmcneill int ch;
233 1.1 jmcneill unsigned long num;
234 1.1 jmcneill
235 1.1 jmcneill while ((ch = getopt(argc, argv, "A:C:T:a:e:hn:")) != -1) {
236 1.1 jmcneill switch (ch) {
237 1.1 jmcneill case 'A': /* arch */
238 1.1 jmcneill image_arch = get_arch(optarg);
239 1.1 jmcneill break;
240 1.1 jmcneill case 'C': /* comp */
241 1.1 jmcneill image_comp = get_comp(optarg);
242 1.1 jmcneill break;
243 1.1 jmcneill case 'T': /* type */
244 1.1 jmcneill image_type = get_type(optarg);
245 1.1 jmcneill break;
246 1.1 jmcneill case 'a': /* addr */
247 1.1 jmcneill errno = 0;
248 1.1 jmcneill num = strtoul(optarg, &ep, 0);
249 1.1 jmcneill if (*ep != '\0' || (errno == ERANGE &&
250 1.1 jmcneill (num == ULONG_MAX || num == 0)))
251 1.1 jmcneill errx(1, "illegal number -- %s", optarg);
252 1.1 jmcneill image_loadaddr = (uint32_t)num;
253 1.1 jmcneill break;
254 1.1 jmcneill case 'e': /* ep */
255 1.1 jmcneill errno = 0;
256 1.1 jmcneill num = strtoul(optarg, &ep, 0);
257 1.1 jmcneill if (*ep != '\0' || (errno == ERANGE &&
258 1.1 jmcneill (num == ULONG_MAX || num == 0)))
259 1.1 jmcneill errx(1, "illegal number -- %s", optarg);
260 1.1 jmcneill image_entrypoint = (uint32_t)num;
261 1.1 jmcneill break;
262 1.1 jmcneill case 'n': /* name */
263 1.1 jmcneill image_name = strdup(optarg);
264 1.1 jmcneill break;
265 1.1 jmcneill case 'h':
266 1.1 jmcneill default:
267 1.1 jmcneill usage();
268 1.1 jmcneill /* NOTREACHED */
269 1.1 jmcneill }
270 1.1 jmcneill }
271 1.1 jmcneill argc -= optind;
272 1.1 jmcneill argv += optind;
273 1.1 jmcneill
274 1.1 jmcneill if (argc != 2)
275 1.1 jmcneill usage();
276 1.1 jmcneill
277 1.1 jmcneill if (image_arch == IH_ARCH_UNKNOWN ||
278 1.1 jmcneill image_type == IH_TYPE_UNKNOWN ||
279 1.1 jmcneill image_loadaddr == 0 ||
280 1.1 jmcneill image_entrypoint == 0 ||
281 1.1 jmcneill image_name == NULL)
282 1.1 jmcneill usage();
283 1.1 jmcneill
284 1.1 jmcneill src = argv[0];
285 1.1 jmcneill dest = argv[1];
286 1.1 jmcneill
287 1.1 jmcneill kernel_fd = open(src, O_RDONLY);
288 1.1 jmcneill if (kernel_fd == -1) {
289 1.1 jmcneill perror("open kernel");
290 1.1 jmcneill return EXIT_FAILURE;
291 1.1 jmcneill }
292 1.1 jmcneill image_fd = open(dest, O_WRONLY|O_CREAT, 0666);
293 1.1 jmcneill if (image_fd == -1) {
294 1.1 jmcneill perror("open image");
295 1.1 jmcneill return EXIT_FAILURE;
296 1.1 jmcneill }
297 1.1 jmcneill
298 1.1 jmcneill if (generate_header(&hdr, kernel_fd) != 0)
299 1.1 jmcneill return EXIT_FAILURE;
300 1.1 jmcneill
301 1.1 jmcneill if (write_image(&hdr, kernel_fd, image_fd) != 0)
302 1.1 jmcneill return EXIT_FAILURE;
303 1.1 jmcneill
304 1.1 jmcneill close(image_fd);
305 1.1 jmcneill close(kernel_fd);
306 1.1 jmcneill
307 1.1 jmcneill return EXIT_SUCCESS;
308 1.1 jmcneill }
309