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