1 /* $NetBSD: news.c,v 1.6 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 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 #if HAVE_NBTOOL_CONFIG_H 40 #include "nbtool_config.h" 41 #endif 42 43 #include <sys/cdefs.h> 44 #if !defined(__lint) 45 __RCSID("$NetBSD: news.c,v 1.6 2006/02/18 10:08:07 dsl Exp $"); 46 #endif /* !__lint */ 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 static int news68k_clearboot(ib_params *); 63 static int news68k_setboot(ib_params *); 64 static int newsmips_clearboot(ib_params *); 65 static int newsmips_setboot(ib_params *); 66 67 struct ib_mach ib_mach_news68k = 68 { "news68k", news68k_setboot, news68k_clearboot, no_editboot, 69 IB_STAGE2START }; 70 71 struct ib_mach ib_mach_newsmips = 72 { "newsmips", newsmips_setboot, newsmips_clearboot, no_editboot, 73 IB_STAGE2START }; 74 75 /* 76 * news68k specific support 77 */ 78 79 static struct bbinfo_params news68k_bbparams = { 80 NEWS68K_BBINFO_MAGIC, 81 NEWS_BOOT_BLOCK_OFFSET, /* write all 8K (including disklabel) */ 82 NEWS_BOOT_BLOCK_BLOCKSIZE, 83 NEWS_BOOT_BLOCK_MAX_SIZE, 84 0, 85 BBINFO_BIG_ENDIAN, 86 }; 87 88 static int 89 news68k_clearboot(ib_params *params) 90 { 91 92 assert(params != NULL); 93 94 return (shared_bbinfo_clearboot(params, &news68k_bbparams, 95 news_copydisklabel)); 96 } 97 98 static int 99 news68k_setboot(ib_params *params) 100 { 101 102 assert(params != NULL); 103 104 return (shared_bbinfo_setboot(params, &news68k_bbparams, 105 news_copydisklabel)); 106 } 107 108 109 /* 110 * newsmips specific support 111 */ 112 113 static struct bbinfo_params newsmips_bbparams = { 114 NEWSMIPS_BBINFO_MAGIC, 115 NEWS_BOOT_BLOCK_OFFSET, /* write all 8K (including disklabel) */ 116 NEWS_BOOT_BLOCK_BLOCKSIZE, 117 NEWS_BOOT_BLOCK_MAX_SIZE, 118 0, 119 BBINFO_BIG_ENDIAN, 120 }; 121 122 static int 123 newsmips_clearboot(ib_params *params) 124 { 125 126 assert(params != NULL); 127 128 return (shared_bbinfo_clearboot(params, &newsmips_bbparams, 129 news_copydisklabel)); 130 } 131 132 static int 133 newsmips_setboot(ib_params *params) 134 { 135 136 assert(params != NULL); 137 138 return (shared_bbinfo_setboot(params, &newsmips_bbparams, 139 news_copydisklabel)); 140 } 141 142 143 /* 144 * news_copydisklabel -- 145 * copy disklabel from existing location on disk into bootstrap, 146 * as the primary bootstrap contains the disklabel. 147 */ 148 static int 149 news_copydisklabel(ib_params *params, struct bbinfo_params *bbparams, 150 uint8_t *bb) 151 { 152 uint8_t boot00[NEWS_BOOT_BLOCK_BLOCKSIZE]; 153 ssize_t rv; 154 155 assert(params != NULL); 156 assert(params->fsfd != -1); 157 assert(bbparams != NULL); 158 assert(bb != NULL); 159 160 /* Read label sector to copy disklabel from */ 161 memset(boot00, 0, sizeof(boot00)); 162 rv = pread(params->fsfd, boot00, sizeof(boot00), 0); 163 if (rv == -1) { 164 warn("Reading label sector from `%s'", params->filesystem); 165 return (0); 166 } 167 /* Copy disklabel */ 168 memcpy(bb + NEWS_BOOT_BLOCK_LABELOFFSET, 169 boot00 + NEWS_BOOT_BLOCK_LABELOFFSET, 170 sizeof(boot00) - NEWS_BOOT_BLOCK_LABELOFFSET); 171 172 return (1); 173 } 174