efiblock.c revision 1.3 1 /* $NetBSD: efiblock.c,v 1.3 2018/09/14 21:37:03 jakllsch Exp $ */
2
3 /*-
4 * Copyright (c) 2016 Kimihiro Nonaka <nonaka (at) netbsd.org>
5 * Copyright (c) 2018 Jared McNeill <jmcneill (at) 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
35 #include "efiboot.h"
36 #include "efiblock.h"
37
38 static EFI_HANDLE *efi_block;
39 static UINTN efi_nblock;
40 static struct efi_block_part *efi_block_booted = NULL;
41
42 static TAILQ_HEAD(, efi_block_dev) efi_block_devs = TAILQ_HEAD_INITIALIZER(efi_block_devs);
43
44 static int
45 efi_block_parse(const char *fname, struct efi_block_part **pbpart, char **pfile)
46 {
47 struct efi_block_dev *bdev;
48 struct efi_block_part *bpart;
49 char pathbuf[PATH_MAX], *default_device, *ep = NULL;
50 const char *full_path;
51 intmax_t dev;
52 int part;
53
54 default_device = get_default_device();
55 if (strchr(fname, ':') == NULL) {
56 if (strlen(default_device) > 0) {
57 snprintf(pathbuf, sizeof(pathbuf), "%s:%s", default_device, fname);
58 full_path = pathbuf;
59 *pfile = __UNCONST(fname);
60 } else {
61 return EINVAL;
62 }
63 } else {
64 full_path = fname;
65 *pfile = strchr(fname, ':') + 1;
66 }
67
68 if (strncasecmp(full_path, "hd", 2) != 0)
69 return EINVAL;
70 dev = strtoimax(full_path + 2, &ep, 10);
71 if (dev < 0 || dev >= efi_nblock)
72 return ENXIO;
73 if (ep[0] < 'a' || ep[0] >= 'a' + MAXPARTITIONS || ep[1] != ':')
74 return EINVAL;
75 part = ep[0] - 'a';
76 TAILQ_FOREACH(bdev, &efi_block_devs, entries) {
77 if (bdev->index == dev) {
78 TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
79 if (bpart->index == part) {
80 *pbpart = bpart;
81 return 0;
82 }
83 }
84 }
85 }
86
87 return ENOENT;
88 }
89
90 static void
91 efi_block_generate_hash_mbr(struct efi_block_part *bpart, struct mbr_sector *mbr)
92 {
93 MD5_CTX md5ctx;
94
95 MD5Init(&md5ctx);
96 MD5Update(&md5ctx, (void *)mbr, sizeof(*mbr));
97 MD5Final(bpart->hash, &md5ctx);
98 }
99
100 static int
101 efi_block_find_partitions_disklabel(struct efi_block_dev *bdev, struct mbr_sector *mbr, uint32_t start, uint32_t size)
102 {
103 struct efi_block_part *bpart;
104 struct disklabel d;
105 struct partition *p;
106 EFI_STATUS status;
107 EFI_LBA lba;
108 uint8_t *buf;
109 UINT32 sz;
110 int n;
111
112 sz = __MAX(sizeof(d), bdev->bio->Media->BlockSize);
113 sz = roundup(sz, bdev->bio->Media->BlockSize);
114 buf = AllocatePool(sz);
115 if (!buf)
116 return ENOMEM;
117
118 lba = (((EFI_LBA)start + LABELSECTOR) * DEV_BSIZE) / bdev->bio->Media->BlockSize;
119 status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id, lba, sz, buf);
120 if (EFI_ERROR(status) || getdisklabel(buf, &d) != NULL) {
121 FreePool(buf);
122 return EIO;
123 }
124 FreePool(buf);
125
126 if (le32toh(d.d_magic) != DISKMAGIC || le32toh(d.d_magic2) != DISKMAGIC)
127 return EINVAL;
128 if (le16toh(d.d_npartitions) > MAXPARTITIONS)
129 return EINVAL;
130
131 for (n = 0; n < le16toh(d.d_npartitions); n++) {
132 p = &d.d_partitions[n];
133 switch (p->p_fstype) {
134 case FS_BSDFFS:
135 case FS_MSDOS:
136 case FS_BSDLFS:
137 break;
138 default:
139 continue;
140 }
141
142 bpart = alloc(sizeof(*bpart));
143 bpart->index = n;
144 bpart->bdev = bdev;
145 bpart->type = EFI_BLOCK_PART_DISKLABEL;
146 bpart->disklabel.secsize = le32toh(d.d_secsize);
147 bpart->disklabel.part = *p;
148 efi_block_generate_hash_mbr(bpart, mbr);
149 TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
150 }
151
152 return 0;
153 }
154
155 static int
156 efi_block_find_partitions_mbr(struct efi_block_dev *bdev)
157 {
158 struct mbr_sector mbr;
159 struct mbr_partition *mbr_part;
160 EFI_STATUS status;
161 uint8_t *buf;
162 UINT32 sz;
163 int n;
164
165 sz = __MAX(sizeof(mbr), bdev->bio->Media->BlockSize);
166 sz = roundup(sz, bdev->bio->Media->BlockSize);
167 buf = AllocatePool(sz);
168 if (!buf)
169 return ENOMEM;
170
171 status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id, 0, sz, buf);
172 if (EFI_ERROR(status)) {
173 FreePool(buf);
174 return EIO;
175 }
176 memcpy(&mbr, buf, sizeof(mbr));
177 FreePool(buf);
178
179 if (le32toh(mbr.mbr_magic) != MBR_MAGIC)
180 return ENOENT;
181
182 for (n = 0; n < MBR_PART_COUNT; n++) {
183 mbr_part = &mbr.mbr_parts[n];
184 if (le32toh(mbr_part->mbrp_size) == 0)
185 continue;
186 if (mbr_part->mbrp_type == MBR_PTYPE_NETBSD) {
187 efi_block_find_partitions_disklabel(bdev, &mbr, le32toh(mbr_part->mbrp_start), le32toh(mbr_part->mbrp_size));
188 break;
189 }
190 }
191
192 return 0;
193 }
194
195 static int
196 efi_block_find_partitions(struct efi_block_dev *bdev)
197 {
198 return efi_block_find_partitions_mbr(bdev);
199 }
200
201 void
202 efi_block_probe(void)
203 {
204 struct efi_block_dev *bdev;
205 EFI_BLOCK_IO *bio;
206 EFI_STATUS status;
207 uint16_t devindex = 0;
208 int depth = -1;
209 int n;
210
211 status = LibLocateHandle(ByProtocol, &BlockIoProtocol, NULL, &efi_nblock, &efi_block);
212 if (EFI_ERROR(status))
213 return;
214
215 if (efi_bootdp) {
216 depth = efi_device_path_depth(efi_bootdp, MEDIA_DEVICE_PATH);
217 if (depth == 0)
218 depth = 1;
219 }
220
221 for (n = 0; n < efi_nblock; n++) {
222 status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_block[n], &BlockIoProtocol, (void **)&bio);
223 if (EFI_ERROR(status) || !bio->Media->MediaPresent)
224 continue;
225
226 if (bio->Media->LogicalPartition)
227 continue;
228
229 bdev = alloc(sizeof(*bdev));
230 bdev->index = devindex++;
231 bdev->bio = bio;
232 bdev->media_id = bio->Media->MediaId;
233 bdev->path = DevicePathFromHandle(efi_block[n]);
234 TAILQ_INIT(&bdev->partitions);
235 TAILQ_INSERT_TAIL(&efi_block_devs, bdev, entries);
236
237 if (depth > 0 && efi_device_path_ncmp(efi_bootdp, DevicePathFromHandle(efi_block[n]), depth) == 0) {
238 char devname[9];
239 snprintf(devname, sizeof(devname), "hd%ua", bdev->index);
240 set_default_device(devname);
241 }
242
243 efi_block_find_partitions(bdev);
244 }
245 }
246
247 void
248 efi_block_show(void)
249 {
250 struct efi_block_dev *bdev;
251 struct efi_block_part *bpart;
252 uint64_t size;
253 CHAR16 *path;
254
255 TAILQ_FOREACH(bdev, &efi_block_devs, entries) {
256 printf("hd%u (", bdev->index);
257
258 /* Size in MB */
259 size = ((bdev->bio->Media->LastBlock + 1) * bdev->bio->Media->BlockSize) / (1024 * 1024);
260 if (size >= 10000)
261 printf("%"PRIu64" GB", size / 1024);
262 else
263 printf("%"PRIu64" MB", size);
264 printf("): ");
265
266 path = DevicePathToStr(bdev->path);
267 Print(L"%s", path);
268 FreePool(path);
269
270 printf("\n");
271
272 TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
273 switch (bpart->type) {
274 case EFI_BLOCK_PART_DISKLABEL:
275 printf(" hd%u%c (", bdev->index, bpart->index + 'a');
276
277 /* Size in MB */
278 size = ((uint64_t)bpart->disklabel.secsize * bpart->disklabel.part.p_size) / (1024 * 1024);
279 if (size >= 10000)
280 printf("%"PRIu64" GB", size / 1024);
281 else
282 printf("%"PRIu64" MB", size);
283 printf("): ");
284
285 printf("%s\n", fstypenames[bpart->disklabel.part.p_fstype]);
286 break;
287 default:
288 break;
289 }
290 }
291 }
292 }
293
294 struct efi_block_part *
295 efi_block_boot_part(void)
296 {
297 return efi_block_booted;
298 }
299
300 int
301 efi_block_open(struct open_file *f, ...)
302 {
303 struct efi_block_part *bpart;
304 const char *fname;
305 char **file;
306 char *path;
307 va_list ap;
308 int rv, n;
309
310 va_start(ap, f);
311 fname = va_arg(ap, const char *);
312 file = va_arg(ap, char **);
313 va_end(ap);
314
315 rv = efi_block_parse(fname, &bpart, &path);
316 if (rv != 0)
317 return rv;
318
319 for (n = 0; n < ndevs; n++)
320 if (strcmp(DEV_NAME(&devsw[n]), "efiblock") == 0) {
321 f->f_dev = &devsw[n];
322 break;
323 }
324 if (n == ndevs)
325 return ENXIO;
326
327 f->f_devdata = bpart;
328
329 *file = path;
330
331 efi_block_booted = bpart;
332
333 return 0;
334 }
335
336 int
337 efi_block_close(struct open_file *f)
338 {
339 return 0;
340 }
341
342 int
343 efi_block_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize)
344 {
345 struct efi_block_part *bpart = devdata;
346 EFI_STATUS status;
347
348 if (rw != F_READ)
349 return EROFS;
350
351 switch (bpart->type) {
352 case EFI_BLOCK_PART_DISKLABEL:
353 if (bpart->bdev->bio->Media->BlockSize != bpart->disklabel.secsize) {
354 printf("%s: unsupported block size %d (expected %d)\n", __func__,
355 bpart->bdev->bio->Media->BlockSize, bpart->disklabel.secsize);
356 return EIO;
357 }
358 dblk += bpart->disklabel.part.p_offset;
359 break;
360 default:
361 return EINVAL;
362 }
363
364 status = uefi_call_wrapper(bpart->bdev->bio->ReadBlocks, 5, bpart->bdev->bio, bpart->bdev->media_id, dblk, size, buf);
365 if (EFI_ERROR(status))
366 return EIO;
367
368 *rsize = size;
369
370 return 0;
371 }
372