]> Git Repo - J-u-boot.git/blob - lib/efi_loader/efi_memory.c
Merge tag 'u-boot-imx-master-20250127' of https://gitlab.denx.de/u-boot/custodians...
[J-u-boot.git] / lib / efi_loader / efi_memory.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI application memory management
4  *
5  *  Copyright (c) 2016 Alexander Graf
6  */
7
8 #define LOG_CATEGORY LOGC_EFI
9
10 #include <efi_loader.h>
11 #include <init.h>
12 #include <lmb.h>
13 #include <log.h>
14 #include <malloc.h>
15 #include <mapmem.h>
16 #include <watchdog.h>
17 #include <asm/cache.h>
18 #include <asm/global_data.h>
19 #include <asm/sections.h>
20 #include <linux/list_sort.h>
21 #include <linux/sizes.h>
22
23 DECLARE_GLOBAL_DATA_PTR;
24
25 /* Magic number identifying memory allocated from pool */
26 #define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2
27
28 efi_uintn_t efi_memory_map_key;
29
30 struct efi_mem_list {
31         struct list_head link;
32         struct efi_mem_desc desc;
33 };
34
35 #define EFI_CARVE_NO_OVERLAP            -1
36 #define EFI_CARVE_LOOP_AGAIN            -2
37 #define EFI_CARVE_OVERLAPS_NONRAM       -3
38 #define EFI_CARVE_OUT_OF_RESOURCES      -4
39
40 /* This list contains all memory map items */
41 static LIST_HEAD(efi_mem);
42
43 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
44 void *efi_bounce_buffer;
45 #endif
46
47 /**
48  * struct efi_pool_allocation - memory block allocated from pool
49  *
50  * @num_pages:  number of pages allocated
51  * @checksum:   checksum
52  * @data:       allocated pool memory
53  *
54  * U-Boot services each UEFI AllocatePool() request as a separate
55  * (multiple) page allocation. We have to track the number of pages
56  * to be able to free the correct amount later.
57  *
58  * The checksum calculated in function checksum() is used in FreePool() to avoid
59  * freeing memory not allocated by AllocatePool() and duplicate freeing.
60  *
61  * EFI requires 8 byte alignment for pool allocations, so we can
62  * prepend each allocation with these header fields.
63  */
64 struct efi_pool_allocation {
65         u64 num_pages;
66         u64 checksum;
67         char data[] __aligned(ARCH_DMA_MINALIGN);
68 };
69
70 /**
71  * checksum() - calculate checksum for memory allocated from pool
72  *
73  * @alloc:      allocation header
74  * Return:      checksum, always non-zero
75  */
76 static u64 checksum(struct efi_pool_allocation *alloc)
77 {
78         u64 addr = (uintptr_t)alloc;
79         u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^
80                   EFI_ALLOC_POOL_MAGIC;
81         if (!ret)
82                 ++ret;
83         return ret;
84 }
85
86 /**
87  * efi_mem_cmp() - comparator function for sorting memory map
88  *
89  * Sorts the memory list from highest address to lowest address
90  *
91  * When allocating memory we should always start from the highest
92  * address chunk, so sort the memory list such that the first list
93  * iterator gets the highest address and goes lower from there.
94  *
95  * @priv:       unused
96  * @a:          first memory area
97  * @b:          second memory area
98  * Return:      1 if @a is before @b, -1 if @b is before @a, 0 if equal
99  */
100 static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
101 {
102         struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
103         struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
104
105         if (mema->desc.physical_start == memb->desc.physical_start)
106                 return 0;
107         else if (mema->desc.physical_start < memb->desc.physical_start)
108                 return 1;
109         else
110                 return -1;
111 }
112
113 /**
114  * desc_get_end() - get end address of memory area
115  *
116  * @desc:       memory descriptor
117  * Return:      end address + 1
118  */
119 static uint64_t desc_get_end(struct efi_mem_desc *desc)
120 {
121         return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
122 }
123
124 /**
125  * efi_mem_sort() - sort memory map
126  *
127  * Sort the memory map and then try to merge adjacent memory areas.
128  */
129 static void efi_mem_sort(void)
130 {
131         struct efi_mem_list *lmem;
132         struct efi_mem_list *prevmem = NULL;
133         bool merge_again = true;
134
135         list_sort(NULL, &efi_mem, efi_mem_cmp);
136
137         /* Now merge entries that can be merged */
138         while (merge_again) {
139                 merge_again = false;
140                 list_for_each_entry(lmem, &efi_mem, link) {
141                         struct efi_mem_desc *prev;
142                         struct efi_mem_desc *cur;
143                         uint64_t pages;
144
145                         if (!prevmem) {
146                                 prevmem = lmem;
147                                 continue;
148                         }
149
150                         cur = &lmem->desc;
151                         prev = &prevmem->desc;
152
153                         if ((desc_get_end(cur) == prev->physical_start) &&
154                             (prev->type == cur->type) &&
155                             (prev->attribute == cur->attribute)) {
156                                 /* There is an existing map before, reuse it */
157                                 pages = cur->num_pages;
158                                 prev->num_pages += pages;
159                                 prev->physical_start -= pages << EFI_PAGE_SHIFT;
160                                 prev->virtual_start -= pages << EFI_PAGE_SHIFT;
161                                 list_del(&lmem->link);
162                                 free(lmem);
163
164                                 merge_again = true;
165                                 break;
166                         }
167
168                         prevmem = lmem;
169                 }
170         }
171 }
172
173 /**
174  * efi_mem_carve_out() - unmap memory region
175  *
176  * @map:                        memory map
177  * @carve_desc:                 memory region to unmap
178  * @overlap_conventional:       the carved out region may only overlap free,
179  *                              or conventional memory
180  * Return:                      the number of overlapping pages which have been
181  *                              removed from the map,
182  *                              EFI_CARVE_NO_OVERLAP, if the regions don't
183  *                              overlap, EFI_CARVE_OVERLAPS_NONRAM, if the carve
184  *                              and map overlap, and the map contains anything
185  *                              but free ram(only when overlap_conventional is
186  *                              true),
187  *                              EFI_CARVE_LOOP_AGAIN, if the mapping list should
188  *                              be traversed again, as it has been altered.
189  *
190  * Unmaps all memory occupied by the carve_desc region from the list entry
191  * pointed to by map.
192  *
193  * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
194  * to re-add the already carved out pages to the mapping.
195  */
196 static s64 efi_mem_carve_out(struct efi_mem_list *map,
197                              struct efi_mem_desc *carve_desc,
198                              bool overlap_conventional)
199 {
200         struct efi_mem_list *newmap;
201         struct efi_mem_desc *map_desc = &map->desc;
202         uint64_t map_start = map_desc->physical_start;
203         uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
204         uint64_t carve_start = carve_desc->physical_start;
205         uint64_t carve_end = carve_start +
206                              (carve_desc->num_pages << EFI_PAGE_SHIFT);
207
208         /* check whether we're overlapping */
209         if ((carve_end <= map_start) || (carve_start >= map_end))
210                 return EFI_CARVE_NO_OVERLAP;
211
212         /* We're overlapping with non-RAM, warn the caller if desired */
213         if (overlap_conventional && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
214                 return EFI_CARVE_OVERLAPS_NONRAM;
215
216         /* Sanitize carve_start and carve_end to lie within our bounds */
217         carve_start = max(carve_start, map_start);
218         carve_end = min(carve_end, map_end);
219
220         /* Carving at the beginning of our map? Just move it! */
221         if (carve_start == map_start) {
222                 if (map_end == carve_end) {
223                         /* Full overlap, just remove map */
224                         list_del(&map->link);
225                         free(map);
226                 } else {
227                         map->desc.physical_start = carve_end;
228                         map->desc.virtual_start = carve_end;
229                         map->desc.num_pages = (map_end - carve_end)
230                                               >> EFI_PAGE_SHIFT;
231                 }
232
233                 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
234         }
235
236         /*
237          * Overlapping maps, just split the list map at carve_start,
238          * it will get moved or removed in the next iteration.
239          *
240          * [ map_desc |__carve_start__| newmap ]
241          */
242
243         /* Create a new map from [ carve_start ... map_end ] */
244         newmap = calloc(1, sizeof(*newmap));
245         if (!newmap)
246                 return EFI_CARVE_OUT_OF_RESOURCES;
247         newmap->desc = map->desc;
248         newmap->desc.physical_start = carve_start;
249         newmap->desc.virtual_start = carve_start;
250         newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
251         /* Insert before current entry (descending address order) */
252         list_add_tail(&newmap->link, &map->link);
253
254         /* Shrink the map to [ map_start ... carve_start ] */
255         map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
256
257         return EFI_CARVE_LOOP_AGAIN;
258 }
259
260 /**
261  * efi_add_memory_map_pg() - add pages to the memory map
262  *
263  * @start:                      start address, must be a multiple of
264  *                              EFI_PAGE_SIZE
265  * @pages:                      number of pages to add
266  * @memory_type:                type of memory added
267  * @overlap_conventional:       region may only overlap free(conventional)
268  *                              memory
269  * Return:                      status code
270  */
271 efi_status_t efi_add_memory_map_pg(u64 start, u64 pages,
272                                    int memory_type,
273                                    bool overlap_conventional)
274 {
275         struct efi_mem_list *lmem;
276         struct efi_mem_list *newlist;
277         bool carve_again;
278         uint64_t carved_pages = 0;
279         struct efi_event *evt;
280
281         EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__,
282                   start, pages, memory_type, overlap_conventional ?
283                   "yes" : "no");
284
285         if (memory_type >= EFI_MAX_MEMORY_TYPE)
286                 return EFI_INVALID_PARAMETER;
287
288         if (!pages)
289                 return EFI_SUCCESS;
290
291         ++efi_memory_map_key;
292         newlist = calloc(1, sizeof(*newlist));
293         if (!newlist)
294                 return EFI_OUT_OF_RESOURCES;
295         newlist->desc.type = memory_type;
296         newlist->desc.physical_start = start;
297         newlist->desc.virtual_start = start;
298         newlist->desc.num_pages = pages;
299
300         switch (memory_type) {
301         case EFI_RUNTIME_SERVICES_CODE:
302         case EFI_RUNTIME_SERVICES_DATA:
303                 newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
304                 break;
305         case EFI_MMAP_IO:
306                 newlist->desc.attribute = EFI_MEMORY_RUNTIME;
307                 break;
308         default:
309                 newlist->desc.attribute = EFI_MEMORY_WB;
310                 break;
311         }
312
313         /* Add our new map */
314         do {
315                 carve_again = false;
316                 list_for_each_entry(lmem, &efi_mem, link) {
317                         s64 r;
318
319                         r = efi_mem_carve_out(lmem, &newlist->desc,
320                                               overlap_conventional);
321                         switch (r) {
322                         case EFI_CARVE_OUT_OF_RESOURCES:
323                                 free(newlist);
324                                 return EFI_OUT_OF_RESOURCES;
325                         case EFI_CARVE_OVERLAPS_NONRAM:
326                                 /*
327                                  * The user requested to only have RAM overlaps,
328                                  * but we hit a non-RAM region. Error out.
329                                  */
330                                 free(newlist);
331                                 return EFI_NO_MAPPING;
332                         case EFI_CARVE_NO_OVERLAP:
333                                 /* Just ignore this list entry */
334                                 break;
335                         case EFI_CARVE_LOOP_AGAIN:
336                                 /*
337                                  * We split an entry, but need to loop through
338                                  * the list again to actually carve it.
339                                  */
340                                 carve_again = true;
341                                 break;
342                         default:
343                                 /* We carved a number of pages */
344                                 carved_pages += r;
345                                 carve_again = true;
346                                 break;
347                         }
348
349                         if (carve_again) {
350                                 /* The list changed, we need to start over */
351                                 break;
352                         }
353                 }
354         } while (carve_again);
355
356         if (overlap_conventional && (carved_pages != pages)) {
357                 /*
358                  * The payload wanted to have RAM overlaps, but we overlapped
359                  * with an unallocated region. Error out.
360                  */
361                 free(newlist);
362                 return EFI_NO_MAPPING;
363         }
364
365         /* Add our new map */
366         list_add_tail(&newlist->link, &efi_mem);
367
368         /* And make sure memory is listed in descending order */
369         efi_mem_sort();
370
371         /* Notify that the memory map was changed */
372         list_for_each_entry(evt, &efi_events, link) {
373                 if (evt->group &&
374                     !guidcmp(evt->group,
375                              &efi_guid_event_group_memory_map_change)) {
376                         efi_signal_event(evt);
377                         break;
378                 }
379         }
380
381         return EFI_SUCCESS;
382 }
383
384 /**
385  * efi_add_memory_map() - add memory area to the memory map
386  *
387  * @start:              start address of the memory area
388  * @size:               length in bytes of the memory area
389  * @memory_type:        type of memory added
390  *
391  * Return:              status code
392  *
393  * This function automatically aligns the start and size of the memory area
394  * to EFI_PAGE_SIZE.
395  */
396 efi_status_t efi_add_memory_map(u64 start, u64 size, int memory_type)
397 {
398         u64 pages;
399
400         pages = efi_size_in_pages(size + (start & EFI_PAGE_MASK));
401         start &= ~EFI_PAGE_MASK;
402
403         return efi_add_memory_map_pg(start, pages, memory_type, false);
404 }
405
406 /**
407  * efi_check_allocated() - validate address to be freed
408  *
409  * Check that the address is within allocated memory:
410  *
411  * * The address must be in a range of the memory map.
412  * * The address may not point to EFI_CONVENTIONAL_MEMORY.
413  *
414  * Page alignment is not checked as this is not a requirement of
415  * efi_free_pool().
416  *
417  * @addr:               address of page to be freed
418  * @must_be_allocated:  return success if the page is allocated
419  * Return:              status code
420  */
421 static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated)
422 {
423         struct efi_mem_list *item;
424
425         list_for_each_entry(item, &efi_mem, link) {
426                 u64 start = item->desc.physical_start;
427                 u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT);
428
429                 if (addr >= start && addr < end) {
430                         if (must_be_allocated ^
431                             (item->desc.type == EFI_CONVENTIONAL_MEMORY))
432                                 return EFI_SUCCESS;
433                         else
434                                 return EFI_NOT_FOUND;
435                 }
436         }
437
438         return EFI_NOT_FOUND;
439 }
440
441 /**
442  * efi_allocate_pages - allocate memory pages
443  *
444  * @type:               type of allocation to be performed
445  * @memory_type:        usage type of the allocated memory
446  * @pages:              number of pages to be allocated
447  * @memory:             allocated memory
448  * Return:              status code
449  */
450 efi_status_t efi_allocate_pages(enum efi_allocate_type type,
451                                 enum efi_memory_type memory_type,
452                                 efi_uintn_t pages, uint64_t *memory)
453 {
454         u64 efi_addr, len;
455         uint flags;
456         efi_status_t ret;
457         phys_addr_t addr;
458
459         /* Check import parameters */
460         if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE &&
461             memory_type <= 0x6FFFFFFF)
462                 return EFI_INVALID_PARAMETER;
463         if (!memory)
464                 return EFI_INVALID_PARAMETER;
465         len = (u64)pages << EFI_PAGE_SHIFT;
466         /* Catch possible overflow on 64bit systems */
467         if (sizeof(efi_uintn_t) == sizeof(u64) &&
468             (len >> EFI_PAGE_SHIFT) != (u64)pages)
469                 return EFI_OUT_OF_RESOURCES;
470
471         flags = LMB_NOOVERWRITE | LMB_NONOTIFY;
472         switch (type) {
473         case EFI_ALLOCATE_ANY_PAGES:
474                 /* Any page */
475                 addr = (u64)lmb_alloc_base(len, EFI_PAGE_SIZE,
476                                                  LMB_ALLOC_ANYWHERE, flags);
477                 if (!addr)
478                         return EFI_OUT_OF_RESOURCES;
479                 break;
480         case EFI_ALLOCATE_MAX_ADDRESS:
481                 /* Max address */
482                 addr = map_to_sysmem((void *)(uintptr_t)*memory);
483                 addr = (u64)lmb_alloc_base(len, EFI_PAGE_SIZE, addr,
484                                                  flags);
485                 if (!addr)
486                         return EFI_OUT_OF_RESOURCES;
487                 break;
488         case EFI_ALLOCATE_ADDRESS:
489                 if (*memory & EFI_PAGE_MASK)
490                         return EFI_NOT_FOUND;
491
492                 addr = map_to_sysmem((void *)(uintptr_t)*memory);
493                 addr = (u64)lmb_alloc_addr(addr, len, flags);
494                 if (!addr)
495                         return EFI_NOT_FOUND;
496                 break;
497         default:
498                 /* UEFI doesn't specify other allocation types */
499                 return EFI_INVALID_PARAMETER;
500         }
501
502         efi_addr = (u64)(uintptr_t)map_sysmem(addr, 0);
503         /* Reserve that map in our memory maps */
504         ret = efi_add_memory_map_pg(efi_addr, pages, memory_type, true);
505         if (ret != EFI_SUCCESS) {
506                 /* Map would overlap, bail out */
507                 lmb_free_flags(addr, (u64)pages << EFI_PAGE_SHIFT, flags);
508                 unmap_sysmem((void *)(uintptr_t)efi_addr);
509                 return  EFI_OUT_OF_RESOURCES;
510         }
511
512         *memory = efi_addr;
513
514         return EFI_SUCCESS;
515 }
516
517 /**
518  * efi_free_pages() - free memory pages
519  *
520  * @memory:     start of the memory area to be freed
521  * @pages:      number of pages to be freed
522  * Return:      status code
523  */
524 efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
525 {
526         u64 len;
527         long status;
528         efi_status_t ret;
529
530         ret = efi_check_allocated(memory, true);
531         if (ret != EFI_SUCCESS)
532                 return ret;
533
534         /* Sanity check */
535         if (!memory || (memory & EFI_PAGE_MASK) || !pages) {
536                 printf("%s: illegal free 0x%llx, 0x%zx\n", __func__,
537                        memory, pages);
538                 return EFI_INVALID_PARAMETER;
539         }
540
541         len = (u64)pages << EFI_PAGE_SHIFT;
542         /*
543          * The 'memory' variable for sandbox holds a pointer which has already
544          * been mapped with map_sysmem() from efi_allocate_pages(). Convert
545          * it back to an address LMB understands
546          */
547         status = lmb_free_flags(map_to_sysmem((void *)(uintptr_t)memory), len,
548                                 LMB_NOOVERWRITE);
549         if (status)
550                 return EFI_NOT_FOUND;
551
552         unmap_sysmem((void *)(uintptr_t)memory);
553
554         return ret;
555 }
556
557 /**
558  * efi_alloc_aligned_pages() - allocate aligned memory pages
559  *
560  * @len:                len in bytes
561  * @memory_type:        usage type of the allocated memory
562  * @align:              alignment in bytes
563  * Return:              aligned memory or NULL
564  */
565 void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align)
566 {
567         u64 req_pages = efi_size_in_pages(len);
568         u64 true_pages = req_pages + efi_size_in_pages(align) - 1;
569         u64 free_pages;
570         u64 aligned_mem;
571         efi_status_t r;
572         u64 mem;
573
574         /* align must be zero or a power of two */
575         if (align & (align - 1))
576                 return NULL;
577
578         /* Check for overflow */
579         if (true_pages < req_pages)
580                 return NULL;
581
582         if (align < EFI_PAGE_SIZE) {
583                 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
584                                        req_pages, &mem);
585                 return (r == EFI_SUCCESS) ? (void *)(uintptr_t)mem : NULL;
586         }
587
588         r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
589                                true_pages, &mem);
590         if (r != EFI_SUCCESS)
591                 return NULL;
592
593         aligned_mem = ALIGN(mem, align);
594         /* Free pages before alignment */
595         free_pages = efi_size_in_pages(aligned_mem - mem);
596         if (free_pages)
597                 efi_free_pages(mem, free_pages);
598
599         /* Free trailing pages */
600         free_pages = true_pages - (req_pages + free_pages);
601         if (free_pages) {
602                 mem = aligned_mem + req_pages * EFI_PAGE_SIZE;
603                 efi_free_pages(mem, free_pages);
604         }
605
606         return (void *)(uintptr_t)aligned_mem;
607 }
608
609 /**
610  * efi_allocate_pool - allocate memory from pool
611  *
612  * @pool_type:  type of the pool from which memory is to be allocated
613  * @size:       number of bytes to be allocated
614  * @buffer:     allocated memory
615  * Return:      status code
616  */
617 efi_status_t efi_allocate_pool(enum efi_memory_type pool_type, efi_uintn_t size, void **buffer)
618 {
619         efi_status_t r;
620         u64 addr;
621         struct efi_pool_allocation *alloc;
622         u64 num_pages = efi_size_in_pages(size +
623                                           sizeof(struct efi_pool_allocation));
624
625         if (!buffer)
626                 return EFI_INVALID_PARAMETER;
627
628         if (size == 0) {
629                 *buffer = NULL;
630                 return EFI_SUCCESS;
631         }
632
633         r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
634                                &addr);
635         if (r == EFI_SUCCESS) {
636                 alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
637                 alloc->num_pages = num_pages;
638                 alloc->checksum = checksum(alloc);
639                 *buffer = alloc->data;
640         }
641
642         return r;
643 }
644
645 /**
646  * efi_alloc() - allocate boot services data pool memory
647  *
648  * Allocate memory from pool and zero it out.
649  *
650  * @size:       number of bytes to allocate
651  * Return:      pointer to allocated memory or NULL
652  */
653 void *efi_alloc(size_t size)
654 {
655         void *buf;
656
657         if (efi_allocate_pool(EFI_BOOT_SERVICES_DATA, size, &buf) !=
658             EFI_SUCCESS) {
659                 log_err("out of memory\n");
660                 return NULL;
661         }
662         memset(buf, 0, size);
663
664         return buf;
665 }
666
667 /**
668  * efi_free_pool() - free memory from pool
669  *
670  * @buffer:     start of memory to be freed
671  * Return:      status code
672  */
673 efi_status_t efi_free_pool(void *buffer)
674 {
675         efi_status_t ret;
676         struct efi_pool_allocation *alloc;
677
678         if (!buffer)
679                 return EFI_INVALID_PARAMETER;
680
681         ret = efi_check_allocated((uintptr_t)buffer, true);
682         if (ret != EFI_SUCCESS)
683                 return ret;
684
685         alloc = container_of(buffer, struct efi_pool_allocation, data);
686
687         /* Check that this memory was allocated by efi_allocate_pool() */
688         if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
689             alloc->checksum != checksum(alloc)) {
690                 printf("%s: illegal free 0x%p\n", __func__, buffer);
691                 return EFI_INVALID_PARAMETER;
692         }
693         /* Avoid double free */
694         alloc->checksum = 0;
695
696         ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
697
698         return ret;
699 }
700
701 /**
702  * efi_get_memory_map() - get map describing memory usage.
703  *
704  * @memory_map_size:    on entry the size, in bytes, of the memory map buffer,
705  *                      on exit the size of the copied memory map
706  * @memory_map:         buffer to which the memory map is written
707  * @map_key:            key for the memory map
708  * @descriptor_size:    size of an individual memory descriptor
709  * @descriptor_version: version number of the memory descriptor structure
710  * Return:              status code
711  */
712 efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
713                                 struct efi_mem_desc *memory_map,
714                                 efi_uintn_t *map_key,
715                                 efi_uintn_t *descriptor_size,
716                                 uint32_t *descriptor_version)
717 {
718         size_t map_entries;
719         efi_uintn_t map_size = 0;
720         struct efi_mem_list *lmem;
721         efi_uintn_t provided_map_size;
722
723         if (!memory_map_size)
724                 return EFI_INVALID_PARAMETER;
725
726         provided_map_size = *memory_map_size;
727
728         map_entries = list_count_nodes(&efi_mem);
729
730         map_size = map_entries * sizeof(struct efi_mem_desc);
731
732         *memory_map_size = map_size;
733
734         if (descriptor_size)
735                 *descriptor_size = sizeof(struct efi_mem_desc);
736
737         if (descriptor_version)
738                 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
739
740         if (provided_map_size < map_size)
741                 return EFI_BUFFER_TOO_SMALL;
742
743         if (!memory_map)
744                 return EFI_INVALID_PARAMETER;
745
746         /* Copy list into array */
747         /* Return the list in ascending order */
748         memory_map = &memory_map[map_entries - 1];
749         list_for_each_entry(lmem, &efi_mem, link) {
750                 *memory_map = lmem->desc;
751                 memory_map--;
752         }
753
754         if (map_key)
755                 *map_key = efi_memory_map_key;
756
757         return EFI_SUCCESS;
758 }
759
760 /**
761  * efi_get_memory_map_alloc() - allocate map describing memory usage
762  *
763  * The caller is responsible for calling FreePool() if the call succeeds.
764  *
765  * @map_size:           size of the memory map
766  * @memory_map:         buffer to which the memory map is written
767  * Return:              status code
768  */
769 efi_status_t efi_get_memory_map_alloc(efi_uintn_t *map_size,
770                                       struct efi_mem_desc **memory_map)
771 {
772         efi_status_t ret;
773
774         *memory_map = NULL;
775         *map_size = 0;
776         ret = efi_get_memory_map(map_size, *memory_map, NULL, NULL, NULL);
777         if (ret == EFI_BUFFER_TOO_SMALL) {
778                 *map_size += sizeof(struct efi_mem_desc); /* for the map */
779                 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, *map_size,
780                                         (void **)memory_map);
781                 if (ret != EFI_SUCCESS)
782                         return ret;
783                 ret = efi_get_memory_map(map_size, *memory_map,
784                                          NULL, NULL, NULL);
785                 if (ret != EFI_SUCCESS) {
786                         efi_free_pool(*memory_map);
787                         *memory_map = NULL;
788                 }
789         }
790
791         return ret;
792 }
793
794 /**
795  * efi_add_known_memory() - add memory types to the EFI memory map
796  *
797  * This function is to be used to add different memory types other
798  * than EFI_CONVENTIONAL_MEMORY to the EFI memory map. The conventional
799  * memory is handled by the LMB module and gets added to the memory
800  * map through the LMB module.
801  *
802  * This function may be overridden for architectures specific purposes.
803  */
804 __weak void efi_add_known_memory(void)
805 {
806 }
807
808 /**
809  * add_u_boot_and_runtime() - add U-Boot code to memory map
810  *
811  * Add memory regions for U-Boot's memory and for the runtime services code.
812  */
813 static void add_u_boot_and_runtime(void)
814 {
815         unsigned long runtime_start, runtime_end, runtime_pages;
816         unsigned long runtime_mask = EFI_PAGE_MASK;
817         unsigned long uboot_start, uboot_pages;
818         unsigned long uboot_stack_size = CONFIG_STACK_SIZE;
819
820         /* Add U-Boot */
821         uboot_start = ((uintptr_t)map_sysmem(gd->start_addr_sp, 0) -
822                        uboot_stack_size) & ~EFI_PAGE_MASK;
823         uboot_pages = ((uintptr_t)map_sysmem(gd->ram_top - 1, 0) -
824                        uboot_start + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
825         efi_add_memory_map_pg(uboot_start, uboot_pages, EFI_BOOT_SERVICES_CODE,
826                               false);
827 #if defined(__aarch64__)
828         /*
829          * Runtime Services must be 64KiB aligned according to the
830          * "AArch64 Platforms" section in the UEFI spec (2.7+).
831          */
832
833         runtime_mask = SZ_64K - 1;
834 #endif
835
836         /*
837          * Add Runtime Services. We mark surrounding boottime code as runtime as
838          * well to fulfill the runtime alignment constraints but avoid padding.
839          */
840         runtime_start = (uintptr_t)__efi_runtime_start & ~runtime_mask;
841         runtime_end = (uintptr_t)__efi_runtime_stop;
842         runtime_end = (runtime_end + runtime_mask) & ~runtime_mask;
843         runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
844         efi_add_memory_map_pg(runtime_start, runtime_pages,
845                               EFI_RUNTIME_SERVICES_CODE, false);
846 }
847
848 int efi_memory_init(void)
849 {
850         efi_add_known_memory();
851
852         add_u_boot_and_runtime();
853
854 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
855         /* Request a 32bit 64MB bounce buffer region */
856         uint64_t efi_bounce_buffer_addr = 0xffffffff;
857
858         if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_BOOT_SERVICES_DATA,
859                                (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
860                                &efi_bounce_buffer_addr) != EFI_SUCCESS)
861                 return -1;
862
863         efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
864 #endif
865
866         return 0;
867 }
This page took 0.076032 seconds and 4 git commands to generate.