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