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