efiblock.c revision 1.2.2.4 1 1.2.2.4 pgoyette /* $NetBSD: efiblock.c,v 1.2.2.4 2018/11/26 01:52:52 pgoyette Exp $ */
2 1.2.2.2 pgoyette
3 1.2.2.2 pgoyette /*-
4 1.2.2.2 pgoyette * Copyright (c) 2016 Kimihiro Nonaka <nonaka (at) netbsd.org>
5 1.2.2.2 pgoyette * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
6 1.2.2.2 pgoyette * All rights reserved.
7 1.2.2.2 pgoyette *
8 1.2.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
9 1.2.2.2 pgoyette * modification, are permitted provided that the following conditions
10 1.2.2.2 pgoyette * are met:
11 1.2.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
12 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
13 1.2.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
14 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
15 1.2.2.2 pgoyette * documentation and/or other materials provided with the distribution.
16 1.2.2.2 pgoyette *
17 1.2.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 1.2.2.2 pgoyette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.2.2.2 pgoyette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.2.2.2 pgoyette * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 1.2.2.2 pgoyette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.2.2.2 pgoyette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.2.2.2 pgoyette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.2.2.2 pgoyette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.2.2.2 pgoyette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.2.2.2 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.2.2.2 pgoyette * SUCH DAMAGE.
28 1.2.2.2 pgoyette */
29 1.2.2.2 pgoyette
30 1.2.2.2 pgoyette #define FSTYPENAMES
31 1.2.2.2 pgoyette
32 1.2.2.2 pgoyette #include <sys/param.h>
33 1.2.2.2 pgoyette #include <sys/md5.h>
34 1.2.2.4 pgoyette #include <sys/uuid.h>
35 1.2.2.2 pgoyette
36 1.2.2.2 pgoyette #include "efiboot.h"
37 1.2.2.2 pgoyette #include "efiblock.h"
38 1.2.2.2 pgoyette
39 1.2.2.2 pgoyette static EFI_HANDLE *efi_block;
40 1.2.2.2 pgoyette static UINTN efi_nblock;
41 1.2.2.2 pgoyette static struct efi_block_part *efi_block_booted = NULL;
42 1.2.2.2 pgoyette
43 1.2.2.2 pgoyette static TAILQ_HEAD(, efi_block_dev) efi_block_devs = TAILQ_HEAD_INITIALIZER(efi_block_devs);
44 1.2.2.2 pgoyette
45 1.2.2.2 pgoyette static int
46 1.2.2.2 pgoyette efi_block_parse(const char *fname, struct efi_block_part **pbpart, char **pfile)
47 1.2.2.2 pgoyette {
48 1.2.2.2 pgoyette struct efi_block_dev *bdev;
49 1.2.2.2 pgoyette struct efi_block_part *bpart;
50 1.2.2.2 pgoyette char pathbuf[PATH_MAX], *default_device, *ep = NULL;
51 1.2.2.2 pgoyette const char *full_path;
52 1.2.2.2 pgoyette intmax_t dev;
53 1.2.2.2 pgoyette int part;
54 1.2.2.2 pgoyette
55 1.2.2.2 pgoyette default_device = get_default_device();
56 1.2.2.2 pgoyette if (strchr(fname, ':') == NULL) {
57 1.2.2.2 pgoyette if (strlen(default_device) > 0) {
58 1.2.2.2 pgoyette snprintf(pathbuf, sizeof(pathbuf), "%s:%s", default_device, fname);
59 1.2.2.2 pgoyette full_path = pathbuf;
60 1.2.2.2 pgoyette *pfile = __UNCONST(fname);
61 1.2.2.2 pgoyette } else {
62 1.2.2.2 pgoyette return EINVAL;
63 1.2.2.2 pgoyette }
64 1.2.2.2 pgoyette } else {
65 1.2.2.2 pgoyette full_path = fname;
66 1.2.2.2 pgoyette *pfile = strchr(fname, ':') + 1;
67 1.2.2.2 pgoyette }
68 1.2.2.2 pgoyette
69 1.2.2.2 pgoyette if (strncasecmp(full_path, "hd", 2) != 0)
70 1.2.2.2 pgoyette return EINVAL;
71 1.2.2.2 pgoyette dev = strtoimax(full_path + 2, &ep, 10);
72 1.2.2.2 pgoyette if (dev < 0 || dev >= efi_nblock)
73 1.2.2.2 pgoyette return ENXIO;
74 1.2.2.2 pgoyette if (ep[0] < 'a' || ep[0] >= 'a' + MAXPARTITIONS || ep[1] != ':')
75 1.2.2.2 pgoyette return EINVAL;
76 1.2.2.2 pgoyette part = ep[0] - 'a';
77 1.2.2.2 pgoyette TAILQ_FOREACH(bdev, &efi_block_devs, entries) {
78 1.2.2.2 pgoyette if (bdev->index == dev) {
79 1.2.2.2 pgoyette TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
80 1.2.2.2 pgoyette if (bpart->index == part) {
81 1.2.2.2 pgoyette *pbpart = bpart;
82 1.2.2.2 pgoyette return 0;
83 1.2.2.2 pgoyette }
84 1.2.2.2 pgoyette }
85 1.2.2.2 pgoyette }
86 1.2.2.2 pgoyette }
87 1.2.2.2 pgoyette
88 1.2.2.2 pgoyette return ENOENT;
89 1.2.2.2 pgoyette }
90 1.2.2.2 pgoyette
91 1.2.2.2 pgoyette static void
92 1.2.2.2 pgoyette efi_block_generate_hash_mbr(struct efi_block_part *bpart, struct mbr_sector *mbr)
93 1.2.2.2 pgoyette {
94 1.2.2.2 pgoyette MD5_CTX md5ctx;
95 1.2.2.2 pgoyette
96 1.2.2.2 pgoyette MD5Init(&md5ctx);
97 1.2.2.2 pgoyette MD5Update(&md5ctx, (void *)mbr, sizeof(*mbr));
98 1.2.2.2 pgoyette MD5Final(bpart->hash, &md5ctx);
99 1.2.2.2 pgoyette }
100 1.2.2.2 pgoyette
101 1.2.2.2 pgoyette static int
102 1.2.2.2 pgoyette efi_block_find_partitions_disklabel(struct efi_block_dev *bdev, struct mbr_sector *mbr, uint32_t start, uint32_t size)
103 1.2.2.2 pgoyette {
104 1.2.2.2 pgoyette struct efi_block_part *bpart;
105 1.2.2.2 pgoyette struct disklabel d;
106 1.2.2.2 pgoyette struct partition *p;
107 1.2.2.2 pgoyette EFI_STATUS status;
108 1.2.2.2 pgoyette EFI_LBA lba;
109 1.2.2.2 pgoyette uint8_t *buf;
110 1.2.2.2 pgoyette UINT32 sz;
111 1.2.2.2 pgoyette int n;
112 1.2.2.2 pgoyette
113 1.2.2.2 pgoyette sz = __MAX(sizeof(d), bdev->bio->Media->BlockSize);
114 1.2.2.2 pgoyette sz = roundup(sz, bdev->bio->Media->BlockSize);
115 1.2.2.2 pgoyette buf = AllocatePool(sz);
116 1.2.2.2 pgoyette if (!buf)
117 1.2.2.2 pgoyette return ENOMEM;
118 1.2.2.2 pgoyette
119 1.2.2.3 pgoyette lba = (((EFI_LBA)start + LABELSECTOR) * DEV_BSIZE) / bdev->bio->Media->BlockSize;
120 1.2.2.2 pgoyette status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id, lba, sz, buf);
121 1.2.2.2 pgoyette if (EFI_ERROR(status) || getdisklabel(buf, &d) != NULL) {
122 1.2.2.2 pgoyette FreePool(buf);
123 1.2.2.2 pgoyette return EIO;
124 1.2.2.2 pgoyette }
125 1.2.2.2 pgoyette FreePool(buf);
126 1.2.2.2 pgoyette
127 1.2.2.2 pgoyette if (le32toh(d.d_magic) != DISKMAGIC || le32toh(d.d_magic2) != DISKMAGIC)
128 1.2.2.2 pgoyette return EINVAL;
129 1.2.2.2 pgoyette if (le16toh(d.d_npartitions) > MAXPARTITIONS)
130 1.2.2.2 pgoyette return EINVAL;
131 1.2.2.2 pgoyette
132 1.2.2.2 pgoyette for (n = 0; n < le16toh(d.d_npartitions); n++) {
133 1.2.2.2 pgoyette p = &d.d_partitions[n];
134 1.2.2.2 pgoyette switch (p->p_fstype) {
135 1.2.2.2 pgoyette case FS_BSDFFS:
136 1.2.2.2 pgoyette case FS_MSDOS:
137 1.2.2.2 pgoyette case FS_BSDLFS:
138 1.2.2.2 pgoyette break;
139 1.2.2.2 pgoyette default:
140 1.2.2.2 pgoyette continue;
141 1.2.2.2 pgoyette }
142 1.2.2.2 pgoyette
143 1.2.2.2 pgoyette bpart = alloc(sizeof(*bpart));
144 1.2.2.2 pgoyette bpart->index = n;
145 1.2.2.2 pgoyette bpart->bdev = bdev;
146 1.2.2.2 pgoyette bpart->type = EFI_BLOCK_PART_DISKLABEL;
147 1.2.2.2 pgoyette bpart->disklabel.secsize = le32toh(d.d_secsize);
148 1.2.2.2 pgoyette bpart->disklabel.part = *p;
149 1.2.2.2 pgoyette efi_block_generate_hash_mbr(bpart, mbr);
150 1.2.2.2 pgoyette TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
151 1.2.2.2 pgoyette }
152 1.2.2.2 pgoyette
153 1.2.2.2 pgoyette return 0;
154 1.2.2.2 pgoyette }
155 1.2.2.2 pgoyette
156 1.2.2.2 pgoyette static int
157 1.2.2.2 pgoyette efi_block_find_partitions_mbr(struct efi_block_dev *bdev)
158 1.2.2.2 pgoyette {
159 1.2.2.2 pgoyette struct mbr_sector mbr;
160 1.2.2.2 pgoyette struct mbr_partition *mbr_part;
161 1.2.2.2 pgoyette EFI_STATUS status;
162 1.2.2.2 pgoyette uint8_t *buf;
163 1.2.2.2 pgoyette UINT32 sz;
164 1.2.2.2 pgoyette int n;
165 1.2.2.2 pgoyette
166 1.2.2.2 pgoyette sz = __MAX(sizeof(mbr), bdev->bio->Media->BlockSize);
167 1.2.2.2 pgoyette sz = roundup(sz, bdev->bio->Media->BlockSize);
168 1.2.2.2 pgoyette buf = AllocatePool(sz);
169 1.2.2.2 pgoyette if (!buf)
170 1.2.2.2 pgoyette return ENOMEM;
171 1.2.2.2 pgoyette
172 1.2.2.2 pgoyette status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id, 0, sz, buf);
173 1.2.2.2 pgoyette if (EFI_ERROR(status)) {
174 1.2.2.2 pgoyette FreePool(buf);
175 1.2.2.2 pgoyette return EIO;
176 1.2.2.2 pgoyette }
177 1.2.2.2 pgoyette memcpy(&mbr, buf, sizeof(mbr));
178 1.2.2.2 pgoyette FreePool(buf);
179 1.2.2.2 pgoyette
180 1.2.2.2 pgoyette if (le32toh(mbr.mbr_magic) != MBR_MAGIC)
181 1.2.2.2 pgoyette return ENOENT;
182 1.2.2.2 pgoyette
183 1.2.2.2 pgoyette for (n = 0; n < MBR_PART_COUNT; n++) {
184 1.2.2.2 pgoyette mbr_part = &mbr.mbr_parts[n];
185 1.2.2.2 pgoyette if (le32toh(mbr_part->mbrp_size) == 0)
186 1.2.2.2 pgoyette continue;
187 1.2.2.2 pgoyette if (mbr_part->mbrp_type == MBR_PTYPE_NETBSD) {
188 1.2.2.2 pgoyette efi_block_find_partitions_disklabel(bdev, &mbr, le32toh(mbr_part->mbrp_start), le32toh(mbr_part->mbrp_size));
189 1.2.2.2 pgoyette break;
190 1.2.2.2 pgoyette }
191 1.2.2.2 pgoyette }
192 1.2.2.2 pgoyette
193 1.2.2.2 pgoyette return 0;
194 1.2.2.2 pgoyette }
195 1.2.2.2 pgoyette
196 1.2.2.4 pgoyette static const struct {
197 1.2.2.4 pgoyette struct uuid guid;
198 1.2.2.4 pgoyette uint8_t fstype;
199 1.2.2.4 pgoyette } gpt_guid_to_str[] = {
200 1.2.2.4 pgoyette { GPT_ENT_TYPE_NETBSD_FFS, FS_BSDFFS },
201 1.2.2.4 pgoyette { GPT_ENT_TYPE_NETBSD_LFS, FS_BSDLFS },
202 1.2.2.4 pgoyette { GPT_ENT_TYPE_NETBSD_RAIDFRAME, FS_RAID },
203 1.2.2.4 pgoyette { GPT_ENT_TYPE_NETBSD_CCD, FS_CCD },
204 1.2.2.4 pgoyette { GPT_ENT_TYPE_NETBSD_CGD, FS_CGD },
205 1.2.2.4 pgoyette { GPT_ENT_TYPE_MS_BASIC_DATA, FS_MSDOS }, /* or NTFS? ambiguous */
206 1.2.2.4 pgoyette };
207 1.2.2.4 pgoyette
208 1.2.2.4 pgoyette static int
209 1.2.2.4 pgoyette efi_block_find_partitions_gpt_entry(struct efi_block_dev *bdev, struct gpt_hdr *hdr, struct gpt_ent *ent, UINT32 index)
210 1.2.2.4 pgoyette {
211 1.2.2.4 pgoyette struct efi_block_part *bpart;
212 1.2.2.4 pgoyette uint8_t fstype = FS_UNUSED;
213 1.2.2.4 pgoyette struct uuid uuid;
214 1.2.2.4 pgoyette int n;
215 1.2.2.4 pgoyette
216 1.2.2.4 pgoyette memcpy(&uuid, ent->ent_type, sizeof(uuid));
217 1.2.2.4 pgoyette for (n = 0; n < __arraycount(gpt_guid_to_str); n++)
218 1.2.2.4 pgoyette if (memcmp(ent->ent_type, &gpt_guid_to_str[n].guid, sizeof(ent->ent_type)) == 0) {
219 1.2.2.4 pgoyette fstype = gpt_guid_to_str[n].fstype;
220 1.2.2.4 pgoyette break;
221 1.2.2.4 pgoyette }
222 1.2.2.4 pgoyette if (fstype == FS_UNUSED)
223 1.2.2.4 pgoyette return 0;
224 1.2.2.4 pgoyette
225 1.2.2.4 pgoyette bpart = alloc(sizeof(*bpart));
226 1.2.2.4 pgoyette bpart->index = index;
227 1.2.2.4 pgoyette bpart->bdev = bdev;
228 1.2.2.4 pgoyette bpart->type = EFI_BLOCK_PART_GPT;
229 1.2.2.4 pgoyette bpart->gpt.fstype = fstype;
230 1.2.2.4 pgoyette bpart->gpt.ent = *ent;
231 1.2.2.4 pgoyette memcpy(bpart->hash, ent->ent_guid, sizeof(bpart->hash));
232 1.2.2.4 pgoyette TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
233 1.2.2.4 pgoyette
234 1.2.2.4 pgoyette return 0;
235 1.2.2.4 pgoyette }
236 1.2.2.4 pgoyette
237 1.2.2.4 pgoyette static int
238 1.2.2.4 pgoyette efi_block_find_partitions_gpt(struct efi_block_dev *bdev)
239 1.2.2.4 pgoyette {
240 1.2.2.4 pgoyette struct gpt_hdr hdr;
241 1.2.2.4 pgoyette struct gpt_ent ent;
242 1.2.2.4 pgoyette EFI_STATUS status;
243 1.2.2.4 pgoyette UINT32 sz, entry;
244 1.2.2.4 pgoyette uint8_t *buf;
245 1.2.2.4 pgoyette
246 1.2.2.4 pgoyette sz = __MAX(sizeof(hdr), bdev->bio->Media->BlockSize);
247 1.2.2.4 pgoyette sz = roundup(sz, bdev->bio->Media->BlockSize);
248 1.2.2.4 pgoyette buf = AllocatePool(sz);
249 1.2.2.4 pgoyette if (!buf)
250 1.2.2.4 pgoyette return ENOMEM;
251 1.2.2.4 pgoyette
252 1.2.2.4 pgoyette status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id, GPT_HDR_BLKNO, sz, buf);
253 1.2.2.4 pgoyette if (EFI_ERROR(status)) {
254 1.2.2.4 pgoyette FreePool(buf);
255 1.2.2.4 pgoyette return EIO;
256 1.2.2.4 pgoyette }
257 1.2.2.4 pgoyette memcpy(&hdr, buf, sizeof(hdr));
258 1.2.2.4 pgoyette FreePool(buf);
259 1.2.2.4 pgoyette
260 1.2.2.4 pgoyette if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0)
261 1.2.2.4 pgoyette return ENOENT;
262 1.2.2.4 pgoyette if (le32toh(hdr.hdr_entsz) < sizeof(ent))
263 1.2.2.4 pgoyette return EINVAL;
264 1.2.2.4 pgoyette
265 1.2.2.4 pgoyette sz = __MAX(le32toh(hdr.hdr_entsz) * le32toh(hdr.hdr_entries), bdev->bio->Media->BlockSize);
266 1.2.2.4 pgoyette sz = roundup(sz, bdev->bio->Media->BlockSize);
267 1.2.2.4 pgoyette buf = AllocatePool(sz);
268 1.2.2.4 pgoyette if (!buf)
269 1.2.2.4 pgoyette return ENOMEM;
270 1.2.2.4 pgoyette
271 1.2.2.4 pgoyette status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id, le64toh(hdr.hdr_lba_table), sz, buf);
272 1.2.2.4 pgoyette if (EFI_ERROR(status)) {
273 1.2.2.4 pgoyette FreePool(buf);
274 1.2.2.4 pgoyette return EIO;
275 1.2.2.4 pgoyette }
276 1.2.2.4 pgoyette
277 1.2.2.4 pgoyette for (entry = 0; entry < le32toh(hdr.hdr_entries); entry++) {
278 1.2.2.4 pgoyette memcpy(&ent, buf + (entry * le32toh(hdr.hdr_entsz)), sizeof(ent));
279 1.2.2.4 pgoyette efi_block_find_partitions_gpt_entry(bdev, &hdr, &ent, entry);
280 1.2.2.4 pgoyette }
281 1.2.2.4 pgoyette
282 1.2.2.4 pgoyette FreePool(buf);
283 1.2.2.4 pgoyette
284 1.2.2.4 pgoyette return 0;
285 1.2.2.4 pgoyette }
286 1.2.2.4 pgoyette
287 1.2.2.2 pgoyette static int
288 1.2.2.2 pgoyette efi_block_find_partitions(struct efi_block_dev *bdev)
289 1.2.2.2 pgoyette {
290 1.2.2.4 pgoyette int error;
291 1.2.2.4 pgoyette
292 1.2.2.4 pgoyette error = efi_block_find_partitions_gpt(bdev);
293 1.2.2.4 pgoyette if (error)
294 1.2.2.4 pgoyette error = efi_block_find_partitions_mbr(bdev);
295 1.2.2.4 pgoyette
296 1.2.2.4 pgoyette return error;
297 1.2.2.2 pgoyette }
298 1.2.2.2 pgoyette
299 1.2.2.2 pgoyette void
300 1.2.2.2 pgoyette efi_block_probe(void)
301 1.2.2.2 pgoyette {
302 1.2.2.2 pgoyette struct efi_block_dev *bdev;
303 1.2.2.4 pgoyette struct efi_block_part *bpart;
304 1.2.2.2 pgoyette EFI_BLOCK_IO *bio;
305 1.2.2.2 pgoyette EFI_STATUS status;
306 1.2.2.2 pgoyette uint16_t devindex = 0;
307 1.2.2.2 pgoyette int depth = -1;
308 1.2.2.2 pgoyette int n;
309 1.2.2.2 pgoyette
310 1.2.2.2 pgoyette status = LibLocateHandle(ByProtocol, &BlockIoProtocol, NULL, &efi_nblock, &efi_block);
311 1.2.2.2 pgoyette if (EFI_ERROR(status))
312 1.2.2.2 pgoyette return;
313 1.2.2.2 pgoyette
314 1.2.2.2 pgoyette if (efi_bootdp) {
315 1.2.2.2 pgoyette depth = efi_device_path_depth(efi_bootdp, MEDIA_DEVICE_PATH);
316 1.2.2.2 pgoyette if (depth == 0)
317 1.2.2.2 pgoyette depth = 1;
318 1.2.2.2 pgoyette }
319 1.2.2.2 pgoyette
320 1.2.2.2 pgoyette for (n = 0; n < efi_nblock; n++) {
321 1.2.2.2 pgoyette status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_block[n], &BlockIoProtocol, (void **)&bio);
322 1.2.2.2 pgoyette if (EFI_ERROR(status) || !bio->Media->MediaPresent)
323 1.2.2.2 pgoyette continue;
324 1.2.2.2 pgoyette
325 1.2.2.2 pgoyette if (bio->Media->LogicalPartition)
326 1.2.2.2 pgoyette continue;
327 1.2.2.2 pgoyette
328 1.2.2.2 pgoyette bdev = alloc(sizeof(*bdev));
329 1.2.2.2 pgoyette bdev->index = devindex++;
330 1.2.2.2 pgoyette bdev->bio = bio;
331 1.2.2.2 pgoyette bdev->media_id = bio->Media->MediaId;
332 1.2.2.2 pgoyette bdev->path = DevicePathFromHandle(efi_block[n]);
333 1.2.2.2 pgoyette TAILQ_INIT(&bdev->partitions);
334 1.2.2.2 pgoyette TAILQ_INSERT_TAIL(&efi_block_devs, bdev, entries);
335 1.2.2.2 pgoyette
336 1.2.2.4 pgoyette efi_block_find_partitions(bdev);
337 1.2.2.4 pgoyette
338 1.2.2.2 pgoyette if (depth > 0 && efi_device_path_ncmp(efi_bootdp, DevicePathFromHandle(efi_block[n]), depth) == 0) {
339 1.2.2.4 pgoyette TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
340 1.2.2.4 pgoyette uint8_t fstype = FS_UNUSED;
341 1.2.2.4 pgoyette switch (bpart->type) {
342 1.2.2.4 pgoyette case EFI_BLOCK_PART_DISKLABEL:
343 1.2.2.4 pgoyette fstype = bpart->disklabel.part.p_fstype;
344 1.2.2.4 pgoyette break;
345 1.2.2.4 pgoyette case EFI_BLOCK_PART_GPT:
346 1.2.2.4 pgoyette fstype = bpart->gpt.fstype;
347 1.2.2.4 pgoyette break;
348 1.2.2.4 pgoyette }
349 1.2.2.4 pgoyette if (fstype == FS_BSDFFS) {
350 1.2.2.4 pgoyette char devname[9];
351 1.2.2.4 pgoyette snprintf(devname, sizeof(devname), "hd%u%c", bdev->index, bpart->index + 'a');
352 1.2.2.4 pgoyette set_default_device(devname);
353 1.2.2.4 pgoyette break;
354 1.2.2.4 pgoyette }
355 1.2.2.4 pgoyette }
356 1.2.2.2 pgoyette }
357 1.2.2.4 pgoyette }
358 1.2.2.4 pgoyette }
359 1.2.2.2 pgoyette
360 1.2.2.4 pgoyette static void
361 1.2.2.4 pgoyette print_guid(const uint8_t *guid)
362 1.2.2.4 pgoyette {
363 1.2.2.4 pgoyette const int index[] = { 3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15 };
364 1.2.2.4 pgoyette int i;
365 1.2.2.4 pgoyette
366 1.2.2.4 pgoyette for (i = 0; i < 16; i++) {
367 1.2.2.4 pgoyette printf("%02x", guid[index[i]]);
368 1.2.2.4 pgoyette if (i == 3 || i == 5 || i == 7 || i == 9)
369 1.2.2.4 pgoyette printf("-");
370 1.2.2.2 pgoyette }
371 1.2.2.2 pgoyette }
372 1.2.2.2 pgoyette
373 1.2.2.2 pgoyette void
374 1.2.2.2 pgoyette efi_block_show(void)
375 1.2.2.2 pgoyette {
376 1.2.2.2 pgoyette struct efi_block_dev *bdev;
377 1.2.2.2 pgoyette struct efi_block_part *bpart;
378 1.2.2.2 pgoyette uint64_t size;
379 1.2.2.2 pgoyette CHAR16 *path;
380 1.2.2.2 pgoyette
381 1.2.2.2 pgoyette TAILQ_FOREACH(bdev, &efi_block_devs, entries) {
382 1.2.2.2 pgoyette printf("hd%u (", bdev->index);
383 1.2.2.2 pgoyette
384 1.2.2.2 pgoyette /* Size in MB */
385 1.2.2.2 pgoyette size = ((bdev->bio->Media->LastBlock + 1) * bdev->bio->Media->BlockSize) / (1024 * 1024);
386 1.2.2.2 pgoyette if (size >= 10000)
387 1.2.2.2 pgoyette printf("%"PRIu64" GB", size / 1024);
388 1.2.2.2 pgoyette else
389 1.2.2.2 pgoyette printf("%"PRIu64" MB", size);
390 1.2.2.2 pgoyette printf("): ");
391 1.2.2.2 pgoyette
392 1.2.2.2 pgoyette path = DevicePathToStr(bdev->path);
393 1.2.2.2 pgoyette Print(L"%s", path);
394 1.2.2.2 pgoyette FreePool(path);
395 1.2.2.2 pgoyette
396 1.2.2.2 pgoyette printf("\n");
397 1.2.2.2 pgoyette
398 1.2.2.2 pgoyette TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
399 1.2.2.2 pgoyette switch (bpart->type) {
400 1.2.2.2 pgoyette case EFI_BLOCK_PART_DISKLABEL:
401 1.2.2.2 pgoyette printf(" hd%u%c (", bdev->index, bpart->index + 'a');
402 1.2.2.2 pgoyette
403 1.2.2.2 pgoyette /* Size in MB */
404 1.2.2.2 pgoyette size = ((uint64_t)bpart->disklabel.secsize * bpart->disklabel.part.p_size) / (1024 * 1024);
405 1.2.2.2 pgoyette if (size >= 10000)
406 1.2.2.2 pgoyette printf("%"PRIu64" GB", size / 1024);
407 1.2.2.2 pgoyette else
408 1.2.2.2 pgoyette printf("%"PRIu64" MB", size);
409 1.2.2.2 pgoyette printf("): ");
410 1.2.2.2 pgoyette
411 1.2.2.2 pgoyette printf("%s\n", fstypenames[bpart->disklabel.part.p_fstype]);
412 1.2.2.2 pgoyette break;
413 1.2.2.4 pgoyette case EFI_BLOCK_PART_GPT:
414 1.2.2.4 pgoyette printf(" hd%u%c ", bdev->index, bpart->index + 'a');
415 1.2.2.4 pgoyette
416 1.2.2.4 pgoyette if (bpart->gpt.ent.ent_name[0] == 0x0000) {
417 1.2.2.4 pgoyette printf("\"");
418 1.2.2.4 pgoyette print_guid(bpart->gpt.ent.ent_guid);
419 1.2.2.4 pgoyette printf("\"");
420 1.2.2.4 pgoyette } else {
421 1.2.2.4 pgoyette Print(L"\"%s\"", bpart->gpt.ent.ent_name);
422 1.2.2.4 pgoyette }
423 1.2.2.4 pgoyette
424 1.2.2.4 pgoyette /* Size in MB */
425 1.2.2.4 pgoyette size = (le64toh(bpart->gpt.ent.ent_lba_end) - le64toh(bpart->gpt.ent.ent_lba_start)) * bdev->bio->Media->BlockSize;
426 1.2.2.4 pgoyette size /= (1024 * 1024);
427 1.2.2.4 pgoyette if (size >= 10000)
428 1.2.2.4 pgoyette printf(" (%"PRIu64" GB): ", size / 1024);
429 1.2.2.4 pgoyette else
430 1.2.2.4 pgoyette printf(" (%"PRIu64" MB): ", size);
431 1.2.2.4 pgoyette
432 1.2.2.4 pgoyette printf("%s\n", fstypenames[bpart->gpt.fstype]);
433 1.2.2.4 pgoyette break;
434 1.2.2.2 pgoyette default:
435 1.2.2.2 pgoyette break;
436 1.2.2.2 pgoyette }
437 1.2.2.2 pgoyette }
438 1.2.2.2 pgoyette }
439 1.2.2.2 pgoyette }
440 1.2.2.2 pgoyette
441 1.2.2.2 pgoyette struct efi_block_part *
442 1.2.2.2 pgoyette efi_block_boot_part(void)
443 1.2.2.2 pgoyette {
444 1.2.2.2 pgoyette return efi_block_booted;
445 1.2.2.2 pgoyette }
446 1.2.2.2 pgoyette
447 1.2.2.2 pgoyette int
448 1.2.2.2 pgoyette efi_block_open(struct open_file *f, ...)
449 1.2.2.2 pgoyette {
450 1.2.2.2 pgoyette struct efi_block_part *bpart;
451 1.2.2.2 pgoyette const char *fname;
452 1.2.2.2 pgoyette char **file;
453 1.2.2.2 pgoyette char *path;
454 1.2.2.2 pgoyette va_list ap;
455 1.2.2.2 pgoyette int rv, n;
456 1.2.2.2 pgoyette
457 1.2.2.2 pgoyette va_start(ap, f);
458 1.2.2.2 pgoyette fname = va_arg(ap, const char *);
459 1.2.2.2 pgoyette file = va_arg(ap, char **);
460 1.2.2.2 pgoyette va_end(ap);
461 1.2.2.2 pgoyette
462 1.2.2.2 pgoyette rv = efi_block_parse(fname, &bpart, &path);
463 1.2.2.2 pgoyette if (rv != 0)
464 1.2.2.2 pgoyette return rv;
465 1.2.2.2 pgoyette
466 1.2.2.2 pgoyette for (n = 0; n < ndevs; n++)
467 1.2.2.2 pgoyette if (strcmp(DEV_NAME(&devsw[n]), "efiblock") == 0) {
468 1.2.2.2 pgoyette f->f_dev = &devsw[n];
469 1.2.2.2 pgoyette break;
470 1.2.2.2 pgoyette }
471 1.2.2.2 pgoyette if (n == ndevs)
472 1.2.2.2 pgoyette return ENXIO;
473 1.2.2.2 pgoyette
474 1.2.2.2 pgoyette f->f_devdata = bpart;
475 1.2.2.2 pgoyette
476 1.2.2.2 pgoyette *file = path;
477 1.2.2.2 pgoyette
478 1.2.2.2 pgoyette efi_block_booted = bpart;
479 1.2.2.2 pgoyette
480 1.2.2.2 pgoyette return 0;
481 1.2.2.2 pgoyette }
482 1.2.2.2 pgoyette
483 1.2.2.2 pgoyette int
484 1.2.2.2 pgoyette efi_block_close(struct open_file *f)
485 1.2.2.2 pgoyette {
486 1.2.2.2 pgoyette return 0;
487 1.2.2.2 pgoyette }
488 1.2.2.2 pgoyette
489 1.2.2.2 pgoyette int
490 1.2.2.2 pgoyette efi_block_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize)
491 1.2.2.2 pgoyette {
492 1.2.2.2 pgoyette struct efi_block_part *bpart = devdata;
493 1.2.2.2 pgoyette EFI_STATUS status;
494 1.2.2.2 pgoyette
495 1.2.2.2 pgoyette if (rw != F_READ)
496 1.2.2.2 pgoyette return EROFS;
497 1.2.2.2 pgoyette
498 1.2.2.2 pgoyette switch (bpart->type) {
499 1.2.2.2 pgoyette case EFI_BLOCK_PART_DISKLABEL:
500 1.2.2.2 pgoyette if (bpart->bdev->bio->Media->BlockSize != bpart->disklabel.secsize) {
501 1.2.2.2 pgoyette printf("%s: unsupported block size %d (expected %d)\n", __func__,
502 1.2.2.2 pgoyette bpart->bdev->bio->Media->BlockSize, bpart->disklabel.secsize);
503 1.2.2.2 pgoyette return EIO;
504 1.2.2.2 pgoyette }
505 1.2.2.2 pgoyette dblk += bpart->disklabel.part.p_offset;
506 1.2.2.2 pgoyette break;
507 1.2.2.4 pgoyette case EFI_BLOCK_PART_GPT:
508 1.2.2.4 pgoyette if (bpart->bdev->bio->Media->BlockSize != DEV_BSIZE) {
509 1.2.2.4 pgoyette printf("%s: unsupported block size %d (expected %d)\n", __func__,
510 1.2.2.4 pgoyette bpart->bdev->bio->Media->BlockSize, DEV_BSIZE);
511 1.2.2.4 pgoyette return EIO;
512 1.2.2.4 pgoyette }
513 1.2.2.4 pgoyette dblk += le64toh(bpart->gpt.ent.ent_lba_start);
514 1.2.2.4 pgoyette break;
515 1.2.2.2 pgoyette default:
516 1.2.2.2 pgoyette return EINVAL;
517 1.2.2.2 pgoyette }
518 1.2.2.2 pgoyette
519 1.2.2.2 pgoyette status = uefi_call_wrapper(bpart->bdev->bio->ReadBlocks, 5, bpart->bdev->bio, bpart->bdev->media_id, dblk, size, buf);
520 1.2.2.2 pgoyette if (EFI_ERROR(status))
521 1.2.2.2 pgoyette return EIO;
522 1.2.2.2 pgoyette
523 1.2.2.2 pgoyette *rsize = size;
524 1.2.2.2 pgoyette
525 1.2.2.2 pgoyette return 0;
526 1.2.2.2 pgoyette }
527