Home | History | Annotate | Line # | Download | only in arch
hppa.c revision 1.1
      1  1.1  skrll /*	$NetBSD: hppa.c,v 1.1 2014/02/24 07:23:44 skrll Exp $	*/
      2  1.1  skrll 
      3  1.1  skrll /*-
      4  1.1  skrll  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  1.1  skrll  * All rights reserved.
      6  1.1  skrll  *
      7  1.1  skrll  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  skrll  * by Luke Mewburn of Wasabi Systems.
      9  1.1  skrll  *
     10  1.1  skrll  * Redistribution and use in source and binary forms, with or without
     11  1.1  skrll  * modification, are permitted provided that the following conditions
     12  1.1  skrll  * are met:
     13  1.1  skrll  * 1. Redistributions of source code must retain the above copyright
     14  1.1  skrll  *    notice, this list of conditions and the following disclaimer.
     15  1.1  skrll  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  skrll  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  skrll  *    documentation and/or other materials provided with the distribution.
     18  1.1  skrll  *
     19  1.1  skrll  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  skrll  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  skrll  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  skrll  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  skrll  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  skrll  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  skrll  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  skrll  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  skrll  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  skrll  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  skrll  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  skrll  */
     31  1.1  skrll 
     32  1.1  skrll #if HAVE_NBTOOL_CONFIG_H
     33  1.1  skrll #include "nbtool_config.h"
     34  1.1  skrll #endif
     35  1.1  skrll 
     36  1.1  skrll #include <sys/cdefs.h>
     37  1.1  skrll #if !defined(__lint)
     38  1.1  skrll __RCSID("$NetBSD: hppa.c,v 1.1 2014/02/24 07:23:44 skrll Exp $");
     39  1.1  skrll #endif	/* !__lint */
     40  1.1  skrll 
     41  1.1  skrll /* We need the target disklabel.h, not the hosts one..... */
     42  1.1  skrll #ifdef HAVE_NBTOOL_CONFIG_H
     43  1.1  skrll #include "nbtool_config.h"
     44  1.1  skrll #include <nbinclude/sys/disklabel.h>
     45  1.1  skrll #else
     46  1.1  skrll #include <sys/disklabel.h>
     47  1.1  skrll #endif
     48  1.1  skrll #include <sys/param.h>
     49  1.1  skrll #include <sys/stat.h>
     50  1.1  skrll 
     51  1.1  skrll #include <assert.h>
     52  1.1  skrll #include <err.h>
     53  1.1  skrll #include <stddef.h>
     54  1.1  skrll #include <stdio.h>
     55  1.1  skrll #include <stdlib.h>
     56  1.1  skrll #include <string.h>
     57  1.1  skrll #include <unistd.h>
     58  1.1  skrll 
     59  1.1  skrll #include "installboot.h"
     60  1.1  skrll 
     61  1.1  skrll #define HPPA_LABELOFFSET	512
     62  1.1  skrll #define HPPA_LABELSIZE		404 /* reserve 16 partitions */
     63  1.1  skrll #define	HPPA_BOOT_BLOCK_SIZE	8192
     64  1.1  skrll 
     65  1.1  skrll static int hppa_clearboot(ib_params *);
     66  1.1  skrll static int hppa_setboot(ib_params *);
     67  1.1  skrll 
     68  1.1  skrll struct ib_mach ib_mach_hppa =
     69  1.1  skrll 	{ "hppa", hppa_setboot, hppa_clearboot, no_editboot, 0};
     70  1.1  skrll 
     71  1.1  skrll static int
     72  1.1  skrll hppa_clearboot(ib_params *params)
     73  1.1  skrll {
     74  1.1  skrll 	char		bb[HPPA_BOOT_BLOCK_SIZE];
     75  1.1  skrll 	int		retval, eol;
     76  1.1  skrll 	ssize_t		rv;
     77  1.1  skrll 
     78  1.1  skrll 	assert(params != NULL);
     79  1.1  skrll 	assert(params->fsfd != -1);
     80  1.1  skrll 	assert(params->filesystem != NULL);
     81  1.1  skrll 
     82  1.1  skrll 	retval = 0;
     83  1.1  skrll 
     84  1.1  skrll 	/* read disklabel on the target disk */
     85  1.1  skrll 	rv = pread(params->fsfd, bb, sizeof bb, 0);
     86  1.1  skrll 	if (rv == -1) {
     87  1.1  skrll 		warn("Reading `%s'", params->filesystem);
     88  1.1  skrll 		goto done;
     89  1.1  skrll 	} else if (rv != sizeof bb) {
     90  1.1  skrll 		warnx("Reading `%s': short read", params->filesystem);
     91  1.1  skrll 		goto done;
     92  1.1  skrll 	}
     93  1.1  skrll 
     94  1.1  skrll 	/* clear header */
     95  1.1  skrll 	memset(bb, 0, HPPA_LABELOFFSET);
     96  1.1  skrll 	eol = HPPA_LABELOFFSET + HPPA_LABELSIZE;
     97  1.1  skrll 	memset(&bb[eol], 0, sizeof bb - eol);
     98  1.1  skrll 
     99  1.1  skrll 	if (params->flags & IB_VERBOSE) {
    100  1.1  skrll 		printf("%slearing bootstrap\n",
    101  1.1  skrll 		    (params->flags & IB_NOWRITE) ? "Not c" : "C");
    102  1.1  skrll 	}
    103  1.1  skrll 	if (params->flags & IB_NOWRITE) {
    104  1.1  skrll 		retval = 1;
    105  1.1  skrll 		goto done;
    106  1.1  skrll 	}
    107  1.1  skrll 
    108  1.1  skrll 	rv = pwrite(params->fsfd, bb, sizeof bb, 0);
    109  1.1  skrll 	if (rv == -1) {
    110  1.1  skrll 		warn("Writing `%s'", params->filesystem);
    111  1.1  skrll 		goto done;
    112  1.1  skrll 	} else if (rv != HPPA_BOOT_BLOCK_SIZE) {
    113  1.1  skrll 		warnx("Writing `%s': short write", params->filesystem);
    114  1.1  skrll 		goto done;
    115  1.1  skrll 	} else
    116  1.1  skrll 		retval = 1;
    117  1.1  skrll 
    118  1.1  skrll  done:
    119  1.1  skrll 	return (retval);
    120  1.1  skrll }
    121  1.1  skrll 
    122  1.1  skrll static int
    123  1.1  skrll hppa_setboot(ib_params *params)
    124  1.1  skrll {
    125  1.1  skrll 	struct stat	bootstrapsb;
    126  1.1  skrll 	char		bb[HPPA_BOOT_BLOCK_SIZE];
    127  1.1  skrll 	struct {
    128  1.1  skrll 		char	l_off[HPPA_LABELOFFSET];
    129  1.1  skrll 		struct disklabel l;
    130  1.1  skrll 		char	l_pad[HPPA_BOOT_BLOCK_SIZE
    131  1.1  skrll 			    - HPPA_LABELOFFSET - sizeof(struct disklabel)];
    132  1.1  skrll 	} label;
    133  1.1  skrll 	unsigned int	secsize, npart;
    134  1.1  skrll 	int		retval;
    135  1.1  skrll 	ssize_t		rv;
    136  1.1  skrll 
    137  1.1  skrll 	assert(params != NULL);
    138  1.1  skrll 	assert(params->fsfd != -1);
    139  1.1  skrll 	assert(params->filesystem != NULL);
    140  1.1  skrll 	assert(params->s1fd != -1);
    141  1.1  skrll 	assert(params->stage1 != NULL);
    142  1.1  skrll 
    143  1.1  skrll 	retval = 0;
    144  1.1  skrll 
    145  1.1  skrll 	/* read disklabel on the target disk */
    146  1.1  skrll 	rv = pread(params->fsfd, &label, HPPA_BOOT_BLOCK_SIZE, 0);
    147  1.1  skrll 	if (rv == -1) {
    148  1.1  skrll 		warn("Reading `%s'", params->filesystem);
    149  1.1  skrll 		goto done;
    150  1.1  skrll 	} else if (rv != HPPA_BOOT_BLOCK_SIZE) {
    151  1.1  skrll 		warnx("Reading `%s': short read", params->filesystem);
    152  1.1  skrll 		goto done;
    153  1.1  skrll 	}
    154  1.1  skrll 
    155  1.1  skrll 	if (fstat(params->s1fd, &bootstrapsb) == -1) {
    156  1.1  skrll 		warn("Examining `%s'", params->stage1);
    157  1.1  skrll 		goto done;
    158  1.1  skrll 	}
    159  1.1  skrll 	if (!S_ISREG(bootstrapsb.st_mode)) {
    160  1.1  skrll 		warnx("`%s' must be a regular file", params->stage1);
    161  1.1  skrll 		goto done;
    162  1.1  skrll 	}
    163  1.1  skrll 
    164  1.1  skrll 	/* check if valid disklabel exists */
    165  1.1  skrll 	secsize = be32toh(label.l.d_secsize);
    166  1.1  skrll 	npart = be16toh(label.l.d_npartitions);
    167  1.1  skrll 	if (label.l.d_magic != htobe32(DISKMAGIC) ||
    168  1.1  skrll 	    label.l.d_magic2 != htobe32(DISKMAGIC) ||
    169  1.1  skrll 	    secsize == 0 || secsize & (secsize - 1) ||
    170  1.1  skrll 	    npart > MAXMAXPARTITIONS) {
    171  1.1  skrll 		warnx("No disklabel in `%s'", params->filesystem);
    172  1.1  skrll 
    173  1.1  skrll 	/* then check if boot partition exists */
    174  1.1  skrll 	} else if (npart < 1 || label.l.d_partitions[0].p_size == 0) {
    175  1.1  skrll 		warnx("Partition `a' doesn't exist in %s", params->filesystem);
    176  1.1  skrll 
    177  1.1  skrll 	/* check if the boot partition is below 2GB */
    178  1.1  skrll 	} else if (be32toh(label.l.d_partitions[0].p_offset) +
    179  1.1  skrll 	    be32toh(label.l.d_partitions[0].p_size) >
    180  1.1  skrll 	    ((unsigned)2*1024*1024*1024) / secsize) {
    181  1.1  skrll 		warnx("Partition `a' of `%s' exceeds 2GB boundary.",
    182  1.1  skrll 		    params->filesystem);
    183  1.1  skrll 		warnx("It won't boot since hppa PDC can handle only 2GB.");
    184  1.1  skrll 		goto done;
    185  1.1  skrll 	}
    186  1.1  skrll 
    187  1.1  skrll 	/* read boot loader */
    188  1.1  skrll 	memset(&bb, 0, sizeof bb);
    189  1.1  skrll 	rv = read(params->s1fd, &bb, sizeof bb);
    190  1.1  skrll 	if (rv == -1) {
    191  1.1  skrll 		warn("Reading `%s'", params->stage1);
    192  1.1  skrll 		goto done;
    193  1.1  skrll 	}
    194  1.1  skrll 	/* then, overwrite disklabel */
    195  1.1  skrll 	memcpy(&bb[HPPA_LABELOFFSET], &label.l, HPPA_LABELSIZE);
    196  1.1  skrll 
    197  1.1  skrll 	if (params->flags & IB_VERBOSE) {
    198  1.1  skrll 		printf("Bootstrap start sector: %#x\n", 0);
    199  1.1  skrll 		printf("Bootstrap byte count:   %#zx\n", rv);
    200  1.1  skrll 		printf("%sriting bootstrap\n",
    201  1.1  skrll 		    (params->flags & IB_NOWRITE) ? "Not w" : "W");
    202  1.1  skrll 	}
    203  1.1  skrll 	if (params->flags & IB_NOWRITE) {
    204  1.1  skrll 		retval = 1;
    205  1.1  skrll 		goto done;
    206  1.1  skrll 	}
    207  1.1  skrll 
    208  1.1  skrll 	/* write boot loader and disklabel into the target disk */
    209  1.1  skrll 	rv = pwrite(params->fsfd, &bb, HPPA_BOOT_BLOCK_SIZE, 0);
    210  1.1  skrll 	if (rv == -1) {
    211  1.1  skrll 		warn("Writing `%s'", params->filesystem);
    212  1.1  skrll 		goto done;
    213  1.1  skrll 	} else if (rv != HPPA_BOOT_BLOCK_SIZE) {
    214  1.1  skrll 		warnx("Writing `%s': short write", params->filesystem);
    215  1.1  skrll 		goto done;
    216  1.1  skrll 	} else
    217  1.1  skrll 		retval = 1;
    218  1.1  skrll 
    219  1.1  skrll  done:
    220  1.1  skrll 	return (retval);
    221  1.1  skrll }
    222