mkbootimage.c revision 1.13 1 1.13 kiyohara /* $NetBSD: mkbootimage.c,v 1.13 2010/10/16 05:14:14 kiyohara Exp $ */
2 1.1 garbled
3 1.1 garbled /*-
4 1.2 garbled * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.1 garbled * All rights reserved.
6 1.1 garbled *
7 1.2 garbled * This code is derived from software contributed to The NetBSD Foundation
8 1.2 garbled * by Tim Rightnour and NONAKA Kimihiro
9 1.2 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 *
19 1.2 garbled * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 garbled * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 garbled * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 garbled * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 garbled * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 garbled * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 garbled * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 garbled * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 garbled * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 garbled * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 garbled * POSSIBILITY OF SUCH DAMAGE.
30 1.1 garbled */
31 1.1 garbled
32 1.1 garbled #if HAVE_NBTOOL_CONFIG_H
33 1.1 garbled #include "nbtool_config.h"
34 1.1 garbled #include "../../sys/sys/bootblock.h"
35 1.1 garbled #else
36 1.1 garbled #include <sys/bootblock.h>
37 1.1 garbled #endif
38 1.1 garbled
39 1.1 garbled #include <stdio.h>
40 1.1 garbled #include <stdlib.h>
41 1.1 garbled #include <string.h>
42 1.1 garbled #include <fcntl.h>
43 1.1 garbled #include <unistd.h>
44 1.1 garbled #include <errno.h>
45 1.1 garbled #include <zlib.h>
46 1.1 garbled #include <err.h>
47 1.1 garbled #include <sys/stat.h>
48 1.1 garbled #include <sys/types.h>
49 1.1 garbled #include <sys/uio.h>
50 1.9 garbled #include <sys/signal.h>
51 1.1 garbled
52 1.9 garbled #undef USE_SYSCTL
53 1.9 garbled
54 1.9 garbled #if defined(__NetBSD__) && !defined(HAVE_NBTOOL_CONFIG_H)
55 1.9 garbled #define USE_SYSCTL 1
56 1.9 garbled #include <sys/param.h>
57 1.2 garbled #include <sys/sysctl.h>
58 1.2 garbled #include <sys/utsname.h>
59 1.2 garbled #endif
60 1.2 garbled
61 1.1 garbled /* BFD ELF headers */
62 1.1 garbled #include <elf/common.h>
63 1.1 garbled #include <elf/external.h>
64 1.1 garbled
65 1.6 garbled #include "bebox_bootrec.h"
66 1.1 garbled #include "byteorder.h"
67 1.3 garbled #include "magic.h"
68 1.6 garbled #include "pef.h"
69 1.6 garbled #include "rs6000_bootrec.h"
70 1.1 garbled
71 1.1 garbled /* Globals */
72 1.1 garbled
73 1.13 kiyohara int inkernflag = 1;
74 1.1 garbled int saloneflag = 0;
75 1.4 garbled int verboseflag = 0;
76 1.4 garbled int lfloppyflag = 0;
77 1.1 garbled Elf32_External_Ehdr hdr, khdr;
78 1.1 garbled struct stat elf_stat;
79 1.1 garbled unsigned char mbr[512];
80 1.4 garbled
81 1.4 garbled /* the boot and config records for rs6000 */
82 1.4 garbled rs6000_boot_record_t bootrec;
83 1.4 garbled rs6000_config_record_t confrec;
84 1.4 garbled
85 1.4 garbled /* supported platforms */
86 1.2 garbled char *sup_plats[] = {
87 1.6 garbled "bebox",
88 1.2 garbled "prep",
89 1.4 garbled "rs6000",
90 1.2 garbled NULL,
91 1.2 garbled };
92 1.1 garbled
93 1.1 garbled /*
94 1.1 garbled * Macros to get values from multi-byte ELF header fields. These assume
95 1.1 garbled * a big-endian image.
96 1.1 garbled */
97 1.1 garbled #define ELFGET16(x) (((x)[0] << 8) | (x)[1])
98 1.1 garbled
99 1.1 garbled #define ELFGET32(x) (((x)[0] << 24) | ((x)[1] << 16) | \
100 1.1 garbled ((x)[2] << 8) | (x)[3])
101 1.1 garbled
102 1.6 garbled #define ULALIGN(x) ((x + 0x0f) & 0xfffffff0)
103 1.6 garbled
104 1.4 garbled static void usage(int);
105 1.1 garbled static int open_file(const char *, char *, Elf32_External_Ehdr *,
106 1.1 garbled struct stat *);
107 1.4 garbled static void check_mbr(int, char *);
108 1.4 garbled static int prep_build_image(char *, char *, char *, char *);
109 1.4 garbled static void rs6000_build_records(int);
110 1.4 garbled static int rs6000_build_image(char *, char *, char *, char *);
111 1.1 garbled int main(int, char **);
112 1.1 garbled
113 1.6 garbled
114 1.1 garbled static void
115 1.4 garbled usage(int extended)
116 1.1 garbled {
117 1.4 garbled int i;
118 1.4 garbled
119 1.4 garbled if (extended) {
120 1.4 garbled fprintf(stderr, "You are not running this program on"
121 1.4 garbled " the target machine. You must supply the\n"
122 1.4 garbled "machine architecture with the -m flag\n");
123 1.4 garbled fprintf(stderr, "Supported architectures: ");
124 1.4 garbled for (i=0; sup_plats[i] != NULL; i++)
125 1.4 garbled fprintf(stderr, " %s", sup_plats[i]);
126 1.4 garbled fprintf(stderr, "\n\n");
127 1.4 garbled }
128 1.9 garbled #ifdef USE_SYSCTL
129 1.13 kiyohara fprintf(stderr, "usage: %s [-Ilsv] [-m machine] [-b bootfile] "
130 1.2 garbled "[-k kernel] [-r rawdev] bootimage\n", getprogname());
131 1.2 garbled #else
132 1.13 kiyohara fprintf(stderr, "usage: %s [-Ilsv] -m machine [-b bootfile] "
133 1.2 garbled "[-k kernel] [-r rawdev] bootimage\n", getprogname());
134 1.2 garbled #endif
135 1.1 garbled exit(1);
136 1.1 garbled }
137 1.1 garbled
138 1.1 garbled /* verify the file is ELF and ppc, and open it up */
139 1.1 garbled static int
140 1.1 garbled open_file(const char *ftype, char *file, Elf32_External_Ehdr *hdr,
141 1.1 garbled struct stat *f_stat)
142 1.1 garbled {
143 1.1 garbled int fd;
144 1.1 garbled
145 1.1 garbled if ((fd = open(file, 0)) < 0)
146 1.1 garbled errx(2, "Can't open %s '%s': %s", ftype, file, strerror(errno));
147 1.1 garbled fstat(fd, f_stat);
148 1.1 garbled
149 1.1 garbled if (read(fd, hdr, sizeof(Elf32_External_Ehdr)) !=
150 1.1 garbled sizeof(Elf32_External_Ehdr))
151 1.1 garbled errx(3, "Can't read input '%s': %s", file, strerror(errno));
152 1.1 garbled
153 1.1 garbled if (hdr->e_ident[EI_MAG0] != ELFMAG0 ||
154 1.1 garbled hdr->e_ident[EI_MAG1] != ELFMAG1 ||
155 1.1 garbled hdr->e_ident[EI_MAG2] != ELFMAG2 ||
156 1.1 garbled hdr->e_ident[EI_MAG3] != ELFMAG3 ||
157 1.1 garbled hdr->e_ident[EI_CLASS] != ELFCLASS32)
158 1.1 garbled errx(3, "input '%s' is not ELF32 format", file);
159 1.1 garbled
160 1.1 garbled if (hdr->e_ident[EI_DATA] != ELFDATA2MSB)
161 1.1 garbled errx(3, "input '%s' is not big-endian", file);
162 1.1 garbled
163 1.1 garbled if (ELFGET16(hdr->e_machine) != EM_PPC)
164 1.1 garbled errx(3, "input '%s' is not PowerPC exec binary", file);
165 1.1 garbled
166 1.1 garbled return(fd);
167 1.1 garbled }
168 1.1 garbled
169 1.1 garbled static void
170 1.4 garbled prep_check_mbr(int prep_fd, char *rawdev)
171 1.1 garbled {
172 1.1 garbled int raw_fd;
173 1.1 garbled unsigned long entry, length;
174 1.1 garbled struct mbr_partition *mbrp;
175 1.1 garbled struct stat raw_stat;
176 1.1 garbled
177 1.1 garbled /* If we are building a standalone image, do not write an MBR, just
178 1.1 garbled * set entry point and boot image size skipping over elf header
179 1.1 garbled */
180 1.1 garbled if (saloneflag) {
181 1.1 garbled entry = sa_htole32(0x400);
182 1.1 garbled length = sa_htole32(elf_stat.st_size - sizeof(hdr) + 0x400);
183 1.1 garbled lseek(prep_fd, sizeof(mbr), SEEK_SET);
184 1.1 garbled write(prep_fd, &entry, sizeof(entry));
185 1.1 garbled write(prep_fd, &length, sizeof(length));
186 1.1 garbled return;
187 1.1 garbled }
188 1.1 garbled
189 1.1 garbled /*
190 1.1 garbled * if we have a raw device, we need to check to see if it already
191 1.1 garbled * has a partition table, and if so, read it in and check for
192 1.1 garbled * suitability.
193 1.1 garbled */
194 1.1 garbled if (rawdev != NULL) {
195 1.1 garbled raw_fd = open(rawdev, O_RDONLY, 0);
196 1.1 garbled if (raw_fd == -1)
197 1.1 garbled errx(3, "couldn't open raw device %s: %s", rawdev,
198 1.1 garbled strerror(errno));
199 1.1 garbled
200 1.1 garbled fstat(raw_fd, &raw_stat);
201 1.1 garbled if (!S_ISCHR(raw_stat.st_mode))
202 1.1 garbled errx(3, "%s is not a raw device", rawdev);
203 1.1 garbled
204 1.1 garbled if (read(raw_fd, mbr, 512) != 512)
205 1.1 garbled errx(3, "MBR Read Failed: %s", strerror(errno));
206 1.1 garbled
207 1.1 garbled mbrp = (struct mbr_partition *)&mbr[MBR_PART_OFFSET];
208 1.1 garbled if (mbrp->mbrp_type != MBR_PTYPE_PREP)
209 1.1 garbled errx(3, "First partition is not of type 0x%x.",
210 1.1 garbled MBR_PTYPE_PREP);
211 1.1 garbled if (mbrp->mbrp_start != 0)
212 1.1 garbled errx(3, "Use of the raw device is intended for"
213 1.1 garbled " upgrading of legacy installations. Your"
214 1.1 garbled " install does not have a PReP boot partition"
215 1.1 garbled " starting at sector 0. Use the -s option"
216 1.1 garbled " to build an image instead.");
217 1.1 garbled
218 1.1 garbled /* if we got this far, we are fine, write back the partition
219 1.1 garbled * and write the entry points and get outta here */
220 1.1 garbled /* Set entry point and boot image size skipping over elf header */
221 1.1 garbled lseek(prep_fd, 0, SEEK_SET);
222 1.1 garbled entry = sa_htole32(0x400);
223 1.1 garbled length = sa_htole32(elf_stat.st_size - sizeof(hdr) + 0x400);
224 1.1 garbled write(prep_fd, mbr, sizeof(mbr));
225 1.1 garbled write(prep_fd, &entry, sizeof(entry));
226 1.1 garbled write(prep_fd, &length, sizeof(length));
227 1.1 garbled close(raw_fd);
228 1.1 garbled return;
229 1.1 garbled }
230 1.1 garbled
231 1.1 garbled /* if we get to here, we want to build a standard floppy or netboot
232 1.1 garbled * image to file, so just build it */
233 1.1 garbled
234 1.1 garbled memset(mbr, 0, sizeof(mbr));
235 1.1 garbled mbrp = (struct mbr_partition *)&mbr[MBR_PART_OFFSET];
236 1.12 kiyohara
237 1.1 garbled /* Set entry point and boot image size skipping over elf header */
238 1.1 garbled entry = sa_htole32(0x400);
239 1.1 garbled length = sa_htole32(elf_stat.st_size - sizeof(hdr) + 0x400);
240 1.1 garbled
241 1.1 garbled /*
242 1.1 garbled * Set magic number for msdos partition
243 1.1 garbled */
244 1.1 garbled *(unsigned short *)&mbr[MBR_MAGIC_OFFSET] = sa_htole16(MBR_MAGIC);
245 1.12 kiyohara
246 1.1 garbled /*
247 1.1 garbled * Build a "PReP" partition table entry in the boot record
248 1.1 garbled * - "PReP" may only look at the system_indicator
249 1.1 garbled */
250 1.1 garbled mbrp->mbrp_flag = MBR_PFLAG_ACTIVE;
251 1.1 garbled mbrp->mbrp_type = MBR_PTYPE_PREP;
252 1.1 garbled
253 1.1 garbled /*
254 1.1 garbled * The first block of the diskette is used by this "boot record" which
255 1.1 garbled * actually contains the partition table. (The first block of the
256 1.1 garbled * partition contains the boot image, but I digress...) We'll set up
257 1.1 garbled * one partition on the diskette and it shall contain the rest of the
258 1.1 garbled * diskette.
259 1.1 garbled */
260 1.12 kiyohara mbrp->mbrp_shd = 0; /* zero-based */
261 1.12 kiyohara mbrp->mbrp_ssect = 2; /* one-based */
262 1.12 kiyohara mbrp->mbrp_scyl = 0; /* zero-based */
263 1.12 kiyohara mbrp->mbrp_ehd = 1; /* assumes two heads */
264 1.1 garbled if (lfloppyflag)
265 1.12 kiyohara mbrp->mbrp_esect = 36; /* 2.88MB floppy */
266 1.1 garbled else
267 1.12 kiyohara mbrp->mbrp_esect = 18; /* assumes 18 sectors/track */
268 1.12 kiyohara mbrp->mbrp_ecyl = 79; /* assumes 80 cylinders/diskette */
269 1.1 garbled
270 1.1 garbled /*
271 1.1 garbled * The "PReP" software ignores the above fields and just looks at
272 1.1 garbled * the next two.
273 1.1 garbled * - size of the diskette is (assumed to be)
274 1.1 garbled * (2 tracks/cylinder)(18 sectors/tracks)(80 cylinders/diskette)
275 1.1 garbled * - unlike the above sector numbers,
276 1.1 garbled * the beginning sector is zero-based!
277 1.1 garbled */
278 1.1 garbled
279 1.12 kiyohara /* This has to be 0 on the PowerStack? */
280 1.1 garbled mbrp->mbrp_start = sa_htole32(0);
281 1.1 garbled mbrp->mbrp_size = sa_htole32(2 * 18 * 80 - 1);
282 1.1 garbled
283 1.1 garbled write(prep_fd, mbr, sizeof(mbr));
284 1.1 garbled write(prep_fd, &entry, sizeof(entry));
285 1.12 kiyohara write(prep_fd, &length, sizeof(length));
286 1.1 garbled }
287 1.1 garbled
288 1.2 garbled static int
289 1.4 garbled prep_build_image(char *kernel, char *boot, char *rawdev, char *outname)
290 1.1 garbled {
291 1.1 garbled unsigned char *elf_img = NULL, *kern_img = NULL;
292 1.2 garbled int i, ch, tmp, kgzlen, err;
293 1.1 garbled int elf_fd, prep_fd, kern_fd, elf_img_len = 0;
294 1.1 garbled off_t lenpos, kstart, kend;
295 1.1 garbled unsigned long length;
296 1.1 garbled long flength;
297 1.1 garbled gzFile gzf;
298 1.1 garbled struct stat kern_stat;
299 1.1 garbled Elf32_External_Phdr phdr;
300 1.1 garbled
301 1.1 garbled elf_fd = open_file("bootloader", boot, &hdr, &elf_stat);
302 1.1 garbled kern_fd = open_file("kernel", kernel, &khdr, &kern_stat);
303 1.2 garbled kern_len = kern_stat.st_size + PREP_MAGICSIZE + KERNLENSIZE;
304 1.1 garbled
305 1.1 garbled for (i = 0; i < ELFGET16(hdr.e_phnum); i++) {
306 1.1 garbled lseek(elf_fd, ELFGET32(hdr.e_phoff) + sizeof(phdr) * i,
307 1.1 garbled SEEK_SET);
308 1.1 garbled if (read(elf_fd, &phdr, sizeof(phdr)) != sizeof(phdr))
309 1.1 garbled errx(3, "Can't read input '%s' phdr : %s", boot,
310 1.1 garbled strerror(errno));
311 1.1 garbled
312 1.1 garbled if ((ELFGET32(phdr.p_type) != PT_LOAD) ||
313 1.1 garbled !(ELFGET32(phdr.p_flags) & PF_X))
314 1.1 garbled continue;
315 1.1 garbled
316 1.1 garbled fstat(elf_fd, &elf_stat);
317 1.1 garbled elf_img_len = elf_stat.st_size - ELFGET32(phdr.p_offset);
318 1.1 garbled lseek(elf_fd, ELFGET32(phdr.p_offset), SEEK_SET);
319 1.1 garbled
320 1.1 garbled break;
321 1.1 garbled }
322 1.2 garbled if ((prep_fd = open(outname, O_RDWR|O_TRUNC, 0)) < 0) {
323 1.1 garbled /* we couldn't open it, it must be new */
324 1.2 garbled prep_fd = creat(outname, 0644);
325 1.1 garbled if (prep_fd < 0)
326 1.2 garbled errx(2, "Can't open output '%s': %s", outname,
327 1.1 garbled strerror(errno));
328 1.1 garbled }
329 1.1 garbled
330 1.4 garbled prep_check_mbr(prep_fd, rawdev);
331 1.1 garbled
332 1.1 garbled /* Set file pos. to 2nd sector where image will be written */
333 1.1 garbled lseek(prep_fd, 0x400, SEEK_SET);
334 1.1 garbled
335 1.1 garbled /* Copy boot image */
336 1.1 garbled elf_img = (unsigned char *)malloc(elf_img_len);
337 1.1 garbled if (!elf_img)
338 1.1 garbled errx(3, "Can't malloc: %s", strerror(errno));
339 1.1 garbled if (read(elf_fd, elf_img, elf_img_len) != elf_img_len)
340 1.1 garbled errx(3, "Can't read file '%s' : %s", boot, strerror(errno));
341 1.1 garbled
342 1.1 garbled write(prep_fd, elf_img, elf_img_len);
343 1.1 garbled free(elf_img);
344 1.1 garbled
345 1.1 garbled /* Copy kernel */
346 1.1 garbled kern_img = (unsigned char *)malloc(kern_stat.st_size);
347 1.1 garbled
348 1.1 garbled if (kern_img == NULL)
349 1.1 garbled errx(3, "Can't malloc: %s", strerror(errno));
350 1.1 garbled
351 1.1 garbled /* we need to jump back after having read the headers */
352 1.1 garbled lseek(kern_fd, 0, SEEK_SET);
353 1.1 garbled if (read(kern_fd, (void *)kern_img, kern_stat.st_size) !=
354 1.1 garbled kern_stat.st_size)
355 1.1 garbled errx(3, "Can't read kernel '%s' : %s", kernel, strerror(errno));
356 1.1 garbled
357 1.1 garbled gzf = gzdopen(dup(prep_fd), "a");
358 1.1 garbled if (gzf == NULL)
359 1.1 garbled errx(3, "Can't init compression: %s", strerror(errno));
360 1.1 garbled if (gzsetparams(gzf, Z_BEST_COMPRESSION, Z_DEFAULT_STRATEGY) != Z_OK)
361 1.1 garbled errx(3, "%s", gzerror(gzf, &err));
362 1.1 garbled
363 1.1 garbled /* write a magic number and size before the kernel */
364 1.2 garbled write(prep_fd, (void *)prep_magic, PREP_MAGICSIZE);
365 1.1 garbled lenpos = lseek(prep_fd, 0, SEEK_CUR);
366 1.1 garbled tmp = sa_htobe32(0);
367 1.1 garbled write(prep_fd, (void *)&tmp, KERNLENSIZE);
368 1.1 garbled
369 1.1 garbled /* write in the compressed kernel */
370 1.1 garbled kstart = lseek(prep_fd, 0, SEEK_CUR);
371 1.1 garbled kgzlen = gzwrite(gzf, kern_img, kern_stat.st_size);
372 1.1 garbled gzclose(gzf);
373 1.1 garbled kend = lseek(prep_fd, 0, SEEK_CUR);
374 1.1 garbled
375 1.1 garbled /* jump back to the length position now that we know the length */
376 1.1 garbled lseek(prep_fd, lenpos, SEEK_SET);
377 1.1 garbled kgzlen = kend - kstart;
378 1.1 garbled tmp = sa_htobe32(kgzlen);
379 1.1 garbled write(prep_fd, (void *)&tmp, KERNLENSIZE);
380 1.1 garbled
381 1.1 garbled length = sa_htole32(0x400 + elf_img_len + 8 + kgzlen);
382 1.1 garbled lseek(prep_fd, sizeof(mbr) + 4, SEEK_SET);
383 1.1 garbled write(prep_fd, &length, sizeof(length));
384 1.1 garbled
385 1.1 garbled flength = 0x400 + elf_img_len + 8 + kgzlen;
386 1.4 garbled if (lfloppyflag)
387 1.1 garbled flength -= (5760 * 512);
388 1.1 garbled else
389 1.1 garbled flength -= (2880 * 512);
390 1.1 garbled if (flength > 0 && !saloneflag)
391 1.1 garbled fprintf(stderr, "%s: Image %s is %d bytes larger than single"
392 1.1 garbled " floppy. Can only be used for netboot.\n", getprogname(),
393 1.2 garbled outname, flength);
394 1.1 garbled
395 1.1 garbled free(kern_img);
396 1.1 garbled close(kern_fd);
397 1.1 garbled close(prep_fd);
398 1.1 garbled close(elf_fd);
399 1.1 garbled
400 1.4 garbled return 0;
401 1.12 kiyohara }
402 1.2 garbled
403 1.4 garbled /* Fill in the needed information on the boot and config records. Most of
404 1.4 garbled * this is just AIX garbage that we don't really need to boot.
405 1.4 garbled */
406 1.4 garbled static void
407 1.4 garbled rs6000_build_records(int img_len)
408 1.4 garbled {
409 1.4 garbled int bcl;
410 1.4 garbled
411 1.4 garbled /* zero out all the fields, so we only have to set the ones
412 1.4 garbled * we care about, which are rather few.
413 1.4 garbled */
414 1.4 garbled memset(&bootrec, 0, sizeof(rs6000_boot_record_t));
415 1.4 garbled memset(&confrec, 0, sizeof(rs6000_config_record_t));
416 1.4 garbled
417 1.4 garbled bootrec.ipl_record = IPLRECID;
418 1.4 garbled bcl = img_len/512;
419 1.4 garbled if (img_len%512 != 0)
420 1.4 garbled bcl++;
421 1.4 garbled bootrec.bootcode_len = bcl;
422 1.4 garbled bootrec.bootcode_off = 0; /* XXX */
423 1.4 garbled bootrec.bootpart_start = 2; /* skip bootrec and confrec */
424 1.4 garbled bootrec.bootprg_start = 2;
425 1.4 garbled bootrec.bootpart_len = bcl;
426 1.4 garbled bootrec.boot_load_addr = 0x800000; /* XXX? */
427 1.4 garbled bootrec.boot_frag = 1;
428 1.4 garbled bootrec.boot_emul = 0x02; /* ?? */
429 1.4 garbled /* service mode is a repeat of normal mode */
430 1.4 garbled bootrec.servcode_len = bootrec.bootcode_len;
431 1.4 garbled bootrec.servcode_off = bootrec.bootcode_off;
432 1.4 garbled bootrec.servpart_start = bootrec.bootpart_start;
433 1.4 garbled bootrec.servprg_start = bootrec.bootprg_start;
434 1.4 garbled bootrec.servpart_len = bootrec.bootpart_len;
435 1.4 garbled bootrec.serv_load_addr = bootrec.boot_load_addr;
436 1.4 garbled bootrec.serv_frag = bootrec.boot_frag;
437 1.4 garbled bootrec.serv_emul = bootrec.boot_emul;
438 1.4 garbled
439 1.4 garbled /* now the config record */
440 1.4 garbled confrec.conf_rec = CONFRECID;
441 1.4 garbled confrec.sector_size = 0x02; /* 512 bytes */
442 1.4 garbled confrec.last_cyl = 0x4f; /* 79 cyl, emulates floppy */
443 1.4 garbled }
444 1.4 garbled
445 1.4 garbled static int
446 1.4 garbled rs6000_build_image(char *kernel, char *boot, char *rawdev, char *outname)
447 1.4 garbled {
448 1.4 garbled unsigned char *elf_img = NULL, *kern_img = NULL;
449 1.4 garbled int i, ch, tmp, kgzlen, err;
450 1.4 garbled int elf_fd, rs6000_fd, kern_fd, elf_img_len = 0, elf_pad;
451 1.4 garbled uint32_t swapped[128];
452 1.4 garbled off_t lenpos, kstart, kend;
453 1.4 garbled unsigned long length;
454 1.4 garbled long flength;
455 1.4 garbled gzFile gzf;
456 1.4 garbled struct stat kern_stat;
457 1.4 garbled Elf32_External_Phdr phdr;
458 1.4 garbled
459 1.4 garbled elf_fd = open_file("bootloader", boot, &hdr, &elf_stat);
460 1.4 garbled kern_fd = open_file("kernel", kernel, &khdr, &kern_stat);
461 1.4 garbled kern_len = kern_stat.st_size + RS6000_MAGICSIZE + KERNLENSIZE;
462 1.4 garbled
463 1.4 garbled for (i = 0; i < ELFGET16(hdr.e_phnum); i++) {
464 1.4 garbled lseek(elf_fd, ELFGET32(hdr.e_phoff) + sizeof(phdr) * i,
465 1.4 garbled SEEK_SET);
466 1.4 garbled if (read(elf_fd, &phdr, sizeof(phdr)) != sizeof(phdr))
467 1.4 garbled errx(3, "Can't read input '%s' phdr : %s", boot,
468 1.4 garbled strerror(errno));
469 1.4 garbled
470 1.4 garbled if ((ELFGET32(phdr.p_type) != PT_LOAD) ||
471 1.4 garbled !(ELFGET32(phdr.p_flags) & PF_X))
472 1.4 garbled continue;
473 1.4 garbled
474 1.4 garbled fstat(elf_fd, &elf_stat);
475 1.4 garbled elf_img_len = elf_stat.st_size - ELFGET32(phdr.p_offset);
476 1.4 garbled elf_pad = ELFGET32(phdr.p_memsz) - ELFGET32(phdr.p_filesz);
477 1.4 garbled if (verboseflag)
478 1.4 garbled printf("Padding %d\n", elf_pad);
479 1.4 garbled lseek(elf_fd, ELFGET32(phdr.p_offset), SEEK_SET);
480 1.4 garbled
481 1.4 garbled break;
482 1.4 garbled }
483 1.4 garbled if ((rs6000_fd = open(outname, O_RDWR|O_TRUNC, 0)) < 0) {
484 1.4 garbled /* we couldn't open it, it must be new */
485 1.4 garbled rs6000_fd = creat(outname, 0644);
486 1.4 garbled if (rs6000_fd < 0)
487 1.4 garbled errx(2, "Can't open output '%s': %s", outname,
488 1.4 garbled strerror(errno));
489 1.4 garbled }
490 1.4 garbled
491 1.4 garbled /* Set file pos. to 2nd sector where image will be written */
492 1.4 garbled lseek(rs6000_fd, 0x400, SEEK_SET);
493 1.4 garbled
494 1.4 garbled /* Copy boot image */
495 1.4 garbled elf_img = (unsigned char *)malloc(elf_img_len);
496 1.4 garbled if (!elf_img)
497 1.4 garbled errx(3, "Can't malloc: %s", strerror(errno));
498 1.4 garbled if (read(elf_fd, elf_img, elf_img_len) != elf_img_len)
499 1.4 garbled errx(3, "Can't read file '%s' : %s", boot, strerror(errno));
500 1.4 garbled
501 1.4 garbled write(rs6000_fd, elf_img, elf_img_len);
502 1.4 garbled free(elf_img);
503 1.4 garbled
504 1.4 garbled /* now dump in the padding space for the BSS */
505 1.4 garbled elf_pad += 100; /* just a little extra for good luck */
506 1.4 garbled lseek(rs6000_fd, elf_pad, SEEK_CUR);
507 1.4 garbled
508 1.4 garbled /* Copy kernel */
509 1.4 garbled kern_img = (unsigned char *)malloc(kern_stat.st_size);
510 1.4 garbled
511 1.4 garbled if (kern_img == NULL)
512 1.4 garbled errx(3, "Can't malloc: %s", strerror(errno));
513 1.4 garbled
514 1.4 garbled /* we need to jump back after having read the headers */
515 1.4 garbled lseek(kern_fd, 0, SEEK_SET);
516 1.4 garbled if (read(kern_fd, (void *)kern_img, kern_stat.st_size) !=
517 1.4 garbled kern_stat.st_size)
518 1.4 garbled errx(3, "Can't read kernel '%s' : %s", kernel, strerror(errno));
519 1.4 garbled
520 1.4 garbled gzf = gzdopen(dup(rs6000_fd), "a");
521 1.4 garbled if (gzf == NULL)
522 1.4 garbled errx(3, "Can't init compression: %s", strerror(errno));
523 1.4 garbled if (gzsetparams(gzf, Z_BEST_COMPRESSION, Z_DEFAULT_STRATEGY) != Z_OK)
524 1.4 garbled errx(3, "%s", gzerror(gzf, &err));
525 1.4 garbled
526 1.4 garbled /* write a magic number and size before the kernel */
527 1.4 garbled write(rs6000_fd, (void *)rs6000_magic, RS6000_MAGICSIZE);
528 1.4 garbled lenpos = lseek(rs6000_fd, 0, SEEK_CUR);
529 1.4 garbled if (verboseflag)
530 1.4 garbled printf("wrote magic at pos 0x%x\n", lenpos);
531 1.4 garbled tmp = sa_htobe32(0);
532 1.4 garbled write(rs6000_fd, (void *)&tmp, KERNLENSIZE);
533 1.4 garbled
534 1.4 garbled /* write in the compressed kernel */
535 1.4 garbled kstart = lseek(rs6000_fd, 0, SEEK_CUR);
536 1.4 garbled if (verboseflag)
537 1.4 garbled printf("kernel start at pos 0x%x\n", kstart);
538 1.4 garbled kgzlen = gzwrite(gzf, kern_img, kern_stat.st_size);
539 1.4 garbled gzclose(gzf);
540 1.4 garbled kend = lseek(rs6000_fd, 0, SEEK_CUR);
541 1.4 garbled if (verboseflag)
542 1.4 garbled printf("kernel end at pos 0x%x\n", kend);
543 1.4 garbled
544 1.4 garbled /* jump back to the length position now that we know the length */
545 1.4 garbled lseek(rs6000_fd, lenpos, SEEK_SET);
546 1.4 garbled kgzlen = kend - kstart;
547 1.4 garbled tmp = sa_htobe32(kgzlen);
548 1.4 garbled if (verboseflag)
549 1.4 garbled printf("kernel len = 0x%x tmp = 0x%x\n", kgzlen, tmp);
550 1.4 garbled write(rs6000_fd, (void *)&tmp, KERNLENSIZE);
551 1.4 garbled
552 1.4 garbled #if 0
553 1.4 garbled lseek(rs6000_fd, sizeof(boot_record_t) + sizeof(config_record_t),
554 1.4 garbled SEEK_SET);
555 1.4 garbled /* set entry and length */
556 1.4 garbled length = sa_htole32(0x400);
557 1.4 garbled write(rs6000_fd, &length, sizeof(length));
558 1.4 garbled length = sa_htole32(0x400 + elf_img_len + 8 + kgzlen);
559 1.4 garbled write(rs6000_fd, &length, sizeof(length));
560 1.4 garbled #endif
561 1.4 garbled
562 1.4 garbled /* generate the header now that we know the kernel length */
563 1.4 garbled if (verboseflag)
564 1.4 garbled printf("building records\n");
565 1.4 garbled rs6000_build_records(elf_img_len + 8 + kgzlen);
566 1.4 garbled lseek(rs6000_fd, 0, SEEK_SET);
567 1.4 garbled /* ROM wants it byteswapped in 32bit chunks */
568 1.4 garbled if (verboseflag)
569 1.4 garbled printf("writing records\n");
570 1.4 garbled memcpy(swapped, &bootrec, sizeof(rs6000_boot_record_t));
571 1.4 garbled for (i=0; i < 128; i++)
572 1.4 garbled swapped[i] = htonl(swapped[i]);
573 1.4 garbled write(rs6000_fd, swapped, sizeof(rs6000_boot_record_t));
574 1.4 garbled memcpy(swapped, &confrec, sizeof(rs6000_config_record_t));
575 1.4 garbled for (i=0; i < 128; i++)
576 1.4 garbled swapped[i] = htonl(swapped[i]);
577 1.4 garbled write(rs6000_fd, swapped, sizeof(rs6000_config_record_t));
578 1.4 garbled
579 1.4 garbled free(kern_img);
580 1.4 garbled close(kern_fd);
581 1.4 garbled close(rs6000_fd);
582 1.4 garbled close(elf_fd);
583 1.4 garbled
584 1.4 garbled return 0;
585 1.4 garbled }
586 1.4 garbled
587 1.6 garbled static int
588 1.6 garbled bebox_write_header(int bebox_fd, int elf_image_len, int kern_img_len)
589 1.6 garbled {
590 1.6 garbled int hsize = BEBOX_HEADER_SIZE;
591 1.6 garbled unsigned long textOffset, dataOffset, ldrOffset;
592 1.6 garbled unsigned long entry_vector[3];
593 1.6 garbled struct FileHeader fileHdr;
594 1.6 garbled struct SectionHeader textHdr, dataHdr, ldrHdr;
595 1.6 garbled struct LoaderHeader lh;
596 1.6 garbled
597 1.6 garbled if (saloneflag)
598 1.6 garbled hsize = 0;
599 1.12 kiyohara
600 1.6 garbled ldrOffset = ULALIGN(sizeof (fileHdr) + sizeof (textHdr) +
601 1.6 garbled sizeof (dataHdr) + sizeof (ldrHdr));
602 1.6 garbled dataOffset = ULALIGN(ldrOffset + sizeof (lh));
603 1.6 garbled textOffset = ULALIGN(dataOffset + sizeof (entry_vector) + kern_img_len);
604 1.6 garbled
605 1.6 garbled /* Create the File Header */
606 1.6 garbled memset(&fileHdr, 0, sizeof (fileHdr));
607 1.6 garbled fileHdr.magic = sa_htobe32(PEF_MAGIC);
608 1.6 garbled fileHdr.fileTypeID = sa_htobe32(PEF_FILE);
609 1.12 kiyohara fileHdr.archID = sa_htobe32(PEF_PPC);
610 1.12 kiyohara fileHdr.versionNumber = sa_htobe32(1);
611 1.12 kiyohara fileHdr.numSections = sa_htobe16(3);
612 1.12 kiyohara fileHdr.loadableSections = sa_htobe16(2);
613 1.12 kiyohara write(bebox_fd, &fileHdr, sizeof (fileHdr));
614 1.6 garbled
615 1.6 garbled /* Create the Section Header for TEXT */
616 1.6 garbled memset(&textHdr, 0, sizeof (textHdr));
617 1.12 kiyohara textHdr.sectionName = sa_htobe32(-1);
618 1.12 kiyohara textHdr.sectionAddress = sa_htobe32(0);
619 1.12 kiyohara textHdr.execSize = sa_htobe32(elf_image_len);
620 1.12 kiyohara textHdr.initSize = sa_htobe32(elf_image_len);
621 1.12 kiyohara textHdr.rawSize = sa_htobe32(elf_image_len);
622 1.12 kiyohara textHdr.fileOffset = sa_htobe32(textOffset);
623 1.12 kiyohara textHdr.regionKind = CodeSection;
624 1.12 kiyohara textHdr.shareKind = ContextShare;
625 1.12 kiyohara textHdr.alignment = 4; /* 16 byte alignment */
626 1.12 kiyohara write(bebox_fd, &textHdr, sizeof (textHdr));
627 1.6 garbled
628 1.6 garbled /* Create the Section Header for DATA */
629 1.6 garbled memset(&dataHdr, 0, sizeof (dataHdr));
630 1.12 kiyohara dataHdr.sectionName = sa_htobe32(-1);
631 1.12 kiyohara dataHdr.sectionAddress = sa_htobe32(0);
632 1.12 kiyohara dataHdr.execSize = sa_htobe32(sizeof (entry_vector) + kern_img_len);
633 1.12 kiyohara dataHdr.initSize = sa_htobe32(sizeof (entry_vector) + kern_img_len);
634 1.12 kiyohara dataHdr.rawSize = sa_htobe32(sizeof (entry_vector) + kern_img_len);
635 1.12 kiyohara dataHdr.fileOffset = sa_htobe32(dataOffset);
636 1.12 kiyohara dataHdr.regionKind = DataSection;
637 1.12 kiyohara dataHdr.shareKind = ContextShare;
638 1.12 kiyohara dataHdr.alignment = 4; /* 16 byte alignment */
639 1.12 kiyohara write(bebox_fd, &dataHdr, sizeof (dataHdr));
640 1.6 garbled
641 1.6 garbled /* Create the Section Header for loader stuff */
642 1.6 garbled memset(&ldrHdr, 0, sizeof (ldrHdr));
643 1.12 kiyohara ldrHdr.sectionName = sa_htobe32(-1);
644 1.12 kiyohara ldrHdr.sectionAddress = sa_htobe32(0);
645 1.12 kiyohara ldrHdr.execSize = sa_htobe32(sizeof (lh));
646 1.12 kiyohara ldrHdr.initSize = sa_htobe32(sizeof (lh));
647 1.12 kiyohara ldrHdr.rawSize = sa_htobe32(sizeof (lh));
648 1.12 kiyohara ldrHdr.fileOffset = sa_htobe32(ldrOffset);
649 1.12 kiyohara ldrHdr.regionKind = LoaderSection;
650 1.12 kiyohara ldrHdr.shareKind = GlobalShare;
651 1.12 kiyohara ldrHdr.alignment = 4; /* 16 byte alignment */
652 1.12 kiyohara write(bebox_fd, &ldrHdr, sizeof (ldrHdr));
653 1.6 garbled
654 1.6 garbled /* Create the Loader Header */
655 1.6 garbled memset(&lh, 0, sizeof (lh));
656 1.12 kiyohara lh.entryPointSection = sa_htobe32(1); /* Data */
657 1.12 kiyohara lh.entryPointOffset = sa_htobe32(0);
658 1.12 kiyohara lh.initPointSection = sa_htobe32(-1);
659 1.12 kiyohara lh.initPointOffset = sa_htobe32(0);
660 1.12 kiyohara lh.termPointSection = sa_htobe32(-1);
661 1.12 kiyohara lh.termPointOffset = sa_htobe32(0);
662 1.12 kiyohara lseek(bebox_fd, ldrOffset + hsize, SEEK_SET);
663 1.12 kiyohara write(bebox_fd, &lh, sizeof (lh));
664 1.6 garbled
665 1.6 garbled /* Copy the pseudo-DATA */
666 1.6 garbled memset(entry_vector, 0, sizeof (entry_vector));
667 1.12 kiyohara entry_vector[0] = sa_htobe32(BEBOX_ENTRY); /* Magic */
668 1.12 kiyohara lseek(bebox_fd, dataOffset + hsize, SEEK_SET);
669 1.12 kiyohara write(bebox_fd, entry_vector, sizeof (entry_vector));
670 1.6 garbled
671 1.6 garbled return textOffset;
672 1.12 kiyohara }
673 1.6 garbled
674 1.6 garbled static int
675 1.6 garbled bebox_build_image(char *kernel, char *boot, char *rawdev, char *outname)
676 1.6 garbled {
677 1.6 garbled unsigned char *elf_img = NULL, *kern_img = NULL, *header_img = NULL;
678 1.6 garbled int i, ch, tmp, kgzlen, err, hsize = BEBOX_HEADER_SIZE;
679 1.6 garbled int elf_fd, bebox_fd, kern_fd, elf_img_len = 0;
680 1.6 garbled uint32_t swapped[128];
681 1.6 garbled off_t lenpos, kstart, kend, toff, endoff;
682 1.6 garbled unsigned long length;
683 1.6 garbled long flength, *offset;
684 1.6 garbled gzFile gzf;
685 1.6 garbled struct stat kern_stat;
686 1.6 garbled struct bebox_image_block *p;
687 1.6 garbled struct timeval tp;
688 1.6 garbled Elf32_External_Phdr phdr;
689 1.6 garbled
690 1.6 garbled if (saloneflag)
691 1.6 garbled hsize = 0;
692 1.12 kiyohara
693 1.6 garbled elf_fd = open_file("bootloader", boot, &hdr, &elf_stat);
694 1.13 kiyohara if (inkernflag) {
695 1.13 kiyohara kern_fd = open_file("kernel", kernel, &khdr, &kern_stat);
696 1.13 kiyohara kern_len = kern_stat.st_size + BEBOX_MAGICSIZE + KERNLENSIZE;
697 1.13 kiyohara } else
698 1.13 kiyohara kern_len = BEBOX_MAGICSIZE + KERNLENSIZE;
699 1.6 garbled
700 1.6 garbled for (i = 0; i < ELFGET16(hdr.e_phnum); i++) {
701 1.6 garbled lseek(elf_fd, ELFGET32(hdr.e_phoff) + sizeof(phdr) * i,
702 1.6 garbled SEEK_SET);
703 1.6 garbled if (read(elf_fd, &phdr, sizeof(phdr)) != sizeof(phdr))
704 1.6 garbled errx(3, "Can't read input '%s' phdr : %s", boot,
705 1.6 garbled strerror(errno));
706 1.6 garbled
707 1.6 garbled if ((ELFGET32(phdr.p_type) != PT_LOAD) ||
708 1.6 garbled !(ELFGET32(phdr.p_flags) & PF_X))
709 1.6 garbled continue;
710 1.6 garbled
711 1.6 garbled fstat(elf_fd, &elf_stat);
712 1.10 kiyohara elf_img_len = ELFGET32(phdr.p_filesz);
713 1.6 garbled lseek(elf_fd, ELFGET32(phdr.p_offset), SEEK_SET);
714 1.6 garbled
715 1.6 garbled break;
716 1.6 garbled }
717 1.6 garbled if ((bebox_fd = open(outname, O_RDWR|O_TRUNC, 0)) < 0) {
718 1.6 garbled /* we couldn't open it, it must be new */
719 1.6 garbled bebox_fd = creat(outname, 0644);
720 1.6 garbled if (bebox_fd < 0)
721 1.6 garbled errx(2, "Can't open output '%s': %s", outname,
722 1.6 garbled strerror(errno));
723 1.6 garbled }
724 1.6 garbled lseek(bebox_fd, hsize, SEEK_SET);
725 1.12 kiyohara
726 1.13 kiyohara if (inkernflag) {
727 1.13 kiyohara /*
728 1.13 kiyohara * write the header with the wrong values to get the offset
729 1.13 kiyohara * right
730 1.13 kiyohara */
731 1.13 kiyohara bebox_write_header(bebox_fd, elf_img_len, kern_stat.st_size);
732 1.13 kiyohara
733 1.13 kiyohara /* Copy kernel */
734 1.13 kiyohara kern_img = (unsigned char *)malloc(kern_stat.st_size);
735 1.13 kiyohara
736 1.13 kiyohara if (kern_img == NULL)
737 1.13 kiyohara errx(3, "Can't malloc: %s", strerror(errno));
738 1.13 kiyohara
739 1.13 kiyohara /* we need to jump back after having read the headers */
740 1.13 kiyohara lseek(kern_fd, 0, SEEK_SET);
741 1.13 kiyohara if (read(kern_fd, (void *)kern_img, kern_stat.st_size) !=
742 1.13 kiyohara kern_stat.st_size)
743 1.13 kiyohara errx(3, "Can't read kernel '%s' : %s",
744 1.13 kiyohara kernel, strerror(errno));
745 1.13 kiyohara
746 1.13 kiyohara gzf = gzdopen(dup(bebox_fd), "a");
747 1.13 kiyohara if (gzf == NULL)
748 1.13 kiyohara errx(3, "Can't init compression: %s", strerror(errno));
749 1.13 kiyohara if (gzsetparams(gzf, Z_BEST_COMPRESSION, Z_DEFAULT_STRATEGY) !=
750 1.13 kiyohara Z_OK)
751 1.13 kiyohara errx(3, "%s", gzerror(gzf, &err));
752 1.13 kiyohara } else
753 1.13 kiyohara bebox_write_header(bebox_fd, elf_img_len, 0);
754 1.6 garbled
755 1.6 garbled /* write a magic number and size before the kernel */
756 1.6 garbled write(bebox_fd, (void *)bebox_magic, BEBOX_MAGICSIZE);
757 1.6 garbled lenpos = lseek(bebox_fd, 0, SEEK_CUR);
758 1.6 garbled tmp = sa_htobe32(0);
759 1.6 garbled write(bebox_fd, (void *)&tmp, KERNLENSIZE);
760 1.6 garbled
761 1.13 kiyohara if (inkernflag) {
762 1.13 kiyohara /* write in the compressed kernel */
763 1.13 kiyohara kstart = lseek(bebox_fd, 0, SEEK_CUR);
764 1.13 kiyohara kgzlen = gzwrite(gzf, kern_img, kern_stat.st_size);
765 1.13 kiyohara gzclose(gzf);
766 1.13 kiyohara kend = lseek(bebox_fd, 0, SEEK_CUR);
767 1.13 kiyohara free(kern_img);
768 1.13 kiyohara } else {
769 1.13 kiyohara kstart = kend = lseek(bebox_fd, 0, SEEK_CUR);
770 1.13 kiyohara kgzlen = 0;
771 1.13 kiyohara }
772 1.6 garbled
773 1.6 garbled /* jump back to the length position now that we know the length */
774 1.6 garbled lseek(bebox_fd, lenpos, SEEK_SET);
775 1.6 garbled kgzlen = kend - kstart;
776 1.6 garbled tmp = sa_htobe32(kgzlen);
777 1.6 garbled write(bebox_fd, (void *)&tmp, KERNLENSIZE);
778 1.6 garbled
779 1.6 garbled /* now rewrite the header correctly */
780 1.6 garbled lseek(bebox_fd, hsize, SEEK_SET);
781 1.10 kiyohara tmp = kgzlen + BEBOX_MAGICSIZE + KERNLENSIZE;
782 1.10 kiyohara toff = bebox_write_header(bebox_fd, elf_img_len, tmp);
783 1.6 garbled
784 1.6 garbled /* Copy boot image */
785 1.6 garbled elf_img = (unsigned char *)malloc(elf_img_len);
786 1.6 garbled if (!elf_img)
787 1.6 garbled errx(3, "Can't malloc: %s", strerror(errno));
788 1.6 garbled if (read(elf_fd, elf_img, elf_img_len) != elf_img_len)
789 1.6 garbled errx(3, "Can't read file '%s' : %s", boot, strerror(errno));
790 1.6 garbled lseek(bebox_fd, toff + hsize, SEEK_SET);
791 1.6 garbled write(bebox_fd, elf_img, elf_img_len);
792 1.6 garbled free(elf_img);
793 1.12 kiyohara
794 1.13 kiyohara if (inkernflag)
795 1.13 kiyohara close(kern_fd);
796 1.6 garbled close(elf_fd);
797 1.6 garbled
798 1.6 garbled if (saloneflag) {
799 1.6 garbled close(bebox_fd);
800 1.6 garbled return 0;
801 1.6 garbled }
802 1.12 kiyohara
803 1.6 garbled /* Now go back and write in the block header */
804 1.6 garbled endoff = lseek(bebox_fd, 0, SEEK_END);
805 1.6 garbled lseek(bebox_fd, 0, SEEK_SET);
806 1.6 garbled header_img = (unsigned char *)malloc(BEBOX_HEADER_SIZE);
807 1.6 garbled if (!header_img)
808 1.6 garbled errx(3, "Can't malloc: %s", strerror(errno));
809 1.6 garbled memset(header_img, 0, BEBOX_HEADER_SIZE);
810 1.6 garbled
811 1.6 garbled /* copy the boot image into the buffer */
812 1.6 garbled for (p = bebox_image_block; p->offset != -1; p++)
813 1.6 garbled memcpy(header_img + p->offset, p->data, p->size);
814 1.6 garbled
815 1.6 garbled /* fill used block bitmap */
816 1.6 garbled memset(header_img + BEBOX_FILE_BLOCK_MAP_START, 0xff,
817 1.6 garbled BEBOX_FILE_BLOCK_MAP_END - BEBOX_FILE_BLOCK_MAP_START);
818 1.6 garbled
819 1.6 garbled /* fix the file size in the header */
820 1.10 kiyohara tmp = endoff - BEBOX_HEADER_SIZE;
821 1.6 garbled *(long *)(header_img + BEBOX_FILE_SIZE_OFFSET) =
822 1.10 kiyohara (long)sa_htobe32(tmp);
823 1.6 garbled *(long *)(header_img + BEBOX_FILE_SIZE_ALIGN_OFFSET) =
824 1.10 kiyohara (long)sa_htobe32(roundup(tmp, BEBOX_FILE_BLOCK_SIZE));
825 1.6 garbled
826 1.6 garbled gettimeofday(&tp, 0);
827 1.6 garbled for (offset = bebox_mtime_offset; *offset != -1; offset++)
828 1.6 garbled *(long *)(header_img + *offset) = (long)sa_htobe32(tp.tv_sec);
829 1.6 garbled
830 1.6 garbled write(bebox_fd, header_img, BEBOX_HEADER_SIZE);
831 1.12 kiyohara
832 1.6 garbled /* now pad the end */
833 1.6 garbled flength = roundup(endoff, BEBOX_BLOCK_SIZE);
834 1.6 garbled /* refill the header_img with zeros */
835 1.6 garbled memset(header_img, 0, BEBOX_BLOCK_SIZE * 2);
836 1.6 garbled lseek(bebox_fd, 0, SEEK_END);
837 1.6 garbled write(bebox_fd, header_img, flength - endoff);
838 1.12 kiyohara
839 1.6 garbled close(bebox_fd);
840 1.6 garbled
841 1.6 garbled return 0;
842 1.6 garbled }
843 1.6 garbled
844 1.2 garbled int
845 1.2 garbled main(int argc, char **argv)
846 1.2 garbled {
847 1.2 garbled int ch, lfloppyflag=0;
848 1.2 garbled char *kernel = NULL, *boot = NULL, *rawdev = NULL, *outname = NULL;
849 1.2 garbled char *march = NULL;
850 1.9 garbled #ifdef USE_SYSCTL
851 1.11 martin char machine[SYS_NMLN];
852 1.11 martin int mib[2] = { CTL_HW, HW_MACHINE };
853 1.2 garbled #endif
854 1.12 kiyohara
855 1.2 garbled setprogname(argv[0]);
856 1.2 garbled kern_len = 0;
857 1.2 garbled
858 1.13 kiyohara while ((ch = getopt(argc, argv, "b:Ik:lm:r:sv")) != -1)
859 1.2 garbled switch (ch) {
860 1.2 garbled case 'b':
861 1.2 garbled boot = optarg;
862 1.2 garbled break;
863 1.13 kiyohara case 'I':
864 1.13 kiyohara inkernflag = 0;
865 1.13 kiyohara break;
866 1.2 garbled case 'k':
867 1.2 garbled kernel = optarg;
868 1.13 kiyohara inkernflag = 1;
869 1.2 garbled break;
870 1.2 garbled case 'l':
871 1.2 garbled lfloppyflag = 1;
872 1.2 garbled break;
873 1.2 garbled case 'm':
874 1.2 garbled march = optarg;
875 1.2 garbled break;
876 1.2 garbled case 'r':
877 1.2 garbled rawdev = optarg;
878 1.2 garbled break;
879 1.2 garbled case 's':
880 1.2 garbled saloneflag = 1;
881 1.2 garbled break;
882 1.4 garbled case 'v':
883 1.4 garbled verboseflag = 1;
884 1.4 garbled break;
885 1.2 garbled case '?':
886 1.2 garbled default:
887 1.4 garbled usage(0);
888 1.2 garbled /* NOTREACHED */
889 1.2 garbled }
890 1.2 garbled argc -= optind;
891 1.2 garbled argv += optind;
892 1.2 garbled
893 1.2 garbled if (argc < 1)
894 1.4 garbled usage(0);
895 1.2 garbled
896 1.13 kiyohara if (kernel == NULL && inkernflag)
897 1.2 garbled kernel = "/netbsd";
898 1.2 garbled
899 1.2 garbled if (boot == NULL)
900 1.2 garbled boot = "/usr/mdec/boot";
901 1.2 garbled
902 1.7 garbled if (march != NULL && strcmp(march, "") == 0)
903 1.4 garbled march = NULL;
904 1.2 garbled if (march == NULL) {
905 1.2 garbled int i;
906 1.9 garbled #ifdef USE_SYSCTL
907 1.11 martin size_t len = sizeof(machine);
908 1.2 garbled
909 1.11 martin if (sysctl(mib, sizeof (mib) / sizeof (mib[0]), machine,
910 1.2 garbled &len, NULL, 0) != -1) {
911 1.2 garbled for (i=0; sup_plats[i] != NULL; i++) {
912 1.11 martin if (strcmp(sup_plats[i], machine) == 0) {
913 1.2 garbled march = strdup(sup_plats[i]);
914 1.2 garbled break;
915 1.2 garbled }
916 1.2 garbled }
917 1.2 garbled }
918 1.4 garbled if (march == NULL)
919 1.2 garbled #endif
920 1.4 garbled usage(1);
921 1.2 garbled }
922 1.12 kiyohara
923 1.2 garbled outname = argv[0];
924 1.2 garbled
925 1.2 garbled if (strcmp(march, "prep") == 0)
926 1.4 garbled return(prep_build_image(kernel, boot, rawdev, outname));
927 1.4 garbled if (strcmp(march, "rs6000") == 0)
928 1.4 garbled return(rs6000_build_image(kernel, boot, rawdev, outname));
929 1.6 garbled if (strcmp(march, "bebox") == 0)
930 1.6 garbled return(bebox_build_image(kernel, boot, rawdev, outname));
931 1.2 garbled
932 1.4 garbled usage(1);
933 1.2 garbled return(0);
934 1.1 garbled }
935