Home | History | Annotate | Line # | Download | only in arch
hp300.c revision 1.19.2.1
      1  1.19.2.1  perseant /* $NetBSD: hp300.c,v 1.19.2.1 2025/08/02 05:58:49 perseant Exp $ */
      2       1.1       dsl 
      3       1.1       dsl /*-
      4       1.1       dsl  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5       1.1       dsl  * All rights reserved.
      6       1.1       dsl  *
      7       1.1       dsl  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1       dsl  * by David Laight.
      9       1.1       dsl  *
     10       1.1       dsl  * Redistribution and use in source and binary forms, with or without
     11       1.1       dsl  * modification, are permitted provided that the following conditions
     12       1.1       dsl  * are met:
     13       1.1       dsl  * 1. Redistributions of source code must retain the above copyright
     14       1.1       dsl  *    notice, this list of conditions and the following disclaimer.
     15       1.1       dsl  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       dsl  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       dsl  *    documentation and/or other materials provided with the distribution.
     18       1.1       dsl  *
     19       1.1       dsl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1       dsl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1       dsl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1       dsl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1       dsl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1       dsl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1       dsl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1       dsl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1       dsl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1       dsl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1       dsl  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1       dsl  */
     31       1.1       dsl 
     32       1.1       dsl #if HAVE_NBTOOL_CONFIG_H
     33       1.1       dsl #include "nbtool_config.h"
     34       1.1       dsl #endif
     35       1.1       dsl 
     36       1.1       dsl #include <sys/cdefs.h>
     37       1.1       dsl #if !defined(__lint)
     38  1.19.2.1  perseant __RCSID("$NetBSD: hp300.c,v 1.19.2.1 2025/08/02 05:58:49 perseant Exp $");
     39       1.1       dsl #endif /* !__lint */
     40       1.1       dsl 
     41       1.3       dsl /* We need the target disklabel.h, not the hosts one..... */
     42       1.3       dsl #ifdef HAVE_NBTOOL_CONFIG_H
     43       1.3       dsl #include "nbtool_config.h"
     44      1.14      matt #include <nbinclude/hp300/disklabel.h>
     45       1.6    dyoung #include <nbinclude/sys/disklabel.h>
     46      1.15   msaitoh #else
     47       1.1       dsl #include <sys/disklabel.h>
     48       1.3       dsl #endif
     49       1.1       dsl #include <sys/fcntl.h>
     50       1.1       dsl #include <sys/ioctl.h>
     51       1.1       dsl #include <sys/mman.h>
     52       1.1       dsl #include <sys/param.h>
     53       1.1       dsl 
     54       1.1       dsl #include <assert.h>
     55       1.1       dsl #include <err.h>
     56       1.1       dsl #include <md5.h>
     57       1.1       dsl #include <stddef.h>
     58       1.1       dsl #include <stdio.h>
     59       1.1       dsl #include <stdlib.h>
     60       1.1       dsl #include <string.h>
     61       1.1       dsl #include <unistd.h>
     62       1.1       dsl 
     63       1.1       dsl #include "installboot.h"
     64       1.1       dsl 
     65      1.19   tsutsui #define HP300_MAXBLOCKS	1	/* Only contiguous blocks are expected. */
     66      1.19   tsutsui #define LIF_VOLDIRSIZE	1024	/* size of LIF volume header and directory */
     67      1.19   tsutsui 
     68  1.19.2.1  perseant /* XXX: should be HP300_LIF_FILESTART? */
     69  1.19.2.1  perseant #define LIF_PRESERVED	(HP300_SECTSIZE * 16)
     70  1.19.2.1  perseant 
     71       1.7       dsl static int hp300_setboot(ib_params *);
     72       1.7       dsl 
     73      1.16   thorpej struct ib_mach ib_mach_hp300 = {
     74      1.16   thorpej 	.name		=	"hp300",
     75      1.16   thorpej 	.setboot	=	hp300_setboot,
     76      1.16   thorpej 	.clearboot	=	no_clearboot,
     77      1.16   thorpej 	.editboot	=	no_editboot,
     78      1.16   thorpej 	.valid_flags	=	IB_APPEND,
     79      1.16   thorpej };
     80       1.7       dsl 
     81       1.7       dsl static int
     82       1.1       dsl hp300_setboot(ib_params *params)
     83       1.1       dsl {
     84       1.1       dsl 	int		retval;
     85       1.1       dsl 	uint8_t		*bootstrap;
     86  1.19.2.1  perseant 	size_t		bootstrap_mapsize;
     87       1.1       dsl 	ssize_t		rv;
     88       1.1       dsl 	struct partition *boot;
     89       1.1       dsl 	struct hp300_lifdir *lifdir;
     90  1.19.2.1  perseant 	int		size, offset;
     91       1.1       dsl 	int		i;
     92       1.6    dyoung 	unsigned int	secsize = HP300_SECTSIZE;
     93       1.3       dsl 	uint64_t	boot_size, boot_offset;
     94      1.19   tsutsui #ifdef SUPPORT_CD9660
     95      1.19   tsutsui 	uint32_t	nblk;
     96      1.19   tsutsui 	ib_block	*blocks;
     97      1.19   tsutsui #endif
     98      1.12   tsutsui 	struct disklabel *label;
     99       1.1       dsl 
    100       1.1       dsl 	assert(params != NULL);
    101       1.1       dsl 	assert(params->fsfd != -1);
    102       1.1       dsl 	assert(params->filesystem != NULL);
    103       1.1       dsl 	assert(params->s1fd != -1);
    104       1.1       dsl 	assert(params->stage1 != NULL);
    105       1.1       dsl 
    106       1.1       dsl 	retval = 0;
    107       1.1       dsl 	bootstrap = MAP_FAILED;
    108       1.1       dsl 
    109  1.19.2.1  perseant 	/* needs whole LIF volume/directory and actual bootstrap by default */
    110  1.19.2.1  perseant 	bootstrap_mapsize = params->s1stat.st_size;
    111  1.19.2.1  perseant 
    112      1.12   tsutsui 	label = malloc(params->sectorsize);
    113      1.12   tsutsui 	if (label == NULL) {
    114      1.12   tsutsui 		warn("Failed to allocate memory for disklabel");
    115      1.12   tsutsui 		goto done;
    116      1.12   tsutsui 	}
    117      1.12   tsutsui 
    118      1.19   tsutsui #ifdef SUPPORT_CD9660
    119      1.19   tsutsui 	if (params->stage2 != NULL) {
    120      1.19   tsutsui 		/*
    121      1.19   tsutsui 		 * Use contiguous blocks of SYS_BOOT in the target filesystem
    122      1.19   tsutsui 		 * (assuming ISO9660) for a LIF directory entry used
    123      1.19   tsutsui 		 * by BOOTROM on bootstrap.
    124      1.19   tsutsui 		 */
    125      1.19   tsutsui 		if (strcmp(params->fstype->name, "cd9660") != 0) {
    126      1.19   tsutsui 			warn("Target filesystem `%s' is unexpected",
    127      1.19   tsutsui 			    params->fstype->name);
    128      1.19   tsutsui 		}
    129      1.19   tsutsui 
    130      1.19   tsutsui 		if (S_ISREG(params->fsstat.st_mode)) {
    131      1.19   tsutsui 			if (fsync(params->fsfd) == -1)
    132      1.19   tsutsui 				warn("Synchronising file system `%s'",
    133      1.19   tsutsui 				    params->filesystem);
    134      1.19   tsutsui 		} else {
    135      1.19   tsutsui 			/* Don't allow real file systems for sanity */
    136      1.19   tsutsui 			warnx("`%s' must be a regular file to append "
    137      1.19   tsutsui 			    "a bootstrap", params->filesystem);
    138      1.19   tsutsui 			goto done;
    139      1.19   tsutsui 		}
    140      1.19   tsutsui 
    141      1.19   tsutsui 		/* Allocate space for our block list. */
    142      1.19   tsutsui 		nblk = HP300_MAXBLOCKS;
    143      1.19   tsutsui 		blocks = malloc(sizeof(*blocks) * nblk);
    144      1.19   tsutsui 		if (blocks == NULL) {
    145      1.19   tsutsui 			warn("Allocating %lu bytes for block list",
    146      1.19   tsutsui 			    (unsigned long)sizeof(*blocks) * nblk);
    147      1.19   tsutsui 			goto done;
    148      1.19   tsutsui 		}
    149      1.19   tsutsui 
    150      1.19   tsutsui 		/* Check the block of for the SYS_UBOOT in the target fs */
    151      1.19   tsutsui 		if (!params->fstype->findstage2(params, &nblk, blocks))
    152      1.19   tsutsui 			goto done;
    153      1.19   tsutsui 
    154      1.19   tsutsui 		if (nblk == 0) {
    155      1.19   tsutsui 			warnx("Secondary bootstrap `%s' is empty",
    156      1.19   tsutsui 			    params->stage2);
    157      1.19   tsutsui 			goto done;
    158      1.19   tsutsui 		} else if (nblk > 1) {
    159      1.19   tsutsui 			warnx("Secondary bootstrap `%s' doesn't have "
    160      1.19   tsutsui 			    "contiguous blocks", params->stage2);
    161      1.19   tsutsui 			goto done;
    162      1.19   tsutsui 		}
    163      1.19   tsutsui 
    164      1.19   tsutsui 		boot_offset = blocks[0].block * params->fstype->blocksize;
    165      1.19   tsutsui 		/* need to read only LIF volume and directories */
    166  1.19.2.1  perseant 		bootstrap_mapsize = LIF_VOLDIRSIZE;
    167      1.19   tsutsui 
    168      1.19   tsutsui 		if ((params->flags & IB_VERBOSE) != 0) {
    169      1.19   tsutsui 			printf("Bootstrap `%s' found at offset %lu in `%s'\n",
    170      1.19   tsutsui 			    params->stage2, (unsigned long)boot_offset,
    171      1.19   tsutsui 			    params->filesystem);
    172      1.19   tsutsui 		}
    173      1.19   tsutsui 
    174      1.19   tsutsui 	} else
    175      1.19   tsutsui #endif
    176  1.19.2.1  perseant 	if ((params->flags & IB_APPEND) != 0) {
    177       1.3       dsl 		if (!S_ISREG(params->fsstat.st_mode)) {
    178       1.3       dsl 			warnx(
    179       1.3       dsl 		    "`%s' must be a regular file to append a bootstrap",
    180       1.3       dsl 			    params->filesystem);
    181       1.3       dsl 			goto done;
    182       1.3       dsl 		}
    183       1.3       dsl 		boot_offset = roundup(params->fsstat.st_size, HP300_SECTSIZE);
    184       1.3       dsl 	} else {
    185       1.3       dsl 		/*
    186       1.3       dsl 		 * The bootstrap can be well over 8k, and must go into a BOOT
    187       1.3       dsl 		 * partition. Read NetBSD label to locate BOOT partition.
    188       1.3       dsl 		 */
    189      1.13   tsutsui 		if (pread(params->fsfd, label, params->sectorsize,
    190      1.13   tsutsui 		    LABELSECTOR * params->sectorsize)
    191      1.12   tsutsui 		    != (ssize_t)params->sectorsize) {
    192       1.3       dsl 			warn("reading disklabel");
    193       1.3       dsl 			goto done;
    194       1.3       dsl 		}
    195       1.3       dsl 		/* And a quick validation - must be a big-endian label */
    196       1.3       dsl 		secsize = be32toh(label->d_secsize);
    197       1.9     itohy 		if (label->d_magic != htobe32(DISKMAGIC) ||
    198       1.9     itohy 		    label->d_magic2 != htobe32(DISKMAGIC) ||
    199  1.19.2.1  perseant 		    secsize == 0 || !powerof2(secsize) ||
    200       1.8     itohy 		    be16toh(label->d_npartitions) > MAXMAXPARTITIONS) {
    201       1.3       dsl 			warnx("Invalid disklabel in %s", params->filesystem);
    202       1.3       dsl 			goto done;
    203       1.3       dsl 		}
    204       1.3       dsl 
    205       1.8     itohy 		i = be16toh(label->d_npartitions);
    206       1.3       dsl 		for (boot = label->d_partitions; ; boot++) {
    207       1.3       dsl 			if (--i < 0) {
    208       1.3       dsl 				warnx("No BOOT partition");
    209       1.3       dsl 				goto done;
    210       1.3       dsl 			}
    211       1.3       dsl 			if (boot->p_fstype == FS_BOOT)
    212       1.3       dsl 				break;
    213       1.3       dsl 		}
    214       1.3       dsl 		boot_size = be32toh(boot->p_size) * (uint64_t)secsize;
    215       1.3       dsl 		boot_offset = be32toh(boot->p_offset) * (uint64_t)secsize;
    216       1.1       dsl 
    217       1.3       dsl 		/*
    218       1.3       dsl 		 * We put the entire LIF file into the BOOT partition even when
    219       1.3       dsl 		 * it doesn't start at the beginning of the disk.
    220       1.3       dsl 		 *
    221       1.3       dsl 		 * Maybe we ought to be able to take a binary file and add
    222       1.3       dsl 		 * it to the LIF filesystem.
    223       1.3       dsl 		 */
    224      1.11     lukem 		if (boot_size < (uint64_t)params->s1stat.st_size) {
    225       1.3       dsl 			warn("BOOT partition too small (%llu < %llu)",
    226       1.3       dsl 				(unsigned long long)boot_size,
    227       1.3       dsl 				(unsigned long long)params->s1stat.st_size);
    228       1.1       dsl 			goto done;
    229       1.1       dsl 		}
    230       1.1       dsl 	}
    231       1.1       dsl 
    232  1.19.2.1  perseant 	/* Read LIF volume/directory (and bootstrap) from stage1 */
    233  1.19.2.1  perseant 	bootstrap = mmap(NULL, bootstrap_mapsize,
    234  1.19.2.1  perseant 	    PROT_READ | PROT_WRITE, MAP_PRIVATE, params->s1fd, 0);
    235  1.19.2.1  perseant 	if (bootstrap == MAP_FAILED) {
    236  1.19.2.1  perseant 		warn("mmapping `%s'", params->stage1);
    237  1.19.2.1  perseant 		goto done;
    238       1.1       dsl 	}
    239       1.1       dsl 
    240       1.1       dsl 	/* Relocate files, sanity check LIF directory on the way */
    241  1.19.2.1  perseant 	lifdir = (void *)(bootstrap + HP300_LIF_DIRSTART);
    242  1.19.2.1  perseant 	for (i = 0; i < HP300_LIF_NUMDIR; i++) {
    243  1.19.2.1  perseant 		uint32_t addr = be32toh(lifdir[i].dir_addr);
    244  1.19.2.1  perseant 		uint32_t limit = hp300_btolifs(params->s1stat.st_size);
    245  1.19.2.1  perseant 		uint32_t end = addr + be32toh(lifdir[i].dir_length);
    246      1.11     lukem 		if (end > limit) {
    247      1.18   tsutsui 			warnx("LIF entry %d larger (%u %u) than LIF file",
    248  1.19.2.1  perseant 			    i, end, limit);
    249       1.1       dsl 			goto done;
    250       1.1       dsl 		}
    251       1.3       dsl 		if (addr != 0 && boot_offset != 0)
    252  1.19.2.1  perseant 			lifdir[i].dir_addr =
    253  1.19.2.1  perseant 			    htobe32(boot_offset / HP300_SECTSIZE + addr);
    254       1.1       dsl 	}
    255       1.1       dsl 
    256  1.19.2.1  perseant 	if ((params->flags & IB_NOWRITE) != 0) {
    257       1.1       dsl 		retval = 1;
    258       1.1       dsl 		goto done;
    259       1.1       dsl 	}
    260       1.1       dsl 
    261       1.1       dsl 	/* Write LIF volume header and directory to sectors 0 and 1 */
    262      1.19   tsutsui 	rv = pwrite(params->fsfd, bootstrap, LIF_VOLDIRSIZE, 0);
    263      1.19   tsutsui 	if (rv != LIF_VOLDIRSIZE) {
    264       1.1       dsl 		if (rv == -1)
    265       1.1       dsl 			warn("Writing `%s'", params->filesystem);
    266       1.1       dsl 		else
    267       1.1       dsl 			warnx("Writing `%s': short write", params->filesystem);
    268       1.1       dsl 		goto done;
    269       1.1       dsl 	}
    270       1.1       dsl 
    271      1.19   tsutsui #ifdef SUPPORT_CD9660
    272      1.19   tsutsui 	if (params->stage2 != NULL) {
    273      1.19   tsutsui 		/*
    274      1.19   tsutsui 		 * Bootstrap in the target filesystem is used.
    275      1.19   tsutsui 		 * No need to write bootstrap to BOOT partition.
    276      1.19   tsutsui 		 */
    277      1.19   tsutsui 		retval = 1;
    278      1.19   tsutsui 		goto done;
    279      1.19   tsutsui 	}
    280      1.19   tsutsui #endif
    281      1.19   tsutsui 
    282  1.19.2.1  perseant 	/* Write bootstrap to BOOT partition (or at EOF in IB_APPEND case) */
    283  1.19.2.1  perseant 	/* LIF header and directories, and disklabel should be preserved */
    284  1.19.2.1  perseant 	offset = (boot_offset <= LIF_PRESERVED) ? LIF_PRESERVED : 0;
    285  1.19.2.1  perseant 	size = roundup(params->s1stat.st_size, secsize) - offset;
    286  1.19.2.1  perseant 
    287  1.19.2.1  perseant 	rv = pwrite(params->fsfd, bootstrap + offset, size,
    288  1.19.2.1  perseant 	    boot_offset + offset);
    289  1.19.2.1  perseant 	if (rv != size) {
    290       1.1       dsl 		if (rv == -1)
    291       1.1       dsl 			warn("Writing boot filesystem of `%s'",
    292  1.19.2.1  perseant 			    params->filesystem);
    293       1.1       dsl 		else
    294       1.3       dsl 			warnx("Writing boot filesystem of `%s': short write",
    295  1.19.2.1  perseant 			    params->filesystem);
    296       1.1       dsl 		goto done;
    297       1.1       dsl 	}
    298       1.1       dsl 
    299       1.1       dsl 	retval = 1;
    300       1.1       dsl 
    301       1.1       dsl  done:
    302      1.12   tsutsui 	if (label != NULL)
    303      1.12   tsutsui 		free(label);
    304       1.1       dsl 	if (bootstrap != MAP_FAILED)
    305  1.19.2.1  perseant 		munmap(bootstrap, bootstrap_mapsize);
    306       1.1       dsl 	return retval;
    307       1.1       dsl }
    308