hp300.c revision 1.2 1 1.2 uwe /* $NetBSD: hp300.c,v 1.2 2003/11/08 22:39:07 uwe 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 * 3. All advertising materials mentioning features or use of this software
19 1.1 dsl * must display the following acknowledgement:
20 1.1 dsl * This product includes software developed by the NetBSD
21 1.1 dsl * Foundation, Inc. and its contributors.
22 1.1 dsl * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 dsl * contributors may be used to endorse or promote products derived
24 1.1 dsl * from this software without specific prior written permission.
25 1.1 dsl *
26 1.1 dsl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 dsl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 dsl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 dsl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 dsl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 dsl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 dsl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 dsl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 dsl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 dsl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 dsl * POSSIBILITY OF SUCH DAMAGE.
37 1.1 dsl */
38 1.1 dsl
39 1.1 dsl #if HAVE_NBTOOL_CONFIG_H
40 1.1 dsl #include "nbtool_config.h"
41 1.1 dsl #endif
42 1.1 dsl
43 1.1 dsl #include <sys/cdefs.h>
44 1.1 dsl #if !defined(__lint)
45 1.2 uwe __RCSID("$NetBSD: hp300.c,v 1.2 2003/11/08 22:39:07 uwe Exp $");
46 1.1 dsl #endif /* !__lint */
47 1.1 dsl
48 1.1 dsl #include <sys/disklabel.h>
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 #include "hp300_volhdr.h"
65 1.1 dsl
66 1.1 dsl #define nelem(x) (sizeof (x)/sizeof *(x))
67 1.1 dsl
68 1.1 dsl int
69 1.1 dsl hp300_setboot(ib_params *params)
70 1.1 dsl {
71 1.1 dsl int retval;
72 1.1 dsl uint8_t *bootstrap;
73 1.1 dsl ssize_t rv;
74 1.1 dsl struct disklabel label;
75 1.1 dsl struct partition *boot;
76 1.1 dsl struct hp300_lifdir *lifdir;
77 1.1 dsl char ch, *cp;
78 1.1 dsl int bootfd = -1;
79 1.1 dsl int offset;
80 1.1 dsl int max_ptn;
81 1.1 dsl int i;
82 1.1 dsl struct stat sb;
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.1 dsl /* The bootstrap can be well over 8k, and must go into a BOOT ptn. */
94 1.1 dsl if (ioctl(params->fsfd, DIOCGDINFO, &label) != 0) {
95 1.1 dsl warn("reading disklabel");
96 1.1 dsl goto done;
97 1.1 dsl }
98 1.1 dsl
99 1.1 dsl max_ptn = label.d_npartitions;
100 1.1 dsl if (max_ptn > nelem(label.d_partitions))
101 1.1 dsl max_ptn = nelem(label.d_partitions);
102 1.1 dsl for (boot = label.d_partitions; ; boot++) {
103 1.1 dsl if (--max_ptn < 0) {
104 1.1 dsl warnx("No BOOT partition");
105 1.1 dsl goto done;
106 1.1 dsl }
107 1.1 dsl if (boot->p_fstype == FS_BOOT)
108 1.1 dsl break;
109 1.1 dsl }
110 1.1 dsl
111 1.1 dsl /*
112 1.1 dsl * We put the entire LIF file into the BOOT partition even when
113 1.1 dsl * it doesn't start at the beginning of the disk.
114 1.1 dsl *
115 1.1 dsl * Maybe we ought to be able to take a binary file and add
116 1.1 dsl * it to the LIF filesystem.
117 1.1 dsl */
118 1.1 dsl if (boot->p_size * label.d_secsize < params->s1stat.st_size) {
119 1.2 uwe warn("BOOT partition too small (%d < %lld)",
120 1.1 dsl boot->p_size * label.d_secsize,
121 1.2 uwe (long long int)params->s1stat.st_size);
122 1.1 dsl goto done;
123 1.1 dsl }
124 1.1 dsl
125 1.1 dsl bootstrap = mmap(NULL, params->s1stat.st_size, PROT_READ | PROT_WRITE,
126 1.1 dsl MAP_PRIVATE, params->s1fd, 0);
127 1.1 dsl if (bootstrap == MAP_FAILED) {
128 1.1 dsl warn("mmaping `%s'", params->stage1);
129 1.1 dsl goto done;
130 1.1 dsl }
131 1.1 dsl
132 1.1 dsl /* Relocate files, sanity check LIF directory on the way */
133 1.1 dsl lifdir = (void *)(bootstrap + HP300_SECTSIZE * 2);
134 1.1 dsl for (i = 0; i < 8; lifdir++, i++) {
135 1.1 dsl int addr = be32toh(lifdir->dir_addr);
136 1.1 dsl int limit = (params->s1stat.st_size - 1) / HP300_SECTSIZE + 1;
137 1.1 dsl if (addr + be32toh(lifdir->dir_length) > limit) {
138 1.1 dsl warnx("LIF entry %d larger (%d %d) than LIF file",
139 1.1 dsl i, addr + be32toh(lifdir->dir_length), limit);
140 1.1 dsl goto done;
141 1.1 dsl }
142 1.1 dsl if (addr != 0 && boot->p_offset != 0)
143 1.1 dsl lifdir->dir_addr = htobe32(addr + boot->p_offset
144 1.1 dsl * (label.d_secsize / HP300_SECTSIZE));
145 1.1 dsl }
146 1.1 dsl
147 1.1 dsl /* Open boot partition itself */
148 1.1 dsl cp = strchr(params->filesystem, 0) - 1;
149 1.1 dsl ch = *cp;
150 1.1 dsl *cp = 'a' + (boot - label.d_partitions);
151 1.1 dsl bootfd = open(params->filesystem,
152 1.1 dsl params->flags & IB_NOWRITE ? O_RDONLY : O_RDWR, 0);
153 1.1 dsl if (bootfd == -1) {
154 1.1 dsl warn("Cannot open BOOT partition %s", params->filesystem);
155 1.1 dsl *cp = ch;
156 1.1 dsl goto done;
157 1.1 dsl }
158 1.1 dsl *cp = ch;
159 1.1 dsl
160 1.1 dsl /* stat as a slight sanity check */
161 1.1 dsl if (fstat(bootfd, &sb) == -1
162 1.1 dsl || sb.st_size != boot->p_size * label.d_secsize) {
163 1.1 dsl warnx("Opened BOOT partition size doesn't match disklabel");
164 1.1 dsl goto done;
165 1.1 dsl }
166 1.1 dsl
167 1.1 dsl if (params->flags & IB_NOWRITE) {
168 1.1 dsl retval = 1;
169 1.1 dsl goto done;
170 1.1 dsl }
171 1.1 dsl
172 1.1 dsl /* Write LIF volume header and directory to sectors 0 and 1 */
173 1.1 dsl rv = pwrite(params->fsfd, bootstrap, 1024, 0);
174 1.1 dsl if (rv != 1024) {
175 1.1 dsl if (rv == -1)
176 1.1 dsl warn("Writing `%s'", params->filesystem);
177 1.1 dsl else
178 1.1 dsl warnx("Writing `%s': short write", params->filesystem);
179 1.1 dsl goto done;
180 1.1 dsl }
181 1.1 dsl
182 1.1 dsl /* Write files to BOOT partition */
183 1.1 dsl offset = boot->p_offset <= HP300_SECTSIZE * 16 / label.d_secsize
184 1.1 dsl ? HP300_SECTSIZE * 16 : 0;
185 1.1 dsl rv = pwrite(bootfd, bootstrap + offset, params->s1stat.st_size - offset, offset);
186 1.1 dsl if (rv != params->s1stat.st_size - offset) {
187 1.1 dsl if (rv == -1)
188 1.1 dsl warn("Writing boot filesystem of `%s'",
189 1.1 dsl params->filesystem);
190 1.1 dsl else
191 1.1 dsl warnx("Writing boot filesystem of `%s': short write", params->filesystem);
192 1.1 dsl goto done;
193 1.1 dsl }
194 1.1 dsl
195 1.1 dsl retval = 1;
196 1.1 dsl
197 1.1 dsl done:
198 1.1 dsl if (bootstrap != MAP_FAILED)
199 1.1 dsl munmap(bootstrap, params->s1stat.st_size);
200 1.1 dsl if (bootfd != -1)
201 1.1 dsl close(bootfd);
202 1.1 dsl return retval;
203 1.1 dsl }
204