]> git.ipfire.org Git - people/ms/u-boot.git/blobdiff - lib/efi_loader/efi_memory.c
imx: hab: Check if CSF contains deprecated commands
[people/ms/u-boot.git] / lib / efi_loader / efi_memory.c
index db2ae19f590d1beacfb104533e5807bf7c93b2a2..aaf64421a395d2d7ff6ea4060f52fc33090db15a 100644 (file)
@@ -43,7 +43,7 @@ void *efi_bounce_buffer;
  */
 struct efi_pool_allocation {
        u64 num_pages;
-       char data[];
+       char data[] __aligned(ARCH_DMA_MINALIGN);
 };
 
 /*
@@ -275,8 +275,17 @@ static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
        return 0;
 }
 
+/*
+ * Allocate memory pages.
+ *
+ * @type               type of allocation to be performed
+ * @memory_type                usage type of the allocated memory
+ * @pages              number of pages to be allocated
+ * @memory             allocated memory
+ * @return             status code
+ */
 efi_status_t efi_allocate_pages(int type, int memory_type,
-                               unsigned long pages, uint64_t *memory)
+                               efi_uintn_t pages, uint64_t *memory)
 {
        u64 len = pages << EFI_PAGE_SHIFT;
        efi_status_t r = EFI_SUCCESS;
@@ -338,7 +347,14 @@ void *efi_alloc(uint64_t len, int memory_type)
        return NULL;
 }
 
-efi_status_t efi_free_pages(uint64_t memory, unsigned long pages)
+/*
+ * Free memory pages.
+ *
+ * @memory     start of the memory area to be freed
+ * @pages      number of pages to be freed
+ * @return     status code
+ */
+efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
 {
        uint64_t r = 0;
 
@@ -351,12 +367,20 @@ efi_status_t efi_free_pages(uint64_t memory, unsigned long pages)
        return EFI_NOT_FOUND;
 }
 
-efi_status_t efi_allocate_pool(int pool_type, unsigned long size,
-                              void **buffer)
+/*
+ * Allocate memory from pool.
+ *
+ * @pool_type  type of the pool from which memory is to be allocated
+ * @size       number of bytes to be allocated
+ * @buffer     allocated memory
+ * @return     status code
+ */
+efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
 {
        efi_status_t r;
        efi_physical_addr_t t;
-       u64 num_pages = (size + sizeof(u64) + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
+       u64 num_pages = (size + sizeof(struct efi_pool_allocation) +
+                        EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
 
        if (size == 0) {
                *buffer = NULL;
@@ -374,11 +398,20 @@ efi_status_t efi_allocate_pool(int pool_type, unsigned long size,
        return r;
 }
 
+/*
+ * Free memory from pool.
+ *
+ * @buffer     start of memory to be freed
+ * @return     status code
+ */
 efi_status_t efi_free_pool(void *buffer)
 {
        efi_status_t r;
        struct efi_pool_allocation *alloc;
 
+       if (buffer == NULL)
+               return EFI_INVALID_PARAMETER;
+
        alloc = container_of(buffer, struct efi_pool_allocation, data);
        /* Sanity check, was the supplied address returned by allocate_pool */
        assert(((uintptr_t)alloc & EFI_PAGE_MASK) == 0);
@@ -388,16 +421,27 @@ efi_status_t efi_free_pool(void *buffer)
        return r;
 }
 
-efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
-                              struct efi_mem_desc *memory_map,
-                              unsigned long *map_key,
-                              unsigned long *descriptor_size,
-                              uint32_t *descriptor_version)
+/*
+ * Get map describing memory usage.
+ *
+ * @memory_map_size    on entry the size, in bytes, of the memory map buffer,
+ *                     on exit the size of the copied memory map
+ * @memory_map         buffer to which the memory map is written
+ * @map_key            key for the memory map
+ * @descriptor_size    size of an individual memory descriptor
+ * @descriptor_version version number of the memory descriptor structure
+ * @return             status code
+ */
+efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
+                               struct efi_mem_desc *memory_map,
+                               efi_uintn_t *map_key,
+                               efi_uintn_t *descriptor_size,
+                               uint32_t *descriptor_version)
 {
-       ulong map_size = 0;
+       efi_uintn_t map_size = 0;
        int map_entries = 0;
        struct list_head *lhandle;
-       unsigned long provided_map_size = *memory_map_size;
+       efi_uintn_t provided_map_size = *memory_map_size;
 
        list_for_each(lhandle, &efi_mem)
                map_entries++;
@@ -406,15 +450,15 @@ efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
 
        *memory_map_size = map_size;
 
+       if (provided_map_size < map_size)
+               return EFI_BUFFER_TOO_SMALL;
+
        if (descriptor_size)
                *descriptor_size = sizeof(struct efi_mem_desc);
 
        if (descriptor_version)
                *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
 
-       if (provided_map_size < map_size)
-               return EFI_BUFFER_TOO_SMALL;
-
        /* Copy list into array */
        if (memory_map) {
                /* Return the list in ascending order */
@@ -428,6 +472,8 @@ efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
                }
        }
 
+       *map_key = 0;
+
        return EFI_SUCCESS;
 }