xref: /netbsd-src/sys/dev/dkwedge/dkwedge_apple.c (revision 89c40d7e6d8331b8b56615fed77be1b42a5efc4d)
1 /*	$NetBSD: dkwedge_apple.c,v 1.6 2020/04/11 16:00:34 jdolecek 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.6 2020/04/11 16:00:34 jdolecek 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/bitops.h>
48 #include <sys/buf.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
swap_apple_drvr_descriptor(struct apple_drvr_descriptor * ap)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
swap_apple_drvr_map(struct apple_drvr_map * ap)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
swap_apple_part_map_entry(struct apple_part_map_entry * ap)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 static void
swap_apple_blockzeroblock(struct apple_blockzeroblock * ap)110 swap_apple_blockzeroblock(struct apple_blockzeroblock *ap)
111 {
112         SWAP32(bzbMagic);
113         SWAP16(bzbBadBlockInode);
114         SWAP16(bzbFlags);
115         SWAP16(bzbReserved);
116         SWAP32(bzbCreationTime);
117         SWAP32(bzbMountTime);
118         SWAP32(bzbUMountTime);
119 }
120 
121 #undef SWAP16
122 #undef SWAP32
123 
124 #define ASIZE	16384
125 
126 static struct {
127 	const char *name;
128 	const char *type;
129 } map[] = {
130 	{ APPLE_PART_TYPE_UNIX, DKW_PTYPE_SYSV },
131 	{ APPLE_PART_TYPE_MAC, DKW_PTYPE_APPLEHFS },
132 };
133 
134 
135 static int
dkwedge_discover_apple(struct disk * pdk,struct vnode * vp)136 dkwedge_discover_apple(struct disk *pdk, struct vnode *vp)
137 {
138 	size_t i, n;
139 	int error;
140 	struct buf *bp;
141 	uint32_t blocksize, blockcount, offset, rsize;
142 	struct apple_drvr_map *am;
143 	struct apple_part_map_entry *ae;
144 	struct apple_blockzeroblock ab;
145 	const char *ptype;
146 
147 	bp = geteblk(ASIZE);
148 	if ((error = dkwedge_read(pdk, vp, 0, bp->b_data, ASIZE)) != 0) {
149 		DPRINTF("%s: read @%u %d\n", __func__, 0, error);
150 		goto out;
151 	}
152 
153 	am = bp->b_data;
154 	swap_apple_drvr_map(am);
155 
156 	error = ESRCH;
157 
158 	if (am->sbSig != APPLE_DRVR_MAP_MAGIC) {
159 		DPRINTF("%s: drvr magic %x != %x\n", __func__, am->sbSig,
160 		    APPLE_DRVR_MAP_MAGIC);
161 		goto out;
162 	}
163 
164 	blocksize = am->sbBlockSize;
165 
166 	rsize = 1 << (ilog2(MAX(sizeof(*ae), blocksize) - 1) + 1);
167 	if (ASIZE < rsize) {
168 		DPRINTF("%s: buffer too small %u < %u\n", __func__, ASIZE,
169 		    rsize);
170 		goto out;
171 	}
172 
173 	/* XXX Clamp entries at 512 for now. */
174 	blockcount = am->sbBlkCount;
175 	if (blockcount > 512) {
176 		aprint_error("%s: WARNING: clamping number of blocks to "
177 		    "512 (was %u)\n", pdk->dk_name, blockcount);
178 		blockcount = 512;
179 	}
180 
181 	ae = bp->b_data;
182 	offset = blocksize;
183 	for (n = 0; n < blockcount; n++, offset += rsize) {
184 		DPRINTF("%s: offset %x rsize %x\n", __func__, offset, rsize);
185 		if ((error = dkwedge_read(pdk, vp, offset / DEV_BSIZE,
186 		    bp->b_data, rsize)) != 0) {
187 			DPRINTF("%s: read @%u %d\n", __func__, offset,
188 			    error);
189 			goto out;
190 		}
191 
192 		swap_apple_part_map_entry(ae);
193 		if (ae->pmSig != APPLE_PART_MAP_ENTRY_MAGIC) {
194 			DPRINTF("%s: part magic %x != %x\n", __func__,
195 			    ae->pmSig, APPLE_PART_MAP_ENTRY_MAGIC);
196 			break;
197 		}
198 
199 		for (i = 0; i < __arraycount(map); i++)
200 			if (strcasecmp(map[i].name, ae->pmPartType) == 0)
201 				break;
202 
203 		DPRINTF("%s: %s/%s PH=%u/%u LG=%u/%u\n", __func__,
204 		    ae->pmPartName, ae->pmPartType,
205 		    ae->pmPyPartStart, ae->pmPartBlkCnt,
206 		    ae->pmLgDataStart, ae->pmDataCnt);
207 
208 		if (i == __arraycount(map))
209 			continue;
210 
211 		ptype = map[i].type;
212 		memcpy(&ab, ae->pmBootArgs, sizeof(ab));
213 		swap_apple_blockzeroblock(&ab);
214 		if (ab.bzbMagic == APPLE_BZB_MAGIC) {
215 			if (ab.bzbType == APPLE_BZB_TYPESWAP)
216 				ptype = DKW_PTYPE_SWAP;
217 		}
218 
219 		struct dkwedge_info dkw;
220 		memset(&dkw, 0, sizeof(dkw));
221 
222 		strlcpy(dkw.dkw_ptype, ptype, sizeof(dkw.dkw_ptype));
223 		strlcpy(dkw.dkw_parent, pdk->dk_name, sizeof(dkw.dkw_parent));
224 		dkw.dkw_offset = ae->pmPyPartStart;
225 		dkw.dkw_size = ae->pmPartBlkCnt;
226 		strlcpy(dkw.dkw_wname, ae->pmPartName, sizeof(dkw.dkw_wname));
227 		error = dkwedge_add(&dkw);
228 		if (error == EEXIST)
229 			aprint_error("%s: wedge named '%s' already "
230 			    "exists, manual intervention required\n",
231 			    pdk->dk_name, dkw.dkw_wname);
232 		else if (error)
233 			aprint_error("%s: error %d adding partition "
234 			    "%s type %s\n", pdk->dk_name, error,
235 			    ae->pmPartType, dkw.dkw_ptype);
236 	}
237 
238 out:
239 	brelse(bp, 0);
240 	DPRINTF("%s: return %d\n", __func__, error);
241 	return error;
242 }
243 
244 #ifdef _KERNEL
245 DKWEDGE_DISCOVERY_METHOD_DECL(APPLE, -5, dkwedge_discover_apple);
246 #endif
247