summaryrefslogtreecommitdiffstats
path: root/common/recipes-bsp/systemd-boot/systemd-boot/0002-sd-boot-Load-board-specific-boot-entries-from-RMC-da.patch
diff options
context:
space:
mode:
Diffstat (limited to 'common/recipes-bsp/systemd-boot/systemd-boot/0002-sd-boot-Load-board-specific-boot-entries-from-RMC-da.patch')
-rw-r--r--common/recipes-bsp/systemd-boot/systemd-boot/0002-sd-boot-Load-board-specific-boot-entries-from-RMC-da.patch263
1 files changed, 263 insertions, 0 deletions
diff --git a/common/recipes-bsp/systemd-boot/systemd-boot/0002-sd-boot-Load-board-specific-boot-entries-from-RMC-da.patch b/common/recipes-bsp/systemd-boot/systemd-boot/0002-sd-boot-Load-board-specific-boot-entries-from-RMC-da.patch
new file mode 100644
index 00000000..ddad940b
--- /dev/null
+++ b/common/recipes-bsp/systemd-boot/systemd-boot/0002-sd-boot-Load-board-specific-boot-entries-from-RMC-da.patch
@@ -0,0 +1,263 @@
1From f714cdc84b791d84099f7461c4f223677456720f Mon Sep 17 00:00:00 2001
2From: Jianxun Zhang <jianxun.zhang@linux.intel.com>
3Date: Wed, 1 Jun 2016 16:32:22 -0700
4Subject: [PATCH 2/3] sd-boot: Load board-specific boot entries from RMC
5 database
6
7RMC provides a centralized database file on ESP. The DB contains
8fingerprints and any file blobs associated to physical boards.
9Callers can fetch board-specific data with fingerprint info
10collected from board at runtime if there is any record matched
11board's fingerprint.
12
13To let bootloader know which file blob in RMC should be queried,
14a special config file BOOTENTRY.CONFIG is defined as:
15
16boot.conf
17install.conf
18
19Bootloader calls RMC APIs and other functions to perform these
20tasks before it shows boot menu to user:
21
22(1) Load RMC database file from ESP
23(2) Collect fingerprint data from board
24(3) Query BOOTENTRY.CONFIG from RMC DB with fingerprint
25(4) Parse BOOTENTRY.CONFIG to know names of boot entry files
26(5) Query boot entry files one by one from RMC DB, and add
27 them into sd-boot config data.
28
29The final effect is that bootloader will show board-specific
30boot entries in boot menu to user. User then can choose one
31of them to boot system with the selected configuration.
32
33If any of these steps fails, bootloader simply skips loading
34RMC configs or any entry file not successfully fetched from
35RMC DB. Once any entry is loaded successfully from RMC DB,
36bootloader skips loading any boot entries from ESP.
37
38Upstream-Status: Pending
39
40Signed-off-by: Jianxun Zhang <jianxun.zhang@linux.intel.com>
41---
42 src/boot/efi/boot.c | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++-
43 1 file changed, 158 insertions(+), 2 deletions(-)
44
45diff --git a/src/boot/efi/boot.c b/src/boot/efi/boot.c
46index 30c1ead..43b0793 100644
47--- a/src/boot/efi/boot.c
48+++ b/src/boot/efi/boot.c
49@@ -15,6 +15,8 @@
50
51 #include <efi.h>
52 #include <efilib.h>
53+#include <rmcl.h>
54+#include <rsmp.h>
55
56 #include "console.h"
57 #include "disk.h"
58@@ -33,6 +35,9 @@ static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-boot
59
60 static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE;
61
62+static CHAR8* rmc_db;
63+static rmc_fingerprint_t *rmc_fp;
64+
65 enum loader_type {
66 LOADER_UNDEFINED,
67 LOADER_EFI,
68@@ -1702,6 +1707,148 @@ static VOID config_free(Config *config) {
69 FreePool(config->entry_oneshot);
70 }
71
72+/* Derived from line_get_key_value(), we could consolidate two functions later */
73+static CHAR8 *get_line(CHAR8 *content, UINT64 *pos) {
74+ CHAR8 *line;
75+ UINT64 linelen;
76+
77+skip:
78+ line = content + *pos;
79+ if (*line == '\0')
80+ return NULL;
81+
82+ linelen = 0;
83+ while (line[linelen] && !strchra((CHAR8 *)"\n\r", line[linelen]))
84+ linelen++;
85+
86+ /* move pos to next line */
87+ *pos += linelen;
88+ if (content[*pos])
89+ (*pos)++;
90+
91+ /* empty line */
92+ if (linelen == 0)
93+ goto skip;
94+
95+ /* terminate line */
96+ line[linelen] = '\0';
97+
98+ /* remove leading whitespace */
99+ while (strchra((CHAR8 *)" \t", *line)) {
100+ line++;
101+ linelen--;
102+ }
103+
104+ /* remove trailing whitespace */
105+ while (linelen > 0 && strchra((CHAR8 *)" \t", line[linelen-1]))
106+ linelen--;
107+ line[linelen] = '\0';
108+
109+ if (*line == '#')
110+ goto skip;
111+
112+ return line;
113+}
114+
115+/* load rmc database file from ESP and try to get fingerprint. These
116+ * are essential information indicating we could query rmc data for
117+ * this board at least
118+ * return 0 if both database file and fingerprint can be obtained, otherwise
119+ * non-zero value is returned.
120+ *
121+ * Note: db and fp hold valid values only when this function returns 0.
122+ * Caller is responsible to free allocated memory pointed by *db and *fp when
123+ * this function returns 0.
124+ */
125+
126+static UINTN rmc_initialize(EFI_FILE *root_dir, CHAR8 **db, rmc_fingerprint_t **fp) {
127+ UINTN len;
128+ EFI_GUID smbios_guid = SMBIOS_TABLE_GUID;
129+ EFI_GUID smbios3_guid = SMBIOS3_TABLE_GUID;
130+ VOID *smbios_entry = NULL;
131+ UINT64 smbios_struct_addr = 0;
132+ UINT16 smbios_struct_len = 0;
133+ UINTN ret = 1;
134+
135+ if (!db || !fp)
136+ return ret;
137+
138+ *db = NULL;
139+ *fp = NULL;
140+
141+ /* load rmc database */
142+ len = file_read(root_dir, L"\\rmc.db", 0, 0, db);
143+
144+ if (len <= 0)
145+ goto done;
146+
147+ /* locate smbios entry table, try both 32 and 64 bit */
148+ if (LibGetSystemConfigurationTable(&smbios3_guid, &smbios_entry) != EFI_SUCCESS
149+ && LibGetSystemConfigurationTable(&smbios_guid, &smbios_entry) != EFI_SUCCESS)
150+ goto done;
151+
152+ /* call rsmp to get fp */
153+ if (rsmp_get_smbios_strcut(smbios_entry, &smbios_struct_addr, &smbios_struct_len))
154+ goto done;
155+
156+ *fp = AllocateZeroPool(sizeof(rmc_fingerprint_t));
157+
158+ if (rsmp_get_fingerprint_from_smbios_struct((BYTE *) smbios_struct_addr, *fp))
159+ goto done;
160+
161+ ret = 0;
162+done:
163+ if (ret) {
164+ FreePool(*db);
165+ FreePool(*fp);
166+ }
167+
168+ return ret;
169+}
170+
171+/* load RMC entries
172+ * return TRUE when at least one entry is loaded, otherwise, return FALSE
173+ */
174+static BOOLEAN config_load_rmc_entries(Config *config, EFI_HANDLE *device, CHAR16 *loaded_image_path, CHAR8 *db, rmc_fingerprint_t *fp) {
175+ CHAR8 *boot_entry = NULL;
176+ CHAR8 *boot_config = NULL;
177+ rmc_policy_file_t rp;
178+ CHAR8 *line;
179+ UINT64 pos = 0;
180+ BOOLEAN ret = FALSE;
181+
182+ if (!db || !fp)
183+ return ret;
184+
185+ /* query boot entry config file */
186+ if (query_policy_from_db(fp, db, RMC_POLICY_BLOB, "BOOTENTRY.CONFIG", &rp))
187+ return ret;
188+
189+ /* file blob read from rmc db is not necessarily null-terminated, and we
190+ * should keep mem where rmc db lives from change during parsing
191+ */
192+ boot_config = AllocatePool(rp.blob_len * sizeof(CHAR8) + 1);
193+ CopyMem(boot_config, rp.blob, rp.blob_len);
194+ boot_config[rp.blob_len] = '\0';
195+ /* parse boot entry config */
196+ while ((line = get_line(boot_config, &pos))) {
197+ if (query_policy_from_db(fp, db, RMC_POLICY_BLOB, (char *)line, &rp))
198+ continue;
199+ if (rp.blob_len > 0) {
200+ boot_entry = AllocatePool(rp.blob_len * sizeof(CHAR8) + 1);
201+ CopyMem(boot_entry, rp.blob, rp.blob_len);
202+ boot_entry[rp.blob_len] = '\0';
203+ config_entry_add_from_file(config, device,
204+ stra_to_str(line), boot_entry,
205+ loaded_image_path);
206+ /* tell caller success when a RMC entry is loaded */
207+ ret = TRUE;
208+ }
209+ }
210+
211+ return ret;
212+}
213+
214 EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
215 CHAR16 *s;
216 CHAR8 *b;
217@@ -1714,6 +1861,7 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
218 UINT64 init_usec;
219 BOOLEAN menu = FALSE;
220 CHAR16 uuid[37];
221+ BOOLEAN rmc_entry = FALSE;
222
223 InitializeLib(image, sys_table);
224 init_usec = time_usec();
225@@ -1745,6 +1893,8 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
226 return EFI_LOAD_ERROR;
227 }
228
229+ /* Initialize rmc before loading any config */
230+ rmc_initialize(root_dir, &rmc_db, &rmc_fp);
231
232 /* the filesystem path to this image, to prevent adding ourselves to the menu */
233 loaded_image_path = DevicePathToStr(loaded_image->FilePath);
234@@ -1753,11 +1903,15 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
235 ZeroMem(&config, sizeof(Config));
236 config_load_defaults(&config, root_dir);
237
238+ if (rmc_db && rmc_fp)
239+ rmc_entry = config_load_rmc_entries(&config, loaded_image->DeviceHandle, loaded_image_path, rmc_db, rmc_fp);
240+
241 /* scan /EFI/Linux/ directory */
242 config_entry_add_linux(&config, loaded_image, root_dir);
243
244- /* scan /loader/entries/\*.conf files */
245- config_load_entries(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path);
246+ /* scan /loader/entries/\*.conf files only when no RMC entry is loaded */
247+ if (rmc_entry == FALSE)
248+ config_load_entries(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path);
249
250 /* sort entries after version number */
251 config_sort_entries(&config);
252@@ -1851,6 +2005,8 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
253 out:
254 FreePool(loaded_image_path);
255 config_free(&config);
256+ FreePool(rmc_db);
257+ FreePool(rmc_fp);
258 uefi_call_wrapper(root_dir->Close, 1, root_dir);
259 uefi_call_wrapper(BS->CloseProtocol, 4, image, &LoadedImageProtocol, image, NULL);
260 return err;
261--
2622.7.4
263