init.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* init.c -- Initialize GRUB on the newworld mac (PPC). */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2003,2004,2005,2007,2008,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/kernel.h>
  20. #include <grub/dl.h>
  21. #include <grub/disk.h>
  22. #include <grub/mm.h>
  23. #include <grub/partition.h>
  24. #include <grub/normal.h>
  25. #include <grub/fs.h>
  26. #include <grub/setjmp.h>
  27. #include <grub/env.h>
  28. #include <grub/misc.h>
  29. #include <grub/time.h>
  30. #include <grub/ieee1275/console.h>
  31. #include <grub/ieee1275/ofdisk.h>
  32. #ifdef __sparc__
  33. #include <grub/ieee1275/obdisk.h>
  34. #endif
  35. #include <grub/ieee1275/ieee1275.h>
  36. #include <grub/net.h>
  37. #include <grub/offsets.h>
  38. #include <grub/memory.h>
  39. #include <grub/loader.h>
  40. #ifdef __i386__
  41. #include <grub/cpu/tsc.h>
  42. #endif
  43. #ifdef __sparc__
  44. #include <grub/machine/kernel.h>
  45. #endif
  46. /* The minimal heap size we can live with. */
  47. #define HEAP_MIN_SIZE (unsigned long) (2 * 1024 * 1024)
  48. /* The maximum heap size we're going to claim */
  49. #ifdef __i386__
  50. #define HEAP_MAX_SIZE (unsigned long) (64 * 1024 * 1024)
  51. #else
  52. #define HEAP_MAX_SIZE (unsigned long) (32 * 1024 * 1024)
  53. #endif
  54. /* If possible, we will avoid claiming heap above this address, because it
  55. seems to cause relocation problems with OSes that link at 4 MiB */
  56. #ifdef __i386__
  57. #define HEAP_MAX_ADDR (unsigned long) (64 * 1024 * 1024)
  58. #else
  59. #define HEAP_MAX_ADDR (unsigned long) (32 * 1024 * 1024)
  60. #endif
  61. extern char _start[];
  62. extern char _end[];
  63. #ifdef __sparc__
  64. grub_addr_t grub_ieee1275_original_stack;
  65. #endif
  66. void
  67. grub_exit (void)
  68. {
  69. grub_ieee1275_exit ();
  70. }
  71. /* Translate an OF filesystem path (separated by backslashes), into a GRUB
  72. path (separated by forward slashes). */
  73. static void
  74. grub_translate_ieee1275_path (char *filepath)
  75. {
  76. char *backslash;
  77. backslash = grub_strchr (filepath, '\\');
  78. while (backslash != 0)
  79. {
  80. *backslash = '/';
  81. backslash = grub_strchr (filepath, '\\');
  82. }
  83. }
  84. void (*grub_ieee1275_net_config) (const char *dev, char **device, char **path,
  85. char *bootpath);
  86. void
  87. grub_machine_get_bootlocation (char **device, char **path)
  88. {
  89. char *bootpath;
  90. char *filename;
  91. char *type;
  92. bootpath = grub_ieee1275_get_boot_dev ();
  93. if (! bootpath)
  94. return;
  95. /* Transform an OF device path to a GRUB path. */
  96. type = grub_ieee1275_get_device_type (bootpath);
  97. if (type && grub_strcmp (type, "network") == 0)
  98. {
  99. char *dev, *canon;
  100. char *ptr;
  101. dev = grub_ieee1275_get_aliasdevname (bootpath);
  102. canon = grub_ieee1275_canonicalise_devname (dev);
  103. if (! canon)
  104. return;
  105. ptr = canon + grub_strlen (canon) - 1;
  106. while (ptr > canon && (*ptr == ',' || *ptr == ':'))
  107. ptr--;
  108. ptr++;
  109. *ptr = 0;
  110. if (grub_ieee1275_net_config)
  111. grub_ieee1275_net_config (canon, device, path, bootpath);
  112. grub_free (dev);
  113. grub_free (canon);
  114. }
  115. else
  116. *device = grub_ieee1275_encode_devname (bootpath);
  117. grub_free (type);
  118. filename = grub_ieee1275_get_filename (bootpath);
  119. if (filename)
  120. {
  121. char *lastslash = grub_strrchr (filename, '\\');
  122. /* Truncate at last directory. */
  123. if (lastslash)
  124. {
  125. *lastslash = '\0';
  126. grub_translate_ieee1275_path (filename);
  127. *path = filename;
  128. }
  129. }
  130. grub_free (bootpath);
  131. }
  132. /* Claim some available memory in the first /memory node. */
  133. #ifdef __sparc__
  134. static void
  135. grub_claim_heap (void)
  136. {
  137. grub_mm_init_region ((void *) (grub_modules_get_end ()
  138. + GRUB_KERNEL_MACHINE_STACK_SIZE), 0x200000);
  139. }
  140. #else
  141. /* Helper for grub_claim_heap. */
  142. static int
  143. heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
  144. void *data)
  145. {
  146. unsigned long *total = data;
  147. if (type != GRUB_MEMORY_AVAILABLE)
  148. return 0;
  149. if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_PRE1_5M_CLAIM))
  150. {
  151. if (addr + len <= 0x180000)
  152. return 0;
  153. if (addr < 0x180000)
  154. {
  155. len = addr + len - 0x180000;
  156. addr = 0x180000;
  157. }
  158. }
  159. len -= 1; /* Required for some firmware. */
  160. /* Never exceed HEAP_MAX_SIZE */
  161. if (*total + len > HEAP_MAX_SIZE)
  162. len = HEAP_MAX_SIZE - *total;
  163. /* Avoid claiming anything above HEAP_MAX_ADDR, if possible. */
  164. if ((addr < HEAP_MAX_ADDR) && /* if it's too late, don't bother */
  165. (addr + len > HEAP_MAX_ADDR) && /* if it wasn't available anyway, don't bother */
  166. (*total + (HEAP_MAX_ADDR - addr) > HEAP_MIN_SIZE)) /* only limit ourselves when we can afford to */
  167. len = HEAP_MAX_ADDR - addr;
  168. /* In theory, firmware should already prevent this from happening by not
  169. listing our own image in /memory/available. The check below is intended
  170. as a safeguard in case that doesn't happen. However, it doesn't protect
  171. us from corrupting our module area, which extends up to a
  172. yet-undetermined region above _end. */
  173. if ((addr < (grub_addr_t) _end) && ((addr + len) > (grub_addr_t) _start))
  174. {
  175. grub_printf ("Warning: attempt to claim over our own code!\n");
  176. len = 0;
  177. }
  178. if (len)
  179. {
  180. grub_err_t err;
  181. /* Claim and use it. */
  182. err = grub_claimmap (addr, len);
  183. if (err)
  184. return err;
  185. grub_mm_init_region ((void *) (grub_addr_t) addr, len);
  186. }
  187. *total += len;
  188. if (*total >= HEAP_MAX_SIZE)
  189. return 1;
  190. return 0;
  191. }
  192. static void
  193. grub_claim_heap (void)
  194. {
  195. unsigned long total = 0;
  196. if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_FORCE_CLAIM))
  197. heap_init (GRUB_IEEE1275_STATIC_HEAP_START, GRUB_IEEE1275_STATIC_HEAP_LEN,
  198. 1, &total);
  199. else
  200. grub_machine_mmap_iterate (heap_init, &total);
  201. }
  202. #endif
  203. static void
  204. grub_parse_cmdline (void)
  205. {
  206. grub_ssize_t actual;
  207. char args[256];
  208. if (grub_ieee1275_get_property (grub_ieee1275_chosen, "bootargs", &args,
  209. sizeof args, &actual) == 0
  210. && actual > 1)
  211. {
  212. int i = 0;
  213. while (i < actual)
  214. {
  215. char *command = &args[i];
  216. char *end;
  217. char *val;
  218. end = grub_strchr (command, ';');
  219. if (end == 0)
  220. i = actual; /* No more commands after this one. */
  221. else
  222. {
  223. *end = '\0';
  224. i += end - command + 1;
  225. while (grub_isspace(args[i]))
  226. i++;
  227. }
  228. /* Process command. */
  229. val = grub_strchr (command, '=');
  230. if (val)
  231. {
  232. *val = '\0';
  233. grub_env_set (command, val + 1);
  234. }
  235. }
  236. }
  237. }
  238. grub_addr_t grub_modbase;
  239. void
  240. grub_machine_init (void)
  241. {
  242. grub_modbase = ALIGN_UP((grub_addr_t) _end
  243. + GRUB_KERNEL_MACHINE_MOD_GAP,
  244. GRUB_KERNEL_MACHINE_MOD_ALIGN);
  245. grub_ieee1275_init ();
  246. grub_console_init_early ();
  247. grub_claim_heap ();
  248. grub_console_init_lately ();
  249. #ifdef __sparc__
  250. grub_obdisk_init ();
  251. #else
  252. grub_ofdisk_init ();
  253. #endif
  254. grub_parse_cmdline ();
  255. #ifdef __i386__
  256. grub_tsc_init ();
  257. #else
  258. grub_install_get_time_ms (grub_rtc_get_time_ms);
  259. #endif
  260. }
  261. void
  262. grub_machine_fini (int flags)
  263. {
  264. if (flags & GRUB_LOADER_FLAG_NORETURN)
  265. {
  266. #ifdef __sparc__
  267. grub_obdisk_fini ();
  268. #else
  269. grub_ofdisk_fini ();
  270. #endif
  271. grub_console_fini ();
  272. }
  273. }
  274. grub_uint64_t
  275. grub_rtc_get_time_ms (void)
  276. {
  277. grub_uint32_t msecs = 0;
  278. grub_ieee1275_milliseconds (&msecs);
  279. return msecs;
  280. }