Home | History | Annotate | Line # | Download | only in arch
pmax.c revision 1.3
      1  1.3    lukem /*	$NetBSD: pmax.c,v 1.3 2002/04/12 06:50:41 lukem Exp $	*/
      2  1.1    lukem 
      3  1.1    lukem /*-
      4  1.1    lukem  * Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
      5  1.1    lukem  * All rights reserved.
      6  1.1    lukem  *
      7  1.1    lukem  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1    lukem  * by Simon Burge.
      9  1.1    lukem  *
     10  1.1    lukem  * This code is derived from software contributed to The NetBSD Foundation
     11  1.1    lukem  * by Luke Mewburn of Wasabi Systems.
     12  1.1    lukem  *
     13  1.1    lukem  * Redistribution and use in source and binary forms, with or without
     14  1.1    lukem  * modification, are permitted provided that the following conditions
     15  1.1    lukem  * are met:
     16  1.1    lukem  * 1. Redistributions of source code must retain the above copyright
     17  1.1    lukem  *    notice, this list of conditions and the following disclaimer.
     18  1.1    lukem  * 2. Redistributions in binary form must reproduce the above copyright
     19  1.1    lukem  *    notice, this list of conditions and the following disclaimer in the
     20  1.1    lukem  *    documentation and/or other materials provided with the distribution.
     21  1.1    lukem  * 3. All advertising materials mentioning features or use of this software
     22  1.1    lukem  *    must display the following acknowledgement:
     23  1.1    lukem  *	This product includes software developed by the NetBSD
     24  1.1    lukem  *	Foundation, Inc. and its contributors.
     25  1.1    lukem  * 4. Neither the name of The NetBSD Foundation nor the names of its
     26  1.1    lukem  *    contributors may be used to endorse or promote products derived
     27  1.1    lukem  *    from this software without specific prior written permission.
     28  1.1    lukem  *
     29  1.1    lukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     30  1.1    lukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     31  1.1    lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     32  1.1    lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     33  1.1    lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     34  1.1    lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     35  1.1    lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     36  1.1    lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     37  1.1    lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     38  1.1    lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     39  1.1    lukem  * POSSIBILITY OF SUCH DAMAGE.
     40  1.1    lukem  */
     41  1.1    lukem 
     42  1.1    lukem /*
     43  1.1    lukem  * Copyright (c) 1999 Ross Harvey.  All rights reserved.
     44  1.1    lukem  *
     45  1.1    lukem  * Redistribution and use in source and binary forms, with or without
     46  1.1    lukem  * modification, are permitted provided that the following conditions
     47  1.1    lukem  * are met:
     48  1.1    lukem  * 1. Redistributions of source code must retain the above copyright
     49  1.1    lukem  *    notice, this list of conditions and the following disclaimer.
     50  1.1    lukem  * 2. Redistributions in binary form must reproduce the above copyright
     51  1.1    lukem  *    notice, this list of conditions and the following disclaimer in the
     52  1.1    lukem  *    documentation and/or other materials provided with the distribution.
     53  1.1    lukem  * 3. All advertising materials mentioning features or use of this software
     54  1.1    lukem  *    must display the following acknowledgement:
     55  1.1    lukem  *      This product includes software developed by Ross Harvey
     56  1.1    lukem  *	for the NetBSD Project.
     57  1.1    lukem  * 4. The name of the author may not be used to endorse or promote products
     58  1.1    lukem  *    derived from this software without specific prior written permission
     59  1.1    lukem  *
     60  1.1    lukem  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     61  1.1    lukem  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     62  1.1    lukem  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     63  1.1    lukem  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     64  1.1    lukem  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     65  1.1    lukem  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     66  1.1    lukem  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     67  1.1    lukem  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     68  1.1    lukem  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     69  1.1    lukem  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     70  1.1    lukem  */
     71  1.1    lukem 
     72  1.1    lukem /*
     73  1.1    lukem  * Copyright (c) 1999 Christopher G. Demetriou.  All rights reserved.
     74  1.1    lukem  *
     75  1.1    lukem  * Redistribution and use in source and binary forms, with or without
     76  1.1    lukem  * modification, are permitted provided that the following conditions
     77  1.1    lukem  * are met:
     78  1.1    lukem  * 1. Redistributions of source code must retain the above copyright
     79  1.1    lukem  *    notice, this list of conditions and the following disclaimer.
     80  1.1    lukem  * 2. Redistributions in binary form must reproduce the above copyright
     81  1.1    lukem  *    notice, this list of conditions and the following disclaimer in the
     82  1.1    lukem  *    documentation and/or other materials provided with the distribution.
     83  1.1    lukem  * 3. All advertising materials mentioning features or use of this software
     84  1.1    lukem  *    must display the following acknowledgement:
     85  1.1    lukem  *      This product includes software developed by Christopher G. Demetriou
     86  1.1    lukem  *	for the NetBSD Project.
     87  1.1    lukem  * 4. The name of the author may not be used to endorse or promote products
     88  1.1    lukem  *    derived from this software without specific prior written permission
     89  1.1    lukem  *
     90  1.1    lukem  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     91  1.1    lukem  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     92  1.1    lukem  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     93  1.1    lukem  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     94  1.1    lukem  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     95  1.1    lukem  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     96  1.1    lukem  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     97  1.1    lukem  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     98  1.1    lukem  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     99  1.1    lukem  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    100  1.1    lukem  */
    101  1.1    lukem 
    102  1.1    lukem #include <sys/cdefs.h>
    103  1.1    lukem #if defined(__RCSID) && !defined(__lint)
    104  1.3    lukem __RCSID("$NetBSD: pmax.c,v 1.3 2002/04/12 06:50:41 lukem Exp $");
    105  1.1    lukem #endif	/* !__lint */
    106  1.1    lukem 
    107  1.1    lukem #include <sys/param.h>
    108  1.1    lukem #include <sys/stat.h>
    109  1.1    lukem 
    110  1.1    lukem #include <assert.h>
    111  1.1    lukem #include <err.h>
    112  1.1    lukem #include <stddef.h>
    113  1.1    lukem #include <stdio.h>
    114  1.1    lukem #include <stdlib.h>
    115  1.2  thorpej #include <string.h>
    116  1.1    lukem #include <unistd.h>
    117  1.1    lukem 
    118  1.1    lukem #include <sys/exec_elf.h>
    119  1.1    lukem #include <dev/dec/dec_boot.h>
    120  1.1    lukem 
    121  1.1    lukem #include "installboot.h"
    122  1.1    lukem 
    123  1.1    lukem int		pmax_parseopt(ib_params *, const char *);
    124  1.1    lukem int		pmax_setboot(ib_params *);
    125  1.1    lukem int		pmax_clearboot(ib_params *);
    126  1.1    lukem static int	load_bootstrap(ib_params *, char **,
    127  1.1    lukem 				u_int32_t *, u_int32_t *, size_t *);
    128  1.1    lukem 
    129  1.1    lukem 
    130  1.1    lukem int
    131  1.1    lukem pmax_parseopt(ib_params *params, const char *option)
    132  1.1    lukem {
    133  1.1    lukem 
    134  1.1    lukem 	if (parseoptionflag(params, option, IB_APPEND | IB_SUNSUM))
    135  1.1    lukem 		return (1);
    136  1.1    lukem 
    137  1.1    lukem 	warnx("Unknown -o option `%s'", option);
    138  1.1    lukem 	return (0);
    139  1.1    lukem }
    140  1.1    lukem 
    141  1.1    lukem int
    142  1.1    lukem pmax_clearboot(ib_params *params)
    143  1.1    lukem {
    144  1.1    lukem 	struct pmax_boot_block	bb;
    145  1.1    lukem 	ssize_t			rv;
    146  1.1    lukem 
    147  1.1    lukem 	assert(params != NULL);
    148  1.1    lukem 	assert(params->fsfd != -1);
    149  1.1    lukem 	assert(params->filesystem != NULL);
    150  1.1    lukem 	assert(sizeof(struct pmax_boot_block) == PMAX_BOOT_BLOCK_BLOCKSIZE);
    151  1.1    lukem 
    152  1.1    lukem 	if (params->flags & (IB_STARTBLOCK | IB_APPEND)) {
    153  1.1    lukem 		warnx("Can't use `-b bno' or `-o append' with `-c'");
    154  1.1    lukem 		return (0);
    155  1.1    lukem 	}
    156  1.1    lukem 	rv = pread(params->fsfd, &bb, sizeof(bb), PMAX_BOOT_BLOCK_OFFSET);
    157  1.1    lukem 	if (rv == -1) {
    158  1.1    lukem 		warn("Reading `%s'", params->filesystem);
    159  1.1    lukem 		return (0);
    160  1.1    lukem 	} else if (rv != sizeof(bb)) {
    161  1.1    lukem 		warnx("Reading `%s': short read", params->filesystem);
    162  1.1    lukem 		return (0);
    163  1.1    lukem 	}
    164  1.1    lukem 
    165  1.1    lukem 	if (le32toh(bb.magic) != PMAX_BOOT_MAGIC) {
    166  1.1    lukem 		warnx(
    167  1.1    lukem 		    "Old boot block magic number invalid; boot block invalid");
    168  1.1    lukem 		return (0);
    169  1.1    lukem 	}
    170  1.1    lukem 
    171  1.1    lukem 	bb.map[0].num_blocks = bb.map[0].start_block = bb.mode = 0;
    172  1.1    lukem 	bb.magic = htole32(PMAX_BOOT_MAGIC);
    173  1.1    lukem 
    174  1.1    lukem 	if (params->flags & IB_SUNSUM) {
    175  1.1    lukem 		u_int16_t	sum;
    176  1.1    lukem 
    177  1.1    lukem 		sum = compute_sunsum((u_int16_t *)&bb);
    178  1.1    lukem 		if (! set_sunsum(params, (u_int16_t *)&bb, sum))
    179  1.1    lukem 			return (0);
    180  1.1    lukem 	}
    181  1.1    lukem 
    182  1.1    lukem 	if (params->flags & IB_VERBOSE)
    183  1.1    lukem 		printf("%slearing boot block\n",
    184  1.1    lukem 		    (params->flags & IB_NOWRITE) ? "Not c" : "C");
    185  1.1    lukem 	if (params->flags & IB_NOWRITE)
    186  1.1    lukem 		return (1);
    187  1.1    lukem 
    188  1.1    lukem 	rv = pwrite(params->fsfd, &bb, sizeof(bb), PMAX_BOOT_BLOCK_OFFSET);
    189  1.1    lukem 	if (rv == -1) {
    190  1.1    lukem 		warn("Writing `%s'", params->filesystem);
    191  1.1    lukem 		return (0);
    192  1.1    lukem 	} else if (rv != sizeof(bb)) {
    193  1.1    lukem 		warnx("Writing `%s': short write", params->filesystem);
    194  1.1    lukem 		return (0);
    195  1.1    lukem 	}
    196  1.1    lukem 
    197  1.1    lukem 	return (1);
    198  1.1    lukem }
    199  1.1    lukem 
    200  1.1    lukem int
    201  1.1    lukem pmax_setboot(ib_params *params)
    202  1.1    lukem {
    203  1.1    lukem 	struct stat		bootstrapsb;
    204  1.1    lukem 	struct pmax_boot_block	bb;
    205  1.1    lukem 	int			startblock, retval;
    206  1.1    lukem 	char			*bootstrapbuf;
    207  1.1    lukem 	size_t			bootstrapsize;
    208  1.1    lukem 	u_int32_t		bootstrapload, bootstrapexec;
    209  1.1    lukem 	ssize_t			rv;
    210  1.1    lukem 
    211  1.1    lukem 	assert(params != NULL);
    212  1.1    lukem 	assert(params->fsfd != -1);
    213  1.1    lukem 	assert(params->filesystem != NULL);
    214  1.3    lukem 	assert(params->s1fd != -1);
    215  1.3    lukem 	assert(params->stage1 != NULL);
    216  1.1    lukem 	assert(sizeof(struct pmax_boot_block) == PMAX_BOOT_BLOCK_BLOCKSIZE);
    217  1.1    lukem 
    218  1.1    lukem 	retval = 0;
    219  1.1    lukem 	bootstrapbuf = NULL;
    220  1.1    lukem 
    221  1.1    lukem 	if ((params->flags & IB_STARTBLOCK) &&
    222  1.1    lukem 	    (params->flags & IB_APPEND)) {
    223  1.1    lukem 		warnx("Can't use `-b bno' with `-o append'");
    224  1.1    lukem 		goto done;
    225  1.1    lukem 	}
    226  1.1    lukem 
    227  1.3    lukem 	if (fstat(params->s1fd, &bootstrapsb) == -1) {
    228  1.3    lukem 		warn("Examining `%s'", params->stage1);
    229  1.1    lukem 		goto done;
    230  1.1    lukem 	}
    231  1.1    lukem 	if (!S_ISREG(bootstrapsb.st_mode)) {
    232  1.3    lukem 		warnx("`%s' must be a regular file", params->stage1);
    233  1.1    lukem 		goto done;
    234  1.1    lukem 	}
    235  1.1    lukem 	if (! load_bootstrap(params, &bootstrapbuf, &bootstrapload,
    236  1.1    lukem 	    &bootstrapexec, &bootstrapsize))
    237  1.1    lukem 		goto done;
    238  1.1    lukem 
    239  1.1    lukem 	rv = pread(params->fsfd, &bb, sizeof(bb), PMAX_BOOT_BLOCK_OFFSET);
    240  1.1    lukem 	if (rv == -1) {
    241  1.1    lukem 		warn("Reading `%s'", params->filesystem);
    242  1.1    lukem 		goto done;
    243  1.1    lukem 	} else if (rv != sizeof(bb)) {
    244  1.1    lukem 		warnx("Reading `%s': short read", params->filesystem);
    245  1.1    lukem 		goto done;
    246  1.1    lukem 	}
    247  1.1    lukem 
    248  1.1    lukem 		/* fill in the updated boot block fields */
    249  1.1    lukem 	if (params->flags & IB_APPEND) {
    250  1.1    lukem 		struct stat	filesyssb;
    251  1.1    lukem 
    252  1.1    lukem 		if (fstat(params->fsfd, &filesyssb) == -1) {
    253  1.1    lukem 			warn("Examining `%s'", params->filesystem);
    254  1.1    lukem 			goto done;
    255  1.1    lukem 		}
    256  1.1    lukem 		if (!S_ISREG(filesyssb.st_mode)) {
    257  1.1    lukem 			warnx(
    258  1.3    lukem 		    "`%s' must be a regular file to append a bootstrap",
    259  1.1    lukem 			    params->filesystem);
    260  1.1    lukem 			goto done;
    261  1.1    lukem 		}
    262  1.1    lukem 		startblock = howmany(filesyssb.st_size,
    263  1.1    lukem 		    PMAX_BOOT_BLOCK_BLOCKSIZE);
    264  1.1    lukem 	} else if (params->flags & IB_STARTBLOCK) {
    265  1.1    lukem 		startblock = params->startblock;
    266  1.1    lukem 	} else {
    267  1.1    lukem 		startblock = PMAX_BOOT_BLOCK_OFFSET / PMAX_BOOT_BLOCK_BLOCKSIZE
    268  1.1    lukem 		    + 1;
    269  1.1    lukem 	}
    270  1.1    lukem 
    271  1.1    lukem 	bb.map[0].start_block = htole32(startblock);
    272  1.1    lukem 	bb.map[0].num_blocks =
    273  1.1    lukem 	    htole32(howmany(bootstrapsize, PMAX_BOOT_BLOCK_BLOCKSIZE));
    274  1.1    lukem 	bb.magic = htole32(PMAX_BOOT_MAGIC);
    275  1.1    lukem 	bb.load_addr = htole32(bootstrapload);
    276  1.1    lukem 	bb.exec_addr = htole32(bootstrapexec);
    277  1.1    lukem 	bb.mode = htole32(PMAX_BOOTMODE_CONTIGUOUS);
    278  1.1    lukem 
    279  1.1    lukem 	if (params->flags & IB_SUNSUM) {
    280  1.1    lukem 		u_int16_t	sum;
    281  1.1    lukem 
    282  1.1    lukem 		sum = compute_sunsum((u_int16_t *)&bb);
    283  1.1    lukem 		if (! set_sunsum(params, (u_int16_t *)&bb, sum))
    284  1.1    lukem 			goto done;
    285  1.1    lukem 	}
    286  1.1    lukem 
    287  1.1    lukem 	if (params->flags & IB_VERBOSE) {
    288  1.1    lukem 		printf("Bootstrap start sector: %#x\n",
    289  1.1    lukem 		    le32toh(bb.map[0].start_block));
    290  1.1    lukem 		printf("Bootstrap sector count: %#x\n",
    291  1.1    lukem 		    le32toh(bb.map[0].num_blocks));
    292  1.1    lukem 		printf("Bootstrap load address: %#x\n",
    293  1.1    lukem 		    le32toh(bb.load_addr));
    294  1.1    lukem 		printf("Bootstrap exec address: %#x\n",
    295  1.1    lukem 		    le32toh(bb.exec_addr));
    296  1.1    lukem 		printf("%sriting bootstrap\n",
    297  1.1    lukem 		    (params->flags & IB_NOWRITE) ? "Not w" : "W");
    298  1.1    lukem 	}
    299  1.1    lukem 	if (params->flags & IB_NOWRITE) {
    300  1.1    lukem 		retval = 1;
    301  1.1    lukem 		goto done;
    302  1.1    lukem 	}
    303  1.1    lukem 	rv = pwrite(params->fsfd, bootstrapbuf, bootstrapsize,
    304  1.1    lukem 	     startblock * PMAX_BOOT_BLOCK_BLOCKSIZE);
    305  1.1    lukem 	if (rv == -1) {
    306  1.1    lukem 		warn("Writing `%s'", params->filesystem);
    307  1.1    lukem 		goto done;
    308  1.1    lukem 	} else if (rv != bootstrapsize) {
    309  1.1    lukem 		warnx("Writing `%s': short write", params->filesystem);
    310  1.1    lukem 		goto done;
    311  1.1    lukem 	}
    312  1.1    lukem 
    313  1.1    lukem 	if (params->flags & IB_VERBOSE)
    314  1.1    lukem 		printf("Writing boot block\n");
    315  1.1    lukem 	rv = pwrite(params->fsfd, &bb, sizeof(bb), PMAX_BOOT_BLOCK_OFFSET);
    316  1.1    lukem 	if (rv == -1) {
    317  1.1    lukem 		warn("Writing `%s'", params->filesystem);
    318  1.1    lukem 		goto done;
    319  1.1    lukem 	} else if (rv != sizeof(bb)) {
    320  1.1    lukem 		warnx("Writing `%s': short write", params->filesystem);
    321  1.1    lukem 		goto done;
    322  1.1    lukem 	} else {
    323  1.1    lukem 		retval = 1;
    324  1.1    lukem 	}
    325  1.1    lukem 
    326  1.1    lukem  done:
    327  1.1    lukem 	if (bootstrapbuf)
    328  1.1    lukem 		free(bootstrapbuf);
    329  1.1    lukem 	return (retval);
    330  1.1    lukem }
    331  1.1    lukem 
    332  1.1    lukem 
    333  1.1    lukem #define MAX_SEGMENTS	10	/* We can load up to 10 segments */
    334  1.1    lukem 
    335  1.1    lukem struct seglist {
    336  1.1    lukem 	Elf32_Addr	addr;
    337  1.1    lukem 	Elf32_Off	f_offset;
    338  1.1    lukem 	Elf32_Word	f_size;
    339  1.1    lukem };
    340  1.1    lukem 
    341  1.1    lukem static int
    342  1.1    lukem load_bootstrap(ib_params *params, char **data,
    343  1.1    lukem 	u_int32_t *loadaddr, u_int32_t *execaddr, size_t *len)
    344  1.1    lukem {
    345  1.1    lukem 	int		i, nsegs;
    346  1.1    lukem 	Elf32_Addr	lowaddr, highaddr;
    347  1.1    lukem 	Elf32_Ehdr	ehdr;
    348  1.1    lukem 	Elf32_Phdr	phdr;
    349  1.1    lukem 	struct seglist	seglist[MAX_SEGMENTS];
    350  1.1    lukem 
    351  1.3    lukem 	if ((pread(params->s1fd, &ehdr, sizeof(ehdr), 0)) != sizeof(ehdr)) {
    352  1.3    lukem 		warn("Reading `%s'", params->stage1);
    353  1.1    lukem 		return (0);
    354  1.1    lukem 	}
    355  1.1    lukem 	if ((memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) ||
    356  1.1    lukem 	    (ehdr.e_ident[EI_CLASS] != ELFCLASS32)) {
    357  1.3    lukem 		warnx("No ELF header in `%s'", params->stage1);
    358  1.1    lukem 		return (0);
    359  1.1    lukem 	}
    360  1.1    lukem 
    361  1.1    lukem 	nsegs = highaddr = 0;
    362  1.1    lukem 	lowaddr = (u_int32_t) ULONG_MAX;
    363  1.1    lukem 
    364  1.1    lukem 	for (i = 0; i < le16toh(ehdr.e_phnum); i++) {
    365  1.3    lukem 		if (pread(params->s1fd, &phdr, sizeof(phdr),
    366  1.1    lukem 		    (off_t) le32toh(ehdr.e_phoff) + i * sizeof(phdr))
    367  1.1    lukem 		    != sizeof(phdr)) {
    368  1.3    lukem 			warn("Reading `%s'", params->stage1);
    369  1.1    lukem 			return (0);
    370  1.1    lukem 		}
    371  1.1    lukem 		if (le32toh(phdr.p_type) != PT_LOAD)
    372  1.1    lukem 			continue;
    373  1.1    lukem 
    374  1.1    lukem 		seglist[nsegs].addr = le32toh(phdr.p_paddr);
    375  1.1    lukem 		seglist[nsegs].f_offset = le32toh(phdr.p_offset);
    376  1.1    lukem 		seglist[nsegs].f_size = le32toh(phdr.p_filesz);
    377  1.1    lukem 		nsegs++;
    378  1.1    lukem 
    379  1.1    lukem 		if (le32toh(phdr.p_paddr) < lowaddr)
    380  1.1    lukem 			lowaddr = le32toh(phdr.p_paddr);
    381  1.1    lukem 		if (le32toh(phdr.p_paddr) + le32toh(phdr.p_filesz) > highaddr)
    382  1.1    lukem 			highaddr = le32toh(phdr.p_paddr) +
    383  1.1    lukem 			    le32toh(phdr.p_filesz);
    384  1.1    lukem 	}
    385  1.1    lukem 
    386  1.1    lukem 	*loadaddr = lowaddr;
    387  1.1    lukem 	*execaddr = le32toh(ehdr.e_entry);
    388  1.1    lukem 	*len = roundup(highaddr - lowaddr, PMAX_BOOT_BLOCK_BLOCKSIZE);
    389  1.1    lukem 	if ((*data = malloc(*len)) == NULL) {
    390  1.1    lukem 		warn("Allocating %lu bytes", (unsigned long) *len);
    391  1.1    lukem 		return (0);
    392  1.1    lukem 	}
    393  1.1    lukem 
    394  1.1    lukem 	/* Now load the bootstrap into memory */
    395  1.1    lukem 	for (i = 0; i < nsegs; i++) {
    396  1.3    lukem 		if (pread(params->s1fd, *data + seglist[i].addr - lowaddr,
    397  1.1    lukem 		    seglist[i].f_size, (off_t)seglist[i].f_offset)
    398  1.1    lukem 		    != seglist[i].f_size) {
    399  1.3    lukem 			warn("Reading `%s'", params->stage1);
    400  1.1    lukem 			return (0);
    401  1.1    lukem 		}
    402  1.1    lukem 	}
    403  1.1    lukem 	return (1);
    404  1.1    lukem }
    405