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