init.c 7.9 KB

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