init.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* init.c - generic U-Boot initialization and finalization */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2013 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/env.h>
  20. #include <grub/kernel.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. #include <grub/offsets.h>
  24. #include <grub/term.h>
  25. #include <grub/time.h>
  26. #include <grub/machine/kernel.h>
  27. #include <grub/uboot/console.h>
  28. #include <grub/uboot/disk.h>
  29. #include <grub/uboot/uboot.h>
  30. #include <grub/uboot/api_public.h>
  31. #include <grub/cpu/system.h>
  32. #include <grub/cache.h>
  33. extern char __bss_start[];
  34. extern char _end[];
  35. extern grub_size_t grub_total_module_size;
  36. static unsigned long timer_start;
  37. void
  38. grub_exit (void)
  39. {
  40. grub_uboot_return (0);
  41. }
  42. static grub_uint64_t
  43. uboot_timer_ms (void)
  44. {
  45. static grub_uint32_t last = 0, high = 0;
  46. grub_uint32_t cur = grub_uboot_get_timer (timer_start);
  47. if (cur < last)
  48. high++;
  49. last = cur;
  50. return (((grub_uint64_t) high) << 32) | cur;
  51. }
  52. #ifdef __arm__
  53. static grub_uint64_t
  54. rpi_timer_ms (void)
  55. {
  56. static grub_uint32_t last = 0, high = 0;
  57. grub_uint32_t cur = *(volatile grub_uint32_t *) 0x20003004;
  58. if (cur < last)
  59. high++;
  60. last = cur;
  61. return grub_divmod64 ((((grub_uint64_t) high) << 32) | cur, 1000, 0);
  62. }
  63. #endif
  64. void
  65. grub_machine_init (void)
  66. {
  67. int ver;
  68. /* First of all - establish connection with U-Boot */
  69. ver = grub_uboot_api_init ();
  70. if (!ver)
  71. {
  72. /* Don't even have a console to log errors to... */
  73. grub_exit ();
  74. }
  75. else if (ver > API_SIG_VERSION)
  76. {
  77. /* Try to print an error message */
  78. grub_uboot_puts ("invalid U-Boot API version\n");
  79. }
  80. /* Initialize the console so that GRUB can display messages. */
  81. grub_console_init_early ();
  82. /* Enumerate memory and initialize the memory management system. */
  83. grub_uboot_mm_init ();
  84. /* Should be earlier but it needs memalign. */
  85. #ifdef __arm__
  86. grub_arm_enable_caches_mmu ();
  87. #endif
  88. grub_dprintf ("init", "__bss_start: %p\n", __bss_start);
  89. grub_dprintf ("init", "_end: %p\n", _end);
  90. grub_dprintf ("init", "grub_modbase: %p\n", (void *) grub_modbase);
  91. grub_dprintf ("init", "grub_modules_get_end(): %p\n",
  92. (void *) grub_modules_get_end ());
  93. /* Initialise full terminfo support */
  94. grub_console_init_lately ();
  95. /* Enumerate uboot devices */
  96. grub_uboot_probe_hardware ();
  97. /* Initialise timer */
  98. #ifdef __arm__
  99. if (grub_uboot_get_machine_type () == GRUB_ARM_MACHINE_TYPE_RASPBERRY_PI)
  100. {
  101. grub_install_get_time_ms (rpi_timer_ms);
  102. }
  103. else
  104. #endif
  105. {
  106. timer_start = grub_uboot_get_timer (0);
  107. grub_install_get_time_ms (uboot_timer_ms);
  108. }
  109. /* Initialize */
  110. grub_ubootdisk_init ();
  111. }
  112. void
  113. grub_machine_fini (int flags __attribute__ ((unused)))
  114. {
  115. }
  116. /*
  117. * grub_machine_get_bootlocation():
  118. * Called from kern/main.c, which expects a device name (minus parentheses)
  119. * and a filesystem path back, if any are known.
  120. * Any returned values must be pointers to dynamically allocated strings.
  121. */
  122. void
  123. grub_machine_get_bootlocation (char **device, char **path)
  124. {
  125. char *tmp;
  126. tmp = grub_uboot_env_get ("grub_bootdev");
  127. if (tmp)
  128. {
  129. *device = grub_strdup (tmp);
  130. if (*device == NULL)
  131. return;
  132. }
  133. else
  134. *device = NULL;
  135. tmp = grub_uboot_env_get ("grub_bootpath");
  136. if (tmp)
  137. {
  138. *path = grub_strdup (tmp);
  139. if (*path == NULL)
  140. return;
  141. }
  142. else
  143. *path = NULL;
  144. }
  145. void
  146. grub_uboot_fini (void)
  147. {
  148. grub_ubootdisk_fini ();
  149. grub_console_fini ();
  150. }