Home | History | Annotate | Line # | Download | only in arch
hp300.c revision 1.19
      1  1.19  tsutsui /* $NetBSD: hp300.c,v 1.19 2024/05/19 15:48:57 tsutsui 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  tsutsui __RCSID("$NetBSD: hp300.c,v 1.19 2024/05/19 15:48:57 tsutsui 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.7      dsl static int hp300_setboot(ib_params *);
     69   1.7      dsl 
     70  1.16  thorpej struct ib_mach ib_mach_hp300 = {
     71  1.16  thorpej 	.name		=	"hp300",
     72  1.16  thorpej 	.setboot	=	hp300_setboot,
     73  1.16  thorpej 	.clearboot	=	no_clearboot,
     74  1.16  thorpej 	.editboot	=	no_editboot,
     75  1.16  thorpej 	.valid_flags	=	IB_APPEND,
     76  1.16  thorpej };
     77   1.7      dsl 
     78   1.7      dsl static int
     79   1.1      dsl hp300_setboot(ib_params *params)
     80   1.1      dsl {
     81   1.1      dsl 	int		retval;
     82   1.1      dsl 	uint8_t		*bootstrap;
     83  1.19  tsutsui 	size_t		bootstrap_size;
     84   1.1      dsl 	ssize_t		rv;
     85   1.1      dsl 	struct partition *boot;
     86   1.1      dsl 	struct hp300_lifdir *lifdir;
     87   1.1      dsl 	int		offset;
     88   1.1      dsl 	int		i;
     89   1.6   dyoung 	unsigned int	secsize = HP300_SECTSIZE;
     90   1.3      dsl 	uint64_t	boot_size, boot_offset;
     91  1.19  tsutsui #ifdef SUPPORT_CD9660
     92  1.19  tsutsui 	uint32_t	nblk;
     93  1.19  tsutsui 	ib_block	*blocks;
     94  1.19  tsutsui #endif
     95  1.12  tsutsui 	struct disklabel *label;
     96   1.1      dsl 
     97   1.1      dsl 	assert(params != NULL);
     98   1.1      dsl 	assert(params->fsfd != -1);
     99   1.1      dsl 	assert(params->filesystem != NULL);
    100   1.1      dsl 	assert(params->s1fd != -1);
    101   1.1      dsl 	assert(params->stage1 != NULL);
    102   1.1      dsl 
    103   1.1      dsl 	retval = 0;
    104   1.1      dsl 	bootstrap = MAP_FAILED;
    105   1.1      dsl 
    106  1.12  tsutsui 	label = malloc(params->sectorsize);
    107  1.12  tsutsui 	if (label == NULL) {
    108  1.12  tsutsui 		warn("Failed to allocate memory for disklabel");
    109  1.12  tsutsui 		goto done;
    110  1.12  tsutsui 	}
    111  1.12  tsutsui 
    112  1.19  tsutsui #ifdef SUPPORT_CD9660
    113  1.19  tsutsui 	if (params->stage2 != NULL) {
    114  1.19  tsutsui 		/*
    115  1.19  tsutsui 		 * Use contiguous blocks of SYS_BOOT in the target filesystem
    116  1.19  tsutsui 		 * (assuming ISO9660) for a LIF directory entry used
    117  1.19  tsutsui 		 * by BOOTROM on bootstrap.
    118  1.19  tsutsui 		 */
    119  1.19  tsutsui 		if (strcmp(params->fstype->name, "cd9660") != 0) {
    120  1.19  tsutsui 			warn("Target filesystem `%s' is unexpected",
    121  1.19  tsutsui 			    params->fstype->name);
    122  1.19  tsutsui 		}
    123  1.19  tsutsui 
    124  1.19  tsutsui 		if (S_ISREG(params->fsstat.st_mode)) {
    125  1.19  tsutsui 			if (fsync(params->fsfd) == -1)
    126  1.19  tsutsui 				warn("Synchronising file system `%s'",
    127  1.19  tsutsui 				    params->filesystem);
    128  1.19  tsutsui 		} else {
    129  1.19  tsutsui 			/* Don't allow real file systems for sanity */
    130  1.19  tsutsui 			warnx("`%s' must be a regular file to append "
    131  1.19  tsutsui 			    "a bootstrap", params->filesystem);
    132  1.19  tsutsui 			goto done;
    133  1.19  tsutsui 		}
    134  1.19  tsutsui 
    135  1.19  tsutsui 		/* Allocate space for our block list. */
    136  1.19  tsutsui 		nblk = HP300_MAXBLOCKS;
    137  1.19  tsutsui 		blocks = malloc(sizeof(*blocks) * nblk);
    138  1.19  tsutsui 		if (blocks == NULL) {
    139  1.19  tsutsui 			warn("Allocating %lu bytes for block list",
    140  1.19  tsutsui 			    (unsigned long)sizeof(*blocks) * nblk);
    141  1.19  tsutsui 			goto done;
    142  1.19  tsutsui 		}
    143  1.19  tsutsui 
    144  1.19  tsutsui 		/* Check the block of for the SYS_UBOOT in the target fs */
    145  1.19  tsutsui 		if (!params->fstype->findstage2(params, &nblk, blocks))
    146  1.19  tsutsui 			goto done;
    147  1.19  tsutsui 
    148  1.19  tsutsui 		if (nblk == 0) {
    149  1.19  tsutsui 			warnx("Secondary bootstrap `%s' is empty",
    150  1.19  tsutsui 			    params->stage2);
    151  1.19  tsutsui 			goto done;
    152  1.19  tsutsui 		} else if (nblk > 1) {
    153  1.19  tsutsui 			warnx("Secondary bootstrap `%s' doesn't have "
    154  1.19  tsutsui 			    "contiguous blocks", params->stage2);
    155  1.19  tsutsui 			goto done;
    156  1.19  tsutsui 		}
    157  1.19  tsutsui 
    158  1.19  tsutsui 		boot_offset = blocks[0].block * params->fstype->blocksize;
    159  1.19  tsutsui 		/* need to read only LIF volume and directories */
    160  1.19  tsutsui 		bootstrap_size = LIF_VOLDIRSIZE;
    161  1.19  tsutsui 
    162  1.19  tsutsui 		if ((params->flags & IB_VERBOSE) != 0) {
    163  1.19  tsutsui 			printf("Bootstrap `%s' found at offset %lu in `%s'\n",
    164  1.19  tsutsui 			    params->stage2, (unsigned long)boot_offset,
    165  1.19  tsutsui 			    params->filesystem);
    166  1.19  tsutsui 		}
    167  1.19  tsutsui 
    168  1.19  tsutsui 	} else
    169  1.19  tsutsui #endif
    170   1.3      dsl 	if (params->flags & IB_APPEND) {
    171   1.3      dsl 		if (!S_ISREG(params->fsstat.st_mode)) {
    172   1.3      dsl 			warnx(
    173   1.3      dsl 		    "`%s' must be a regular file to append a bootstrap",
    174   1.3      dsl 			    params->filesystem);
    175   1.3      dsl 			goto done;
    176   1.3      dsl 		}
    177   1.3      dsl 		boot_offset = roundup(params->fsstat.st_size, HP300_SECTSIZE);
    178   1.3      dsl 	} else {
    179   1.3      dsl 		/*
    180   1.3      dsl 		 * The bootstrap can be well over 8k, and must go into a BOOT
    181   1.3      dsl 		 * partition. Read NetBSD label to locate BOOT partition.
    182   1.3      dsl 		 */
    183  1.13  tsutsui 		if (pread(params->fsfd, label, params->sectorsize,
    184  1.13  tsutsui 		    LABELSECTOR * params->sectorsize)
    185  1.12  tsutsui 		    != (ssize_t)params->sectorsize) {
    186   1.3      dsl 			warn("reading disklabel");
    187   1.3      dsl 			goto done;
    188   1.3      dsl 		}
    189   1.3      dsl 		/* And a quick validation - must be a big-endian label */
    190   1.3      dsl 		secsize = be32toh(label->d_secsize);
    191   1.9    itohy 		if (label->d_magic != htobe32(DISKMAGIC) ||
    192   1.9    itohy 		    label->d_magic2 != htobe32(DISKMAGIC) ||
    193   1.3      dsl 		    secsize == 0 || secsize & (secsize - 1) ||
    194   1.8    itohy 		    be16toh(label->d_npartitions) > MAXMAXPARTITIONS) {
    195   1.3      dsl 			warnx("Invalid disklabel in %s", params->filesystem);
    196   1.3      dsl 			goto done;
    197   1.3      dsl 		}
    198   1.3      dsl 
    199   1.8    itohy 		i = be16toh(label->d_npartitions);
    200   1.3      dsl 		for (boot = label->d_partitions; ; boot++) {
    201   1.3      dsl 			if (--i < 0) {
    202   1.3      dsl 				warnx("No BOOT partition");
    203   1.3      dsl 				goto done;
    204   1.3      dsl 			}
    205   1.3      dsl 			if (boot->p_fstype == FS_BOOT)
    206   1.3      dsl 				break;
    207   1.3      dsl 		}
    208   1.3      dsl 		boot_size = be32toh(boot->p_size) * (uint64_t)secsize;
    209   1.3      dsl 		boot_offset = be32toh(boot->p_offset) * (uint64_t)secsize;
    210   1.1      dsl 
    211   1.3      dsl 		/*
    212   1.3      dsl 		 * We put the entire LIF file into the BOOT partition even when
    213   1.3      dsl 		 * it doesn't start at the beginning of the disk.
    214   1.3      dsl 		 *
    215   1.3      dsl 		 * Maybe we ought to be able to take a binary file and add
    216   1.3      dsl 		 * it to the LIF filesystem.
    217   1.3      dsl 		 */
    218  1.11    lukem 		if (boot_size < (uint64_t)params->s1stat.st_size) {
    219   1.3      dsl 			warn("BOOT partition too small (%llu < %llu)",
    220   1.3      dsl 				(unsigned long long)boot_size,
    221   1.3      dsl 				(unsigned long long)params->s1stat.st_size);
    222   1.1      dsl 			goto done;
    223   1.1      dsl 		}
    224   1.1      dsl 	}
    225   1.1      dsl 
    226  1.19  tsutsui #ifdef SUPPORT_CD9660
    227  1.19  tsutsui 	if (params->stage2 != NULL) {
    228  1.19  tsutsui 		/* Use bootstrap file in the target filesystem. */
    229  1.19  tsutsui 		bootstrap = mmap(NULL, bootstrap_size,
    230  1.19  tsutsui 		    PROT_READ | PROT_WRITE, MAP_PRIVATE, params->fsfd,
    231  1.19  tsutsui 		    boot_offset);
    232  1.19  tsutsui 		if (bootstrap == MAP_FAILED) {
    233  1.19  tsutsui 			warn("mmapping `%s'", params->filesystem);
    234  1.19  tsutsui 			goto done;
    235  1.19  tsutsui 		}
    236  1.19  tsutsui 	} else
    237  1.19  tsutsui #endif
    238  1.19  tsutsui 	{
    239  1.19  tsutsui 		/* Use bootstrap specified as stage1. */
    240  1.19  tsutsui 		bootstrap_size = params->s1stat.st_size;
    241  1.19  tsutsui 		bootstrap = mmap(NULL, bootstrap_size,
    242  1.19  tsutsui 		    PROT_READ | PROT_WRITE, MAP_PRIVATE, params->s1fd, 0);
    243  1.19  tsutsui 		if (bootstrap == MAP_FAILED) {
    244  1.19  tsutsui 			warn("mmapping `%s'", params->stage1);
    245  1.19  tsutsui 			goto done;
    246  1.19  tsutsui 		}
    247   1.1      dsl 	}
    248   1.1      dsl 
    249   1.1      dsl 	/* Relocate files, sanity check LIF directory on the way */
    250   1.1      dsl 	lifdir = (void *)(bootstrap + HP300_SECTSIZE * 2);
    251   1.1      dsl 	for (i = 0; i < 8; lifdir++, i++) {
    252  1.18  tsutsui 		uint32_t addr = be32toh(lifdir->dir_addr);
    253  1.18  tsutsui 		uint32_t limit = (params->s1stat.st_size - 1) / HP300_SECTSIZE
    254  1.18  tsutsui 		    + 1;
    255  1.18  tsutsui 		uint32_t end = addr + be32toh(lifdir->dir_length);
    256  1.11    lukem 		if (end > limit) {
    257  1.18  tsutsui 			warnx("LIF entry %d larger (%u %u) than LIF file",
    258  1.11    lukem 				i, end, limit);
    259   1.1      dsl 			goto done;
    260   1.1      dsl 		}
    261   1.3      dsl 		if (addr != 0 && boot_offset != 0)
    262   1.3      dsl 			lifdir->dir_addr = htobe32(addr + boot_offset
    263   1.3      dsl 							    / HP300_SECTSIZE);
    264   1.1      dsl 	}
    265   1.1      dsl 
    266   1.1      dsl 	if (params->flags & IB_NOWRITE) {
    267   1.1      dsl 		retval = 1;
    268   1.1      dsl 		goto done;
    269   1.1      dsl 	}
    270   1.1      dsl 
    271   1.1      dsl 	/* Write LIF volume header and directory to sectors 0 and 1 */
    272  1.19  tsutsui 	rv = pwrite(params->fsfd, bootstrap, LIF_VOLDIRSIZE, 0);
    273  1.19  tsutsui 	if (rv != LIF_VOLDIRSIZE) {
    274   1.1      dsl 		if (rv == -1)
    275   1.1      dsl 			warn("Writing `%s'", params->filesystem);
    276   1.1      dsl 		else
    277   1.1      dsl 			warnx("Writing `%s': short write", params->filesystem);
    278   1.1      dsl 		goto done;
    279   1.1      dsl 	}
    280   1.1      dsl 
    281  1.19  tsutsui #ifdef SUPPORT_CD9660
    282  1.19  tsutsui 	if (params->stage2 != NULL) {
    283  1.19  tsutsui 		/*
    284  1.19  tsutsui 		 * Bootstrap in the target filesystem is used.
    285  1.19  tsutsui 		 * No need to write bootstrap to BOOT partition.
    286  1.19  tsutsui 		 */
    287  1.19  tsutsui 		retval = 1;
    288  1.19  tsutsui 		goto done;
    289  1.19  tsutsui 	}
    290  1.19  tsutsui #endif
    291  1.19  tsutsui 
    292   1.1      dsl 	/* Write files to BOOT partition */
    293   1.3      dsl 	offset = boot_offset <= HP300_SECTSIZE * 16 ? HP300_SECTSIZE * 16 : 0;
    294   1.4      dsl 	i = roundup(params->s1stat.st_size, secsize) - offset;
    295   1.3      dsl 	rv = pwrite(params->fsfd, bootstrap + offset, i, boot_offset + offset);
    296   1.3      dsl 	if (rv != i) {
    297   1.1      dsl 		if (rv == -1)
    298   1.1      dsl 			warn("Writing boot filesystem of `%s'",
    299   1.1      dsl 				params->filesystem);
    300   1.1      dsl 		else
    301   1.3      dsl 			warnx("Writing boot filesystem of `%s': short write",
    302   1.3      dsl 				params->filesystem);
    303   1.1      dsl 		goto done;
    304   1.1      dsl 	}
    305   1.1      dsl 
    306   1.1      dsl 	retval = 1;
    307   1.1      dsl 
    308   1.1      dsl  done:
    309  1.12  tsutsui 	if (label != NULL)
    310  1.12  tsutsui 		free(label);
    311   1.1      dsl 	if (bootstrap != MAP_FAILED)
    312  1.19  tsutsui 		munmap(bootstrap, bootstrap_size);
    313   1.1      dsl 	return retval;
    314   1.1      dsl }
    315