mmap.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2009 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/i386/memory.h>
  19. #include <grub/machine/memory.h>
  20. #include <grub/machine/boot.h>
  21. #include <grub/types.h>
  22. #include <grub/err.h>
  23. #include <grub/misc.h>
  24. #include <grub/cmos.h>
  25. #include <grub/offsets.h>
  26. #define QEMU_CMOS_MEMSIZE_HIGH 0x35
  27. #define QEMU_CMOS_MEMSIZE_LOW 0x34
  28. #define QEMU_CMOS_MEMSIZE2_HIGH 0x31
  29. #define QEMU_CMOS_MEMSIZE2_LOW 0x30
  30. #define min(a,b) ((a) > (b) ? (b) : (a))
  31. extern char _start[];
  32. extern char _end[];
  33. static grub_uint64_t mem_size, above_4g;
  34. void
  35. grub_machine_mmap_init (void)
  36. {
  37. grub_uint8_t high, low, b, c, d;
  38. grub_cmos_read (QEMU_CMOS_MEMSIZE_HIGH, &high);
  39. grub_cmos_read (QEMU_CMOS_MEMSIZE_LOW, &low);
  40. mem_size = ((grub_uint64_t) high) << 24
  41. | ((grub_uint64_t) low) << 16;
  42. if (mem_size > 0)
  43. {
  44. /* Don't ask... */
  45. mem_size += (16 * 1024 * 1024);
  46. }
  47. else
  48. {
  49. grub_cmos_read (QEMU_CMOS_MEMSIZE2_HIGH, &high);
  50. grub_cmos_read (QEMU_CMOS_MEMSIZE2_LOW, &low);
  51. mem_size
  52. = ((((grub_uint64_t) high) << 18) | (((grub_uint64_t) low) << 10))
  53. + 1024 * 1024;
  54. }
  55. grub_cmos_read (0x5b, &b);
  56. grub_cmos_read (0x5c, &c);
  57. grub_cmos_read (0x5d, &d);
  58. above_4g = (((grub_uint64_t) b) << 16)
  59. | (((grub_uint64_t) c) << 24)
  60. | (((grub_uint64_t) d) << 32);
  61. }
  62. grub_err_t
  63. grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
  64. {
  65. if (hook (0x0,
  66. (grub_addr_t) _start,
  67. GRUB_MEMORY_AVAILABLE, hook_data))
  68. return 1;
  69. if (hook ((grub_addr_t) _end,
  70. 0xa0000 - (grub_addr_t) _end,
  71. GRUB_MEMORY_AVAILABLE, hook_data))
  72. return 1;
  73. if (hook (GRUB_MEMORY_MACHINE_UPPER,
  74. 0x100000 - GRUB_MEMORY_MACHINE_UPPER,
  75. GRUB_MEMORY_RESERVED, hook_data))
  76. return 1;
  77. /* Everything else is free. */
  78. if (hook (0x100000,
  79. min (mem_size, (grub_uint32_t) -GRUB_BOOT_MACHINE_SIZE) - 0x100000,
  80. GRUB_MEMORY_AVAILABLE, hook_data))
  81. return 1;
  82. /* Protect boot.img, which contains the gdt. It is mapped at the top of memory
  83. (it is also mapped below 0x100000, but we already reserved that area). */
  84. if (hook ((grub_uint32_t) -GRUB_BOOT_MACHINE_SIZE,
  85. GRUB_BOOT_MACHINE_SIZE,
  86. GRUB_MEMORY_RESERVED, hook_data))
  87. return 1;
  88. if (above_4g != 0 && hook (0x100000000ULL, above_4g,
  89. GRUB_MEMORY_AVAILABLE, hook_data))
  90. return 1;
  91. return 0;
  92. }