uppermem.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Compute amount of lower and upper memory till the first hole. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009 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/memory.h>
  20. #include <grub/i386/memory.h>
  21. #include <grub/mm.h>
  22. #include <grub/misc.h>
  23. /* Helper for grub_mmap_get_lower. */
  24. static int
  25. lower_hook (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
  26. void *data)
  27. {
  28. grub_uint64_t *lower = data;
  29. if (type != GRUB_MEMORY_AVAILABLE)
  30. return 0;
  31. #ifdef GRUB_MACHINE_COREBOOT
  32. if (addr <= 0x1000)
  33. #else
  34. if (addr == 0)
  35. #endif
  36. *lower = size + addr;
  37. return 0;
  38. }
  39. grub_uint64_t
  40. grub_mmap_get_lower (void)
  41. {
  42. grub_uint64_t lower = 0;
  43. grub_mmap_iterate (lower_hook, &lower);
  44. if (lower > 0x100000)
  45. lower = 0x100000;
  46. return lower;
  47. }
  48. /* Helper for grub_mmap_get_upper. */
  49. static int
  50. upper_hook (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
  51. void *data)
  52. {
  53. grub_uint64_t *upper = data;
  54. if (type != GRUB_MEMORY_AVAILABLE)
  55. return 0;
  56. if (addr <= 0x100000 && addr + size > 0x100000)
  57. *upper = addr + size - 0x100000;
  58. return 0;
  59. }
  60. grub_uint64_t
  61. grub_mmap_get_upper (void)
  62. {
  63. grub_uint64_t upper = 0;
  64. grub_mmap_iterate (upper_hook, &upper);
  65. return upper;
  66. }
  67. /* Helper for grub_mmap_get_post64. */
  68. static int
  69. post64_hook (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
  70. void *data)
  71. {
  72. grub_uint64_t *post64 = data;
  73. if (type != GRUB_MEMORY_AVAILABLE)
  74. return 0;
  75. if (addr <= 0x4000000 && addr + size > 0x4000000)
  76. *post64 = addr + size - 0x4000000;
  77. return 0;
  78. }
  79. /* Count the continuous bytes after 64 MiB. */
  80. grub_uint64_t
  81. grub_mmap_get_post64 (void)
  82. {
  83. grub_uint64_t post64 = 0;
  84. grub_mmap_iterate (post64_hook, &post64);
  85. return post64;
  86. }