blob: 32d5cca30f496d88594de5f74d0d1de9bce5057d [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 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +020012
13#include <linux/efi.h>
14#include <asm/efi.h>
15
16#include "efistub.h"
17
Roy Franz7721da42013-09-22 15:45:27 -070018#define EFI_READ_CHUNK_SIZE (1024 * 1024)
19
Roy Franz36f89612013-09-22 15:45:40 -070020struct file_info {
Roy Franz7721da42013-09-22 15:45:27 -070021 efi_file_handle_t *handle;
22 u64 size;
23};
24
Ard Biesheuvelbd669472014-07-02 14:54:42 +020025void efi_printk(efi_system_table_t *sys_table_arg, char *str)
Roy Franz7721da42013-09-22 15:45:27 -070026{
27 char *s8;
28
29 for (s8 = str; *s8; s8++) {
30 efi_char16_t ch[2] = { 0 };
31
32 ch[0] = *s8;
33 if (*s8 == '\n') {
34 efi_char16_t nl[2] = { '\r', 0 };
Roy Franz876dc362013-09-22 15:45:28 -070035 efi_char16_printk(sys_table_arg, nl);
Roy Franz7721da42013-09-22 15:45:27 -070036 }
37
Roy Franz876dc362013-09-22 15:45:28 -070038 efi_char16_printk(sys_table_arg, ch);
Roy Franz7721da42013-09-22 15:45:27 -070039 }
40}
41
Ard Biesheuvelbd669472014-07-02 14:54:42 +020042efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
43 efi_memory_desc_t **map,
44 unsigned long *map_size,
45 unsigned long *desc_size,
46 u32 *desc_ver,
47 unsigned long *key_ptr)
Roy Franz7721da42013-09-22 15:45:27 -070048{
49 efi_memory_desc_t *m = NULL;
50 efi_status_t status;
51 unsigned long key;
52 u32 desc_version;
53
54 *map_size = sizeof(*m) * 32;
55again:
56 /*
57 * Add an additional efi_memory_desc_t because we're doing an
58 * allocation which may be in a new descriptor region.
59 */
60 *map_size += sizeof(*m);
Matt Fleming204b0a12014-03-22 10:09:01 +000061 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
62 *map_size, (void **)&m);
Roy Franz7721da42013-09-22 15:45:27 -070063 if (status != EFI_SUCCESS)
64 goto fail;
65
Matt Fleming54b52d82014-01-10 15:27:14 +000066 *desc_size = 0;
67 key = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +000068 status = efi_call_early(get_memory_map, map_size, m,
69 &key, desc_size, &desc_version);
Roy Franz7721da42013-09-22 15:45:27 -070070 if (status == EFI_BUFFER_TOO_SMALL) {
Matt Fleming204b0a12014-03-22 10:09:01 +000071 efi_call_early(free_pool, m);
Roy Franz7721da42013-09-22 15:45:27 -070072 goto again;
73 }
74
75 if (status != EFI_SUCCESS)
Matt Fleming204b0a12014-03-22 10:09:01 +000076 efi_call_early(free_pool, m);
Matt Fleming54b52d82014-01-10 15:27:14 +000077
Roy Franz1c089c62013-09-22 15:45:36 -070078 if (key_ptr && status == EFI_SUCCESS)
79 *key_ptr = key;
80 if (desc_ver && status == EFI_SUCCESS)
81 *desc_ver = desc_version;
Roy Franz7721da42013-09-22 15:45:27 -070082
83fail:
84 *map = m;
85 return status;
86}
87
Roy Franz9bb40192014-01-28 10:41:28 -080088
Ard Biesheuvelbd669472014-07-02 14:54:42 +020089unsigned long __init get_dram_base(efi_system_table_t *sys_table_arg)
Roy Franz9bb40192014-01-28 10:41:28 -080090{
91 efi_status_t status;
92 unsigned long map_size;
93 unsigned long membase = EFI_ERROR;
94 struct efi_memory_map map;
95 efi_memory_desc_t *md;
96
97 status = efi_get_memory_map(sys_table_arg, (efi_memory_desc_t **)&map.map,
98 &map_size, &map.desc_size, NULL, NULL);
99 if (status != EFI_SUCCESS)
100 return membase;
101
102 map.map_end = map.map + map_size;
103
104 for_each_efi_memory_desc(&map, md)
105 if (md->attribute & EFI_MEMORY_WB)
106 if (membase > md->phys_addr)
107 membase = md->phys_addr;
108
109 efi_call_early(free_pool, map.map);
110
111 return membase;
112}
113
Roy Franz7721da42013-09-22 15:45:27 -0700114/*
115 * Allocate at the highest possible address that is not above 'max'.
116 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200117efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
118 unsigned long size, unsigned long align,
119 unsigned long *addr, unsigned long max)
Roy Franz7721da42013-09-22 15:45:27 -0700120{
121 unsigned long map_size, desc_size;
122 efi_memory_desc_t *map;
123 efi_status_t status;
124 unsigned long nr_pages;
125 u64 max_addr = 0;
126 int i;
127
Roy Franz1c089c62013-09-22 15:45:36 -0700128 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
129 NULL, NULL);
Roy Franz7721da42013-09-22 15:45:27 -0700130 if (status != EFI_SUCCESS)
131 goto fail;
132
Roy Franz38dd9c02013-09-22 15:45:30 -0700133 /*
134 * Enforce minimum alignment that EFI requires when requesting
135 * a specific address. We are doing page-based allocations,
136 * so we must be aligned to a page.
137 */
138 if (align < EFI_PAGE_SIZE)
139 align = EFI_PAGE_SIZE;
140
Roy Franz7721da42013-09-22 15:45:27 -0700141 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
142again:
143 for (i = 0; i < map_size / desc_size; i++) {
144 efi_memory_desc_t *desc;
145 unsigned long m = (unsigned long)map;
146 u64 start, end;
147
148 desc = (efi_memory_desc_t *)(m + (i * desc_size));
149 if (desc->type != EFI_CONVENTIONAL_MEMORY)
150 continue;
151
152 if (desc->num_pages < nr_pages)
153 continue;
154
155 start = desc->phys_addr;
156 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
157
158 if ((start + size) > end || (start + size) > max)
159 continue;
160
161 if (end - size > max)
162 end = max;
163
164 if (round_down(end - size, align) < start)
165 continue;
166
167 start = round_down(end - size, align);
168
169 /*
170 * Don't allocate at 0x0. It will confuse code that
171 * checks pointers against NULL.
172 */
173 if (start == 0x0)
174 continue;
175
176 if (start > max_addr)
177 max_addr = start;
178 }
179
180 if (!max_addr)
181 status = EFI_NOT_FOUND;
182 else {
Matt Fleming204b0a12014-03-22 10:09:01 +0000183 status = efi_call_early(allocate_pages,
184 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
185 nr_pages, &max_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700186 if (status != EFI_SUCCESS) {
187 max = max_addr;
188 max_addr = 0;
189 goto again;
190 }
191
192 *addr = max_addr;
193 }
194
Matt Fleming204b0a12014-03-22 10:09:01 +0000195 efi_call_early(free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700196fail:
197 return status;
198}
199
200/*
201 * Allocate at the lowest possible address.
202 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200203efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
204 unsigned long size, unsigned long align,
205 unsigned long *addr)
Roy Franz7721da42013-09-22 15:45:27 -0700206{
207 unsigned long map_size, desc_size;
208 efi_memory_desc_t *map;
209 efi_status_t status;
210 unsigned long nr_pages;
211 int i;
212
Roy Franz1c089c62013-09-22 15:45:36 -0700213 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
214 NULL, NULL);
Roy Franz7721da42013-09-22 15:45:27 -0700215 if (status != EFI_SUCCESS)
216 goto fail;
217
Roy Franz38dd9c02013-09-22 15:45:30 -0700218 /*
219 * Enforce minimum alignment that EFI requires when requesting
220 * a specific address. We are doing page-based allocations,
221 * so we must be aligned to a page.
222 */
223 if (align < EFI_PAGE_SIZE)
224 align = EFI_PAGE_SIZE;
225
Roy Franz7721da42013-09-22 15:45:27 -0700226 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
227 for (i = 0; i < map_size / desc_size; i++) {
228 efi_memory_desc_t *desc;
229 unsigned long m = (unsigned long)map;
230 u64 start, end;
231
232 desc = (efi_memory_desc_t *)(m + (i * desc_size));
233
234 if (desc->type != EFI_CONVENTIONAL_MEMORY)
235 continue;
236
237 if (desc->num_pages < nr_pages)
238 continue;
239
240 start = desc->phys_addr;
241 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
242
243 /*
244 * Don't allocate at 0x0. It will confuse code that
245 * checks pointers against NULL. Skip the first 8
246 * bytes so we start at a nice even number.
247 */
248 if (start == 0x0)
249 start += 8;
250
251 start = round_up(start, align);
252 if ((start + size) > end)
253 continue;
254
Matt Fleming204b0a12014-03-22 10:09:01 +0000255 status = efi_call_early(allocate_pages,
256 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
257 nr_pages, &start);
Roy Franz7721da42013-09-22 15:45:27 -0700258 if (status == EFI_SUCCESS) {
259 *addr = start;
260 break;
261 }
262 }
263
264 if (i == map_size / desc_size)
265 status = EFI_NOT_FOUND;
266
Matt Fleming204b0a12014-03-22 10:09:01 +0000267 efi_call_early(free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700268fail:
269 return status;
270}
271
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200272void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
273 unsigned long addr)
Roy Franz7721da42013-09-22 15:45:27 -0700274{
275 unsigned long nr_pages;
276
Roy Franz0e1cadb2013-09-22 15:45:38 -0700277 if (!size)
278 return;
279
Roy Franz7721da42013-09-22 15:45:27 -0700280 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
Matt Fleming204b0a12014-03-22 10:09:01 +0000281 efi_call_early(free_pages, addr, nr_pages);
Roy Franz7721da42013-09-22 15:45:27 -0700282}
283
284
285/*
Roy Franz36f89612013-09-22 15:45:40 -0700286 * Check the cmdline for a LILO-style file= arguments.
Roy Franz7721da42013-09-22 15:45:27 -0700287 *
Roy Franz36f89612013-09-22 15:45:40 -0700288 * We only support loading a file from the same filesystem as
289 * the kernel image.
Roy Franz7721da42013-09-22 15:45:27 -0700290 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200291efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
292 efi_loaded_image_t *image,
293 char *cmd_line, char *option_string,
294 unsigned long max_addr,
295 unsigned long *load_addr,
296 unsigned long *load_size)
Roy Franz7721da42013-09-22 15:45:27 -0700297{
Roy Franz36f89612013-09-22 15:45:40 -0700298 struct file_info *files;
299 unsigned long file_addr;
Roy Franz36f89612013-09-22 15:45:40 -0700300 u64 file_size_total;
Leif Lindholm9403e462014-04-04 13:25:46 +0100301 efi_file_handle_t *fh = NULL;
Roy Franz7721da42013-09-22 15:45:27 -0700302 efi_status_t status;
Roy Franz36f89612013-09-22 15:45:40 -0700303 int nr_files;
Roy Franz7721da42013-09-22 15:45:27 -0700304 char *str;
305 int i, j, k;
306
Roy Franz36f89612013-09-22 15:45:40 -0700307 file_addr = 0;
308 file_size_total = 0;
Roy Franz7721da42013-09-22 15:45:27 -0700309
Roy Franz46f45822013-09-22 15:45:39 -0700310 str = cmd_line;
Roy Franz7721da42013-09-22 15:45:27 -0700311
312 j = 0; /* See close_handles */
313
Roy Franz46f45822013-09-22 15:45:39 -0700314 if (!load_addr || !load_size)
315 return EFI_INVALID_PARAMETER;
316
317 *load_addr = 0;
318 *load_size = 0;
319
Roy Franz7721da42013-09-22 15:45:27 -0700320 if (!str || !*str)
321 return EFI_SUCCESS;
322
Roy Franz36f89612013-09-22 15:45:40 -0700323 for (nr_files = 0; *str; nr_files++) {
Roy Franz46f45822013-09-22 15:45:39 -0700324 str = strstr(str, option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700325 if (!str)
326 break;
327
Roy Franz46f45822013-09-22 15:45:39 -0700328 str += strlen(option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700329
330 /* Skip any leading slashes */
331 while (*str == '/' || *str == '\\')
332 str++;
333
334 while (*str && *str != ' ' && *str != '\n')
335 str++;
336 }
337
Roy Franz36f89612013-09-22 15:45:40 -0700338 if (!nr_files)
Roy Franz7721da42013-09-22 15:45:27 -0700339 return EFI_SUCCESS;
340
Matt Fleming204b0a12014-03-22 10:09:01 +0000341 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
342 nr_files * sizeof(*files), (void **)&files);
Roy Franz7721da42013-09-22 15:45:27 -0700343 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800344 pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
Roy Franz7721da42013-09-22 15:45:27 -0700345 goto fail;
346 }
347
Roy Franz46f45822013-09-22 15:45:39 -0700348 str = cmd_line;
Roy Franz36f89612013-09-22 15:45:40 -0700349 for (i = 0; i < nr_files; i++) {
350 struct file_info *file;
Roy Franz7721da42013-09-22 15:45:27 -0700351 efi_char16_t filename_16[256];
Roy Franz7721da42013-09-22 15:45:27 -0700352 efi_char16_t *p;
Roy Franz7721da42013-09-22 15:45:27 -0700353
Roy Franz46f45822013-09-22 15:45:39 -0700354 str = strstr(str, option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700355 if (!str)
356 break;
357
Roy Franz46f45822013-09-22 15:45:39 -0700358 str += strlen(option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700359
Roy Franz36f89612013-09-22 15:45:40 -0700360 file = &files[i];
Roy Franz7721da42013-09-22 15:45:27 -0700361 p = filename_16;
362
363 /* Skip any leading slashes */
364 while (*str == '/' || *str == '\\')
365 str++;
366
367 while (*str && *str != ' ' && *str != '\n') {
368 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
369 break;
370
371 if (*str == '/') {
372 *p++ = '\\';
Roy Franz4e283082013-09-22 15:45:42 -0700373 str++;
Roy Franz7721da42013-09-22 15:45:27 -0700374 } else {
375 *p++ = *str++;
376 }
377 }
378
379 *p = '\0';
380
381 /* Only open the volume once. */
382 if (!i) {
Matt Fleming54b52d82014-01-10 15:27:14 +0000383 status = efi_open_volume(sys_table_arg, image,
384 (void **)&fh);
385 if (status != EFI_SUCCESS)
Roy Franz36f89612013-09-22 15:45:40 -0700386 goto free_files;
Roy Franz7721da42013-09-22 15:45:27 -0700387 }
388
Matt Fleming54b52d82014-01-10 15:27:14 +0000389 status = efi_file_size(sys_table_arg, fh, filename_16,
390 (void **)&file->handle, &file->size);
391 if (status != EFI_SUCCESS)
Roy Franz7721da42013-09-22 15:45:27 -0700392 goto close_handles;
Roy Franz7721da42013-09-22 15:45:27 -0700393
Matt Fleming54b52d82014-01-10 15:27:14 +0000394 file_size_total += file->size;
Roy Franz7721da42013-09-22 15:45:27 -0700395 }
396
Roy Franz36f89612013-09-22 15:45:40 -0700397 if (file_size_total) {
Roy Franz7721da42013-09-22 15:45:27 -0700398 unsigned long addr;
399
400 /*
Roy Franz36f89612013-09-22 15:45:40 -0700401 * Multiple files need to be at consecutive addresses in memory,
402 * so allocate enough memory for all the files. This is used
403 * for loading multiple files.
Roy Franz7721da42013-09-22 15:45:27 -0700404 */
Roy Franz36f89612013-09-22 15:45:40 -0700405 status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
406 &file_addr, max_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700407 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800408 pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
Roy Franz7721da42013-09-22 15:45:27 -0700409 goto close_handles;
410 }
411
412 /* We've run out of free low memory. */
Roy Franz36f89612013-09-22 15:45:40 -0700413 if (file_addr > max_addr) {
Roy Franzf966ea02013-12-13 11:04:49 -0800414 pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
Roy Franz7721da42013-09-22 15:45:27 -0700415 status = EFI_INVALID_PARAMETER;
Roy Franz36f89612013-09-22 15:45:40 -0700416 goto free_file_total;
Roy Franz7721da42013-09-22 15:45:27 -0700417 }
418
Roy Franz36f89612013-09-22 15:45:40 -0700419 addr = file_addr;
420 for (j = 0; j < nr_files; j++) {
Roy Franz6a5fe772013-09-22 15:45:41 -0700421 unsigned long size;
Roy Franz7721da42013-09-22 15:45:27 -0700422
Roy Franz36f89612013-09-22 15:45:40 -0700423 size = files[j].size;
Roy Franz7721da42013-09-22 15:45:27 -0700424 while (size) {
Roy Franz6a5fe772013-09-22 15:45:41 -0700425 unsigned long chunksize;
Roy Franz7721da42013-09-22 15:45:27 -0700426 if (size > EFI_READ_CHUNK_SIZE)
427 chunksize = EFI_READ_CHUNK_SIZE;
428 else
429 chunksize = size;
Matt Fleming54b52d82014-01-10 15:27:14 +0000430
Matt Fleming47514c92014-04-10 14:11:45 +0100431 status = efi_file_read(files[j].handle,
Matt Fleming54b52d82014-01-10 15:27:14 +0000432 &chunksize,
433 (void *)addr);
Roy Franz7721da42013-09-22 15:45:27 -0700434 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800435 pr_efi_err(sys_table_arg, "Failed to read file\n");
Roy Franz36f89612013-09-22 15:45:40 -0700436 goto free_file_total;
Roy Franz7721da42013-09-22 15:45:27 -0700437 }
438 addr += chunksize;
439 size -= chunksize;
440 }
441
Matt Fleming47514c92014-04-10 14:11:45 +0100442 efi_file_close(files[j].handle);
Roy Franz7721da42013-09-22 15:45:27 -0700443 }
444
445 }
446
Matt Fleming204b0a12014-03-22 10:09:01 +0000447 efi_call_early(free_pool, files);
Roy Franz7721da42013-09-22 15:45:27 -0700448
Roy Franz36f89612013-09-22 15:45:40 -0700449 *load_addr = file_addr;
450 *load_size = file_size_total;
Roy Franz7721da42013-09-22 15:45:27 -0700451
452 return status;
453
Roy Franz36f89612013-09-22 15:45:40 -0700454free_file_total:
455 efi_free(sys_table_arg, file_size_total, file_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700456
457close_handles:
458 for (k = j; k < i; k++)
Matt Fleming47514c92014-04-10 14:11:45 +0100459 efi_file_close(files[k].handle);
Roy Franz36f89612013-09-22 15:45:40 -0700460free_files:
Matt Fleming204b0a12014-03-22 10:09:01 +0000461 efi_call_early(free_pool, files);
Roy Franz7721da42013-09-22 15:45:27 -0700462fail:
Roy Franz46f45822013-09-22 15:45:39 -0700463 *load_addr = 0;
464 *load_size = 0;
Roy Franz7721da42013-09-22 15:45:27 -0700465
466 return status;
467}
Roy Franz4a9f3a72013-09-22 15:45:32 -0700468/*
469 * Relocate a kernel image, either compressed or uncompressed.
470 * In the ARM64 case, all kernel images are currently
471 * uncompressed, and as such when we relocate it we need to
472 * allocate additional space for the BSS segment. Any low
473 * memory that this function should avoid needs to be
474 * unavailable in the EFI memory map, as if the preferred
475 * address is not available the lowest available address will
476 * be used.
477 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200478efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
479 unsigned long *image_addr,
480 unsigned long image_size,
481 unsigned long alloc_size,
482 unsigned long preferred_addr,
483 unsigned long alignment)
Roy Franzc6866d72013-09-22 15:45:31 -0700484{
Roy Franz4a9f3a72013-09-22 15:45:32 -0700485 unsigned long cur_image_addr;
486 unsigned long new_addr = 0;
Roy Franzc6866d72013-09-22 15:45:31 -0700487 efi_status_t status;
Roy Franz4a9f3a72013-09-22 15:45:32 -0700488 unsigned long nr_pages;
489 efi_physical_addr_t efi_addr = preferred_addr;
490
491 if (!image_addr || !image_size || !alloc_size)
492 return EFI_INVALID_PARAMETER;
493 if (alloc_size < image_size)
494 return EFI_INVALID_PARAMETER;
495
496 cur_image_addr = *image_addr;
Roy Franzc6866d72013-09-22 15:45:31 -0700497
498 /*
499 * The EFI firmware loader could have placed the kernel image
Roy Franz4a9f3a72013-09-22 15:45:32 -0700500 * anywhere in memory, but the kernel has restrictions on the
501 * max physical address it can run at. Some architectures
502 * also have a prefered address, so first try to relocate
503 * to the preferred address. If that fails, allocate as low
504 * as possible while respecting the required alignment.
505 */
506 nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
Matt Fleming204b0a12014-03-22 10:09:01 +0000507 status = efi_call_early(allocate_pages,
508 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
509 nr_pages, &efi_addr);
Roy Franz4a9f3a72013-09-22 15:45:32 -0700510 new_addr = efi_addr;
511 /*
512 * If preferred address allocation failed allocate as low as
Roy Franzc6866d72013-09-22 15:45:31 -0700513 * possible.
514 */
Roy Franzc6866d72013-09-22 15:45:31 -0700515 if (status != EFI_SUCCESS) {
Roy Franz4a9f3a72013-09-22 15:45:32 -0700516 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
517 &new_addr);
518 }
519 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800520 pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
Roy Franz4a9f3a72013-09-22 15:45:32 -0700521 return status;
Roy Franzc6866d72013-09-22 15:45:31 -0700522 }
523
Roy Franz4a9f3a72013-09-22 15:45:32 -0700524 /*
525 * We know source/dest won't overlap since both memory ranges
526 * have been allocated by UEFI, so we can safely use memcpy.
527 */
528 memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
Roy Franzc6866d72013-09-22 15:45:31 -0700529
Roy Franz4a9f3a72013-09-22 15:45:32 -0700530 /* Return the new address of the relocated image. */
531 *image_addr = new_addr;
Roy Franzc6866d72013-09-22 15:45:31 -0700532
533 return status;
534}
Roy Franz5fef3872013-09-22 15:45:33 -0700535
536/*
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500537 * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
538 * This overestimates for surrogates, but that is okay.
539 */
540static int efi_utf8_bytes(u16 c)
541{
542 return 1 + (c >= 0x80) + (c >= 0x800);
543}
544
545/*
546 * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
547 */
548static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
549{
550 unsigned int c;
551
552 while (n--) {
553 c = *src++;
554 if (n && c >= 0xd800 && c <= 0xdbff &&
555 *src >= 0xdc00 && *src <= 0xdfff) {
556 c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
557 src++;
558 n--;
559 }
560 if (c >= 0xd800 && c <= 0xdfff)
561 c = 0xfffd; /* Unmatched surrogate */
562 if (c < 0x80) {
563 *dst++ = c;
564 continue;
565 }
566 if (c < 0x800) {
567 *dst++ = 0xc0 + (c >> 6);
568 goto t1;
569 }
570 if (c < 0x10000) {
571 *dst++ = 0xe0 + (c >> 12);
572 goto t2;
573 }
574 *dst++ = 0xf0 + (c >> 18);
575 *dst++ = 0x80 + ((c >> 12) & 0x3f);
576 t2:
577 *dst++ = 0x80 + ((c >> 6) & 0x3f);
578 t1:
579 *dst++ = 0x80 + (c & 0x3f);
580 }
581
582 return dst;
583}
584
585/*
Roy Franz5fef3872013-09-22 15:45:33 -0700586 * Convert the unicode UEFI command line to ASCII to pass to kernel.
587 * Size of memory allocated return in *cmd_line_len.
588 * Returns NULL on error.
589 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200590char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
591 efi_loaded_image_t *image,
592 int *cmd_line_len)
Roy Franz5fef3872013-09-22 15:45:33 -0700593{
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500594 const u16 *s2;
Roy Franz5fef3872013-09-22 15:45:33 -0700595 u8 *s1 = NULL;
596 unsigned long cmdline_addr = 0;
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500597 int load_options_chars = image->load_options_size / 2; /* UTF-16 */
598 const u16 *options = image->load_options;
599 int options_bytes = 0; /* UTF-8 bytes */
600 int options_chars = 0; /* UTF-16 chars */
Roy Franz5fef3872013-09-22 15:45:33 -0700601 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -0700602 u16 zero = 0;
603
604 if (options) {
605 s2 = options;
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500606 while (*s2 && *s2 != '\n'
607 && options_chars < load_options_chars) {
608 options_bytes += efi_utf8_bytes(*s2++);
609 options_chars++;
Roy Franz5fef3872013-09-22 15:45:33 -0700610 }
611 }
612
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500613 if (!options_chars) {
Roy Franz5fef3872013-09-22 15:45:33 -0700614 /* No command line options, so return empty string*/
Roy Franz5fef3872013-09-22 15:45:33 -0700615 options = &zero;
616 }
617
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500618 options_bytes++; /* NUL termination */
Leif Lindholm9403e462014-04-04 13:25:46 +0100619
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500620 status = efi_low_alloc(sys_table_arg, options_bytes, 0, &cmdline_addr);
Roy Franz5fef3872013-09-22 15:45:33 -0700621 if (status != EFI_SUCCESS)
622 return NULL;
623
624 s1 = (u8 *)cmdline_addr;
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500625 s2 = (const u16 *)options;
Roy Franz5fef3872013-09-22 15:45:33 -0700626
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500627 s1 = efi_utf16_to_utf8(s1, s2, options_chars);
Roy Franz5fef3872013-09-22 15:45:33 -0700628 *s1 = '\0';
629
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500630 *cmd_line_len = options_bytes;
Roy Franz5fef3872013-09-22 15:45:33 -0700631 return (char *)cmdline_addr;
632}