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