mmap.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2002,2003,2004,2005,2006,2007,2008 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/int.h>
  20. #include <grub/err.h>
  21. #include <grub/types.h>
  22. #include <grub/misc.h>
  23. struct grub_machine_mmap_entry
  24. {
  25. grub_uint32_t size;
  26. grub_uint64_t addr;
  27. grub_uint64_t len;
  28. #define GRUB_MACHINE_MEMORY_AVAILABLE 1
  29. #define GRUB_MACHINE_MEMORY_RESERVED 2
  30. #define GRUB_MACHINE_MEMORY_ACPI 3
  31. #define GRUB_MACHINE_MEMORY_NVS 4
  32. #define GRUB_MACHINE_MEMORY_BADRAM 5
  33. grub_uint32_t type;
  34. } GRUB_PACKED;
  35. /*
  36. *
  37. * grub_get_conv_memsize(i) : return the conventional memory size in KB.
  38. * BIOS call "INT 12H" to get conventional memory size
  39. * The return value in AX.
  40. */
  41. static inline grub_uint16_t
  42. grub_get_conv_memsize (void)
  43. {
  44. struct grub_bios_int_registers regs;
  45. regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
  46. grub_bios_interrupt (0x12, &regs);
  47. return regs.eax & 0xffff;
  48. }
  49. /*
  50. * grub_get_ext_memsize() : return the extended memory size in KB.
  51. * BIOS call "INT 15H, AH=88H" to get extended memory size
  52. * The return value in AX.
  53. *
  54. */
  55. static inline grub_uint16_t
  56. grub_get_ext_memsize (void)
  57. {
  58. struct grub_bios_int_registers regs;
  59. regs.eax = 0x8800;
  60. regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
  61. grub_bios_interrupt (0x15, &regs);
  62. return regs.eax & 0xffff;
  63. }
  64. /* Get a packed EISA memory map. Lower 16 bits are between 1MB and 16MB
  65. in 1KB parts, and upper 16 bits are above 16MB in 64KB parts. If error, return zero.
  66. BIOS call "INT 15H, AH=E801H" to get EISA memory map,
  67. AX = memory between 1M and 16M in 1K parts.
  68. BX = memory above 16M in 64K parts.
  69. */
  70. static inline grub_uint32_t
  71. grub_get_eisa_mmap (void)
  72. {
  73. struct grub_bios_int_registers regs;
  74. regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
  75. regs.eax = 0xe801;
  76. grub_bios_interrupt (0x15, &regs);
  77. if ((regs.eax & 0xff00) == 0x8600)
  78. return 0;
  79. return (regs.eax & 0xffff) | (regs.ebx << 16);
  80. }
  81. /*
  82. *
  83. * grub_get_mmap_entry(addr, cont) : address and old continuation value (zero to
  84. * start), for the Query System Address Map BIOS call.
  85. *
  86. * Sets the first 4-byte int value of "addr" to the size returned by
  87. * the call. If the call fails, sets it to zero.
  88. *
  89. * Returns: new (non-zero) continuation value, 0 if done.
  90. */
  91. /* Get a memory map entry. Return next continuation value. Zero means
  92. the end. */
  93. static grub_uint32_t
  94. grub_get_mmap_entry (struct grub_machine_mmap_entry *entry,
  95. grub_uint32_t cont)
  96. {
  97. struct grub_bios_int_registers regs;
  98. regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
  99. /* place address (+4) in ES:DI */
  100. regs.es = ((grub_addr_t) &entry->addr) >> 4;
  101. regs.edi = ((grub_addr_t) &entry->addr) & 0xf;
  102. /* set continuation value */
  103. regs.ebx = cont;
  104. /* set default maximum buffer size */
  105. regs.ecx = sizeof (*entry) - sizeof (entry->size);
  106. /* set EDX to 'SMAP' */
  107. regs.edx = 0x534d4150;
  108. regs.eax = 0xe820;
  109. grub_bios_interrupt (0x15, &regs);
  110. /* write length of buffer (zero if error) into ADDR */
  111. if ((regs.flags & GRUB_CPU_INT_FLAGS_CARRY) || regs.eax != 0x534d4150
  112. || regs.ecx < 0x14 || regs.ecx > 0x400)
  113. entry->size = 0;
  114. else
  115. entry->size = regs.ecx;
  116. /* return the continuation value */
  117. return regs.ebx;
  118. }
  119. grub_err_t
  120. grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
  121. {
  122. grub_uint32_t cont = 0;
  123. struct grub_machine_mmap_entry *entry
  124. = (struct grub_machine_mmap_entry *) grub_absolute_pointer (GRUB_MEMORY_MACHINE_SCRATCH_ADDR);
  125. int e820_works = 0;
  126. while (1)
  127. {
  128. grub_memset (entry, 0, sizeof (*entry));
  129. cont = grub_get_mmap_entry (entry, cont);
  130. if (!entry->size)
  131. break;
  132. if (entry->len)
  133. e820_works = 1;
  134. if (entry->len
  135. && hook (entry->addr, entry->len,
  136. /* GRUB mmaps have been defined to match with
  137. the E820 definition.
  138. Therefore, we can just pass type through. */
  139. entry->type, hook_data))
  140. break;
  141. if (! cont)
  142. break;
  143. }
  144. if (!e820_works)
  145. {
  146. grub_uint32_t eisa_mmap = grub_get_eisa_mmap ();
  147. if (hook (0x0, ((grub_uint32_t) grub_get_conv_memsize ()) << 10,
  148. GRUB_MEMORY_AVAILABLE, hook_data))
  149. return 0;
  150. if (eisa_mmap)
  151. {
  152. if (hook (0x100000, (eisa_mmap & 0xFFFF) << 10,
  153. GRUB_MEMORY_AVAILABLE, hook_data) == 0)
  154. hook (0x1000000, eisa_mmap & ~0xFFFF, GRUB_MEMORY_AVAILABLE,
  155. hook_data);
  156. }
  157. else
  158. hook (0x100000, ((grub_uint32_t) grub_get_ext_memsize ()) << 10,
  159. GRUB_MEMORY_AVAILABLE, hook_data);
  160. }
  161. return 0;
  162. }