blob: fda510fa357941f3c83b418ff0e45d93fdf70e9e [file] [log] [blame]
Roy Franz7721da42013-09-22 15:45:27 -07001/*
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
14struct initrd {
15 efi_file_handle_t *handle;
16 u64 size;
17};
18
19
20
21
Roy Franz876dc362013-09-22 15:45:28 -070022static void efi_char16_printk(efi_system_table_t *sys_table_arg,
23 efi_char16_t *str)
Roy Franz7721da42013-09-22 15:45:27 -070024{
25 struct efi_simple_text_output_protocol *out;
26
Roy Franz876dc362013-09-22 15:45:28 -070027 out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
Roy Franz7721da42013-09-22 15:45:27 -070028 efi_call_phys2(out->output_string, out, str);
29}
30
Roy Franz876dc362013-09-22 15:45:28 -070031static void efi_printk(efi_system_table_t *sys_table_arg, char *str)
Roy Franz7721da42013-09-22 15:45:27 -070032{
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 };
Roy Franz876dc362013-09-22 15:45:28 -070041 efi_char16_printk(sys_table_arg, nl);
Roy Franz7721da42013-09-22 15:45:27 -070042 }
43
Roy Franz876dc362013-09-22 15:45:28 -070044 efi_char16_printk(sys_table_arg, ch);
Roy Franz7721da42013-09-22 15:45:27 -070045 }
46}
47
48
Roy Franz86cc6532013-09-22 15:45:35 -070049static 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,
Roy Franz1c089c62013-09-22 15:45:36 -070052 unsigned long *desc_size,
53 u32 *desc_ver,
54 unsigned long *key_ptr)
Roy Franz7721da42013-09-22 15:45:27 -070055{
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;
62again:
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);
Roy Franz876dc362013-09-22 15:45:28 -070068 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
Roy Franz7721da42013-09-22 15:45:27 -070069 EFI_LOADER_DATA, *map_size, (void **)&m);
70 if (status != EFI_SUCCESS)
71 goto fail;
72
Roy Franz876dc362013-09-22 15:45:28 -070073 status = efi_call_phys5(sys_table_arg->boottime->get_memory_map,
74 map_size, m, &key, desc_size, &desc_version);
Roy Franz7721da42013-09-22 15:45:27 -070075 if (status == EFI_BUFFER_TOO_SMALL) {
Roy Franz876dc362013-09-22 15:45:28 -070076 efi_call_phys1(sys_table_arg->boottime->free_pool, m);
Roy Franz7721da42013-09-22 15:45:27 -070077 goto again;
78 }
79
80 if (status != EFI_SUCCESS)
Roy Franz876dc362013-09-22 15:45:28 -070081 efi_call_phys1(sys_table_arg->boottime->free_pool, m);
Roy Franz1c089c62013-09-22 15:45:36 -070082 if (key_ptr && status == EFI_SUCCESS)
83 *key_ptr = key;
84 if (desc_ver && status == EFI_SUCCESS)
85 *desc_ver = desc_version;
Roy Franz7721da42013-09-22 15:45:27 -070086
87fail:
88 *map = m;
89 return status;
90}
91
92/*
93 * Allocate at the highest possible address that is not above 'max'.
94 */
Roy Franz40e45302013-09-22 15:45:29 -070095static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
Roy Franz876dc362013-09-22 15:45:28 -070096 unsigned long size, unsigned long align,
97 unsigned long *addr, unsigned long max)
Roy Franz7721da42013-09-22 15:45:27 -070098{
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
Roy Franz1c089c62013-09-22 15:45:36 -0700106 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
107 NULL, NULL);
Roy Franz7721da42013-09-22 15:45:27 -0700108 if (status != EFI_SUCCESS)
109 goto fail;
110
Roy Franz38dd9c02013-09-22 15:45:30 -0700111 /*
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
Roy Franz7721da42013-09-22 15:45:27 -0700119 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
120again:
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 {
Roy Franz876dc362013-09-22 15:45:28 -0700161 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
Roy Franz7721da42013-09-22 15:45:27 -0700162 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
173free_pool:
Roy Franz876dc362013-09-22 15:45:28 -0700174 efi_call_phys1(sys_table_arg->boottime->free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700175
176fail:
177 return status;
178}
179
180/*
181 * Allocate at the lowest possible address.
182 */
Roy Franz40e45302013-09-22 15:45:29 -0700183static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
184 unsigned long size, unsigned long align,
Roy Franz7721da42013-09-22 15:45:27 -0700185 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
Roy Franz1c089c62013-09-22 15:45:36 -0700193 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
194 NULL, NULL);
Roy Franz7721da42013-09-22 15:45:27 -0700195 if (status != EFI_SUCCESS)
196 goto fail;
197
Roy Franz38dd9c02013-09-22 15:45:30 -0700198 /*
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
Roy Franz7721da42013-09-22 15:45:27 -0700206 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
Roy Franz876dc362013-09-22 15:45:28 -0700235 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
Roy Franz7721da42013-09-22 15:45:27 -0700236 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
247free_pool:
Roy Franz876dc362013-09-22 15:45:28 -0700248 efi_call_phys1(sys_table_arg->boottime->free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700249fail:
250 return status;
251}
252
Roy Franz40e45302013-09-22 15:45:29 -0700253static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
Roy Franz876dc362013-09-22 15:45:28 -0700254 unsigned long addr)
Roy Franz7721da42013-09-22 15:45:27 -0700255{
256 unsigned long nr_pages;
257
Roy Franz0e1cadb2013-09-22 15:45:38 -0700258 if (!size)
259 return;
260
Roy Franz7721da42013-09-22 15:45:27 -0700261 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
Roy Franz876dc362013-09-22 15:45:28 -0700262 efi_call_phys2(sys_table_arg->boottime->free_pages, addr, nr_pages);
Roy Franz7721da42013-09-22 15:45:27 -0700263}
264
265
266/*
267 * Check the cmdline for a LILO-style initrd= arguments.
268 *
269 * We only support loading an initrd from the same filesystem as the
270 * kernel image.
271 */
Roy Franz876dc362013-09-22 15:45:28 -0700272static efi_status_t handle_ramdisks(efi_system_table_t *sys_table_arg,
273 efi_loaded_image_t *image,
Roy Franz7721da42013-09-22 15:45:27 -0700274 struct setup_header *hdr)
275{
276 struct initrd *initrds;
277 unsigned long initrd_addr;
278 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
279 u64 initrd_total;
280 efi_file_io_interface_t *io;
281 efi_file_handle_t *fh;
282 efi_status_t status;
283 int nr_initrds;
284 char *str;
285 int i, j, k;
286
287 initrd_addr = 0;
288 initrd_total = 0;
289
290 str = (char *)(unsigned long)hdr->cmd_line_ptr;
291
292 j = 0; /* See close_handles */
293
294 if (!str || !*str)
295 return EFI_SUCCESS;
296
297 for (nr_initrds = 0; *str; nr_initrds++) {
298 str = strstr(str, "initrd=");
299 if (!str)
300 break;
301
302 str += 7;
303
304 /* Skip any leading slashes */
305 while (*str == '/' || *str == '\\')
306 str++;
307
308 while (*str && *str != ' ' && *str != '\n')
309 str++;
310 }
311
312 if (!nr_initrds)
313 return EFI_SUCCESS;
314
Roy Franz876dc362013-09-22 15:45:28 -0700315 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
Roy Franz7721da42013-09-22 15:45:27 -0700316 EFI_LOADER_DATA,
317 nr_initrds * sizeof(*initrds),
318 &initrds);
319 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700320 efi_printk(sys_table_arg, "Failed to alloc mem for initrds\n");
Roy Franz7721da42013-09-22 15:45:27 -0700321 goto fail;
322 }
323
324 str = (char *)(unsigned long)hdr->cmd_line_ptr;
325 for (i = 0; i < nr_initrds; i++) {
326 struct initrd *initrd;
327 efi_file_handle_t *h;
328 efi_file_info_t *info;
329 efi_char16_t filename_16[256];
330 unsigned long info_sz;
331 efi_guid_t info_guid = EFI_FILE_INFO_ID;
332 efi_char16_t *p;
333 u64 file_sz;
334
335 str = strstr(str, "initrd=");
336 if (!str)
337 break;
338
339 str += 7;
340
341 initrd = &initrds[i];
342 p = filename_16;
343
344 /* Skip any leading slashes */
345 while (*str == '/' || *str == '\\')
346 str++;
347
348 while (*str && *str != ' ' && *str != '\n') {
349 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
350 break;
351
352 if (*str == '/') {
353 *p++ = '\\';
354 *str++;
355 } else {
356 *p++ = *str++;
357 }
358 }
359
360 *p = '\0';
361
362 /* Only open the volume once. */
363 if (!i) {
364 efi_boot_services_t *boottime;
365
Roy Franz876dc362013-09-22 15:45:28 -0700366 boottime = sys_table_arg->boottime;
Roy Franz7721da42013-09-22 15:45:27 -0700367
368 status = efi_call_phys3(boottime->handle_protocol,
369 image->device_handle, &fs_proto, &io);
370 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700371 efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
Roy Franz7721da42013-09-22 15:45:27 -0700372 goto free_initrds;
373 }
374
375 status = efi_call_phys2(io->open_volume, io, &fh);
376 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700377 efi_printk(sys_table_arg, "Failed to open volume\n");
Roy Franz7721da42013-09-22 15:45:27 -0700378 goto free_initrds;
379 }
380 }
381
382 status = efi_call_phys5(fh->open, fh, &h, filename_16,
383 EFI_FILE_MODE_READ, (u64)0);
384 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700385 efi_printk(sys_table_arg, "Failed to open initrd file: ");
386 efi_char16_printk(sys_table_arg, filename_16);
387 efi_printk(sys_table_arg, "\n");
Roy Franz7721da42013-09-22 15:45:27 -0700388 goto close_handles;
389 }
390
391 initrd->handle = h;
392
393 info_sz = 0;
394 status = efi_call_phys4(h->get_info, h, &info_guid,
395 &info_sz, NULL);
396 if (status != EFI_BUFFER_TOO_SMALL) {
Roy Franz876dc362013-09-22 15:45:28 -0700397 efi_printk(sys_table_arg, "Failed to get initrd info size\n");
Roy Franz7721da42013-09-22 15:45:27 -0700398 goto close_handles;
399 }
400
401grow:
Roy Franz876dc362013-09-22 15:45:28 -0700402 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
Roy Franz7721da42013-09-22 15:45:27 -0700403 EFI_LOADER_DATA, info_sz, &info);
404 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700405 efi_printk(sys_table_arg, "Failed to alloc mem for initrd info\n");
Roy Franz7721da42013-09-22 15:45:27 -0700406 goto close_handles;
407 }
408
409 status = efi_call_phys4(h->get_info, h, &info_guid,
410 &info_sz, info);
411 if (status == EFI_BUFFER_TOO_SMALL) {
Roy Franz876dc362013-09-22 15:45:28 -0700412 efi_call_phys1(sys_table_arg->boottime->free_pool,
413 info);
Roy Franz7721da42013-09-22 15:45:27 -0700414 goto grow;
415 }
416
417 file_sz = info->file_size;
Roy Franz876dc362013-09-22 15:45:28 -0700418 efi_call_phys1(sys_table_arg->boottime->free_pool, info);
Roy Franz7721da42013-09-22 15:45:27 -0700419
420 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700421 efi_printk(sys_table_arg, "Failed to get initrd info\n");
Roy Franz7721da42013-09-22 15:45:27 -0700422 goto close_handles;
423 }
424
425 initrd->size = file_sz;
426 initrd_total += file_sz;
427 }
428
429 if (initrd_total) {
430 unsigned long addr;
431
432 /*
433 * Multiple initrd's need to be at consecutive
434 * addresses in memory, so allocate enough memory for
435 * all the initrd's.
436 */
Roy Franz40e45302013-09-22 15:45:29 -0700437 status = efi_high_alloc(sys_table_arg, initrd_total, 0x1000,
438 &initrd_addr, hdr->initrd_addr_max);
Roy Franz7721da42013-09-22 15:45:27 -0700439 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700440 efi_printk(sys_table_arg, "Failed to alloc highmem for initrds\n");
Roy Franz7721da42013-09-22 15:45:27 -0700441 goto close_handles;
442 }
443
444 /* We've run out of free low memory. */
445 if (initrd_addr > hdr->initrd_addr_max) {
Roy Franz876dc362013-09-22 15:45:28 -0700446 efi_printk(sys_table_arg, "We've run out of free low memory\n");
Roy Franz7721da42013-09-22 15:45:27 -0700447 status = EFI_INVALID_PARAMETER;
448 goto free_initrd_total;
449 }
450
451 addr = initrd_addr;
452 for (j = 0; j < nr_initrds; j++) {
453 u64 size;
454
455 size = initrds[j].size;
456 while (size) {
457 u64 chunksize;
458 if (size > EFI_READ_CHUNK_SIZE)
459 chunksize = EFI_READ_CHUNK_SIZE;
460 else
461 chunksize = size;
462 status = efi_call_phys3(fh->read,
463 initrds[j].handle,
464 &chunksize, addr);
465 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700466 efi_printk(sys_table_arg, "Failed to read initrd\n");
Roy Franz7721da42013-09-22 15:45:27 -0700467 goto free_initrd_total;
468 }
469 addr += chunksize;
470 size -= chunksize;
471 }
472
473 efi_call_phys1(fh->close, initrds[j].handle);
474 }
475
476 }
477
Roy Franz876dc362013-09-22 15:45:28 -0700478 efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
Roy Franz7721da42013-09-22 15:45:27 -0700479
480 hdr->ramdisk_image = initrd_addr;
481 hdr->ramdisk_size = initrd_total;
482
483 return status;
484
485free_initrd_total:
Roy Franz40e45302013-09-22 15:45:29 -0700486 efi_free(sys_table_arg, initrd_total, initrd_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700487
488close_handles:
489 for (k = j; k < i; k++)
490 efi_call_phys1(fh->close, initrds[k].handle);
491free_initrds:
Roy Franz876dc362013-09-22 15:45:28 -0700492 efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
Roy Franz7721da42013-09-22 15:45:27 -0700493fail:
494 hdr->ramdisk_image = 0;
495 hdr->ramdisk_size = 0;
496
497 return status;
498}
Roy Franz4a9f3a72013-09-22 15:45:32 -0700499/*
500 * Relocate a kernel image, either compressed or uncompressed.
501 * In the ARM64 case, all kernel images are currently
502 * uncompressed, and as such when we relocate it we need to
503 * allocate additional space for the BSS segment. Any low
504 * memory that this function should avoid needs to be
505 * unavailable in the EFI memory map, as if the preferred
506 * address is not available the lowest available address will
507 * be used.
508 */
509static efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
510 unsigned long *image_addr,
511 unsigned long image_size,
512 unsigned long alloc_size,
513 unsigned long preferred_addr,
514 unsigned long alignment)
Roy Franzc6866d72013-09-22 15:45:31 -0700515{
Roy Franz4a9f3a72013-09-22 15:45:32 -0700516 unsigned long cur_image_addr;
517 unsigned long new_addr = 0;
Roy Franzc6866d72013-09-22 15:45:31 -0700518 efi_status_t status;
Roy Franz4a9f3a72013-09-22 15:45:32 -0700519 unsigned long nr_pages;
520 efi_physical_addr_t efi_addr = preferred_addr;
521
522 if (!image_addr || !image_size || !alloc_size)
523 return EFI_INVALID_PARAMETER;
524 if (alloc_size < image_size)
525 return EFI_INVALID_PARAMETER;
526
527 cur_image_addr = *image_addr;
Roy Franzc6866d72013-09-22 15:45:31 -0700528
529 /*
530 * The EFI firmware loader could have placed the kernel image
Roy Franz4a9f3a72013-09-22 15:45:32 -0700531 * anywhere in memory, but the kernel has restrictions on the
532 * max physical address it can run at. Some architectures
533 * also have a prefered address, so first try to relocate
534 * to the preferred address. If that fails, allocate as low
535 * as possible while respecting the required alignment.
536 */
537 nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
538 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
539 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
540 nr_pages, &efi_addr);
541 new_addr = efi_addr;
542 /*
543 * If preferred address allocation failed allocate as low as
Roy Franzc6866d72013-09-22 15:45:31 -0700544 * possible.
545 */
Roy Franzc6866d72013-09-22 15:45:31 -0700546 if (status != EFI_SUCCESS) {
Roy Franz4a9f3a72013-09-22 15:45:32 -0700547 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
548 &new_addr);
549 }
550 if (status != EFI_SUCCESS) {
551 efi_printk(sys_table_arg, "ERROR: Failed to allocate usable memory for kernel.\n");
552 return status;
Roy Franzc6866d72013-09-22 15:45:31 -0700553 }
554
Roy Franz4a9f3a72013-09-22 15:45:32 -0700555 /*
556 * We know source/dest won't overlap since both memory ranges
557 * have been allocated by UEFI, so we can safely use memcpy.
558 */
559 memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
560 /* Zero any extra space we may have allocated for BSS. */
561 memset((void *)(new_addr + image_size), alloc_size - image_size, 0);
Roy Franzc6866d72013-09-22 15:45:31 -0700562
Roy Franz4a9f3a72013-09-22 15:45:32 -0700563 /* Return the new address of the relocated image. */
564 *image_addr = new_addr;
Roy Franzc6866d72013-09-22 15:45:31 -0700565
566 return status;
567}
Roy Franz5fef3872013-09-22 15:45:33 -0700568
569/*
570 * Convert the unicode UEFI command line to ASCII to pass to kernel.
571 * Size of memory allocated return in *cmd_line_len.
572 * Returns NULL on error.
573 */
574static char *efi_convert_cmdline_to_ascii(efi_system_table_t *sys_table_arg,
575 efi_loaded_image_t *image,
576 int *cmd_line_len)
577{
578 u16 *s2;
579 u8 *s1 = NULL;
580 unsigned long cmdline_addr = 0;
581 int load_options_size = image->load_options_size / 2; /* ASCII */
582 void *options = image->load_options;
583 int options_size = 0;
584 efi_status_t status;
585 int i;
586 u16 zero = 0;
587
588 if (options) {
589 s2 = options;
590 while (*s2 && *s2 != '\n' && options_size < load_options_size) {
591 s2++;
592 options_size++;
593 }
594 }
595
596 if (options_size == 0) {
597 /* No command line options, so return empty string*/
598 options_size = 1;
599 options = &zero;
600 }
601
602 options_size++; /* NUL termination */
603#ifdef CONFIG_ARM
604 /*
605 * For ARM, allocate at a high address to avoid reserved
606 * regions at low addresses that we don't know the specfics of
607 * at the time we are processing the command line.
608 */
609 status = efi_high_alloc(sys_table_arg, options_size, 0,
610 &cmdline_addr, 0xfffff000);
611#else
612 status = efi_low_alloc(sys_table_arg, options_size, 0,
613 &cmdline_addr);
614#endif
615 if (status != EFI_SUCCESS)
616 return NULL;
617
618 s1 = (u8 *)cmdline_addr;
619 s2 = (u16 *)options;
620
621 for (i = 0; i < options_size - 1; i++)
622 *s1++ = *s2++;
623
624 *s1 = '\0';
625
626 *cmd_line_len = options_size;
627 return (char *)cmdline_addr;
628}