1 /* $NetBSD: hp300.c,v 1.16 2019/05/07 04:35:31 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2003 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by David Laight. 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: hp300.c,v 1.16 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/hp300/disklabel.h> 45 #include <nbinclude/sys/disklabel.h> 46 #else 47 #include <sys/disklabel.h> 48 #endif 49 #include <sys/fcntl.h> 50 #include <sys/ioctl.h> 51 #include <sys/mman.h> 52 #include <sys/param.h> 53 54 #include <assert.h> 55 #include <err.h> 56 #include <md5.h> 57 #include <stddef.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <unistd.h> 62 63 #include "installboot.h" 64 65 static int hp300_setboot(ib_params *); 66 67 struct ib_mach ib_mach_hp300 = { 68 .name = "hp300", 69 .setboot = hp300_setboot, 70 .clearboot = no_clearboot, 71 .editboot = no_editboot, 72 .valid_flags = IB_APPEND, 73 }; 74 75 static int 76 hp300_setboot(ib_params *params) 77 { 78 int retval; 79 uint8_t *bootstrap; 80 ssize_t rv; 81 struct partition *boot; 82 struct hp300_lifdir *lifdir; 83 int offset; 84 int i; 85 unsigned int secsize = HP300_SECTSIZE; 86 uint64_t boot_size, boot_offset; 87 struct disklabel *label; 88 89 assert(params != NULL); 90 assert(params->fsfd != -1); 91 assert(params->filesystem != NULL); 92 assert(params->s1fd != -1); 93 assert(params->stage1 != NULL); 94 95 retval = 0; 96 bootstrap = MAP_FAILED; 97 98 label = malloc(params->sectorsize); 99 if (label == NULL) { 100 warn("Failed to allocate memory for disklabel"); 101 goto done; 102 } 103 104 if (params->flags & IB_APPEND) { 105 if (!S_ISREG(params->fsstat.st_mode)) { 106 warnx( 107 "`%s' must be a regular file to append a bootstrap", 108 params->filesystem); 109 goto done; 110 } 111 boot_offset = roundup(params->fsstat.st_size, HP300_SECTSIZE); 112 } else { 113 /* 114 * The bootstrap can be well over 8k, and must go into a BOOT 115 * partition. Read NetBSD label to locate BOOT partition. 116 */ 117 if (pread(params->fsfd, label, params->sectorsize, 118 LABELSECTOR * params->sectorsize) 119 != (ssize_t)params->sectorsize) { 120 warn("reading disklabel"); 121 goto done; 122 } 123 /* And a quick validation - must be a big-endian label */ 124 secsize = be32toh(label->d_secsize); 125 if (label->d_magic != htobe32(DISKMAGIC) || 126 label->d_magic2 != htobe32(DISKMAGIC) || 127 secsize == 0 || secsize & (secsize - 1) || 128 be16toh(label->d_npartitions) > MAXMAXPARTITIONS) { 129 warnx("Invalid disklabel in %s", params->filesystem); 130 goto done; 131 } 132 133 i = be16toh(label->d_npartitions); 134 for (boot = label->d_partitions; ; boot++) { 135 if (--i < 0) { 136 warnx("No BOOT partition"); 137 goto done; 138 } 139 if (boot->p_fstype == FS_BOOT) 140 break; 141 } 142 boot_size = be32toh(boot->p_size) * (uint64_t)secsize; 143 boot_offset = be32toh(boot->p_offset) * (uint64_t)secsize; 144 145 /* 146 * We put the entire LIF file into the BOOT partition even when 147 * it doesn't start at the beginning of the disk. 148 * 149 * Maybe we ought to be able to take a binary file and add 150 * it to the LIF filesystem. 151 */ 152 if (boot_size < (uint64_t)params->s1stat.st_size) { 153 warn("BOOT partition too small (%llu < %llu)", 154 (unsigned long long)boot_size, 155 (unsigned long long)params->s1stat.st_size); 156 goto done; 157 } 158 } 159 160 bootstrap = mmap(NULL, params->s1stat.st_size, PROT_READ | PROT_WRITE, 161 MAP_PRIVATE, params->s1fd, 0); 162 if (bootstrap == MAP_FAILED) { 163 warn("mmaping `%s'", params->stage1); 164 goto done; 165 } 166 167 /* Relocate files, sanity check LIF directory on the way */ 168 lifdir = (void *)(bootstrap + HP300_SECTSIZE * 2); 169 for (i = 0; i < 8; lifdir++, i++) { 170 int32_t addr = be32toh(lifdir->dir_addr); 171 int32_t limit = (params->s1stat.st_size - 1) / HP300_SECTSIZE + 1; 172 int32_t end = addr + be32toh(lifdir->dir_length); 173 if (end > limit) { 174 warnx("LIF entry %d larger (%d %d) than LIF file", 175 i, end, limit); 176 goto done; 177 } 178 if (addr != 0 && boot_offset != 0) 179 lifdir->dir_addr = htobe32(addr + boot_offset 180 / HP300_SECTSIZE); 181 } 182 183 if (params->flags & IB_NOWRITE) { 184 retval = 1; 185 goto done; 186 } 187 188 /* Write LIF volume header and directory to sectors 0 and 1 */ 189 rv = pwrite(params->fsfd, bootstrap, 1024, 0); 190 if (rv != 1024) { 191 if (rv == -1) 192 warn("Writing `%s'", params->filesystem); 193 else 194 warnx("Writing `%s': short write", params->filesystem); 195 goto done; 196 } 197 198 /* Write files to BOOT partition */ 199 offset = boot_offset <= HP300_SECTSIZE * 16 ? HP300_SECTSIZE * 16 : 0; 200 i = roundup(params->s1stat.st_size, secsize) - offset; 201 rv = pwrite(params->fsfd, bootstrap + offset, i, boot_offset + offset); 202 if (rv != i) { 203 if (rv == -1) 204 warn("Writing boot filesystem of `%s'", 205 params->filesystem); 206 else 207 warnx("Writing boot filesystem of `%s': short write", 208 params->filesystem); 209 goto done; 210 } 211 212 retval = 1; 213 214 done: 215 if (label != NULL) 216 free(label); 217 if (bootstrap != MAP_FAILED) 218 munmap(bootstrap, params->s1stat.st_size); 219 return retval; 220 } 221