/* $NetBSD: dkwedge_mbr.c,v 1.13 2024/02/26 21:55:05 charlotte Exp $ */
/*-
* Copyright (c) 2004 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Master Boot Record partition table support for disk wedges
*/
mbr = a->bp->b_data;
if (mbr->mbr_magic != htole16(MBR_MAGIC))
return;
dp = mbr->mbr_parts;
for (i = 0; i < MBR_PART_COUNT; i++) {
switch (dp[i].mbrp_type) {
case 0: /* empty */
case MBR_PTYPE_PMBR: /* Handled by GPT */
continue;
default:
/* Extended partitions are handled below. */
if (MBR_IS_EXTENDED(dp[i].mbrp_type))
continue;
break;
}
memset(&dkw, 0, sizeof(dkw));
if ((ptype = mbr_ptype_to_str(dp[i].mbrp_type)) == NULL) {
/*
* XXX Should probably just add these...
* XXX maybe just have an empty ptype?
*/
aprint_verbose("%s: skipping partition %d, "
"type 0x%02x\n", a->pdk->dk_name, i,
dp[i].mbrp_type);
continue;
}
strlcpy(dkw.dkw_ptype, ptype, sizeof(dkw.dkw_ptype));
/*
* These get historical disk naming style
* wedge names. We start at 'e', and reserve
* 4 slots for each MBR we parse.
*
* XXX For FAT, we should extract the FAT volume
* XXX name.
*/
snprintf(dkw.dkw_wname, sizeof(dkw.dkw_wname),
"%s%c", a->pdk->dk_name,
'e' + (a->mbr_count * MBR_PART_COUNT) + i);
error = dkwedge_add(&dkw);
if (error == EEXIST)
aprint_error("%s: wedge named '%s' already "
"exists, manual intervention required\n",
a->pdk->dk_name, dkw.dkw_wname);
else if (error)
aprint_error("%s: error %d adding partition "
"%d type 0x%02x\n", a->pdk->dk_name, error,
(a->mbr_count * MBR_PART_COUNT) + i,
dp[i].mbrp_type);
}
/* We've parsed one MBR. */
a->mbr_count++;
/* Recursively scan extended partitions. */
for (i = 0; i < MBR_PART_COUNT; i++) {
uint32_t poff;