1 /* $NetBSD: dkwedge_apple.c,v 1.1 2012/04/07 05:36:10 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Apple support for disk wedges 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: dkwedge_apple.c,v 1.1 2012/04/07 05:36:10 christos Exp $"); 38 39 #include <sys/param.h> 40 #ifdef _KERNEL 41 #include <sys/systm.h> 42 #endif 43 #include <sys/proc.h> 44 #include <sys/errno.h> 45 #include <sys/disk.h> 46 #include <sys/vnode.h> 47 #include <sys/malloc.h> 48 #include <sys/bitops.h> 49 50 #include <sys/bootblock.h> 51 52 #define SWAP16(x) ap->x = be16toh(ap->x) 53 #define SWAP32(x) ap->x = be32toh(ap->x) 54 55 #ifdef DKWEDGE_DEBUG 56 #define DPRINTF(fmt, ...) printf(fmt, __VA_ARGS__) 57 #else 58 #define DPRINTF(fmt, ...) 59 #endif 60 61 static void 62 swap_apple_drvr_descriptor(struct apple_drvr_descriptor *ap) 63 { 64 SWAP32(descBlock); 65 SWAP16(descSize); 66 SWAP16(descType); 67 } 68 69 static void 70 swap_apple_drvr_map(struct apple_drvr_map *ap) 71 { 72 uint16_t i; 73 74 SWAP16(sbSig); 75 SWAP16(sbBlockSize); 76 SWAP32(sbBlkCount); 77 SWAP16(sbDevType); 78 SWAP16(sbDevID); 79 SWAP32(sbData); 80 SWAP16(sbDrvrCount); 81 82 if (ap->sbDrvrCount >= APPLE_DRVR_MAP_MAX_DESCRIPTORS) 83 ap->sbDrvrCount = APPLE_DRVR_MAP_MAX_DESCRIPTORS; 84 85 for (i = 0; i < ap->sbDrvrCount; i++) 86 swap_apple_drvr_descriptor(&ap->sb_dd[i]); 87 } 88 89 static void 90 swap_apple_part_map_entry(struct apple_part_map_entry *ap) 91 { 92 SWAP16(pmSig); 93 SWAP16(pmSigPad); 94 SWAP32(pmMapBlkCnt); 95 SWAP32(pmPyPartStart); 96 SWAP32(pmPartBlkCnt); 97 SWAP32(pmLgDataStart); 98 SWAP32(pmDataCnt); 99 SWAP32(pmPartStatus); 100 SWAP32(pmLgBootStart); 101 SWAP32(pmBootSize); 102 SWAP32(pmBootLoad); 103 SWAP32(pmBootLoad2); 104 SWAP32(pmBootEntry); 105 SWAP32(pmBootEntry2); 106 SWAP32(pmBootCksum); 107 } 108 109 #undef SWAP16 110 #undef SWAP32 111 112 #define ASIZE 16384 113 114 #ifdef _KERNEL 115 #define DKW_MALLOC(SZ) malloc((SZ), M_DEVBUF, M_WAITOK) 116 #define DKW_FREE(PTR) free((PTR), M_DEVBUF) 117 #else 118 #define DKW_MALLOC(SZ) malloc((SZ)) 119 #define DKW_FREE(PTR) free((PTR)) 120 #endif 121 122 static struct { 123 const char *name; 124 const char *type; 125 } map[] = { 126 { APPLE_PART_TYPE_UNIX, DKW_PTYPE_SYSV }, 127 { APPLE_PART_TYPE_MAC, DKW_PTYPE_APPLEHFS }, 128 }; 129 130 131 static int 132 dkwedge_discover_apple(struct disk *pdk, struct vnode *vp) 133 { 134 size_t i; 135 int error; 136 void *buf; 137 uint32_t blocksize, offset, rsize; 138 struct apple_drvr_map *am; 139 struct apple_part_map_entry *ae; 140 141 buf = DKW_MALLOC(ASIZE); 142 if ((error = dkwedge_read(pdk, vp, 0, buf, ASIZE)) != 0) { 143 DPRINTF("%s: read @%u %d\n", __func__, 0, error); 144 goto out; 145 } 146 147 am = buf; 148 swap_apple_drvr_map(am); 149 150 error = ESRCH; 151 152 if (am->sbSig != APPLE_DRVR_MAP_MAGIC) { 153 DPRINTF("%s: drvr magic %x != %x\n", __func__, am->sbSig, 154 APPLE_DRVR_MAP_MAGIC); 155 goto out; 156 } 157 158 blocksize = am->sbBlockSize; 159 160 rsize = 1 << (ilog2(MAX(sizeof(*ae), blocksize) - 1) + 1); 161 if (ASIZE < rsize) { 162 DPRINTF("%s: buffer too small %u < %u\n", __func__, ASIZE, 163 rsize); 164 goto out; 165 } 166 167 ae = buf; 168 for (offset = blocksize;; offset += rsize) { 169 DPRINTF("%s: offset %x rsize %x\n", __func__, offset, rsize); 170 if ((error = dkwedge_read(pdk, vp, offset / DEV_BSIZE, buf, 171 rsize)) != 0) { 172 DPRINTF("%s: read @%u %d\n", __func__, offset, 173 error); 174 goto out; 175 } 176 177 swap_apple_part_map_entry(ae); 178 if (ae->pmSig != APPLE_PART_MAP_ENTRY_MAGIC) { 179 DPRINTF("%s: part magic %x != %x\n", __func__, 180 ae->pmSig, APPLE_PART_MAP_ENTRY_MAGIC); 181 break; 182 } 183 184 for (i = 0; i < __arraycount(map); i++) 185 if (strcasecmp(map[i].name, ae->pmPartType) == 0) 186 break; 187 188 DPRINTF("%s: %s/%s PH=%u/%u LG=%u/%u\n", __func__, 189 ae->pmPartName, ae->pmPartType, 190 ae->pmPyPartStart, ae->pmPartBlkCnt, 191 ae->pmLgDataStart, ae->pmDataCnt); 192 193 if (i == __arraycount(map)) 194 continue; 195 196 struct dkwedge_info dkw; 197 198 strcpy(dkw.dkw_ptype, map[i].type); 199 strcpy(dkw.dkw_parent, pdk->dk_name); 200 dkw.dkw_offset = ae->pmPyPartStart; 201 dkw.dkw_size = ae->pmPartBlkCnt; 202 strlcpy(dkw.dkw_wname, ae->pmPartName, sizeof(dkw.dkw_wname)); 203 error = dkwedge_add(&dkw); 204 if (error == EEXIST) 205 aprint_error("%s: wedge named '%s' already " 206 "exists, manual intervention required\n", 207 pdk->dk_name, dkw.dkw_wname); 208 else if (error) 209 aprint_error("%s: error %d adding partition " 210 "%s type %s\n", pdk->dk_name, error, 211 ae->pmPartType, dkw.dkw_ptype); 212 } 213 214 out: 215 DKW_FREE(buf); 216 DPRINTF("%s: return %d\n", __func__, error); 217 return error; 218 } 219 220 #ifdef _KERNEL 221 DKWEDGE_DISCOVERY_METHOD_DECL(APPLE, -5, dkwedge_discover_apple); 222 #endif 223