header.S 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * header.S
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * Based on bootsect.S and setup.S
  7. * modified by more people than can be counted
  8. *
  9. * Rewritten as a common file by H. Peter Anvin (Apr 2007)
  10. *
  11. * BIG FAT NOTE: We're in real mode using 64k segments. Therefore segment
  12. * addresses must be multiplied by 16 to obtain their respective linear
  13. * addresses. To avoid confusion, linear addresses are written using leading
  14. * hex while segment addresses are written as segment:offset.
  15. *
  16. */
  17. #include <asm/segment.h>
  18. #include <linux/utsrelease.h>
  19. #include <asm/boot.h>
  20. #include <asm/e820.h>
  21. #include <asm/page_types.h>
  22. #include <asm/setup.h>
  23. #include "boot.h"
  24. #include "offsets.h"
  25. BOOTSEG = 0x07C0 /* original address of boot-sector */
  26. SYSSEG = 0x1000 /* historical load address >> 4 */
  27. #ifndef SVGA_MODE
  28. #define SVGA_MODE ASK_VGA
  29. #endif
  30. #ifndef RAMDISK
  31. #define RAMDISK 0
  32. #endif
  33. #ifndef ROOT_RDONLY
  34. #define ROOT_RDONLY 1
  35. #endif
  36. .code16
  37. .section ".bstext", "ax"
  38. .global bootsect_start
  39. bootsect_start:
  40. # Normalize the start address
  41. ljmp $BOOTSEG, $start2
  42. start2:
  43. movw %cs, %ax
  44. movw %ax, %ds
  45. movw %ax, %es
  46. movw %ax, %ss
  47. xorw %sp, %sp
  48. sti
  49. cld
  50. movw $bugger_off_msg, %si
  51. msg_loop:
  52. lodsb
  53. andb %al, %al
  54. jz bs_die
  55. movb $0xe, %ah
  56. movw $7, %bx
  57. int $0x10
  58. jmp msg_loop
  59. bs_die:
  60. # Allow the user to press a key, then reboot
  61. xorw %ax, %ax
  62. int $0x16
  63. int $0x19
  64. # int 0x19 should never return. In case it does anyway,
  65. # invoke the BIOS reset code...
  66. ljmp $0xf000,$0xfff0
  67. .section ".bsdata", "a"
  68. bugger_off_msg:
  69. .ascii "Direct booting from floppy is no longer supported.\r\n"
  70. .ascii "Please use a boot loader program instead.\r\n"
  71. .ascii "\n"
  72. .ascii "Remove disk and press any key to reboot . . .\r\n"
  73. .byte 0
  74. # Kernel attributes; used by setup. This is part 1 of the
  75. # header, from the old boot sector.
  76. .section ".header", "a"
  77. .globl hdr
  78. hdr:
  79. setup_sects: .byte 0 /* Filled in by build.c */
  80. root_flags: .word ROOT_RDONLY
  81. syssize: .long 0 /* Filled in by build.c */
  82. ram_size: .word 0 /* Obsolete */
  83. vid_mode: .word SVGA_MODE
  84. root_dev: .word 0 /* Filled in by build.c */
  85. boot_flag: .word 0xAA55
  86. # offset 512, entry point
  87. .globl _start
  88. _start:
  89. # Explicitly enter this as bytes, or the assembler
  90. # tries to generate a 3-byte jump here, which causes
  91. # everything else to push off to the wrong offset.
  92. .byte 0xeb # short (2-byte) jump
  93. .byte start_of_setup-1f
  94. 1:
  95. # Part 2 of the header, from the old setup.S
  96. .ascii "HdrS" # header signature
  97. .word 0x0209 # header version number (>= 0x0105)
  98. # or else old loadlin-1.5 will fail)
  99. .globl realmode_swtch
  100. realmode_swtch: .word 0, 0 # default_switch, SETUPSEG
  101. start_sys_seg: .word SYSSEG # obsolete and meaningless, but just
  102. # in case something decided to "use" it
  103. .word kernel_version-512 # pointing to kernel version string
  104. # above section of header is compatible
  105. # with loadlin-1.5 (header v1.5). Don't
  106. # change it.
  107. type_of_loader: .byte 0 # 0 means ancient bootloader, newer
  108. # bootloaders know to change this.
  109. # See Documentation/i386/boot.txt for
  110. # assigned ids
  111. # flags, unused bits must be zero (RFU) bit within loadflags
  112. loadflags:
  113. LOADED_HIGH = 1 # If set, the kernel is loaded high
  114. CAN_USE_HEAP = 0x80 # If set, the loader also has set
  115. # heap_end_ptr to tell how much
  116. # space behind setup.S can be used for
  117. # heap purposes.
  118. # Only the loader knows what is free
  119. .byte LOADED_HIGH
  120. setup_move_size: .word 0x8000 # size to move, when setup is not
  121. # loaded at 0x90000. We will move setup
  122. # to 0x90000 then just before jumping
  123. # into the kernel. However, only the
  124. # loader knows how much data behind
  125. # us also needs to be loaded.
  126. code32_start: # here loaders can put a different
  127. # start address for 32-bit code.
  128. .long 0x100000 # 0x100000 = default for big kernel
  129. ramdisk_image: .long 0 # address of loaded ramdisk image
  130. # Here the loader puts the 32-bit
  131. # address where it loaded the image.
  132. # This only will be read by the kernel.
  133. ramdisk_size: .long 0 # its size in bytes
  134. bootsect_kludge:
  135. .long 0 # obsolete
  136. heap_end_ptr: .word _end+STACK_SIZE-512
  137. # (Header version 0x0201 or later)
  138. # space from here (exclusive) down to
  139. # end of setup code can be used by setup
  140. # for local heap purposes.
  141. pad1: .word 0
  142. cmd_line_ptr: .long 0 # (Header version 0x0202 or later)
  143. # If nonzero, a 32-bit pointer
  144. # to the kernel command line.
  145. # The command line should be
  146. # located between the start of
  147. # setup and the end of low
  148. # memory (0xa0000), or it may
  149. # get overwritten before it
  150. # gets read. If this field is
  151. # used, there is no longer
  152. # anything magical about the
  153. # 0x90000 segment; the setup
  154. # can be located anywhere in
  155. # low memory 0x10000 or higher.
  156. ramdisk_max: .long 0x7fffffff
  157. # (Header version 0x0203 or later)
  158. # The highest safe address for
  159. # the contents of an initrd
  160. # The current kernel allows up to 4 GB,
  161. # but leave it at 2 GB to avoid
  162. # possible bootloader bugs.
  163. kernel_alignment: .long CONFIG_PHYSICAL_ALIGN #physical addr alignment
  164. #required for protected mode
  165. #kernel
  166. #ifdef CONFIG_RELOCATABLE
  167. relocatable_kernel: .byte 1
  168. #else
  169. relocatable_kernel: .byte 0
  170. #endif
  171. pad2: .byte 0
  172. pad3: .word 0
  173. cmdline_size: .long COMMAND_LINE_SIZE-1 #length of the command line,
  174. #added with boot protocol
  175. #version 2.06
  176. hardware_subarch: .long 0 # subarchitecture, added with 2.07
  177. # default to 0 for normal x86 PC
  178. hardware_subarch_data: .quad 0
  179. payload_offset: .long input_data
  180. payload_length: .long input_data_end-input_data
  181. setup_data: .quad 0 # 64-bit physical pointer to
  182. # single linked list of
  183. # struct setup_data
  184. # End of setup header #####################################################
  185. .section ".inittext", "ax"
  186. start_of_setup:
  187. #ifdef SAFE_RESET_DISK_CONTROLLER
  188. # Reset the disk controller.
  189. movw $0x0000, %ax # Reset disk controller
  190. movb $0x80, %dl # All disks
  191. int $0x13
  192. #endif
  193. # Force %es = %ds
  194. movw %ds, %ax
  195. movw %ax, %es
  196. cld
  197. # Apparently some ancient versions of LILO invoked the kernel with %ss != %ds,
  198. # which happened to work by accident for the old code. Recalculate the stack
  199. # pointer if %ss is invalid. Otherwise leave it alone, LOADLIN sets up the
  200. # stack behind its own code, so we can't blindly put it directly past the heap.
  201. movw %ss, %dx
  202. cmpw %ax, %dx # %ds == %ss?
  203. movw %sp, %dx
  204. je 2f # -> assume %sp is reasonably set
  205. # Invalid %ss, make up a new stack
  206. movw $_end, %dx
  207. testb $CAN_USE_HEAP, loadflags
  208. jz 1f
  209. movw heap_end_ptr, %dx
  210. 1: addw $STACK_SIZE, %dx
  211. jnc 2f
  212. xorw %dx, %dx # Prevent wraparound
  213. 2: # Now %dx should point to the end of our stack space
  214. andw $~3, %dx # dword align (might as well...)
  215. jnz 3f
  216. movw $0xfffc, %dx # Make sure we're not zero
  217. 3: movw %ax, %ss
  218. movzwl %dx, %esp # Clear upper half of %esp
  219. sti # Now we should have a working stack
  220. # We will have entered with %cs = %ds+0x20, normalize %cs so
  221. # it is on par with the other segments.
  222. pushw %ds
  223. pushw $6f
  224. lretw
  225. 6:
  226. # Check signature at end of setup
  227. cmpl $0x5a5aaa55, setup_sig
  228. jne setup_bad
  229. # Zero the bss
  230. movw $__bss_start, %di
  231. movw $_end+3, %cx
  232. xorl %eax, %eax
  233. subw %di, %cx
  234. shrw $2, %cx
  235. rep; stosl
  236. # Jump to C code (should not return)
  237. calll main
  238. # Setup corrupt somehow...
  239. setup_bad:
  240. movl $setup_corrupt, %eax
  241. calll puts
  242. # Fall through...
  243. .globl die
  244. .type die, @function
  245. die:
  246. hlt
  247. jmp die
  248. .size die, .-die
  249. .section ".initdata", "a"
  250. setup_corrupt:
  251. .byte 7
  252. .string "No setup signature found...\n"