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