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