misc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* misc.c - various system functions for an arm-based EFI system */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2013 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/misc.h>
  20. #include <grub/mm.h>
  21. #include <grub/cpu/linux.h>
  22. #include <grub/cpu/system.h>
  23. #include <grub/efi/efi.h>
  24. #include <grub/machine/loader.h>
  25. static inline grub_size_t
  26. page_align (grub_size_t size)
  27. {
  28. return (size + (1 << 12) - 1) & (~((1 << 12) - 1));
  29. }
  30. /* Find the optimal number of pages for the memory map. Is it better to
  31. move this code to efi/mm.c? */
  32. static grub_efi_uintn_t
  33. find_mmap_size (void)
  34. {
  35. static grub_efi_uintn_t mmap_size = 0;
  36. if (mmap_size != 0)
  37. return mmap_size;
  38. mmap_size = (1 << 12);
  39. while (1)
  40. {
  41. int ret;
  42. grub_efi_memory_descriptor_t *mmap;
  43. grub_efi_uintn_t desc_size;
  44. mmap = grub_malloc (mmap_size);
  45. if (! mmap)
  46. return 0;
  47. ret = grub_efi_get_memory_map (&mmap_size, mmap, 0, &desc_size, 0);
  48. grub_free (mmap);
  49. if (ret < 0)
  50. {
  51. grub_error (GRUB_ERR_IO, "cannot get memory map");
  52. return 0;
  53. }
  54. else if (ret > 0)
  55. break;
  56. mmap_size += (1 << 12);
  57. }
  58. /* Increase the size a bit for safety, because GRUB allocates more on
  59. later, and EFI itself may allocate more. */
  60. mmap_size += (1 << 12);
  61. return page_align (mmap_size);
  62. }
  63. #define NEXT_MEMORY_DESCRIPTOR(desc, size) \
  64. ((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size)))
  65. #define PAGE_SHIFT 12
  66. void *
  67. grub_efi_allocate_loader_memory (grub_uint32_t min_offset, grub_uint32_t size)
  68. {
  69. grub_efi_uintn_t desc_size;
  70. grub_efi_memory_descriptor_t *mmap, *mmap_end;
  71. grub_efi_uintn_t mmap_size, tmp_mmap_size;
  72. grub_efi_memory_descriptor_t *desc;
  73. void *mem = NULL;
  74. grub_addr_t min_start = 0;
  75. mmap_size = find_mmap_size();
  76. if (!mmap_size)
  77. return NULL;
  78. mmap = grub_malloc(mmap_size);
  79. if (!mmap)
  80. return NULL;
  81. tmp_mmap_size = mmap_size;
  82. if (grub_efi_get_memory_map (&tmp_mmap_size, mmap, 0, &desc_size, 0) <= 0)
  83. {
  84. grub_error (GRUB_ERR_IO, "cannot get memory map");
  85. goto fail;
  86. }
  87. mmap_end = NEXT_MEMORY_DESCRIPTOR (mmap, tmp_mmap_size);
  88. /* Find lowest accessible RAM location */
  89. {
  90. int found = 0;
  91. for (desc = mmap ; !found && (desc < mmap_end) ;
  92. desc = NEXT_MEMORY_DESCRIPTOR(desc, desc_size))
  93. {
  94. switch (desc->type)
  95. {
  96. case GRUB_EFI_CONVENTIONAL_MEMORY:
  97. case GRUB_EFI_LOADER_CODE:
  98. case GRUB_EFI_LOADER_DATA:
  99. min_start = desc->physical_start + min_offset;
  100. found = 1;
  101. break;
  102. default:
  103. break;
  104. }
  105. }
  106. }
  107. /* First, find free pages for the real mode code
  108. and the memory map buffer. */
  109. for (desc = mmap ; desc < mmap_end ;
  110. desc = NEXT_MEMORY_DESCRIPTOR(desc, desc_size))
  111. {
  112. grub_uint64_t start, end;
  113. grub_dprintf("mm", "%s: 0x%08x bytes @ 0x%08x\n",
  114. __FUNCTION__,
  115. (grub_uint32_t) (desc->num_pages << PAGE_SHIFT),
  116. (grub_uint32_t) (desc->physical_start));
  117. if (desc->type != GRUB_EFI_CONVENTIONAL_MEMORY)
  118. continue;
  119. start = desc->physical_start;
  120. end = start + (desc->num_pages << PAGE_SHIFT);
  121. grub_dprintf("mm", "%s: start=0x%016llx, end=0x%016llx\n",
  122. __FUNCTION__, start, end);
  123. start = start < min_start ? min_start : start;
  124. if (start + size > end)
  125. continue;
  126. grub_dprintf("mm", "%s: let's allocate some (0x%x) pages @ 0x%08x...\n",
  127. __FUNCTION__, (size >> PAGE_SHIFT), (grub_addr_t) start);
  128. mem = grub_efi_allocate_pages (start, (size >> PAGE_SHIFT) + 1);
  129. grub_dprintf("mm", "%s: retval=0x%08x\n",
  130. __FUNCTION__, (grub_addr_t) mem);
  131. if (! mem)
  132. {
  133. grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate memory");
  134. goto fail;
  135. }
  136. break;
  137. }
  138. if (! mem)
  139. {
  140. grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate memory");
  141. goto fail;
  142. }
  143. grub_free (mmap);
  144. return mem;
  145. fail:
  146. grub_free (mmap);
  147. return NULL;
  148. }
  149. grub_err_t
  150. grub_efi_prepare_platform (void)
  151. {
  152. grub_efi_uintn_t mmap_size;
  153. grub_efi_uintn_t map_key;
  154. grub_efi_uintn_t desc_size;
  155. grub_efi_uint32_t desc_version;
  156. grub_efi_memory_descriptor_t *mmap_buf;
  157. grub_err_t err;
  158. /*
  159. * Cloned from IA64
  160. * Must be done after grub_machine_fini because map_key is used by
  161. *exit_boot_services.
  162. */
  163. mmap_size = find_mmap_size ();
  164. if (! mmap_size)
  165. return GRUB_ERR_OUT_OF_MEMORY;
  166. mmap_buf = grub_efi_allocate_pages (0, page_align (mmap_size) >> 12);
  167. if (! mmap_buf)
  168. return GRUB_ERR_OUT_OF_MEMORY;
  169. err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, &map_key,
  170. &desc_size, &desc_version);
  171. if (err != GRUB_ERR_NONE)
  172. return err;
  173. return GRUB_ERR_NONE;
  174. }