| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "device-path-util.h" |
| 4 |
#include "efi-efivars.h" |
| 5 |
#include "export-vars.h" |
| 6 |
#include "hii.h" |
| 7 |
#include "measure.h" |
| 8 |
#include "part-discovery.h" |
| 9 |
#include "url-discovery.h" |
| 10 |
#include "util.h" |
| 11 |
|
| 12 |
void export_common_variables(EFI_LOADED_IMAGE_PROTOCOL *loaded_image) { |
| 13 |
assert(loaded_image); |
| 14 |
|
| 15 |
/* Export the device path this image is started from, if it's not set yet */ |
| 16 |
if (loaded_image->DeviceHandle) { |
| 17 |
if (efivar_get_raw(MAKE_GUID_PTR(LOADER), u"LoaderDevicePartUUID", /* ret_data= */ NULL, /* ret_size= */ NULL) != EFI_SUCCESS) { |
| 18 |
_cleanup_free_ char16_t *uuid = disk_get_part_uuid(loaded_image->DeviceHandle); |
| 19 |
if (uuid) |
| 20 |
efivar_set_str16(MAKE_GUID_PTR(LOADER), u"LoaderDevicePartUUID", uuid, 0); |
| 21 |
} |
| 22 |
|
| 23 |
if (efivar_get_raw(MAKE_GUID_PTR(LOADER), u"LoaderDeviceURL", /* ret_data= */ NULL, /* ret_size= */ NULL) != EFI_SUCCESS) { |
| 24 |
_cleanup_free_ char16_t *url = disk_get_url(loaded_image->DeviceHandle); |
| 25 |
if (url) |
| 26 |
efivar_set_str16(MAKE_GUID_PTR(LOADER), u"LoaderDeviceURL", url, 0); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
/* If LoaderImageIdentifier is not set, assume the image with this stub was loaded directly from the |
| 31 |
* UEFI firmware without any boot loader, and hence set the LoaderImageIdentifier ourselves. Note |
| 32 |
* that some boot chain loaders neither set LoaderImageIdentifier nor make FilePath available to us, |
| 33 |
* in which case there's simple nothing to set for us. (The UEFI spec doesn't really say who's wrong |
| 34 |
* here, i.e. whether FilePath may be NULL or not, hence handle this gracefully and check if FilePath |
| 35 |
* is non-NULL explicitly.) */ |
| 36 |
if (loaded_image->FilePath && |
| 37 |
efivar_get_raw(MAKE_GUID_PTR(LOADER), u"LoaderImageIdentifier", NULL, NULL) != EFI_SUCCESS) { |
| 38 |
_cleanup_free_ char16_t *s = NULL; |
| 39 |
if (device_path_to_str(loaded_image->FilePath, &s) == EFI_SUCCESS) |
| 40 |
efivar_set_str16(MAKE_GUID_PTR(LOADER), u"LoaderImageIdentifier", s, 0); |
| 41 |
} |
| 42 |
|
| 43 |
/* if LoaderFirmwareInfo is not set, let's set it */ |
| 44 |
if (efivar_get_raw(MAKE_GUID_PTR(LOADER), u"LoaderFirmwareInfo", NULL, NULL) != EFI_SUCCESS) { |
| 45 |
_cleanup_free_ char16_t *s = NULL; |
| 46 |
s = xasprintf("%ls %u.%02u", ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff); |
| 47 |
efivar_set_str16(MAKE_GUID_PTR(LOADER), u"LoaderFirmwareInfo", s, 0); |
| 48 |
} |
| 49 |
|
| 50 |
/* ditto for LoaderFirmwareType */ |
| 51 |
if (efivar_get_raw(MAKE_GUID_PTR(LOADER), u"LoaderFirmwareType", NULL, NULL) != EFI_SUCCESS) { |
| 52 |
_cleanup_free_ char16_t *s = NULL; |
| 53 |
s = xasprintf("UEFI %u.%02u", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff); |
| 54 |
efivar_set_str16(MAKE_GUID_PTR(LOADER), u"LoaderFirmwareType", s, 0); |
| 55 |
} |
| 56 |
|
| 57 |
/* ditto for LoaderTpm2ActivePcrBanks */ |
| 58 |
if (efivar_get_raw(MAKE_GUID_PTR(LOADER), u"LoaderTpm2ActivePcrBanks", NULL, NULL) != EFI_SUCCESS) { |
| 59 |
uint32_t active_pcr_banks = tpm_get_active_pcr_banks(); |
| 60 |
_cleanup_free_ char16_t *s = NULL; |
| 61 |
s = xasprintf("0x%08x", active_pcr_banks); |
| 62 |
efivar_set_str16(MAKE_GUID_PTR(LOADER), u"LoaderTpm2ActivePcrBanks", s, 0); |
| 63 |
} |
| 64 |
|
| 65 |
/* Report the firmware's currently-active HII keyboard layout (as an RFC 4646 language tag, e.g. |
| 66 |
* "de-DE"), so the OS can pick a matching console keymap. Best-effort: many firmwares do not |
| 67 |
* implement the HII database protocol or expose no keyboard layout. */ |
| 68 |
if (efivar_get_raw(MAKE_GUID_PTR(LOADER), u"LoaderKeyboardLayout", /* ret_data= */ NULL, /* ret_size= */ NULL) != EFI_SUCCESS) { |
| 69 |
_cleanup_free_ char16_t *lang = hii_query_keyboard_layout_language(); |
| 70 |
if (lang) |
| 71 |
efivar_set_str16(MAKE_GUID_PTR(LOADER), u"LoaderKeyboardLayout", lang, /* flags= */ 0); |
| 72 |
} |
| 73 |
} |