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