init.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010 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/kernel.h>
  19. #include <grub/mm.h>
  20. #include <grub/machine/boot.h>
  21. #include <grub/machine/init.h>
  22. #include <grub/machine/memory.h>
  23. #include <grub/machine/console.h>
  24. #include <grub/machine/kernel.h>
  25. #include <grub/types.h>
  26. #include <grub/err.h>
  27. #include <grub/dl.h>
  28. #include <grub/misc.h>
  29. #include <grub/loader.h>
  30. #include <grub/env.h>
  31. #include <grub/cache.h>
  32. #include <grub/time.h>
  33. #include <grub/cpu/tsc.h>
  34. GRUB_EXPORT(grub_os_area_addr);
  35. GRUB_EXPORT(grub_os_area_size);
  36. GRUB_EXPORT(grub_get_mmap_entry);
  37. GRUB_EXPORT(grub_reboot);
  38. GRUB_EXPORT(grub_halt);
  39. GRUB_EXPORT(grub_stop_floppy);
  40. GRUB_EXPORT(grub_mmap_high);
  41. struct mem_region
  42. {
  43. grub_addr_t addr;
  44. grub_size_t size;
  45. };
  46. #define MAX_REGIONS 32
  47. static struct mem_region mem_regions[MAX_REGIONS];
  48. static int num_regions;
  49. grub_addr_t grub_os_area_addr;
  50. grub_size_t grub_os_area_size;
  51. grub_size_t grub_mmap_high;
  52. static char *
  53. make_install_device (void)
  54. {
  55. /* XXX: This should be enough. */
  56. char dev[100], *ptr = dev;
  57. grub_snprintf (dev, sizeof (dev), "%d", grub_boot_drive);
  58. grub_env_set ("boot_drive", dev);
  59. if (grub_prefix[0] != '(')
  60. {
  61. /* No hardcoded root partition - make it from the boot drive and the
  62. partition number encoded at the install time. */
  63. if (grub_boot_drive == GRUB_BOOT_MACHINE_PXE_DL)
  64. {
  65. grub_strcpy (dev, "(pxe");
  66. ptr += sizeof ("(pxe") - 1;
  67. }
  68. else
  69. {
  70. grub_snprintf (dev, sizeof (dev),
  71. "(%cd%u", (grub_boot_drive & 0x80) ? 'h' : 'f',
  72. grub_boot_drive & 0x7f);
  73. ptr += grub_strlen (ptr);
  74. if (grub_install_dos_part >= 0)
  75. grub_snprintf (ptr, sizeof (dev) - (ptr - dev),
  76. ",%u", grub_install_dos_part + 1);
  77. ptr += grub_strlen (ptr);
  78. if (grub_install_bsd_part >= 0)
  79. grub_snprintf (ptr, sizeof (dev) - (ptr - dev), ",%c",
  80. grub_install_bsd_part + 'a');
  81. ptr += grub_strlen (ptr);
  82. }
  83. grub_snprintf (ptr, sizeof (dev) - (ptr - dev), ")%s", grub_prefix);
  84. grub_strcpy (grub_prefix, dev);
  85. }
  86. return grub_prefix;
  87. }
  88. /* Add a memory region. */
  89. static void
  90. add_mem_region (grub_addr_t addr, grub_size_t size)
  91. {
  92. if (num_regions == MAX_REGIONS)
  93. /* Ignore. */
  94. return;
  95. mem_regions[num_regions].addr = addr;
  96. mem_regions[num_regions].size = size;
  97. num_regions++;
  98. }
  99. /* Compact memory regions. */
  100. static void
  101. compact_mem_regions (void)
  102. {
  103. int i, j;
  104. /* Sort them. */
  105. for (i = 0; i < num_regions - 1; i++)
  106. for (j = i + 1; j < num_regions; j++)
  107. if (mem_regions[i].addr > mem_regions[j].addr)
  108. {
  109. struct mem_region tmp = mem_regions[i];
  110. mem_regions[i] = mem_regions[j];
  111. mem_regions[j] = tmp;
  112. }
  113. /* Merge overlaps. */
  114. for (i = 0; i < num_regions - 1; i++)
  115. if (mem_regions[i].addr + mem_regions[i].size >= mem_regions[i + 1].addr)
  116. {
  117. j = i + 1;
  118. if (mem_regions[i].addr + mem_regions[i].size
  119. < mem_regions[j].addr + mem_regions[j].size)
  120. mem_regions[i].size = (mem_regions[j].addr + mem_regions[j].size
  121. - mem_regions[i].addr);
  122. grub_memmove (mem_regions + j, mem_regions + j + 1,
  123. (num_regions - j - 1) * sizeof (struct mem_region));
  124. i--;
  125. num_regions--;
  126. }
  127. }
  128. static int
  129. mmap_hook (grub_uint64_t addr, grub_uint64_t size, grub_uint32_t type,
  130. void *closure __attribute__ ((unused)))
  131. {
  132. /* Avoid the lower memory. */
  133. if (addr < 0x100000)
  134. {
  135. if (size <= 0x100000 - addr)
  136. return 0;
  137. size -= 0x100000 - addr;
  138. addr = 0x100000;
  139. }
  140. /* Ignore >4GB. */
  141. if (addr <= 0xFFFFFFFF && type == GRUB_MACHINE_MEMORY_AVAILABLE)
  142. {
  143. grub_size_t len;
  144. len = (grub_size_t) ((addr + size > 0xFFFFFFFF)
  145. ? 0xFFFFFFFF - addr
  146. : size);
  147. add_mem_region (addr, len);
  148. }
  149. return 0;
  150. }
  151. void
  152. grub_machine_init (void)
  153. {
  154. int i;
  155. int grub_lower_mem;
  156. /* Initialize the console as early as possible. */
  157. grub_console_init ();
  158. grub_lower_mem = grub_get_memsize (0) << 10;
  159. /* Sanity check. */
  160. if (grub_lower_mem < GRUB_MEMORY_MACHINE_RESERVED_END)
  161. grub_fatal ("too small memory");
  162. #if 0
  163. /* Turn on Gate A20 to access >1MB. */
  164. grub_gate_a20 (1);
  165. #endif
  166. /* FIXME: This prevents loader/i386/linux.c from using low memory. When our
  167. heap implements support for requesting a chunk in low memory, this should
  168. no longer be a problem. */
  169. #if 0
  170. /* Add the lower memory into free memory. */
  171. if (grub_lower_mem >= GRUB_MEMORY_MACHINE_RESERVED_END)
  172. add_mem_region (GRUB_MEMORY_MACHINE_RESERVED_END,
  173. grub_lower_mem - GRUB_MEMORY_MACHINE_RESERVED_END);
  174. #endif
  175. grub_machine_mmap_iterate (mmap_hook, 0);
  176. compact_mem_regions ();
  177. /* Add the memory regions to free memory, except for the region starting
  178. from 1MB. This region is partially used for loading OS images.
  179. For now, 1/4 of this is added to free memory. */
  180. for (i = 0; i < num_regions; i++)
  181. if (mem_regions[i].addr == 0x100000)
  182. {
  183. grub_size_t high, quarter;
  184. high = (mem_regions[i].addr + mem_regions[i].size) >> 1;
  185. if (high > GRUB_MMAP_HIGH_MAX)
  186. high = GRUB_MMAP_HIGH_MAX;
  187. grub_mmap_high = high;
  188. high -= mem_regions[i].addr;
  189. quarter = high >> 1;
  190. grub_os_area_addr = mem_regions[i].addr;
  191. grub_os_area_size = high - quarter;
  192. grub_mm_init_region ((void *) (grub_os_area_addr + grub_os_area_size),
  193. quarter);
  194. }
  195. else
  196. grub_mm_init_region ((void *) mem_regions[i].addr, mem_regions[i].size);
  197. if (! grub_os_area_addr)
  198. grub_fatal ("no upper memory");
  199. grub_tsc_init ();
  200. }
  201. void
  202. grub_machine_set_prefix (void)
  203. {
  204. /* Initialize the prefix. */
  205. grub_env_set ("prefix", make_install_device ());
  206. }
  207. void
  208. grub_machine_fini (void)
  209. {
  210. grub_console_fini ();
  211. grub_stop_floppy ();
  212. }
  213. char *
  214. grub_arch_menu_addr (void)
  215. {
  216. char *p =
  217. (char *) (GRUB_MEMORY_MACHINE_DECOMPRESSION_ADDR + grub_kernel_image_size
  218. + grub_total_module_size - GRUB_KERNEL_MACHINE_RAW_SIZE);
  219. return (*p == 0) ? 0 : p;
  220. }