blob: eb6d4be9e722ac0ab87c875b6dbcda04b27ce3bd [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 Franz9bb40192014-01-28 10:41:28 -080014/* error code which can't be mistaken for valid address */
15#define EFI_ERROR (~0UL)
16
17
Roy Franz36f89612013-09-22 15:45:40 -070018struct file_info {
Roy Franz7721da42013-09-22 15:45:27 -070019 efi_file_handle_t *handle;
20 u64 size;
21};
22
Roy Franz876dc362013-09-22 15:45:28 -070023static void efi_printk(efi_system_table_t *sys_table_arg, char *str)
Roy Franz7721da42013-09-22 15:45:27 -070024{
25 char *s8;
26
27 for (s8 = str; *s8; s8++) {
28 efi_char16_t ch[2] = { 0 };
29
30 ch[0] = *s8;
31 if (*s8 == '\n') {
32 efi_char16_t nl[2] = { '\r', 0 };
Roy Franz876dc362013-09-22 15:45:28 -070033 efi_char16_printk(sys_table_arg, nl);
Roy Franz7721da42013-09-22 15:45:27 -070034 }
35
Roy Franz876dc362013-09-22 15:45:28 -070036 efi_char16_printk(sys_table_arg, ch);
Roy Franz7721da42013-09-22 15:45:27 -070037 }
38}
39
Roy Franzf966ea02013-12-13 11:04:49 -080040#define pr_efi(sys_table, msg) efi_printk(sys_table, "EFI stub: "msg)
41#define pr_efi_err(sys_table, msg) efi_printk(sys_table, "EFI stub: ERROR: "msg)
42
Roy Franz7721da42013-09-22 15:45:27 -070043
Roy Franz86cc6532013-09-22 15:45:35 -070044static efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
45 efi_memory_desc_t **map,
46 unsigned long *map_size,
Roy Franz1c089c62013-09-22 15:45:36 -070047 unsigned long *desc_size,
48 u32 *desc_ver,
49 unsigned long *key_ptr)
Roy Franz7721da42013-09-22 15:45:27 -070050{
51 efi_memory_desc_t *m = NULL;
52 efi_status_t status;
53 unsigned long key;
54 u32 desc_version;
55
56 *map_size = sizeof(*m) * 32;
57again:
58 /*
59 * Add an additional efi_memory_desc_t because we're doing an
60 * allocation which may be in a new descriptor region.
61 */
62 *map_size += sizeof(*m);
Matt Fleming204b0a12014-03-22 10:09:01 +000063 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
64 *map_size, (void **)&m);
Roy Franz7721da42013-09-22 15:45:27 -070065 if (status != EFI_SUCCESS)
66 goto fail;
67
Matt Fleming54b52d82014-01-10 15:27:14 +000068 *desc_size = 0;
69 key = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +000070 status = efi_call_early(get_memory_map, map_size, m,
71 &key, desc_size, &desc_version);
Roy Franz7721da42013-09-22 15:45:27 -070072 if (status == EFI_BUFFER_TOO_SMALL) {
Matt Fleming204b0a12014-03-22 10:09:01 +000073 efi_call_early(free_pool, m);
Roy Franz7721da42013-09-22 15:45:27 -070074 goto again;
75 }
76
77 if (status != EFI_SUCCESS)
Matt Fleming204b0a12014-03-22 10:09:01 +000078 efi_call_early(free_pool, m);
Matt Fleming54b52d82014-01-10 15:27:14 +000079
Roy Franz1c089c62013-09-22 15:45:36 -070080 if (key_ptr && status == EFI_SUCCESS)
81 *key_ptr = key;
82 if (desc_ver && status == EFI_SUCCESS)
83 *desc_ver = desc_version;
Roy Franz7721da42013-09-22 15:45:27 -070084
85fail:
86 *map = m;
87 return status;
88}
89
Roy Franz9bb40192014-01-28 10:41:28 -080090
91static unsigned long __init get_dram_base(efi_system_table_t *sys_table_arg)
92{
93 efi_status_t status;
94 unsigned long map_size;
95 unsigned long membase = EFI_ERROR;
96 struct efi_memory_map map;
97 efi_memory_desc_t *md;
98
99 status = efi_get_memory_map(sys_table_arg, (efi_memory_desc_t **)&map.map,
100 &map_size, &map.desc_size, NULL, NULL);
101 if (status != EFI_SUCCESS)
102 return membase;
103
104 map.map_end = map.map + map_size;
105
106 for_each_efi_memory_desc(&map, md)
107 if (md->attribute & EFI_MEMORY_WB)
108 if (membase > md->phys_addr)
109 membase = md->phys_addr;
110
111 efi_call_early(free_pool, map.map);
112
113 return membase;
114}
115
Roy Franz7721da42013-09-22 15:45:27 -0700116/*
117 * Allocate at the highest possible address that is not above 'max'.
118 */
Roy Franz40e45302013-09-22 15:45:29 -0700119static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
Roy Franz876dc362013-09-22 15:45:28 -0700120 unsigned long size, unsigned long align,
121 unsigned long *addr, unsigned long max)
Roy Franz7721da42013-09-22 15:45:27 -0700122{
123 unsigned long map_size, desc_size;
124 efi_memory_desc_t *map;
125 efi_status_t status;
126 unsigned long nr_pages;
127 u64 max_addr = 0;
128 int i;
129
Roy Franz1c089c62013-09-22 15:45:36 -0700130 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
131 NULL, NULL);
Roy Franz7721da42013-09-22 15:45:27 -0700132 if (status != EFI_SUCCESS)
133 goto fail;
134
Roy Franz38dd9c02013-09-22 15:45:30 -0700135 /*
136 * Enforce minimum alignment that EFI requires when requesting
137 * a specific address. We are doing page-based allocations,
138 * so we must be aligned to a page.
139 */
140 if (align < EFI_PAGE_SIZE)
141 align = EFI_PAGE_SIZE;
142
Roy Franz7721da42013-09-22 15:45:27 -0700143 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
144again:
145 for (i = 0; i < map_size / desc_size; i++) {
146 efi_memory_desc_t *desc;
147 unsigned long m = (unsigned long)map;
148 u64 start, end;
149
150 desc = (efi_memory_desc_t *)(m + (i * desc_size));
151 if (desc->type != EFI_CONVENTIONAL_MEMORY)
152 continue;
153
154 if (desc->num_pages < nr_pages)
155 continue;
156
157 start = desc->phys_addr;
158 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
159
160 if ((start + size) > end || (start + size) > max)
161 continue;
162
163 if (end - size > max)
164 end = max;
165
166 if (round_down(end - size, align) < start)
167 continue;
168
169 start = round_down(end - size, align);
170
171 /*
172 * Don't allocate at 0x0. It will confuse code that
173 * checks pointers against NULL.
174 */
175 if (start == 0x0)
176 continue;
177
178 if (start > max_addr)
179 max_addr = start;
180 }
181
182 if (!max_addr)
183 status = EFI_NOT_FOUND;
184 else {
Matt Fleming204b0a12014-03-22 10:09:01 +0000185 status = efi_call_early(allocate_pages,
186 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
187 nr_pages, &max_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700188 if (status != EFI_SUCCESS) {
189 max = max_addr;
190 max_addr = 0;
191 goto again;
192 }
193
194 *addr = max_addr;
195 }
196
Matt Fleming204b0a12014-03-22 10:09:01 +0000197 efi_call_early(free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700198fail:
199 return status;
200}
201
202/*
203 * Allocate at the lowest possible address.
204 */
Roy Franz40e45302013-09-22 15:45:29 -0700205static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
206 unsigned long size, unsigned long align,
Roy Franz7721da42013-09-22 15:45:27 -0700207 unsigned long *addr)
208{
209 unsigned long map_size, desc_size;
210 efi_memory_desc_t *map;
211 efi_status_t status;
212 unsigned long nr_pages;
213 int i;
214
Roy Franz1c089c62013-09-22 15:45:36 -0700215 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
216 NULL, NULL);
Roy Franz7721da42013-09-22 15:45:27 -0700217 if (status != EFI_SUCCESS)
218 goto fail;
219
Roy Franz38dd9c02013-09-22 15:45:30 -0700220 /*
221 * Enforce minimum alignment that EFI requires when requesting
222 * a specific address. We are doing page-based allocations,
223 * so we must be aligned to a page.
224 */
225 if (align < EFI_PAGE_SIZE)
226 align = EFI_PAGE_SIZE;
227
Roy Franz7721da42013-09-22 15:45:27 -0700228 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
229 for (i = 0; i < map_size / desc_size; i++) {
230 efi_memory_desc_t *desc;
231 unsigned long m = (unsigned long)map;
232 u64 start, end;
233
234 desc = (efi_memory_desc_t *)(m + (i * desc_size));
235
236 if (desc->type != EFI_CONVENTIONAL_MEMORY)
237 continue;
238
239 if (desc->num_pages < nr_pages)
240 continue;
241
242 start = desc->phys_addr;
243 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
244
245 /*
246 * Don't allocate at 0x0. It will confuse code that
247 * checks pointers against NULL. Skip the first 8
248 * bytes so we start at a nice even number.
249 */
250 if (start == 0x0)
251 start += 8;
252
253 start = round_up(start, align);
254 if ((start + size) > end)
255 continue;
256
Matt Fleming204b0a12014-03-22 10:09:01 +0000257 status = efi_call_early(allocate_pages,
258 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
259 nr_pages, &start);
Roy Franz7721da42013-09-22 15:45:27 -0700260 if (status == EFI_SUCCESS) {
261 *addr = start;
262 break;
263 }
264 }
265
266 if (i == map_size / desc_size)
267 status = EFI_NOT_FOUND;
268
Matt Fleming204b0a12014-03-22 10:09:01 +0000269 efi_call_early(free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700270fail:
271 return status;
272}
273
Roy Franz40e45302013-09-22 15:45:29 -0700274static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
Roy Franz876dc362013-09-22 15:45:28 -0700275 unsigned long addr)
Roy Franz7721da42013-09-22 15:45:27 -0700276{
277 unsigned long nr_pages;
278
Roy Franz0e1cadb2013-09-22 15:45:38 -0700279 if (!size)
280 return;
281
Roy Franz7721da42013-09-22 15:45:27 -0700282 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
Matt Fleming204b0a12014-03-22 10:09:01 +0000283 efi_call_early(free_pages, addr, nr_pages);
Roy Franz7721da42013-09-22 15:45:27 -0700284}
285
286
287/*
Roy Franz36f89612013-09-22 15:45:40 -0700288 * Check the cmdline for a LILO-style file= arguments.
Roy Franz7721da42013-09-22 15:45:27 -0700289 *
Roy Franz36f89612013-09-22 15:45:40 -0700290 * We only support loading a file from the same filesystem as
291 * the kernel image.
Roy Franz7721da42013-09-22 15:45:27 -0700292 */
Roy Franz46f45822013-09-22 15:45:39 -0700293static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
294 efi_loaded_image_t *image,
295 char *cmd_line, char *option_string,
296 unsigned long max_addr,
297 unsigned long *load_addr,
298 unsigned long *load_size)
Roy Franz7721da42013-09-22 15:45:27 -0700299{
Roy Franz36f89612013-09-22 15:45:40 -0700300 struct file_info *files;
301 unsigned long file_addr;
Roy Franz36f89612013-09-22 15:45:40 -0700302 u64 file_size_total;
Leif Lindholm9403e462014-04-04 13:25:46 +0100303 efi_file_handle_t *fh = NULL;
Roy Franz7721da42013-09-22 15:45:27 -0700304 efi_status_t status;
Roy Franz36f89612013-09-22 15:45:40 -0700305 int nr_files;
Roy Franz7721da42013-09-22 15:45:27 -0700306 char *str;
307 int i, j, k;
308
Roy Franz36f89612013-09-22 15:45:40 -0700309 file_addr = 0;
310 file_size_total = 0;
Roy Franz7721da42013-09-22 15:45:27 -0700311
Roy Franz46f45822013-09-22 15:45:39 -0700312 str = cmd_line;
Roy Franz7721da42013-09-22 15:45:27 -0700313
314 j = 0; /* See close_handles */
315
Roy Franz46f45822013-09-22 15:45:39 -0700316 if (!load_addr || !load_size)
317 return EFI_INVALID_PARAMETER;
318
319 *load_addr = 0;
320 *load_size = 0;
321
Roy Franz7721da42013-09-22 15:45:27 -0700322 if (!str || !*str)
323 return EFI_SUCCESS;
324
Roy Franz36f89612013-09-22 15:45:40 -0700325 for (nr_files = 0; *str; nr_files++) {
Roy Franz46f45822013-09-22 15:45:39 -0700326 str = strstr(str, option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700327 if (!str)
328 break;
329
Roy Franz46f45822013-09-22 15:45:39 -0700330 str += strlen(option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700331
332 /* Skip any leading slashes */
333 while (*str == '/' || *str == '\\')
334 str++;
335
336 while (*str && *str != ' ' && *str != '\n')
337 str++;
338 }
339
Roy Franz36f89612013-09-22 15:45:40 -0700340 if (!nr_files)
Roy Franz7721da42013-09-22 15:45:27 -0700341 return EFI_SUCCESS;
342
Matt Fleming204b0a12014-03-22 10:09:01 +0000343 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
344 nr_files * sizeof(*files), (void **)&files);
Roy Franz7721da42013-09-22 15:45:27 -0700345 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800346 pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
Roy Franz7721da42013-09-22 15:45:27 -0700347 goto fail;
348 }
349
Roy Franz46f45822013-09-22 15:45:39 -0700350 str = cmd_line;
Roy Franz36f89612013-09-22 15:45:40 -0700351 for (i = 0; i < nr_files; i++) {
352 struct file_info *file;
Roy Franz7721da42013-09-22 15:45:27 -0700353 efi_char16_t filename_16[256];
Roy Franz7721da42013-09-22 15:45:27 -0700354 efi_char16_t *p;
Roy Franz7721da42013-09-22 15:45:27 -0700355
Roy Franz46f45822013-09-22 15:45:39 -0700356 str = strstr(str, option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700357 if (!str)
358 break;
359
Roy Franz46f45822013-09-22 15:45:39 -0700360 str += strlen(option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700361
Roy Franz36f89612013-09-22 15:45:40 -0700362 file = &files[i];
Roy Franz7721da42013-09-22 15:45:27 -0700363 p = filename_16;
364
365 /* Skip any leading slashes */
366 while (*str == '/' || *str == '\\')
367 str++;
368
369 while (*str && *str != ' ' && *str != '\n') {
370 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
371 break;
372
373 if (*str == '/') {
374 *p++ = '\\';
Roy Franz4e283082013-09-22 15:45:42 -0700375 str++;
Roy Franz7721da42013-09-22 15:45:27 -0700376 } else {
377 *p++ = *str++;
378 }
379 }
380
381 *p = '\0';
382
383 /* Only open the volume once. */
384 if (!i) {
Matt Fleming54b52d82014-01-10 15:27:14 +0000385 status = efi_open_volume(sys_table_arg, image,
386 (void **)&fh);
387 if (status != EFI_SUCCESS)
Roy Franz36f89612013-09-22 15:45:40 -0700388 goto free_files;
Roy Franz7721da42013-09-22 15:45:27 -0700389 }
390
Matt Fleming54b52d82014-01-10 15:27:14 +0000391 status = efi_file_size(sys_table_arg, fh, filename_16,
392 (void **)&file->handle, &file->size);
393 if (status != EFI_SUCCESS)
Roy Franz7721da42013-09-22 15:45:27 -0700394 goto close_handles;
Roy Franz7721da42013-09-22 15:45:27 -0700395
Matt Fleming54b52d82014-01-10 15:27:14 +0000396 file_size_total += file->size;
Roy Franz7721da42013-09-22 15:45:27 -0700397 }
398
Roy Franz36f89612013-09-22 15:45:40 -0700399 if (file_size_total) {
Roy Franz7721da42013-09-22 15:45:27 -0700400 unsigned long addr;
401
402 /*
Roy Franz36f89612013-09-22 15:45:40 -0700403 * Multiple files need to be at consecutive addresses in memory,
404 * so allocate enough memory for all the files. This is used
405 * for loading multiple files.
Roy Franz7721da42013-09-22 15:45:27 -0700406 */
Roy Franz36f89612013-09-22 15:45:40 -0700407 status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
408 &file_addr, max_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700409 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800410 pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
Roy Franz7721da42013-09-22 15:45:27 -0700411 goto close_handles;
412 }
413
414 /* We've run out of free low memory. */
Roy Franz36f89612013-09-22 15:45:40 -0700415 if (file_addr > max_addr) {
Roy Franzf966ea02013-12-13 11:04:49 -0800416 pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
Roy Franz7721da42013-09-22 15:45:27 -0700417 status = EFI_INVALID_PARAMETER;
Roy Franz36f89612013-09-22 15:45:40 -0700418 goto free_file_total;
Roy Franz7721da42013-09-22 15:45:27 -0700419 }
420
Roy Franz36f89612013-09-22 15:45:40 -0700421 addr = file_addr;
422 for (j = 0; j < nr_files; j++) {
Roy Franz6a5fe772013-09-22 15:45:41 -0700423 unsigned long size;
Roy Franz7721da42013-09-22 15:45:27 -0700424
Roy Franz36f89612013-09-22 15:45:40 -0700425 size = files[j].size;
Roy Franz7721da42013-09-22 15:45:27 -0700426 while (size) {
Roy Franz6a5fe772013-09-22 15:45:41 -0700427 unsigned long chunksize;
Roy Franz7721da42013-09-22 15:45:27 -0700428 if (size > EFI_READ_CHUNK_SIZE)
429 chunksize = EFI_READ_CHUNK_SIZE;
430 else
431 chunksize = size;
Matt Fleming54b52d82014-01-10 15:27:14 +0000432
Matt Fleming47514c92014-04-10 14:11:45 +0100433 status = efi_file_read(files[j].handle,
Matt Fleming54b52d82014-01-10 15:27:14 +0000434 &chunksize,
435 (void *)addr);
Roy Franz7721da42013-09-22 15:45:27 -0700436 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800437 pr_efi_err(sys_table_arg, "Failed to read file\n");
Roy Franz36f89612013-09-22 15:45:40 -0700438 goto free_file_total;
Roy Franz7721da42013-09-22 15:45:27 -0700439 }
440 addr += chunksize;
441 size -= chunksize;
442 }
443
Matt Fleming47514c92014-04-10 14:11:45 +0100444 efi_file_close(files[j].handle);
Roy Franz7721da42013-09-22 15:45:27 -0700445 }
446
447 }
448
Matt Fleming204b0a12014-03-22 10:09:01 +0000449 efi_call_early(free_pool, files);
Roy Franz7721da42013-09-22 15:45:27 -0700450
Roy Franz36f89612013-09-22 15:45:40 -0700451 *load_addr = file_addr;
452 *load_size = file_size_total;
Roy Franz7721da42013-09-22 15:45:27 -0700453
454 return status;
455
Roy Franz36f89612013-09-22 15:45:40 -0700456free_file_total:
457 efi_free(sys_table_arg, file_size_total, file_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700458
459close_handles:
460 for (k = j; k < i; k++)
Matt Fleming47514c92014-04-10 14:11:45 +0100461 efi_file_close(files[k].handle);
Roy Franz36f89612013-09-22 15:45:40 -0700462free_files:
Matt Fleming204b0a12014-03-22 10:09:01 +0000463 efi_call_early(free_pool, files);
Roy Franz7721da42013-09-22 15:45:27 -0700464fail:
Roy Franz46f45822013-09-22 15:45:39 -0700465 *load_addr = 0;
466 *load_size = 0;
Roy Franz7721da42013-09-22 15:45:27 -0700467
468 return status;
469}
Roy Franz4a9f3a72013-09-22 15:45:32 -0700470/*
471 * Relocate a kernel image, either compressed or uncompressed.
472 * In the ARM64 case, all kernel images are currently
473 * uncompressed, and as such when we relocate it we need to
474 * allocate additional space for the BSS segment. Any low
475 * memory that this function should avoid needs to be
476 * unavailable in the EFI memory map, as if the preferred
477 * address is not available the lowest available address will
478 * be used.
479 */
480static efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
481 unsigned long *image_addr,
482 unsigned long image_size,
483 unsigned long alloc_size,
484 unsigned long preferred_addr,
485 unsigned long alignment)
Roy Franzc6866d72013-09-22 15:45:31 -0700486{
Roy Franz4a9f3a72013-09-22 15:45:32 -0700487 unsigned long cur_image_addr;
488 unsigned long new_addr = 0;
Roy Franzc6866d72013-09-22 15:45:31 -0700489 efi_status_t status;
Roy Franz4a9f3a72013-09-22 15:45:32 -0700490 unsigned long nr_pages;
491 efi_physical_addr_t efi_addr = preferred_addr;
492
493 if (!image_addr || !image_size || !alloc_size)
494 return EFI_INVALID_PARAMETER;
495 if (alloc_size < image_size)
496 return EFI_INVALID_PARAMETER;
497
498 cur_image_addr = *image_addr;
Roy Franzc6866d72013-09-22 15:45:31 -0700499
500 /*
501 * The EFI firmware loader could have placed the kernel image
Roy Franz4a9f3a72013-09-22 15:45:32 -0700502 * anywhere in memory, but the kernel has restrictions on the
503 * max physical address it can run at. Some architectures
504 * also have a prefered address, so first try to relocate
505 * to the preferred address. If that fails, allocate as low
506 * as possible while respecting the required alignment.
507 */
508 nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
Matt Fleming204b0a12014-03-22 10:09:01 +0000509 status = efi_call_early(allocate_pages,
510 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
511 nr_pages, &efi_addr);
Roy Franz4a9f3a72013-09-22 15:45:32 -0700512 new_addr = efi_addr;
513 /*
514 * If preferred address allocation failed allocate as low as
Roy Franzc6866d72013-09-22 15:45:31 -0700515 * possible.
516 */
Roy Franzc6866d72013-09-22 15:45:31 -0700517 if (status != EFI_SUCCESS) {
Roy Franz4a9f3a72013-09-22 15:45:32 -0700518 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
519 &new_addr);
520 }
521 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800522 pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
Roy Franz4a9f3a72013-09-22 15:45:32 -0700523 return status;
Roy Franzc6866d72013-09-22 15:45:31 -0700524 }
525
Roy Franz4a9f3a72013-09-22 15:45:32 -0700526 /*
527 * We know source/dest won't overlap since both memory ranges
528 * have been allocated by UEFI, so we can safely use memcpy.
529 */
530 memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
Roy Franzc6866d72013-09-22 15:45:31 -0700531
Roy Franz4a9f3a72013-09-22 15:45:32 -0700532 /* Return the new address of the relocated image. */
533 *image_addr = new_addr;
Roy Franzc6866d72013-09-22 15:45:31 -0700534
535 return status;
536}
Roy Franz5fef3872013-09-22 15:45:33 -0700537
538/*
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500539 * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
540 * This overestimates for surrogates, but that is okay.
541 */
542static int efi_utf8_bytes(u16 c)
543{
544 return 1 + (c >= 0x80) + (c >= 0x800);
545}
546
547/*
548 * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
549 */
550static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
551{
552 unsigned int c;
553
554 while (n--) {
555 c = *src++;
556 if (n && c >= 0xd800 && c <= 0xdbff &&
557 *src >= 0xdc00 && *src <= 0xdfff) {
558 c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
559 src++;
560 n--;
561 }
562 if (c >= 0xd800 && c <= 0xdfff)
563 c = 0xfffd; /* Unmatched surrogate */
564 if (c < 0x80) {
565 *dst++ = c;
566 continue;
567 }
568 if (c < 0x800) {
569 *dst++ = 0xc0 + (c >> 6);
570 goto t1;
571 }
572 if (c < 0x10000) {
573 *dst++ = 0xe0 + (c >> 12);
574 goto t2;
575 }
576 *dst++ = 0xf0 + (c >> 18);
577 *dst++ = 0x80 + ((c >> 12) & 0x3f);
578 t2:
579 *dst++ = 0x80 + ((c >> 6) & 0x3f);
580 t1:
581 *dst++ = 0x80 + (c & 0x3f);
582 }
583
584 return dst;
585}
586
587/*
Roy Franz5fef3872013-09-22 15:45:33 -0700588 * Convert the unicode UEFI command line to ASCII to pass to kernel.
589 * Size of memory allocated return in *cmd_line_len.
590 * Returns NULL on error.
591 */
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500592static char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
593 efi_loaded_image_t *image,
594 int *cmd_line_len)
Roy Franz5fef3872013-09-22 15:45:33 -0700595{
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500596 const u16 *s2;
Roy Franz5fef3872013-09-22 15:45:33 -0700597 u8 *s1 = NULL;
598 unsigned long cmdline_addr = 0;
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500599 int load_options_chars = image->load_options_size / 2; /* UTF-16 */
600 const u16 *options = image->load_options;
601 int options_bytes = 0; /* UTF-8 bytes */
602 int options_chars = 0; /* UTF-16 chars */
Roy Franz5fef3872013-09-22 15:45:33 -0700603 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -0700604 u16 zero = 0;
605
606 if (options) {
607 s2 = options;
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500608 while (*s2 && *s2 != '\n'
609 && options_chars < load_options_chars) {
610 options_bytes += efi_utf8_bytes(*s2++);
611 options_chars++;
Roy Franz5fef3872013-09-22 15:45:33 -0700612 }
613 }
614
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500615 if (!options_chars) {
Roy Franz5fef3872013-09-22 15:45:33 -0700616 /* No command line options, so return empty string*/
Roy Franz5fef3872013-09-22 15:45:33 -0700617 options = &zero;
618 }
619
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500620 options_bytes++; /* NUL termination */
Leif Lindholm9403e462014-04-04 13:25:46 +0100621
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500622 status = efi_low_alloc(sys_table_arg, options_bytes, 0, &cmdline_addr);
Roy Franz5fef3872013-09-22 15:45:33 -0700623 if (status != EFI_SUCCESS)
624 return NULL;
625
626 s1 = (u8 *)cmdline_addr;
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500627 s2 = (const u16 *)options;
Roy Franz5fef3872013-09-22 15:45:33 -0700628
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500629 s1 = efi_utf16_to_utf8(s1, s2, options_chars);
Roy Franz5fef3872013-09-22 15:45:33 -0700630 *s1 = '\0';
631
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500632 *cmd_line_len = options_bytes;
Roy Franz5fef3872013-09-22 15:45:33 -0700633 return (char *)cmdline_addr;
634}