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