1 /* $NetBSD: mbr.c,v 1.1 2009/09/11 12:00:12 phx Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 5 * Copyright (C) 1995, 1996 TooLs GmbH. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by TooLs GmbH. 19 * 4. The name of TooLs GmbH may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/bootblock.h> 36 37 #include <lib/libsa/byteorder.h> 38 #include <lib/libsa/stand.h> 39 40 #include "mbr.h" 41 42 43 static u_long 44 get_long(const void *p) 45 { 46 const unsigned char *cp = p; 47 48 return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24); 49 } 50 51 52 /* 53 * Find a valid MBR disklabel. 54 */ 55 int 56 search_mbr_label(struct of_dev *devp, u_long off, char *buf, 57 struct disklabel *lp, u_long off0) 58 { 59 size_t read; 60 struct mbr_partition *p; 61 int i; 62 u_long poff; 63 static int recursion; 64 65 if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read) 66 || read != DEV_BSIZE) 67 return ERDLAB; 68 69 if (*(u_int16_t *)&buf[MBR_MAGIC_OFFSET] != sa_htole16(MBR_MAGIC)) 70 return ERDLAB; 71 72 if (recursion++ <= 1) 73 off0 += off; 74 for (p = (struct mbr_partition *)(buf + MBR_PART_OFFSET), i = 0; 75 i < MBR_PART_COUNT; i++, p++) { 76 if (p->mbrp_type == MBR_PTYPE_NETBSD 77 #ifdef COMPAT_386BSD_MBRPART 78 || (p->mbrp_type == MBR_PTYPE_386BSD && 79 (printf("WARNING: old BSD partition ID!\n"), 1) 80 /* XXX XXX - libsa printf() is void */ ) 81 #endif 82 ) { 83 poff = get_long(&p->mbrp_start) + off0; 84 if (strategy(devp, F_READ, poff + LABELSECTOR, 85 DEV_BSIZE, buf, &read) == 0 86 && read == DEV_BSIZE) { 87 if (!getdisklabel(buf, lp)) { 88 recursion--; 89 return 0; 90 } 91 } 92 if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read) 93 || read != DEV_BSIZE) { 94 recursion--; 95 return ERDLAB; 96 } 97 } else if (p->mbrp_type == MBR_PTYPE_EXT) { 98 poff = get_long(&p->mbrp_start); 99 if (!search_mbr_label(devp, poff, buf, lp, off0)) { 100 recursion--; 101 return 0; 102 } 103 if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read) 104 || read != DEV_BSIZE) { 105 recursion--; 106 return ERDLAB; 107 } 108 } 109 } 110 111 recursion--; 112 return ERDLAB; 113 } 114