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