uppermem.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/mm.h>
  21. #include <grub/misc.h>
  22. GRUB_EXPORT(grub_mmap_get_lower);
  23. GRUB_EXPORT(grub_mmap_get_upper);
  24. static int
  25. grub_mmap_get_lower_hook (grub_uint64_t addr, grub_uint64_t size,
  26. grub_uint32_t type, void *closure)
  27. {
  28. grub_uint64_t *lower = closure;
  29. if (type != GRUB_MACHINE_MEMORY_AVAILABLE)
  30. return 0;
  31. if (addr == 0)
  32. *lower = size;
  33. return 0;
  34. }
  35. grub_uint64_t
  36. grub_mmap_get_lower (void)
  37. {
  38. grub_uint64_t lower = 0;
  39. grub_mmap_iterate (grub_mmap_get_lower_hook, &lower);
  40. if (lower > 0x100000)
  41. lower = 0x100000;
  42. return lower;
  43. }
  44. static int
  45. grub_mmap_get_upper_hook (grub_uint64_t addr, grub_uint64_t size,
  46. grub_uint32_t type, void *closure)
  47. {
  48. grub_uint64_t *upper = closure;
  49. if (type != GRUB_MACHINE_MEMORY_AVAILABLE)
  50. return 0;
  51. if (addr <= 0x100000 && addr + size > 0x100000)
  52. *upper = addr + size - 0x100000;
  53. return 0;
  54. }
  55. grub_uint64_t
  56. grub_mmap_get_upper (void)
  57. {
  58. grub_uint64_t upper = 0;
  59. grub_mmap_iterate (grub_mmap_get_upper_hook, &upper);
  60. return upper;
  61. }
  62. static int
  63. grub_mmap_get_post64_hook (grub_uint64_t addr, grub_uint64_t size,
  64. grub_uint32_t type, void *closure)
  65. {
  66. grub_uint64_t *post64 = closure;
  67. if (type != GRUB_MACHINE_MEMORY_AVAILABLE)
  68. return 0;
  69. if (addr <= 0x4000000 && addr + size > 0x4000000)
  70. *post64 = addr + size - 0x4000000;
  71. return 0;
  72. }
  73. /* Count the continuous bytes after 64 MiB. */
  74. grub_uint64_t
  75. grub_mmap_get_post64 (void)
  76. {
  77. grub_uint64_t post64 = 0;
  78. grub_mmap_iterate (grub_mmap_get_post64_hook, &post64);
  79. return post64;
  80. }