hp300.c revision 1.3 1 /* $NetBSD: hp300.c,v 1.3 2003/11/10 10:48:30 dsl 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #if HAVE_NBTOOL_CONFIG_H
40 #include "nbtool_config.h"
41 #endif
42
43 #include <sys/cdefs.h>
44 #if !defined(__lint)
45 __RCSID("$NetBSD: hp300.c,v 1.3 2003/11/10 10:48:30 dsl Exp $");
46 #endif /* !__lint */
47
48 /* We need the target disklabel.h, not the hosts one..... */
49 #ifdef HAVE_NBTOOL_CONFIG_H
50 #include "nbtool_config.h"
51 #undef __HAVE_OLD_DISKLABEL /* host's <machine/types.h> may define this */
52 #include "../../sys/arch/hp300/include/disklabel.h"
53 #include "../../sys/sys/disklabel.h"
54 #else
55 #include <sys/disklabel.h>
56 #endif
57 #include <sys/fcntl.h>
58 #include <sys/ioctl.h>
59 #include <sys/mman.h>
60 #include <sys/param.h>
61
62 #include <assert.h>
63 #include <err.h>
64 #include <md5.h>
65 #include <stddef.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <unistd.h>
70
71 #include "installboot.h"
72 #include "hp300_volhdr.h"
73
74 int
75 hp300_setboot(ib_params *params)
76 {
77 int retval;
78 uint8_t *bootstrap;
79 ssize_t rv;
80 struct partition *boot;
81 struct hp300_lifdir *lifdir;
82 int offset;
83 int i;
84 uint secsize;
85 uint64_t boot_size, boot_offset;
86 char label_buf[DEV_BSIZE];
87 struct disklabel *label = (void *)label_buf;
88
89 assert(params != NULL);
90 assert(params->fsfd != -1);
91 assert(params->filesystem != NULL);
92 assert(params->s1fd != -1);
93 assert(params->stage1 != NULL);
94
95 retval = 0;
96 bootstrap = MAP_FAILED;
97
98 if (params->flags & IB_APPEND) {
99 if (!S_ISREG(params->fsstat.st_mode)) {
100 warnx(
101 "`%s' must be a regular file to append a bootstrap",
102 params->filesystem);
103 goto done;
104 }
105 boot_offset = roundup(params->fsstat.st_size, HP300_SECTSIZE);
106 } else {
107 /*
108 * The bootstrap can be well over 8k, and must go into a BOOT
109 * partition. Read NetBSD label to locate BOOT partition.
110 */
111 if (pread(params->fsfd, label, DEV_BSIZE, 2 * DEV_BSIZE)
112 != DEV_BSIZE) {
113 warn("reading disklabel");
114 goto done;
115 }
116 /* And a quick validation - must be a big-endian label */
117 secsize = be32toh(label->d_secsize);
118 if (label->d_magic != be32toh(DISKMAGIC) ||
119 label->d_magic2 != be32toh(DISKMAGIC) ||
120 secsize == 0 || secsize & (secsize - 1) ||
121 be32toh(label->d_npartitions) > MAXMAXPARTITIONS) {
122 warnx("Invalid disklabel in %s", params->filesystem);
123 goto done;
124 }
125
126 i = be32toh(label->d_npartitions);
127 for (boot = label->d_partitions; ; boot++) {
128 if (--i < 0) {
129 warnx("No BOOT partition");
130 goto done;
131 }
132 if (boot->p_fstype == FS_BOOT)
133 break;
134 }
135 boot_size = be32toh(boot->p_size) * (uint64_t)secsize;
136 boot_offset = be32toh(boot->p_offset) * (uint64_t)secsize;
137
138 /*
139 * We put the entire LIF file into the BOOT partition even when
140 * it doesn't start at the beginning of the disk.
141 *
142 * Maybe we ought to be able to take a binary file and add
143 * it to the LIF filesystem.
144 */
145 if (boot_size < params->s1stat.st_size) {
146 warn("BOOT partition too small (%llu < %llu)",
147 (unsigned long long)boot_size,
148 (unsigned long long)params->s1stat.st_size);
149 goto done;
150 }
151 }
152
153 bootstrap = mmap(NULL, params->s1stat.st_size, PROT_READ | PROT_WRITE,
154 MAP_PRIVATE, params->s1fd, 0);
155 if (bootstrap == MAP_FAILED) {
156 warn("mmaping `%s'", params->stage1);
157 goto done;
158 }
159
160 /* Relocate files, sanity check LIF directory on the way */
161 lifdir = (void *)(bootstrap + HP300_SECTSIZE * 2);
162 for (i = 0; i < 8; lifdir++, i++) {
163 int addr = be32toh(lifdir->dir_addr);
164 int limit = (params->s1stat.st_size - 1) / HP300_SECTSIZE + 1;
165 if (addr + be32toh(lifdir->dir_length) > limit) {
166 warnx("LIF entry %d larger (%d %d) than LIF file",
167 i, addr + be32toh(lifdir->dir_length), limit);
168 goto done;
169 }
170 if (addr != 0 && boot_offset != 0)
171 lifdir->dir_addr = htobe32(addr + boot_offset
172 / HP300_SECTSIZE);
173 }
174
175 if (params->flags & IB_NOWRITE) {
176 retval = 1;
177 goto done;
178 }
179
180 /* Write LIF volume header and directory to sectors 0 and 1 */
181 rv = pwrite(params->fsfd, bootstrap, 1024, 0);
182 if (rv != 1024) {
183 if (rv == -1)
184 warn("Writing `%s'", params->filesystem);
185 else
186 warnx("Writing `%s': short write", params->filesystem);
187 goto done;
188 }
189
190 /* Write files to BOOT partition */
191 offset = boot_offset <= HP300_SECTSIZE * 16 ? HP300_SECTSIZE * 16 : 0;
192 i = params->s1stat.st_size - offset;
193 rv = pwrite(params->fsfd, bootstrap + offset, i, boot_offset + offset);
194 if (rv != i) {
195 if (rv == -1)
196 warn("Writing boot filesystem of `%s'",
197 params->filesystem);
198 else
199 warnx("Writing boot filesystem of `%s': short write",
200 params->filesystem);
201 goto done;
202 }
203
204 retval = 1;
205
206 done:
207 if (bootstrap != MAP_FAILED)
208 munmap(bootstrap, params->s1stat.st_size);
209 return retval;
210 }
211