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