1f4f75ad5SArd Biesheuvel /* 2f4f75ad5SArd Biesheuvel * Helper functions used by the EFI stub on multiple 3f4f75ad5SArd Biesheuvel * architectures. This should be #included by the EFI stub 4f4f75ad5SArd Biesheuvel * implementation files. 5f4f75ad5SArd Biesheuvel * 6f4f75ad5SArd Biesheuvel * Copyright 2011 Intel Corporation; author Matt Fleming 7f4f75ad5SArd Biesheuvel * 8f4f75ad5SArd Biesheuvel * This file is part of the Linux kernel, and is made available 9f4f75ad5SArd Biesheuvel * under the terms of the GNU General Public License version 2. 10f4f75ad5SArd Biesheuvel * 11f4f75ad5SArd Biesheuvel */ 12f4f75ad5SArd Biesheuvel 13f4f75ad5SArd Biesheuvel #include <linux/efi.h> 14f4f75ad5SArd Biesheuvel #include <asm/efi.h> 15f4f75ad5SArd Biesheuvel 16f4f75ad5SArd Biesheuvel #include "efistub.h" 17f4f75ad5SArd Biesheuvel 18f4f75ad5SArd Biesheuvel #define EFI_READ_CHUNK_SIZE (1024 * 1024) 19f4f75ad5SArd Biesheuvel 20f4f75ad5SArd Biesheuvel struct file_info { 21f4f75ad5SArd Biesheuvel efi_file_handle_t *handle; 22f4f75ad5SArd Biesheuvel u64 size; 23f4f75ad5SArd Biesheuvel }; 24f4f75ad5SArd Biesheuvel 25f4f75ad5SArd Biesheuvel void efi_printk(efi_system_table_t *sys_table_arg, char *str) 26f4f75ad5SArd Biesheuvel { 27f4f75ad5SArd Biesheuvel char *s8; 28f4f75ad5SArd Biesheuvel 29f4f75ad5SArd Biesheuvel for (s8 = str; *s8; s8++) { 30f4f75ad5SArd Biesheuvel efi_char16_t ch[2] = { 0 }; 31f4f75ad5SArd Biesheuvel 32f4f75ad5SArd Biesheuvel ch[0] = *s8; 33f4f75ad5SArd Biesheuvel if (*s8 == '\n') { 34f4f75ad5SArd Biesheuvel efi_char16_t nl[2] = { '\r', 0 }; 35f4f75ad5SArd Biesheuvel efi_char16_printk(sys_table_arg, nl); 36f4f75ad5SArd Biesheuvel } 37f4f75ad5SArd Biesheuvel 38f4f75ad5SArd Biesheuvel efi_char16_printk(sys_table_arg, ch); 39f4f75ad5SArd Biesheuvel } 40f4f75ad5SArd Biesheuvel } 41f4f75ad5SArd Biesheuvel 42f4f75ad5SArd Biesheuvel efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg, 43f4f75ad5SArd Biesheuvel efi_memory_desc_t **map, 44f4f75ad5SArd Biesheuvel unsigned long *map_size, 45f4f75ad5SArd Biesheuvel unsigned long *desc_size, 46f4f75ad5SArd Biesheuvel u32 *desc_ver, 47f4f75ad5SArd Biesheuvel unsigned long *key_ptr) 48f4f75ad5SArd Biesheuvel { 49f4f75ad5SArd Biesheuvel efi_memory_desc_t *m = NULL; 50f4f75ad5SArd Biesheuvel efi_status_t status; 51f4f75ad5SArd Biesheuvel unsigned long key; 52f4f75ad5SArd Biesheuvel u32 desc_version; 53f4f75ad5SArd Biesheuvel 54f4f75ad5SArd Biesheuvel *map_size = sizeof(*m) * 32; 55f4f75ad5SArd Biesheuvel again: 56f4f75ad5SArd Biesheuvel /* 57f4f75ad5SArd Biesheuvel * Add an additional efi_memory_desc_t because we're doing an 58f4f75ad5SArd Biesheuvel * allocation which may be in a new descriptor region. 59f4f75ad5SArd Biesheuvel */ 60f4f75ad5SArd Biesheuvel *map_size += sizeof(*m); 61f4f75ad5SArd Biesheuvel status = efi_call_early(allocate_pool, EFI_LOADER_DATA, 62f4f75ad5SArd Biesheuvel *map_size, (void **)&m); 63f4f75ad5SArd Biesheuvel if (status != EFI_SUCCESS) 64f4f75ad5SArd Biesheuvel goto fail; 65f4f75ad5SArd Biesheuvel 66f4f75ad5SArd Biesheuvel *desc_size = 0; 67f4f75ad5SArd Biesheuvel key = 0; 68f4f75ad5SArd Biesheuvel status = efi_call_early(get_memory_map, map_size, m, 69f4f75ad5SArd Biesheuvel &key, desc_size, &desc_version); 70f4f75ad5SArd Biesheuvel if (status == EFI_BUFFER_TOO_SMALL) { 71f4f75ad5SArd Biesheuvel efi_call_early(free_pool, m); 72f4f75ad5SArd Biesheuvel goto again; 73f4f75ad5SArd Biesheuvel } 74f4f75ad5SArd Biesheuvel 75f4f75ad5SArd Biesheuvel if (status != EFI_SUCCESS) 76f4f75ad5SArd Biesheuvel efi_call_early(free_pool, m); 77f4f75ad5SArd Biesheuvel 78f4f75ad5SArd Biesheuvel if (key_ptr && status == EFI_SUCCESS) 79f4f75ad5SArd Biesheuvel *key_ptr = key; 80f4f75ad5SArd Biesheuvel if (desc_ver && status == EFI_SUCCESS) 81f4f75ad5SArd Biesheuvel *desc_ver = desc_version; 82f4f75ad5SArd Biesheuvel 83f4f75ad5SArd Biesheuvel fail: 84f4f75ad5SArd Biesheuvel *map = m; 85f4f75ad5SArd Biesheuvel return status; 86f4f75ad5SArd Biesheuvel } 87f4f75ad5SArd Biesheuvel 88f4f75ad5SArd Biesheuvel 89f4f75ad5SArd Biesheuvel unsigned long __init get_dram_base(efi_system_table_t *sys_table_arg) 90f4f75ad5SArd Biesheuvel { 91f4f75ad5SArd Biesheuvel efi_status_t status; 92f4f75ad5SArd Biesheuvel unsigned long map_size; 93f4f75ad5SArd Biesheuvel unsigned long membase = EFI_ERROR; 94f4f75ad5SArd Biesheuvel struct efi_memory_map map; 95f4f75ad5SArd Biesheuvel efi_memory_desc_t *md; 96f4f75ad5SArd Biesheuvel 97f4f75ad5SArd Biesheuvel status = efi_get_memory_map(sys_table_arg, (efi_memory_desc_t **)&map.map, 98f4f75ad5SArd Biesheuvel &map_size, &map.desc_size, NULL, NULL); 99f4f75ad5SArd Biesheuvel if (status != EFI_SUCCESS) 100f4f75ad5SArd Biesheuvel return membase; 101f4f75ad5SArd Biesheuvel 102f4f75ad5SArd Biesheuvel map.map_end = map.map + map_size; 103f4f75ad5SArd Biesheuvel 104f4f75ad5SArd Biesheuvel for_each_efi_memory_desc(&map, md) 105f4f75ad5SArd Biesheuvel if (md->attribute & EFI_MEMORY_WB) 106f4f75ad5SArd Biesheuvel if (membase > md->phys_addr) 107f4f75ad5SArd Biesheuvel membase = md->phys_addr; 108f4f75ad5SArd Biesheuvel 109f4f75ad5SArd Biesheuvel efi_call_early(free_pool, map.map); 110f4f75ad5SArd Biesheuvel 111f4f75ad5SArd Biesheuvel return membase; 112f4f75ad5SArd Biesheuvel } 113f4f75ad5SArd Biesheuvel 114f4f75ad5SArd Biesheuvel /* 115f4f75ad5SArd Biesheuvel * Allocate at the highest possible address that is not above 'max'. 116f4f75ad5SArd Biesheuvel */ 117f4f75ad5SArd Biesheuvel efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg, 118f4f75ad5SArd Biesheuvel unsigned long size, unsigned long align, 119f4f75ad5SArd Biesheuvel unsigned long *addr, unsigned long max) 120f4f75ad5SArd Biesheuvel { 121f4f75ad5SArd Biesheuvel unsigned long map_size, desc_size; 122f4f75ad5SArd Biesheuvel efi_memory_desc_t *map; 123f4f75ad5SArd Biesheuvel efi_status_t status; 124f4f75ad5SArd Biesheuvel unsigned long nr_pages; 125f4f75ad5SArd Biesheuvel u64 max_addr = 0; 126f4f75ad5SArd Biesheuvel int i; 127f4f75ad5SArd Biesheuvel 128f4f75ad5SArd Biesheuvel status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size, 129f4f75ad5SArd Biesheuvel NULL, NULL); 130f4f75ad5SArd Biesheuvel if (status != EFI_SUCCESS) 131f4f75ad5SArd Biesheuvel goto fail; 132f4f75ad5SArd Biesheuvel 133f4f75ad5SArd Biesheuvel /* 134f4f75ad5SArd Biesheuvel * Enforce minimum alignment that EFI requires when requesting 135f4f75ad5SArd Biesheuvel * a specific address. We are doing page-based allocations, 136f4f75ad5SArd Biesheuvel * so we must be aligned to a page. 137f4f75ad5SArd Biesheuvel */ 138f4f75ad5SArd Biesheuvel if (align < EFI_PAGE_SIZE) 139f4f75ad5SArd Biesheuvel align = EFI_PAGE_SIZE; 140f4f75ad5SArd Biesheuvel 141f4f75ad5SArd Biesheuvel nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; 142f4f75ad5SArd Biesheuvel again: 143f4f75ad5SArd Biesheuvel for (i = 0; i < map_size / desc_size; i++) { 144f4f75ad5SArd Biesheuvel efi_memory_desc_t *desc; 145f4f75ad5SArd Biesheuvel unsigned long m = (unsigned long)map; 146f4f75ad5SArd Biesheuvel u64 start, end; 147f4f75ad5SArd Biesheuvel 148f4f75ad5SArd Biesheuvel desc = (efi_memory_desc_t *)(m + (i * desc_size)); 149f4f75ad5SArd Biesheuvel if (desc->type != EFI_CONVENTIONAL_MEMORY) 150f4f75ad5SArd Biesheuvel continue; 151f4f75ad5SArd Biesheuvel 152f4f75ad5SArd Biesheuvel if (desc->num_pages < nr_pages) 153f4f75ad5SArd Biesheuvel continue; 154f4f75ad5SArd Biesheuvel 155f4f75ad5SArd Biesheuvel start = desc->phys_addr; 156f4f75ad5SArd Biesheuvel end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT); 157f4f75ad5SArd Biesheuvel 158f4f75ad5SArd Biesheuvel if ((start + size) > end || (start + size) > max) 159f4f75ad5SArd Biesheuvel continue; 160f4f75ad5SArd Biesheuvel 161f4f75ad5SArd Biesheuvel if (end - size > max) 162f4f75ad5SArd Biesheuvel end = max; 163f4f75ad5SArd Biesheuvel 164f4f75ad5SArd Biesheuvel if (round_down(end - size, align) < start) 165f4f75ad5SArd Biesheuvel continue; 166f4f75ad5SArd Biesheuvel 167f4f75ad5SArd Biesheuvel start = round_down(end - size, align); 168f4f75ad5SArd Biesheuvel 169f4f75ad5SArd Biesheuvel /* 170f4f75ad5SArd Biesheuvel * Don't allocate at 0x0. It will confuse code that 171f4f75ad5SArd Biesheuvel * checks pointers against NULL. 172f4f75ad5SArd Biesheuvel */ 173f4f75ad5SArd Biesheuvel if (start == 0x0) 174f4f75ad5SArd Biesheuvel continue; 175f4f75ad5SArd Biesheuvel 176f4f75ad5SArd Biesheuvel if (start > max_addr) 177f4f75ad5SArd Biesheuvel max_addr = start; 178f4f75ad5SArd Biesheuvel } 179f4f75ad5SArd Biesheuvel 180f4f75ad5SArd Biesheuvel if (!max_addr) 181f4f75ad5SArd Biesheuvel status = EFI_NOT_FOUND; 182f4f75ad5SArd Biesheuvel else { 183f4f75ad5SArd Biesheuvel status = efi_call_early(allocate_pages, 184f4f75ad5SArd Biesheuvel EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, 185f4f75ad5SArd Biesheuvel nr_pages, &max_addr); 186f4f75ad5SArd Biesheuvel if (status != EFI_SUCCESS) { 187f4f75ad5SArd Biesheuvel max = max_addr; 188f4f75ad5SArd Biesheuvel max_addr = 0; 189f4f75ad5SArd Biesheuvel goto again; 190f4f75ad5SArd Biesheuvel } 191f4f75ad5SArd Biesheuvel 192f4f75ad5SArd Biesheuvel *addr = max_addr; 193f4f75ad5SArd Biesheuvel } 194f4f75ad5SArd Biesheuvel 195f4f75ad5SArd Biesheuvel efi_call_early(free_pool, map); 196f4f75ad5SArd Biesheuvel fail: 197f4f75ad5SArd Biesheuvel return status; 198f4f75ad5SArd Biesheuvel } 199f4f75ad5SArd Biesheuvel 200f4f75ad5SArd Biesheuvel /* 201f4f75ad5SArd Biesheuvel * Allocate at the lowest possible address. 202f4f75ad5SArd Biesheuvel */ 203f4f75ad5SArd Biesheuvel efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg, 204f4f75ad5SArd Biesheuvel unsigned long size, unsigned long align, 205f4f75ad5SArd Biesheuvel unsigned long *addr) 206f4f75ad5SArd Biesheuvel { 207f4f75ad5SArd Biesheuvel unsigned long map_size, desc_size; 208f4f75ad5SArd Biesheuvel efi_memory_desc_t *map; 209f4f75ad5SArd Biesheuvel efi_status_t status; 210f4f75ad5SArd Biesheuvel unsigned long nr_pages; 211f4f75ad5SArd Biesheuvel int i; 212f4f75ad5SArd Biesheuvel 213f4f75ad5SArd Biesheuvel status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size, 214f4f75ad5SArd Biesheuvel NULL, NULL); 215f4f75ad5SArd Biesheuvel if (status != EFI_SUCCESS) 216f4f75ad5SArd Biesheuvel goto fail; 217f4f75ad5SArd Biesheuvel 218f4f75ad5SArd Biesheuvel /* 219f4f75ad5SArd Biesheuvel * Enforce minimum alignment that EFI requires when requesting 220f4f75ad5SArd Biesheuvel * a specific address. We are doing page-based allocations, 221f4f75ad5SArd Biesheuvel * so we must be aligned to a page. 222f4f75ad5SArd Biesheuvel */ 223f4f75ad5SArd Biesheuvel if (align < EFI_PAGE_SIZE) 224f4f75ad5SArd Biesheuvel align = EFI_PAGE_SIZE; 225f4f75ad5SArd Biesheuvel 226f4f75ad5SArd Biesheuvel nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; 227f4f75ad5SArd Biesheuvel for (i = 0; i < map_size / desc_size; i++) { 228f4f75ad5SArd Biesheuvel efi_memory_desc_t *desc; 229f4f75ad5SArd Biesheuvel unsigned long m = (unsigned long)map; 230f4f75ad5SArd Biesheuvel u64 start, end; 231f4f75ad5SArd Biesheuvel 232f4f75ad5SArd Biesheuvel desc = (efi_memory_desc_t *)(m + (i * desc_size)); 233f4f75ad5SArd Biesheuvel 234f4f75ad5SArd Biesheuvel if (desc->type != EFI_CONVENTIONAL_MEMORY) 235f4f75ad5SArd Biesheuvel continue; 236f4f75ad5SArd Biesheuvel 237f4f75ad5SArd Biesheuvel if (desc->num_pages < nr_pages) 238f4f75ad5SArd Biesheuvel continue; 239f4f75ad5SArd Biesheuvel 240f4f75ad5SArd Biesheuvel start = desc->phys_addr; 241f4f75ad5SArd Biesheuvel end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT); 242f4f75ad5SArd Biesheuvel 243f4f75ad5SArd Biesheuvel /* 244f4f75ad5SArd Biesheuvel * Don't allocate at 0x0. It will confuse code that 245f4f75ad5SArd Biesheuvel * checks pointers against NULL. Skip the first 8 246f4f75ad5SArd Biesheuvel * bytes so we start at a nice even number. 247f4f75ad5SArd Biesheuvel */ 248f4f75ad5SArd Biesheuvel if (start == 0x0) 249f4f75ad5SArd Biesheuvel start += 8; 250f4f75ad5SArd Biesheuvel 251f4f75ad5SArd Biesheuvel start = round_up(start, align); 252f4f75ad5SArd Biesheuvel if ((start + size) > end) 253f4f75ad5SArd Biesheuvel continue; 254f4f75ad5SArd Biesheuvel 255f4f75ad5SArd Biesheuvel status = efi_call_early(allocate_pages, 256f4f75ad5SArd Biesheuvel EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, 257f4f75ad5SArd Biesheuvel nr_pages, &start); 258f4f75ad5SArd Biesheuvel if (status == EFI_SUCCESS) { 259f4f75ad5SArd Biesheuvel *addr = start; 260f4f75ad5SArd Biesheuvel break; 261f4f75ad5SArd Biesheuvel } 262f4f75ad5SArd Biesheuvel } 263f4f75ad5SArd Biesheuvel 264f4f75ad5SArd Biesheuvel if (i == map_size / desc_size) 265f4f75ad5SArd Biesheuvel status = EFI_NOT_FOUND; 266f4f75ad5SArd Biesheuvel 267f4f75ad5SArd Biesheuvel efi_call_early(free_pool, map); 268f4f75ad5SArd Biesheuvel fail: 269f4f75ad5SArd Biesheuvel return status; 270f4f75ad5SArd Biesheuvel } 271f4f75ad5SArd Biesheuvel 272f4f75ad5SArd Biesheuvel void efi_free(efi_system_table_t *sys_table_arg, unsigned long size, 273f4f75ad5SArd Biesheuvel unsigned long addr) 274f4f75ad5SArd Biesheuvel { 275f4f75ad5SArd Biesheuvel unsigned long nr_pages; 276f4f75ad5SArd Biesheuvel 277f4f75ad5SArd Biesheuvel if (!size) 278f4f75ad5SArd Biesheuvel return; 279f4f75ad5SArd Biesheuvel 280f4f75ad5SArd Biesheuvel nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; 281f4f75ad5SArd Biesheuvel efi_call_early(free_pages, addr, nr_pages); 282f4f75ad5SArd Biesheuvel } 283f4f75ad5SArd Biesheuvel 284f4f75ad5SArd Biesheuvel 285f4f75ad5SArd Biesheuvel /* 286f4f75ad5SArd Biesheuvel * Check the cmdline for a LILO-style file= arguments. 287f4f75ad5SArd Biesheuvel * 288f4f75ad5SArd Biesheuvel * We only support loading a file from the same filesystem as 289f4f75ad5SArd Biesheuvel * the kernel image. 290f4f75ad5SArd Biesheuvel */ 291f4f75ad5SArd Biesheuvel efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg, 292f4f75ad5SArd Biesheuvel efi_loaded_image_t *image, 293f4f75ad5SArd Biesheuvel char *cmd_line, char *option_string, 294f4f75ad5SArd Biesheuvel unsigned long max_addr, 295f4f75ad5SArd Biesheuvel unsigned long *load_addr, 296f4f75ad5SArd Biesheuvel unsigned long *load_size) 297f4f75ad5SArd Biesheuvel { 298f4f75ad5SArd Biesheuvel struct file_info *files; 299f4f75ad5SArd Biesheuvel unsigned long file_addr; 300f4f75ad5SArd Biesheuvel u64 file_size_total; 301f4f75ad5SArd Biesheuvel efi_file_handle_t *fh = NULL; 302f4f75ad5SArd Biesheuvel efi_status_t status; 303f4f75ad5SArd Biesheuvel int nr_files; 304f4f75ad5SArd Biesheuvel char *str; 305f4f75ad5SArd Biesheuvel int i, j, k; 306f4f75ad5SArd Biesheuvel 307f4f75ad5SArd Biesheuvel file_addr = 0; 308f4f75ad5SArd Biesheuvel file_size_total = 0; 309f4f75ad5SArd Biesheuvel 310f4f75ad5SArd Biesheuvel str = cmd_line; 311f4f75ad5SArd Biesheuvel 312f4f75ad5SArd Biesheuvel j = 0; /* See close_handles */ 313f4f75ad5SArd Biesheuvel 314f4f75ad5SArd Biesheuvel if (!load_addr || !load_size) 315f4f75ad5SArd Biesheuvel return EFI_INVALID_PARAMETER; 316f4f75ad5SArd Biesheuvel 317f4f75ad5SArd Biesheuvel *load_addr = 0; 318f4f75ad5SArd Biesheuvel *load_size = 0; 319f4f75ad5SArd Biesheuvel 320f4f75ad5SArd Biesheuvel if (!str || !*str) 321f4f75ad5SArd Biesheuvel return EFI_SUCCESS; 322f4f75ad5SArd Biesheuvel 323f4f75ad5SArd Biesheuvel for (nr_files = 0; *str; nr_files++) { 324f4f75ad5SArd Biesheuvel str = strstr(str, option_string); 325f4f75ad5SArd Biesheuvel if (!str) 326f4f75ad5SArd Biesheuvel break; 327f4f75ad5SArd Biesheuvel 328f4f75ad5SArd Biesheuvel str += strlen(option_string); 329f4f75ad5SArd Biesheuvel 330f4f75ad5SArd Biesheuvel /* Skip any leading slashes */ 331f4f75ad5SArd Biesheuvel while (*str == '/' || *str == '\\') 332f4f75ad5SArd Biesheuvel str++; 333f4f75ad5SArd Biesheuvel 334f4f75ad5SArd Biesheuvel while (*str && *str != ' ' && *str != '\n') 335f4f75ad5SArd Biesheuvel str++; 336f4f75ad5SArd Biesheuvel } 337f4f75ad5SArd Biesheuvel 338f4f75ad5SArd Biesheuvel if (!nr_files) 339f4f75ad5SArd Biesheuvel return EFI_SUCCESS; 340f4f75ad5SArd Biesheuvel 341f4f75ad5SArd Biesheuvel status = efi_call_early(allocate_pool, EFI_LOADER_DATA, 342f4f75ad5SArd Biesheuvel nr_files * sizeof(*files), (void **)&files); 343f4f75ad5SArd Biesheuvel if (status != EFI_SUCCESS) { 344f4f75ad5SArd Biesheuvel pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n"); 345f4f75ad5SArd Biesheuvel goto fail; 346f4f75ad5SArd Biesheuvel } 347f4f75ad5SArd Biesheuvel 348f4f75ad5SArd Biesheuvel str = cmd_line; 349f4f75ad5SArd Biesheuvel for (i = 0; i < nr_files; i++) { 350f4f75ad5SArd Biesheuvel struct file_info *file; 351f4f75ad5SArd Biesheuvel efi_char16_t filename_16[256]; 352f4f75ad5SArd Biesheuvel efi_char16_t *p; 353f4f75ad5SArd Biesheuvel 354f4f75ad5SArd Biesheuvel str = strstr(str, option_string); 355f4f75ad5SArd Biesheuvel if (!str) 356f4f75ad5SArd Biesheuvel break; 357f4f75ad5SArd Biesheuvel 358f4f75ad5SArd Biesheuvel str += strlen(option_string); 359f4f75ad5SArd Biesheuvel 360f4f75ad5SArd Biesheuvel file = &files[i]; 361f4f75ad5SArd Biesheuvel p = filename_16; 362f4f75ad5SArd Biesheuvel 363f4f75ad5SArd Biesheuvel /* Skip any leading slashes */ 364f4f75ad5SArd Biesheuvel while (*str == '/' || *str == '\\') 365f4f75ad5SArd Biesheuvel str++; 366f4f75ad5SArd Biesheuvel 367f4f75ad5SArd Biesheuvel while (*str && *str != ' ' && *str != '\n') { 368f4f75ad5SArd Biesheuvel if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16)) 369f4f75ad5SArd Biesheuvel break; 370f4f75ad5SArd Biesheuvel 371f4f75ad5SArd Biesheuvel if (*str == '/') { 372f4f75ad5SArd Biesheuvel *p++ = '\\'; 373f4f75ad5SArd Biesheuvel str++; 374f4f75ad5SArd Biesheuvel } else { 375f4f75ad5SArd Biesheuvel *p++ = *str++; 376f4f75ad5SArd Biesheuvel } 377f4f75ad5SArd Biesheuvel } 378f4f75ad5SArd Biesheuvel 379f4f75ad5SArd Biesheuvel *p = '\0'; 380f4f75ad5SArd Biesheuvel 381f4f75ad5SArd Biesheuvel /* Only open the volume once. */ 382f4f75ad5SArd Biesheuvel if (!i) { 383f4f75ad5SArd Biesheuvel status = efi_open_volume(sys_table_arg, image, 384f4f75ad5SArd Biesheuvel (void **)&fh); 385f4f75ad5SArd Biesheuvel if (status != EFI_SUCCESS) 386f4f75ad5SArd Biesheuvel goto free_files; 387f4f75ad5SArd Biesheuvel } 388f4f75ad5SArd Biesheuvel 389f4f75ad5SArd Biesheuvel status = efi_file_size(sys_table_arg, fh, filename_16, 390f4f75ad5SArd Biesheuvel (void **)&file->handle, &file->size); 391f4f75ad5SArd Biesheuvel if (status != EFI_SUCCESS) 392f4f75ad5SArd Biesheuvel goto close_handles; 393f4f75ad5SArd Biesheuvel 394f4f75ad5SArd Biesheuvel file_size_total += file->size; 395f4f75ad5SArd Biesheuvel } 396f4f75ad5SArd Biesheuvel 397f4f75ad5SArd Biesheuvel if (file_size_total) { 398f4f75ad5SArd Biesheuvel unsigned long addr; 399f4f75ad5SArd Biesheuvel 400f4f75ad5SArd Biesheuvel /* 401f4f75ad5SArd Biesheuvel * Multiple files need to be at consecutive addresses in memory, 402f4f75ad5SArd Biesheuvel * so allocate enough memory for all the files. This is used 403f4f75ad5SArd Biesheuvel * for loading multiple files. 404f4f75ad5SArd Biesheuvel */ 405f4f75ad5SArd Biesheuvel status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000, 406f4f75ad5SArd Biesheuvel &file_addr, max_addr); 407f4f75ad5SArd Biesheuvel if (status != EFI_SUCCESS) { 408f4f75ad5SArd Biesheuvel pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n"); 409f4f75ad5SArd Biesheuvel goto close_handles; 410f4f75ad5SArd Biesheuvel } 411f4f75ad5SArd Biesheuvel 412f4f75ad5SArd Biesheuvel /* We've run out of free low memory. */ 413f4f75ad5SArd Biesheuvel if (file_addr > max_addr) { 414f4f75ad5SArd Biesheuvel pr_efi_err(sys_table_arg, "We've run out of free low memory\n"); 415f4f75ad5SArd Biesheuvel status = EFI_INVALID_PARAMETER; 416f4f75ad5SArd Biesheuvel goto free_file_total; 417f4f75ad5SArd Biesheuvel } 418f4f75ad5SArd Biesheuvel 419f4f75ad5SArd Biesheuvel addr = file_addr; 420f4f75ad5SArd Biesheuvel for (j = 0; j < nr_files; j++) { 421f4f75ad5SArd Biesheuvel unsigned long size; 422f4f75ad5SArd Biesheuvel 423f4f75ad5SArd Biesheuvel size = files[j].size; 424f4f75ad5SArd Biesheuvel while (size) { 425f4f75ad5SArd Biesheuvel unsigned long chunksize; 426f4f75ad5SArd Biesheuvel if (size > EFI_READ_CHUNK_SIZE) 427f4f75ad5SArd Biesheuvel chunksize = EFI_READ_CHUNK_SIZE; 428f4f75ad5SArd Biesheuvel else 429f4f75ad5SArd Biesheuvel chunksize = size; 430f4f75ad5SArd Biesheuvel 431f4f75ad5SArd Biesheuvel status = efi_file_read(files[j].handle, 432f4f75ad5SArd Biesheuvel &chunksize, 433f4f75ad5SArd Biesheuvel (void *)addr); 434f4f75ad5SArd Biesheuvel if (status != EFI_SUCCESS) { 435f4f75ad5SArd Biesheuvel pr_efi_err(sys_table_arg, "Failed to read file\n"); 436f4f75ad5SArd Biesheuvel goto free_file_total; 437f4f75ad5SArd Biesheuvel } 438f4f75ad5SArd Biesheuvel addr += chunksize; 439f4f75ad5SArd Biesheuvel size -= chunksize; 440f4f75ad5SArd Biesheuvel } 441f4f75ad5SArd Biesheuvel 442f4f75ad5SArd Biesheuvel efi_file_close(files[j].handle); 443f4f75ad5SArd Biesheuvel } 444f4f75ad5SArd Biesheuvel 445f4f75ad5SArd Biesheuvel } 446f4f75ad5SArd Biesheuvel 447f4f75ad5SArd Biesheuvel efi_call_early(free_pool, files); 448f4f75ad5SArd Biesheuvel 449f4f75ad5SArd Biesheuvel *load_addr = file_addr; 450f4f75ad5SArd Biesheuvel *load_size = file_size_total; 451f4f75ad5SArd Biesheuvel 452f4f75ad5SArd Biesheuvel return status; 453f4f75ad5SArd Biesheuvel 454f4f75ad5SArd Biesheuvel free_file_total: 455f4f75ad5SArd Biesheuvel efi_free(sys_table_arg, file_size_total, file_addr); 456f4f75ad5SArd Biesheuvel 457f4f75ad5SArd Biesheuvel close_handles: 458f4f75ad5SArd Biesheuvel for (k = j; k < i; k++) 459f4f75ad5SArd Biesheuvel efi_file_close(files[k].handle); 460f4f75ad5SArd Biesheuvel free_files: 461f4f75ad5SArd Biesheuvel efi_call_early(free_pool, files); 462f4f75ad5SArd Biesheuvel fail: 463f4f75ad5SArd Biesheuvel *load_addr = 0; 464f4f75ad5SArd Biesheuvel *load_size = 0; 465f4f75ad5SArd Biesheuvel 466f4f75ad5SArd Biesheuvel return status; 467f4f75ad5SArd Biesheuvel } 468f4f75ad5SArd Biesheuvel /* 469f4f75ad5SArd Biesheuvel * Relocate a kernel image, either compressed or uncompressed. 470f4f75ad5SArd Biesheuvel * In the ARM64 case, all kernel images are currently 471f4f75ad5SArd Biesheuvel * uncompressed, and as such when we relocate it we need to 472f4f75ad5SArd Biesheuvel * allocate additional space for the BSS segment. Any low 473f4f75ad5SArd Biesheuvel * memory that this function should avoid needs to be 474f4f75ad5SArd Biesheuvel * unavailable in the EFI memory map, as if the preferred 475f4f75ad5SArd Biesheuvel * address is not available the lowest available address will 476f4f75ad5SArd Biesheuvel * be used. 477f4f75ad5SArd Biesheuvel */ 478f4f75ad5SArd Biesheuvel efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg, 479f4f75ad5SArd Biesheuvel unsigned long *image_addr, 480f4f75ad5SArd Biesheuvel unsigned long image_size, 481f4f75ad5SArd Biesheuvel unsigned long alloc_size, 482f4f75ad5SArd Biesheuvel unsigned long preferred_addr, 483f4f75ad5SArd Biesheuvel unsigned long alignment) 484f4f75ad5SArd Biesheuvel { 485f4f75ad5SArd Biesheuvel unsigned long cur_image_addr; 486f4f75ad5SArd Biesheuvel unsigned long new_addr = 0; 487f4f75ad5SArd Biesheuvel efi_status_t status; 488f4f75ad5SArd Biesheuvel unsigned long nr_pages; 489f4f75ad5SArd Biesheuvel efi_physical_addr_t efi_addr = preferred_addr; 490f4f75ad5SArd Biesheuvel 491f4f75ad5SArd Biesheuvel if (!image_addr || !image_size || !alloc_size) 492f4f75ad5SArd Biesheuvel return EFI_INVALID_PARAMETER; 493f4f75ad5SArd Biesheuvel if (alloc_size < image_size) 494f4f75ad5SArd Biesheuvel return EFI_INVALID_PARAMETER; 495f4f75ad5SArd Biesheuvel 496f4f75ad5SArd Biesheuvel cur_image_addr = *image_addr; 497f4f75ad5SArd Biesheuvel 498f4f75ad5SArd Biesheuvel /* 499f4f75ad5SArd Biesheuvel * The EFI firmware loader could have placed the kernel image 500f4f75ad5SArd Biesheuvel * anywhere in memory, but the kernel has restrictions on the 501f4f75ad5SArd Biesheuvel * max physical address it can run at. Some architectures 502f4f75ad5SArd Biesheuvel * also have a prefered address, so first try to relocate 503f4f75ad5SArd Biesheuvel * to the preferred address. If that fails, allocate as low 504f4f75ad5SArd Biesheuvel * as possible while respecting the required alignment. 505f4f75ad5SArd Biesheuvel */ 506f4f75ad5SArd Biesheuvel nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; 507f4f75ad5SArd Biesheuvel status = efi_call_early(allocate_pages, 508f4f75ad5SArd Biesheuvel EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, 509f4f75ad5SArd Biesheuvel nr_pages, &efi_addr); 510f4f75ad5SArd Biesheuvel new_addr = efi_addr; 511f4f75ad5SArd Biesheuvel /* 512f4f75ad5SArd Biesheuvel * If preferred address allocation failed allocate as low as 513f4f75ad5SArd Biesheuvel * possible. 514f4f75ad5SArd Biesheuvel */ 515f4f75ad5SArd Biesheuvel if (status != EFI_SUCCESS) { 516f4f75ad5SArd Biesheuvel status = efi_low_alloc(sys_table_arg, alloc_size, alignment, 517f4f75ad5SArd Biesheuvel &new_addr); 518f4f75ad5SArd Biesheuvel } 519f4f75ad5SArd Biesheuvel if (status != EFI_SUCCESS) { 520f4f75ad5SArd Biesheuvel pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n"); 521f4f75ad5SArd Biesheuvel return status; 522f4f75ad5SArd Biesheuvel } 523f4f75ad5SArd Biesheuvel 524f4f75ad5SArd Biesheuvel /* 525f4f75ad5SArd Biesheuvel * We know source/dest won't overlap since both memory ranges 526f4f75ad5SArd Biesheuvel * have been allocated by UEFI, so we can safely use memcpy. 527f4f75ad5SArd Biesheuvel */ 528f4f75ad5SArd Biesheuvel memcpy((void *)new_addr, (void *)cur_image_addr, image_size); 529f4f75ad5SArd Biesheuvel 530f4f75ad5SArd Biesheuvel /* Return the new address of the relocated image. */ 531f4f75ad5SArd Biesheuvel *image_addr = new_addr; 532f4f75ad5SArd Biesheuvel 533f4f75ad5SArd Biesheuvel return status; 534f4f75ad5SArd Biesheuvel } 535f4f75ad5SArd Biesheuvel 536f4f75ad5SArd Biesheuvel /* 537f4f75ad5SArd Biesheuvel * Get the number of UTF-8 bytes corresponding to an UTF-16 character. 538f4f75ad5SArd Biesheuvel * This overestimates for surrogates, but that is okay. 539f4f75ad5SArd Biesheuvel */ 540f4f75ad5SArd Biesheuvel static int efi_utf8_bytes(u16 c) 541f4f75ad5SArd Biesheuvel { 542f4f75ad5SArd Biesheuvel return 1 + (c >= 0x80) + (c >= 0x800); 543f4f75ad5SArd Biesheuvel } 544f4f75ad5SArd Biesheuvel 545f4f75ad5SArd Biesheuvel /* 546f4f75ad5SArd Biesheuvel * Convert an UTF-16 string, not necessarily null terminated, to UTF-8. 547f4f75ad5SArd Biesheuvel */ 548f4f75ad5SArd Biesheuvel static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n) 549f4f75ad5SArd Biesheuvel { 550f4f75ad5SArd Biesheuvel unsigned int c; 551f4f75ad5SArd Biesheuvel 552f4f75ad5SArd Biesheuvel while (n--) { 553f4f75ad5SArd Biesheuvel c = *src++; 554f4f75ad5SArd Biesheuvel if (n && c >= 0xd800 && c <= 0xdbff && 555f4f75ad5SArd Biesheuvel *src >= 0xdc00 && *src <= 0xdfff) { 556f4f75ad5SArd Biesheuvel c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff); 557f4f75ad5SArd Biesheuvel src++; 558f4f75ad5SArd Biesheuvel n--; 559f4f75ad5SArd Biesheuvel } 560f4f75ad5SArd Biesheuvel if (c >= 0xd800 && c <= 0xdfff) 561f4f75ad5SArd Biesheuvel c = 0xfffd; /* Unmatched surrogate */ 562f4f75ad5SArd Biesheuvel if (c < 0x80) { 563f4f75ad5SArd Biesheuvel *dst++ = c; 564f4f75ad5SArd Biesheuvel continue; 565f4f75ad5SArd Biesheuvel } 566f4f75ad5SArd Biesheuvel if (c < 0x800) { 567f4f75ad5SArd Biesheuvel *dst++ = 0xc0 + (c >> 6); 568f4f75ad5SArd Biesheuvel goto t1; 569f4f75ad5SArd Biesheuvel } 570f4f75ad5SArd Biesheuvel if (c < 0x10000) { 571f4f75ad5SArd Biesheuvel *dst++ = 0xe0 + (c >> 12); 572f4f75ad5SArd Biesheuvel goto t2; 573f4f75ad5SArd Biesheuvel } 574f4f75ad5SArd Biesheuvel *dst++ = 0xf0 + (c >> 18); 575f4f75ad5SArd Biesheuvel *dst++ = 0x80 + ((c >> 12) & 0x3f); 576f4f75ad5SArd Biesheuvel t2: 577f4f75ad5SArd Biesheuvel *dst++ = 0x80 + ((c >> 6) & 0x3f); 578f4f75ad5SArd Biesheuvel t1: 579f4f75ad5SArd Biesheuvel *dst++ = 0x80 + (c & 0x3f); 580f4f75ad5SArd Biesheuvel } 581f4f75ad5SArd Biesheuvel 582f4f75ad5SArd Biesheuvel return dst; 583f4f75ad5SArd Biesheuvel } 584f4f75ad5SArd Biesheuvel 585f4f75ad5SArd Biesheuvel /* 586f4f75ad5SArd Biesheuvel * Convert the unicode UEFI command line to ASCII to pass to kernel. 587f4f75ad5SArd Biesheuvel * Size of memory allocated return in *cmd_line_len. 588f4f75ad5SArd Biesheuvel * Returns NULL on error. 589f4f75ad5SArd Biesheuvel */ 590f4f75ad5SArd Biesheuvel char *efi_convert_cmdline(efi_system_table_t *sys_table_arg, 591f4f75ad5SArd Biesheuvel efi_loaded_image_t *image, 592f4f75ad5SArd Biesheuvel int *cmd_line_len) 593f4f75ad5SArd Biesheuvel { 594f4f75ad5SArd Biesheuvel const u16 *s2; 595f4f75ad5SArd Biesheuvel u8 *s1 = NULL; 596f4f75ad5SArd Biesheuvel unsigned long cmdline_addr = 0; 597f4f75ad5SArd Biesheuvel int load_options_chars = image->load_options_size / 2; /* UTF-16 */ 598f4f75ad5SArd Biesheuvel const u16 *options = image->load_options; 599f4f75ad5SArd Biesheuvel int options_bytes = 0; /* UTF-8 bytes */ 600f4f75ad5SArd Biesheuvel int options_chars = 0; /* UTF-16 chars */ 601f4f75ad5SArd Biesheuvel efi_status_t status; 602f4f75ad5SArd Biesheuvel u16 zero = 0; 603f4f75ad5SArd Biesheuvel 604f4f75ad5SArd Biesheuvel if (options) { 605f4f75ad5SArd Biesheuvel s2 = options; 606f4f75ad5SArd Biesheuvel while (*s2 && *s2 != '\n' 607f4f75ad5SArd Biesheuvel && options_chars < load_options_chars) { 608f4f75ad5SArd Biesheuvel options_bytes += efi_utf8_bytes(*s2++); 609f4f75ad5SArd Biesheuvel options_chars++; 610f4f75ad5SArd Biesheuvel } 611f4f75ad5SArd Biesheuvel } 612f4f75ad5SArd Biesheuvel 613f4f75ad5SArd Biesheuvel if (!options_chars) { 614f4f75ad5SArd Biesheuvel /* No command line options, so return empty string*/ 615f4f75ad5SArd Biesheuvel options = &zero; 616f4f75ad5SArd Biesheuvel } 617f4f75ad5SArd Biesheuvel 618f4f75ad5SArd Biesheuvel options_bytes++; /* NUL termination */ 619f4f75ad5SArd Biesheuvel 620f4f75ad5SArd Biesheuvel status = efi_low_alloc(sys_table_arg, options_bytes, 0, &cmdline_addr); 621f4f75ad5SArd Biesheuvel if (status != EFI_SUCCESS) 622f4f75ad5SArd Biesheuvel return NULL; 623f4f75ad5SArd Biesheuvel 624f4f75ad5SArd Biesheuvel s1 = (u8 *)cmdline_addr; 625f4f75ad5SArd Biesheuvel s2 = (const u16 *)options; 626f4f75ad5SArd Biesheuvel 627f4f75ad5SArd Biesheuvel s1 = efi_utf16_to_utf8(s1, s2, options_chars); 628f4f75ad5SArd Biesheuvel *s1 = '\0'; 629f4f75ad5SArd Biesheuvel 630f4f75ad5SArd Biesheuvel *cmd_line_len = options_bytes; 631f4f75ad5SArd Biesheuvel return (char *)cmdline_addr; 632f4f75ad5SArd Biesheuvel } 633