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