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