1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU Lesser General Public License as published by
5 * the Free Software Foundation; either version 2.1 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * Copyright (C) 2015 Kay Sievers <kay@vrfy.org>
23 /* magic string to find in the binary image */
24 static const char __attribute__((used)) magic[] = "#### LoaderInfo: stub " VERSION " ####";
26 static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE;
28 EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
29 EFI_LOADED_IMAGE *loaded_image;
31 CHAR16 *loaded_image_path;
34 BOOLEAN secure = FALSE;
41 UINTN addrs[ELEMENTSOF(sections)-1] = {};
42 UINTN offs[ELEMENTSOF(sections)-1] = {};
43 UINTN szs[ELEMENTSOF(sections)-1] = {};
44 CHAR8 *cmdline = NULL;
48 InitializeLib(image, sys_table);
50 err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image,
51 image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
53 Print(L"Error getting a LoadedImageProtocol handle: %r ", err);
54 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
58 root_dir = LibOpenRoot(loaded_image->DeviceHandle);
60 Print(L"Unable to open root directory: %r ", err);
61 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
62 return EFI_LOAD_ERROR;
65 loaded_image_path = DevicePathToStr(loaded_image->FilePath);
67 if (efivar_get_raw(&global_guid, L"SecureBoot", &b, &size) == EFI_SUCCESS) {
73 err = pefile_locate_sections(root_dir, loaded_image_path, sections, addrs, offs, szs);
75 Print(L"Unable to locate embedded .linux section: %r ", err);
76 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
81 cmdline = (CHAR8 *)(loaded_image->ImageBase + addrs[0]);
85 /* if we are not in secure boot mode, accept a custom command line and replace the built-in one */
86 if (!secure && loaded_image->LoadOptionsSize > 0) {
91 options = (CHAR16 *)loaded_image->LoadOptions;
92 cmdline_len = (loaded_image->LoadOptionsSize / sizeof(CHAR16)) * sizeof(CHAR8);
93 line = AllocatePool(cmdline_len);
94 for (i = 0; i < cmdline_len; i++)
99 err = linux_exec(image, cmdline, cmdline_len,
100 (UINTN)loaded_image->ImageBase + addrs[1],
101 (UINTN)loaded_image->ImageBase + addrs[2], szs[2]);
103 Print(L"Execution of embedded linux image failed: %r\n", err);
104 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);