summaryrefslogtreecommitdiffstats
path: root/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 'recipes-bsp/systemd-boot/systemd-boot/0002-sd-boot-Load-board-specific-boot-entries-from-RMC-da.patch')
-rw-r--r--recipes-bsp/systemd-boot/systemd-boot/0002-sd-boot-Load-board-specific-boot-entries-from-RMC-da.patch250
1 files changed, 250 insertions, 0 deletions
diff --git a/recipes-bsp/systemd-boot/systemd-boot/0002-sd-boot-Load-board-specific-boot-entries-from-RMC-da.patch b/recipes-bsp/systemd-boot/systemd-boot/0002-sd-boot-Load-board-specific-boot-entries-from-RMC-da.patch
new file mode 100644
index 0000000..e88012c
--- /dev/null
+++ b/recipes-bsp/systemd-boot/systemd-boot/0002-sd-boot-Load-board-specific-boot-entries-from-RMC-da.patch
@@ -0,0 +1,250 @@
1From a3c4fc8c2847fe289a617bcba1d905f580f0e18d 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 | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++-
43 1 file changed, 145 insertions(+), 2 deletions(-)
44
45diff --git a/src/boot/efi/boot.c b/src/boot/efi/boot.c
46index 30c1ead..d1b029b 100644
47--- a/src/boot/efi/boot.c
48+++ b/src/boot/efi/boot.c
49@@ -15,6 +15,7 @@
50
51 #include <efi.h>
52 #include <efilib.h>
53+#include <rmc_api.h>
54
55 #include "console.h"
56 #include "disk.h"
57@@ -33,6 +34,9 @@ static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-boot
58
59 static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE;
60
61+static CHAR8* rmc_db;
62+static rmc_fingerprint_t *rmc_fp;
63+
64 enum loader_type {
65 LOADER_UNDEFINED,
66 LOADER_EFI,
67@@ -1702,6 +1706,136 @@ static VOID config_free(Config *config) {
68 FreePool(config->entry_oneshot);
69 }
70
71+/* Derived from line_get_key_value(), we could consolidate two functions later */
72+static CHAR8 *get_line(CHAR8 *content, UINT64 *pos) {
73+ CHAR8 *line;
74+ UINT64 linelen;
75+
76+skip:
77+ line = content + *pos;
78+ if (*line == '\0')
79+ return NULL;
80+
81+ linelen = 0;
82+ while (line[linelen] && !strchra((CHAR8 *)"\n\r", line[linelen]))
83+ linelen++;
84+
85+ /* move pos to next line */
86+ *pos += linelen;
87+ if (content[*pos])
88+ (*pos)++;
89+
90+ /* empty line */
91+ if (linelen == 0)
92+ goto skip;
93+
94+ /* terminate line */
95+ line[linelen] = '\0';
96+
97+ /* remove leading whitespace */
98+ while (strchra((CHAR8 *)" \t", *line)) {
99+ line++;
100+ linelen--;
101+ }
102+
103+ /* remove trailing whitespace */
104+ while (linelen > 0 && strchra((CHAR8 *)" \t", line[linelen-1]))
105+ linelen--;
106+ line[linelen] = '\0';
107+
108+ if (*line == '#')
109+ goto skip;
110+
111+ return line;
112+}
113+
114+/* load rmc database file from ESP and try to get fingerprint. These
115+ * are essential information indicating we could query rmc data for
116+ * this board at least
117+ * return 0 if both database file and fingerprint can be obtained, otherwise
118+ * non-zero value is returned.
119+ *
120+ * Note: db and fp hold valid values only when this function returns 0.
121+ * Caller is responsible to free allocated memory pointed by *db and *fp when
122+ * this function returns 0.
123+ */
124+
125+static UINTN rmc_initialize(EFI_FILE *root_dir, EFI_SYSTEM_TABLE *sys_table, CHAR8 **db, rmc_fingerprint_t **fp) {
126+ UINTN len;
127+ UINTN ret = 1;
128+
129+ if (!db || !fp)
130+ return ret;
131+
132+ *db = NULL;
133+ *fp = NULL;
134+
135+ /* load rmc database */
136+ len = file_read(root_dir, L"\\rmc.db", 0, 0, db);
137+
138+ if (len <= 0)
139+ goto done;
140+
141+ *fp = AllocateZeroPool(sizeof(rmc_fingerprint_t));
142+ /* call rmc to get fingerprint. We will use single-action rmc APIs to query multiple files.
143+ * This should bring a better performance than calling double-action rmc API every time.
144+ */
145+ if (rmc_get_fingerprint(sys_table, *fp))
146+ goto done;
147+
148+ ret = 0;
149+done:
150+ if (ret) {
151+ FreePool(*db);
152+ FreePool(*fp);
153+ }
154+
155+ return ret;
156+}
157+
158+/* load RMC entries
159+ * return TRUE when at least one entry is loaded, otherwise, return FALSE
160+ */
161+static BOOLEAN config_load_rmc_entries(Config *config, EFI_HANDLE *device, CHAR16 *loaded_image_path, CHAR8 *db, rmc_fingerprint_t *fp) {
162+ CHAR8 *boot_entry = NULL;
163+ CHAR8 *boot_config = NULL;
164+ rmc_file_t rp;
165+ CHAR8 *line;
166+ UINT64 pos = 0;
167+ BOOLEAN ret = FALSE;
168+
169+ if (!db || !fp)
170+ return ret;
171+
172+ /* query boot entry config file */
173+ if (rmc_query_file_by_fp(fp, db, "BOOTENTRY.CONFIG", &rp))
174+ return ret;
175+
176+ /* file blob read from rmc db is not necessarily null-terminated, and we
177+ * should keep mem where rmc db lives from change during parsing
178+ */
179+ boot_config = AllocatePool(rp.blob_len * sizeof(CHAR8) + 1);
180+ CopyMem(boot_config, rp.blob, rp.blob_len);
181+ boot_config[rp.blob_len] = '\0';
182+ /* parse boot entry config */
183+ while ((line = get_line(boot_config, &pos))) {
184+ if (rmc_query_file_by_fp(fp, db, (char *)line, &rp))
185+ continue;
186+ if (rp.blob_len > 0) {
187+ boot_entry = AllocatePool(rp.blob_len * sizeof(CHAR8) + 1);
188+ CopyMem(boot_entry, rp.blob, rp.blob_len);
189+ boot_entry[rp.blob_len] = '\0';
190+ config_entry_add_from_file(config, device,
191+ stra_to_str(line), boot_entry,
192+ loaded_image_path);
193+ /* tell caller success when a RMC entry is loaded */
194+ ret = TRUE;
195+ }
196+ }
197+
198+ return ret;
199+}
200+
201 EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
202 CHAR16 *s;
203 CHAR8 *b;
204@@ -1714,6 +1848,7 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
205 UINT64 init_usec;
206 BOOLEAN menu = FALSE;
207 CHAR16 uuid[37];
208+ BOOLEAN rmc_entry = FALSE;
209
210 InitializeLib(image, sys_table);
211 init_usec = time_usec();
212@@ -1745,6 +1880,8 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
213 return EFI_LOAD_ERROR;
214 }
215
216+ /* Initialize rmc before loading any config */
217+ rmc_initialize(root_dir, sys_table, &rmc_db, &rmc_fp);
218
219 /* the filesystem path to this image, to prevent adding ourselves to the menu */
220 loaded_image_path = DevicePathToStr(loaded_image->FilePath);
221@@ -1753,11 +1890,15 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
222 ZeroMem(&config, sizeof(Config));
223 config_load_defaults(&config, root_dir);
224
225+ if (rmc_db && rmc_fp)
226+ rmc_entry = config_load_rmc_entries(&config, loaded_image->DeviceHandle, loaded_image_path, rmc_db, rmc_fp);
227+
228 /* scan /EFI/Linux/ directory */
229 config_entry_add_linux(&config, loaded_image, root_dir);
230
231- /* scan /loader/entries/\*.conf files */
232- config_load_entries(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path);
233+ /* scan /loader/entries/\*.conf files only when no RMC entry is loaded */
234+ if (rmc_entry == FALSE)
235+ config_load_entries(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path);
236
237 /* sort entries after version number */
238 config_sort_entries(&config);
239@@ -1851,6 +1992,8 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
240 out:
241 FreePool(loaded_image_path);
242 config_free(&config);
243+ FreePool(rmc_db);
244+ FreePool(rmc_fp);
245 uefi_call_wrapper(root_dir->Close, 1, root_dir);
246 uefi_call_wrapper(BS->CloseProtocol, 4, image, &LoadedImageProtocol, image, NULL);
247 return err;
248--
2492.7.4
250