blob: a0282872d97dc0f7050c412758996643ee26e00f [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
Roy Franz36f89612013-09-22 15:45:40 -070014struct file_info {
Roy Franz7721da42013-09-22 15:45:27 -070015 efi_file_handle_t *handle;
16 u64 size;
17};
18
Roy Franz876dc362013-09-22 15:45:28 -070019static void efi_printk(efi_system_table_t *sys_table_arg, char *str)
Roy Franz7721da42013-09-22 15:45:27 -070020{
21 char *s8;
22
23 for (s8 = str; *s8; s8++) {
24 efi_char16_t ch[2] = { 0 };
25
26 ch[0] = *s8;
27 if (*s8 == '\n') {
28 efi_char16_t nl[2] = { '\r', 0 };
Roy Franz876dc362013-09-22 15:45:28 -070029 efi_char16_printk(sys_table_arg, nl);
Roy Franz7721da42013-09-22 15:45:27 -070030 }
31
Roy Franz876dc362013-09-22 15:45:28 -070032 efi_char16_printk(sys_table_arg, ch);
Roy Franz7721da42013-09-22 15:45:27 -070033 }
34}
35
36
Roy Franz86cc6532013-09-22 15:45:35 -070037static efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
38 efi_memory_desc_t **map,
39 unsigned long *map_size,
Roy Franz1c089c62013-09-22 15:45:36 -070040 unsigned long *desc_size,
41 u32 *desc_ver,
42 unsigned long *key_ptr)
Roy Franz7721da42013-09-22 15:45:27 -070043{
44 efi_memory_desc_t *m = NULL;
45 efi_status_t status;
46 unsigned long key;
47 u32 desc_version;
48
49 *map_size = sizeof(*m) * 32;
50again:
51 /*
52 * Add an additional efi_memory_desc_t because we're doing an
53 * allocation which may be in a new descriptor region.
54 */
55 *map_size += sizeof(*m);
Matt Fleming54b52d82014-01-10 15:27:14 +000056 status = efi_early->call(efi_early->allocate_pool, EFI_LOADER_DATA,
57 *map_size, (void **)&m);
Roy Franz7721da42013-09-22 15:45:27 -070058 if (status != EFI_SUCCESS)
59 goto fail;
60
Matt Fleming54b52d82014-01-10 15:27:14 +000061 *desc_size = 0;
62 key = 0;
63 status = efi_early->call(efi_early->get_memory_map, map_size, m,
64 &key, desc_size, &desc_version);
Roy Franz7721da42013-09-22 15:45:27 -070065 if (status == EFI_BUFFER_TOO_SMALL) {
Matt Fleming54b52d82014-01-10 15:27:14 +000066 efi_early->call(efi_early->free_pool, m);
Roy Franz7721da42013-09-22 15:45:27 -070067 goto again;
68 }
69
70 if (status != EFI_SUCCESS)
Matt Fleming54b52d82014-01-10 15:27:14 +000071 efi_early->call(efi_early->free_pool, m);
72
Roy Franz1c089c62013-09-22 15:45:36 -070073 if (key_ptr && status == EFI_SUCCESS)
74 *key_ptr = key;
75 if (desc_ver && status == EFI_SUCCESS)
76 *desc_ver = desc_version;
Roy Franz7721da42013-09-22 15:45:27 -070077
78fail:
79 *map = m;
80 return status;
81}
82
83/*
84 * Allocate at the highest possible address that is not above 'max'.
85 */
Roy Franz40e45302013-09-22 15:45:29 -070086static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
Roy Franz876dc362013-09-22 15:45:28 -070087 unsigned long size, unsigned long align,
88 unsigned long *addr, unsigned long max)
Roy Franz7721da42013-09-22 15:45:27 -070089{
90 unsigned long map_size, desc_size;
91 efi_memory_desc_t *map;
92 efi_status_t status;
93 unsigned long nr_pages;
94 u64 max_addr = 0;
95 int i;
96
Roy Franz1c089c62013-09-22 15:45:36 -070097 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
98 NULL, NULL);
Roy Franz7721da42013-09-22 15:45:27 -070099 if (status != EFI_SUCCESS)
100 goto fail;
101
Roy Franz38dd9c02013-09-22 15:45:30 -0700102 /*
103 * Enforce minimum alignment that EFI requires when requesting
104 * a specific address. We are doing page-based allocations,
105 * so we must be aligned to a page.
106 */
107 if (align < EFI_PAGE_SIZE)
108 align = EFI_PAGE_SIZE;
109
Roy Franz7721da42013-09-22 15:45:27 -0700110 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
111again:
112 for (i = 0; i < map_size / desc_size; i++) {
113 efi_memory_desc_t *desc;
114 unsigned long m = (unsigned long)map;
115 u64 start, end;
116
117 desc = (efi_memory_desc_t *)(m + (i * desc_size));
118 if (desc->type != EFI_CONVENTIONAL_MEMORY)
119 continue;
120
121 if (desc->num_pages < nr_pages)
122 continue;
123
124 start = desc->phys_addr;
125 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
126
127 if ((start + size) > end || (start + size) > max)
128 continue;
129
130 if (end - size > max)
131 end = max;
132
133 if (round_down(end - size, align) < start)
134 continue;
135
136 start = round_down(end - size, align);
137
138 /*
139 * Don't allocate at 0x0. It will confuse code that
140 * checks pointers against NULL.
141 */
142 if (start == 0x0)
143 continue;
144
145 if (start > max_addr)
146 max_addr = start;
147 }
148
149 if (!max_addr)
150 status = EFI_NOT_FOUND;
151 else {
Matt Fleming54b52d82014-01-10 15:27:14 +0000152 status = efi_early->call(efi_early->allocate_pages,
153 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
154 nr_pages, &max_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700155 if (status != EFI_SUCCESS) {
156 max = max_addr;
157 max_addr = 0;
158 goto again;
159 }
160
161 *addr = max_addr;
162 }
163
Matt Fleming54b52d82014-01-10 15:27:14 +0000164 efi_early->call(efi_early->free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700165fail:
166 return status;
167}
168
169/*
170 * Allocate at the lowest possible address.
171 */
Roy Franz40e45302013-09-22 15:45:29 -0700172static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
173 unsigned long size, unsigned long align,
Roy Franz7721da42013-09-22 15:45:27 -0700174 unsigned long *addr)
175{
176 unsigned long map_size, desc_size;
177 efi_memory_desc_t *map;
178 efi_status_t status;
179 unsigned long nr_pages;
180 int i;
181
Roy Franz1c089c62013-09-22 15:45:36 -0700182 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
183 NULL, NULL);
Roy Franz7721da42013-09-22 15:45:27 -0700184 if (status != EFI_SUCCESS)
185 goto fail;
186
Roy Franz38dd9c02013-09-22 15:45:30 -0700187 /*
188 * Enforce minimum alignment that EFI requires when requesting
189 * a specific address. We are doing page-based allocations,
190 * so we must be aligned to a page.
191 */
192 if (align < EFI_PAGE_SIZE)
193 align = EFI_PAGE_SIZE;
194
Roy Franz7721da42013-09-22 15:45:27 -0700195 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
196 for (i = 0; i < map_size / desc_size; i++) {
197 efi_memory_desc_t *desc;
198 unsigned long m = (unsigned long)map;
199 u64 start, end;
200
201 desc = (efi_memory_desc_t *)(m + (i * desc_size));
202
203 if (desc->type != EFI_CONVENTIONAL_MEMORY)
204 continue;
205
206 if (desc->num_pages < nr_pages)
207 continue;
208
209 start = desc->phys_addr;
210 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
211
212 /*
213 * Don't allocate at 0x0. It will confuse code that
214 * checks pointers against NULL. Skip the first 8
215 * bytes so we start at a nice even number.
216 */
217 if (start == 0x0)
218 start += 8;
219
220 start = round_up(start, align);
221 if ((start + size) > end)
222 continue;
223
Matt Fleming54b52d82014-01-10 15:27:14 +0000224 status = efi_early->call(efi_early->allocate_pages,
225 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
226 nr_pages, &start);
Roy Franz7721da42013-09-22 15:45:27 -0700227 if (status == EFI_SUCCESS) {
228 *addr = start;
229 break;
230 }
231 }
232
233 if (i == map_size / desc_size)
234 status = EFI_NOT_FOUND;
235
Matt Fleming54b52d82014-01-10 15:27:14 +0000236 efi_early->call(efi_early->free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700237fail:
238 return status;
239}
240
Roy Franz40e45302013-09-22 15:45:29 -0700241static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
Roy Franz876dc362013-09-22 15:45:28 -0700242 unsigned long addr)
Roy Franz7721da42013-09-22 15:45:27 -0700243{
244 unsigned long nr_pages;
245
Roy Franz0e1cadb2013-09-22 15:45:38 -0700246 if (!size)
247 return;
248
Roy Franz7721da42013-09-22 15:45:27 -0700249 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
Matt Fleming54b52d82014-01-10 15:27:14 +0000250 efi_early->call(efi_early->free_pages, addr, nr_pages);
Roy Franz7721da42013-09-22 15:45:27 -0700251}
252
253
254/*
Roy Franz36f89612013-09-22 15:45:40 -0700255 * Check the cmdline for a LILO-style file= arguments.
Roy Franz7721da42013-09-22 15:45:27 -0700256 *
Roy Franz36f89612013-09-22 15:45:40 -0700257 * We only support loading a file from the same filesystem as
258 * the kernel image.
Roy Franz7721da42013-09-22 15:45:27 -0700259 */
Roy Franz46f45822013-09-22 15:45:39 -0700260static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
261 efi_loaded_image_t *image,
262 char *cmd_line, char *option_string,
263 unsigned long max_addr,
264 unsigned long *load_addr,
265 unsigned long *load_size)
Roy Franz7721da42013-09-22 15:45:27 -0700266{
Roy Franz36f89612013-09-22 15:45:40 -0700267 struct file_info *files;
268 unsigned long file_addr;
Roy Franz36f89612013-09-22 15:45:40 -0700269 u64 file_size_total;
Roy Franz7721da42013-09-22 15:45:27 -0700270 efi_file_handle_t *fh;
271 efi_status_t status;
Roy Franz36f89612013-09-22 15:45:40 -0700272 int nr_files;
Roy Franz7721da42013-09-22 15:45:27 -0700273 char *str;
274 int i, j, k;
275
Roy Franz36f89612013-09-22 15:45:40 -0700276 file_addr = 0;
277 file_size_total = 0;
Roy Franz7721da42013-09-22 15:45:27 -0700278
Roy Franz46f45822013-09-22 15:45:39 -0700279 str = cmd_line;
Roy Franz7721da42013-09-22 15:45:27 -0700280
281 j = 0; /* See close_handles */
282
Roy Franz46f45822013-09-22 15:45:39 -0700283 if (!load_addr || !load_size)
284 return EFI_INVALID_PARAMETER;
285
286 *load_addr = 0;
287 *load_size = 0;
288
Roy Franz7721da42013-09-22 15:45:27 -0700289 if (!str || !*str)
290 return EFI_SUCCESS;
291
Roy Franz36f89612013-09-22 15:45:40 -0700292 for (nr_files = 0; *str; nr_files++) {
Roy Franz46f45822013-09-22 15:45:39 -0700293 str = strstr(str, option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700294 if (!str)
295 break;
296
Roy Franz46f45822013-09-22 15:45:39 -0700297 str += strlen(option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700298
299 /* Skip any leading slashes */
300 while (*str == '/' || *str == '\\')
301 str++;
302
303 while (*str && *str != ' ' && *str != '\n')
304 str++;
305 }
306
Roy Franz36f89612013-09-22 15:45:40 -0700307 if (!nr_files)
Roy Franz7721da42013-09-22 15:45:27 -0700308 return EFI_SUCCESS;
309
Matt Fleming54b52d82014-01-10 15:27:14 +0000310 status = efi_early->call(efi_early->allocate_pool, EFI_LOADER_DATA,
311 nr_files * sizeof(*files), (void **)&files);
Roy Franz7721da42013-09-22 15:45:27 -0700312 if (status != EFI_SUCCESS) {
Roy Franz36f89612013-09-22 15:45:40 -0700313 efi_printk(sys_table_arg, "Failed to alloc mem for file handle list\n");
Roy Franz7721da42013-09-22 15:45:27 -0700314 goto fail;
315 }
316
Roy Franz46f45822013-09-22 15:45:39 -0700317 str = cmd_line;
Roy Franz36f89612013-09-22 15:45:40 -0700318 for (i = 0; i < nr_files; i++) {
319 struct file_info *file;
Roy Franz7721da42013-09-22 15:45:27 -0700320 efi_char16_t filename_16[256];
Roy Franz7721da42013-09-22 15:45:27 -0700321 efi_char16_t *p;
Roy Franz7721da42013-09-22 15:45:27 -0700322
Roy Franz46f45822013-09-22 15:45:39 -0700323 str = strstr(str, option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700324 if (!str)
325 break;
326
Roy Franz46f45822013-09-22 15:45:39 -0700327 str += strlen(option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700328
Roy Franz36f89612013-09-22 15:45:40 -0700329 file = &files[i];
Roy Franz7721da42013-09-22 15:45:27 -0700330 p = filename_16;
331
332 /* Skip any leading slashes */
333 while (*str == '/' || *str == '\\')
334 str++;
335
336 while (*str && *str != ' ' && *str != '\n') {
337 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
338 break;
339
340 if (*str == '/') {
341 *p++ = '\\';
Roy Franz4e283082013-09-22 15:45:42 -0700342 str++;
Roy Franz7721da42013-09-22 15:45:27 -0700343 } else {
344 *p++ = *str++;
345 }
346 }
347
348 *p = '\0';
349
350 /* Only open the volume once. */
351 if (!i) {
Matt Fleming54b52d82014-01-10 15:27:14 +0000352 status = efi_open_volume(sys_table_arg, image,
353 (void **)&fh);
354 if (status != EFI_SUCCESS)
Roy Franz36f89612013-09-22 15:45:40 -0700355 goto free_files;
Roy Franz7721da42013-09-22 15:45:27 -0700356 }
357
Matt Fleming54b52d82014-01-10 15:27:14 +0000358 status = efi_file_size(sys_table_arg, fh, filename_16,
359 (void **)&file->handle, &file->size);
360 if (status != EFI_SUCCESS)
Roy Franz7721da42013-09-22 15:45:27 -0700361 goto close_handles;
Roy Franz7721da42013-09-22 15:45:27 -0700362
Matt Fleming54b52d82014-01-10 15:27:14 +0000363 file_size_total += file->size;
Roy Franz7721da42013-09-22 15:45:27 -0700364 }
365
Roy Franz36f89612013-09-22 15:45:40 -0700366 if (file_size_total) {
Roy Franz7721da42013-09-22 15:45:27 -0700367 unsigned long addr;
368
369 /*
Roy Franz36f89612013-09-22 15:45:40 -0700370 * Multiple files need to be at consecutive addresses in memory,
371 * so allocate enough memory for all the files. This is used
372 * for loading multiple files.
Roy Franz7721da42013-09-22 15:45:27 -0700373 */
Roy Franz36f89612013-09-22 15:45:40 -0700374 status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
375 &file_addr, max_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700376 if (status != EFI_SUCCESS) {
Roy Franz36f89612013-09-22 15:45:40 -0700377 efi_printk(sys_table_arg, "Failed to alloc highmem for files\n");
Roy Franz7721da42013-09-22 15:45:27 -0700378 goto close_handles;
379 }
380
381 /* We've run out of free low memory. */
Roy Franz36f89612013-09-22 15:45:40 -0700382 if (file_addr > max_addr) {
Roy Franz876dc362013-09-22 15:45:28 -0700383 efi_printk(sys_table_arg, "We've run out of free low memory\n");
Roy Franz7721da42013-09-22 15:45:27 -0700384 status = EFI_INVALID_PARAMETER;
Roy Franz36f89612013-09-22 15:45:40 -0700385 goto free_file_total;
Roy Franz7721da42013-09-22 15:45:27 -0700386 }
387
Roy Franz36f89612013-09-22 15:45:40 -0700388 addr = file_addr;
389 for (j = 0; j < nr_files; j++) {
Roy Franz6a5fe772013-09-22 15:45:41 -0700390 unsigned long size;
Roy Franz7721da42013-09-22 15:45:27 -0700391
Roy Franz36f89612013-09-22 15:45:40 -0700392 size = files[j].size;
Roy Franz7721da42013-09-22 15:45:27 -0700393 while (size) {
Roy Franz6a5fe772013-09-22 15:45:41 -0700394 unsigned long chunksize;
Roy Franz7721da42013-09-22 15:45:27 -0700395 if (size > EFI_READ_CHUNK_SIZE)
396 chunksize = EFI_READ_CHUNK_SIZE;
397 else
398 chunksize = size;
Matt Fleming54b52d82014-01-10 15:27:14 +0000399
400 status = efi_file_read(fh, files[j].handle,
401 &chunksize,
402 (void *)addr);
Roy Franz7721da42013-09-22 15:45:27 -0700403 if (status != EFI_SUCCESS) {
Roy Franz46f45822013-09-22 15:45:39 -0700404 efi_printk(sys_table_arg, "Failed to read file\n");
Roy Franz36f89612013-09-22 15:45:40 -0700405 goto free_file_total;
Roy Franz7721da42013-09-22 15:45:27 -0700406 }
407 addr += chunksize;
408 size -= chunksize;
409 }
410
Matt Fleming54b52d82014-01-10 15:27:14 +0000411 efi_file_close(fh, files[j].handle);
Roy Franz7721da42013-09-22 15:45:27 -0700412 }
413
414 }
415
Matt Fleming54b52d82014-01-10 15:27:14 +0000416 efi_early->call(efi_early->free_pool, files);
Roy Franz7721da42013-09-22 15:45:27 -0700417
Roy Franz36f89612013-09-22 15:45:40 -0700418 *load_addr = file_addr;
419 *load_size = file_size_total;
Roy Franz7721da42013-09-22 15:45:27 -0700420
421 return status;
422
Roy Franz36f89612013-09-22 15:45:40 -0700423free_file_total:
424 efi_free(sys_table_arg, file_size_total, file_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700425
426close_handles:
427 for (k = j; k < i; k++)
Matt Fleming54b52d82014-01-10 15:27:14 +0000428 efi_file_close(fh, files[k].handle);
Roy Franz36f89612013-09-22 15:45:40 -0700429free_files:
Matt Fleming54b52d82014-01-10 15:27:14 +0000430 efi_early->call(efi_early->free_pool, files);
Roy Franz7721da42013-09-22 15:45:27 -0700431fail:
Roy Franz46f45822013-09-22 15:45:39 -0700432 *load_addr = 0;
433 *load_size = 0;
Roy Franz7721da42013-09-22 15:45:27 -0700434
435 return status;
436}
Roy Franz4a9f3a72013-09-22 15:45:32 -0700437/*
438 * Relocate a kernel image, either compressed or uncompressed.
439 * In the ARM64 case, all kernel images are currently
440 * uncompressed, and as such when we relocate it we need to
441 * allocate additional space for the BSS segment. Any low
442 * memory that this function should avoid needs to be
443 * unavailable in the EFI memory map, as if the preferred
444 * address is not available the lowest available address will
445 * be used.
446 */
447static efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
448 unsigned long *image_addr,
449 unsigned long image_size,
450 unsigned long alloc_size,
451 unsigned long preferred_addr,
452 unsigned long alignment)
Roy Franzc6866d72013-09-22 15:45:31 -0700453{
Roy Franz4a9f3a72013-09-22 15:45:32 -0700454 unsigned long cur_image_addr;
455 unsigned long new_addr = 0;
Roy Franzc6866d72013-09-22 15:45:31 -0700456 efi_status_t status;
Roy Franz4a9f3a72013-09-22 15:45:32 -0700457 unsigned long nr_pages;
458 efi_physical_addr_t efi_addr = preferred_addr;
459
460 if (!image_addr || !image_size || !alloc_size)
461 return EFI_INVALID_PARAMETER;
462 if (alloc_size < image_size)
463 return EFI_INVALID_PARAMETER;
464
465 cur_image_addr = *image_addr;
Roy Franzc6866d72013-09-22 15:45:31 -0700466
467 /*
468 * The EFI firmware loader could have placed the kernel image
Roy Franz4a9f3a72013-09-22 15:45:32 -0700469 * anywhere in memory, but the kernel has restrictions on the
470 * max physical address it can run at. Some architectures
471 * also have a prefered address, so first try to relocate
472 * to the preferred address. If that fails, allocate as low
473 * as possible while respecting the required alignment.
474 */
475 nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
Matt Fleming54b52d82014-01-10 15:27:14 +0000476 status = efi_early->call(efi_early->allocate_pages,
477 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
478 nr_pages, &efi_addr);
Roy Franz4a9f3a72013-09-22 15:45:32 -0700479 new_addr = efi_addr;
480 /*
481 * If preferred address allocation failed allocate as low as
Roy Franzc6866d72013-09-22 15:45:31 -0700482 * possible.
483 */
Roy Franzc6866d72013-09-22 15:45:31 -0700484 if (status != EFI_SUCCESS) {
Roy Franz4a9f3a72013-09-22 15:45:32 -0700485 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
486 &new_addr);
487 }
488 if (status != EFI_SUCCESS) {
489 efi_printk(sys_table_arg, "ERROR: Failed to allocate usable memory for kernel.\n");
490 return status;
Roy Franzc6866d72013-09-22 15:45:31 -0700491 }
492
Roy Franz4a9f3a72013-09-22 15:45:32 -0700493 /*
494 * We know source/dest won't overlap since both memory ranges
495 * have been allocated by UEFI, so we can safely use memcpy.
496 */
497 memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
Roy Franzc6866d72013-09-22 15:45:31 -0700498
Roy Franz4a9f3a72013-09-22 15:45:32 -0700499 /* Return the new address of the relocated image. */
500 *image_addr = new_addr;
Roy Franzc6866d72013-09-22 15:45:31 -0700501
502 return status;
503}
Roy Franz5fef3872013-09-22 15:45:33 -0700504
505/*
506 * Convert the unicode UEFI command line to ASCII to pass to kernel.
507 * Size of memory allocated return in *cmd_line_len.
508 * Returns NULL on error.
509 */
510static char *efi_convert_cmdline_to_ascii(efi_system_table_t *sys_table_arg,
511 efi_loaded_image_t *image,
512 int *cmd_line_len)
513{
514 u16 *s2;
515 u8 *s1 = NULL;
516 unsigned long cmdline_addr = 0;
517 int load_options_size = image->load_options_size / 2; /* ASCII */
518 void *options = image->load_options;
519 int options_size = 0;
520 efi_status_t status;
521 int i;
522 u16 zero = 0;
523
524 if (options) {
525 s2 = options;
526 while (*s2 && *s2 != '\n' && options_size < load_options_size) {
527 s2++;
528 options_size++;
529 }
530 }
531
532 if (options_size == 0) {
533 /* No command line options, so return empty string*/
534 options_size = 1;
535 options = &zero;
536 }
537
538 options_size++; /* NUL termination */
539#ifdef CONFIG_ARM
540 /*
541 * For ARM, allocate at a high address to avoid reserved
542 * regions at low addresses that we don't know the specfics of
543 * at the time we are processing the command line.
544 */
545 status = efi_high_alloc(sys_table_arg, options_size, 0,
546 &cmdline_addr, 0xfffff000);
547#else
548 status = efi_low_alloc(sys_table_arg, options_size, 0,
549 &cmdline_addr);
550#endif
551 if (status != EFI_SUCCESS)
552 return NULL;
553
554 s1 = (u8 *)cmdline_addr;
555 s2 = (u16 *)options;
556
557 for (i = 0; i < options_size - 1; i++)
558 *s1++ = *s2++;
559
560 *s1 = '\0';
561
562 *cmd_line_len = options_size;
563 return (char *)cmdline_addr;
564}