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