xref: /netbsd-src/sys/stand/efiboot/efiblock.c (revision fc7406911ad6c5cbc676719977365ffcdaf529a2)
1 /* $NetBSD: efiblock.c,v 1.20 2023/06/14 00:52:25 rin Exp $ */
2 
3 /*-
4  * Copyright (c) 2016 Kimihiro Nonaka <nonaka@netbsd.org>
5  * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #define FSTYPENAMES
31 
32 #include <sys/param.h>
33 #include <sys/md5.h>
34 #include <sys/uuid.h>
35 
36 #include <fs/cd9660/iso.h>
37 
38 #include "efiboot.h"
39 #include "efiblock.h"
40 
41 #define	EFI_BLOCK_READAHEAD	(64 * 1024)
42 #define	EFI_BLOCK_TIMEOUT	120
43 #define	EFI_BLOCK_TIMEOUT_CODE	0x810c0000
44 
45 /*
46  * The raidframe support is basic.  Ideally, it should be expanded to
47  * consider raid volumes a first-class citizen like the x86 efiboot does,
48  * but for now, we simply assume each RAID is potentially bootable.
49  */
50 #define	RF_PROTECTED_SECTORS	64	/* XXX refer to <.../rf_optnames.h> */
51 
52 static EFI_HANDLE *efi_block;
53 static UINTN efi_nblock;
54 static struct efi_block_part *efi_block_booted = NULL;
55 
56 static bool efi_ra_enable = false;
57 static UINT8 *efi_ra_buffer = NULL;
58 static UINT32 efi_ra_media_id;
59 static UINT64 efi_ra_start = 0;
60 static UINT64 efi_ra_length = 0;
61 
62 static TAILQ_HEAD(, efi_block_dev) efi_block_devs = TAILQ_HEAD_INITIALIZER(efi_block_devs);
63 
64 static int
efi_block_parse(const char * fname,struct efi_block_part ** pbpart,char ** pfile)65 efi_block_parse(const char *fname, struct efi_block_part **pbpart, char **pfile)
66 {
67 	struct efi_block_dev *bdev;
68 	struct efi_block_part *bpart;
69 	char pathbuf[PATH_MAX], *default_device, *ep = NULL;
70 	const char *full_path;
71 	intmax_t dev;
72 	int part;
73 
74 	default_device = get_default_device();
75 	if (strchr(fname, ':') == NULL) {
76 		if (strlen(default_device) > 0) {
77 			snprintf(pathbuf, sizeof(pathbuf), "%s:%s", default_device, fname);
78 			full_path = pathbuf;
79 			*pfile = __UNCONST(fname);
80 		} else {
81 			return EINVAL;
82 		}
83 	} else {
84 		full_path = fname;
85 		*pfile = strchr(fname, ':') + 1;
86 	}
87 
88 	if (*pfile[0] == '\0') {
89 		*pfile = __UNCONST("/");
90 	}
91 
92 	if (strncasecmp(full_path, "hd", 2) != 0)
93 		return EINVAL;
94 	dev = strtoimax(full_path + 2, &ep, 10);
95 	if (dev < 0 || dev >= efi_nblock)
96 		return ENXIO;
97 	if (ep[0] < 'a' || ep[0] >= 'a' + MAXPARTITIONS || ep[1] != ':')
98 		return EINVAL;
99 	part = ep[0] - 'a';
100 	TAILQ_FOREACH(bdev, &efi_block_devs, entries) {
101 		if (bdev->index == dev) {
102 			TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
103 				if (bpart->index == part) {
104 					*pbpart = bpart;
105 					return 0;
106 				}
107 			}
108 		}
109 	}
110 
111 	return ENOENT;
112 }
113 
114 static void
efi_block_generate_hash_mbr(struct efi_block_part * bpart,struct mbr_sector * mbr)115 efi_block_generate_hash_mbr(struct efi_block_part *bpart, struct mbr_sector *mbr)
116 {
117 	MD5_CTX md5ctx;
118 
119 	MD5Init(&md5ctx);
120 	MD5Update(&md5ctx, (void *)mbr, sizeof(*mbr));
121 	MD5Final(bpart->hash, &md5ctx);
122 }
123 
124 static EFI_STATUS
efi_block_do_read_blockio(struct efi_block_dev * bdev,UINT64 off,void * buf,UINTN bufsize)125 efi_block_do_read_blockio(struct efi_block_dev *bdev, UINT64 off, void *buf,
126     UINTN bufsize)
127 {
128 	UINT8 *blkbuf, *blkbuf_start;
129 	EFI_STATUS status;
130 	EFI_LBA lba_start, lba_end;
131 	UINT64 blkbuf_offset;
132 	UINT64 blkbuf_size, alloc_size;
133 
134 	lba_start = off / bdev->bio->Media->BlockSize;
135 	lba_end = (off + bufsize - 1) / bdev->bio->Media->BlockSize;
136 	blkbuf_offset = off % bdev->bio->Media->BlockSize;
137 	blkbuf_size = (lba_end - lba_start + 1) * bdev->bio->Media->BlockSize;
138 
139 	alloc_size = blkbuf_size;
140 	if (bdev->bio->Media->IoAlign > 1) {
141 		alloc_size = (blkbuf_size + bdev->bio->Media->IoAlign - 1) /
142 		    bdev->bio->Media->IoAlign *
143 		    bdev->bio->Media->IoAlign;
144 	}
145 
146 	blkbuf = AllocatePool(alloc_size);
147 	if (blkbuf == NULL) {
148 		return EFI_OUT_OF_RESOURCES;
149 	}
150 
151 	if (bdev->bio->Media->IoAlign > 1) {
152 		blkbuf_start = (void *)roundup2((intptr_t)blkbuf,
153 		    bdev->bio->Media->IoAlign);
154 	} else {
155 		blkbuf_start = blkbuf;
156 	}
157 
158 	status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio,
159 	    bdev->media_id, lba_start, blkbuf_size, blkbuf_start);
160 	if (EFI_ERROR(status)) {
161 		goto done;
162 	}
163 
164 	memcpy(buf, blkbuf_start + blkbuf_offset, bufsize);
165 
166 done:
167 	FreePool(blkbuf);
168 	return status;
169 }
170 
171 static EFI_STATUS
efi_block_do_read_diskio(struct efi_block_dev * bdev,UINT64 off,void * buf,UINTN bufsize)172 efi_block_do_read_diskio(struct efi_block_dev *bdev, UINT64 off, void *buf,
173     UINTN bufsize)
174 {
175 	return uefi_call_wrapper(bdev->dio->ReadDisk, 5, bdev->dio,
176 	    bdev->media_id, off, bufsize, buf);
177 }
178 
179 static EFI_STATUS
efi_block_do_read(struct efi_block_dev * bdev,UINT64 off,void * buf,UINTN bufsize)180 efi_block_do_read(struct efi_block_dev *bdev, UINT64 off, void *buf,
181     UINTN bufsize)
182 {
183 	/*
184 	 * Perform read access using EFI_DISK_IO_PROTOCOL if available,
185 	 * otherwise use EFI_BLOCK_IO_PROTOCOL.
186 	 */
187 	if (bdev->dio != NULL) {
188 		return efi_block_do_read_diskio(bdev, off, buf, bufsize);
189 	} else {
190 		return efi_block_do_read_blockio(bdev, off, buf, bufsize);
191 	}
192 }
193 
194 static EFI_STATUS
efi_block_readahead(struct efi_block_dev * bdev,UINT64 off,void * buf,UINTN bufsize)195 efi_block_readahead(struct efi_block_dev *bdev, UINT64 off, void *buf,
196     UINTN bufsize)
197 {
198 	EFI_STATUS status;
199 	UINT64 mediasize, len;
200 
201 	if (efi_ra_buffer == NULL) {
202 		efi_ra_buffer = AllocatePool(EFI_BLOCK_READAHEAD);
203 		if (efi_ra_buffer == NULL) {
204 			return EFI_OUT_OF_RESOURCES;
205 		}
206 	}
207 
208 	if (bdev->media_id != efi_ra_media_id ||
209 	    off < efi_ra_start ||
210 	    off + bufsize > efi_ra_start + efi_ra_length) {
211 		mediasize = bdev->bio->Media->BlockSize *
212 		    (bdev->bio->Media->LastBlock + 1);
213 		len = EFI_BLOCK_READAHEAD;
214 		if (len > mediasize - off) {
215 			len = mediasize - off;
216 		}
217 		status = efi_block_do_read(bdev, off, efi_ra_buffer, len);
218 		if (EFI_ERROR(status)) {
219 			efi_ra_start = efi_ra_length = 0;
220 			return status;
221 		}
222 		efi_ra_start = off;
223 		efi_ra_length = len;
224 		efi_ra_media_id = bdev->media_id;
225 	}
226 
227 	memcpy(buf, &efi_ra_buffer[off - efi_ra_start], bufsize);
228 	return EFI_SUCCESS;
229 }
230 
231 static EFI_STATUS
efi_block_read(struct efi_block_dev * bdev,UINT64 off,void * buf,UINTN bufsize)232 efi_block_read(struct efi_block_dev *bdev, UINT64 off, void *buf,
233     UINTN bufsize)
234 {
235 	if (efi_ra_enable) {
236 		return efi_block_readahead(bdev, off, buf, bufsize);
237 	}
238 
239 	return efi_block_do_read(bdev, off, buf, bufsize);
240 }
241 
242 static int
efi_block_find_partitions_cd9660(struct efi_block_dev * bdev)243 efi_block_find_partitions_cd9660(struct efi_block_dev *bdev)
244 {
245 	struct efi_block_part *bpart;
246 	struct iso_primary_descriptor vd;
247 	EFI_STATUS status;
248 	EFI_LBA lba;
249 
250 	for (lba = 16;; lba++) {
251 		status = efi_block_read(bdev,
252 		    lba * ISO_DEFAULT_BLOCK_SIZE, &vd, sizeof(vd));
253 		if (EFI_ERROR(status)) {
254 			goto io_error;
255 		}
256 
257 		if (memcmp(vd.id, ISO_STANDARD_ID, sizeof vd.id) != 0) {
258 			goto io_error;
259 		}
260 		if (isonum_711(vd.type) == ISO_VD_END) {
261 			goto io_error;
262 		}
263 		if (isonum_711(vd.type) == ISO_VD_PRIMARY) {
264 			break;
265 		}
266 	}
267 
268 	if (isonum_723(vd.logical_block_size) != ISO_DEFAULT_BLOCK_SIZE) {
269 		goto io_error;
270 	}
271 
272 	bpart = alloc(sizeof(*bpart));
273 	bpart->index = 0;
274 	bpart->bdev = bdev;
275 	bpart->type = EFI_BLOCK_PART_CD9660;
276 	TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
277 
278 	return 0;
279 
280 io_error:
281 	return EIO;
282 }
283 
284 static int
efi_block_find_partitions_disklabel(struct efi_block_dev * bdev,struct mbr_sector * mbr,uint32_t start,uint32_t size)285 efi_block_find_partitions_disklabel(struct efi_block_dev *bdev,
286     struct mbr_sector *mbr, uint32_t start, uint32_t size)
287 {
288 	struct efi_block_part *bpart;
289 	char buf[DEV_BSIZE]; /* XXX, arbitrary size >= struct disklabel */
290 	struct disklabel d;
291 	struct partition *p;
292 	EFI_STATUS status;
293 	int n;
294 
295 	status = efi_block_read(bdev,
296 	    ((EFI_LBA)start + LABELSECTOR) * bdev->bio->Media->BlockSize, buf, sizeof(buf));
297 	if (EFI_ERROR(status) || getdisklabel(buf, &d) != NULL)
298 		return EIO;
299 
300 	if (le32toh(d.d_magic) != DISKMAGIC || le32toh(d.d_magic2) != DISKMAGIC)
301 		return EINVAL;
302 	if (le16toh(d.d_npartitions) > MAXPARTITIONS)
303 		return EINVAL;
304 
305 	for (n = 0; n < le16toh(d.d_npartitions); n++) {
306 		p = &d.d_partitions[n];
307 		switch (p->p_fstype) {
308 		case FS_BSDFFS:
309 		case FS_MSDOS:
310 		case FS_BSDLFS:
311 			break;
312 		case FS_RAID:
313 			p->p_size -= RF_PROTECTED_SECTORS;
314 			p->p_offset += RF_PROTECTED_SECTORS;
315 			break;
316 		default:
317 			continue;
318 		}
319 
320 		bpart = alloc(sizeof(*bpart));
321 		bpart->index = n;
322 		bpart->bdev = bdev;
323 		bpart->type = EFI_BLOCK_PART_DISKLABEL;
324 		bpart->disklabel.secsize = d.d_secsize;
325 		bpart->disklabel.part = *p;
326 		efi_block_generate_hash_mbr(bpart, mbr);
327 		TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
328 	}
329 
330 	return 0;
331 }
332 
333 static int
efi_block_find_partitions_mbr(struct efi_block_dev * bdev)334 efi_block_find_partitions_mbr(struct efi_block_dev *bdev)
335 {
336 	struct mbr_sector mbr;
337 	struct mbr_partition *mbr_part;
338 	EFI_STATUS status;
339 	int n;
340 
341 	status = efi_block_read(bdev, 0, &mbr, sizeof(mbr));
342 	if (EFI_ERROR(status))
343 		return EIO;
344 
345 	if (le32toh(mbr.mbr_magic) != MBR_MAGIC)
346 		return ENOENT;
347 
348 	for (n = 0; n < MBR_PART_COUNT; n++) {
349 		mbr_part = &mbr.mbr_parts[n];
350 		if (le32toh(mbr_part->mbrp_size) == 0)
351 			continue;
352 		if (mbr_part->mbrp_type == MBR_PTYPE_NETBSD) {
353 			efi_block_find_partitions_disklabel(bdev, &mbr,
354 			    le32toh(mbr_part->mbrp_start),
355 			    le32toh(mbr_part->mbrp_size));
356 			break;
357 		}
358 	}
359 
360 	return 0;
361 }
362 
363 static const struct {
364 	struct uuid guid;
365 	uint8_t fstype;
366 } gpt_guid_to_str[] = {
367 	{ GPT_ENT_TYPE_NETBSD_FFS,		FS_BSDFFS },
368 	{ GPT_ENT_TYPE_NETBSD_LFS,		FS_BSDLFS },
369 	{ GPT_ENT_TYPE_NETBSD_RAIDFRAME,	FS_RAID },
370 	{ GPT_ENT_TYPE_NETBSD_CCD,		FS_CCD },
371 	{ GPT_ENT_TYPE_NETBSD_CGD,		FS_CGD },
372 	{ GPT_ENT_TYPE_MS_BASIC_DATA,		FS_MSDOS },	/* or NTFS? ambiguous */
373 	{ GPT_ENT_TYPE_EFI,			FS_MSDOS },
374 };
375 
376 static int
efi_block_find_partitions_gpt_entry(struct efi_block_dev * bdev,struct gpt_hdr * hdr,struct gpt_ent * ent,UINT32 index)377 efi_block_find_partitions_gpt_entry(struct efi_block_dev *bdev,
378     struct gpt_hdr *hdr, struct gpt_ent *ent, UINT32 index)
379 {
380 	struct efi_block_part *bpart;
381 	uint8_t fstype = FS_UNUSED;
382 	struct uuid uuid;
383 	int n;
384 
385 	memcpy(&uuid, ent->ent_type, sizeof(uuid));
386 	for (n = 0; n < __arraycount(gpt_guid_to_str); n++)
387 		if (memcmp(ent->ent_type, &gpt_guid_to_str[n].guid,
388 		    sizeof(ent->ent_type)) == 0) {
389 			fstype = gpt_guid_to_str[n].fstype;
390 			break;
391 		}
392 	if (fstype == FS_UNUSED)
393 		return 0;
394 
395 	bpart = alloc(sizeof(*bpart));
396 	bpart->index = index;
397 	bpart->bdev = bdev;
398 	bpart->type = EFI_BLOCK_PART_GPT;
399 	bpart->gpt.fstype = fstype;
400 	bpart->gpt.ent = *ent;
401 	if (fstype == FS_RAID) {
402 		bpart->gpt.ent.ent_lba_start += RF_PROTECTED_SECTORS;
403 		bpart->gpt.ent.ent_lba_end -= RF_PROTECTED_SECTORS;
404 	}
405 	memcpy(bpart->hash, ent->ent_guid, sizeof(bpart->hash));
406 	TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
407 
408 	return 0;
409 }
410 
411 static int
efi_block_find_partitions_gpt(struct efi_block_dev * bdev)412 efi_block_find_partitions_gpt(struct efi_block_dev *bdev)
413 {
414 	struct gpt_hdr hdr;
415 	struct gpt_ent ent;
416 	EFI_STATUS status;
417 	UINT32 entry;
418 	void *buf;
419 	UINTN sz;
420 
421 	status = efi_block_read(bdev, (EFI_LBA)GPT_HDR_BLKNO * bdev->bio->Media->BlockSize, &hdr,
422 	    sizeof(hdr));
423 	if (EFI_ERROR(status)) {
424 		return EIO;
425 	}
426 
427 	if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0)
428 		return ENOENT;
429 	if (le32toh(hdr.hdr_entsz) < sizeof(ent))
430 		return EINVAL;
431 
432 	sz = le32toh(hdr.hdr_entsz) * le32toh(hdr.hdr_entries);
433 	buf = AllocatePool(sz);
434 	if (buf == NULL)
435 		return ENOMEM;
436 
437 	status = efi_block_read(bdev,
438 	    le64toh(hdr.hdr_lba_table) * bdev->bio->Media->BlockSize, buf, sz);
439 	if (EFI_ERROR(status)) {
440 		FreePool(buf);
441 		return EIO;
442 	}
443 
444 	for (entry = 0; entry < le32toh(hdr.hdr_entries); entry++) {
445 		memcpy(&ent, (UINT8 *)buf + (entry * le32toh(hdr.hdr_entsz)),
446 			sizeof(ent));
447 		efi_block_find_partitions_gpt_entry(bdev, &hdr, &ent, entry);
448 	}
449 
450 	FreePool(buf);
451 
452 	return 0;
453 }
454 
455 static int
efi_block_find_partitions(struct efi_block_dev * bdev)456 efi_block_find_partitions(struct efi_block_dev *bdev)
457 {
458 	int error;
459 
460 	error = efi_block_find_partitions_gpt(bdev);
461 	if (error)
462 		error = efi_block_find_partitions_mbr(bdev);
463 	if (error)
464 		error = efi_block_find_partitions_cd9660(bdev);
465 
466 	return error;
467 }
468 
469 void
efi_block_probe(void)470 efi_block_probe(void)
471 {
472 	struct efi_block_dev *bdev;
473 	struct efi_block_part *bpart;
474 	EFI_BLOCK_IO *bio;
475 	EFI_DISK_IO *dio;
476 	EFI_STATUS status;
477 	uint16_t devindex = 0;
478 	int depth = -1;
479 	int n;
480 
481 	status = LibLocateHandle(ByProtocol, &BlockIoProtocol, NULL, &efi_nblock, &efi_block);
482 	if (EFI_ERROR(status))
483 		return;
484 
485 	if (efi_bootdp) {
486 		depth = efi_device_path_depth(efi_bootdp, MEDIA_DEVICE_PATH);
487 		if (depth == 0)
488 			depth = 1;
489 		else if (depth == -1)
490 			depth = 2;
491 	}
492 
493 	for (n = 0; n < efi_nblock; n++) {
494 		/* EFI_BLOCK_IO_PROTOCOL is required */
495 		status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_block[n],
496 		    &BlockIoProtocol, (void **)&bio);
497 		if (EFI_ERROR(status) || !bio->Media->MediaPresent)
498 			continue;
499 
500 		/* Ignore logical partitions (we do our own partition discovery) */
501 		if (bio->Media->LogicalPartition)
502 			continue;
503 
504 		/* EFI_DISK_IO_PROTOCOL is optional */
505 		status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_block[n],
506 		    &DiskIoProtocol, (void **)&dio);
507 		if (EFI_ERROR(status)) {
508 			dio = NULL;
509 		}
510 
511 		bdev = alloc(sizeof(*bdev));
512 		bdev->index = devindex++;
513 		bdev->bio = bio;
514 		bdev->dio = dio;
515 		bdev->media_id = bio->Media->MediaId;
516 		bdev->path = DevicePathFromHandle(efi_block[n]);
517 		TAILQ_INIT(&bdev->partitions);
518 		TAILQ_INSERT_TAIL(&efi_block_devs, bdev, entries);
519 
520 		efi_block_find_partitions(bdev);
521 
522 		if (depth > 0 && efi_device_path_ncmp(efi_bootdp, DevicePathFromHandle(efi_block[n]), depth) == 0) {
523 			TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
524 				uint8_t fstype = FS_UNUSED;
525 				switch (bpart->type) {
526 				case EFI_BLOCK_PART_DISKLABEL:
527 					fstype = bpart->disklabel.part.p_fstype;
528 					break;
529 				case EFI_BLOCK_PART_GPT:
530 					fstype = bpart->gpt.fstype;
531 					break;
532 				case EFI_BLOCK_PART_CD9660:
533 					fstype = FS_ISO9660;
534 					break;
535 				}
536 				if (fstype == FS_BSDFFS || fstype == FS_ISO9660 || fstype == FS_RAID) {
537 					char devname[9];
538 					snprintf(devname, sizeof(devname), "hd%u%c", bdev->index, bpart->index + 'a');
539 					set_default_device(devname);
540 					set_default_fstype(fstype);
541 					break;
542 				}
543 			}
544 		}
545 	}
546 }
547 
548 static void
print_guid(const uint8_t * guid)549 print_guid(const uint8_t *guid)
550 {
551 	const int index[] = { 3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15 };
552 	int i;
553 
554 	for (i = 0; i < 16; i++) {
555 		printf("%02x", guid[index[i]]);
556 		if (i == 3 || i == 5 || i == 7 || i == 9)
557 			printf("-");
558 	}
559 }
560 
561 void
efi_block_show(void)562 efi_block_show(void)
563 {
564 	struct efi_block_dev *bdev;
565 	struct efi_block_part *bpart;
566 	uint64_t size;
567 	CHAR16 *path;
568 
569 	TAILQ_FOREACH(bdev, &efi_block_devs, entries) {
570 		printf("hd%u (", bdev->index);
571 
572 		/* Size in MB */
573 		size = ((bdev->bio->Media->LastBlock + 1) * bdev->bio->Media->BlockSize) / (1024 * 1024);
574 		if (size >= 10000)
575 			printf("%"PRIu64" GB", size / 1024);
576 		else
577 			printf("%"PRIu64" MB", size);
578 		printf("): ");
579 
580 		path = DevicePathToStr(bdev->path);
581 		Print(L"%s", path);
582 		FreePool(path);
583 
584 		printf("\n");
585 
586 		TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
587 			switch (bpart->type) {
588 			case EFI_BLOCK_PART_DISKLABEL:
589 				printf("  hd%u%c (", bdev->index, bpart->index + 'a');
590 
591 				/* Size in MB */
592 				size = ((uint64_t)bpart->disklabel.secsize * bpart->disklabel.part.p_size) / (1024 * 1024);
593 				if (size >= 10000)
594 					printf("%"PRIu64" GB", size / 1024);
595 				else
596 					printf("%"PRIu64" MB", size);
597 				printf("): ");
598 
599 				printf("%s\n", fstypenames[bpart->disklabel.part.p_fstype]);
600 				break;
601 			case EFI_BLOCK_PART_GPT:
602 				printf("  hd%u%c ", bdev->index, bpart->index + 'a');
603 
604 				if (bpart->gpt.ent.ent_name[0] == 0x0000) {
605 					printf("\"");
606 					print_guid(bpart->gpt.ent.ent_guid);
607 					printf("\"");
608 				} else {
609 					Print(L"\"%s\"", bpart->gpt.ent.ent_name);
610 				}
611 
612 				/* Size in MB */
613 				size = (le64toh(bpart->gpt.ent.ent_lba_end) - le64toh(bpart->gpt.ent.ent_lba_start)) * bdev->bio->Media->BlockSize;
614 				size /= (1024 * 1024);
615 				if (size >= 10000)
616 					printf(" (%"PRIu64" GB): ", size / 1024);
617 				else
618 					printf(" (%"PRIu64" MB): ", size);
619 
620 				printf("%s\n", fstypenames[bpart->gpt.fstype]);
621 				break;
622 			case EFI_BLOCK_PART_CD9660:
623 				printf("  hd%u%c %s\n", bdev->index, bpart->index + 'a', fstypenames[FS_ISO9660]);
624 				break;
625 			default:
626 				break;
627 			}
628 		}
629 	}
630 }
631 
632 struct efi_block_part *
efi_block_boot_part(void)633 efi_block_boot_part(void)
634 {
635 	return efi_block_booted;
636 }
637 
638 int
efi_block_open(struct open_file * f,...)639 efi_block_open(struct open_file *f, ...)
640 {
641 	struct efi_block_part *bpart;
642 	const char *fname;
643 	char **file;
644 	char *path;
645 	va_list ap;
646 	int rv, n;
647 
648 	va_start(ap, f);
649 	fname = va_arg(ap, const char *);
650 	file = va_arg(ap, char **);
651 	va_end(ap);
652 
653 	rv = efi_block_parse(fname, &bpart, &path);
654 	if (rv != 0)
655 		return rv;
656 
657 	for (n = 0; n < ndevs; n++)
658 		if (strcmp(DEV_NAME(&devsw[n]), "efiblock") == 0) {
659 			f->f_dev = &devsw[n];
660 			break;
661 		}
662 	if (n == ndevs)
663 		return ENXIO;
664 
665 	f->f_devdata = bpart;
666 
667 	*file = path;
668 
669 	efi_block_booted = bpart;
670 
671 	return 0;
672 }
673 
674 int
efi_block_close(struct open_file * f)675 efi_block_close(struct open_file *f)
676 {
677 	return 0;
678 }
679 
680 int
efi_block_strategy(void * devdata,int rw,daddr_t dblk,size_t size,void * buf,size_t * rsize)681 efi_block_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize)
682 {
683 	struct efi_block_part *bpart = devdata;
684 	struct efi_block_dev *bdev = bpart->bdev;
685 	EFI_STATUS status;
686 	UINT64 off;
687 
688 	if (rw != F_READ)
689 		return EROFS;
690 
691 	efi_set_watchdog(EFI_BLOCK_TIMEOUT, EFI_BLOCK_TIMEOUT_CODE);
692 
693 	switch (bpart->type) {
694 	case EFI_BLOCK_PART_DISKLABEL:
695 		off = ((EFI_LBA)dblk + bpart->disklabel.part.p_offset) * bdev->bio->Media->BlockSize;
696 		break;
697 	case EFI_BLOCK_PART_GPT:
698 		off = ((EFI_LBA)dblk + le64toh(bpart->gpt.ent.ent_lba_start)) * bdev->bio->Media->BlockSize;
699 		break;
700 	case EFI_BLOCK_PART_CD9660:
701 		off = (EFI_LBA)dblk * ISO_DEFAULT_BLOCK_SIZE;
702 		break;
703 	default:
704 		return EINVAL;
705 	}
706 
707 	status = efi_block_read(bpart->bdev, off, buf, size);
708 	if (EFI_ERROR(status))
709 		return EIO;
710 
711 	*rsize = size;
712 
713 	return 0;
714 }
715 
716 void
efi_block_set_readahead(bool onoff)717 efi_block_set_readahead(bool onoff)
718 {
719 	efi_ra_enable = onoff;
720 }
721 
722 int
efi_block_ioctl(struct open_file * f,u_long cmd,void * data)723 efi_block_ioctl(struct open_file *f, u_long cmd, void *data)
724 {
725 	struct efi_block_part *bpart = f->f_devdata;
726 	struct efi_block_dev *bdev = bpart->bdev;
727 	int error = 0;
728 
729 	switch (cmd) {
730 	case SAIOSECSIZE:
731 		*(u_int *)data = bdev->bio->Media->BlockSize;
732 		break;
733 	default:
734 		error = ENOTTY;
735 		break;
736 	}
737 
738 	return error;
739 }
740