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