2 * Copyright (c) 2019 Netflix, Inc
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <machine/elf.h>
31 #include <machine/stdarg.h>
35 #include <eficonsctl.h>
38 #include "boot_module.h"
44 static const uuid_t freebsd_ufs_uuid = GPT_ENT_TYPE_FREEBSD_UFS;
45 static char secbuf[4096];
46 static struct dsk dsk;
47 static dev_info_t *devices = NULL;
48 static dev_info_t *raw_device = NULL;
50 static EFI_GUID BlockIoProtocolGUID = BLOCK_IO_PROTOCOL;
51 static EFI_GUID DevicePathGUID = DEVICE_PATH_PROTOCOL;
54 * Shim routine for the gpt code to read in the gpt table. The
55 * devinfo is always going to be for the raw device.
58 drvread(struct dsk *dskp, void *buf, daddr_t lba, unsigned nblk)
62 dev_info_t *devinfo = (dev_info_t *)dskp->devinfo;
63 EFI_BLOCK_IO *dev = devinfo->dev;
65 lba = lba / (dev->Media->BlockSize / DEV_BSIZE);
66 size = nblk * DEV_BSIZE;
68 status = dev->ReadBlocks(dev, dev->Media->MediaId, lba, size, buf);
69 if (status != EFI_SUCCESS) {
70 DPRINTF("dskread: failed dev: %p, id: %u, lba: %ju, size: %d, "
71 "status: %lu\n", devinfo->dev,
72 dev->Media->MediaId, (uintmax_t)lba, size,
73 EFI_ERROR_CODE(status));
81 * Shim routine for the gpt code to write in the gpt table. The
82 * devinfo is always going to be for the raw device.
85 drvwrite(struct dsk *dskp, void *buf, daddr_t lba, unsigned nblk)
89 dev_info_t *devinfo = (dev_info_t *)dskp->devinfo;
90 EFI_BLOCK_IO *dev = devinfo->dev;
92 if (dev->Media->ReadOnly)
95 lba = lba / (dev->Media->BlockSize / DEV_BSIZE);
96 size = nblk * DEV_BSIZE;
98 status = dev->WriteBlocks(dev, dev->Media->MediaId, lba, size, buf);
99 if (status != EFI_SUCCESS) {
100 DPRINTF("dskread: failed dev: %p, id: %u, lba: %ju, size: %d, "
101 "status: %lu\n", devinfo->dev,
102 dev->Media->MediaId, (uintmax_t)lba, size,
103 EFI_ERROR_CODE(status));
111 * Return the number of LBAs the drive has.
114 drvsize(struct dsk *dskp)
116 dev_info_t *devinfo = (dev_info_t *)dskp->devinfo;
117 EFI_BLOCK_IO *dev = devinfo->dev;
119 return (dev->Media->LastBlock + 1);
123 partition_number(EFI_DEVICE_PATH *devpath)
126 HARDDRIVE_DEVICE_PATH *hd;
128 md = efi_devpath_last_node(devpath);
131 if (DevicePathSubType(md) != MEDIA_HARDDRIVE_DP)
133 hd = (HARDDRIVE_DEVICE_PATH *)md;
134 return (hd->PartitionNumber);
138 * Find the raw partition for the imgpath and save it
141 probe_handle(EFI_HANDLE h, EFI_DEVICE_PATH *imgpath)
145 EFI_DEVICE_PATH *devpath, *trimmed = NULL;
148 /* Figure out if we're dealing with an actual partition. */
149 status = OpenProtocolByHandle(h, &DevicePathGUID, (void **)&devpath);
150 if (status != EFI_SUCCESS)
154 CHAR16 *text = efi_devpath_name(devpath);
155 DPRINTF("probing: %S ", text);
156 efi_free_devpath_name(text);
160 * The RAW device is the same as the imgpath with the last
161 * MEDIA_DEVICE bit trimmed off. imgpath will end with the
162 * MEDIA_DEVICE for the ESP we booted off of.
164 if (!efi_devpath_same_disk(imgpath, devpath)) {
165 trimmed = efi_devpath_trim(imgpath);
166 if (!efi_devpath_match(trimmed, devpath)) {
168 DPRINTF("Not the same disk\n");
172 status = OpenProtocolByHandle(h, &BlockIoProtocolGUID, (void **)&blkio);
173 if (status != EFI_SUCCESS) {
174 DPRINTF("Can't get the block I/O protocol block\n");
177 devinfo = malloc(sizeof(*devinfo));
178 if (devinfo == NULL) {
179 DPRINTF("Failed to allocate devinfo\n");
182 devinfo->dev = blkio;
183 devinfo->devpath = devpath;
184 devinfo->devhandle = h;
185 devinfo->preferred = 1;
186 devinfo->next = NULL;
187 devinfo->devdata = NULL;
188 if (trimmed == NULL) {
189 DPRINTF("Found partition %d\n", partition_number(devpath));
190 add_device(&devices, devinfo);
193 DPRINTF("Found raw device\n");
195 printf(BOOTPROG": Found two raw devices, inconceivable?\n");
198 raw_device = devinfo;
203 probe_handles(EFI_HANDLE *handles, UINTN nhandles, EFI_DEVICE_PATH *imgpath)
207 for (i = 0; i < nhandles; i++)
208 probe_handle(handles[i], imgpath);
212 find_partition(int part)
218 for (dev = devices; dev != NULL; dev = dev->next)
219 if (partition_number(dev->devpath) == part)
225 choice_protocol(EFI_HANDLE *handles, UINTN nhandles, EFI_DEVICE_PATH *imgpath)
227 const boot_module_t *mod = &ufs_module;
232 const char *fn = PATH_LOADER_EFI;
235 * Probe the provided handles to find the partitions that
236 * are on the same drive.
238 probe_handles(handles, nhandles, imgpath);
239 dsk.devinfo = raw_device;
240 if (dsk.devinfo == NULL) {
241 printf(BOOTPROG": unable to find raw disk to read gpt\n");
246 * Read in the GPT table, and then find the right partition.
247 * gptread, gptfind and gptfaileboot are shared with the
248 * BIOS version of the gptboot program.
250 if (gptread(&dsk, secbuf) == -1) {
251 printf(BOOTPROG ": unable to load GPT\n");
255 // real gptboot can parse a command line before trying this loop.
256 // But since we don't parse anything at all, hard wire the partition
257 // to be -1 (meaning look for the next one).
259 while (gptfind(&freebsd_ufs_uuid, &dsk, -1) != -1) {
261 bootdev = find_partition(dsk.part);
262 if (bootdev == NULL) {
263 printf(BOOTPROG": Can't find partition %d\n",
267 if (mod->load(fn, bootdev, &loaderbuf, &loadersize) !=
269 printf(BOOTPROG": Can't load %s from partition %d\n",
273 try_boot(mod, bootdev, loaderbuf, loadersize);
278 printf("%s: no UFS partition was found\n", BOOTPROG);