hp300.c revision 1.9 1 /* $NetBSD: hp300.c,v 1.9 2008/02/02 13:37:13 itohy 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.9 2008/02/02 13:37:13 itohy 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 #include <nbinclude/sys/disklabel.h>
52 #else
53 #include <sys/disklabel.h>
54 #endif
55 #include <sys/fcntl.h>
56 #include <sys/ioctl.h>
57 #include <sys/mman.h>
58 #include <sys/param.h>
59
60 #include <assert.h>
61 #include <err.h>
62 #include <md5.h>
63 #include <stddef.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68
69 #include "installboot.h"
70
71 static int hp300_setboot(ib_params *);
72
73 struct ib_mach ib_mach_hp300 =
74 { "hp300", hp300_setboot, no_clearboot, no_editboot, IB_APPEND };
75
76 static int
77 hp300_setboot(ib_params *params)
78 {
79 int retval;
80 uint8_t *bootstrap;
81 ssize_t rv;
82 struct partition *boot;
83 struct hp300_lifdir *lifdir;
84 int offset;
85 int i;
86 unsigned int secsize = HP300_SECTSIZE;
87 uint64_t boot_size, boot_offset;
88 char label_buf[DEV_BSIZE];
89 struct disklabel *label = (void *)label_buf;
90
91 assert(params != NULL);
92 assert(params->fsfd != -1);
93 assert(params->filesystem != NULL);
94 assert(params->s1fd != -1);
95 assert(params->stage1 != NULL);
96
97 retval = 0;
98 bootstrap = MAP_FAILED;
99
100 if (params->flags & IB_APPEND) {
101 if (!S_ISREG(params->fsstat.st_mode)) {
102 warnx(
103 "`%s' must be a regular file to append a bootstrap",
104 params->filesystem);
105 goto done;
106 }
107 boot_offset = roundup(params->fsstat.st_size, HP300_SECTSIZE);
108 } else {
109 /*
110 * The bootstrap can be well over 8k, and must go into a BOOT
111 * partition. Read NetBSD label to locate BOOT partition.
112 */
113 if (pread(params->fsfd, label, DEV_BSIZE, 2 * DEV_BSIZE)
114 != DEV_BSIZE) {
115 warn("reading disklabel");
116 goto done;
117 }
118 /* And a quick validation - must be a big-endian label */
119 secsize = be32toh(label->d_secsize);
120 if (label->d_magic != htobe32(DISKMAGIC) ||
121 label->d_magic2 != htobe32(DISKMAGIC) ||
122 secsize == 0 || secsize & (secsize - 1) ||
123 be16toh(label->d_npartitions) > MAXMAXPARTITIONS) {
124 warnx("Invalid disklabel in %s", params->filesystem);
125 goto done;
126 }
127
128 i = be16toh(label->d_npartitions);
129 for (boot = label->d_partitions; ; boot++) {
130 if (--i < 0) {
131 warnx("No BOOT partition");
132 goto done;
133 }
134 if (boot->p_fstype == FS_BOOT)
135 break;
136 }
137 boot_size = be32toh(boot->p_size) * (uint64_t)secsize;
138 boot_offset = be32toh(boot->p_offset) * (uint64_t)secsize;
139
140 /*
141 * We put the entire LIF file into the BOOT partition even when
142 * it doesn't start at the beginning of the disk.
143 *
144 * Maybe we ought to be able to take a binary file and add
145 * it to the LIF filesystem.
146 */
147 if (boot_size < params->s1stat.st_size) {
148 warn("BOOT partition too small (%llu < %llu)",
149 (unsigned long long)boot_size,
150 (unsigned long long)params->s1stat.st_size);
151 goto done;
152 }
153 }
154
155 bootstrap = mmap(NULL, params->s1stat.st_size, PROT_READ | PROT_WRITE,
156 MAP_PRIVATE, params->s1fd, 0);
157 if (bootstrap == MAP_FAILED) {
158 warn("mmaping `%s'", params->stage1);
159 goto done;
160 }
161
162 /* Relocate files, sanity check LIF directory on the way */
163 lifdir = (void *)(bootstrap + HP300_SECTSIZE * 2);
164 for (i = 0; i < 8; lifdir++, i++) {
165 int addr = be32toh(lifdir->dir_addr);
166 int limit = (params->s1stat.st_size - 1) / HP300_SECTSIZE + 1;
167 if (addr + be32toh(lifdir->dir_length) > limit) {
168 warnx("LIF entry %d larger (%d %d) than LIF file",
169 i, addr + be32toh(lifdir->dir_length), limit);
170 goto done;
171 }
172 if (addr != 0 && boot_offset != 0)
173 lifdir->dir_addr = htobe32(addr + boot_offset
174 / HP300_SECTSIZE);
175 }
176
177 if (params->flags & IB_NOWRITE) {
178 retval = 1;
179 goto done;
180 }
181
182 /* Write LIF volume header and directory to sectors 0 and 1 */
183 rv = pwrite(params->fsfd, bootstrap, 1024, 0);
184 if (rv != 1024) {
185 if (rv == -1)
186 warn("Writing `%s'", params->filesystem);
187 else
188 warnx("Writing `%s': short write", params->filesystem);
189 goto done;
190 }
191
192 /* Write files to BOOT partition */
193 offset = boot_offset <= HP300_SECTSIZE * 16 ? HP300_SECTSIZE * 16 : 0;
194 i = roundup(params->s1stat.st_size, secsize) - offset;
195 rv = pwrite(params->fsfd, bootstrap + offset, i, boot_offset + offset);
196 if (rv != i) {
197 if (rv == -1)
198 warn("Writing boot filesystem of `%s'",
199 params->filesystem);
200 else
201 warnx("Writing boot filesystem of `%s': short write",
202 params->filesystem);
203 goto done;
204 }
205
206 retval = 1;
207
208 done:
209 if (bootstrap != MAP_FAILED)
210 munmap(bootstrap, params->s1stat.st_size);
211 return retval;
212 }
213