1 /* 2 * FDT related Helper functions used by the EFI stub on multiple 3 * architectures. This should be #included by the EFI stub 4 * implementation files. 5 * 6 * Copyright 2013 Linaro Limited; author Roy Franz 7 * 8 * This file is part of the Linux kernel, and is made available 9 * under the terms of the GNU General Public License version 2. 10 * 11 */ 12 13 #include <linux/efi.h> 14 #include <linux/libfdt.h> 15 #include <asm/efi.h> 16 17 #include "efistub.h" 18 19 efi_status_t update_fdt(efi_system_table_t *sys_table, void *orig_fdt, 20 unsigned long orig_fdt_size, 21 void *fdt, int new_fdt_size, char *cmdline_ptr, 22 u64 initrd_addr, u64 initrd_size, 23 efi_memory_desc_t *memory_map, 24 unsigned long map_size, unsigned long desc_size, 25 u32 desc_ver) 26 { 27 int node, prev, num_rsv; 28 int status; 29 u32 fdt_val32; 30 u64 fdt_val64; 31 32 /* Do some checks on provided FDT, if it exists*/ 33 if (orig_fdt) { 34 if (fdt_check_header(orig_fdt)) { 35 pr_efi_err(sys_table, "Device Tree header not valid!\n"); 36 return EFI_LOAD_ERROR; 37 } 38 /* 39 * We don't get the size of the FDT if we get if from a 40 * configuration table. 41 */ 42 if (orig_fdt_size && fdt_totalsize(orig_fdt) > orig_fdt_size) { 43 pr_efi_err(sys_table, "Truncated device tree! foo!\n"); 44 return EFI_LOAD_ERROR; 45 } 46 } 47 48 if (orig_fdt) 49 status = fdt_open_into(orig_fdt, fdt, new_fdt_size); 50 else 51 status = fdt_create_empty_tree(fdt, new_fdt_size); 52 53 if (status != 0) 54 goto fdt_set_fail; 55 56 /* 57 * Delete any memory nodes present. We must delete nodes which 58 * early_init_dt_scan_memory may try to use. 59 */ 60 prev = 0; 61 for (;;) { 62 const char *type; 63 int len; 64 65 node = fdt_next_node(fdt, prev, NULL); 66 if (node < 0) 67 break; 68 69 type = fdt_getprop(fdt, node, "device_type", &len); 70 if (type && strncmp(type, "memory", len) == 0) { 71 fdt_del_node(fdt, node); 72 continue; 73 } 74 75 prev = node; 76 } 77 78 /* 79 * Delete all memory reserve map entries. When booting via UEFI, 80 * kernel will use the UEFI memory map to find reserved regions. 81 */ 82 num_rsv = fdt_num_mem_rsv(fdt); 83 while (num_rsv-- > 0) 84 fdt_del_mem_rsv(fdt, num_rsv); 85 86 node = fdt_subnode_offset(fdt, 0, "chosen"); 87 if (node < 0) { 88 node = fdt_add_subnode(fdt, 0, "chosen"); 89 if (node < 0) { 90 status = node; /* node is error code when negative */ 91 goto fdt_set_fail; 92 } 93 } 94 95 if ((cmdline_ptr != NULL) && (strlen(cmdline_ptr) > 0)) { 96 status = fdt_setprop(fdt, node, "bootargs", cmdline_ptr, 97 strlen(cmdline_ptr) + 1); 98 if (status) 99 goto fdt_set_fail; 100 } 101 102 /* Set initrd address/end in device tree, if present */ 103 if (initrd_size != 0) { 104 u64 initrd_image_end; 105 u64 initrd_image_start = cpu_to_fdt64(initrd_addr); 106 107 status = fdt_setprop(fdt, node, "linux,initrd-start", 108 &initrd_image_start, sizeof(u64)); 109 if (status) 110 goto fdt_set_fail; 111 initrd_image_end = cpu_to_fdt64(initrd_addr + initrd_size); 112 status = fdt_setprop(fdt, node, "linux,initrd-end", 113 &initrd_image_end, sizeof(u64)); 114 if (status) 115 goto fdt_set_fail; 116 } 117 118 /* Add FDT entries for EFI runtime services in chosen node. */ 119 node = fdt_subnode_offset(fdt, 0, "chosen"); 120 fdt_val64 = cpu_to_fdt64((u64)(unsigned long)sys_table); 121 status = fdt_setprop(fdt, node, "linux,uefi-system-table", 122 &fdt_val64, sizeof(fdt_val64)); 123 if (status) 124 goto fdt_set_fail; 125 126 fdt_val64 = cpu_to_fdt64((u64)(unsigned long)memory_map); 127 status = fdt_setprop(fdt, node, "linux,uefi-mmap-start", 128 &fdt_val64, sizeof(fdt_val64)); 129 if (status) 130 goto fdt_set_fail; 131 132 fdt_val32 = cpu_to_fdt32(map_size); 133 status = fdt_setprop(fdt, node, "linux,uefi-mmap-size", 134 &fdt_val32, sizeof(fdt_val32)); 135 if (status) 136 goto fdt_set_fail; 137 138 fdt_val32 = cpu_to_fdt32(desc_size); 139 status = fdt_setprop(fdt, node, "linux,uefi-mmap-desc-size", 140 &fdt_val32, sizeof(fdt_val32)); 141 if (status) 142 goto fdt_set_fail; 143 144 fdt_val32 = cpu_to_fdt32(desc_ver); 145 status = fdt_setprop(fdt, node, "linux,uefi-mmap-desc-ver", 146 &fdt_val32, sizeof(fdt_val32)); 147 if (status) 148 goto fdt_set_fail; 149 150 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 151 efi_status_t efi_status; 152 153 efi_status = efi_get_random_bytes(sys_table, sizeof(fdt_val64), 154 (u8 *)&fdt_val64); 155 if (efi_status == EFI_SUCCESS) { 156 status = fdt_setprop(fdt, node, "kaslr-seed", 157 &fdt_val64, sizeof(fdt_val64)); 158 if (status) 159 goto fdt_set_fail; 160 } else if (efi_status != EFI_NOT_FOUND) { 161 return efi_status; 162 } 163 } 164 return EFI_SUCCESS; 165 166 fdt_set_fail: 167 if (status == -FDT_ERR_NOSPACE) 168 return EFI_BUFFER_TOO_SMALL; 169 170 return EFI_LOAD_ERROR; 171 } 172 173 #ifndef EFI_FDT_ALIGN 174 #define EFI_FDT_ALIGN EFI_PAGE_SIZE 175 #endif 176 177 /* 178 * Allocate memory for a new FDT, then add EFI, commandline, and 179 * initrd related fields to the FDT. This routine increases the 180 * FDT allocation size until the allocated memory is large 181 * enough. EFI allocations are in EFI_PAGE_SIZE granules, 182 * which are fixed at 4K bytes, so in most cases the first 183 * allocation should succeed. 184 * EFI boot services are exited at the end of this function. 185 * There must be no allocations between the get_memory_map() 186 * call and the exit_boot_services() call, so the exiting of 187 * boot services is very tightly tied to the creation of the FDT 188 * with the final memory map in it. 189 */ 190 191 efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table, 192 void *handle, 193 unsigned long *new_fdt_addr, 194 unsigned long max_addr, 195 u64 initrd_addr, u64 initrd_size, 196 char *cmdline_ptr, 197 unsigned long fdt_addr, 198 unsigned long fdt_size) 199 { 200 unsigned long map_size, desc_size; 201 u32 desc_ver; 202 unsigned long mmap_key; 203 efi_memory_desc_t *memory_map, *runtime_map; 204 unsigned long new_fdt_size; 205 efi_status_t status; 206 int runtime_entry_count = 0; 207 208 /* 209 * Get a copy of the current memory map that we will use to prepare 210 * the input for SetVirtualAddressMap(). We don't have to worry about 211 * subsequent allocations adding entries, since they could not affect 212 * the number of EFI_MEMORY_RUNTIME regions. 213 */ 214 status = efi_get_memory_map(sys_table, &runtime_map, &map_size, 215 &desc_size, &desc_ver, &mmap_key); 216 if (status != EFI_SUCCESS) { 217 pr_efi_err(sys_table, "Unable to retrieve UEFI memory map.\n"); 218 return status; 219 } 220 221 pr_efi(sys_table, 222 "Exiting boot services and installing virtual address map...\n"); 223 224 /* 225 * Estimate size of new FDT, and allocate memory for it. We 226 * will allocate a bigger buffer if this ends up being too 227 * small, so a rough guess is OK here. 228 */ 229 new_fdt_size = fdt_size + EFI_PAGE_SIZE; 230 while (1) { 231 status = efi_high_alloc(sys_table, new_fdt_size, EFI_FDT_ALIGN, 232 new_fdt_addr, max_addr); 233 if (status != EFI_SUCCESS) { 234 pr_efi_err(sys_table, "Unable to allocate memory for new device tree.\n"); 235 goto fail; 236 } 237 238 /* 239 * Now that we have done our final memory allocation (and free) 240 * we can get the memory map key needed for 241 * exit_boot_services(). 242 */ 243 status = efi_get_memory_map(sys_table, &memory_map, &map_size, 244 &desc_size, &desc_ver, &mmap_key); 245 if (status != EFI_SUCCESS) 246 goto fail_free_new_fdt; 247 248 status = update_fdt(sys_table, 249 (void *)fdt_addr, fdt_size, 250 (void *)*new_fdt_addr, new_fdt_size, 251 cmdline_ptr, initrd_addr, initrd_size, 252 memory_map, map_size, desc_size, desc_ver); 253 254 /* Succeeding the first time is the expected case. */ 255 if (status == EFI_SUCCESS) 256 break; 257 258 if (status == EFI_BUFFER_TOO_SMALL) { 259 /* 260 * We need to allocate more space for the new 261 * device tree, so free existing buffer that is 262 * too small. Also free memory map, as we will need 263 * to get new one that reflects the free/alloc we do 264 * on the device tree buffer. 265 */ 266 efi_free(sys_table, new_fdt_size, *new_fdt_addr); 267 sys_table->boottime->free_pool(memory_map); 268 new_fdt_size += EFI_PAGE_SIZE; 269 } else { 270 pr_efi_err(sys_table, "Unable to construct new device tree.\n"); 271 goto fail_free_mmap; 272 } 273 } 274 275 /* 276 * Update the memory map with virtual addresses. The function will also 277 * populate @runtime_map with copies of just the EFI_MEMORY_RUNTIME 278 * entries so that we can pass it straight into SetVirtualAddressMap() 279 */ 280 efi_get_virtmap(memory_map, map_size, desc_size, runtime_map, 281 &runtime_entry_count); 282 283 /* Now we are ready to exit_boot_services.*/ 284 status = sys_table->boottime->exit_boot_services(handle, mmap_key); 285 286 if (status == EFI_SUCCESS) { 287 efi_set_virtual_address_map_t *svam; 288 289 /* Install the new virtual address map */ 290 svam = sys_table->runtime->set_virtual_address_map; 291 status = svam(runtime_entry_count * desc_size, desc_size, 292 desc_ver, runtime_map); 293 294 /* 295 * We are beyond the point of no return here, so if the call to 296 * SetVirtualAddressMap() failed, we need to signal that to the 297 * incoming kernel but proceed normally otherwise. 298 */ 299 if (status != EFI_SUCCESS) { 300 int l; 301 302 /* 303 * Set the virtual address field of all 304 * EFI_MEMORY_RUNTIME entries to 0. This will signal 305 * the incoming kernel that no virtual translation has 306 * been installed. 307 */ 308 for (l = 0; l < map_size; l += desc_size) { 309 efi_memory_desc_t *p = (void *)memory_map + l; 310 311 if (p->attribute & EFI_MEMORY_RUNTIME) 312 p->virt_addr = 0; 313 } 314 } 315 return EFI_SUCCESS; 316 } 317 318 pr_efi_err(sys_table, "Exit boot services failed.\n"); 319 320 fail_free_mmap: 321 sys_table->boottime->free_pool(memory_map); 322 323 fail_free_new_fdt: 324 efi_free(sys_table, new_fdt_size, *new_fdt_addr); 325 326 fail: 327 sys_table->boottime->free_pool(runtime_map); 328 return EFI_LOAD_ERROR; 329 } 330 331 void *get_fdt(efi_system_table_t *sys_table, unsigned long *fdt_size) 332 { 333 efi_guid_t fdt_guid = DEVICE_TREE_GUID; 334 efi_config_table_t *tables; 335 void *fdt; 336 int i; 337 338 tables = (efi_config_table_t *) sys_table->tables; 339 fdt = NULL; 340 341 for (i = 0; i < sys_table->nr_tables; i++) 342 if (efi_guidcmp(tables[i].guid, fdt_guid) == 0) { 343 fdt = (void *) tables[i].table; 344 if (fdt_check_header(fdt) != 0) { 345 pr_efi_err(sys_table, "Invalid header detected on UEFI supplied FDT, ignoring ...\n"); 346 return NULL; 347 } 348 *fdt_size = fdt_totalsize(fdt); 349 break; 350 } 351 352 return fdt; 353 } 354