1 /* SPDX-License-Identifier: LGPL-2.1+ */
14 /* magic string to find in the binary image */
15 static const char __attribute__((used
)) magic
[] = "#### LoaderInfo: systemd-stub " GIT_VERSION
" ####";
17 static const EFI_GUID global_guid
= EFI_GLOBAL_VARIABLE
;
19 EFI_STATUS
efi_main(EFI_HANDLE image
, EFI_SYSTEM_TABLE
*sys_table
) {
20 EFI_LOADED_IMAGE
*loaded_image
;
21 _cleanup_freepool_ CHAR8
*b
= NULL
;
23 BOOLEAN secure
= FALSE
;
31 UINTN addrs
[ELEMENTSOF(sections
)-1] = {};
32 UINTN offs
[ELEMENTSOF(sections
)-1] = {};
33 UINTN szs
[ELEMENTSOF(sections
)-1] = {};
34 CHAR8
*cmdline
= NULL
;
39 InitializeLib(image
, sys_table
);
41 err
= uefi_call_wrapper(BS
->OpenProtocol
, 6, image
, &LoadedImageProtocol
, (VOID
**)&loaded_image
,
42 image
, NULL
, EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
44 Print(L
"Error getting a LoadedImageProtocol handle: %r ", err
);
45 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
49 if (efivar_get_raw(&global_guid
, L
"SecureBoot", &b
, &size
) == EFI_SUCCESS
)
53 err
= pe_memory_locate_sections(loaded_image
->ImageBase
, sections
, addrs
, offs
, szs
);
55 Print(L
"Unable to locate embedded .linux section: %r ", err
);
56 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);
61 cmdline
= (CHAR8
*)(loaded_image
->ImageBase
+ addrs
[0]);
65 /* if we are not in secure boot mode, accept a custom command line and replace the built-in one */
66 if (!secure
&& loaded_image
->LoadOptionsSize
> 0 && *(CHAR16
*)loaded_image
->LoadOptions
> 0x1F) {
71 options
= (CHAR16
*)loaded_image
->LoadOptions
;
72 cmdline_len
= (loaded_image
->LoadOptionsSize
/ sizeof(CHAR16
)) * sizeof(CHAR8
);
73 line
= AllocatePool(cmdline_len
);
74 for (i
= 0; i
< cmdline_len
; i
++)
79 /* Try to log any options to the TPM, especially manually edited options */
80 err
= tpm_log_event(SD_TPM_PCR
,
81 (EFI_PHYSICAL_ADDRESS
) (UINTN
) loaded_image
->LoadOptions
,
82 loaded_image
->LoadOptionsSize
, loaded_image
->LoadOptions
);
84 Print(L
"Unable to add image options measurement: %r", err
);
85 uefi_call_wrapper(BS
->Stall
, 1, 200 * 1000);
90 /* export the device path this image is started from */
91 if (disk_get_part_uuid(loaded_image
->DeviceHandle
, uuid
) == EFI_SUCCESS
)
92 efivar_set(L
"LoaderDevicePartUUID", uuid
, FALSE
);
94 /* if LoaderImageIdentifier is not set, assume the image with this stub was loaded directly from UEFI */
95 if (efivar_get_raw(&global_guid
, L
"LoaderImageIdentifier", &b
, &size
) != EFI_SUCCESS
) {
96 _cleanup_freepool_ CHAR16
*s
;
98 s
= DevicePathToStr(loaded_image
->FilePath
);
99 efivar_set(L
"LoaderImageIdentifier", s
, FALSE
);
102 /* if LoaderFirmwareInfo is not set, let's set it */
103 if (efivar_get_raw(&global_guid
, L
"LoaderFirmwareInfo", &b
, &size
) != EFI_SUCCESS
) {
104 _cleanup_freepool_ CHAR16
*s
;
106 s
= PoolPrint(L
"%s %d.%02d", ST
->FirmwareVendor
, ST
->FirmwareRevision
>> 16, ST
->FirmwareRevision
& 0xffff);
107 efivar_set(L
"LoaderFirmwareInfo", s
, FALSE
);
110 /* ditto for LoaderFirmwareType */
111 if (efivar_get_raw(&global_guid
, L
"LoaderFirmwareType", &b
, &size
) != EFI_SUCCESS
) {
112 _cleanup_freepool_ CHAR16
*s
;
114 s
= PoolPrint(L
"UEFI %d.%02d", ST
->Hdr
.Revision
>> 16, ST
->Hdr
.Revision
& 0xffff);
115 efivar_set(L
"LoaderFirmwareType", s
, FALSE
);
119 if (efivar_get_raw(&global_guid
, L
"StubInfo", &b
, &size
) != EFI_SUCCESS
)
120 efivar_set(L
"StubInfo", L
"systemd-stub " GIT_VERSION
, FALSE
);
123 graphics_splash((UINT8
*)((UINTN
)loaded_image
->ImageBase
+ addrs
[3]), szs
[3], NULL
);
125 err
= linux_exec(image
, cmdline
, cmdline_len
,
126 (UINTN
)loaded_image
->ImageBase
+ addrs
[1],
127 (UINTN
)loaded_image
->ImageBase
+ addrs
[2], szs
[2]);
129 graphics_mode(FALSE
);
130 Print(L
"Execution of embedded linux image failed: %r\n", err
);
131 uefi_call_wrapper(BS
->Stall
, 1, 3 * 1000 * 1000);