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