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