Debian Codegraph @

src/boot/chid.h

raw
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 #pragma once
3
4 #include "efi.h"
5
6 #include "../fundamental/chid.h" /* IWYU pragma: export */
7
8 /* A .hwids PE section consists of a series of 'Device' structures. A 'Device' structure binds a CHID to some
9 * resource, for now only Devicetree blobs. Designed to be extensible to other types of resources, should the
10 * need arise. The series of 'Device' structures is followed by some space for strings that can be referenced
11 * by offset by the Device structures. */
12
13 enum {
14 DEVICE_TYPE_DEVICETREE = 0x1, /* A devicetree blob */
15 DEVICE_TYPE_UEFI_FW = 0x2, /* A firmware blob */
16
17 /* Maybe later additional types for:
18 * - CoCo Bring-Your-Own-Firmware
19 * - ACPI DSDT Overrides
20 * - … */
21 _DEVICE_TYPE_MAX,
22 };
23
24 #define DEVICE_SIZE_FROM_DESCRIPTOR(u) ((uint32_t) (u) & UINT32_C(0x0FFFFFFF))
25 #define DEVICE_TYPE_FROM_DESCRIPTOR(u) ((uint32_t) (u) >> 28)
26 #define DEVICE_MAKE_DESCRIPTOR(type, size) (((uint32_t) (size) | ((uint32_t) type << 28)))
27
28 #define DEVICE_DESCRIPTOR_DEVICETREE DEVICE_MAKE_DESCRIPTOR(DEVICE_TYPE_DEVICETREE, sizeof(Device))
29 #define DEVICE_DESCRIPTOR_UEFI_FW DEVICE_MAKE_DESCRIPTOR(DEVICE_TYPE_UEFI_FW, sizeof(Device))
30 #define DEVICE_DESCRIPTOR_EOL UINT32_C(0)
31
32 typedef struct Device {
33 uint32_t descriptor; /* The highest four bit encode the type of entry, the other 28 bit encode the
34 * size of the structure. Use the macros above to generate or take apart this
35 * field. */
36 EFI_GUID chid;
37 union {
38 struct {
39 /* These offsets are relative to the beginning of the .hwids PE section. */
40 uint32_t name_offset; /* nul-terminated string or 0 if not present */
41 uint32_t compatible_offset; /* nul-terminated string or 0 if not present */
42 } devicetree;
43 struct {
44 /* Offsets are relative to the beginning of the .hwids PE section.
45 * They are nul-terminated strings when present or 0 if not present */
46 uint32_t name_offset; /* name or identifier for the firmware blob */
47 uint32_t fwid_offset; /* identifier to match a specific uefi firmware blob */
48 } uefi_fw;
49
50 /* fields for other descriptor types… */
51 };
52 } _packed_ Device;
53
54 /* Validate some offset, since the structure is API and src/ukify/ukify.py encodes them directly */
55 assert_cc(offsetof(Device, descriptor) == 0);
56 assert_cc(offsetof(Device, chid) == 4);
57 assert_cc(offsetof(Device, devicetree.name_offset) == 20);
58 assert_cc(offsetof(Device, devicetree.compatible_offset) == 24);
59 assert_cc(offsetof(Device, uefi_fw.name_offset) == 20);
60 assert_cc(offsetof(Device, uefi_fw.fwid_offset) == 24);
61 assert_cc(sizeof(Device) == 28);
62
63 static inline const char* device_get_name(const void *base, const Device *device) {
64 size_t off = 0;
65 switch (DEVICE_TYPE_FROM_DESCRIPTOR(device->descriptor)) {
66 case DEVICE_TYPE_DEVICETREE:
67 off = device->devicetree.name_offset;
68 break;
69 case DEVICE_TYPE_UEFI_FW:
70 off = device->uefi_fw.name_offset;
71 break;
72 default:
73 return NULL;
74 }
75 return off == 0 ? NULL : (const char *) ((const uint8_t *) base + off);
76 }
77
78 static inline const char* device_get_compatible(const void *base, const Device *device) {
79 size_t off = 0;
80 switch (DEVICE_TYPE_FROM_DESCRIPTOR(device->descriptor)) {
81 case DEVICE_TYPE_DEVICETREE:
82 off = device->devicetree.compatible_offset;
83 break;
84 default:
85 return NULL;
86 }
87 return off == 0 ? NULL : (const char *) ((const uint8_t *) base + off);
88 }
89
90 static inline const char* device_get_fwid(const void *base, const Device *device) {
91 size_t off = 0;
92 switch (DEVICE_TYPE_FROM_DESCRIPTOR(device->descriptor)) {
93 case DEVICE_TYPE_UEFI_FW:
94 off = device->uefi_fw.fwid_offset;
95 break;
96 default:
97 return NULL;
98 }
99 return off == 0 ? NULL : (const char *) ((const uint8_t *) base + off);
100 }
101
102 EFI_STATUS chid_match(
103 const void *hwid_buffer,
104 size_t hwid_length,
105 uint32_t match_type,
106 const Device **ret_device);