efiblock.c revision 1.6 1 1.6 jmcneill /* $NetBSD: efiblock.c,v 1.6 2019/09/21 10:19:37 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.1 jmcneill #include "efiboot.h"
37 1.1 jmcneill #include "efiblock.h"
38 1.1 jmcneill
39 1.1 jmcneill static EFI_HANDLE *efi_block;
40 1.1 jmcneill static UINTN efi_nblock;
41 1.2 jmcneill static struct efi_block_part *efi_block_booted = NULL;
42 1.1 jmcneill
43 1.1 jmcneill static TAILQ_HEAD(, efi_block_dev) efi_block_devs = TAILQ_HEAD_INITIALIZER(efi_block_devs);
44 1.1 jmcneill
45 1.1 jmcneill static int
46 1.1 jmcneill efi_block_parse(const char *fname, struct efi_block_part **pbpart, char **pfile)
47 1.1 jmcneill {
48 1.1 jmcneill struct efi_block_dev *bdev;
49 1.1 jmcneill struct efi_block_part *bpart;
50 1.1 jmcneill char pathbuf[PATH_MAX], *default_device, *ep = NULL;
51 1.1 jmcneill const char *full_path;
52 1.1 jmcneill intmax_t dev;
53 1.1 jmcneill int part;
54 1.1 jmcneill
55 1.1 jmcneill default_device = get_default_device();
56 1.1 jmcneill if (strchr(fname, ':') == NULL) {
57 1.1 jmcneill if (strlen(default_device) > 0) {
58 1.1 jmcneill snprintf(pathbuf, sizeof(pathbuf), "%s:%s", default_device, fname);
59 1.1 jmcneill full_path = pathbuf;
60 1.1 jmcneill *pfile = __UNCONST(fname);
61 1.1 jmcneill } else {
62 1.1 jmcneill return EINVAL;
63 1.1 jmcneill }
64 1.1 jmcneill } else {
65 1.1 jmcneill full_path = fname;
66 1.1 jmcneill *pfile = strchr(fname, ':') + 1;
67 1.1 jmcneill }
68 1.1 jmcneill
69 1.1 jmcneill if (strncasecmp(full_path, "hd", 2) != 0)
70 1.1 jmcneill return EINVAL;
71 1.1 jmcneill dev = strtoimax(full_path + 2, &ep, 10);
72 1.1 jmcneill if (dev < 0 || dev >= efi_nblock)
73 1.1 jmcneill return ENXIO;
74 1.1 jmcneill if (ep[0] < 'a' || ep[0] >= 'a' + MAXPARTITIONS || ep[1] != ':')
75 1.1 jmcneill return EINVAL;
76 1.1 jmcneill part = ep[0] - 'a';
77 1.1 jmcneill TAILQ_FOREACH(bdev, &efi_block_devs, entries) {
78 1.1 jmcneill if (bdev->index == dev) {
79 1.1 jmcneill TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
80 1.1 jmcneill if (bpart->index == part) {
81 1.1 jmcneill *pbpart = bpart;
82 1.1 jmcneill return 0;
83 1.1 jmcneill }
84 1.1 jmcneill }
85 1.1 jmcneill }
86 1.1 jmcneill }
87 1.1 jmcneill
88 1.1 jmcneill return ENOENT;
89 1.1 jmcneill }
90 1.1 jmcneill
91 1.2 jmcneill static void
92 1.2 jmcneill efi_block_generate_hash_mbr(struct efi_block_part *bpart, struct mbr_sector *mbr)
93 1.2 jmcneill {
94 1.2 jmcneill MD5_CTX md5ctx;
95 1.2 jmcneill
96 1.2 jmcneill MD5Init(&md5ctx);
97 1.2 jmcneill MD5Update(&md5ctx, (void *)mbr, sizeof(*mbr));
98 1.2 jmcneill MD5Final(bpart->hash, &md5ctx);
99 1.2 jmcneill }
100 1.2 jmcneill
101 1.6 jmcneill static void *
102 1.6 jmcneill efi_block_allocate_device_buffer(struct efi_block_dev *bdev, UINTN size,
103 1.6 jmcneill void **buf_start)
104 1.6 jmcneill {
105 1.6 jmcneill void *buf;
106 1.6 jmcneill
107 1.6 jmcneill if (bdev->bio->Media->IoAlign <= 1)
108 1.6 jmcneill *buf_start = buf = AllocatePool(size);
109 1.6 jmcneill else {
110 1.6 jmcneill buf = AllocatePool(size + bdev->bio->Media->IoAlign - 1);
111 1.6 jmcneill *buf_start = (buf == NULL) ? NULL : (void *)(((intptr_t)buf +
112 1.6 jmcneill bdev->bio->Media->IoAlign - 1) & ~(bdev->bio->Media->IoAlign - 1));
113 1.6 jmcneill }
114 1.6 jmcneill
115 1.6 jmcneill return buf;
116 1.6 jmcneill }
117 1.6 jmcneill
118 1.1 jmcneill static int
119 1.2 jmcneill efi_block_find_partitions_disklabel(struct efi_block_dev *bdev, struct mbr_sector *mbr, uint32_t start, uint32_t size)
120 1.1 jmcneill {
121 1.1 jmcneill struct efi_block_part *bpart;
122 1.1 jmcneill struct disklabel d;
123 1.1 jmcneill struct partition *p;
124 1.1 jmcneill EFI_STATUS status;
125 1.1 jmcneill EFI_LBA lba;
126 1.6 jmcneill void *buf, *buf_start;
127 1.1 jmcneill UINT32 sz;
128 1.1 jmcneill int n;
129 1.1 jmcneill
130 1.1 jmcneill sz = __MAX(sizeof(d), bdev->bio->Media->BlockSize);
131 1.1 jmcneill sz = roundup(sz, bdev->bio->Media->BlockSize);
132 1.6 jmcneill if ((buf = efi_block_allocate_device_buffer(bdev, sz, &buf_start)) == NULL)
133 1.1 jmcneill return ENOMEM;
134 1.1 jmcneill
135 1.3 jakllsch lba = (((EFI_LBA)start + LABELSECTOR) * DEV_BSIZE) / bdev->bio->Media->BlockSize;
136 1.6 jmcneill status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id,
137 1.6 jmcneill lba, sz, buf_start);
138 1.6 jmcneill if (EFI_ERROR(status) || getdisklabel(buf_start, &d) != NULL) {
139 1.1 jmcneill FreePool(buf);
140 1.1 jmcneill return EIO;
141 1.1 jmcneill }
142 1.1 jmcneill FreePool(buf);
143 1.1 jmcneill
144 1.1 jmcneill if (le32toh(d.d_magic) != DISKMAGIC || le32toh(d.d_magic2) != DISKMAGIC)
145 1.1 jmcneill return EINVAL;
146 1.1 jmcneill if (le16toh(d.d_npartitions) > MAXPARTITIONS)
147 1.1 jmcneill return EINVAL;
148 1.1 jmcneill
149 1.1 jmcneill for (n = 0; n < le16toh(d.d_npartitions); n++) {
150 1.1 jmcneill p = &d.d_partitions[n];
151 1.1 jmcneill switch (p->p_fstype) {
152 1.1 jmcneill case FS_BSDFFS:
153 1.1 jmcneill case FS_MSDOS:
154 1.1 jmcneill case FS_BSDLFS:
155 1.1 jmcneill break;
156 1.1 jmcneill default:
157 1.1 jmcneill continue;
158 1.1 jmcneill }
159 1.1 jmcneill
160 1.1 jmcneill bpart = alloc(sizeof(*bpart));
161 1.1 jmcneill bpart->index = n;
162 1.1 jmcneill bpart->bdev = bdev;
163 1.1 jmcneill bpart->type = EFI_BLOCK_PART_DISKLABEL;
164 1.1 jmcneill bpart->disklabel.secsize = le32toh(d.d_secsize);
165 1.1 jmcneill bpart->disklabel.part = *p;
166 1.2 jmcneill efi_block_generate_hash_mbr(bpart, mbr);
167 1.1 jmcneill TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
168 1.1 jmcneill }
169 1.1 jmcneill
170 1.1 jmcneill return 0;
171 1.1 jmcneill }
172 1.1 jmcneill
173 1.1 jmcneill static int
174 1.1 jmcneill efi_block_find_partitions_mbr(struct efi_block_dev *bdev)
175 1.1 jmcneill {
176 1.1 jmcneill struct mbr_sector mbr;
177 1.1 jmcneill struct mbr_partition *mbr_part;
178 1.1 jmcneill EFI_STATUS status;
179 1.6 jmcneill void *buf, *buf_start;
180 1.1 jmcneill UINT32 sz;
181 1.1 jmcneill int n;
182 1.1 jmcneill
183 1.1 jmcneill sz = __MAX(sizeof(mbr), bdev->bio->Media->BlockSize);
184 1.1 jmcneill sz = roundup(sz, bdev->bio->Media->BlockSize);
185 1.6 jmcneill if ((buf = efi_block_allocate_device_buffer(bdev, sz, &buf_start)) == NULL)
186 1.1 jmcneill return ENOMEM;
187 1.1 jmcneill
188 1.6 jmcneill status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id,
189 1.6 jmcneill 0, sz, buf_start);
190 1.1 jmcneill if (EFI_ERROR(status)) {
191 1.1 jmcneill FreePool(buf);
192 1.1 jmcneill return EIO;
193 1.1 jmcneill }
194 1.6 jmcneill memcpy(&mbr, buf_start, sizeof(mbr));
195 1.1 jmcneill FreePool(buf);
196 1.1 jmcneill
197 1.1 jmcneill if (le32toh(mbr.mbr_magic) != MBR_MAGIC)
198 1.1 jmcneill return ENOENT;
199 1.1 jmcneill
200 1.1 jmcneill for (n = 0; n < MBR_PART_COUNT; n++) {
201 1.1 jmcneill mbr_part = &mbr.mbr_parts[n];
202 1.1 jmcneill if (le32toh(mbr_part->mbrp_size) == 0)
203 1.1 jmcneill continue;
204 1.1 jmcneill if (mbr_part->mbrp_type == MBR_PTYPE_NETBSD) {
205 1.2 jmcneill efi_block_find_partitions_disklabel(bdev, &mbr, le32toh(mbr_part->mbrp_start), le32toh(mbr_part->mbrp_size));
206 1.1 jmcneill break;
207 1.1 jmcneill }
208 1.1 jmcneill }
209 1.1 jmcneill
210 1.1 jmcneill return 0;
211 1.1 jmcneill }
212 1.1 jmcneill
213 1.4 jmcneill static const struct {
214 1.4 jmcneill struct uuid guid;
215 1.4 jmcneill uint8_t fstype;
216 1.4 jmcneill } gpt_guid_to_str[] = {
217 1.4 jmcneill { GPT_ENT_TYPE_NETBSD_FFS, FS_BSDFFS },
218 1.4 jmcneill { GPT_ENT_TYPE_NETBSD_LFS, FS_BSDLFS },
219 1.4 jmcneill { GPT_ENT_TYPE_NETBSD_RAIDFRAME, FS_RAID },
220 1.4 jmcneill { GPT_ENT_TYPE_NETBSD_CCD, FS_CCD },
221 1.4 jmcneill { GPT_ENT_TYPE_NETBSD_CGD, FS_CGD },
222 1.4 jmcneill { GPT_ENT_TYPE_MS_BASIC_DATA, FS_MSDOS }, /* or NTFS? ambiguous */
223 1.4 jmcneill };
224 1.4 jmcneill
225 1.4 jmcneill static int
226 1.4 jmcneill efi_block_find_partitions_gpt_entry(struct efi_block_dev *bdev, struct gpt_hdr *hdr, struct gpt_ent *ent, UINT32 index)
227 1.4 jmcneill {
228 1.4 jmcneill struct efi_block_part *bpart;
229 1.4 jmcneill uint8_t fstype = FS_UNUSED;
230 1.4 jmcneill struct uuid uuid;
231 1.4 jmcneill int n;
232 1.4 jmcneill
233 1.4 jmcneill memcpy(&uuid, ent->ent_type, sizeof(uuid));
234 1.4 jmcneill for (n = 0; n < __arraycount(gpt_guid_to_str); n++)
235 1.4 jmcneill if (memcmp(ent->ent_type, &gpt_guid_to_str[n].guid, sizeof(ent->ent_type)) == 0) {
236 1.4 jmcneill fstype = gpt_guid_to_str[n].fstype;
237 1.4 jmcneill break;
238 1.4 jmcneill }
239 1.4 jmcneill if (fstype == FS_UNUSED)
240 1.4 jmcneill return 0;
241 1.4 jmcneill
242 1.4 jmcneill bpart = alloc(sizeof(*bpart));
243 1.4 jmcneill bpart->index = index;
244 1.4 jmcneill bpart->bdev = bdev;
245 1.4 jmcneill bpart->type = EFI_BLOCK_PART_GPT;
246 1.4 jmcneill bpart->gpt.fstype = fstype;
247 1.4 jmcneill bpart->gpt.ent = *ent;
248 1.4 jmcneill memcpy(bpart->hash, ent->ent_guid, sizeof(bpart->hash));
249 1.4 jmcneill TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
250 1.4 jmcneill
251 1.4 jmcneill return 0;
252 1.4 jmcneill }
253 1.4 jmcneill
254 1.4 jmcneill static int
255 1.4 jmcneill efi_block_find_partitions_gpt(struct efi_block_dev *bdev)
256 1.4 jmcneill {
257 1.4 jmcneill struct gpt_hdr hdr;
258 1.4 jmcneill struct gpt_ent ent;
259 1.4 jmcneill EFI_STATUS status;
260 1.6 jmcneill void *buf, *buf_start;
261 1.4 jmcneill UINT32 sz, entry;
262 1.4 jmcneill
263 1.4 jmcneill sz = __MAX(sizeof(hdr), bdev->bio->Media->BlockSize);
264 1.4 jmcneill sz = roundup(sz, bdev->bio->Media->BlockSize);
265 1.6 jmcneill if ((buf = efi_block_allocate_device_buffer(bdev, sz, &buf_start)) == NULL)
266 1.4 jmcneill return ENOMEM;
267 1.4 jmcneill
268 1.6 jmcneill status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id,
269 1.6 jmcneill GPT_HDR_BLKNO, sz, buf_start);
270 1.4 jmcneill if (EFI_ERROR(status)) {
271 1.4 jmcneill FreePool(buf);
272 1.4 jmcneill return EIO;
273 1.4 jmcneill }
274 1.6 jmcneill memcpy(&hdr, buf_start, sizeof(hdr));
275 1.4 jmcneill FreePool(buf);
276 1.4 jmcneill
277 1.4 jmcneill if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0)
278 1.4 jmcneill return ENOENT;
279 1.4 jmcneill if (le32toh(hdr.hdr_entsz) < sizeof(ent))
280 1.4 jmcneill return EINVAL;
281 1.4 jmcneill
282 1.4 jmcneill sz = __MAX(le32toh(hdr.hdr_entsz) * le32toh(hdr.hdr_entries), bdev->bio->Media->BlockSize);
283 1.4 jmcneill sz = roundup(sz, bdev->bio->Media->BlockSize);
284 1.6 jmcneill if ((buf = efi_block_allocate_device_buffer(bdev, sz, &buf_start)) == NULL)
285 1.4 jmcneill return ENOMEM;
286 1.4 jmcneill
287 1.6 jmcneill status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id,
288 1.6 jmcneill le64toh(hdr.hdr_lba_table), sz, buf_start);
289 1.4 jmcneill if (EFI_ERROR(status)) {
290 1.4 jmcneill FreePool(buf);
291 1.4 jmcneill return EIO;
292 1.4 jmcneill }
293 1.4 jmcneill
294 1.4 jmcneill for (entry = 0; entry < le32toh(hdr.hdr_entries); entry++) {
295 1.6 jmcneill memcpy(&ent, buf_start + (entry * le32toh(hdr.hdr_entsz)),
296 1.6 jmcneill sizeof(ent));
297 1.4 jmcneill efi_block_find_partitions_gpt_entry(bdev, &hdr, &ent, entry);
298 1.4 jmcneill }
299 1.4 jmcneill
300 1.4 jmcneill FreePool(buf);
301 1.4 jmcneill
302 1.4 jmcneill return 0;
303 1.4 jmcneill }
304 1.4 jmcneill
305 1.1 jmcneill static int
306 1.1 jmcneill efi_block_find_partitions(struct efi_block_dev *bdev)
307 1.1 jmcneill {
308 1.4 jmcneill int error;
309 1.4 jmcneill
310 1.4 jmcneill error = efi_block_find_partitions_gpt(bdev);
311 1.4 jmcneill if (error)
312 1.4 jmcneill error = efi_block_find_partitions_mbr(bdev);
313 1.4 jmcneill
314 1.4 jmcneill return error;
315 1.1 jmcneill }
316 1.1 jmcneill
317 1.1 jmcneill void
318 1.1 jmcneill efi_block_probe(void)
319 1.1 jmcneill {
320 1.1 jmcneill struct efi_block_dev *bdev;
321 1.4 jmcneill struct efi_block_part *bpart;
322 1.1 jmcneill EFI_BLOCK_IO *bio;
323 1.1 jmcneill EFI_STATUS status;
324 1.1 jmcneill uint16_t devindex = 0;
325 1.1 jmcneill int depth = -1;
326 1.1 jmcneill int n;
327 1.1 jmcneill
328 1.1 jmcneill status = LibLocateHandle(ByProtocol, &BlockIoProtocol, NULL, &efi_nblock, &efi_block);
329 1.1 jmcneill if (EFI_ERROR(status))
330 1.1 jmcneill return;
331 1.1 jmcneill
332 1.1 jmcneill if (efi_bootdp) {
333 1.1 jmcneill depth = efi_device_path_depth(efi_bootdp, MEDIA_DEVICE_PATH);
334 1.1 jmcneill if (depth == 0)
335 1.1 jmcneill depth = 1;
336 1.5 jmcneill else if (depth == -1)
337 1.5 jmcneill depth = 2;
338 1.1 jmcneill }
339 1.1 jmcneill
340 1.1 jmcneill for (n = 0; n < efi_nblock; n++) {
341 1.1 jmcneill status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_block[n], &BlockIoProtocol, (void **)&bio);
342 1.1 jmcneill if (EFI_ERROR(status) || !bio->Media->MediaPresent)
343 1.1 jmcneill continue;
344 1.1 jmcneill
345 1.1 jmcneill if (bio->Media->LogicalPartition)
346 1.1 jmcneill continue;
347 1.1 jmcneill
348 1.1 jmcneill bdev = alloc(sizeof(*bdev));
349 1.1 jmcneill bdev->index = devindex++;
350 1.1 jmcneill bdev->bio = bio;
351 1.1 jmcneill bdev->media_id = bio->Media->MediaId;
352 1.1 jmcneill bdev->path = DevicePathFromHandle(efi_block[n]);
353 1.1 jmcneill TAILQ_INIT(&bdev->partitions);
354 1.1 jmcneill TAILQ_INSERT_TAIL(&efi_block_devs, bdev, entries);
355 1.1 jmcneill
356 1.4 jmcneill efi_block_find_partitions(bdev);
357 1.4 jmcneill
358 1.1 jmcneill if (depth > 0 && efi_device_path_ncmp(efi_bootdp, DevicePathFromHandle(efi_block[n]), depth) == 0) {
359 1.4 jmcneill TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
360 1.4 jmcneill uint8_t fstype = FS_UNUSED;
361 1.4 jmcneill switch (bpart->type) {
362 1.4 jmcneill case EFI_BLOCK_PART_DISKLABEL:
363 1.4 jmcneill fstype = bpart->disklabel.part.p_fstype;
364 1.4 jmcneill break;
365 1.4 jmcneill case EFI_BLOCK_PART_GPT:
366 1.4 jmcneill fstype = bpart->gpt.fstype;
367 1.4 jmcneill break;
368 1.4 jmcneill }
369 1.4 jmcneill if (fstype == FS_BSDFFS) {
370 1.4 jmcneill char devname[9];
371 1.4 jmcneill snprintf(devname, sizeof(devname), "hd%u%c", bdev->index, bpart->index + 'a');
372 1.4 jmcneill set_default_device(devname);
373 1.4 jmcneill break;
374 1.4 jmcneill }
375 1.4 jmcneill }
376 1.1 jmcneill }
377 1.4 jmcneill }
378 1.4 jmcneill }
379 1.1 jmcneill
380 1.4 jmcneill static void
381 1.4 jmcneill print_guid(const uint8_t *guid)
382 1.4 jmcneill {
383 1.4 jmcneill const int index[] = { 3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15 };
384 1.4 jmcneill int i;
385 1.4 jmcneill
386 1.4 jmcneill for (i = 0; i < 16; i++) {
387 1.4 jmcneill printf("%02x", guid[index[i]]);
388 1.4 jmcneill if (i == 3 || i == 5 || i == 7 || i == 9)
389 1.4 jmcneill printf("-");
390 1.1 jmcneill }
391 1.1 jmcneill }
392 1.1 jmcneill
393 1.1 jmcneill void
394 1.1 jmcneill efi_block_show(void)
395 1.1 jmcneill {
396 1.1 jmcneill struct efi_block_dev *bdev;
397 1.1 jmcneill struct efi_block_part *bpart;
398 1.1 jmcneill uint64_t size;
399 1.1 jmcneill CHAR16 *path;
400 1.1 jmcneill
401 1.1 jmcneill TAILQ_FOREACH(bdev, &efi_block_devs, entries) {
402 1.1 jmcneill printf("hd%u (", bdev->index);
403 1.1 jmcneill
404 1.1 jmcneill /* Size in MB */
405 1.1 jmcneill size = ((bdev->bio->Media->LastBlock + 1) * bdev->bio->Media->BlockSize) / (1024 * 1024);
406 1.1 jmcneill if (size >= 10000)
407 1.1 jmcneill printf("%"PRIu64" GB", size / 1024);
408 1.1 jmcneill else
409 1.1 jmcneill printf("%"PRIu64" MB", size);
410 1.1 jmcneill printf("): ");
411 1.1 jmcneill
412 1.1 jmcneill path = DevicePathToStr(bdev->path);
413 1.1 jmcneill Print(L"%s", path);
414 1.1 jmcneill FreePool(path);
415 1.1 jmcneill
416 1.1 jmcneill printf("\n");
417 1.1 jmcneill
418 1.1 jmcneill TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
419 1.1 jmcneill switch (bpart->type) {
420 1.1 jmcneill case EFI_BLOCK_PART_DISKLABEL:
421 1.1 jmcneill printf(" hd%u%c (", bdev->index, bpart->index + 'a');
422 1.1 jmcneill
423 1.1 jmcneill /* Size in MB */
424 1.1 jmcneill size = ((uint64_t)bpart->disklabel.secsize * bpart->disklabel.part.p_size) / (1024 * 1024);
425 1.1 jmcneill if (size >= 10000)
426 1.1 jmcneill printf("%"PRIu64" GB", size / 1024);
427 1.1 jmcneill else
428 1.1 jmcneill printf("%"PRIu64" MB", size);
429 1.1 jmcneill printf("): ");
430 1.1 jmcneill
431 1.1 jmcneill printf("%s\n", fstypenames[bpart->disklabel.part.p_fstype]);
432 1.1 jmcneill break;
433 1.4 jmcneill case EFI_BLOCK_PART_GPT:
434 1.4 jmcneill printf(" hd%u%c ", bdev->index, bpart->index + 'a');
435 1.4 jmcneill
436 1.4 jmcneill if (bpart->gpt.ent.ent_name[0] == 0x0000) {
437 1.4 jmcneill printf("\"");
438 1.4 jmcneill print_guid(bpart->gpt.ent.ent_guid);
439 1.4 jmcneill printf("\"");
440 1.4 jmcneill } else {
441 1.4 jmcneill Print(L"\"%s\"", bpart->gpt.ent.ent_name);
442 1.4 jmcneill }
443 1.4 jmcneill
444 1.4 jmcneill /* Size in MB */
445 1.4 jmcneill size = (le64toh(bpart->gpt.ent.ent_lba_end) - le64toh(bpart->gpt.ent.ent_lba_start)) * bdev->bio->Media->BlockSize;
446 1.4 jmcneill size /= (1024 * 1024);
447 1.4 jmcneill if (size >= 10000)
448 1.4 jmcneill printf(" (%"PRIu64" GB): ", size / 1024);
449 1.4 jmcneill else
450 1.4 jmcneill printf(" (%"PRIu64" MB): ", size);
451 1.4 jmcneill
452 1.4 jmcneill printf("%s\n", fstypenames[bpart->gpt.fstype]);
453 1.4 jmcneill break;
454 1.1 jmcneill default:
455 1.1 jmcneill break;
456 1.1 jmcneill }
457 1.1 jmcneill }
458 1.1 jmcneill }
459 1.1 jmcneill }
460 1.1 jmcneill
461 1.2 jmcneill struct efi_block_part *
462 1.2 jmcneill efi_block_boot_part(void)
463 1.2 jmcneill {
464 1.2 jmcneill return efi_block_booted;
465 1.2 jmcneill }
466 1.2 jmcneill
467 1.1 jmcneill int
468 1.1 jmcneill efi_block_open(struct open_file *f, ...)
469 1.1 jmcneill {
470 1.1 jmcneill struct efi_block_part *bpart;
471 1.1 jmcneill const char *fname;
472 1.1 jmcneill char **file;
473 1.1 jmcneill char *path;
474 1.1 jmcneill va_list ap;
475 1.1 jmcneill int rv, n;
476 1.1 jmcneill
477 1.1 jmcneill va_start(ap, f);
478 1.1 jmcneill fname = va_arg(ap, const char *);
479 1.1 jmcneill file = va_arg(ap, char **);
480 1.1 jmcneill va_end(ap);
481 1.1 jmcneill
482 1.1 jmcneill rv = efi_block_parse(fname, &bpart, &path);
483 1.1 jmcneill if (rv != 0)
484 1.1 jmcneill return rv;
485 1.1 jmcneill
486 1.1 jmcneill for (n = 0; n < ndevs; n++)
487 1.1 jmcneill if (strcmp(DEV_NAME(&devsw[n]), "efiblock") == 0) {
488 1.1 jmcneill f->f_dev = &devsw[n];
489 1.1 jmcneill break;
490 1.1 jmcneill }
491 1.1 jmcneill if (n == ndevs)
492 1.1 jmcneill return ENXIO;
493 1.1 jmcneill
494 1.1 jmcneill f->f_devdata = bpart;
495 1.1 jmcneill
496 1.1 jmcneill *file = path;
497 1.1 jmcneill
498 1.2 jmcneill efi_block_booted = bpart;
499 1.2 jmcneill
500 1.1 jmcneill return 0;
501 1.1 jmcneill }
502 1.1 jmcneill
503 1.1 jmcneill int
504 1.1 jmcneill efi_block_close(struct open_file *f)
505 1.1 jmcneill {
506 1.1 jmcneill return 0;
507 1.1 jmcneill }
508 1.1 jmcneill
509 1.1 jmcneill int
510 1.1 jmcneill efi_block_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize)
511 1.1 jmcneill {
512 1.1 jmcneill struct efi_block_part *bpart = devdata;
513 1.1 jmcneill EFI_STATUS status;
514 1.6 jmcneill void *allocated_buf, *aligned_buf;
515 1.1 jmcneill
516 1.1 jmcneill if (rw != F_READ)
517 1.1 jmcneill return EROFS;
518 1.1 jmcneill
519 1.1 jmcneill switch (bpart->type) {
520 1.1 jmcneill case EFI_BLOCK_PART_DISKLABEL:
521 1.1 jmcneill if (bpart->bdev->bio->Media->BlockSize != bpart->disklabel.secsize) {
522 1.1 jmcneill printf("%s: unsupported block size %d (expected %d)\n", __func__,
523 1.1 jmcneill bpart->bdev->bio->Media->BlockSize, bpart->disklabel.secsize);
524 1.1 jmcneill return EIO;
525 1.1 jmcneill }
526 1.1 jmcneill dblk += bpart->disklabel.part.p_offset;
527 1.1 jmcneill break;
528 1.4 jmcneill case EFI_BLOCK_PART_GPT:
529 1.4 jmcneill if (bpart->bdev->bio->Media->BlockSize != DEV_BSIZE) {
530 1.4 jmcneill printf("%s: unsupported block size %d (expected %d)\n", __func__,
531 1.4 jmcneill bpart->bdev->bio->Media->BlockSize, DEV_BSIZE);
532 1.4 jmcneill return EIO;
533 1.4 jmcneill }
534 1.4 jmcneill dblk += le64toh(bpart->gpt.ent.ent_lba_start);
535 1.4 jmcneill break;
536 1.1 jmcneill default:
537 1.1 jmcneill return EINVAL;
538 1.1 jmcneill }
539 1.1 jmcneill
540 1.6 jmcneill if ((bpart->bdev->bio->Media->IoAlign <= 1) ||
541 1.6 jmcneill ((intptr_t)buf & (bpart->bdev->bio->Media->IoAlign - 1)) == 0) {
542 1.6 jmcneill allocated_buf = NULL;
543 1.6 jmcneill aligned_buf = buf;
544 1.6 jmcneill } else if ((allocated_buf = efi_block_allocate_device_buffer(bpart->bdev,
545 1.6 jmcneill size, &aligned_buf)) == NULL)
546 1.6 jmcneill return ENOMEM;
547 1.6 jmcneill
548 1.6 jmcneill status = uefi_call_wrapper(bpart->bdev->bio->ReadBlocks, 5,
549 1.6 jmcneill bpart->bdev->bio, bpart->bdev->media_id, dblk, size, aligned_buf);
550 1.6 jmcneill if (EFI_ERROR(status)) {
551 1.6 jmcneill if (allocated_buf != NULL)
552 1.6 jmcneill FreePool(allocated_buf);
553 1.1 jmcneill return EIO;
554 1.6 jmcneill }
555 1.6 jmcneill if (allocated_buf != NULL) {
556 1.6 jmcneill memcpy(buf, aligned_buf, size);
557 1.6 jmcneill FreePool(allocated_buf);
558 1.6 jmcneill }
559 1.1 jmcneill
560 1.1 jmcneill *rsize = size;
561 1.1 jmcneill
562 1.1 jmcneill return 0;
563 1.1 jmcneill }
564