1 /* $NetBSD: news.c,v 1.5 2003/10/27 00:12:44 lukem 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.5 2003/10/27 00:12:44 lukem 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 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 return (shared_bbinfo_clearboot(params, &news68k_bbparams, 83 news_copydisklabel)); 84 } 85 86 int 87 news68k_setboot(ib_params *params) 88 { 89 90 assert(params != NULL); 91 92 return (shared_bbinfo_setboot(params, &news68k_bbparams, 93 news_copydisklabel)); 94 } 95 96 97 /* 98 * newsmips specific support 99 */ 100 101 static struct bbinfo_params newsmips_bbparams = { 102 NEWSMIPS_BBINFO_MAGIC, 103 NEWS_BOOT_BLOCK_OFFSET, /* write all 8K (including disklabel) */ 104 NEWS_BOOT_BLOCK_BLOCKSIZE, 105 NEWS_BOOT_BLOCK_MAX_SIZE, 106 0, 107 BBINFO_BIG_ENDIAN, 108 }; 109 110 int 111 newsmips_clearboot(ib_params *params) 112 { 113 114 assert(params != NULL); 115 116 return (shared_bbinfo_clearboot(params, &newsmips_bbparams, 117 news_copydisklabel)); 118 } 119 120 int 121 newsmips_setboot(ib_params *params) 122 { 123 124 assert(params != NULL); 125 126 return (shared_bbinfo_setboot(params, &newsmips_bbparams, 127 news_copydisklabel)); 128 } 129 130 131 /* 132 * news_copydisklabel -- 133 * copy disklabel from existing location on disk into bootstrap, 134 * as the primary bootstrap contains the disklabel. 135 */ 136 static int 137 news_copydisklabel(ib_params *params, struct bbinfo_params *bbparams, 138 uint8_t *bb) 139 { 140 uint8_t boot00[NEWS_BOOT_BLOCK_BLOCKSIZE]; 141 ssize_t rv; 142 143 assert(params != NULL); 144 assert(params->fsfd != -1); 145 assert(bbparams != NULL); 146 assert(bb != NULL); 147 148 /* Read label sector to copy disklabel from */ 149 memset(boot00, 0, sizeof(boot00)); 150 rv = pread(params->fsfd, boot00, sizeof(boot00), 0); 151 if (rv == -1) { 152 warn("Reading label sector from `%s'", params->filesystem); 153 return (0); 154 } 155 /* Copy disklabel */ 156 memcpy(bb + NEWS_BOOT_BLOCK_LABELOFFSET, 157 boot00 + NEWS_BOOT_BLOCK_LABELOFFSET, 158 sizeof(boot00) - NEWS_BOOT_BLOCK_LABELOFFSET); 159 160 return (1); 161 } 162