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