iPXE
Functions
efi_umalloc.c File Reference

iPXE user memory allocation API for EFI More...

#include <string.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/uaccess.h>
#include <ipxe/umalloc.h>
#include <ipxe/efi/efi.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static void * efi_urealloc (void *old_ptr, size_t new_size)
 Reallocate external memory. More...
 
 PROVIDE_UMALLOC (efi, urealloc, efi_urealloc)
 

Detailed Description

iPXE user memory allocation API for EFI

Definition in file efi_umalloc.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ efi_urealloc()

static void* efi_urealloc ( void *  old_ptr,
size_t  new_size 
)
static

Reallocate external memory.

Parameters
old_ptrMemory previously allocated by umalloc(), or NULL
new_sizeRequested size
Return values
new_ptrAllocated memory, or NULL

Calling realloc() with a new size of zero is a valid way to free a memory block.

Definition at line 49 of file efi_umalloc.c.

49  {
51  EFI_PHYSICAL_ADDRESS phys_addr;
52  unsigned int new_pages, old_pages;
53  void *new_ptr = NOWHERE;
54  size_t old_size;
55  size_t *info;
56  EFI_STATUS efirc;
57  int rc;
58 
59  /* Allocate new memory if necessary. If allocation fails,
60  * return without touching the old block.
61  */
62  if ( new_size ) {
63  new_pages = ( EFI_SIZE_TO_PAGES ( new_size ) + 1 );
64  if ( ( efirc = bs->AllocatePages ( AllocateAnyPages,
66  new_pages,
67  &phys_addr ) ) != 0 ) {
68  rc = -EEFI ( efirc );
69  DBG ( "EFI could not allocate %d pages: %s\n",
70  new_pages, strerror ( rc ) );
71  return NULL;
72  }
73  assert ( phys_addr != 0 );
74  new_ptr = phys_to_virt ( phys_addr + EFI_PAGE_SIZE );
75  info = ( new_ptr - EFI_PAGE_SIZE );
76  *info = new_size;
77  DBG ( "EFI allocated %d pages at %llx\n",
78  new_pages, phys_addr );
79  }
80 
81  /* Copy across relevant part of the old data region (if any),
82  * then free it. Note that at this point either (a) new_ptr
83  * is valid, or (b) new_size is 0; either way, the memcpy() is
84  * valid.
85  */
86  if ( old_ptr && ( old_ptr != NOWHERE ) ) {
87  info = ( old_ptr - EFI_PAGE_SIZE );
88  old_size = *info;
89  memcpy ( new_ptr, old_ptr,
90  ( (old_size < new_size) ? old_size : new_size ) );
91  old_pages = ( EFI_SIZE_TO_PAGES ( old_size ) + 1 );
92  phys_addr = virt_to_phys ( old_ptr - EFI_PAGE_SIZE );
93  if ( ( efirc = bs->FreePages ( phys_addr, old_pages ) ) != 0 ){
94  rc = -EEFI ( efirc );
95  DBG ( "EFI could not free %d pages at %llx: %s\n",
96  old_pages, phys_addr, strerror ( rc ) );
97  /* Not fatal; we have leaked memory but successfully
98  * allocated (if asked to do so).
99  */
100  }
101  DBG ( "EFI freed %d pages at %llx\n", old_pages, phys_addr );
102  }
103 
104  return new_ptr;
105 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2098
#define EFI_PAGE_SIZE
Definition: UefiBaseType.h:187
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define NOWHERE
Address for zero-length memory blocks.
Definition: malloc.h:41
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:174
u32 info
Definition: ar9003_mac.h:67
UINT64 EFI_PHYSICAL_ADDRESS
64-bit physical memory address.
Definition: UefiBaseType.h:52
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI Boot Services Table.
Definition: UefiSpec.h:1930
#define EFI_SIZE_TO_PAGES(Size)
Macro that converts a size, in bytes, to a number of EFI_PAGESs.
Definition: UefiBaseType.h:202
EFI_ALLOCATE_PAGES AllocatePages
Definition: UefiSpec.h:1945
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
Allocate any available range of pages that satisfies the request.
Definition: UefiSpec.h:35
EFI_SYSTEM_TABLE * efi_systab
The data portions of a loaded Boot Serves Driver, and the default data allocation type used by a Boot...
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
EFI_FREE_PAGES FreePages
Definition: UefiSpec.h:1946
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References AllocateAnyPages, EFI_BOOT_SERVICES::AllocatePages, assert(), EFI_SYSTEM_TABLE::BootServices, DBG, EEFI, EFI_PAGE_SIZE, EFI_SIZE_TO_PAGES, efi_systab, EfiBootServicesData, EFI_BOOT_SERVICES::FreePages, info, memcpy(), NOWHERE, NULL, rc, and strerror().

◆ PROVIDE_UMALLOC()

PROVIDE_UMALLOC ( efi  ,
urealloc  ,
efi_urealloc   
)