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