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