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