1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
/* $NetBSD: dkwedge_mbr.c,v 1.12 2020/04/11 16:00:34 jdolecek 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
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: dkwedge_mbr.c,v 1.12 2020/04/11 16:00:34 jdolecek Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/errno.h>
#include <sys/disk.h>
#include <sys/vnode.h>
#include <sys/buf.h>
#include <sys/bootblock.h>
#include <sys/disklabel.h>
typedef struct mbr_args {
struct disk *pdk;
struct vnode *vp;
struct buf *bp;
int error;
uint32_t secsize;
int mbr_count;
} mbr_args_t;
static const char *
mbr_ptype_to_str(uint8_t ptype)
{
const char *str;
switch (ptype) {
case MBR_PTYPE_FAT12: str = DKW_PTYPE_FAT; break;
case MBR_PTYPE_FAT16S: str = DKW_PTYPE_FAT; break;
case MBR_PTYPE_FAT16B: str = DKW_PTYPE_FAT; break;
case MBR_PTYPE_NTFS: str = DKW_PTYPE_NTFS; break;
case MBR_PTYPE_FAT32: str = DKW_PTYPE_FAT; break;
case MBR_PTYPE_FAT32L: str = DKW_PTYPE_FAT; break;
case MBR_PTYPE_FAT16L: str = DKW_PTYPE_FAT; break;
case MBR_PTYPE_LNXSWAP: str = DKW_PTYPE_SWAP; break;
case MBR_PTYPE_LNXEXT2: str = DKW_PTYPE_EXT2FS; break;
case MBR_PTYPE_APPLE_UFS:str = DKW_PTYPE_APPLEUFS; break;
case MBR_PTYPE_EFI: str = DKW_PTYPE_FAT; break;
default: str = NULL; break;
}
return (str);
}
static void
getparts(mbr_args_t *a, uint32_t off, uint32_t extoff)
{
struct dkwedge_info dkw;
struct mbr_partition *dp;
struct mbr_sector *mbr;
const char *ptype;
int i, error;
error = dkwedge_read(a->pdk, a->vp, off, a->bp->b_data, a->secsize);
if (error) {
aprint_error("%s: unable to read MBR @ %u/%u, "
"error = %d\n", a->pdk->dk_name, off, a->secsize, a->error);
a->error = error;
return;
}
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));
strlcpy(dkw.dkw_parent, a->pdk->dk_name, sizeof(dkw.dkw_parent));
dkw.dkw_offset = le32toh(dp[i].mbrp_start);
dkw.dkw_size = le32toh(dp[i].mbrp_size);
/*
* 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;
if (MBR_IS_EXTENDED(dp[i].mbrp_type)) {
poff = le32toh(dp[i].mbrp_start) + extoff;
getparts(a, poff, extoff ? extoff : poff);
}
}
}
static int
dkwedge_discover_mbr(struct disk *pdk, struct vnode *vp)
{
mbr_args_t a;
memset(&a, 0, sizeof(a));
a.pdk = pdk;
a.secsize = DEV_BSIZE << pdk->dk_blkshift;
a.vp = vp;
a.bp = geteblk(a.secsize);
a.error = 0;
a.mbr_count = 0;
getparts(&a, MBR_BBSECTOR, 0);
if (a.mbr_count != 0)
a.error = 0; /* found it, wedges installed */
else if (a.error == 0)
a.error = ESRCH; /* no MBRs found */
brelse(a.bp, 0);
return (a.error);
}
DKWEDGE_DISCOVERY_METHOD_DECL(MBR, 10, dkwedge_discover_mbr);
|