1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * EFI boot manager 4 * 5 * Copyright (c) 2017 Rob Clark 6 */ 7 8 #include <common.h> 9 #include <charset.h> 10 #include <malloc.h> 11 #include <efi_loader.h> 12 #include <asm/unaligned.h> 13 14 static const struct efi_boot_services *bs; 15 static const struct efi_runtime_services *rs; 16 17 #define LOAD_OPTION_ACTIVE 0x00000001 18 #define LOAD_OPTION_FORCE_RECONNECT 0x00000002 19 #define LOAD_OPTION_HIDDEN 0x00000008 20 21 /* 22 * bootmgr implements the logic of trying to find a payload to boot 23 * based on the BootOrder + BootXXXX variables, and then loading it. 24 * 25 * TODO detecting a special key held (f9?) and displaying a boot menu 26 * like you would get on a PC would be clever. 27 * 28 * TODO if we had a way to write and persist variables after the OS 29 * has started, we'd also want to check OsIndications to see if we 30 * should do normal or recovery boot. 31 */ 32 33 34 /* Parse serialized data and transform it into efi_load_option structure */ 35 void efi_deserialize_load_option(struct efi_load_option *lo, u8 *data) 36 { 37 lo->attributes = get_unaligned_le32(data); 38 data += sizeof(u32); 39 40 lo->file_path_length = get_unaligned_le16(data); 41 data += sizeof(u16); 42 43 /* FIXME */ 44 lo->label = (u16 *)data; 45 data += (u16_strlen(lo->label) + 1) * sizeof(u16); 46 47 /* FIXME */ 48 lo->file_path = (struct efi_device_path *)data; 49 data += lo->file_path_length; 50 51 lo->optional_data = data; 52 } 53 54 /* 55 * Serialize efi_load_option structure into byte stream for BootXXXX. 56 * Return a size of allocated data. 57 */ 58 unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data) 59 { 60 unsigned long label_len, option_len; 61 unsigned long size; 62 u8 *p; 63 64 label_len = (u16_strlen(lo->label) + 1) * sizeof(u16); 65 option_len = strlen((char *)lo->optional_data); 66 67 /* total size */ 68 size = sizeof(lo->attributes); 69 size += sizeof(lo->file_path_length); 70 size += label_len; 71 size += lo->file_path_length; 72 size += option_len + 1; 73 p = malloc(size); 74 if (!p) 75 return 0; 76 77 /* copy data */ 78 *data = p; 79 memcpy(p, &lo->attributes, sizeof(lo->attributes)); 80 p += sizeof(lo->attributes); 81 82 memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length)); 83 p += sizeof(lo->file_path_length); 84 85 memcpy(p, lo->label, label_len); 86 p += label_len; 87 88 memcpy(p, lo->file_path, lo->file_path_length); 89 p += lo->file_path_length; 90 91 memcpy(p, lo->optional_data, option_len); 92 p += option_len; 93 *(char *)p = '\0'; 94 95 return size; 96 } 97 98 /* free() the result */ 99 static void *get_var(u16 *name, const efi_guid_t *vendor, 100 efi_uintn_t *size) 101 { 102 efi_guid_t *v = (efi_guid_t *)vendor; 103 efi_status_t ret; 104 void *buf = NULL; 105 106 *size = 0; 107 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf)); 108 if (ret == EFI_BUFFER_TOO_SMALL) { 109 buf = malloc(*size); 110 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf)); 111 } 112 113 if (ret != EFI_SUCCESS) { 114 free(buf); 115 *size = 0; 116 return NULL; 117 } 118 119 return buf; 120 } 121 122 /* 123 * Attempt to load load-option number 'n', returning device_path and file_path 124 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled) 125 * and that the specified file to boot exists. 126 */ 127 static void *try_load_entry(uint16_t n, struct efi_device_path **device_path, 128 struct efi_device_path **file_path) 129 { 130 struct efi_load_option lo; 131 u16 varname[] = L"Boot0000"; 132 u16 hexmap[] = L"0123456789ABCDEF"; 133 void *load_option, *image = NULL; 134 efi_uintn_t size; 135 136 varname[4] = hexmap[(n & 0xf000) >> 12]; 137 varname[5] = hexmap[(n & 0x0f00) >> 8]; 138 varname[6] = hexmap[(n & 0x00f0) >> 4]; 139 varname[7] = hexmap[(n & 0x000f) >> 0]; 140 141 load_option = get_var(varname, &efi_global_variable_guid, &size); 142 if (!load_option) 143 return NULL; 144 145 efi_deserialize_load_option(&lo, load_option); 146 147 if (lo.attributes & LOAD_OPTION_ACTIVE) { 148 efi_status_t ret; 149 150 debug("%s: trying to load \"%ls\" from %pD\n", 151 __func__, lo.label, lo.file_path); 152 153 ret = efi_load_image_from_path(lo.file_path, &image); 154 155 if (ret != EFI_SUCCESS) 156 goto error; 157 158 printf("Booting: %ls\n", lo.label); 159 efi_dp_split_file_path(lo.file_path, device_path, file_path); 160 } 161 162 error: 163 free(load_option); 164 165 return image; 166 } 167 168 /* 169 * Attempt to load, in the order specified by BootOrder EFI variable, the 170 * available load-options, finding and returning the first one that can 171 * be loaded successfully. 172 */ 173 void *efi_bootmgr_load(struct efi_device_path **device_path, 174 struct efi_device_path **file_path) 175 { 176 uint16_t *bootorder; 177 efi_uintn_t size; 178 void *image = NULL; 179 int i, num; 180 181 __efi_entry_check(); 182 183 bs = systab.boottime; 184 rs = systab.runtime; 185 186 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size); 187 if (!bootorder) 188 goto error; 189 190 num = size / sizeof(uint16_t); 191 for (i = 0; i < num; i++) { 192 debug("%s: trying to load Boot%04X\n", __func__, bootorder[i]); 193 image = try_load_entry(bootorder[i], device_path, file_path); 194 if (image) 195 break; 196 } 197 198 free(bootorder); 199 200 error: 201 __efi_exit_check(); 202 203 return image; 204 } 205