hppa.c revision 1.1.4.2 1 1.1.4.2 yamt /* $NetBSD: hppa.c,v 1.1.4.2 2014/05/22 11:43:04 yamt Exp $ */
2 1.1.4.2 yamt
3 1.1.4.2 yamt /*-
4 1.1.4.2 yamt * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1.4.2 yamt * All rights reserved.
6 1.1.4.2 yamt *
7 1.1.4.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.1.4.2 yamt * by Luke Mewburn of Wasabi Systems.
9 1.1.4.2 yamt *
10 1.1.4.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.1.4.2 yamt * modification, are permitted provided that the following conditions
12 1.1.4.2 yamt * are met:
13 1.1.4.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.1.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.1.4.2 yamt * documentation and/or other materials provided with the distribution.
18 1.1.4.2 yamt *
19 1.1.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.4.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.4.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.4.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.4.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.4.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.4.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.4.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.4.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.4.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.4.2 yamt * POSSIBILITY OF SUCH DAMAGE.
30 1.1.4.2 yamt */
31 1.1.4.2 yamt
32 1.1.4.2 yamt #if HAVE_NBTOOL_CONFIG_H
33 1.1.4.2 yamt #include "nbtool_config.h"
34 1.1.4.2 yamt #endif
35 1.1.4.2 yamt
36 1.1.4.2 yamt #include <sys/cdefs.h>
37 1.1.4.2 yamt #if !defined(__lint)
38 1.1.4.2 yamt __RCSID("$NetBSD: hppa.c,v 1.1.4.2 2014/05/22 11:43:04 yamt Exp $");
39 1.1.4.2 yamt #endif /* !__lint */
40 1.1.4.2 yamt
41 1.1.4.2 yamt /* We need the target disklabel.h, not the hosts one..... */
42 1.1.4.2 yamt #ifdef HAVE_NBTOOL_CONFIG_H
43 1.1.4.2 yamt #include "nbtool_config.h"
44 1.1.4.2 yamt #include <nbinclude/sys/disklabel.h>
45 1.1.4.2 yamt #else
46 1.1.4.2 yamt #include <sys/disklabel.h>
47 1.1.4.2 yamt #endif
48 1.1.4.2 yamt #include <sys/param.h>
49 1.1.4.2 yamt #include <sys/stat.h>
50 1.1.4.2 yamt
51 1.1.4.2 yamt #include <assert.h>
52 1.1.4.2 yamt #include <err.h>
53 1.1.4.2 yamt #include <stddef.h>
54 1.1.4.2 yamt #include <stdio.h>
55 1.1.4.2 yamt #include <stdlib.h>
56 1.1.4.2 yamt #include <string.h>
57 1.1.4.2 yamt #include <unistd.h>
58 1.1.4.2 yamt
59 1.1.4.2 yamt #include "installboot.h"
60 1.1.4.2 yamt
61 1.1.4.2 yamt #define HPPA_LABELOFFSET 512
62 1.1.4.2 yamt #define HPPA_LABELSIZE 404 /* reserve 16 partitions */
63 1.1.4.2 yamt #define HPPA_BOOT_BLOCK_SIZE 8192
64 1.1.4.2 yamt
65 1.1.4.2 yamt static int hppa_clearboot(ib_params *);
66 1.1.4.2 yamt static int hppa_setboot(ib_params *);
67 1.1.4.2 yamt
68 1.1.4.2 yamt struct ib_mach ib_mach_hppa =
69 1.1.4.2 yamt { "hppa", hppa_setboot, hppa_clearboot, no_editboot, 0};
70 1.1.4.2 yamt
71 1.1.4.2 yamt static int
72 1.1.4.2 yamt hppa_clearboot(ib_params *params)
73 1.1.4.2 yamt {
74 1.1.4.2 yamt char bb[HPPA_BOOT_BLOCK_SIZE];
75 1.1.4.2 yamt int retval, eol;
76 1.1.4.2 yamt ssize_t rv;
77 1.1.4.2 yamt
78 1.1.4.2 yamt assert(params != NULL);
79 1.1.4.2 yamt assert(params->fsfd != -1);
80 1.1.4.2 yamt assert(params->filesystem != NULL);
81 1.1.4.2 yamt
82 1.1.4.2 yamt retval = 0;
83 1.1.4.2 yamt
84 1.1.4.2 yamt /* read disklabel on the target disk */
85 1.1.4.2 yamt rv = pread(params->fsfd, bb, sizeof bb, 0);
86 1.1.4.2 yamt if (rv == -1) {
87 1.1.4.2 yamt warn("Reading `%s'", params->filesystem);
88 1.1.4.2 yamt goto done;
89 1.1.4.2 yamt } else if (rv != sizeof bb) {
90 1.1.4.2 yamt warnx("Reading `%s': short read", params->filesystem);
91 1.1.4.2 yamt goto done;
92 1.1.4.2 yamt }
93 1.1.4.2 yamt
94 1.1.4.2 yamt /* clear header */
95 1.1.4.2 yamt memset(bb, 0, HPPA_LABELOFFSET);
96 1.1.4.2 yamt eol = HPPA_LABELOFFSET + HPPA_LABELSIZE;
97 1.1.4.2 yamt memset(&bb[eol], 0, sizeof bb - eol);
98 1.1.4.2 yamt
99 1.1.4.2 yamt if (params->flags & IB_VERBOSE) {
100 1.1.4.2 yamt printf("%slearing bootstrap\n",
101 1.1.4.2 yamt (params->flags & IB_NOWRITE) ? "Not c" : "C");
102 1.1.4.2 yamt }
103 1.1.4.2 yamt if (params->flags & IB_NOWRITE) {
104 1.1.4.2 yamt retval = 1;
105 1.1.4.2 yamt goto done;
106 1.1.4.2 yamt }
107 1.1.4.2 yamt
108 1.1.4.2 yamt rv = pwrite(params->fsfd, bb, sizeof bb, 0);
109 1.1.4.2 yamt if (rv == -1) {
110 1.1.4.2 yamt warn("Writing `%s'", params->filesystem);
111 1.1.4.2 yamt goto done;
112 1.1.4.2 yamt } else if (rv != HPPA_BOOT_BLOCK_SIZE) {
113 1.1.4.2 yamt warnx("Writing `%s': short write", params->filesystem);
114 1.1.4.2 yamt goto done;
115 1.1.4.2 yamt } else
116 1.1.4.2 yamt retval = 1;
117 1.1.4.2 yamt
118 1.1.4.2 yamt done:
119 1.1.4.2 yamt return (retval);
120 1.1.4.2 yamt }
121 1.1.4.2 yamt
122 1.1.4.2 yamt static int
123 1.1.4.2 yamt hppa_setboot(ib_params *params)
124 1.1.4.2 yamt {
125 1.1.4.2 yamt struct stat bootstrapsb;
126 1.1.4.2 yamt char bb[HPPA_BOOT_BLOCK_SIZE];
127 1.1.4.2 yamt struct {
128 1.1.4.2 yamt char l_off[HPPA_LABELOFFSET];
129 1.1.4.2 yamt struct disklabel l;
130 1.1.4.2 yamt char l_pad[HPPA_BOOT_BLOCK_SIZE
131 1.1.4.2 yamt - HPPA_LABELOFFSET - sizeof(struct disklabel)];
132 1.1.4.2 yamt } label;
133 1.1.4.2 yamt unsigned int secsize, npart;
134 1.1.4.2 yamt int retval;
135 1.1.4.2 yamt ssize_t rv;
136 1.1.4.2 yamt
137 1.1.4.2 yamt assert(params != NULL);
138 1.1.4.2 yamt assert(params->fsfd != -1);
139 1.1.4.2 yamt assert(params->filesystem != NULL);
140 1.1.4.2 yamt assert(params->s1fd != -1);
141 1.1.4.2 yamt assert(params->stage1 != NULL);
142 1.1.4.2 yamt
143 1.1.4.2 yamt retval = 0;
144 1.1.4.2 yamt
145 1.1.4.2 yamt /* read disklabel on the target disk */
146 1.1.4.2 yamt rv = pread(params->fsfd, &label, HPPA_BOOT_BLOCK_SIZE, 0);
147 1.1.4.2 yamt if (rv == -1) {
148 1.1.4.2 yamt warn("Reading `%s'", params->filesystem);
149 1.1.4.2 yamt goto done;
150 1.1.4.2 yamt } else if (rv != HPPA_BOOT_BLOCK_SIZE) {
151 1.1.4.2 yamt warnx("Reading `%s': short read", params->filesystem);
152 1.1.4.2 yamt goto done;
153 1.1.4.2 yamt }
154 1.1.4.2 yamt
155 1.1.4.2 yamt if (fstat(params->s1fd, &bootstrapsb) == -1) {
156 1.1.4.2 yamt warn("Examining `%s'", params->stage1);
157 1.1.4.2 yamt goto done;
158 1.1.4.2 yamt }
159 1.1.4.2 yamt if (!S_ISREG(bootstrapsb.st_mode)) {
160 1.1.4.2 yamt warnx("`%s' must be a regular file", params->stage1);
161 1.1.4.2 yamt goto done;
162 1.1.4.2 yamt }
163 1.1.4.2 yamt
164 1.1.4.2 yamt /* check if valid disklabel exists */
165 1.1.4.2 yamt secsize = be32toh(label.l.d_secsize);
166 1.1.4.2 yamt npart = be16toh(label.l.d_npartitions);
167 1.1.4.2 yamt if (label.l.d_magic != htobe32(DISKMAGIC) ||
168 1.1.4.2 yamt label.l.d_magic2 != htobe32(DISKMAGIC) ||
169 1.1.4.2 yamt secsize == 0 || secsize & (secsize - 1) ||
170 1.1.4.2 yamt npart > MAXMAXPARTITIONS) {
171 1.1.4.2 yamt warnx("No disklabel in `%s'", params->filesystem);
172 1.1.4.2 yamt
173 1.1.4.2 yamt /* then check if boot partition exists */
174 1.1.4.2 yamt } else if (npart < 1 || label.l.d_partitions[0].p_size == 0) {
175 1.1.4.2 yamt warnx("Partition `a' doesn't exist in %s", params->filesystem);
176 1.1.4.2 yamt
177 1.1.4.2 yamt /* check if the boot partition is below 2GB */
178 1.1.4.2 yamt } else if (be32toh(label.l.d_partitions[0].p_offset) +
179 1.1.4.2 yamt be32toh(label.l.d_partitions[0].p_size) >
180 1.1.4.2 yamt ((unsigned)2*1024*1024*1024) / secsize) {
181 1.1.4.2 yamt warnx("Partition `a' of `%s' exceeds 2GB boundary.",
182 1.1.4.2 yamt params->filesystem);
183 1.1.4.2 yamt warnx("It won't boot since hppa PDC can handle only 2GB.");
184 1.1.4.2 yamt goto done;
185 1.1.4.2 yamt }
186 1.1.4.2 yamt
187 1.1.4.2 yamt /* read boot loader */
188 1.1.4.2 yamt memset(&bb, 0, sizeof bb);
189 1.1.4.2 yamt rv = read(params->s1fd, &bb, sizeof bb);
190 1.1.4.2 yamt if (rv == -1) {
191 1.1.4.2 yamt warn("Reading `%s'", params->stage1);
192 1.1.4.2 yamt goto done;
193 1.1.4.2 yamt }
194 1.1.4.2 yamt /* then, overwrite disklabel */
195 1.1.4.2 yamt memcpy(&bb[HPPA_LABELOFFSET], &label.l, HPPA_LABELSIZE);
196 1.1.4.2 yamt
197 1.1.4.2 yamt if (params->flags & IB_VERBOSE) {
198 1.1.4.2 yamt printf("Bootstrap start sector: %#x\n", 0);
199 1.1.4.2 yamt printf("Bootstrap byte count: %#zx\n", rv);
200 1.1.4.2 yamt printf("%sriting bootstrap\n",
201 1.1.4.2 yamt (params->flags & IB_NOWRITE) ? "Not w" : "W");
202 1.1.4.2 yamt }
203 1.1.4.2 yamt if (params->flags & IB_NOWRITE) {
204 1.1.4.2 yamt retval = 1;
205 1.1.4.2 yamt goto done;
206 1.1.4.2 yamt }
207 1.1.4.2 yamt
208 1.1.4.2 yamt /* write boot loader and disklabel into the target disk */
209 1.1.4.2 yamt rv = pwrite(params->fsfd, &bb, HPPA_BOOT_BLOCK_SIZE, 0);
210 1.1.4.2 yamt if (rv == -1) {
211 1.1.4.2 yamt warn("Writing `%s'", params->filesystem);
212 1.1.4.2 yamt goto done;
213 1.1.4.2 yamt } else if (rv != HPPA_BOOT_BLOCK_SIZE) {
214 1.1.4.2 yamt warnx("Writing `%s': short write", params->filesystem);
215 1.1.4.2 yamt goto done;
216 1.1.4.2 yamt } else
217 1.1.4.2 yamt retval = 1;
218 1.1.4.2 yamt
219 1.1.4.2 yamt done:
220 1.1.4.2 yamt return (retval);
221 1.1.4.2 yamt }
222