startup.S 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 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/offsets.h>
  19. #include <grub/symbol.h>
  20. #include <grub/machine/kernel.h>
  21. /*
  22. * GRUB is called from U-Boot as a Linux Kernel type image, which
  23. * means among other things that it always enters in ARM state.
  24. *
  25. * coreboot starts in ARM mode as well.
  26. *
  27. * Overview of GRUB image layout:
  28. *
  29. * _start:
  30. * Entry point (1 ARM branch instruction, to "codestart")
  31. * grub_total_module_size:
  32. * Data field: Size of included module blob
  33. * (when generated by grub-mkimage)
  34. * codestart:
  35. * Remainder of statically-linked executable code and data.
  36. * __bss_start:
  37. * Start of included module blob.
  38. * Also where global/static variables are located.
  39. * _end:
  40. * End of bss region (but not necessarily module blob).
  41. * <stack>:
  42. * <modules>:
  43. * Loadable modules, post relocation.
  44. * <heap>:
  45. */
  46. .text
  47. .arm
  48. FUNCTION(_start)
  49. b codestart
  50. @ Size of final image integrated module blob - set by grub-mkimage
  51. .org _start + GRUB_KERNEL_MACHINE_TOTAL_MODULE_SIZE
  52. VARIABLE(grub_total_module_size)
  53. .long 0
  54. VARIABLE(grub_modbase)
  55. .long 0
  56. bss_start_ptr:
  57. .long EXT_C(__bss_start)
  58. end_ptr:
  59. .long EXT_C(_end)
  60. @ Memory map at start:
  61. @ * text+data
  62. @ * list relocations
  63. @ * modules
  64. @ Before we enter C, we need to apply the relocations
  65. @ and get following map:
  66. @ * text+data
  67. @ * BSS (cleared)
  68. @ * stack
  69. @ * modules
  70. @
  71. @ To make things easier we ensure
  72. @ that BSS+stack is larger than list of relocations
  73. @ by increasing stack if necessarry.
  74. @ This allows us to always unconditionally copy backwards
  75. @ Currently list of relocations is ~5K and stack is set
  76. @ to be at least 256K
  77. FUNCTION(codestart)
  78. @ Store context: Machine ID, atags/dtb, ...
  79. @ U-Boot API signature is stored on the U-Boot heap
  80. @ Stack pointer used as start address for signature probing
  81. mov r12, sp
  82. adr sp, entry_state
  83. push {r0-r12,lr} @ store U-Boot context (sp in r12)
  84. adr r1, _start
  85. ldr r0, bss_start_ptr @ src
  86. add r0, r0, r1
  87. add r0, r0, #(GRUB_KERNEL_MACHINE_MOD_ALIGN - 1)
  88. mvn r2, #(GRUB_KERNEL_MACHINE_MOD_ALIGN - 1)
  89. and r0, r0, r2
  90. 1:
  91. ldr r3, [r0], #4 @load next offset
  92. @ both -2 and -1 are treated the same as we have only one type of relocs
  93. @ -2 means "end of this type of relocs" and -1 means "end of all relocs"
  94. add r2, r3, #2
  95. cmp r2, #1
  96. bls reloc_done
  97. @ Adjust next offset
  98. ldr r2, [r3, r1]
  99. add r2, r2, r1
  100. str r2, [r3, r1]
  101. b 1b
  102. reloc_done:
  103. @ Modules have been stored as a blob
  104. @ they need to be manually relocated to _end
  105. add r0, r0, #(GRUB_KERNEL_MACHINE_MOD_ALIGN - 1)
  106. mvn r1, #(GRUB_KERNEL_MACHINE_MOD_ALIGN - 1)
  107. and r0, r0, r1 @ src = aligned end of relocations
  108. ldr r1, end_ptr @ dst = End of BSS
  109. ldr r2, grub_total_module_size @ blob size
  110. add r1, r1, #GRUB_KERNEL_MACHINE_STACK_SIZE
  111. and r1, r1, #~0x7 @ Ensure 8-byte alignment
  112. sub sp, r1, #8
  113. add r1, r1, #1024
  114. str r1, EXT_C(grub_modbase)
  115. /* Coreboot already places modules at right place. */
  116. #ifndef GRUB_MACHINE_COREBOOT
  117. add r1, r1, r2
  118. add r0, r0, r2
  119. sub r1, r1, #4
  120. sub r0, r0, #4
  121. 1: ldr r3, [r0], #-4 @ r3 = *src--
  122. str r3, [r1], #-4 @ *dst-- = r3
  123. subs r2, #4 @ remaining -= 4
  124. bne 1b @ while remaining != 0
  125. #endif
  126. @ Since we _are_ the C run-time, we need to manually zero the BSS
  127. @ region before continuing
  128. ldr r0, bss_start_ptr @ zero from here
  129. @ If unaligned, bytewise zero until base address aligned.
  130. mov r2, #0
  131. 1: tst r0, #3
  132. beq 2f
  133. strb r2, [r0], #1
  134. b 1b
  135. 2: ldr r1, end_ptr @ to here
  136. 1: str r2, [r0], #4
  137. cmp r0, r1
  138. bne 1b
  139. b EXT_C(grub_main)
  140. .align 3
  141. @ U-boot/coreboot context stack space
  142. VARIABLE(grub_arm_saved_registers)
  143. .long 0 @ r0
  144. .long 0 @ r1
  145. .long 0 @ r2
  146. .long 0 @ r3
  147. .long 0 @ r4
  148. .long 0 @ r5
  149. .long 0 @ r6
  150. .long 0 @ r7
  151. .long 0 @ r8
  152. .long 0 @ r9
  153. .long 0 @ r10
  154. .long 0 @ r11
  155. .long 0 @ sp
  156. .long 0 @ lr
  157. entry_state: