Merge branches 'device-properties' and 'acpi-misc'
[cascardo/linux.git] / drivers / firmware / efi / libstub / 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
13 #include <linux/efi.h>
14 #include <asm/efi.h>
15
16 #include "efistub.h"
17
18 /*
19  * Some firmware implementations have problems reading files in one go.
20  * A read chunk size of 1MB seems to work for most platforms.
21  *
22  * Unfortunately, reading files in chunks triggers *other* bugs on some
23  * platforms, so we provide a way to disable this workaround, which can
24  * be done by passing "efi=nochunk" on the EFI boot stub command line.
25  *
26  * If you experience issues with initrd images being corrupt it's worth
27  * trying efi=nochunk, but chunking is enabled by default because there
28  * are far more machines that require the workaround than those that
29  * break with it enabled.
30  */
31 #define EFI_READ_CHUNK_SIZE     (1024 * 1024)
32
33 static unsigned long __chunk_size = EFI_READ_CHUNK_SIZE;
34
35 /*
36  * Allow the platform to override the allocation granularity: this allows
37  * systems that have the capability to run with a larger page size to deal
38  * with the allocations for initrd and fdt more efficiently.
39  */
40 #ifndef EFI_ALLOC_ALIGN
41 #define EFI_ALLOC_ALIGN         EFI_PAGE_SIZE
42 #endif
43
44 #define EFI_MMAP_NR_SLACK_SLOTS 8
45
46 struct file_info {
47         efi_file_handle_t *handle;
48         u64 size;
49 };
50
51 void efi_printk(efi_system_table_t *sys_table_arg, char *str)
52 {
53         char *s8;
54
55         for (s8 = str; *s8; s8++) {
56                 efi_char16_t ch[2] = { 0 };
57
58                 ch[0] = *s8;
59                 if (*s8 == '\n') {
60                         efi_char16_t nl[2] = { '\r', 0 };
61                         efi_char16_printk(sys_table_arg, nl);
62                 }
63
64                 efi_char16_printk(sys_table_arg, ch);
65         }
66 }
67
68 static inline bool mmap_has_headroom(unsigned long buff_size,
69                                      unsigned long map_size,
70                                      unsigned long desc_size)
71 {
72         unsigned long slack = buff_size - map_size;
73
74         return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS;
75 }
76
77 efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
78                                 struct efi_boot_memmap *map)
79 {
80         efi_memory_desc_t *m = NULL;
81         efi_status_t status;
82         unsigned long key;
83         u32 desc_version;
84
85         *map->desc_size =       sizeof(*m);
86         *map->map_size =        *map->desc_size * 32;
87         *map->buff_size =       *map->map_size;
88 again:
89         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
90                                 *map->map_size, (void **)&m);
91         if (status != EFI_SUCCESS)
92                 goto fail;
93
94         *map->desc_size = 0;
95         key = 0;
96         status = efi_call_early(get_memory_map, map->map_size, m,
97                                 &key, map->desc_size, &desc_version);
98         if (status == EFI_BUFFER_TOO_SMALL ||
99             !mmap_has_headroom(*map->buff_size, *map->map_size,
100                                *map->desc_size)) {
101                 efi_call_early(free_pool, m);
102                 /*
103                  * Make sure there is some entries of headroom so that the
104                  * buffer can be reused for a new map after allocations are
105                  * no longer permitted.  Its unlikely that the map will grow to
106                  * exceed this headroom once we are ready to trigger
107                  * ExitBootServices()
108                  */
109                 *map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS;
110                 *map->buff_size = *map->map_size;
111                 goto again;
112         }
113
114         if (status != EFI_SUCCESS)
115                 efi_call_early(free_pool, m);
116
117         if (map->key_ptr && status == EFI_SUCCESS)
118                 *map->key_ptr = key;
119         if (map->desc_ver && status == EFI_SUCCESS)
120                 *map->desc_ver = desc_version;
121
122 fail:
123         *map->map = m;
124         return status;
125 }
126
127
128 unsigned long get_dram_base(efi_system_table_t *sys_table_arg)
129 {
130         efi_status_t status;
131         unsigned long map_size, buff_size;
132         unsigned long membase  = EFI_ERROR;
133         struct efi_memory_map map;
134         efi_memory_desc_t *md;
135         struct efi_boot_memmap boot_map;
136
137         boot_map.map =          (efi_memory_desc_t **)&map.map;
138         boot_map.map_size =     &map_size;
139         boot_map.desc_size =    &map.desc_size;
140         boot_map.desc_ver =     NULL;
141         boot_map.key_ptr =      NULL;
142         boot_map.buff_size =    &buff_size;
143
144         status = efi_get_memory_map(sys_table_arg, &boot_map);
145         if (status != EFI_SUCCESS)
146                 return membase;
147
148         map.map_end = map.map + map_size;
149
150         for_each_efi_memory_desc_in_map(&map, md) {
151                 if (md->attribute & EFI_MEMORY_WB) {
152                         if (membase > md->phys_addr)
153                                 membase = md->phys_addr;
154                 }
155         }
156
157         efi_call_early(free_pool, map.map);
158
159         return membase;
160 }
161
162 /*
163  * Allocate at the highest possible address that is not above 'max'.
164  */
165 efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
166                             unsigned long size, unsigned long align,
167                             unsigned long *addr, unsigned long max)
168 {
169         unsigned long map_size, desc_size, buff_size;
170         efi_memory_desc_t *map;
171         efi_status_t status;
172         unsigned long nr_pages;
173         u64 max_addr = 0;
174         int i;
175         struct efi_boot_memmap boot_map;
176
177         boot_map.map =          &map;
178         boot_map.map_size =     &map_size;
179         boot_map.desc_size =    &desc_size;
180         boot_map.desc_ver =     NULL;
181         boot_map.key_ptr =      NULL;
182         boot_map.buff_size =    &buff_size;
183
184         status = efi_get_memory_map(sys_table_arg, &boot_map);
185         if (status != EFI_SUCCESS)
186                 goto fail;
187
188         /*
189          * Enforce minimum alignment that EFI requires when requesting
190          * a specific address.  We are doing page-based allocations,
191          * so we must be aligned to a page.
192          */
193         if (align < EFI_ALLOC_ALIGN)
194                 align = EFI_ALLOC_ALIGN;
195
196         nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
197 again:
198         for (i = 0; i < map_size / desc_size; i++) {
199                 efi_memory_desc_t *desc;
200                 unsigned long m = (unsigned long)map;
201                 u64 start, end;
202
203                 desc = (efi_memory_desc_t *)(m + (i * desc_size));
204                 if (desc->type != EFI_CONVENTIONAL_MEMORY)
205                         continue;
206
207                 if (desc->num_pages < nr_pages)
208                         continue;
209
210                 start = desc->phys_addr;
211                 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
212
213                 if (end > max)
214                         end = max;
215
216                 if ((start + size) > end)
217                         continue;
218
219                 if (round_down(end - size, align) < start)
220                         continue;
221
222                 start = round_down(end - size, align);
223
224                 /*
225                  * Don't allocate at 0x0. It will confuse code that
226                  * checks pointers against NULL.
227                  */
228                 if (start == 0x0)
229                         continue;
230
231                 if (start > max_addr)
232                         max_addr = start;
233         }
234
235         if (!max_addr)
236                 status = EFI_NOT_FOUND;
237         else {
238                 status = efi_call_early(allocate_pages,
239                                         EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
240                                         nr_pages, &max_addr);
241                 if (status != EFI_SUCCESS) {
242                         max = max_addr;
243                         max_addr = 0;
244                         goto again;
245                 }
246
247                 *addr = max_addr;
248         }
249
250         efi_call_early(free_pool, map);
251 fail:
252         return status;
253 }
254
255 /*
256  * Allocate at the lowest possible address.
257  */
258 efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
259                            unsigned long size, unsigned long align,
260                            unsigned long *addr)
261 {
262         unsigned long map_size, desc_size, buff_size;
263         efi_memory_desc_t *map;
264         efi_status_t status;
265         unsigned long nr_pages;
266         int i;
267         struct efi_boot_memmap boot_map;
268
269         boot_map.map =          &map;
270         boot_map.map_size =     &map_size;
271         boot_map.desc_size =    &desc_size;
272         boot_map.desc_ver =     NULL;
273         boot_map.key_ptr =      NULL;
274         boot_map.buff_size =    &buff_size;
275
276         status = efi_get_memory_map(sys_table_arg, &boot_map);
277         if (status != EFI_SUCCESS)
278                 goto fail;
279
280         /*
281          * Enforce minimum alignment that EFI requires when requesting
282          * a specific address.  We are doing page-based allocations,
283          * so we must be aligned to a page.
284          */
285         if (align < EFI_ALLOC_ALIGN)
286                 align = EFI_ALLOC_ALIGN;
287
288         nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
289         for (i = 0; i < map_size / desc_size; i++) {
290                 efi_memory_desc_t *desc;
291                 unsigned long m = (unsigned long)map;
292                 u64 start, end;
293
294                 desc = (efi_memory_desc_t *)(m + (i * desc_size));
295
296                 if (desc->type != EFI_CONVENTIONAL_MEMORY)
297                         continue;
298
299                 if (desc->num_pages < nr_pages)
300                         continue;
301
302                 start = desc->phys_addr;
303                 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
304
305                 /*
306                  * Don't allocate at 0x0. It will confuse code that
307                  * checks pointers against NULL. Skip the first 8
308                  * bytes so we start at a nice even number.
309                  */
310                 if (start == 0x0)
311                         start += 8;
312
313                 start = round_up(start, align);
314                 if ((start + size) > end)
315                         continue;
316
317                 status = efi_call_early(allocate_pages,
318                                         EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
319                                         nr_pages, &start);
320                 if (status == EFI_SUCCESS) {
321                         *addr = start;
322                         break;
323                 }
324         }
325
326         if (i == map_size / desc_size)
327                 status = EFI_NOT_FOUND;
328
329         efi_call_early(free_pool, map);
330 fail:
331         return status;
332 }
333
334 void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
335               unsigned long addr)
336 {
337         unsigned long nr_pages;
338
339         if (!size)
340                 return;
341
342         nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
343         efi_call_early(free_pages, addr, nr_pages);
344 }
345
346 /*
347  * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
348  * option, e.g. efi=nochunk.
349  *
350  * It should be noted that efi= is parsed in two very different
351  * environments, first in the early boot environment of the EFI boot
352  * stub, and subsequently during the kernel boot.
353  */
354 efi_status_t efi_parse_options(char *cmdline)
355 {
356         char *str;
357
358         /*
359          * If no EFI parameters were specified on the cmdline we've got
360          * nothing to do.
361          */
362         str = strstr(cmdline, "efi=");
363         if (!str)
364                 return EFI_SUCCESS;
365
366         /* Skip ahead to first argument */
367         str += strlen("efi=");
368
369         /*
370          * Remember, because efi= is also used by the kernel we need to
371          * skip over arguments we don't understand.
372          */
373         while (*str) {
374                 if (!strncmp(str, "nochunk", 7)) {
375                         str += strlen("nochunk");
376                         __chunk_size = -1UL;
377                 }
378
379                 /* Group words together, delimited by "," */
380                 while (*str && *str != ',')
381                         str++;
382
383                 if (*str == ',')
384                         str++;
385         }
386
387         return EFI_SUCCESS;
388 }
389
390 /*
391  * Check the cmdline for a LILO-style file= arguments.
392  *
393  * We only support loading a file from the same filesystem as
394  * the kernel image.
395  */
396 efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
397                                   efi_loaded_image_t *image,
398                                   char *cmd_line, char *option_string,
399                                   unsigned long max_addr,
400                                   unsigned long *load_addr,
401                                   unsigned long *load_size)
402 {
403         struct file_info *files;
404         unsigned long file_addr;
405         u64 file_size_total;
406         efi_file_handle_t *fh = NULL;
407         efi_status_t status;
408         int nr_files;
409         char *str;
410         int i, j, k;
411
412         file_addr = 0;
413         file_size_total = 0;
414
415         str = cmd_line;
416
417         j = 0;                  /* See close_handles */
418
419         if (!load_addr || !load_size)
420                 return EFI_INVALID_PARAMETER;
421
422         *load_addr = 0;
423         *load_size = 0;
424
425         if (!str || !*str)
426                 return EFI_SUCCESS;
427
428         for (nr_files = 0; *str; nr_files++) {
429                 str = strstr(str, option_string);
430                 if (!str)
431                         break;
432
433                 str += strlen(option_string);
434
435                 /* Skip any leading slashes */
436                 while (*str == '/' || *str == '\\')
437                         str++;
438
439                 while (*str && *str != ' ' && *str != '\n')
440                         str++;
441         }
442
443         if (!nr_files)
444                 return EFI_SUCCESS;
445
446         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
447                                 nr_files * sizeof(*files), (void **)&files);
448         if (status != EFI_SUCCESS) {
449                 pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
450                 goto fail;
451         }
452
453         str = cmd_line;
454         for (i = 0; i < nr_files; i++) {
455                 struct file_info *file;
456                 efi_char16_t filename_16[256];
457                 efi_char16_t *p;
458
459                 str = strstr(str, option_string);
460                 if (!str)
461                         break;
462
463                 str += strlen(option_string);
464
465                 file = &files[i];
466                 p = filename_16;
467
468                 /* Skip any leading slashes */
469                 while (*str == '/' || *str == '\\')
470                         str++;
471
472                 while (*str && *str != ' ' && *str != '\n') {
473                         if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
474                                 break;
475
476                         if (*str == '/') {
477                                 *p++ = '\\';
478                                 str++;
479                         } else {
480                                 *p++ = *str++;
481                         }
482                 }
483
484                 *p = '\0';
485
486                 /* Only open the volume once. */
487                 if (!i) {
488                         status = efi_open_volume(sys_table_arg, image,
489                                                  (void **)&fh);
490                         if (status != EFI_SUCCESS)
491                                 goto free_files;
492                 }
493
494                 status = efi_file_size(sys_table_arg, fh, filename_16,
495                                        (void **)&file->handle, &file->size);
496                 if (status != EFI_SUCCESS)
497                         goto close_handles;
498
499                 file_size_total += file->size;
500         }
501
502         if (file_size_total) {
503                 unsigned long addr;
504
505                 /*
506                  * Multiple files need to be at consecutive addresses in memory,
507                  * so allocate enough memory for all the files.  This is used
508                  * for loading multiple files.
509                  */
510                 status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
511                                     &file_addr, max_addr);
512                 if (status != EFI_SUCCESS) {
513                         pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
514                         goto close_handles;
515                 }
516
517                 /* We've run out of free low memory. */
518                 if (file_addr > max_addr) {
519                         pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
520                         status = EFI_INVALID_PARAMETER;
521                         goto free_file_total;
522                 }
523
524                 addr = file_addr;
525                 for (j = 0; j < nr_files; j++) {
526                         unsigned long size;
527
528                         size = files[j].size;
529                         while (size) {
530                                 unsigned long chunksize;
531                                 if (size > __chunk_size)
532                                         chunksize = __chunk_size;
533                                 else
534                                         chunksize = size;
535
536                                 status = efi_file_read(files[j].handle,
537                                                        &chunksize,
538                                                        (void *)addr);
539                                 if (status != EFI_SUCCESS) {
540                                         pr_efi_err(sys_table_arg, "Failed to read file\n");
541                                         goto free_file_total;
542                                 }
543                                 addr += chunksize;
544                                 size -= chunksize;
545                         }
546
547                         efi_file_close(files[j].handle);
548                 }
549
550         }
551
552         efi_call_early(free_pool, files);
553
554         *load_addr = file_addr;
555         *load_size = file_size_total;
556
557         return status;
558
559 free_file_total:
560         efi_free(sys_table_arg, file_size_total, file_addr);
561
562 close_handles:
563         for (k = j; k < i; k++)
564                 efi_file_close(files[k].handle);
565 free_files:
566         efi_call_early(free_pool, files);
567 fail:
568         *load_addr = 0;
569         *load_size = 0;
570
571         return status;
572 }
573 /*
574  * Relocate a kernel image, either compressed or uncompressed.
575  * In the ARM64 case, all kernel images are currently
576  * uncompressed, and as such when we relocate it we need to
577  * allocate additional space for the BSS segment. Any low
578  * memory that this function should avoid needs to be
579  * unavailable in the EFI memory map, as if the preferred
580  * address is not available the lowest available address will
581  * be used.
582  */
583 efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
584                                  unsigned long *image_addr,
585                                  unsigned long image_size,
586                                  unsigned long alloc_size,
587                                  unsigned long preferred_addr,
588                                  unsigned long alignment)
589 {
590         unsigned long cur_image_addr;
591         unsigned long new_addr = 0;
592         efi_status_t status;
593         unsigned long nr_pages;
594         efi_physical_addr_t efi_addr = preferred_addr;
595
596         if (!image_addr || !image_size || !alloc_size)
597                 return EFI_INVALID_PARAMETER;
598         if (alloc_size < image_size)
599                 return EFI_INVALID_PARAMETER;
600
601         cur_image_addr = *image_addr;
602
603         /*
604          * The EFI firmware loader could have placed the kernel image
605          * anywhere in memory, but the kernel has restrictions on the
606          * max physical address it can run at.  Some architectures
607          * also have a prefered address, so first try to relocate
608          * to the preferred address.  If that fails, allocate as low
609          * as possible while respecting the required alignment.
610          */
611         nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
612         status = efi_call_early(allocate_pages,
613                                 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
614                                 nr_pages, &efi_addr);
615         new_addr = efi_addr;
616         /*
617          * If preferred address allocation failed allocate as low as
618          * possible.
619          */
620         if (status != EFI_SUCCESS) {
621                 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
622                                        &new_addr);
623         }
624         if (status != EFI_SUCCESS) {
625                 pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
626                 return status;
627         }
628
629         /*
630          * We know source/dest won't overlap since both memory ranges
631          * have been allocated by UEFI, so we can safely use memcpy.
632          */
633         memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
634
635         /* Return the new address of the relocated image. */
636         *image_addr = new_addr;
637
638         return status;
639 }
640
641 /*
642  * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
643  * This overestimates for surrogates, but that is okay.
644  */
645 static int efi_utf8_bytes(u16 c)
646 {
647         return 1 + (c >= 0x80) + (c >= 0x800);
648 }
649
650 /*
651  * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
652  */
653 static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
654 {
655         unsigned int c;
656
657         while (n--) {
658                 c = *src++;
659                 if (n && c >= 0xd800 && c <= 0xdbff &&
660                     *src >= 0xdc00 && *src <= 0xdfff) {
661                         c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
662                         src++;
663                         n--;
664                 }
665                 if (c >= 0xd800 && c <= 0xdfff)
666                         c = 0xfffd; /* Unmatched surrogate */
667                 if (c < 0x80) {
668                         *dst++ = c;
669                         continue;
670                 }
671                 if (c < 0x800) {
672                         *dst++ = 0xc0 + (c >> 6);
673                         goto t1;
674                 }
675                 if (c < 0x10000) {
676                         *dst++ = 0xe0 + (c >> 12);
677                         goto t2;
678                 }
679                 *dst++ = 0xf0 + (c >> 18);
680                 *dst++ = 0x80 + ((c >> 12) & 0x3f);
681         t2:
682                 *dst++ = 0x80 + ((c >> 6) & 0x3f);
683         t1:
684                 *dst++ = 0x80 + (c & 0x3f);
685         }
686
687         return dst;
688 }
689
690 #ifndef MAX_CMDLINE_ADDRESS
691 #define MAX_CMDLINE_ADDRESS     ULONG_MAX
692 #endif
693
694 /*
695  * Convert the unicode UEFI command line to ASCII to pass to kernel.
696  * Size of memory allocated return in *cmd_line_len.
697  * Returns NULL on error.
698  */
699 char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
700                           efi_loaded_image_t *image,
701                           int *cmd_line_len)
702 {
703         const u16 *s2;
704         u8 *s1 = NULL;
705         unsigned long cmdline_addr = 0;
706         int load_options_chars = image->load_options_size / 2; /* UTF-16 */
707         const u16 *options = image->load_options;
708         int options_bytes = 0;  /* UTF-8 bytes */
709         int options_chars = 0;  /* UTF-16 chars */
710         efi_status_t status;
711         u16 zero = 0;
712
713         if (options) {
714                 s2 = options;
715                 while (*s2 && *s2 != '\n'
716                        && options_chars < load_options_chars) {
717                         options_bytes += efi_utf8_bytes(*s2++);
718                         options_chars++;
719                 }
720         }
721
722         if (!options_chars) {
723                 /* No command line options, so return empty string*/
724                 options = &zero;
725         }
726
727         options_bytes++;        /* NUL termination */
728
729         status = efi_high_alloc(sys_table_arg, options_bytes, 0,
730                                 &cmdline_addr, MAX_CMDLINE_ADDRESS);
731         if (status != EFI_SUCCESS)
732                 return NULL;
733
734         s1 = (u8 *)cmdline_addr;
735         s2 = (const u16 *)options;
736
737         s1 = efi_utf16_to_utf8(s1, s2, options_chars);
738         *s1 = '\0';
739
740         *cmd_line_len = options_bytes;
741         return (char *)cmdline_addr;
742 }
743
744 /*
745  * Handle calling ExitBootServices according to the requirements set out by the
746  * spec.  Obtains the current memory map, and returns that info after calling
747  * ExitBootServices.  The client must specify a function to perform any
748  * processing of the memory map data prior to ExitBootServices.  A client
749  * specific structure may be passed to the function via priv.  The client
750  * function may be called multiple times.
751  */
752 efi_status_t efi_exit_boot_services(efi_system_table_t *sys_table_arg,
753                                     void *handle,
754                                     struct efi_boot_memmap *map,
755                                     void *priv,
756                                     efi_exit_boot_map_processing priv_func)
757 {
758         efi_status_t status;
759
760         status = efi_get_memory_map(sys_table_arg, map);
761
762         if (status != EFI_SUCCESS)
763                 goto fail;
764
765         status = priv_func(sys_table_arg, map, priv);
766         if (status != EFI_SUCCESS)
767                 goto free_map;
768
769         status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
770
771         if (status == EFI_INVALID_PARAMETER) {
772                 /*
773                  * The memory map changed between efi_get_memory_map() and
774                  * exit_boot_services().  Per the UEFI Spec v2.6, Section 6.4:
775                  * EFI_BOOT_SERVICES.ExitBootServices we need to get the
776                  * updated map, and try again.  The spec implies one retry
777                  * should be sufficent, which is confirmed against the EDK2
778                  * implementation.  Per the spec, we can only invoke
779                  * get_memory_map() and exit_boot_services() - we cannot alloc
780                  * so efi_get_memory_map() cannot be used, and we must reuse
781                  * the buffer.  For all practical purposes, the headroom in the
782                  * buffer should account for any changes in the map so the call
783                  * to get_memory_map() is expected to succeed here.
784                  */
785                 *map->map_size = *map->buff_size;
786                 status = efi_call_early(get_memory_map,
787                                         map->map_size,
788                                         *map->map,
789                                         map->key_ptr,
790                                         map->desc_size,
791                                         map->desc_ver);
792
793                 /* exit_boot_services() was called, thus cannot free */
794                 if (status != EFI_SUCCESS)
795                         goto fail;
796
797                 status = priv_func(sys_table_arg, map, priv);
798                 /* exit_boot_services() was called, thus cannot free */
799                 if (status != EFI_SUCCESS)
800                         goto fail;
801
802                 status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
803         }
804
805         /* exit_boot_services() was called, thus cannot free */
806         if (status != EFI_SUCCESS)
807                 goto fail;
808
809         return EFI_SUCCESS;
810
811 free_map:
812         efi_call_early(free_pool, *map->map);
813 fail:
814         return status;
815 }