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