[PATCH v3 3/4] lib: efi_loader: efi_memory.c: add efi_realloc() for realloc memory

Ilias Apalodimas ilias.apalodimas at linaro.org
Mon May 26 16:11:28 CEST 2025


Hi Paul,

[...]

> + * efi_realloc() - reallocate boot services data pool memory
> + *
> + * Reallocate memory from pool for a new size and copy the data from old one.
> + *
> + * @ptr:       pointer to old buffer
> + * @size:      number of bytes to allocate
> + * Return:     pointer to allocated memory or NULL
> + */
> +void *efi_realloc(void *ptr, size_t size)
> +{
> +       efi_status_t ret;
> +       void *new_ptr;
> +       struct efi_pool_allocation *alloc;
> +       u64 num_pages = efi_size_in_pages(size +
> +                                         sizeof(struct efi_pool_allocation));
> +       size_t old_size;
> +
> +       if (!ptr)
> +               return efi_alloc(size);
> +
> +       ret = efi_check_allocated((uintptr_t)ptr, true);
> +       if (ret != EFI_SUCCESS)
> +               return ptr;

This is a bit weird here. If we return a pointer how can we tell
success vs failure?
I think we should return EFI/SUCCESS fail and just pass a **ptr as an argument?

Thanks
/Ilias
> +
> +       alloc = container_of(ptr, struct efi_pool_allocation, data);
> +
> +       /* Check that this memory was allocated by efi_allocate_pool() */
> +       if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
> +           alloc->checksum != checksum(alloc)) {
> +               printf("%s: illegal realloc 0x%p\n", __func__, ptr);
> +               return ptr;
> +       }
> +
> +       /* Don't realloc. The actual size in pages is the same. */
> +       if (alloc->num_pages == num_pages)
> +               return ptr;
> +
> +       old_size = alloc->num_pages * EFI_PAGE_SIZE -
> +               sizeof(struct efi_pool_allocation);
> +
> +       new_ptr = efi_alloc(size);
> +
> +       /* copy old data to new alloced buffer */
> +       memcpy(new_ptr, ptr, min(size, old_size));
> +
> +       /* free the old buffer */
> +       efi_free_pool(ptr);
> +
> +       return new_ptr;
> +}
> +
>  /**
>   * efi_free_pool() - free memory from pool
>   *
> --
> 2.39.5
>


More information about the U-Boot mailing list