iPXE
efi_umalloc.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <string.h>
28#include <errno.h>
29#include <assert.h>
30#include <ipxe/uaccess.h>
31#include <ipxe/umalloc.h>
32#include <ipxe/efi/efi.h>
33
34/** @file
35 *
36 * iPXE user memory allocation API for EFI
37 *
38 */
39
40/**
41 * Reallocate external memory
42 *
43 * @v old_ptr Memory previously allocated by umalloc(), or NULL
44 * @v new_size Requested size
45 * @ret new_ptr Allocated memory, or NULL
46 *
47 * Calling realloc() with a new size of zero is a valid way to free a
48 * memory block.
49 */
50static void * efi_urealloc ( void *old_ptr, size_t new_size ) {
51 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
52 EFI_PHYSICAL_ADDRESS phys_addr;
53 unsigned int new_pages, old_pages;
54 void *new_ptr = NOWHERE;
55 size_t old_size;
56 size_t *info;
57 EFI_STATUS efirc;
58 int rc;
59
60 /* Allocate new memory if necessary. If allocation fails,
61 * return without touching the old block.
62 */
63 if ( new_size ) {
64 new_pages = ( EFI_SIZE_TO_PAGES ( new_size ) + 1 );
65 if ( ( efirc = bs->AllocatePages ( AllocateAnyPages,
67 new_pages,
68 &phys_addr ) ) != 0 ) {
69 rc = -EEFI ( efirc );
70 DBG ( "EFI could not allocate %d pages: %s\n",
71 new_pages, strerror ( rc ) );
72 return NULL;
73 }
74 assert ( phys_addr != 0 );
75 new_ptr = phys_to_virt ( phys_addr + EFI_PAGE_SIZE );
76 info = ( new_ptr - EFI_PAGE_SIZE );
77 *info = new_size;
78 DBG ( "EFI allocated %d pages at %llx\n",
79 new_pages, phys_addr );
80 }
81
82 /* Copy across relevant part of the old data region (if any),
83 * then free it. Note that at this point either (a) new_ptr
84 * is valid, or (b) new_size is 0; either way, the memcpy() is
85 * valid.
86 */
87 if ( old_ptr && ( old_ptr != NOWHERE ) ) {
88 info = ( old_ptr - EFI_PAGE_SIZE );
89 old_size = *info;
90 memcpy ( new_ptr, old_ptr,
91 ( (old_size < new_size) ? old_size : new_size ) );
92 old_pages = ( EFI_SIZE_TO_PAGES ( old_size ) + 1 );
93 phys_addr = virt_to_phys ( old_ptr - EFI_PAGE_SIZE );
94 if ( ( efirc = bs->FreePages ( phys_addr, old_pages ) ) != 0 ){
95 rc = -EEFI ( efirc );
96 DBG ( "EFI could not free %d pages at %llx: %s\n",
97 old_pages, phys_addr, strerror ( rc ) );
98 /* Not fatal; we have leaked memory but successfully
99 * allocated (if asked to do so).
100 */
101 }
102 DBG ( "EFI freed %d pages at %llx\n", old_pages, phys_addr );
103 }
104
105 return new_ptr;
106}
107
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
#define EFI_PAGE_SIZE
UINT64 EFI_PHYSICAL_ADDRESS
64-bit physical memory address.
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
#define EFI_SIZE_TO_PAGES(Size)
Macro that converts a size, in bytes, to a number of EFI_PAGESs.
@ EfiBootServicesData
The data portions of a loaded Boot Serves Driver, and the default data allocation type used by a Boot...
@ AllocateAnyPages
Allocate any available range of pages that satisfies the request.
Definition UefiSpec.h:36
u32 info
Definition ar9003_mac.h:0
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
Assertions.
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
static void * efi_urealloc(void *old_ptr, size_t new_size)
Reallocate external memory.
Definition efi_umalloc.c:50
Error codes.
#define DBG(...)
Print a debugging message.
Definition compiler.h:498
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
EFI API.
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition efi.h:175
EFI_SYSTEM_TABLE * efi_systab
User memory allocation.
void * urealloc(void *ptr, size_t new_size)
Reallocate external memory.
#define PROVIDE_UMALLOC(_subsys, _api_func, _func)
Provide a user memory allocation API implementation.
Definition umalloc.h:26
String functions.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
Access to external ("user") memory.
#define NOWHERE
Address for zero-length memory blocks.
Definition malloc.h:42
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
EFI Boot Services Table.
Definition UefiSpec.h:1931
EFI_ALLOCATE_PAGES AllocatePages
Definition UefiSpec.h:1946
EFI_FREE_PAGES FreePages
Definition UefiSpec.h:1947