1 /* $NetBSD: amiga.c,v 1.8 2013/06/14 03:54:43 msaitoh Exp $ */ 2 3 /*- 4 * Copyright (c) 1999, 2002 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Michael Hitch. 9 * 10 * This code is derived from software contributed to The NetBSD Foundation 11 * by Luke Mewburn of Wasabi Systems. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #if HAVE_NBTOOL_CONFIG_H 36 #include "nbtool_config.h" 37 #endif 38 39 #include <sys/cdefs.h> 40 #if !defined(__lint) 41 __RCSID("$NetBSD: amiga.c,v 1.8 2013/06/14 03:54:43 msaitoh Exp $"); 42 #endif /* !__lint */ 43 44 #include <sys/param.h> 45 #include <sys/stat.h> 46 47 #include <assert.h> 48 #include <err.h> 49 #include <stddef.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <unistd.h> 53 #include <string.h> 54 55 #include "installboot.h" 56 57 /* XXX Must be kept in sync with bbstart.s! */ 58 #define CMDLN_LOC 0x10 59 #define CMDLN_LEN 0x20 60 61 #define CHKSUMOFFS 1 62 63 u_int32_t chksum(u_int32_t *, int); 64 65 static int amiga_setboot(ib_params *); 66 67 struct ib_mach ib_mach_amiga = 68 { "amiga", amiga_setboot, no_clearboot, no_editboot, 69 IB_STAGE1START | IB_STAGE2START | IB_COMMAND }; 70 71 static int 72 amiga_setboot(ib_params *params) 73 { 74 int retval; 75 ssize_t rv; 76 char *dline; 77 int sumlen; 78 u_int32_t sum2, sum16; 79 80 struct stat bootstrapsb; 81 82 u_int32_t block[128*16]; 83 84 retval = 0; 85 if (fstat(params->s1fd, &bootstrapsb) == -1) { 86 warn("Examining `%s'", params->stage1); 87 goto done; 88 } 89 if (!S_ISREG(bootstrapsb.st_mode)) { 90 warnx("`%s' must be a regular file", params->stage1); 91 goto done; 92 } 93 94 rv = pread(params->s1fd, &block, sizeof(block), 0); 95 if (rv == -1) { 96 warn("Reading `%s'", params->stage1); 97 goto done; 98 } else if (rv != sizeof(block)) { 99 warnx("Reading `%s': short read", params->stage1); 100 goto done; 101 } 102 103 /* XXX the choices should not be hardcoded */ 104 105 sum2 = chksum(block, 1024/4); 106 sum16 = chksum(block, 8192/4); 107 108 if (sum16 == 0xffffffff) { 109 sumlen = 8192/4; 110 } else if (sum2 == 0xffffffff) { 111 sumlen = 1024/4; 112 } else { 113 errx(1, "%s: wrong checksum", params->stage1); 114 /* NOTREACHED */ 115 } 116 117 if (sum2 == sum16) { 118 warnx("eek - both sums are the same"); 119 } 120 121 if (params->flags & IB_COMMAND) { 122 dline = (char *)&(block[CMDLN_LOC/4]); 123 /* XXX keep the default default line in sync with bbstart.s */ 124 if (strcmp(dline, "netbsd -ASn2") != 0) { 125 errx(1, "Old bootblock version? Can't change command line."); 126 } 127 (void)strncpy(dline, params->command, CMDLN_LEN-1); 128 129 block[1] = 0; 130 block[1] = 0xffffffff - chksum(block, sumlen); 131 } 132 133 if (params->flags & IB_NOWRITE) { 134 retval = 1; 135 goto done; 136 } 137 138 if (params->flags & IB_VERBOSE) 139 printf("Writing boot block\n"); 140 rv = pwrite(params->fsfd, &block, sizeof(block), 0); 141 if (rv == -1) { 142 warn("Writing `%s'", params->filesystem); 143 goto done; 144 } else if (rv != sizeof(block)) { 145 warnx("Writing `%s': short write", params->filesystem); 146 goto done; 147 } else { 148 retval = 1; 149 } 150 151 done: 152 return (retval); 153 } 154 155 u_int32_t 156 chksum(block, size) 157 u_int32_t *block; 158 int size; 159 { 160 u_int32_t sum, lastsum; 161 int i; 162 163 sum = 0; 164 165 for (i=0; i<size; i++) { 166 lastsum = sum; 167 sum += htobe32(block[i]); 168 if (sum < lastsum) 169 ++sum; 170 } 171 172 return sum; 173 } 174