header.S 8.1 KB

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