You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

efi_umalloc.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <assert.h>
  19. #include <gpxe/umalloc.h>
  20. #include <gpxe/efi/efi.h>
  21. /** @file
  22. *
  23. * gPXE user memory allocation API for EFI
  24. *
  25. */
  26. /** Equivalent of NOWHERE for user pointers */
  27. #define UNOWHERE ( ~UNULL )
  28. /**
  29. * Reallocate external memory
  30. *
  31. * @v old_ptr Memory previously allocated by umalloc(), or UNULL
  32. * @v new_size Requested size
  33. * @ret new_ptr Allocated memory, or UNULL
  34. *
  35. * Calling realloc() with a new size of zero is a valid way to free a
  36. * memory block.
  37. */
  38. static userptr_t efi_urealloc ( userptr_t old_ptr, size_t new_size ) {
  39. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  40. EFI_PHYSICAL_ADDRESS phys_addr;
  41. unsigned int new_pages, old_pages;
  42. userptr_t new_ptr = UNOWHERE;
  43. size_t old_size;
  44. EFI_STATUS efirc;
  45. /* Allocate new memory if necessary. If allocation fails,
  46. * return without touching the old block.
  47. */
  48. if ( new_size ) {
  49. new_pages = ( EFI_SIZE_TO_PAGES ( new_size ) + 1 );
  50. if ( ( efirc = bs->AllocatePages ( AllocateAnyPages,
  51. EfiBootServicesData,
  52. new_pages,
  53. &phys_addr ) ) != 0 ) {
  54. DBG ( "EFI could not allocate %d pages: %s\n",
  55. new_pages, efi_strerror ( efirc ) );
  56. return UNULL;
  57. }
  58. assert ( phys_addr != 0 );
  59. new_ptr = phys_to_user ( phys_addr + EFI_PAGE_SIZE );
  60. copy_to_user ( new_ptr, -EFI_PAGE_SIZE,
  61. &new_size, sizeof ( new_size ) );
  62. DBG ( "EFI allocated %d pages at %llx\n",
  63. new_pages, phys_addr );
  64. }
  65. /* Copy across relevant part of the old data region (if any),
  66. * then free it. Note that at this point either (a) new_ptr
  67. * is valid, or (b) new_size is 0; either way, the memcpy() is
  68. * valid.
  69. */
  70. if ( old_ptr && ( old_ptr != UNOWHERE ) ) {
  71. copy_from_user ( &old_size, old_ptr, -EFI_PAGE_SIZE,
  72. sizeof ( old_size ) );
  73. memcpy_user ( new_ptr, 0, old_ptr, 0,
  74. ( (old_size < new_size) ? old_size : new_size ));
  75. old_pages = ( EFI_SIZE_TO_PAGES ( old_size ) + 1 );
  76. phys_addr = user_to_phys ( old_ptr, -EFI_PAGE_SIZE );
  77. if ( ( efirc = bs->FreePages ( phys_addr, old_pages ) ) != 0 ){
  78. DBG ( "EFI could not free %d pages at %llx: %s\n",
  79. old_pages, phys_addr, efi_strerror ( efirc ) );
  80. /* Not fatal; we have leaked memory but successfully
  81. * allocated (if asked to do so).
  82. */
  83. }
  84. DBG ( "EFI freed %d pages at %llx\n", old_pages, phys_addr );
  85. }
  86. return new_ptr;
  87. }
  88. PROVIDE_UMALLOC ( efi, urealloc, efi_urealloc );