chiark / gitweb /
boot: efi - split graphics and splash handling
[elogind.git] / src / boot / efi / stub.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
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.
7  *
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.
12  *
13  * Copyright (C) 2015 Kay Sievers <kay@vrfy.org>
14  */
15
16 #include <efi.h>
17 #include <efilib.h>
18
19 #include "util.h"
20 #include "pefile.h"
21 #include "graphics.h"
22 #include "splash.h"
23 #include "linux.h"
24
25 /* magic string to find in the binary image */
26 static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-stub " VERSION " ####";
27
28 static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE;
29
30 EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
31         EFI_LOADED_IMAGE *loaded_image;
32         EFI_FILE *root_dir;
33         CHAR16 *loaded_image_path;
34         CHAR8 *b;
35         UINTN size;
36         BOOLEAN secure = FALSE;
37         CHAR8 *sections[] = {
38                 (UINT8 *)".cmdline",
39                 (UINT8 *)".linux",
40                 (UINT8 *)".initrd",
41                 (UINT8 *)".splash",
42                 NULL
43         };
44         UINTN addrs[ELEMENTSOF(sections)-1] = {};
45         UINTN offs[ELEMENTSOF(sections)-1] = {};
46         UINTN szs[ELEMENTSOF(sections)-1] = {};
47         CHAR8 *cmdline = NULL;
48         UINTN cmdline_len;
49         EFI_STATUS err;
50
51         InitializeLib(image, sys_table);
52
53         err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image,
54                                 image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
55         if (EFI_ERROR(err)) {
56                 Print(L"Error getting a LoadedImageProtocol handle: %r ", err);
57                 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
58                 return err;
59         }
60
61         root_dir = LibOpenRoot(loaded_image->DeviceHandle);
62         if (!root_dir) {
63                 Print(L"Unable to open root directory: %r ", err);
64                 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
65                 return EFI_LOAD_ERROR;
66         }
67
68         loaded_image_path = DevicePathToStr(loaded_image->FilePath);
69
70         if (efivar_get_raw(&global_guid, L"SecureBoot", &b, &size) == EFI_SUCCESS) {
71                 if (*b > 0)
72                         secure = TRUE;
73                 FreePool(b);
74         }
75
76         err = pefile_locate_sections(root_dir, loaded_image_path, sections, addrs, offs, szs);
77         if (EFI_ERROR(err)) {
78                 Print(L"Unable to locate embedded .linux section: %r ", err);
79                 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
80                 return err;
81         }
82
83         if (szs[0] > 0)
84                 cmdline = (CHAR8 *)(loaded_image->ImageBase + addrs[0]);
85
86         cmdline_len = szs[0];
87
88         /* if we are not in secure boot mode, accept a custom command line and replace the built-in one */
89         if (!secure && loaded_image->LoadOptionsSize > 0) {
90                 CHAR16 *options;
91                 CHAR8 *line;
92                 UINTN i;
93
94                 options = (CHAR16 *)loaded_image->LoadOptions;
95                 cmdline_len = (loaded_image->LoadOptionsSize / sizeof(CHAR16)) * sizeof(CHAR8);
96                 line = AllocatePool(cmdline_len);
97                 for (i = 0; i < cmdline_len; i++)
98                         line[i] = options[i];
99                 cmdline = line;
100         }
101
102         if (szs[3] > 0)
103                 graphics_splash((UINT8 *)((UINTN)loaded_image->ImageBase + addrs[3]), szs[3], NULL);
104
105         err = linux_exec(image, cmdline, cmdline_len,
106                          (UINTN)loaded_image->ImageBase + addrs[1],
107                          (UINTN)loaded_image->ImageBase + addrs[2], szs[2]);
108
109         graphics_mode(FALSE);
110         Print(L"Execution of embedded linux image failed: %r\n", err);
111         uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
112         return err;
113 }