2 * Copyright (c) 1998 Robert Nordier
4 * Copyright (c) 2001 Robert Drehmel
6 * Copyright (c) 2014 Nathan Whitehorn
8 * Copyright (c) 2015 Eric McCorkle
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #include <sys/disk/bsd.h>
42 #include "boot_module.h"
44 #define BSD_LABEL_BUFFER 8192
45 #define BSD_LABEL_OFFSET DEV_BSIZE
47 static dev_info_t *devinfo;
48 static dev_info_t *devices;
51 dskread(void *buf, uint64_t lba, int nblk)
56 lba += devinfo->partoff;
57 lba = lba / (devinfo->dev->Media->BlockSize / DEV_BSIZE);
58 size = nblk * DEV_BSIZE;
60 status = devinfo->dev->ReadBlocks(devinfo->dev,
61 devinfo->dev->Media->MediaId, lba, size, buf);
63 if (status != EFI_SUCCESS) {
64 DPRINTF("dskread: failed dev: %p, id: %u, lba: %ju, size: %d, "
65 "status: %lu\n", devinfo->dev,
66 devinfo->dev->Media->MediaId, (uintmax_t)lba, size,
67 EFI_ERROR_CODE(status));
76 static struct dmadat __dmadat;
79 init_dev(dev_info_t* dev)
81 char buffer[BSD_LABEL_BUFFER];
90 * First try offset 0. This is the typical GPT case where we have no
91 * further partitioning (as well as the degenerate MBR case where
92 * the bsdlabel has a 0 offset).
95 ok = fsread(0, NULL, 0);
100 * Next, we look for a bsdlabel. This is technically located in sector
101 * 1. For 4k sectors, this offset is 4096, for 512b sectors it's
102 * 512. However, we have to fall back to 512 here because we create
103 * images that assume 512 byte blocks, but these can be put on devices
104 * who have 4k (or other) block sizes. If there's a crazy block size, we
105 * skip the 'at one sector' and go stright to checking at 512 bytes.
106 * There are other offsets that are historic, but we don't probe those
107 * since they were never used for MBR disks on FreeBSD on systems that
108 * could boot UEFI. UEFI is little endian only, as are BSD labels. We
109 * will retry fsread(0) only if there's a label found with a non-zero
112 if (dskread(buffer, 0, BSD_LABEL_BUFFER / DEV_BSIZE) != 0)
115 bs = devinfo->dev->Media->BlockSize;
116 if (bs != 0 && bs <= BSD_LABEL_BUFFER / 2)
117 dl = (struct disklabel *)&buffer[bs];
118 if (dl == NULL || dl->d_magic != BSD_MAGIC || dl->d_magic2 != BSD_MAGIC)
119 dl = (struct disklabel *)&buffer[BSD_LABEL_OFFSET];
120 if (dl->d_magic != BSD_MAGIC || dl->d_magic2 != BSD_MAGIC ||
121 dl->d_partitions[0].p_offset == 0)
123 devinfo->partoff = dl->d_partitions[0].p_offset;
124 return (fsread(0, NULL, 0));
128 probe(dev_info_t* dev)
131 if (init_dev(dev) < 0)
132 return (EFI_UNSUPPORTED);
134 add_device(&devices, dev);
136 return (EFI_SUCCESS);
140 load(const char *filepath, dev_info_t *dev, void **bufp, size_t *bufsize)
149 CHAR16 *text = efi_devpath_name(dev->devpath);
150 DPRINTF("UFS Loading '%s' from %S\n", filepath, text);
151 efi_free_devpath_name(text);
154 if (init_dev(dev) < 0) {
155 DPRINTF("Failed to init device\n");
156 return (EFI_UNSUPPORTED);
159 if ((ino = lookup(filepath)) == 0) {
160 DPRINTF("Failed to lookup '%s' (file not found?)\n", filepath);
161 return (EFI_NOT_FOUND);
164 if (fsread_size(ino, NULL, 0, &size) < 0 || size <= 0) {
165 printf("Failed to read size of '%s' ino: %d\n", filepath, ino);
166 return (EFI_INVALID_PARAMETER);
171 printf("Failed to allocate read buffer %zu for '%s'\n",
173 return (EFI_OUT_OF_RESOURCES);
176 read = fsread(ino, buf, size);
177 if ((size_t)read != size) {
178 printf("Failed to read '%s' (%zd != %zu)\n", filepath, read,
181 return (EFI_INVALID_PARAMETER);
184 DPRINTF("Load complete\n");
189 return (EFI_SUCCESS);
198 for (dev = devices, i = 0; dev != NULL; dev = dev->next, i++)
201 printf("%s found ", ufs_module.name);
204 printf("no partitions\n");
207 printf("%d partition\n", i);
210 printf("%d partitions\n", i);
221 const boot_module_t ufs_module =