1 /* $NetBSD: news.c,v 1.3 2002/05/21 14:59:38 tsutsui 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 and Izumi Tsutsui. 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #if defined(__RCSID) && !defined(__lint) 41 __RCSID("$NetBSD: news.c,v 1.3 2002/05/21 14:59:38 tsutsui Exp $"); 42 #endif /* !__lint */ 43 44 #if HAVE_CONFIG_H 45 #include "config.h" 46 #endif 47 48 #include <sys/param.h> 49 50 #include <assert.h> 51 #include <err.h> 52 #include <stddef.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <unistd.h> 57 58 #include "installboot.h" 59 60 static int news_copydisklabel(ib_params *, struct bbinfo_params *, uint8_t *); 61 62 63 /* 64 * news68k specific support 65 */ 66 67 static struct bbinfo_params news68k_bbparams = { 68 NEWS68K_BBINFO_MAGIC, 69 NEWS_BOOT_BLOCK_OFFSET, /* write all 8K (including disklabel) */ 70 NEWS_BOOT_BLOCK_BLOCKSIZE, 71 NEWS_BOOT_BLOCK_MAX_SIZE, 72 0, 73 BBINFO_BIG_ENDIAN, 74 }; 75 76 int 77 news68k_clearboot(ib_params *params) 78 { 79 80 assert(params != NULL); 81 82 if (params->flags & IB_STAGE1START) { 83 warnx("`-b bno' is not supported for %s", 84 params->machine->name); 85 return (0); 86 } 87 return (shared_bbinfo_clearboot(params, &news68k_bbparams, 88 news_copydisklabel)); 89 } 90 91 int 92 news68k_setboot(ib_params *params) 93 { 94 95 assert(params != NULL); 96 97 if (params->flags & IB_STAGE1START) { 98 warnx("`-b bno' is not supported for %s", 99 params->machine->name); 100 return (0); 101 } 102 return (shared_bbinfo_setboot(params, &news68k_bbparams, 103 news_copydisklabel)); 104 } 105 106 107 /* 108 * newsmips specific support 109 */ 110 111 static struct bbinfo_params newsmips_bbparams = { 112 NEWSMIPS_BBINFO_MAGIC, 113 NEWS_BOOT_BLOCK_OFFSET, /* write all 8K (including disklabel) */ 114 NEWS_BOOT_BLOCK_BLOCKSIZE, 115 NEWS_BOOT_BLOCK_MAX_SIZE, 116 0, 117 BBINFO_BIG_ENDIAN, 118 }; 119 120 int 121 newsmips_clearboot(ib_params *params) 122 { 123 124 assert(params != NULL); 125 126 if (params->flags & IB_STAGE1START) { 127 warnx("`-b bno' is not supported for %s", 128 params->machine->name); 129 return (0); 130 } 131 return (shared_bbinfo_clearboot(params, &newsmips_bbparams, 132 news_copydisklabel)); 133 } 134 135 int 136 newsmips_setboot(ib_params *params) 137 { 138 139 assert(params != NULL); 140 141 if (params->flags & IB_STAGE1START) { 142 warnx("`-b bno' is not supported for %s", 143 params->machine->name); 144 return (0); 145 } 146 return (shared_bbinfo_setboot(params, &newsmips_bbparams, 147 news_copydisklabel)); 148 } 149 150 151 /* 152 * news_copydisklabel -- 153 * copy disklabel from existing location on disk into bootstrap, 154 * as the primary bootstrap contains the disklabel. 155 */ 156 static int 157 news_copydisklabel(ib_params *params, struct bbinfo_params *bbparams, 158 uint8_t *bb) 159 { 160 uint8_t boot00[NEWS_BOOT_BLOCK_BLOCKSIZE]; 161 ssize_t rv; 162 163 assert(params != NULL); 164 assert(params->fsfd != -1); 165 assert(bbparams != NULL); 166 assert(bb != NULL); 167 168 /* Read label sector to copy disklabel from */ 169 memset(boot00, 0, sizeof(boot00)); 170 rv = pread(params->fsfd, boot00, sizeof(boot00), 0); 171 if (rv == -1) { 172 warn("Reading label sector from `%s'", params->filesystem); 173 return (0); 174 } 175 /* Copy disklabel */ 176 memcpy(bb + NEWS_BOOT_BLOCK_LABELOFFSET, 177 boot00 + NEWS_BOOT_BLOCK_LABELOFFSET, 178 sizeof(boot00) - NEWS_BOOT_BLOCK_LABELOFFSET); 179 180 return (1); 181 } 182