header.S 7.8 KB

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