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