mmap.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2007,2008,2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/machine/memory.h>
  19. #include <grub/machine/lbio.h>
  20. #include <grub/types.h>
  21. #include <grub/err.h>
  22. #include <grub/misc.h>
  23. /* Context for grub_machine_mmap_iterate. */
  24. struct grub_machine_mmap_iterate_ctx
  25. {
  26. grub_memory_hook_t hook;
  27. void *hook_data;
  28. };
  29. #define GRUB_MACHINE_MEMORY_BADRAM 5
  30. /* Helper for grub_machine_mmap_iterate. */
  31. static int
  32. iterate_linuxbios_table (grub_linuxbios_table_item_t table_item, void *data)
  33. {
  34. struct grub_machine_mmap_iterate_ctx *ctx = data;
  35. mem_region_t mem_region;
  36. if (table_item->tag != GRUB_LINUXBIOS_MEMBER_MEMORY)
  37. return 0;
  38. mem_region =
  39. (mem_region_t) ((long) table_item +
  40. sizeof (struct grub_linuxbios_table_item));
  41. for (; (long) mem_region < (long) table_item + (long) table_item->size;
  42. mem_region++)
  43. {
  44. grub_uint64_t start = mem_region->addr;
  45. grub_uint64_t end = mem_region->addr + mem_region->size;
  46. /* Mark region 0xa0000 - 0x100000 as reserved. */
  47. if (start < 0x100000 && end >= 0xa0000
  48. && mem_region->type == GRUB_MACHINE_MEMORY_AVAILABLE)
  49. {
  50. if (start < 0xa0000
  51. && ctx->hook (start, 0xa0000 - start,
  52. /* Multiboot mmaps match with the coreboot mmap
  53. definition. Therefore, we can just pass type
  54. through. */
  55. mem_region->type,
  56. ctx->hook_data))
  57. return 1;
  58. if (start < 0xa0000)
  59. start = 0xa0000;
  60. if (start >= end)
  61. continue;
  62. if (ctx->hook (start, (end > 0x100000 ? 0x100000 : end) - start,
  63. GRUB_MEMORY_RESERVED,
  64. ctx->hook_data))
  65. return 1;
  66. start = 0x100000;
  67. if (end <= start)
  68. continue;
  69. }
  70. if (ctx->hook (start, end - start,
  71. /* Multiboot mmaps match with the coreboot mmap
  72. definition. Therefore, we can just pass type
  73. through. */
  74. mem_region->type,
  75. ctx->hook_data))
  76. return 1;
  77. }
  78. return 0;
  79. }
  80. grub_err_t
  81. grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
  82. {
  83. struct grub_machine_mmap_iterate_ctx ctx = { hook, hook_data };
  84. grub_linuxbios_table_iterate (iterate_linuxbios_table, &ctx);
  85. return 0;
  86. }