1 /* $NetBSD: x68k.c,v 1.5 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: x68k.c,v 1.5 2019/05/07 04:35:31 thorpej Exp $"); 39 #endif /* !__lint */ 40 41 #include <sys/param.h> 42 #include <sys/stat.h> 43 44 #include <assert.h> 45 #include <err.h> 46 #include <stddef.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <unistd.h> 51 52 #include "installboot.h" 53 54 #define X68K_LABELOFFSET 64 55 #define X68K_LABELSIZE 404 /* reserve 16 partitions */ 56 57 static int x68k_clearheader(ib_params *, struct bbinfo_params *, uint8_t *); 58 59 static int x68k_clearboot(ib_params *); 60 static int x68k_setboot(ib_params *); 61 62 struct ib_mach ib_mach_x68k = { 63 .name = "x68k", 64 .setboot = x68k_setboot, 65 .clearboot = x68k_clearboot, 66 .editboot = no_editboot, 67 .valid_flags = IB_STAGE1START | IB_STAGE2START, 68 }; 69 70 static struct bbinfo_params bbparams = { 71 X68K_BBINFO_MAGIC, 72 X68K_BOOT_BLOCK_OFFSET, 73 X68K_BOOT_BLOCK_BLOCKSIZE, 74 X68K_BOOT_BLOCK_MAX_SIZE, 75 X68K_LABELOFFSET + X68K_LABELSIZE, /* XXX */ 76 BBINFO_BIG_ENDIAN, 77 }; 78 79 static int 80 x68k_clearboot(ib_params *params) 81 { 82 83 assert(params != NULL); 84 85 if (params->flags & IB_STAGE1START) { 86 warnx("`-b bno' is not supported for %s", 87 params->machine->name); 88 return 0; 89 } 90 return shared_bbinfo_clearboot(params, &bbparams, x68k_clearheader); 91 } 92 93 static int 94 x68k_clearheader(ib_params *params, struct bbinfo_params *bb_params, 95 uint8_t *bb) 96 { 97 98 assert(params != NULL); 99 assert(bb_params != NULL); 100 assert(bb != NULL); 101 102 memset(bb, 0, X68K_LABELOFFSET); 103 return 1; 104 } 105 106 static int 107 x68k_setboot(ib_params *params) 108 { 109 struct stat bootstrapsb; 110 char bb[X68K_BOOT_BLOCK_MAX_SIZE]; 111 char label[X68K_LABELSIZE]; 112 uint32_t s1start; 113 int retval; 114 ssize_t rv; 115 116 assert(params != NULL); 117 assert(params->fsfd != -1); 118 assert(params->filesystem != NULL); 119 assert(params->s1fd != -1); 120 assert(params->stage1 != NULL); 121 122 retval = 0; 123 124 if (params->flags & IB_STAGE1START) 125 s1start = params->s1start; 126 else 127 s1start = X68K_BOOT_BLOCK_OFFSET / 128 X68K_BOOT_BLOCK_BLOCKSIZE; 129 130 /* read disklabel on the target disk */ 131 rv = pread(params->fsfd, label, sizeof label, 132 s1start * X68K_BOOT_BLOCK_BLOCKSIZE + X68K_LABELOFFSET); 133 if (rv == -1) { 134 warn("Reading `%s'", params->filesystem); 135 goto done; 136 } else if (rv != sizeof label) { 137 warnx("Reading `%s': short read", params->filesystem); 138 goto done; 139 } 140 141 if (fstat(params->s1fd, &bootstrapsb) == -1) { 142 warn("Examining `%s'", params->stage1); 143 goto done; 144 } 145 if (!S_ISREG(bootstrapsb.st_mode)) { 146 warnx("`%s' must be a regular file", params->stage1); 147 goto done; 148 } 149 150 /* read boot loader */ 151 memset(&bb, 0, sizeof bb); 152 rv = read(params->s1fd, &bb, sizeof bb); 153 if (rv == -1) { 154 warn("Reading `%s'", params->stage1); 155 goto done; 156 } 157 /* then, overwrite disklabel */ 158 memcpy(&bb[X68K_LABELOFFSET], &label, sizeof label); 159 160 if (params->flags & IB_VERBOSE) { 161 printf("Bootstrap start sector: %#x\n", s1start); 162 printf("Bootstrap byte count: %#x\n", (unsigned)rv); 163 printf("%sriting bootstrap\n", 164 (params->flags & IB_NOWRITE) ? "Not w" : "W"); 165 } 166 if (params->flags & IB_NOWRITE) { 167 retval = 1; 168 goto done; 169 } 170 171 /* write boot loader and disklabel into the target disk */ 172 rv = pwrite(params->fsfd, &bb, X68K_BOOT_BLOCK_MAX_SIZE, 173 s1start * X68K_BOOT_BLOCK_BLOCKSIZE); 174 if (rv == -1) { 175 warn("Writing `%s'", params->filesystem); 176 goto done; 177 } else if (rv != X68K_BOOT_BLOCK_MAX_SIZE) { 178 warnx("Writing `%s': short write", params->filesystem); 179 goto done; 180 } else 181 retval = 1; 182 183 done: 184 return (retval); 185 } 186