1*4882e3f0Stsutsui /* $NetBSD: hp300.c,v 1.19 2024/05/19 15:48:57 tsutsui Exp $ */
26bf8729bSdsl
36bf8729bSdsl /*-
46bf8729bSdsl * Copyright (c) 2003 The NetBSD Foundation, Inc.
56bf8729bSdsl * All rights reserved.
66bf8729bSdsl *
76bf8729bSdsl * This code is derived from software contributed to The NetBSD Foundation
86bf8729bSdsl * by David Laight.
96bf8729bSdsl *
106bf8729bSdsl * Redistribution and use in source and binary forms, with or without
116bf8729bSdsl * modification, are permitted provided that the following conditions
126bf8729bSdsl * are met:
136bf8729bSdsl * 1. Redistributions of source code must retain the above copyright
146bf8729bSdsl * notice, this list of conditions and the following disclaimer.
156bf8729bSdsl * 2. Redistributions in binary form must reproduce the above copyright
166bf8729bSdsl * notice, this list of conditions and the following disclaimer in the
176bf8729bSdsl * documentation and/or other materials provided with the distribution.
186bf8729bSdsl *
196bf8729bSdsl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
206bf8729bSdsl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
216bf8729bSdsl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
226bf8729bSdsl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
236bf8729bSdsl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
246bf8729bSdsl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
256bf8729bSdsl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
266bf8729bSdsl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
276bf8729bSdsl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
286bf8729bSdsl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
296bf8729bSdsl * POSSIBILITY OF SUCH DAMAGE.
306bf8729bSdsl */
316bf8729bSdsl
326bf8729bSdsl #if HAVE_NBTOOL_CONFIG_H
336bf8729bSdsl #include "nbtool_config.h"
346bf8729bSdsl #endif
356bf8729bSdsl
366bf8729bSdsl #include <sys/cdefs.h>
376bf8729bSdsl #if !defined(__lint)
38*4882e3f0Stsutsui __RCSID("$NetBSD: hp300.c,v 1.19 2024/05/19 15:48:57 tsutsui Exp $");
396bf8729bSdsl #endif /* !__lint */
406bf8729bSdsl
41c948aa41Sdsl /* We need the target disklabel.h, not the hosts one..... */
42c948aa41Sdsl #ifdef HAVE_NBTOOL_CONFIG_H
43c948aa41Sdsl #include "nbtool_config.h"
44252b8260Smatt #include <nbinclude/hp300/disklabel.h>
4500218a63Sdyoung #include <nbinclude/sys/disklabel.h>
46c948aa41Sdsl #else
476bf8729bSdsl #include <sys/disklabel.h>
48c948aa41Sdsl #endif
496bf8729bSdsl #include <sys/fcntl.h>
506bf8729bSdsl #include <sys/ioctl.h>
516bf8729bSdsl #include <sys/mman.h>
526bf8729bSdsl #include <sys/param.h>
536bf8729bSdsl
546bf8729bSdsl #include <assert.h>
556bf8729bSdsl #include <err.h>
566bf8729bSdsl #include <md5.h>
576bf8729bSdsl #include <stddef.h>
586bf8729bSdsl #include <stdio.h>
596bf8729bSdsl #include <stdlib.h>
606bf8729bSdsl #include <string.h>
616bf8729bSdsl #include <unistd.h>
626bf8729bSdsl
636bf8729bSdsl #include "installboot.h"
646bf8729bSdsl
65*4882e3f0Stsutsui #define HP300_MAXBLOCKS 1 /* Only contiguous blocks are expected. */
66*4882e3f0Stsutsui #define LIF_VOLDIRSIZE 1024 /* size of LIF volume header and directory */
67*4882e3f0Stsutsui
68cce659e2Sdsl static int hp300_setboot(ib_params *);
69cce659e2Sdsl
70e5c09b19Sthorpej struct ib_mach ib_mach_hp300 = {
71e5c09b19Sthorpej .name = "hp300",
72e5c09b19Sthorpej .setboot = hp300_setboot,
73e5c09b19Sthorpej .clearboot = no_clearboot,
74e5c09b19Sthorpej .editboot = no_editboot,
75e5c09b19Sthorpej .valid_flags = IB_APPEND,
76e5c09b19Sthorpej };
77cce659e2Sdsl
78cce659e2Sdsl static int
hp300_setboot(ib_params * params)796bf8729bSdsl hp300_setboot(ib_params *params)
806bf8729bSdsl {
816bf8729bSdsl int retval;
826bf8729bSdsl uint8_t *bootstrap;
83*4882e3f0Stsutsui size_t bootstrap_size;
846bf8729bSdsl ssize_t rv;
856bf8729bSdsl struct partition *boot;
866bf8729bSdsl struct hp300_lifdir *lifdir;
876bf8729bSdsl int offset;
886bf8729bSdsl int i;
8900218a63Sdyoung unsigned int secsize = HP300_SECTSIZE;
90c948aa41Sdsl uint64_t boot_size, boot_offset;
91*4882e3f0Stsutsui #ifdef SUPPORT_CD9660
92*4882e3f0Stsutsui uint32_t nblk;
93*4882e3f0Stsutsui ib_block *blocks;
94*4882e3f0Stsutsui #endif
9517ad8eceStsutsui struct disklabel *label;
966bf8729bSdsl
976bf8729bSdsl assert(params != NULL);
986bf8729bSdsl assert(params->fsfd != -1);
996bf8729bSdsl assert(params->filesystem != NULL);
1006bf8729bSdsl assert(params->s1fd != -1);
1016bf8729bSdsl assert(params->stage1 != NULL);
1026bf8729bSdsl
1036bf8729bSdsl retval = 0;
1046bf8729bSdsl bootstrap = MAP_FAILED;
1056bf8729bSdsl
10617ad8eceStsutsui label = malloc(params->sectorsize);
10717ad8eceStsutsui if (label == NULL) {
10817ad8eceStsutsui warn("Failed to allocate memory for disklabel");
10917ad8eceStsutsui goto done;
11017ad8eceStsutsui }
11117ad8eceStsutsui
112*4882e3f0Stsutsui #ifdef SUPPORT_CD9660
113*4882e3f0Stsutsui if (params->stage2 != NULL) {
114*4882e3f0Stsutsui /*
115*4882e3f0Stsutsui * Use contiguous blocks of SYS_BOOT in the target filesystem
116*4882e3f0Stsutsui * (assuming ISO9660) for a LIF directory entry used
117*4882e3f0Stsutsui * by BOOTROM on bootstrap.
118*4882e3f0Stsutsui */
119*4882e3f0Stsutsui if (strcmp(params->fstype->name, "cd9660") != 0) {
120*4882e3f0Stsutsui warn("Target filesystem `%s' is unexpected",
121*4882e3f0Stsutsui params->fstype->name);
122*4882e3f0Stsutsui }
123*4882e3f0Stsutsui
124*4882e3f0Stsutsui if (S_ISREG(params->fsstat.st_mode)) {
125*4882e3f0Stsutsui if (fsync(params->fsfd) == -1)
126*4882e3f0Stsutsui warn("Synchronising file system `%s'",
127*4882e3f0Stsutsui params->filesystem);
128*4882e3f0Stsutsui } else {
129*4882e3f0Stsutsui /* Don't allow real file systems for sanity */
130*4882e3f0Stsutsui warnx("`%s' must be a regular file to append "
131*4882e3f0Stsutsui "a bootstrap", params->filesystem);
132*4882e3f0Stsutsui goto done;
133*4882e3f0Stsutsui }
134*4882e3f0Stsutsui
135*4882e3f0Stsutsui /* Allocate space for our block list. */
136*4882e3f0Stsutsui nblk = HP300_MAXBLOCKS;
137*4882e3f0Stsutsui blocks = malloc(sizeof(*blocks) * nblk);
138*4882e3f0Stsutsui if (blocks == NULL) {
139*4882e3f0Stsutsui warn("Allocating %lu bytes for block list",
140*4882e3f0Stsutsui (unsigned long)sizeof(*blocks) * nblk);
141*4882e3f0Stsutsui goto done;
142*4882e3f0Stsutsui }
143*4882e3f0Stsutsui
144*4882e3f0Stsutsui /* Check the block of for the SYS_UBOOT in the target fs */
145*4882e3f0Stsutsui if (!params->fstype->findstage2(params, &nblk, blocks))
146*4882e3f0Stsutsui goto done;
147*4882e3f0Stsutsui
148*4882e3f0Stsutsui if (nblk == 0) {
149*4882e3f0Stsutsui warnx("Secondary bootstrap `%s' is empty",
150*4882e3f0Stsutsui params->stage2);
151*4882e3f0Stsutsui goto done;
152*4882e3f0Stsutsui } else if (nblk > 1) {
153*4882e3f0Stsutsui warnx("Secondary bootstrap `%s' doesn't have "
154*4882e3f0Stsutsui "contiguous blocks", params->stage2);
155*4882e3f0Stsutsui goto done;
156*4882e3f0Stsutsui }
157*4882e3f0Stsutsui
158*4882e3f0Stsutsui boot_offset = blocks[0].block * params->fstype->blocksize;
159*4882e3f0Stsutsui /* need to read only LIF volume and directories */
160*4882e3f0Stsutsui bootstrap_size = LIF_VOLDIRSIZE;
161*4882e3f0Stsutsui
162*4882e3f0Stsutsui if ((params->flags & IB_VERBOSE) != 0) {
163*4882e3f0Stsutsui printf("Bootstrap `%s' found at offset %lu in `%s'\n",
164*4882e3f0Stsutsui params->stage2, (unsigned long)boot_offset,
165*4882e3f0Stsutsui params->filesystem);
166*4882e3f0Stsutsui }
167*4882e3f0Stsutsui
168*4882e3f0Stsutsui } else
169*4882e3f0Stsutsui #endif
170c948aa41Sdsl if (params->flags & IB_APPEND) {
171c948aa41Sdsl if (!S_ISREG(params->fsstat.st_mode)) {
172c948aa41Sdsl warnx(
173c948aa41Sdsl "`%s' must be a regular file to append a bootstrap",
174c948aa41Sdsl params->filesystem);
175c948aa41Sdsl goto done;
176c948aa41Sdsl }
177c948aa41Sdsl boot_offset = roundup(params->fsstat.st_size, HP300_SECTSIZE);
178c948aa41Sdsl } else {
179c948aa41Sdsl /*
180c948aa41Sdsl * The bootstrap can be well over 8k, and must go into a BOOT
181c948aa41Sdsl * partition. Read NetBSD label to locate BOOT partition.
182c948aa41Sdsl */
18314d39e45Stsutsui if (pread(params->fsfd, label, params->sectorsize,
18414d39e45Stsutsui LABELSECTOR * params->sectorsize)
18517ad8eceStsutsui != (ssize_t)params->sectorsize) {
1866bf8729bSdsl warn("reading disklabel");
1876bf8729bSdsl goto done;
1886bf8729bSdsl }
189c948aa41Sdsl /* And a quick validation - must be a big-endian label */
190c948aa41Sdsl secsize = be32toh(label->d_secsize);
191d87794feSitohy if (label->d_magic != htobe32(DISKMAGIC) ||
192d87794feSitohy label->d_magic2 != htobe32(DISKMAGIC) ||
193c948aa41Sdsl secsize == 0 || secsize & (secsize - 1) ||
194347910d4Sitohy be16toh(label->d_npartitions) > MAXMAXPARTITIONS) {
195c948aa41Sdsl warnx("Invalid disklabel in %s", params->filesystem);
196c948aa41Sdsl goto done;
197c948aa41Sdsl }
1986bf8729bSdsl
199347910d4Sitohy i = be16toh(label->d_npartitions);
200c948aa41Sdsl for (boot = label->d_partitions; ; boot++) {
201c948aa41Sdsl if (--i < 0) {
2026bf8729bSdsl warnx("No BOOT partition");
2036bf8729bSdsl goto done;
2046bf8729bSdsl }
2056bf8729bSdsl if (boot->p_fstype == FS_BOOT)
2066bf8729bSdsl break;
2076bf8729bSdsl }
208c948aa41Sdsl boot_size = be32toh(boot->p_size) * (uint64_t)secsize;
209c948aa41Sdsl boot_offset = be32toh(boot->p_offset) * (uint64_t)secsize;
2106bf8729bSdsl
2116bf8729bSdsl /*
2126bf8729bSdsl * We put the entire LIF file into the BOOT partition even when
2136bf8729bSdsl * it doesn't start at the beginning of the disk.
2146bf8729bSdsl *
2156bf8729bSdsl * Maybe we ought to be able to take a binary file and add
2166bf8729bSdsl * it to the LIF filesystem.
2176bf8729bSdsl */
2182b2f4703Slukem if (boot_size < (uint64_t)params->s1stat.st_size) {
219c948aa41Sdsl warn("BOOT partition too small (%llu < %llu)",
220c948aa41Sdsl (unsigned long long)boot_size,
221c948aa41Sdsl (unsigned long long)params->s1stat.st_size);
2226bf8729bSdsl goto done;
2236bf8729bSdsl }
224c948aa41Sdsl }
2256bf8729bSdsl
226*4882e3f0Stsutsui #ifdef SUPPORT_CD9660
227*4882e3f0Stsutsui if (params->stage2 != NULL) {
228*4882e3f0Stsutsui /* Use bootstrap file in the target filesystem. */
229*4882e3f0Stsutsui bootstrap = mmap(NULL, bootstrap_size,
230*4882e3f0Stsutsui PROT_READ | PROT_WRITE, MAP_PRIVATE, params->fsfd,
231*4882e3f0Stsutsui boot_offset);
232*4882e3f0Stsutsui if (bootstrap == MAP_FAILED) {
233*4882e3f0Stsutsui warn("mmapping `%s'", params->filesystem);
234*4882e3f0Stsutsui goto done;
235*4882e3f0Stsutsui }
236*4882e3f0Stsutsui } else
237*4882e3f0Stsutsui #endif
238*4882e3f0Stsutsui {
239*4882e3f0Stsutsui /* Use bootstrap specified as stage1. */
240*4882e3f0Stsutsui bootstrap_size = params->s1stat.st_size;
241*4882e3f0Stsutsui bootstrap = mmap(NULL, bootstrap_size,
242*4882e3f0Stsutsui PROT_READ | PROT_WRITE, MAP_PRIVATE, params->s1fd, 0);
2436bf8729bSdsl if (bootstrap == MAP_FAILED) {
244086f28aeSmsaitoh warn("mmapping `%s'", params->stage1);
2456bf8729bSdsl goto done;
2466bf8729bSdsl }
247*4882e3f0Stsutsui }
2486bf8729bSdsl
2496bf8729bSdsl /* Relocate files, sanity check LIF directory on the way */
2506bf8729bSdsl lifdir = (void *)(bootstrap + HP300_SECTSIZE * 2);
2516bf8729bSdsl for (i = 0; i < 8; lifdir++, i++) {
2527f4c5163Stsutsui uint32_t addr = be32toh(lifdir->dir_addr);
2537f4c5163Stsutsui uint32_t limit = (params->s1stat.st_size - 1) / HP300_SECTSIZE
2547f4c5163Stsutsui + 1;
2557f4c5163Stsutsui uint32_t end = addr + be32toh(lifdir->dir_length);
2562b2f4703Slukem if (end > limit) {
2577f4c5163Stsutsui warnx("LIF entry %d larger (%u %u) than LIF file",
2582b2f4703Slukem i, end, limit);
2596bf8729bSdsl goto done;
2606bf8729bSdsl }
261c948aa41Sdsl if (addr != 0 && boot_offset != 0)
262c948aa41Sdsl lifdir->dir_addr = htobe32(addr + boot_offset
263c948aa41Sdsl / HP300_SECTSIZE);
2646bf8729bSdsl }
2656bf8729bSdsl
2666bf8729bSdsl if (params->flags & IB_NOWRITE) {
2676bf8729bSdsl retval = 1;
2686bf8729bSdsl goto done;
2696bf8729bSdsl }
2706bf8729bSdsl
2716bf8729bSdsl /* Write LIF volume header and directory to sectors 0 and 1 */
272*4882e3f0Stsutsui rv = pwrite(params->fsfd, bootstrap, LIF_VOLDIRSIZE, 0);
273*4882e3f0Stsutsui if (rv != LIF_VOLDIRSIZE) {
2746bf8729bSdsl if (rv == -1)
2756bf8729bSdsl warn("Writing `%s'", params->filesystem);
2766bf8729bSdsl else
2776bf8729bSdsl warnx("Writing `%s': short write", params->filesystem);
2786bf8729bSdsl goto done;
2796bf8729bSdsl }
2806bf8729bSdsl
281*4882e3f0Stsutsui #ifdef SUPPORT_CD9660
282*4882e3f0Stsutsui if (params->stage2 != NULL) {
283*4882e3f0Stsutsui /*
284*4882e3f0Stsutsui * Bootstrap in the target filesystem is used.
285*4882e3f0Stsutsui * No need to write bootstrap to BOOT partition.
286*4882e3f0Stsutsui */
287*4882e3f0Stsutsui retval = 1;
288*4882e3f0Stsutsui goto done;
289*4882e3f0Stsutsui }
290*4882e3f0Stsutsui #endif
291*4882e3f0Stsutsui
2926bf8729bSdsl /* Write files to BOOT partition */
293c948aa41Sdsl offset = boot_offset <= HP300_SECTSIZE * 16 ? HP300_SECTSIZE * 16 : 0;
294d7d05c9eSdsl i = roundup(params->s1stat.st_size, secsize) - offset;
295c948aa41Sdsl rv = pwrite(params->fsfd, bootstrap + offset, i, boot_offset + offset);
296c948aa41Sdsl if (rv != i) {
2976bf8729bSdsl if (rv == -1)
2986bf8729bSdsl warn("Writing boot filesystem of `%s'",
2996bf8729bSdsl params->filesystem);
3006bf8729bSdsl else
301c948aa41Sdsl warnx("Writing boot filesystem of `%s': short write",
302c948aa41Sdsl params->filesystem);
3036bf8729bSdsl goto done;
3046bf8729bSdsl }
3056bf8729bSdsl
3066bf8729bSdsl retval = 1;
3076bf8729bSdsl
3086bf8729bSdsl done:
30917ad8eceStsutsui if (label != NULL)
31017ad8eceStsutsui free(label);
3116bf8729bSdsl if (bootstrap != MAP_FAILED)
312*4882e3f0Stsutsui munmap(bootstrap, bootstrap_size);
3136bf8729bSdsl return retval;
3146bf8729bSdsl }
315