blob: 60ee3efc1c54d97aa0fa4f2032dc47313b9c8c28 [file] [log] [blame]
Lorenz Brun6ff6b452025-05-22 14:35:09 +02001From 24a496c861fff6f8633453eedb92079976d3058f Mon Sep 17 00:00:00 2001
Lorenz Brun35fcf032023-06-29 04:15:58 +02002From: Lorenz Brun <lorenz@monogon.tech>
3Date: Thu, 29 Jun 2023 03:54:01 +0200
Lorenz Brun6ff6b452025-05-22 14:35:09 +02004Subject: [PATCH 1/2] Implement filename-based A/B slot handling
Lorenz Brun35fcf032023-06-29 04:15:58 +02005
6---
7 src/boot/efi/stub.c | 66 +++++++++++++++++++++++++++++++++++++++++++++
8 1 file changed, 66 insertions(+)
9
10diff --git a/src/boot/efi/stub.c b/src/boot/efi/stub.c
Lorenz Brun6ff6b452025-05-22 14:35:09 +020011index 841a0e41bd..267ecbcc81 100644
Lorenz Brun35fcf032023-06-29 04:15:58 +020012--- a/src/boot/efi/stub.c
13+++ b/src/boot/efi/stub.c
Lorenz Brun6ff6b452025-05-22 14:35:09 +020014@@ -6,6 +6,8 @@
Lorenz Brun35fcf032023-06-29 04:15:58 +020015 #include "cpio.h"
Lorenz Brun6ff6b452025-05-22 14:35:09 +020016 #include "devicetree.h"
Lorenz Brun35fcf032023-06-29 04:15:58 +020017 #include "disk.h"
18+#include "efibind.h"
19+#include "efidevp.h"
20 #include "graphics.h"
21 #include "linux.h"
22 #include "measure.h"
Lorenz Brun6ff6b452025-05-22 14:35:09 +020023@@ -15,6 +17,31 @@
24 #include "tpm-pcr.h"
Lorenz Brun35fcf032023-06-29 04:15:58 +020025 #include "util.h"
26
27+// From picolibc under BSD-3-Clause (c) 2018 Arm Ltd.
28+/* Small and efficient strstr implementation. */
Lorenz Brun6ff6b452025-05-22 14:35:09 +020029+char16_t * strstr (const char16_t *hs, const char16_t *ne)
Lorenz Brun35fcf032023-06-29 04:15:58 +020030+{
31+ UINTN i;
32+ int c = ne[0];
33+
34+ if (c == 0)
Lorenz Brun6ff6b452025-05-22 14:35:09 +020035+ return (char16_t*)hs;
Lorenz Brun35fcf032023-06-29 04:15:58 +020036+
37+ for ( ; hs[0] != '\0'; hs++)
38+ {
39+ if (hs[0] != c)
40+ continue;
41+ for (i = 1; ne[i] != 0; i++)
42+ if (hs[i] != ne[i])
43+ break;
44+ if (ne[i] == '\0')
Lorenz Brun6ff6b452025-05-22 14:35:09 +020045+ return (char16_t*)hs;
Lorenz Brun35fcf032023-06-29 04:15:58 +020046+ }
47+
48+ return NULL;
49+}
50+
51+
52 /* magic string to find in the binary image */
Lorenz Brun6ff6b452025-05-22 14:35:09 +020053 _used_ _section_(".sdmagic") static const char magic[] = "#### LoaderInfo: systemd-stub " GIT_VERSION " ####";
Lorenz Brun35fcf032023-06-29 04:15:58 +020054
Lorenz Brun6ff6b452025-05-22 14:35:09 +020055@@ -232,6 +259,45 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
56 mangle_stub_cmdline(cmdline);
Lorenz Brun35fcf032023-06-29 04:15:58 +020057 }
58
59+ /* Extract last FILEPATH element from image path to check file name */
60+ CHAR16 *last_file_path = NULL;
61+ EFI_DEVICE_PATH *current_path_elem = loaded_image->FilePath;
62+ while (current_path_elem != NULL && !IsDevicePathEnd(current_path_elem)) {
63+ if (DevicePathType(current_path_elem) == MEDIA_DEVICE_PATH &&
64+ current_path_elem->SubType == MEDIA_FILEPATH_DP) {
65+ last_file_path = ((FILEPATH_DEVICE_PATH*)current_path_elem)->PathName;
66+ }
67+ current_path_elem = NextDevicePathNode(current_path_elem);
68+ }
69+ /* Check slot based on suffix of the last FILE_PATH value */
Lorenz Brun6ff6b452025-05-22 14:35:09 +020070+ char16_t slot = 'A';
71+ const char16_t suffix_a[] = u"boot-a.efi";
72+ const char16_t suffix_b[] = u"boot-b.efi";
73+ const UINTN suffix_len = (sizeof(suffix_a)/sizeof(char16_t))-1;
Lorenz Brun35fcf032023-06-29 04:15:58 +020074+ if (last_file_path != NULL) {
75+ UINTN plen = StrLen(last_file_path);
76+ if (suffix_len > plen) {
Lorenz Brun6ff6b452025-05-22 14:35:09 +020077+ Print(L"File name too short, blindly booting slot A\n");
78+ } else if (StriCmp(suffix_a, &last_file_path[plen-suffix_len]) == 0){
Lorenz Brun35fcf032023-06-29 04:15:58 +020079+ slot = 'A';
Lorenz Brun6ff6b452025-05-22 14:35:09 +020080+ } else if (StriCmp(suffix_b, &last_file_path[plen-suffix_len]) == 0) {
Lorenz Brun35fcf032023-06-29 04:15:58 +020081+ slot = 'B';
82+ } else {
Lorenz Brun6ff6b452025-05-22 14:35:09 +020083+ Print(L"Unknown file name, blindly booting slot A\n");
Lorenz Brun35fcf032023-06-29 04:15:58 +020084+ }
85+ }
86+ Print(L"Booting into Slot %c\n", slot);
87+ /* Replace METROPOLIS-SYSTEM-X with the correct slot */
Lorenz Brun6ff6b452025-05-22 14:35:09 +020088+ const char16_t slot_identifier[] = u"METROPOLIS-SYSTEM-X\0";
89+ const UINTN slot_id_len = (sizeof(slot_identifier)/sizeof(char16_t))-1;
Lorenz Brun35fcf032023-06-29 04:15:58 +020090+ if (cmdline != NULL) {
Lorenz Brun6ff6b452025-05-22 14:35:09 +020091+ char16_t *rest_ptr = cmdline;
Lorenz Brun35fcf032023-06-29 04:15:58 +020092+ while((rest_ptr = strstr(rest_ptr, slot_identifier))) {
Lorenz Brun6ff6b452025-05-22 14:35:09 +020093+ rest_ptr[slot_id_len-2] = slot;
Lorenz Brun35fcf032023-06-29 04:15:58 +020094+ rest_ptr += slot_id_len;
Lorenz Brun6ff6b452025-05-22 14:35:09 +020095+ }
Lorenz Brun35fcf032023-06-29 04:15:58 +020096+ }
97+
Lorenz Brun6ff6b452025-05-22 14:35:09 +020098 export_variables(loaded_image);
99
100 if (pack_cpio(loaded_image,
Lorenz Brun35fcf032023-06-29 04:15:58 +0200101--
Lorenz Brun6ff6b452025-05-22 14:35:09 +02001022.47.2
Lorenz Brun35fcf032023-06-29 04:15:58 +0200103