header.S 7.8 KB

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