hp300.c revision 1.1 1 /* $NetBSD: hp300.c,v 1.1 2003/11/08 16:44:35 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.1 2003/11/08 16:44:35 dsl Exp $");
46 #endif /* !__lint */
47
48 #include <sys/disklabel.h>
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 #include "hp300_volhdr.h"
65
66 #define nelem(x) (sizeof (x)/sizeof *(x))
67
68 int
69 hp300_setboot(ib_params *params)
70 {
71 int retval;
72 uint8_t *bootstrap;
73 ssize_t rv;
74 struct disklabel label;
75 struct partition *boot;
76 struct hp300_lifdir *lifdir;
77 char ch, *cp;
78 int bootfd = -1;
79 int offset;
80 int max_ptn;
81 int i;
82 struct stat sb;
83
84 assert(params != NULL);
85 assert(params->fsfd != -1);
86 assert(params->filesystem != NULL);
87 assert(params->s1fd != -1);
88 assert(params->stage1 != NULL);
89
90 retval = 0;
91 bootstrap = MAP_FAILED;
92
93 /* The bootstrap can be well over 8k, and must go into a BOOT ptn. */
94 if (ioctl(params->fsfd, DIOCGDINFO, &label) != 0) {
95 warn("reading disklabel");
96 goto done;
97 }
98
99 max_ptn = label.d_npartitions;
100 if (max_ptn > nelem(label.d_partitions))
101 max_ptn = nelem(label.d_partitions);
102 for (boot = label.d_partitions; ; boot++) {
103 if (--max_ptn < 0) {
104 warnx("No BOOT partition");
105 goto done;
106 }
107 if (boot->p_fstype == FS_BOOT)
108 break;
109 }
110
111 /*
112 * We put the entire LIF file into the BOOT partition even when
113 * it doesn't start at the beginning of the disk.
114 *
115 * Maybe we ought to be able to take a binary file and add
116 * it to the LIF filesystem.
117 */
118 if (boot->p_size * label.d_secsize < params->s1stat.st_size) {
119 warn("BOOT partition too small (%d < %" PRId64 ")",
120 boot->p_size * label.d_secsize,
121 params->s1stat.st_size);
122 goto done;
123 }
124
125 bootstrap = mmap(NULL, params->s1stat.st_size, PROT_READ | PROT_WRITE,
126 MAP_PRIVATE, params->s1fd, 0);
127 if (bootstrap == MAP_FAILED) {
128 warn("mmaping `%s'", params->stage1);
129 goto done;
130 }
131
132 /* Relocate files, sanity check LIF directory on the way */
133 lifdir = (void *)(bootstrap + HP300_SECTSIZE * 2);
134 for (i = 0; i < 8; lifdir++, i++) {
135 int addr = be32toh(lifdir->dir_addr);
136 int limit = (params->s1stat.st_size - 1) / HP300_SECTSIZE + 1;
137 if (addr + be32toh(lifdir->dir_length) > limit) {
138 warnx("LIF entry %d larger (%d %d) than LIF file",
139 i, addr + be32toh(lifdir->dir_length), limit);
140 goto done;
141 }
142 if (addr != 0 && boot->p_offset != 0)
143 lifdir->dir_addr = htobe32(addr + boot->p_offset
144 * (label.d_secsize / HP300_SECTSIZE));
145 }
146
147 /* Open boot partition itself */
148 cp = strchr(params->filesystem, 0) - 1;
149 ch = *cp;
150 *cp = 'a' + (boot - label.d_partitions);
151 bootfd = open(params->filesystem,
152 params->flags & IB_NOWRITE ? O_RDONLY : O_RDWR, 0);
153 if (bootfd == -1) {
154 warn("Cannot open BOOT partition %s", params->filesystem);
155 *cp = ch;
156 goto done;
157 }
158 *cp = ch;
159
160 /* stat as a slight sanity check */
161 if (fstat(bootfd, &sb) == -1
162 || sb.st_size != boot->p_size * label.d_secsize) {
163 warnx("Opened BOOT partition size doesn't match disklabel");
164 goto done;
165 }
166
167 if (params->flags & IB_NOWRITE) {
168 retval = 1;
169 goto done;
170 }
171
172 /* Write LIF volume header and directory to sectors 0 and 1 */
173 rv = pwrite(params->fsfd, bootstrap, 1024, 0);
174 if (rv != 1024) {
175 if (rv == -1)
176 warn("Writing `%s'", params->filesystem);
177 else
178 warnx("Writing `%s': short write", params->filesystem);
179 goto done;
180 }
181
182 /* Write files to BOOT partition */
183 offset = boot->p_offset <= HP300_SECTSIZE * 16 / label.d_secsize
184 ? HP300_SECTSIZE * 16 : 0;
185 rv = pwrite(bootfd, bootstrap + offset, params->s1stat.st_size - offset, offset);
186 if (rv != params->s1stat.st_size - offset) {
187 if (rv == -1)
188 warn("Writing boot filesystem of `%s'",
189 params->filesystem);
190 else
191 warnx("Writing boot filesystem of `%s': short write", params->filesystem);
192 goto done;
193 }
194
195 retval = 1;
196
197 done:
198 if (bootstrap != MAP_FAILED)
199 munmap(bootstrap, params->s1stat.st_size);
200 if (bootfd != -1)
201 close(bootfd);
202 return retval;
203 }
204