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