mkbootimage.c revision 1.1 1 1.1 garbled /* $NetBSD: mkbootimage.c,v 1.1 2007/12/18 18:19:07 garbled Exp $ */
2 1.1 garbled
3 1.1 garbled /*-
4 1.1 garbled * Copyright (C) 2006 Tim Rightnour
5 1.1 garbled * Copyright (C) 1999, 2000 NONAKA Kimihiro (nonaka (at) NetBSD.org)
6 1.1 garbled * Copyright (C) 1996, 1997, 1998, 1999 Cort Dougan (cort (at) fsmlasb.com)
7 1.1 garbled * Copyright (C) 1996, 1997, 1998, 1999 Paul Mackeras (paulus (at) linuxcare.com)
8 1.1 garbled * All rights reserved.
9 1.1 garbled *
10 1.1 garbled * Redistribution and use in source and binary forms, with or without
11 1.1 garbled * modification, are permitted provided that the following conditions
12 1.1 garbled * are met:
13 1.1 garbled * 1. Redistributions of source code must retain the above copyright
14 1.1 garbled * notice, this list of conditions and the following disclaimer.
15 1.1 garbled * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 garbled * notice, this list of conditions and the following disclaimer in the
17 1.1 garbled * documentation and/or other materials provided with the distribution.
18 1.1 garbled * 3. All advertising materials mentioning features or use of this software
19 1.1 garbled * must display the following acknowledgement:
20 1.1 garbled * This product includes software developed by Gary Thomas.
21 1.1 garbled * 4. The name of the author may not be used to endorse or promote products
22 1.1 garbled * derived from this software without specific prior written permission.
23 1.1 garbled *
24 1.1 garbled * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 1.1 garbled * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 1.1 garbled * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 1.1 garbled * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 1.1 garbled * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 1.1 garbled * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 1.1 garbled * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 1.1 garbled * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 1.1 garbled * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 1.1 garbled * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1 garbled */
35 1.1 garbled
36 1.1 garbled #if HAVE_NBTOOL_CONFIG_H
37 1.1 garbled #include "nbtool_config.h"
38 1.1 garbled #include "../../sys/sys/bootblock.h"
39 1.1 garbled #else
40 1.1 garbled #include <sys/bootblock.h>
41 1.1 garbled #endif
42 1.1 garbled
43 1.1 garbled #include <stdio.h>
44 1.1 garbled #include <stdlib.h>
45 1.1 garbled #include <string.h>
46 1.1 garbled #include <fcntl.h>
47 1.1 garbled #include <unistd.h>
48 1.1 garbled #include <errno.h>
49 1.1 garbled #include <zlib.h>
50 1.1 garbled #include <err.h>
51 1.1 garbled #include <sys/stat.h>
52 1.1 garbled #include <sys/types.h>
53 1.1 garbled #include <sys/uio.h>
54 1.1 garbled
55 1.1 garbled /* BFD ELF headers */
56 1.1 garbled #include <elf/common.h>
57 1.1 garbled #include <elf/external.h>
58 1.1 garbled
59 1.1 garbled #include "byteorder.h"
60 1.1 garbled #include "magic.h"
61 1.1 garbled
62 1.1 garbled /* Globals */
63 1.1 garbled
64 1.1 garbled int saloneflag = 0;
65 1.1 garbled Elf32_External_Ehdr hdr, khdr;
66 1.1 garbled struct stat elf_stat;
67 1.1 garbled unsigned char mbr[512];
68 1.1 garbled
69 1.1 garbled /*
70 1.1 garbled * Macros to get values from multi-byte ELF header fields. These assume
71 1.1 garbled * a big-endian image.
72 1.1 garbled */
73 1.1 garbled #define ELFGET16(x) (((x)[0] << 8) | (x)[1])
74 1.1 garbled
75 1.1 garbled #define ELFGET32(x) (((x)[0] << 24) | ((x)[1] << 16) | \
76 1.1 garbled ((x)[2] << 8) | (x)[3])
77 1.1 garbled
78 1.1 garbled static void usage(void);
79 1.1 garbled static int open_file(const char *, char *, Elf32_External_Ehdr *,
80 1.1 garbled struct stat *);
81 1.1 garbled static void check_mbr(int, int, char *);
82 1.1 garbled int main(int, char **);
83 1.1 garbled
84 1.1 garbled static void
85 1.1 garbled usage(void)
86 1.1 garbled {
87 1.1 garbled fprintf(stderr, "usage: %s [-ls] [-b bootfile] [-k kernel] [-r rawdev]"
88 1.1 garbled " bootimage\n", getprogname());
89 1.1 garbled exit(1);
90 1.1 garbled }
91 1.1 garbled
92 1.1 garbled /* verify the file is ELF and ppc, and open it up */
93 1.1 garbled static int
94 1.1 garbled open_file(const char *ftype, char *file, Elf32_External_Ehdr *hdr,
95 1.1 garbled struct stat *f_stat)
96 1.1 garbled {
97 1.1 garbled int fd;
98 1.1 garbled
99 1.1 garbled if ((fd = open(file, 0)) < 0)
100 1.1 garbled errx(2, "Can't open %s '%s': %s", ftype, file, strerror(errno));
101 1.1 garbled fstat(fd, f_stat);
102 1.1 garbled
103 1.1 garbled if (read(fd, hdr, sizeof(Elf32_External_Ehdr)) !=
104 1.1 garbled sizeof(Elf32_External_Ehdr))
105 1.1 garbled errx(3, "Can't read input '%s': %s", file, strerror(errno));
106 1.1 garbled
107 1.1 garbled if (hdr->e_ident[EI_MAG0] != ELFMAG0 ||
108 1.1 garbled hdr->e_ident[EI_MAG1] != ELFMAG1 ||
109 1.1 garbled hdr->e_ident[EI_MAG2] != ELFMAG2 ||
110 1.1 garbled hdr->e_ident[EI_MAG3] != ELFMAG3 ||
111 1.1 garbled hdr->e_ident[EI_CLASS] != ELFCLASS32)
112 1.1 garbled errx(3, "input '%s' is not ELF32 format", file);
113 1.1 garbled
114 1.1 garbled if (hdr->e_ident[EI_DATA] != ELFDATA2MSB)
115 1.1 garbled errx(3, "input '%s' is not big-endian", file);
116 1.1 garbled
117 1.1 garbled if (ELFGET16(hdr->e_machine) != EM_PPC)
118 1.1 garbled errx(3, "input '%s' is not PowerPC exec binary", file);
119 1.1 garbled
120 1.1 garbled return(fd);
121 1.1 garbled }
122 1.1 garbled
123 1.1 garbled static void
124 1.1 garbled check_mbr(int prep_fd, int lfloppyflag, char *rawdev)
125 1.1 garbled {
126 1.1 garbled int raw_fd;
127 1.1 garbled unsigned long entry, length;
128 1.1 garbled struct mbr_partition *mbrp;
129 1.1 garbled struct stat raw_stat;
130 1.1 garbled
131 1.1 garbled /* If we are building a standalone image, do not write an MBR, just
132 1.1 garbled * set entry point and boot image size skipping over elf header
133 1.1 garbled */
134 1.1 garbled if (saloneflag) {
135 1.1 garbled entry = sa_htole32(0x400);
136 1.1 garbled length = sa_htole32(elf_stat.st_size - sizeof(hdr) + 0x400);
137 1.1 garbled lseek(prep_fd, sizeof(mbr), SEEK_SET);
138 1.1 garbled write(prep_fd, &entry, sizeof(entry));
139 1.1 garbled write(prep_fd, &length, sizeof(length));
140 1.1 garbled return;
141 1.1 garbled }
142 1.1 garbled
143 1.1 garbled /*
144 1.1 garbled * if we have a raw device, we need to check to see if it already
145 1.1 garbled * has a partition table, and if so, read it in and check for
146 1.1 garbled * suitability.
147 1.1 garbled */
148 1.1 garbled if (rawdev != NULL) {
149 1.1 garbled raw_fd = open(rawdev, O_RDONLY, 0);
150 1.1 garbled if (raw_fd == -1)
151 1.1 garbled errx(3, "couldn't open raw device %s: %s", rawdev,
152 1.1 garbled strerror(errno));
153 1.1 garbled
154 1.1 garbled fstat(raw_fd, &raw_stat);
155 1.1 garbled if (!S_ISCHR(raw_stat.st_mode))
156 1.1 garbled errx(3, "%s is not a raw device", rawdev);
157 1.1 garbled
158 1.1 garbled if (read(raw_fd, mbr, 512) != 512)
159 1.1 garbled errx(3, "MBR Read Failed: %s", strerror(errno));
160 1.1 garbled
161 1.1 garbled mbrp = (struct mbr_partition *)&mbr[MBR_PART_OFFSET];
162 1.1 garbled if (mbrp->mbrp_type != MBR_PTYPE_PREP)
163 1.1 garbled errx(3, "First partition is not of type 0x%x.",
164 1.1 garbled MBR_PTYPE_PREP);
165 1.1 garbled if (mbrp->mbrp_start != 0)
166 1.1 garbled errx(3, "Use of the raw device is intended for"
167 1.1 garbled " upgrading of legacy installations. Your"
168 1.1 garbled " install does not have a PReP boot partition"
169 1.1 garbled " starting at sector 0. Use the -s option"
170 1.1 garbled " to build an image instead.");
171 1.1 garbled
172 1.1 garbled /* if we got this far, we are fine, write back the partition
173 1.1 garbled * and write the entry points and get outta here */
174 1.1 garbled /* Set entry point and boot image size skipping over elf header */
175 1.1 garbled lseek(prep_fd, 0, SEEK_SET);
176 1.1 garbled entry = sa_htole32(0x400);
177 1.1 garbled length = sa_htole32(elf_stat.st_size - sizeof(hdr) + 0x400);
178 1.1 garbled write(prep_fd, mbr, sizeof(mbr));
179 1.1 garbled write(prep_fd, &entry, sizeof(entry));
180 1.1 garbled write(prep_fd, &length, sizeof(length));
181 1.1 garbled close(raw_fd);
182 1.1 garbled return;
183 1.1 garbled }
184 1.1 garbled
185 1.1 garbled /* if we get to here, we want to build a standard floppy or netboot
186 1.1 garbled * image to file, so just build it */
187 1.1 garbled
188 1.1 garbled memset(mbr, 0, sizeof(mbr));
189 1.1 garbled mbrp = (struct mbr_partition *)&mbr[MBR_PART_OFFSET];
190 1.1 garbled
191 1.1 garbled /* Set entry point and boot image size skipping over elf header */
192 1.1 garbled entry = sa_htole32(0x400);
193 1.1 garbled length = sa_htole32(elf_stat.st_size - sizeof(hdr) + 0x400);
194 1.1 garbled
195 1.1 garbled /*
196 1.1 garbled * Set magic number for msdos partition
197 1.1 garbled */
198 1.1 garbled *(unsigned short *)&mbr[MBR_MAGIC_OFFSET] = sa_htole16(MBR_MAGIC);
199 1.1 garbled
200 1.1 garbled /*
201 1.1 garbled * Build a "PReP" partition table entry in the boot record
202 1.1 garbled * - "PReP" may only look at the system_indicator
203 1.1 garbled */
204 1.1 garbled mbrp->mbrp_flag = MBR_PFLAG_ACTIVE;
205 1.1 garbled mbrp->mbrp_type = MBR_PTYPE_PREP;
206 1.1 garbled
207 1.1 garbled /*
208 1.1 garbled * The first block of the diskette is used by this "boot record" which
209 1.1 garbled * actually contains the partition table. (The first block of the
210 1.1 garbled * partition contains the boot image, but I digress...) We'll set up
211 1.1 garbled * one partition on the diskette and it shall contain the rest of the
212 1.1 garbled * diskette.
213 1.1 garbled */
214 1.1 garbled mbrp->mbrp_shd = 0; /* zero-based */
215 1.1 garbled mbrp->mbrp_ssect = 2; /* one-based */
216 1.1 garbled mbrp->mbrp_scyl = 0; /* zero-based */
217 1.1 garbled mbrp->mbrp_ehd = 1; /* assumes two heads */
218 1.1 garbled if (lfloppyflag)
219 1.1 garbled mbrp->mbrp_esect = 36; /* 2.88MB floppy */
220 1.1 garbled else
221 1.1 garbled mbrp->mbrp_esect = 18; /* assumes 18 sectors/track */
222 1.1 garbled mbrp->mbrp_ecyl = 79; /* assumes 80 cylinders/diskette */
223 1.1 garbled
224 1.1 garbled /*
225 1.1 garbled * The "PReP" software ignores the above fields and just looks at
226 1.1 garbled * the next two.
227 1.1 garbled * - size of the diskette is (assumed to be)
228 1.1 garbled * (2 tracks/cylinder)(18 sectors/tracks)(80 cylinders/diskette)
229 1.1 garbled * - unlike the above sector numbers,
230 1.1 garbled * the beginning sector is zero-based!
231 1.1 garbled */
232 1.1 garbled
233 1.1 garbled /* This has to be 0 on the PowerStack? */
234 1.1 garbled mbrp->mbrp_start = sa_htole32(0);
235 1.1 garbled mbrp->mbrp_size = sa_htole32(2 * 18 * 80 - 1);
236 1.1 garbled
237 1.1 garbled write(prep_fd, mbr, sizeof(mbr));
238 1.1 garbled write(prep_fd, &entry, sizeof(entry));
239 1.1 garbled write(prep_fd, &length, sizeof(length));
240 1.1 garbled }
241 1.1 garbled
242 1.1 garbled int
243 1.1 garbled main(int argc, char **argv)
244 1.1 garbled {
245 1.1 garbled unsigned char *elf_img = NULL, *kern_img = NULL;
246 1.1 garbled int i, ch, tmp, kgzlen, err, lfloppyflag=0;
247 1.1 garbled int elf_fd, prep_fd, kern_fd, elf_img_len = 0;
248 1.1 garbled off_t lenpos, kstart, kend;
249 1.1 garbled unsigned long length;
250 1.1 garbled long flength;
251 1.1 garbled gzFile gzf;
252 1.1 garbled struct stat kern_stat;
253 1.1 garbled char *kernel = NULL, *boot = NULL, *rawdev = NULL;
254 1.1 garbled Elf32_External_Phdr phdr;
255 1.1 garbled
256 1.1 garbled setprogname(argv[0]);
257 1.1 garbled kern_len = 0;
258 1.1 garbled
259 1.1 garbled while ((ch = getopt(argc, argv, "b:k:lr:s")) != -1)
260 1.1 garbled switch (ch) {
261 1.1 garbled case 'b':
262 1.1 garbled boot = optarg;
263 1.1 garbled break;
264 1.1 garbled case 'k':
265 1.1 garbled kernel = optarg;
266 1.1 garbled break;
267 1.1 garbled case 'l':
268 1.1 garbled lfloppyflag = 1;
269 1.1 garbled break;
270 1.1 garbled case 'r':
271 1.1 garbled rawdev = optarg;
272 1.1 garbled break;
273 1.1 garbled case 's':
274 1.1 garbled saloneflag = 1;
275 1.1 garbled break;
276 1.1 garbled case '?':
277 1.1 garbled default:
278 1.1 garbled usage();
279 1.1 garbled /* NOTREACHED */
280 1.1 garbled }
281 1.1 garbled argc -= optind;
282 1.1 garbled argv += optind;
283 1.1 garbled
284 1.1 garbled if (argc < 1)
285 1.1 garbled usage();
286 1.1 garbled
287 1.1 garbled if (kernel == NULL)
288 1.1 garbled kernel = "/netbsd";
289 1.1 garbled
290 1.1 garbled if (boot == NULL)
291 1.1 garbled boot = "/usr/mdec/boot";
292 1.1 garbled
293 1.1 garbled elf_fd = open_file("bootloader", boot, &hdr, &elf_stat);
294 1.1 garbled kern_fd = open_file("kernel", kernel, &khdr, &kern_stat);
295 1.1 garbled kern_len = kern_stat.st_size + MAGICSIZE + KERNLENSIZE;
296 1.1 garbled
297 1.1 garbled for (i = 0; i < ELFGET16(hdr.e_phnum); i++) {
298 1.1 garbled lseek(elf_fd, ELFGET32(hdr.e_phoff) + sizeof(phdr) * i,
299 1.1 garbled SEEK_SET);
300 1.1 garbled if (read(elf_fd, &phdr, sizeof(phdr)) != sizeof(phdr))
301 1.1 garbled errx(3, "Can't read input '%s' phdr : %s", boot,
302 1.1 garbled strerror(errno));
303 1.1 garbled
304 1.1 garbled if ((ELFGET32(phdr.p_type) != PT_LOAD) ||
305 1.1 garbled !(ELFGET32(phdr.p_flags) & PF_X))
306 1.1 garbled continue;
307 1.1 garbled
308 1.1 garbled fstat(elf_fd, &elf_stat);
309 1.1 garbled elf_img_len = elf_stat.st_size - ELFGET32(phdr.p_offset);
310 1.1 garbled lseek(elf_fd, ELFGET32(phdr.p_offset), SEEK_SET);
311 1.1 garbled
312 1.1 garbled break;
313 1.1 garbled }
314 1.1 garbled if ((prep_fd = open(argv[0], O_RDWR|O_TRUNC, 0)) < 0) {
315 1.1 garbled /* we couldn't open it, it must be new */
316 1.1 garbled prep_fd = creat(argv[0], 0644);
317 1.1 garbled if (prep_fd < 0)
318 1.1 garbled errx(2, "Can't open output '%s': %s", argv[0],
319 1.1 garbled strerror(errno));
320 1.1 garbled }
321 1.1 garbled
322 1.1 garbled check_mbr(prep_fd, lfloppyflag, rawdev);
323 1.1 garbled
324 1.1 garbled /* Set file pos. to 2nd sector where image will be written */
325 1.1 garbled lseek(prep_fd, 0x400, SEEK_SET);
326 1.1 garbled
327 1.1 garbled /* Copy boot image */
328 1.1 garbled elf_img = (unsigned char *)malloc(elf_img_len);
329 1.1 garbled if (!elf_img)
330 1.1 garbled errx(3, "Can't malloc: %s", strerror(errno));
331 1.1 garbled if (read(elf_fd, elf_img, elf_img_len) != elf_img_len)
332 1.1 garbled errx(3, "Can't read file '%s' : %s", boot, strerror(errno));
333 1.1 garbled
334 1.1 garbled write(prep_fd, elf_img, elf_img_len);
335 1.1 garbled free(elf_img);
336 1.1 garbled
337 1.1 garbled /* Copy kernel */
338 1.1 garbled kern_img = (unsigned char *)malloc(kern_stat.st_size);
339 1.1 garbled
340 1.1 garbled if (kern_img == NULL)
341 1.1 garbled errx(3, "Can't malloc: %s", strerror(errno));
342 1.1 garbled
343 1.1 garbled /* we need to jump back after having read the headers */
344 1.1 garbled lseek(kern_fd, 0, SEEK_SET);
345 1.1 garbled if (read(kern_fd, (void *)kern_img, kern_stat.st_size) !=
346 1.1 garbled kern_stat.st_size)
347 1.1 garbled errx(3, "Can't read kernel '%s' : %s", kernel, strerror(errno));
348 1.1 garbled
349 1.1 garbled gzf = gzdopen(dup(prep_fd), "a");
350 1.1 garbled if (gzf == NULL)
351 1.1 garbled errx(3, "Can't init compression: %s", strerror(errno));
352 1.1 garbled if (gzsetparams(gzf, Z_BEST_COMPRESSION, Z_DEFAULT_STRATEGY) != Z_OK)
353 1.1 garbled errx(3, "%s", gzerror(gzf, &err));
354 1.1 garbled
355 1.1 garbled /* write a magic number and size before the kernel */
356 1.1 garbled write(prep_fd, (void *)magic, MAGICSIZE);
357 1.1 garbled lenpos = lseek(prep_fd, 0, SEEK_CUR);
358 1.1 garbled tmp = sa_htobe32(0);
359 1.1 garbled write(prep_fd, (void *)&tmp, KERNLENSIZE);
360 1.1 garbled
361 1.1 garbled /* write in the compressed kernel */
362 1.1 garbled kstart = lseek(prep_fd, 0, SEEK_CUR);
363 1.1 garbled kgzlen = gzwrite(gzf, kern_img, kern_stat.st_size);
364 1.1 garbled gzclose(gzf);
365 1.1 garbled kend = lseek(prep_fd, 0, SEEK_CUR);
366 1.1 garbled
367 1.1 garbled /* jump back to the length position now that we know the length */
368 1.1 garbled lseek(prep_fd, lenpos, SEEK_SET);
369 1.1 garbled kgzlen = kend - kstart;
370 1.1 garbled tmp = sa_htobe32(kgzlen);
371 1.1 garbled write(prep_fd, (void *)&tmp, KERNLENSIZE);
372 1.1 garbled
373 1.1 garbled length = sa_htole32(0x400 + elf_img_len + 8 + kgzlen);
374 1.1 garbled lseek(prep_fd, sizeof(mbr) + 4, SEEK_SET);
375 1.1 garbled write(prep_fd, &length, sizeof(length));
376 1.1 garbled
377 1.1 garbled flength = 0x400 + elf_img_len + 8 + kgzlen;
378 1.1 garbled if (lfloppyflag)
379 1.1 garbled flength -= (5760 * 512);
380 1.1 garbled else
381 1.1 garbled flength -= (2880 * 512);
382 1.1 garbled if (flength > 0 && !saloneflag)
383 1.1 garbled fprintf(stderr, "%s: Image %s is %d bytes larger than single"
384 1.1 garbled " floppy. Can only be used for netboot.\n", getprogname(),
385 1.1 garbled argv[0], flength);
386 1.1 garbled
387 1.1 garbled free(kern_img);
388 1.1 garbled close(kern_fd);
389 1.1 garbled close(prep_fd);
390 1.1 garbled close(elf_fd);
391 1.1 garbled
392 1.1 garbled return 0;
393 1.1 garbled }
394