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