init.c 4.3 KB

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