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