Home | History | Annotate | Line # | Download | only in mkbootimage
mkbootimage.c revision 1.4
      1  1.4  garbled /*	$NetBSD: mkbootimage.c,v 1.4 2007/12/20 17:58:49 garbled Exp $	*/
      2  1.1  garbled 
      3  1.1  garbled /*-
      4  1.2  garbled  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  1.1  garbled  * All rights reserved.
      6  1.1  garbled  *
      7  1.2  garbled  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2  garbled  * by Tim Rightnour and NONAKA Kimihiro
      9  1.2  garbled  *
     10  1.1  garbled  * Redistribution and use in source and binary forms, with or without
     11  1.1  garbled  * modification, are permitted provided that the following conditions
     12  1.1  garbled  * are met:
     13  1.1  garbled  * 1. Redistributions of source code must retain the above copyright
     14  1.1  garbled  *    notice, this list of conditions and the following disclaimer.
     15  1.1  garbled  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  garbled  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  garbled  *    documentation and/or other materials provided with the distribution.
     18  1.1  garbled  * 3. All advertising materials mentioning features or use of this software
     19  1.1  garbled  *    must display the following acknowledgement:
     20  1.2  garbled  *        This product includes software developed by the NetBSD
     21  1.2  garbled  *        Foundation, Inc. and its contributors.
     22  1.2  garbled  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.2  garbled  *    contributors may be used to endorse or promote products derived
     24  1.2  garbled  *    from this software without specific prior written permission.
     25  1.1  garbled  *
     26  1.2  garbled  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.2  garbled  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.2  garbled  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.2  garbled  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.2  garbled  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.2  garbled  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.2  garbled  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.2  garbled  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.2  garbled  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.2  garbled  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.2  garbled  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  garbled  */
     38  1.1  garbled 
     39  1.1  garbled #if HAVE_NBTOOL_CONFIG_H
     40  1.1  garbled #include "nbtool_config.h"
     41  1.1  garbled #include "../../sys/sys/bootblock.h"
     42  1.1  garbled #else
     43  1.1  garbled #include <sys/bootblock.h>
     44  1.1  garbled #endif
     45  1.1  garbled 
     46  1.1  garbled #include <stdio.h>
     47  1.1  garbled #include <stdlib.h>
     48  1.1  garbled #include <string.h>
     49  1.1  garbled #include <fcntl.h>
     50  1.1  garbled #include <unistd.h>
     51  1.1  garbled #include <errno.h>
     52  1.1  garbled #include <zlib.h>
     53  1.1  garbled #include <err.h>
     54  1.1  garbled #include <sys/stat.h>
     55  1.1  garbled #include <sys/types.h>
     56  1.1  garbled #include <sys/uio.h>
     57  1.1  garbled 
     58  1.2  garbled #ifdef __NetBSD__
     59  1.2  garbled #include <sys/sysctl.h>
     60  1.2  garbled #include <sys/utsname.h>
     61  1.2  garbled #endif
     62  1.2  garbled 
     63  1.1  garbled /* BFD ELF headers */
     64  1.1  garbled #include <elf/common.h>
     65  1.1  garbled #include <elf/external.h>
     66  1.1  garbled 
     67  1.4  garbled #include "rs6000_bootrec.h"
     68  1.1  garbled #include "byteorder.h"
     69  1.3  garbled #include "magic.h"
     70  1.1  garbled 
     71  1.1  garbled /* Globals */
     72  1.1  garbled 
     73  1.1  garbled int saloneflag = 0;
     74  1.4  garbled int verboseflag = 0;
     75  1.4  garbled int lfloppyflag = 0;
     76  1.1  garbled Elf32_External_Ehdr hdr, khdr;
     77  1.1  garbled struct stat elf_stat;
     78  1.1  garbled unsigned char mbr[512];
     79  1.4  garbled 
     80  1.4  garbled /* the boot and config records for rs6000 */
     81  1.4  garbled rs6000_boot_record_t bootrec;
     82  1.4  garbled rs6000_config_record_t confrec;
     83  1.4  garbled 
     84  1.4  garbled /* supported platforms */
     85  1.2  garbled char *sup_plats[] = {
     86  1.2  garbled 	"prep",
     87  1.4  garbled 	"rs6000",
     88  1.2  garbled 	NULL,
     89  1.2  garbled };
     90  1.1  garbled 
     91  1.1  garbled /*
     92  1.1  garbled  * Macros to get values from multi-byte ELF header fields.  These assume
     93  1.1  garbled  * a big-endian image.
     94  1.1  garbled  */
     95  1.1  garbled #define	ELFGET16(x)	(((x)[0] << 8) | (x)[1])
     96  1.1  garbled 
     97  1.1  garbled #define	ELFGET32(x)	(((x)[0] << 24) | ((x)[1] << 16) |		\
     98  1.1  garbled 			 ((x)[2] <<  8) |  (x)[3])
     99  1.1  garbled 
    100  1.4  garbled static void usage(int);
    101  1.1  garbled static int open_file(const char *, char *, Elf32_External_Ehdr *,
    102  1.1  garbled     struct stat *);
    103  1.4  garbled static void check_mbr(int, char *);
    104  1.4  garbled static int prep_build_image(char *, char *, char *, char *);
    105  1.4  garbled static void rs6000_build_records(int);
    106  1.4  garbled static int rs6000_build_image(char *, char *, char *, char *);
    107  1.1  garbled int main(int, char **);
    108  1.1  garbled 
    109  1.1  garbled static void
    110  1.4  garbled usage(int extended)
    111  1.1  garbled {
    112  1.4  garbled 	int i;
    113  1.4  garbled 
    114  1.4  garbled 	if (extended) {
    115  1.4  garbled 		fprintf(stderr, "You are not running this program on"
    116  1.4  garbled 		    " the target machine.  You must supply the\n"
    117  1.4  garbled 		    "machine architecture with the -m flag\n");
    118  1.4  garbled 		fprintf(stderr, "Supported architectures: ");
    119  1.4  garbled 		for (i=0; sup_plats[i] != NULL; i++)
    120  1.4  garbled 			fprintf(stderr, " %s", sup_plats[i]);
    121  1.4  garbled 		fprintf(stderr, "\n\n");
    122  1.4  garbled 	}
    123  1.2  garbled #ifdef __NetBSD__
    124  1.2  garbled 	fprintf(stderr, "usage: %s [-ls] [-m machine_arch] [-b bootfile] "
    125  1.2  garbled 	    "[-k kernel] [-r rawdev] bootimage\n", getprogname());
    126  1.2  garbled #else
    127  1.2  garbled 	fprintf(stderr, "usage: %s [-ls] -m machine_arch [-b bootfile] "
    128  1.2  garbled 	    "[-k kernel] [-r rawdev] bootimage\n", getprogname());
    129  1.2  garbled #endif
    130  1.1  garbled 	exit(1);
    131  1.1  garbled }
    132  1.1  garbled 
    133  1.1  garbled /* verify the file is ELF and ppc, and open it up */
    134  1.1  garbled static int
    135  1.1  garbled open_file(const char *ftype, char *file, Elf32_External_Ehdr *hdr,
    136  1.1  garbled 	struct stat *f_stat)
    137  1.1  garbled {
    138  1.1  garbled 	int fd;
    139  1.1  garbled 
    140  1.1  garbled 	if ((fd = open(file, 0)) < 0)
    141  1.1  garbled 		errx(2, "Can't open %s '%s': %s", ftype, file, strerror(errno));
    142  1.1  garbled 	fstat(fd, f_stat);
    143  1.1  garbled 
    144  1.1  garbled 	if (read(fd, hdr, sizeof(Elf32_External_Ehdr)) !=
    145  1.1  garbled 	    sizeof(Elf32_External_Ehdr))
    146  1.1  garbled 		errx(3, "Can't read input '%s': %s", file, strerror(errno));
    147  1.1  garbled 
    148  1.1  garbled 	if (hdr->e_ident[EI_MAG0] != ELFMAG0 ||
    149  1.1  garbled 	    hdr->e_ident[EI_MAG1] != ELFMAG1 ||
    150  1.1  garbled 	    hdr->e_ident[EI_MAG2] != ELFMAG2 ||
    151  1.1  garbled 	    hdr->e_ident[EI_MAG3] != ELFMAG3 ||
    152  1.1  garbled 	    hdr->e_ident[EI_CLASS] != ELFCLASS32)
    153  1.1  garbled 		errx(3, "input '%s' is not ELF32 format", file);
    154  1.1  garbled 
    155  1.1  garbled 	if (hdr->e_ident[EI_DATA] != ELFDATA2MSB)
    156  1.1  garbled 		errx(3, "input '%s' is not big-endian", file);
    157  1.1  garbled 
    158  1.1  garbled 	if (ELFGET16(hdr->e_machine) != EM_PPC)
    159  1.1  garbled 		errx(3, "input '%s' is not PowerPC exec binary", file);
    160  1.1  garbled 
    161  1.1  garbled 	return(fd);
    162  1.1  garbled }
    163  1.1  garbled 
    164  1.1  garbled static void
    165  1.4  garbled prep_check_mbr(int prep_fd, char *rawdev)
    166  1.1  garbled {
    167  1.1  garbled 	int raw_fd;
    168  1.1  garbled 	unsigned long entry, length;
    169  1.1  garbled 	struct mbr_partition *mbrp;
    170  1.1  garbled 	struct stat raw_stat;
    171  1.1  garbled 
    172  1.1  garbled 	/* If we are building a standalone image, do not write an MBR, just
    173  1.1  garbled 	 * set entry point and boot image size skipping over elf header
    174  1.1  garbled 	 */
    175  1.1  garbled 	if (saloneflag) {
    176  1.1  garbled 		entry  = sa_htole32(0x400);
    177  1.1  garbled 		length = sa_htole32(elf_stat.st_size - sizeof(hdr) + 0x400);
    178  1.1  garbled 		lseek(prep_fd, sizeof(mbr), SEEK_SET);
    179  1.1  garbled 		write(prep_fd, &entry, sizeof(entry));
    180  1.1  garbled 		write(prep_fd, &length, sizeof(length));
    181  1.1  garbled 		return;
    182  1.1  garbled 	}
    183  1.1  garbled 
    184  1.1  garbled 	/*
    185  1.1  garbled 	 * if we have a raw device, we need to check to see if it already
    186  1.1  garbled 	 * has a partition table, and if so, read it in and check for
    187  1.1  garbled 	 * suitability.
    188  1.1  garbled 	 */
    189  1.1  garbled 	if (rawdev != NULL) {
    190  1.1  garbled 		raw_fd = open(rawdev, O_RDONLY, 0);
    191  1.1  garbled 		if (raw_fd == -1)
    192  1.1  garbled 			errx(3, "couldn't open raw device %s: %s", rawdev,
    193  1.1  garbled 			    strerror(errno));
    194  1.1  garbled 
    195  1.1  garbled 		fstat(raw_fd, &raw_stat);
    196  1.1  garbled 		if (!S_ISCHR(raw_stat.st_mode))
    197  1.1  garbled 			errx(3, "%s is not a raw device", rawdev);
    198  1.1  garbled 
    199  1.1  garbled 		if (read(raw_fd, mbr, 512) != 512)
    200  1.1  garbled 			errx(3, "MBR Read Failed: %s", strerror(errno));
    201  1.1  garbled 
    202  1.1  garbled 		mbrp = (struct mbr_partition *)&mbr[MBR_PART_OFFSET];
    203  1.1  garbled 		if (mbrp->mbrp_type != MBR_PTYPE_PREP)
    204  1.1  garbled 			errx(3, "First partition is not of type 0x%x.",
    205  1.1  garbled 			    MBR_PTYPE_PREP);
    206  1.1  garbled 		if (mbrp->mbrp_start != 0)
    207  1.1  garbled 			errx(3, "Use of the raw device is intended for"
    208  1.1  garbled 			    " upgrading of legacy installations.  Your"
    209  1.1  garbled 			    " install does not have a PReP boot partition"
    210  1.1  garbled 			    " starting at sector 0.  Use the -s option"
    211  1.1  garbled 			    " to build an image instead.");
    212  1.1  garbled 
    213  1.1  garbled 		/* if we got this far, we are fine, write back the partition
    214  1.1  garbled 		 * and write the entry points and get outta here */
    215  1.1  garbled 	/* Set entry point and boot image size skipping over elf header */
    216  1.1  garbled 		lseek(prep_fd, 0, SEEK_SET);
    217  1.1  garbled 		entry  = sa_htole32(0x400);
    218  1.1  garbled 		length = sa_htole32(elf_stat.st_size - sizeof(hdr) + 0x400);
    219  1.1  garbled 		write(prep_fd, mbr, sizeof(mbr));
    220  1.1  garbled 		write(prep_fd, &entry, sizeof(entry));
    221  1.1  garbled 		write(prep_fd, &length, sizeof(length));
    222  1.1  garbled 		close(raw_fd);
    223  1.1  garbled 		return;
    224  1.1  garbled 	}
    225  1.1  garbled 
    226  1.1  garbled 	/* if we get to here, we want to build a standard floppy or netboot
    227  1.1  garbled 	 * image to file, so just build it */
    228  1.1  garbled 
    229  1.1  garbled 	memset(mbr, 0, sizeof(mbr));
    230  1.1  garbled 	mbrp = (struct mbr_partition *)&mbr[MBR_PART_OFFSET];
    231  1.1  garbled 
    232  1.1  garbled 	/* Set entry point and boot image size skipping over elf header */
    233  1.1  garbled 	entry  = sa_htole32(0x400);
    234  1.1  garbled 	length = sa_htole32(elf_stat.st_size - sizeof(hdr) + 0x400);
    235  1.1  garbled 
    236  1.1  garbled 	/*
    237  1.1  garbled 	 * Set magic number for msdos partition
    238  1.1  garbled 	 */
    239  1.1  garbled 	*(unsigned short *)&mbr[MBR_MAGIC_OFFSET] = sa_htole16(MBR_MAGIC);
    240  1.1  garbled 
    241  1.1  garbled 	/*
    242  1.1  garbled 	 * Build a "PReP" partition table entry in the boot record
    243  1.1  garbled 	 *  - "PReP" may only look at the system_indicator
    244  1.1  garbled 	 */
    245  1.1  garbled 	mbrp->mbrp_flag = MBR_PFLAG_ACTIVE;
    246  1.1  garbled 	mbrp->mbrp_type  = MBR_PTYPE_PREP;
    247  1.1  garbled 
    248  1.1  garbled 	/*
    249  1.1  garbled 	 * The first block of the diskette is used by this "boot record" which
    250  1.1  garbled 	 * actually contains the partition table. (The first block of the
    251  1.1  garbled 	 * partition contains the boot image, but I digress...)  We'll set up
    252  1.1  garbled 	 * one partition on the diskette and it shall contain the rest of the
    253  1.1  garbled 	 * diskette.
    254  1.1  garbled 	 */
    255  1.1  garbled 	mbrp->mbrp_shd   = 0;	/* zero-based			     */
    256  1.1  garbled 	mbrp->mbrp_ssect = 2;	/* one-based			     */
    257  1.1  garbled 	mbrp->mbrp_scyl  = 0;	/* zero-based			     */
    258  1.1  garbled 	mbrp->mbrp_ehd   = 1;	/* assumes two heads		     */
    259  1.1  garbled 	if (lfloppyflag)
    260  1.1  garbled 		mbrp->mbrp_esect = 36;  /* 2.88MB floppy	     */
    261  1.1  garbled 	else
    262  1.1  garbled 		mbrp->mbrp_esect = 18;	/* assumes 18 sectors/track  */
    263  1.1  garbled 	mbrp->mbrp_ecyl  = 79;	/* assumes 80 cylinders/diskette     */
    264  1.1  garbled 
    265  1.1  garbled 	/*
    266  1.1  garbled 	 * The "PReP" software ignores the above fields and just looks at
    267  1.1  garbled 	 * the next two.
    268  1.1  garbled 	 *   - size of the diskette is (assumed to be)
    269  1.1  garbled 	 *     (2 tracks/cylinder)(18 sectors/tracks)(80 cylinders/diskette)
    270  1.1  garbled 	 *   - unlike the above sector numbers,
    271  1.1  garbled 	 *     the beginning sector is zero-based!
    272  1.1  garbled 	 */
    273  1.1  garbled 
    274  1.1  garbled 	/* This has to be 0 on the PowerStack? */
    275  1.1  garbled 	mbrp->mbrp_start = sa_htole32(0);
    276  1.1  garbled 	mbrp->mbrp_size  = sa_htole32(2 * 18 * 80 - 1);
    277  1.1  garbled 
    278  1.1  garbled 	write(prep_fd, mbr, sizeof(mbr));
    279  1.1  garbled 	write(prep_fd, &entry, sizeof(entry));
    280  1.1  garbled 	write(prep_fd, &length, sizeof(length));
    281  1.1  garbled }
    282  1.1  garbled 
    283  1.2  garbled static int
    284  1.4  garbled prep_build_image(char *kernel, char *boot, char *rawdev, char *outname)
    285  1.1  garbled {
    286  1.1  garbled 	unsigned char *elf_img = NULL, *kern_img = NULL;
    287  1.2  garbled 	int i, ch, tmp, kgzlen, err;
    288  1.1  garbled 	int elf_fd, prep_fd, kern_fd, elf_img_len = 0;
    289  1.1  garbled 	off_t lenpos, kstart, kend;
    290  1.1  garbled 	unsigned long length;
    291  1.1  garbled 	long flength;
    292  1.1  garbled 	gzFile gzf;
    293  1.1  garbled 	struct stat kern_stat;
    294  1.1  garbled 	Elf32_External_Phdr phdr;
    295  1.1  garbled 
    296  1.1  garbled 	elf_fd = open_file("bootloader", boot, &hdr, &elf_stat);
    297  1.1  garbled 	kern_fd = open_file("kernel", kernel, &khdr, &kern_stat);
    298  1.2  garbled 	kern_len = kern_stat.st_size + PREP_MAGICSIZE + KERNLENSIZE;
    299  1.1  garbled 
    300  1.1  garbled 	for (i = 0; i < ELFGET16(hdr.e_phnum); i++) {
    301  1.1  garbled 		lseek(elf_fd, ELFGET32(hdr.e_phoff) + sizeof(phdr) * i,
    302  1.1  garbled 			SEEK_SET);
    303  1.1  garbled 		if (read(elf_fd, &phdr, sizeof(phdr)) != sizeof(phdr))
    304  1.1  garbled 			errx(3, "Can't read input '%s' phdr : %s", boot,
    305  1.1  garbled 			    strerror(errno));
    306  1.1  garbled 
    307  1.1  garbled 		if ((ELFGET32(phdr.p_type) != PT_LOAD) ||
    308  1.1  garbled 		    !(ELFGET32(phdr.p_flags) & PF_X))
    309  1.1  garbled 			continue;
    310  1.1  garbled 
    311  1.1  garbled 		fstat(elf_fd, &elf_stat);
    312  1.1  garbled 		elf_img_len = elf_stat.st_size - ELFGET32(phdr.p_offset);
    313  1.1  garbled 		lseek(elf_fd, ELFGET32(phdr.p_offset), SEEK_SET);
    314  1.1  garbled 
    315  1.1  garbled 		break;
    316  1.1  garbled 	}
    317  1.2  garbled 	if ((prep_fd = open(outname, O_RDWR|O_TRUNC, 0)) < 0) {
    318  1.1  garbled 		/* we couldn't open it, it must be new */
    319  1.2  garbled 		prep_fd = creat(outname, 0644);
    320  1.1  garbled 		if (prep_fd < 0)
    321  1.2  garbled 			errx(2, "Can't open output '%s': %s", outname,
    322  1.1  garbled 			    strerror(errno));
    323  1.1  garbled 	}
    324  1.1  garbled 
    325  1.4  garbled 	prep_check_mbr(prep_fd, rawdev);
    326  1.1  garbled 
    327  1.1  garbled 	/* Set file pos. to 2nd sector where image will be written */
    328  1.1  garbled 	lseek(prep_fd, 0x400, SEEK_SET);
    329  1.1  garbled 
    330  1.1  garbled 	/* Copy boot image */
    331  1.1  garbled 	elf_img = (unsigned char *)malloc(elf_img_len);
    332  1.1  garbled 	if (!elf_img)
    333  1.1  garbled 		errx(3, "Can't malloc: %s", strerror(errno));
    334  1.1  garbled 	if (read(elf_fd, elf_img, elf_img_len) != elf_img_len)
    335  1.1  garbled 		errx(3, "Can't read file '%s' : %s", boot, strerror(errno));
    336  1.1  garbled 
    337  1.1  garbled 	write(prep_fd, elf_img, elf_img_len);
    338  1.1  garbled 	free(elf_img);
    339  1.1  garbled 
    340  1.1  garbled 	/* Copy kernel */
    341  1.1  garbled 	kern_img = (unsigned char *)malloc(kern_stat.st_size);
    342  1.1  garbled 
    343  1.1  garbled 	if (kern_img == NULL)
    344  1.1  garbled 		errx(3, "Can't malloc: %s", strerror(errno));
    345  1.1  garbled 
    346  1.1  garbled 	/* we need to jump back after having read the headers */
    347  1.1  garbled 	lseek(kern_fd, 0, SEEK_SET);
    348  1.1  garbled 	if (read(kern_fd, (void *)kern_img, kern_stat.st_size) !=
    349  1.1  garbled 	    kern_stat.st_size)
    350  1.1  garbled 		errx(3, "Can't read kernel '%s' : %s", kernel, strerror(errno));
    351  1.1  garbled 
    352  1.1  garbled 	gzf = gzdopen(dup(prep_fd), "a");
    353  1.1  garbled 	if (gzf == NULL)
    354  1.1  garbled 		errx(3, "Can't init compression: %s", strerror(errno));
    355  1.1  garbled 	if (gzsetparams(gzf, Z_BEST_COMPRESSION, Z_DEFAULT_STRATEGY) != Z_OK)
    356  1.1  garbled 		errx(3, "%s", gzerror(gzf, &err));
    357  1.1  garbled 
    358  1.1  garbled 	/* write a magic number and size before the kernel */
    359  1.2  garbled 	write(prep_fd, (void *)prep_magic, PREP_MAGICSIZE);
    360  1.1  garbled 	lenpos = lseek(prep_fd, 0, SEEK_CUR);
    361  1.1  garbled 	tmp = sa_htobe32(0);
    362  1.1  garbled 	write(prep_fd, (void *)&tmp, KERNLENSIZE);
    363  1.1  garbled 
    364  1.1  garbled 	/* write in the compressed kernel */
    365  1.1  garbled 	kstart = lseek(prep_fd, 0, SEEK_CUR);
    366  1.1  garbled 	kgzlen = gzwrite(gzf, kern_img, kern_stat.st_size);
    367  1.1  garbled 	gzclose(gzf);
    368  1.1  garbled 	kend = lseek(prep_fd, 0, SEEK_CUR);
    369  1.1  garbled 
    370  1.1  garbled 	/* jump back to the length position now that we know the length */
    371  1.1  garbled 	lseek(prep_fd, lenpos, SEEK_SET);
    372  1.1  garbled 	kgzlen = kend - kstart;
    373  1.1  garbled 	tmp = sa_htobe32(kgzlen);
    374  1.1  garbled 	write(prep_fd, (void *)&tmp, KERNLENSIZE);
    375  1.1  garbled 
    376  1.1  garbled 	length = sa_htole32(0x400 + elf_img_len + 8 + kgzlen);
    377  1.1  garbled 	lseek(prep_fd, sizeof(mbr) + 4, SEEK_SET);
    378  1.1  garbled 	write(prep_fd, &length, sizeof(length));
    379  1.1  garbled 
    380  1.1  garbled 	flength = 0x400 + elf_img_len + 8 + kgzlen;
    381  1.4  garbled 	if (lfloppyflag)
    382  1.1  garbled 		flength -= (5760 * 512);
    383  1.1  garbled 	else
    384  1.1  garbled 		flength -= (2880 * 512);
    385  1.1  garbled 	if (flength > 0 && !saloneflag)
    386  1.1  garbled 		fprintf(stderr, "%s: Image %s is %d bytes larger than single"
    387  1.1  garbled 		    " floppy. Can only be used for netboot.\n", getprogname(),
    388  1.2  garbled 		    outname, flength);
    389  1.1  garbled 
    390  1.1  garbled 	free(kern_img);
    391  1.1  garbled 	close(kern_fd);
    392  1.1  garbled 	close(prep_fd);
    393  1.1  garbled 	close(elf_fd);
    394  1.1  garbled 
    395  1.4  garbled 	return 0;
    396  1.2  garbled }
    397  1.2  garbled 
    398  1.4  garbled /* Fill in the needed information on the boot and config records.  Most of
    399  1.4  garbled  * this is just AIX garbage that we don't really need to boot.
    400  1.4  garbled  */
    401  1.4  garbled static void
    402  1.4  garbled rs6000_build_records(int img_len)
    403  1.4  garbled {
    404  1.4  garbled 	int bcl;
    405  1.4  garbled 
    406  1.4  garbled 	/* zero out all the fields, so we only have to set the ones
    407  1.4  garbled 	 * we care about, which are rather few.
    408  1.4  garbled 	 */
    409  1.4  garbled 	memset(&bootrec, 0, sizeof(rs6000_boot_record_t));
    410  1.4  garbled 	memset(&confrec, 0, sizeof(rs6000_config_record_t));
    411  1.4  garbled 
    412  1.4  garbled 	bootrec.ipl_record = IPLRECID;
    413  1.4  garbled 	bcl = img_len/512;
    414  1.4  garbled 	if (img_len%512 != 0)
    415  1.4  garbled 		bcl++;
    416  1.4  garbled 	bootrec.bootcode_len = bcl;
    417  1.4  garbled 	bootrec.bootcode_off = 0; /* XXX */
    418  1.4  garbled 	bootrec.bootpart_start = 2; /* skip bootrec and confrec */
    419  1.4  garbled 	bootrec.bootprg_start = 2;
    420  1.4  garbled 	bootrec.bootpart_len = bcl;
    421  1.4  garbled 	bootrec.boot_load_addr = 0x800000; /* XXX? */
    422  1.4  garbled 	bootrec.boot_frag = 1;
    423  1.4  garbled 	bootrec.boot_emul = 0x02; /* ?? */
    424  1.4  garbled 	/* service mode is a repeat of normal mode */
    425  1.4  garbled 	bootrec.servcode_len = bootrec.bootcode_len;
    426  1.4  garbled 	bootrec.servcode_off = bootrec.bootcode_off;
    427  1.4  garbled 	bootrec.servpart_start = bootrec.bootpart_start;
    428  1.4  garbled 	bootrec.servprg_start = bootrec.bootprg_start;
    429  1.4  garbled 	bootrec.servpart_len = bootrec.bootpart_len;
    430  1.4  garbled 	bootrec.serv_load_addr = bootrec.boot_load_addr;
    431  1.4  garbled 	bootrec.serv_frag = bootrec.boot_frag;
    432  1.4  garbled 	bootrec.serv_emul = bootrec.boot_emul;
    433  1.4  garbled 
    434  1.4  garbled 	/* now the config record */
    435  1.4  garbled 	confrec.conf_rec = CONFRECID;
    436  1.4  garbled 	confrec.sector_size = 0x02; /* 512 bytes */
    437  1.4  garbled 	confrec.last_cyl = 0x4f; /* 79 cyl, emulates floppy */
    438  1.4  garbled }
    439  1.4  garbled 
    440  1.4  garbled static int
    441  1.4  garbled rs6000_build_image(char *kernel, char *boot, char *rawdev, char *outname)
    442  1.4  garbled {
    443  1.4  garbled 	unsigned char *elf_img = NULL, *kern_img = NULL;
    444  1.4  garbled 	int i, ch, tmp, kgzlen, err;
    445  1.4  garbled 	int elf_fd, rs6000_fd, kern_fd, elf_img_len = 0, elf_pad;
    446  1.4  garbled 	uint32_t swapped[128];
    447  1.4  garbled 	off_t lenpos, kstart, kend;
    448  1.4  garbled 	unsigned long length;
    449  1.4  garbled 	long flength;
    450  1.4  garbled 	gzFile gzf;
    451  1.4  garbled 	struct stat kern_stat;
    452  1.4  garbled 	Elf32_External_Phdr phdr;
    453  1.4  garbled 
    454  1.4  garbled 	elf_fd = open_file("bootloader", boot, &hdr, &elf_stat);
    455  1.4  garbled 	kern_fd = open_file("kernel", kernel, &khdr, &kern_stat);
    456  1.4  garbled 	kern_len = kern_stat.st_size + RS6000_MAGICSIZE + KERNLENSIZE;
    457  1.4  garbled 
    458  1.4  garbled 	for (i = 0; i < ELFGET16(hdr.e_phnum); i++) {
    459  1.4  garbled 		lseek(elf_fd, ELFGET32(hdr.e_phoff) + sizeof(phdr) * i,
    460  1.4  garbled 			SEEK_SET);
    461  1.4  garbled 		if (read(elf_fd, &phdr, sizeof(phdr)) != sizeof(phdr))
    462  1.4  garbled 			errx(3, "Can't read input '%s' phdr : %s", boot,
    463  1.4  garbled 			    strerror(errno));
    464  1.4  garbled 
    465  1.4  garbled 		if ((ELFGET32(phdr.p_type) != PT_LOAD) ||
    466  1.4  garbled 		    !(ELFGET32(phdr.p_flags) & PF_X))
    467  1.4  garbled 			continue;
    468  1.4  garbled 
    469  1.4  garbled 		fstat(elf_fd, &elf_stat);
    470  1.4  garbled 		elf_img_len = elf_stat.st_size - ELFGET32(phdr.p_offset);
    471  1.4  garbled 		elf_pad = ELFGET32(phdr.p_memsz) - ELFGET32(phdr.p_filesz);
    472  1.4  garbled 		if (verboseflag)
    473  1.4  garbled 			printf("Padding %d\n", elf_pad);
    474  1.4  garbled 		lseek(elf_fd, ELFGET32(phdr.p_offset), SEEK_SET);
    475  1.4  garbled 
    476  1.4  garbled 		break;
    477  1.4  garbled 	}
    478  1.4  garbled 	if ((rs6000_fd = open(outname, O_RDWR|O_TRUNC, 0)) < 0) {
    479  1.4  garbled 		/* we couldn't open it, it must be new */
    480  1.4  garbled 		rs6000_fd = creat(outname, 0644);
    481  1.4  garbled 		if (rs6000_fd < 0)
    482  1.4  garbled 			errx(2, "Can't open output '%s': %s", outname,
    483  1.4  garbled 			    strerror(errno));
    484  1.4  garbled 	}
    485  1.4  garbled 
    486  1.4  garbled 	/* Set file pos. to 2nd sector where image will be written */
    487  1.4  garbled 	lseek(rs6000_fd, 0x400, SEEK_SET);
    488  1.4  garbled 
    489  1.4  garbled 	/* Copy boot image */
    490  1.4  garbled 	elf_img = (unsigned char *)malloc(elf_img_len);
    491  1.4  garbled 	if (!elf_img)
    492  1.4  garbled 		errx(3, "Can't malloc: %s", strerror(errno));
    493  1.4  garbled 	if (read(elf_fd, elf_img, elf_img_len) != elf_img_len)
    494  1.4  garbled 		errx(3, "Can't read file '%s' : %s", boot, strerror(errno));
    495  1.4  garbled 
    496  1.4  garbled 	write(rs6000_fd, elf_img, elf_img_len);
    497  1.4  garbled 	free(elf_img);
    498  1.4  garbled 
    499  1.4  garbled 	/* now dump in the padding space for the BSS */
    500  1.4  garbled 	elf_pad += 100; /* just a little extra for good luck */
    501  1.4  garbled 	lseek(rs6000_fd, elf_pad, SEEK_CUR);
    502  1.4  garbled 
    503  1.4  garbled 	/* Copy kernel */
    504  1.4  garbled 	kern_img = (unsigned char *)malloc(kern_stat.st_size);
    505  1.4  garbled 
    506  1.4  garbled 	if (kern_img == NULL)
    507  1.4  garbled 		errx(3, "Can't malloc: %s", strerror(errno));
    508  1.4  garbled 
    509  1.4  garbled 	/* we need to jump back after having read the headers */
    510  1.4  garbled 	lseek(kern_fd, 0, SEEK_SET);
    511  1.4  garbled 	if (read(kern_fd, (void *)kern_img, kern_stat.st_size) !=
    512  1.4  garbled 	    kern_stat.st_size)
    513  1.4  garbled 		errx(3, "Can't read kernel '%s' : %s", kernel, strerror(errno));
    514  1.4  garbled 
    515  1.4  garbled 	gzf = gzdopen(dup(rs6000_fd), "a");
    516  1.4  garbled 	if (gzf == NULL)
    517  1.4  garbled 		errx(3, "Can't init compression: %s", strerror(errno));
    518  1.4  garbled 	if (gzsetparams(gzf, Z_BEST_COMPRESSION, Z_DEFAULT_STRATEGY) != Z_OK)
    519  1.4  garbled 		errx(3, "%s", gzerror(gzf, &err));
    520  1.4  garbled 
    521  1.4  garbled 	/* write a magic number and size before the kernel */
    522  1.4  garbled 	write(rs6000_fd, (void *)rs6000_magic, RS6000_MAGICSIZE);
    523  1.4  garbled 	lenpos = lseek(rs6000_fd, 0, SEEK_CUR);
    524  1.4  garbled 	if (verboseflag)
    525  1.4  garbled 		printf("wrote magic at pos 0x%x\n", lenpos);
    526  1.4  garbled 	tmp = sa_htobe32(0);
    527  1.4  garbled 	write(rs6000_fd, (void *)&tmp, KERNLENSIZE);
    528  1.4  garbled 
    529  1.4  garbled 	/* write in the compressed kernel */
    530  1.4  garbled 	kstart = lseek(rs6000_fd, 0, SEEK_CUR);
    531  1.4  garbled 	if (verboseflag)
    532  1.4  garbled 		printf("kernel start at pos 0x%x\n", kstart);
    533  1.4  garbled 	kgzlen = gzwrite(gzf, kern_img, kern_stat.st_size);
    534  1.4  garbled 	gzclose(gzf);
    535  1.4  garbled 	kend = lseek(rs6000_fd, 0, SEEK_CUR);
    536  1.4  garbled 	if (verboseflag)
    537  1.4  garbled 		printf("kernel end at pos 0x%x\n", kend);
    538  1.4  garbled 
    539  1.4  garbled 	/* jump back to the length position now that we know the length */
    540  1.4  garbled 	lseek(rs6000_fd, lenpos, SEEK_SET);
    541  1.4  garbled 	kgzlen = kend - kstart;
    542  1.4  garbled 	tmp = sa_htobe32(kgzlen);
    543  1.4  garbled 	if (verboseflag)
    544  1.4  garbled 		printf("kernel len = 0x%x tmp = 0x%x\n", kgzlen, tmp);
    545  1.4  garbled 	write(rs6000_fd, (void *)&tmp, KERNLENSIZE);
    546  1.4  garbled 
    547  1.4  garbled #if 0
    548  1.4  garbled 	lseek(rs6000_fd, sizeof(boot_record_t) + sizeof(config_record_t),
    549  1.4  garbled 	    SEEK_SET);
    550  1.4  garbled 	/* set entry and length */
    551  1.4  garbled 	length = sa_htole32(0x400);
    552  1.4  garbled 	write(rs6000_fd, &length, sizeof(length));
    553  1.4  garbled 	length = sa_htole32(0x400 + elf_img_len + 8 + kgzlen);
    554  1.4  garbled 	write(rs6000_fd, &length, sizeof(length));
    555  1.4  garbled #endif
    556  1.4  garbled 
    557  1.4  garbled 	/* generate the header now that we know the kernel length */
    558  1.4  garbled 	if (verboseflag)
    559  1.4  garbled 		printf("building records\n");
    560  1.4  garbled 	rs6000_build_records(elf_img_len + 8 + kgzlen);
    561  1.4  garbled 	lseek(rs6000_fd, 0, SEEK_SET);
    562  1.4  garbled 	/* ROM wants it byteswapped in 32bit chunks */
    563  1.4  garbled 	if (verboseflag)
    564  1.4  garbled 		printf("writing records\n");
    565  1.4  garbled 	memcpy(swapped, &bootrec, sizeof(rs6000_boot_record_t));
    566  1.4  garbled 	for (i=0; i < 128; i++)
    567  1.4  garbled 		swapped[i] = htonl(swapped[i]);
    568  1.4  garbled 	write(rs6000_fd, swapped, sizeof(rs6000_boot_record_t));
    569  1.4  garbled 	memcpy(swapped, &confrec, sizeof(rs6000_config_record_t));
    570  1.4  garbled 	for (i=0; i < 128; i++)
    571  1.4  garbled 		swapped[i] = htonl(swapped[i]);
    572  1.4  garbled 	write(rs6000_fd, swapped, sizeof(rs6000_config_record_t));
    573  1.4  garbled 
    574  1.4  garbled 	free(kern_img);
    575  1.4  garbled 	close(kern_fd);
    576  1.4  garbled 	close(rs6000_fd);
    577  1.4  garbled 	close(elf_fd);
    578  1.4  garbled 
    579  1.4  garbled 	return 0;
    580  1.4  garbled }
    581  1.4  garbled 
    582  1.2  garbled int
    583  1.2  garbled main(int argc, char **argv)
    584  1.2  garbled {
    585  1.2  garbled 	int ch, lfloppyflag=0;
    586  1.2  garbled 	char *kernel = NULL, *boot = NULL, *rawdev = NULL, *outname = NULL;
    587  1.2  garbled 	char *march = NULL;
    588  1.2  garbled #ifdef __NetBSD__
    589  1.2  garbled 	char machine_arch[SYS_NMLN];
    590  1.2  garbled 	int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
    591  1.2  garbled #endif
    592  1.2  garbled 
    593  1.2  garbled 	setprogname(argv[0]);
    594  1.2  garbled 	kern_len = 0;
    595  1.2  garbled 
    596  1.4  garbled 	while ((ch = getopt(argc, argv, "b:k:lm:r:sv")) != -1)
    597  1.2  garbled 		switch (ch) {
    598  1.2  garbled 		case 'b':
    599  1.2  garbled 			boot = optarg;
    600  1.2  garbled 			break;
    601  1.2  garbled 		case 'k':
    602  1.2  garbled 			kernel = optarg;
    603  1.2  garbled 			break;
    604  1.2  garbled 		case 'l':
    605  1.2  garbled 			lfloppyflag = 1;
    606  1.2  garbled 			break;
    607  1.2  garbled 		case 'm':
    608  1.2  garbled 			march = optarg;
    609  1.2  garbled 			break;
    610  1.2  garbled 		case 'r':
    611  1.2  garbled 			rawdev = optarg;
    612  1.2  garbled 			break;
    613  1.2  garbled 		case 's':
    614  1.2  garbled 			saloneflag = 1;
    615  1.2  garbled 			break;
    616  1.4  garbled 		case 'v':
    617  1.4  garbled 			verboseflag = 1;
    618  1.4  garbled 			break;
    619  1.2  garbled 		case '?':
    620  1.2  garbled 		default:
    621  1.4  garbled 			usage(0);
    622  1.2  garbled 			/* NOTREACHED */
    623  1.2  garbled 		}
    624  1.2  garbled 	argc -= optind;
    625  1.2  garbled 	argv += optind;
    626  1.2  garbled 
    627  1.2  garbled 	if (argc < 1)
    628  1.4  garbled 		usage(0);
    629  1.2  garbled 
    630  1.2  garbled 	if (kernel == NULL)
    631  1.2  garbled 		kernel = "/netbsd";
    632  1.2  garbled 
    633  1.2  garbled 	if (boot == NULL)
    634  1.2  garbled 		boot = "/usr/mdec/boot";
    635  1.2  garbled 
    636  1.4  garbled 	if (strcmp(march, "") == 0)
    637  1.4  garbled 		march = NULL;
    638  1.2  garbled 	if (march == NULL) {
    639  1.2  garbled 		int i;
    640  1.2  garbled #ifdef __NetBSD__
    641  1.2  garbled 		size_t len = sizeof(machine_arch);
    642  1.2  garbled 
    643  1.2  garbled 		if (sysctl(mib, sizeof (mib) / sizeof (mib[0]), machine_arch,
    644  1.2  garbled 			&len, NULL, 0) != -1) {
    645  1.2  garbled 			for (i=0; sup_plats[i] != NULL; i++) {
    646  1.2  garbled 				if (strcmp(sup_plats[i], machine_arch) == 0) {
    647  1.2  garbled 					march = strdup(sup_plats[i]);
    648  1.2  garbled 					break;
    649  1.2  garbled 				}
    650  1.2  garbled 			}
    651  1.2  garbled 		}
    652  1.4  garbled 		if (march == NULL)
    653  1.2  garbled #endif
    654  1.4  garbled 			usage(1);
    655  1.2  garbled 	}
    656  1.2  garbled 
    657  1.2  garbled 	outname = argv[0];
    658  1.2  garbled 
    659  1.2  garbled 	if (strcmp(march, "prep") == 0)
    660  1.4  garbled 		return(prep_build_image(kernel, boot, rawdev, outname));
    661  1.4  garbled 	if (strcmp(march, "rs6000") == 0)
    662  1.4  garbled 		return(rs6000_build_image(kernel, boot, rawdev, outname));
    663  1.2  garbled 
    664  1.4  garbled 	usage(1);
    665  1.2  garbled 	return(0);
    666  1.1  garbled }
    667