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 are freely
12 * permitted provided that the above copyright notice and this
13 * paragraph and the following disclaimer are duplicated in all
16 * This software is provided "AS IS" and without any express or
17 * implied warranties, including, without limitation, the implied
18 * warranties of merchantability and fitness for a particular
22 #include <sys/cdefs.h>
23 __FBSDID("$FreeBSD$");
25 #include <sys/param.h>
26 #include <machine/elf.h>
27 #include <machine/stdarg.h>
31 #include <eficonsctl.h>
34 #include "boot_module.h"
38 static void efi_panic(EFI_STATUS s, const char *fmt, ...) __dead2 __printflike(2, 3);
40 const boot_module_t *boot_modules[] =
49 const UINTN num_boot_modules = nitems(boot_modules);
51 static EFI_GUID BlockIoProtocolGUID = BLOCK_IO_PROTOCOL;
52 static EFI_GUID DevicePathGUID = DEVICE_PATH_PROTOCOL;
53 static EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL;
54 static EFI_GUID ConsoleControlGUID = EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
56 static EFI_PHYSICAL_ADDRESS heap;
57 static UINTN heapsize;
60 * try_boot only returns if it fails to load the loader. If it succeeds
61 * it simply boots, otherwise it returns the status of last EFI call.
64 try_boot(const boot_module_t *mod, dev_info_t *dev, void *loaderbuf, size_t loadersize)
66 size_t bufsize, cmdsize;
69 EFI_HANDLE loaderhandle;
70 EFI_LOADED_IMAGE *loaded_image;
74 * Read in and parse the command line from /boot.config or /boot/config,
75 * if present. We'll pass it the next stage via a simple ASCII
76 * string. loader.efi has a hack for ASCII strings, so we'll use that to
77 * keep the size down here. We only try to read the alternate file if
78 * we get EFI_NOT_FOUND because all other errors mean that the boot_module
79 * had troubles with the filesystem. We could return early, but we'll let
80 * loading the actual kernel sort all that out. Since these files are
81 * optional, we don't report errors in trying to read them.
85 status = mod->load(PATH_DOTCONFIG, dev, &buf, &bufsize);
86 if (status == EFI_NOT_FOUND)
87 status = mod->load(PATH_CONFIG, dev, &buf, &bufsize);
88 if (status == EFI_SUCCESS) {
89 cmdsize = bufsize + 1;
90 cmd = malloc(cmdsize);
93 memcpy(cmd, buf, bufsize);
99 if ((status = BS->LoadImage(TRUE, IH, efi_devpath_last_node(dev->devpath),
100 loaderbuf, loadersize, &loaderhandle)) != EFI_SUCCESS) {
101 printf("Failed to load image provided by %s, size: %zu, (%lu)\n",
102 mod->name, loadersize, EFI_ERROR_CODE(status));
106 status = OpenProtocolByHandle(loaderhandle, &LoadedImageGUID,
107 (void **)&loaded_image);
108 if (status != EFI_SUCCESS) {
109 printf("Failed to query LoadedImage provided by %s (%lu)\n",
110 mod->name, EFI_ERROR_CODE(status));
115 printf(" command args: %s\n", cmd);
117 loaded_image->DeviceHandle = dev->devhandle;
118 loaded_image->LoadOptionsSize = cmdsize;
119 loaded_image->LoadOptions = cmd;
121 DPRINTF("Starting '%s' in 5 seconds...", PATH_LOADER_EFI);
133 if ((status = BS->StartImage(loaderhandle, NULL, NULL)) !=
135 printf("Failed to start image provided by %s (%lu)\n",
136 mod->name, EFI_ERROR_CODE(status));
137 loaded_image->LoadOptionsSize = 0;
138 loaded_image->LoadOptions = NULL;
146 if (loaderbuf != NULL)
153 efi_main(EFI_HANDLE Ximage, EFI_SYSTEM_TABLE *Xsystab)
156 EFI_LOADED_IMAGE *img;
157 EFI_DEVICE_PATH *imgpath;
159 EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl = NULL;
160 SIMPLE_TEXT_OUTPUT_INTERFACE *conout = NULL;
161 UINTN i, hsize, nhandles;
164 /* Basic initialization*/
167 BS = ST->BootServices;
168 RS = ST->RuntimeServices;
170 heapsize = 64 * 1024 * 1024;
171 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
172 EFI_SIZE_TO_PAGES(heapsize), &heap);
173 if (status != EFI_SUCCESS) {
174 ST->ConOut->OutputString(ST->ConOut,
176 L"Failed to allocate memory for heap.\r\n"));
177 BS->Exit(IH, status, 0, NULL);
180 setheap((void *)(uintptr_t)heap, (void *)(uintptr_t)(heap + heapsize));
182 /* Set up the console, so printf works. */
183 status = BS->LocateProtocol(&ConsoleControlGUID, NULL,
184 (VOID **)&ConsoleControl);
185 if (status == EFI_SUCCESS)
186 (void)ConsoleControl->SetMode(ConsoleControl,
187 EfiConsoleControlScreenText);
189 * Reset the console enable the cursor. Later we'll choose a better
190 * console size through GOP/UGA.
193 conout->Reset(conout, TRUE);
194 /* Explicitly set conout to mode 0, 80x25 */
195 conout->SetMode(conout, 0);
196 conout->EnableCursor(conout, TRUE);
197 conout->ClearScreen(conout);
199 printf("\n>> FreeBSD EFI boot block\n");
200 printf(" Loader path: %s\n\n", PATH_LOADER_EFI);
201 printf(" Initializing modules:");
202 for (i = 0; i < num_boot_modules; i++) {
203 printf(" %s", boot_modules[i]->name);
204 if (boot_modules[i]->init != NULL)
205 boot_modules[i]->init();
209 /* Fetch all the block I/O handles, we have to search through them later */
211 BS->LocateHandle(ByProtocol, &BlockIoProtocolGUID, NULL,
213 handles = malloc(hsize);
215 efi_panic(EFI_OUT_OF_RESOURCES, "Failed to allocate %d handles\n",
217 status = BS->LocateHandle(ByProtocol, &BlockIoProtocolGUID,
218 NULL, &hsize, handles);
219 if (status != EFI_SUCCESS)
220 efi_panic(status, "Failed to get device handles\n");
221 nhandles = hsize / sizeof(*handles);
223 /* Determine the devpath of our image so we can prefer it. */
224 status = OpenProtocolByHandle(IH, &LoadedImageGUID, (void **)&img);
226 if (status == EFI_SUCCESS) {
227 text = efi_devpath_name(img->FilePath);
229 printf(" Load Path: %S\n", text);
230 efi_setenv_freebsd_wcs("Boot1Path", text);
231 efi_free_devpath_name(text);
234 status = OpenProtocolByHandle(img->DeviceHandle,
235 &DevicePathGUID, (void **)&imgpath);
236 if (status != EFI_SUCCESS) {
237 DPRINTF("Failed to get image DevicePath (%lu)\n",
238 EFI_ERROR_CODE(status));
240 text = efi_devpath_name(imgpath);
242 printf(" Load Device: %S\n", text);
243 efi_setenv_freebsd_wcs("Boot1Dev", text);
244 efi_free_devpath_name(text);
249 choice_protocol(handles, nhandles, imgpath);
251 /* If we get here, we're out of luck... */
252 efi_panic(EFI_LOAD_ERROR, "No bootable partitions found!");
256 * add_device adds a device to the passed devinfo list.
259 add_device(dev_info_t **devinfop, dev_info_t *devinfo)
263 if (*devinfop == NULL) {
268 for (dev = *devinfop; dev->next != NULL; dev = dev->next)
275 efi_exit(EFI_STATUS s)
278 BS->FreePages(heap, EFI_SIZE_TO_PAGES(heapsize));
279 BS->Exit(IH, s, 0, NULL);
283 exit(int error __unused)
285 efi_exit(EFI_LOAD_ERROR);
289 * OK. We totally give up. Exit back to EFI with a sensible status so
290 * it can try the next option on the list.
293 efi_panic(EFI_STATUS s, const char *fmt, ...)
319 ST->ConOut->OutputString(ST->ConOut, buf);
323 ST->ConOut->OutputString(ST->ConOut, buf);