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.2KB

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