| 1 |
/* SPDX-License-Identifier: BSD-3-Clause */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Based on Nikita Travkin's dtbloader implementation. |
| 5 |
* Copyright (c) 2024 Nikita Travkin <[email protected]> |
| 6 |
* |
| 7 |
* https://github.com/TravMurav/dtbloader/blob/main/src/chid.c |
| 8 |
*/ |
| 9 |
|
| 10 |
/* |
| 11 |
* Based on Linaro dtbloader implementation. |
| 12 |
* Copyright (c) 2019, Linaro. All rights reserved. |
| 13 |
* |
| 14 |
* https://github.com/aarch64-laptops/edk2/blob/dtbloader-app/EmbeddedPkg/Application/ConfigTableLoader/CHID.c |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "chid.h" |
| 18 |
#include "edid.h" |
| 19 |
#if SD_BOOT |
| 20 |
#include "efi-log.h" |
| 21 |
#endif |
| 22 |
#include "efi-string.h" |
| 23 |
#include "smbios.h" |
| 24 |
#include "util.h" |
| 25 |
|
| 26 |
/* Validate the descriptor macros a bit that they match our expectations */ |
| 27 |
assert_cc(DEVICE_DESCRIPTOR_DEVICETREE == UINT32_C(0x1000001C)); |
| 28 |
assert_cc(DEVICE_DESCRIPTOR_UEFI_FW == UINT32_C(0x2000001C)); |
| 29 |
assert_cc(DEVICE_SIZE_FROM_DESCRIPTOR(DEVICE_DESCRIPTOR_DEVICETREE) == sizeof(Device)); |
| 30 |
assert_cc(DEVICE_TYPE_FROM_DESCRIPTOR(DEVICE_DESCRIPTOR_DEVICETREE) == DEVICE_TYPE_DEVICETREE); |
| 31 |
assert_cc(DEVICE_SIZE_FROM_DESCRIPTOR(DEVICE_DESCRIPTOR_UEFI_FW) == sizeof(Device)); |
| 32 |
assert_cc(DEVICE_TYPE_FROM_DESCRIPTOR(DEVICE_DESCRIPTOR_UEFI_FW) == DEVICE_TYPE_UEFI_FW); |
| 33 |
|
| 34 |
/** |
| 35 |
* smbios_to_hashable_string() - Convert ascii smbios string to stripped char16_t. |
| 36 |
*/ |
| 37 |
static char16_t *smbios_to_hashable_string(const char *str) { |
| 38 |
if (!str) |
| 39 |
/* User of this function is expected to free the result. */ |
| 40 |
return xnew0(char16_t, 1); |
| 41 |
|
| 42 |
/* |
| 43 |
* We need to strip leading and trailing spaces, leading zeroes. |
| 44 |
* See fwupd/libfwupdplugin/fu-hwids-smbios.c |
| 45 |
*/ |
| 46 |
while (*str == ' ') |
| 47 |
str++; |
| 48 |
|
| 49 |
while (*str == '0') |
| 50 |
str++; |
| 51 |
|
| 52 |
size_t len = strlen8(str); |
| 53 |
|
| 54 |
while (len > 0 && str[len - 1] == ' ') |
| 55 |
len--; |
| 56 |
|
| 57 |
return xstrn8_to_16(str, len); |
| 58 |
} |
| 59 |
|
| 60 |
/* This has to be in a struct due to _cleanup_ in populate_board_chids */ |
| 61 |
typedef struct SmbiosInfo { |
| 62 |
char16_t *smbios_fields[_CHID_SMBIOS_FIELDS_MAX]; |
| 63 |
} SmbiosInfo; |
| 64 |
|
| 65 |
static void smbios_info_populate(SmbiosInfo *ret_info) { |
| 66 |
assert(ret_info); |
| 67 |
|
| 68 |
RawSmbiosInfo raw; |
| 69 |
smbios_raw_info_get_cached(&raw); |
| 70 |
|
| 71 |
ret_info->smbios_fields[CHID_SMBIOS_MANUFACTURER] = smbios_to_hashable_string(raw.manufacturer); |
| 72 |
ret_info->smbios_fields[CHID_SMBIOS_PRODUCT_NAME] = smbios_to_hashable_string(raw.product_name); |
| 73 |
ret_info->smbios_fields[CHID_SMBIOS_PRODUCT_SKU] = smbios_to_hashable_string(raw.product_sku); |
| 74 |
ret_info->smbios_fields[CHID_SMBIOS_FAMILY] = smbios_to_hashable_string(raw.family); |
| 75 |
ret_info->smbios_fields[CHID_SMBIOS_BASEBOARD_PRODUCT] = smbios_to_hashable_string(raw.baseboard_product); |
| 76 |
ret_info->smbios_fields[CHID_SMBIOS_BASEBOARD_MANUFACTURER] = smbios_to_hashable_string(raw.baseboard_manufacturer); |
| 77 |
|
| 78 |
edid_get_discovered_panel_id(&ret_info->smbios_fields[CHID_EDID_PANEL]); |
| 79 |
} |
| 80 |
|
| 81 |
static void smbios_info_done(SmbiosInfo *info) { |
| 82 |
FOREACH_ELEMENT(i, info->smbios_fields) |
| 83 |
free(*i); |
| 84 |
} |
| 85 |
|
| 86 |
static EFI_STATUS populate_board_chids(EFI_GUID ret_chids[static CHID_TYPES_MAX]) { |
| 87 |
_cleanup_(smbios_info_done) SmbiosInfo info = {}; |
| 88 |
|
| 89 |
if (!ret_chids) |
| 90 |
return EFI_INVALID_PARAMETER; |
| 91 |
|
| 92 |
smbios_info_populate(&info); |
| 93 |
chid_calculate((const char16_t *const *) info.smbios_fields, ret_chids); |
| 94 |
|
| 95 |
return EFI_SUCCESS; |
| 96 |
} |
| 97 |
|
| 98 |
EFI_STATUS chid_match(const void *hwid_buffer, size_t hwid_length, uint32_t match_type, const Device **ret_device) { |
| 99 |
EFI_STATUS status; |
| 100 |
|
| 101 |
assert(ret_device); |
| 102 |
|
| 103 |
if ((uintptr_t) hwid_buffer % alignof(Device) != 0) |
| 104 |
return EFI_INVALID_PARAMETER; |
| 105 |
|
| 106 |
const Device *devices = ASSERT_PTR(hwid_buffer); |
| 107 |
|
| 108 |
EFI_GUID chids[CHID_TYPES_MAX] = {}; |
| 109 |
static const size_t priority[] = { EXTRA_CHID_BASE + 2, EXTRA_CHID_BASE + 1, EXTRA_CHID_BASE + 0, |
| 110 |
3, 6, 8, 10, 4, 5, 7, 9, 11 }; /* From most to least specific. */ |
| 111 |
|
| 112 |
status = populate_board_chids(chids); |
| 113 |
if (EFI_STATUS_IS_ERROR(status)) |
| 114 |
#if SD_BOOT |
| 115 |
return log_error_status(status, "Failed to populate board CHIDs: %m"); |
| 116 |
#else |
| 117 |
return status; |
| 118 |
#endif |
| 119 |
|
| 120 |
size_t n_devices = 0; |
| 121 |
|
| 122 |
/* Count devices and check validity */ |
| 123 |
for (; (n_devices + 1) * sizeof(*devices) < hwid_length;) { |
| 124 |
|
| 125 |
if (devices[n_devices].descriptor == DEVICE_DESCRIPTOR_EOL) |
| 126 |
break; |
| 127 |
if (!IN_SET(DEVICE_TYPE_FROM_DESCRIPTOR(devices[n_devices].descriptor), |
| 128 |
DEVICE_TYPE_UEFI_FW, DEVICE_TYPE_DEVICETREE)) |
| 129 |
return EFI_UNSUPPORTED; |
| 130 |
n_devices++; |
| 131 |
} |
| 132 |
|
| 133 |
if (n_devices == 0) |
| 134 |
return EFI_NOT_FOUND; |
| 135 |
|
| 136 |
FOREACH_ELEMENT(i, priority) |
| 137 |
FOREACH_ARRAY(dev, devices, n_devices) { |
| 138 |
/* Can't take a pointer to a packed struct member, so copy to a local variable */ |
| 139 |
EFI_GUID chid = dev->chid; |
| 140 |
if (DEVICE_TYPE_FROM_DESCRIPTOR(dev->descriptor) != match_type) |
| 141 |
continue; |
| 142 |
if (efi_guid_equal(&chids[*i], &chid)) { |
| 143 |
*ret_device = dev; |
| 144 |
return EFI_SUCCESS; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
return EFI_NOT_FOUND; |
| 149 |
} |