setup.S 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. /*
  2. * setup.S Copyright (C) 1991, 1992 Linus Torvalds
  3. *
  4. * setup.s is responsible for getting the system data from the BIOS,
  5. * and putting them into the appropriate places in system memory.
  6. * both setup.s and system has been loaded by the bootblock.
  7. *
  8. * This code asks the bios for memory/disk/other parameters, and
  9. * puts them in a "safe" place: 0x90000-0x901FF, ie where the
  10. * boot-block used to be. It is then up to the protected mode
  11. * system to read them from there before the area is overwritten
  12. * for buffer-blocks.
  13. *
  14. * Move PS/2 aux init code to psaux.c
  15. * (troyer@saifr00.cfsat.Honeywell.COM) 03Oct92
  16. *
  17. * some changes and additional features by Christoph Niemann,
  18. * March 1993/June 1994 (Christoph.Niemann@linux.org)
  19. *
  20. * add APM BIOS checking by Stephen Rothwell, May 1994
  21. * (sfr@canb.auug.org.au)
  22. *
  23. * High load stuff, initrd support and position independency
  24. * by Hans Lermen & Werner Almesberger, February 1996
  25. * <lermen@elserv.ffm.fgan.de>, <almesber@lrc.epfl.ch>
  26. *
  27. * Video handling moved to video.S by Martin Mares, March 1996
  28. * <mj@k332.feld.cvut.cz>
  29. *
  30. * Extended memory detection scheme retwiddled by orc@pell.chi.il.us (david
  31. * parsons) to avoid loadlin confusion, July 1997
  32. *
  33. * Transcribed from Intel (as86) -> AT&T (gas) by Chris Noe, May 1999.
  34. * <stiker@northlink.com>
  35. *
  36. * Fix to work around buggy BIOSes which don't use carry bit correctly
  37. * and/or report extended memory in CX/DX for e801h memory size detection
  38. * call. As a result the kernel got wrong figures. The int15/e801h docs
  39. * from Ralf Brown interrupt list seem to indicate AX/BX should be used
  40. * anyway. So to avoid breaking many machines (presumably there was a reason
  41. * to orginally use CX/DX instead of AX/BX), we do a kludge to see
  42. * if CX/DX have been changed in the e801 call and if so use AX/BX .
  43. * Michael Miller, April 2001 <michaelm@mjmm.org>
  44. *
  45. * New A20 code ported from SYSLINUX by H. Peter Anvin. AMD Elan bugfixes
  46. * by Robert Schwebel, December 2001 <robert@schwebel.de>
  47. */
  48. #include <asm/segment.h>
  49. #include <linux/utsrelease.h>
  50. #include <linux/compile.h>
  51. #include <asm/boot.h>
  52. #include <asm/e820.h>
  53. #include <asm/page.h>
  54. #include <asm/setup.h>
  55. /* Signature words to ensure LILO loaded us right */
  56. #define SIG1 0xAA55
  57. #define SIG2 0x5A5A
  58. INITSEG = DEF_INITSEG # 0x9000, we move boot here, out of the way
  59. SYSSEG = DEF_SYSSEG # 0x1000, system loaded at 0x10000 (65536).
  60. SETUPSEG = DEF_SETUPSEG # 0x9020, this is the current segment
  61. # ... and the former contents of CS
  62. DELTA_INITSEG = SETUPSEG - INITSEG # 0x0020
  63. .code16
  64. .globl begtext, begdata, begbss, endtext, enddata, endbss
  65. .text
  66. begtext:
  67. .data
  68. begdata:
  69. .bss
  70. begbss:
  71. .text
  72. start:
  73. jmp trampoline
  74. # This is the setup header, and it must start at %cs:2 (old 0x9020:2)
  75. .ascii "HdrS" # header signature
  76. .word 0x0206 # header version number (>= 0x0105)
  77. # or else old loadlin-1.5 will fail)
  78. realmode_swtch: .word 0, 0 # default_switch, SETUPSEG
  79. start_sys_seg: .word SYSSEG
  80. .word kernel_version # pointing to kernel version string
  81. # above section of header is compatible
  82. # with loadlin-1.5 (header v1.5). Don't
  83. # change it.
  84. type_of_loader: .byte 0 # = 0, old one (LILO, Loadlin,
  85. # Bootlin, SYSLX, bootsect...)
  86. # See Documentation/i386/boot.txt for
  87. # assigned ids
  88. # flags, unused bits must be zero (RFU) bit within loadflags
  89. loadflags:
  90. LOADED_HIGH = 1 # If set, the kernel is loaded high
  91. CAN_USE_HEAP = 0x80 # If set, the loader also has set
  92. # heap_end_ptr to tell how much
  93. # space behind setup.S can be used for
  94. # heap purposes.
  95. # Only the loader knows what is free
  96. #ifndef __BIG_KERNEL__
  97. .byte 0
  98. #else
  99. .byte LOADED_HIGH
  100. #endif
  101. setup_move_size: .word 0x8000 # size to move, when setup is not
  102. # loaded at 0x90000. We will move setup
  103. # to 0x90000 then just before jumping
  104. # into the kernel. However, only the
  105. # loader knows how much data behind
  106. # us also needs to be loaded.
  107. code32_start: # here loaders can put a different
  108. # start address for 32-bit code.
  109. #ifndef __BIG_KERNEL__
  110. .long 0x1000 # 0x1000 = default for zImage
  111. #else
  112. .long 0x100000 # 0x100000 = default for big kernel
  113. #endif
  114. ramdisk_image: .long 0 # address of loaded ramdisk image
  115. # Here the loader puts the 32-bit
  116. # address where it loaded the image.
  117. # This only will be read by the kernel.
  118. ramdisk_size: .long 0 # its size in bytes
  119. bootsect_kludge:
  120. .long 0 # obsolete
  121. heap_end_ptr: .word modelist+1024 # (Header version 0x0201 or later)
  122. # space from here (exclusive) down to
  123. # end of setup code can be used by setup
  124. # for local heap purposes.
  125. pad1: .word 0
  126. cmd_line_ptr: .long 0 # (Header version 0x0202 or later)
  127. # If nonzero, a 32-bit pointer
  128. # to the kernel command line.
  129. # The command line should be
  130. # located between the start of
  131. # setup and the end of low
  132. # memory (0xa0000), or it may
  133. # get overwritten before it
  134. # gets read. If this field is
  135. # used, there is no longer
  136. # anything magical about the
  137. # 0x90000 segment; the setup
  138. # can be located anywhere in
  139. # low memory 0x10000 or higher.
  140. ramdisk_max: .long (-__PAGE_OFFSET-(512 << 20)-1) & 0x7fffffff
  141. # (Header version 0x0203 or later)
  142. # The highest safe address for
  143. # the contents of an initrd
  144. kernel_alignment: .long CONFIG_PHYSICAL_ALIGN #physical addr alignment
  145. #required for protected mode
  146. #kernel
  147. #ifdef CONFIG_RELOCATABLE
  148. relocatable_kernel: .byte 1
  149. #else
  150. relocatable_kernel: .byte 0
  151. #endif
  152. pad2: .byte 0
  153. pad3: .word 0
  154. cmdline_size: .long COMMAND_LINE_SIZE-1 #length of the command line,
  155. #added with boot protocol
  156. #version 2.06
  157. trampoline: call start_of_setup
  158. .align 16
  159. # The offset at this point is 0x240
  160. .space (0xeff-0x240+1) # E820 & EDD space (ending at 0xeff)
  161. # End of setup header #####################################################
  162. start_of_setup:
  163. # Bootlin depends on this being done early
  164. movw $0x01500, %ax
  165. movb $0x81, %dl
  166. int $0x13
  167. #ifdef SAFE_RESET_DISK_CONTROLLER
  168. # Reset the disk controller.
  169. movw $0x0000, %ax
  170. movb $0x80, %dl
  171. int $0x13
  172. #endif
  173. # Set %ds = %cs, we know that SETUPSEG = %cs at this point
  174. movw %cs, %ax # aka SETUPSEG
  175. movw %ax, %ds
  176. # Check signature at end of setup
  177. cmpw $SIG1, setup_sig1
  178. jne bad_sig
  179. cmpw $SIG2, setup_sig2
  180. jne bad_sig
  181. jmp good_sig1
  182. # Routine to print asciiz string at ds:si
  183. prtstr:
  184. lodsb
  185. andb %al, %al
  186. jz fin
  187. call prtchr
  188. jmp prtstr
  189. fin: ret
  190. # Space printing
  191. prtsp2: call prtspc # Print double space
  192. prtspc: movb $0x20, %al # Print single space (note: fall-thru)
  193. # Part of above routine, this one just prints ascii al
  194. prtchr: pushw %ax
  195. pushw %cx
  196. movw $7,%bx
  197. movw $0x01, %cx
  198. movb $0x0e, %ah
  199. int $0x10
  200. popw %cx
  201. popw %ax
  202. ret
  203. beep: movb $0x07, %al
  204. jmp prtchr
  205. no_sig_mess: .string "No setup signature found ..."
  206. good_sig1:
  207. jmp good_sig
  208. # We now have to find the rest of the setup code/data
  209. bad_sig:
  210. movw %cs, %ax # SETUPSEG
  211. subw $DELTA_INITSEG, %ax # INITSEG
  212. movw %ax, %ds
  213. xorb %bh, %bh
  214. movb (497), %bl # get setup sect from bootsect
  215. subw $4, %bx # LILO loads 4 sectors of setup
  216. shlw $8, %bx # convert to words (1sect=2^8 words)
  217. movw %bx, %cx
  218. shrw $3, %bx # convert to segment
  219. addw $SYSSEG, %bx
  220. movw %bx, %cs:start_sys_seg
  221. # Move rest of setup code/data to here
  222. movw $2048, %di # four sectors loaded by LILO
  223. subw %si, %si
  224. pushw %cs
  225. popw %es
  226. movw $SYSSEG, %ax
  227. movw %ax, %ds
  228. rep
  229. movsw
  230. movw %cs, %ax # aka SETUPSEG
  231. movw %ax, %ds
  232. cmpw $SIG1, setup_sig1
  233. jne no_sig
  234. cmpw $SIG2, setup_sig2
  235. jne no_sig
  236. jmp good_sig
  237. no_sig:
  238. lea no_sig_mess, %si
  239. call prtstr
  240. no_sig_loop:
  241. hlt
  242. jmp no_sig_loop
  243. good_sig:
  244. movw %cs, %ax # aka SETUPSEG
  245. subw $DELTA_INITSEG, %ax # aka INITSEG
  246. movw %ax, %ds
  247. # Check if an old loader tries to load a big-kernel
  248. testb $LOADED_HIGH, %cs:loadflags # Do we have a big kernel?
  249. jz loader_ok # No, no danger for old loaders.
  250. cmpb $0, %cs:type_of_loader # Do we have a loader that
  251. # can deal with us?
  252. jnz loader_ok # Yes, continue.
  253. pushw %cs # No, we have an old loader,
  254. popw %ds # die.
  255. lea loader_panic_mess, %si
  256. call prtstr
  257. jmp no_sig_loop
  258. loader_panic_mess: .string "Wrong loader, giving up..."
  259. loader_ok:
  260. # Get memory size (extended mem, kB)
  261. xorl %eax, %eax
  262. movl %eax, (0x1e0)
  263. #ifndef STANDARD_MEMORY_BIOS_CALL
  264. movb %al, (E820NR)
  265. # Try three different memory detection schemes. First, try
  266. # e820h, which lets us assemble a memory map, then try e801h,
  267. # which returns a 32-bit memory size, and finally 88h, which
  268. # returns 0-64m
  269. # method E820H:
  270. # the memory map from hell. e820h returns memory classified into
  271. # a whole bunch of different types, and allows memory holes and
  272. # everything. We scan through this memory map and build a list
  273. # of the first 32 memory areas, which we return at [E820MAP].
  274. # This is documented at http://www.acpi.info/, in the ACPI 2.0 specification.
  275. #define SMAP 0x534d4150
  276. meme820:
  277. xorl %ebx, %ebx # continuation counter
  278. movw $E820MAP, %di # point into the whitelist
  279. # so we can have the bios
  280. # directly write into it.
  281. jmpe820:
  282. movl $0x0000e820, %eax # e820, upper word zeroed
  283. movl $SMAP, %edx # ascii 'SMAP'
  284. movl $20, %ecx # size of the e820rec
  285. pushw %ds # data record.
  286. popw %es
  287. int $0x15 # make the call
  288. jc bail820 # fall to e801 if it fails
  289. cmpl $SMAP, %eax # check the return is `SMAP'
  290. jne bail820 # fall to e801 if it fails
  291. # cmpl $1, 16(%di) # is this usable memory?
  292. # jne again820
  293. # If this is usable memory, we save it by simply advancing %di by
  294. # sizeof(e820rec).
  295. #
  296. good820:
  297. movb (E820NR), %al # up to 128 entries
  298. cmpb $E820MAX, %al
  299. jae bail820
  300. incb (E820NR)
  301. movw %di, %ax
  302. addw $20, %ax
  303. movw %ax, %di
  304. again820:
  305. cmpl $0, %ebx # check to see if
  306. jne jmpe820 # %ebx is set to EOF
  307. bail820:
  308. # method E801H:
  309. # memory size is in 1k chunksizes, to avoid confusing loadlin.
  310. # we store the 0xe801 memory size in a completely different place,
  311. # because it will most likely be longer than 16 bits.
  312. # (use 1e0 because that's what Larry Augustine uses in his
  313. # alternative new memory detection scheme, and it's sensible
  314. # to write everything into the same place.)
  315. meme801:
  316. stc # fix to work around buggy
  317. xorw %cx,%cx # BIOSes which don't clear/set
  318. xorw %dx,%dx # carry on pass/error of
  319. # e801h memory size call
  320. # or merely pass cx,dx though
  321. # without changing them.
  322. movw $0xe801, %ax
  323. int $0x15
  324. jc mem88
  325. cmpw $0x0, %cx # Kludge to handle BIOSes
  326. jne e801usecxdx # which report their extended
  327. cmpw $0x0, %dx # memory in AX/BX rather than
  328. jne e801usecxdx # CX/DX. The spec I have read
  329. movw %ax, %cx # seems to indicate AX/BX
  330. movw %bx, %dx # are more reasonable anyway...
  331. e801usecxdx:
  332. andl $0xffff, %edx # clear sign extend
  333. shll $6, %edx # and go from 64k to 1k chunks
  334. movl %edx, (0x1e0) # store extended memory size
  335. andl $0xffff, %ecx # clear sign extend
  336. addl %ecx, (0x1e0) # and add lower memory into
  337. # total size.
  338. # Ye Olde Traditional Methode. Returns the memory size (up to 16mb or
  339. # 64mb, depending on the bios) in ax.
  340. mem88:
  341. #endif
  342. movb $0x88, %ah
  343. int $0x15
  344. movw %ax, (2)
  345. # Set the keyboard repeat rate to the max
  346. movw $0x0305, %ax
  347. xorw %bx, %bx
  348. int $0x16
  349. # Check for video adapter and its parameters and allow the
  350. # user to browse video modes.
  351. call video # NOTE: we need %ds pointing
  352. # to bootsector
  353. # Get hd0 data...
  354. xorw %ax, %ax
  355. movw %ax, %ds
  356. ldsw (4 * 0x41), %si
  357. movw %cs, %ax # aka SETUPSEG
  358. subw $DELTA_INITSEG, %ax # aka INITSEG
  359. pushw %ax
  360. movw %ax, %es
  361. movw $0x0080, %di
  362. movw $0x10, %cx
  363. pushw %cx
  364. cld
  365. rep
  366. movsb
  367. # Get hd1 data...
  368. xorw %ax, %ax
  369. movw %ax, %ds
  370. ldsw (4 * 0x46), %si
  371. popw %cx
  372. popw %es
  373. movw $0x0090, %di
  374. rep
  375. movsb
  376. # Check that there IS a hd1 :-)
  377. movw $0x01500, %ax
  378. movb $0x81, %dl
  379. int $0x13
  380. jc no_disk1
  381. cmpb $3, %ah
  382. je is_disk1
  383. no_disk1:
  384. movw %cs, %ax # aka SETUPSEG
  385. subw $DELTA_INITSEG, %ax # aka INITSEG
  386. movw %ax, %es
  387. movw $0x0090, %di
  388. movw $0x10, %cx
  389. xorw %ax, %ax
  390. cld
  391. rep
  392. stosb
  393. is_disk1:
  394. # check for Micro Channel (MCA) bus
  395. movw %cs, %ax # aka SETUPSEG
  396. subw $DELTA_INITSEG, %ax # aka INITSEG
  397. movw %ax, %ds
  398. xorw %ax, %ax
  399. movw %ax, (0xa0) # set table length to 0
  400. movb $0xc0, %ah
  401. stc
  402. int $0x15 # moves feature table to es:bx
  403. jc no_mca
  404. pushw %ds
  405. movw %es, %ax
  406. movw %ax, %ds
  407. movw %cs, %ax # aka SETUPSEG
  408. subw $DELTA_INITSEG, %ax # aka INITSEG
  409. movw %ax, %es
  410. movw %bx, %si
  411. movw $0xa0, %di
  412. movw (%si), %cx
  413. addw $2, %cx # table length is a short
  414. cmpw $0x10, %cx
  415. jc sysdesc_ok
  416. movw $0x10, %cx # we keep only first 16 bytes
  417. sysdesc_ok:
  418. rep
  419. movsb
  420. popw %ds
  421. no_mca:
  422. #ifdef CONFIG_X86_VOYAGER
  423. movb $0xff, 0x40 # flag on config found
  424. movb $0xc0, %al
  425. mov $0xff, %ah
  426. int $0x15 # put voyager config info at es:di
  427. jc no_voyager
  428. movw $0x40, %si # place voyager info in apm table
  429. cld
  430. movw $7, %cx
  431. voyager_rep:
  432. movb %es:(%di), %al
  433. movb %al,(%si)
  434. incw %di
  435. incw %si
  436. decw %cx
  437. jnz voyager_rep
  438. no_voyager:
  439. #endif
  440. # Check for PS/2 pointing device
  441. movw %cs, %ax # aka SETUPSEG
  442. subw $DELTA_INITSEG, %ax # aka INITSEG
  443. movw %ax, %ds
  444. movb $0, (0x1ff) # default is no pointing device
  445. int $0x11 # int 0x11: equipment list
  446. testb $0x04, %al # check if mouse installed
  447. jz no_psmouse
  448. movb $0xAA, (0x1ff) # device present
  449. no_psmouse:
  450. #if defined(CONFIG_X86_SPEEDSTEP_SMI) || defined(CONFIG_X86_SPEEDSTEP_SMI_MODULE)
  451. movl $0x0000E980, %eax # IST Support
  452. movl $0x47534943, %edx # Request value
  453. int $0x15
  454. movl %eax, (96)
  455. movl %ebx, (100)
  456. movl %ecx, (104)
  457. movl %edx, (108)
  458. #endif
  459. #if defined(CONFIG_APM) || defined(CONFIG_APM_MODULE)
  460. # Then check for an APM BIOS...
  461. # %ds points to the bootsector
  462. movw $0, 0x40 # version = 0 means no APM BIOS
  463. movw $0x05300, %ax # APM BIOS installation check
  464. xorw %bx, %bx
  465. int $0x15
  466. jc done_apm_bios # Nope, no APM BIOS
  467. cmpw $0x0504d, %bx # Check for "PM" signature
  468. jne done_apm_bios # No signature, no APM BIOS
  469. andw $0x02, %cx # Is 32 bit supported?
  470. je done_apm_bios # No 32-bit, no (good) APM BIOS
  471. movw $0x05304, %ax # Disconnect first just in case
  472. xorw %bx, %bx
  473. int $0x15 # ignore return code
  474. movw $0x05303, %ax # 32 bit connect
  475. xorl %ebx, %ebx
  476. xorw %cx, %cx # paranoia :-)
  477. xorw %dx, %dx # ...
  478. xorl %esi, %esi # ...
  479. xorw %di, %di # ...
  480. int $0x15
  481. jc no_32_apm_bios # Ack, error.
  482. movw %ax, (66) # BIOS code segment
  483. movl %ebx, (68) # BIOS entry point offset
  484. movw %cx, (72) # BIOS 16 bit code segment
  485. movw %dx, (74) # BIOS data segment
  486. movl %esi, (78) # BIOS code segment lengths
  487. movw %di, (82) # BIOS data segment length
  488. # Redo the installation check as the 32 bit connect
  489. # modifies the flags returned on some BIOSs
  490. movw $0x05300, %ax # APM BIOS installation check
  491. xorw %bx, %bx
  492. xorw %cx, %cx # paranoia
  493. int $0x15
  494. jc apm_disconnect # error -> shouldn't happen
  495. cmpw $0x0504d, %bx # check for "PM" signature
  496. jne apm_disconnect # no sig -> shouldn't happen
  497. movw %ax, (64) # record the APM BIOS version
  498. movw %cx, (76) # and flags
  499. jmp done_apm_bios
  500. apm_disconnect: # Tidy up
  501. movw $0x05304, %ax # Disconnect
  502. xorw %bx, %bx
  503. int $0x15 # ignore return code
  504. jmp done_apm_bios
  505. no_32_apm_bios:
  506. andw $0xfffd, (76) # remove 32 bit support bit
  507. done_apm_bios:
  508. #endif
  509. #include "edd.S"
  510. # Now we want to move to protected mode ...
  511. cmpw $0, %cs:realmode_swtch
  512. jz rmodeswtch_normal
  513. lcall *%cs:realmode_swtch
  514. jmp rmodeswtch_end
  515. rmodeswtch_normal:
  516. pushw %cs
  517. call default_switch
  518. rmodeswtch_end:
  519. # Now we move the system to its rightful place ... but we check if we have a
  520. # big-kernel. In that case we *must* not move it ...
  521. testb $LOADED_HIGH, %cs:loadflags
  522. jz do_move0 # .. then we have a normal low
  523. # loaded zImage
  524. # .. or else we have a high
  525. # loaded bzImage
  526. jmp end_move # ... and we skip moving
  527. do_move0:
  528. movw $0x100, %ax # start of destination segment
  529. movw %cs, %bp # aka SETUPSEG
  530. subw $DELTA_INITSEG, %bp # aka INITSEG
  531. movw %cs:start_sys_seg, %bx # start of source segment
  532. cld
  533. do_move:
  534. movw %ax, %es # destination segment
  535. incb %ah # instead of add ax,#0x100
  536. movw %bx, %ds # source segment
  537. addw $0x100, %bx
  538. subw %di, %di
  539. subw %si, %si
  540. movw $0x800, %cx
  541. rep
  542. movsw
  543. cmpw %bp, %bx # assume start_sys_seg > 0x200,
  544. # so we will perhaps read one
  545. # page more than needed, but
  546. # never overwrite INITSEG
  547. # because destination is a
  548. # minimum one page below source
  549. jb do_move
  550. end_move:
  551. # then we load the segment descriptors
  552. movw %cs, %ax # aka SETUPSEG
  553. movw %ax, %ds
  554. # Check whether we need to be downward compatible with version <=201
  555. cmpl $0, cmd_line_ptr
  556. jne end_move_self # loader uses version >=202 features
  557. cmpb $0x20, type_of_loader
  558. je end_move_self # bootsect loader, we know of it
  559. # Boot loader doesnt support boot protocol version 2.02.
  560. # If we have our code not at 0x90000, we need to move it there now.
  561. # We also then need to move the params behind it (commandline)
  562. # Because we would overwrite the code on the current IP, we move
  563. # it in two steps, jumping high after the first one.
  564. movw %cs, %ax
  565. cmpw $SETUPSEG, %ax
  566. je end_move_self
  567. cli # make sure we really have
  568. # interrupts disabled !
  569. # because after this the stack
  570. # should not be used
  571. subw $DELTA_INITSEG, %ax # aka INITSEG
  572. movw %ss, %dx
  573. cmpw %ax, %dx
  574. jb move_self_1
  575. addw $INITSEG, %dx
  576. subw %ax, %dx # this will go into %ss after
  577. # the move
  578. move_self_1:
  579. movw %ax, %ds
  580. movw $INITSEG, %ax # real INITSEG
  581. movw %ax, %es
  582. movw %cs:setup_move_size, %cx
  583. std # we have to move up, so we use
  584. # direction down because the
  585. # areas may overlap
  586. movw %cx, %di
  587. decw %di
  588. movw %di, %si
  589. subw $move_self_here+0x200, %cx
  590. rep
  591. movsb
  592. ljmp $SETUPSEG, $move_self_here
  593. move_self_here:
  594. movw $move_self_here+0x200, %cx
  595. rep
  596. movsb
  597. movw $SETUPSEG, %ax
  598. movw %ax, %ds
  599. movw %dx, %ss
  600. end_move_self: # now we are at the right place
  601. #
  602. # Enable A20. This is at the very best an annoying procedure.
  603. # A20 code ported from SYSLINUX 1.52-1.63 by H. Peter Anvin.
  604. # AMD Elan bug fix by Robert Schwebel.
  605. #
  606. #if defined(CONFIG_X86_ELAN)
  607. movb $0x02, %al # alternate A20 gate
  608. outb %al, $0x92 # this works on SC410/SC520
  609. a20_elan_wait:
  610. call a20_test
  611. jz a20_elan_wait
  612. jmp a20_done
  613. #endif
  614. A20_TEST_LOOPS = 32 # Iterations per wait
  615. A20_ENABLE_LOOPS = 255 # Total loops to try
  616. #ifndef CONFIG_X86_VOYAGER
  617. a20_try_loop:
  618. # First, see if we are on a system with no A20 gate.
  619. a20_none:
  620. call a20_test
  621. jnz a20_done
  622. # Next, try the BIOS (INT 0x15, AX=0x2401)
  623. a20_bios:
  624. movw $0x2401, %ax
  625. pushfl # Be paranoid about flags
  626. int $0x15
  627. popfl
  628. call a20_test
  629. jnz a20_done
  630. # Try enabling A20 through the keyboard controller
  631. #endif /* CONFIG_X86_VOYAGER */
  632. a20_kbc:
  633. call empty_8042
  634. #ifndef CONFIG_X86_VOYAGER
  635. call a20_test # Just in case the BIOS worked
  636. jnz a20_done # but had a delayed reaction.
  637. #endif
  638. movb $0xD1, %al # command write
  639. outb %al, $0x64
  640. call empty_8042
  641. movb $0xDF, %al # A20 on
  642. outb %al, $0x60
  643. call empty_8042
  644. #ifndef CONFIG_X86_VOYAGER
  645. # Wait until a20 really *is* enabled; it can take a fair amount of
  646. # time on certain systems; Toshiba Tecras are known to have this
  647. # problem.
  648. a20_kbc_wait:
  649. xorw %cx, %cx
  650. a20_kbc_wait_loop:
  651. call a20_test
  652. jnz a20_done
  653. loop a20_kbc_wait_loop
  654. # Final attempt: use "configuration port A"
  655. a20_fast:
  656. inb $0x92, %al # Configuration Port A
  657. orb $0x02, %al # "fast A20" version
  658. andb $0xFE, %al # don't accidentally reset
  659. outb %al, $0x92
  660. # Wait for configuration port A to take effect
  661. a20_fast_wait:
  662. xorw %cx, %cx
  663. a20_fast_wait_loop:
  664. call a20_test
  665. jnz a20_done
  666. loop a20_fast_wait_loop
  667. # A20 is still not responding. Try frobbing it again.
  668. #
  669. decb (a20_tries)
  670. jnz a20_try_loop
  671. movw $a20_err_msg, %si
  672. call prtstr
  673. a20_die:
  674. hlt
  675. jmp a20_die
  676. a20_tries:
  677. .byte A20_ENABLE_LOOPS
  678. a20_err_msg:
  679. .ascii "linux: fatal error: A20 gate not responding!"
  680. .byte 13, 10, 0
  681. # If we get here, all is good
  682. a20_done:
  683. #endif /* CONFIG_X86_VOYAGER */
  684. # set up gdt and idt and 32bit start address
  685. lidt idt_48 # load idt with 0,0
  686. xorl %eax, %eax # Compute gdt_base
  687. movw %ds, %ax # (Convert %ds:gdt to a linear ptr)
  688. shll $4, %eax
  689. addl %eax, code32
  690. addl $gdt, %eax
  691. movl %eax, (gdt_48+2)
  692. lgdt gdt_48 # load gdt with whatever is
  693. # appropriate
  694. # make sure any possible coprocessor is properly reset..
  695. xorw %ax, %ax
  696. outb %al, $0xf0
  697. call delay
  698. outb %al, $0xf1
  699. call delay
  700. # well, that went ok, I hope. Now we mask all interrupts - the rest
  701. # is done in init_IRQ().
  702. movb $0xFF, %al # mask all interrupts for now
  703. outb %al, $0xA1
  704. call delay
  705. movb $0xFB, %al # mask all irq's but irq2 which
  706. outb %al, $0x21 # is cascaded
  707. # Well, that certainly wasn't fun :-(. Hopefully it works, and we don't
  708. # need no steenking BIOS anyway (except for the initial loading :-).
  709. # The BIOS-routine wants lots of unnecessary data, and it's less
  710. # "interesting" anyway. This is how REAL programmers do it.
  711. #
  712. # Well, now's the time to actually move into protected mode. To make
  713. # things as simple as possible, we do no register set-up or anything,
  714. # we let the gnu-compiled 32-bit programs do that. We just jump to
  715. # absolute address 0x1000 (or the loader supplied one),
  716. # in 32-bit protected mode.
  717. #
  718. # Note that the short jump isn't strictly needed, although there are
  719. # reasons why it might be a good idea. It won't hurt in any case.
  720. movw $1, %ax # protected mode (PE) bit
  721. lmsw %ax # This is it!
  722. jmp flush_instr
  723. flush_instr:
  724. xorw %bx, %bx # Flag to indicate a boot
  725. xorl %esi, %esi # Pointer to real-mode code
  726. movw %cs, %si
  727. subw $DELTA_INITSEG, %si
  728. shll $4, %esi # Convert to 32-bit pointer
  729. # jump to startup_32 in arch/i386/boot/compressed/head.S
  730. #
  731. # NOTE: For high loaded big kernels we need a
  732. # jmpi 0x100000,__BOOT_CS
  733. #
  734. # but we yet haven't reloaded the CS register, so the default size
  735. # of the target offset still is 16 bit.
  736. # However, using an operand prefix (0x66), the CPU will properly
  737. # take our 48 bit far pointer. (INTeL 80386 Programmer's Reference
  738. # Manual, Mixing 16-bit and 32-bit code, page 16-6)
  739. .byte 0x66, 0xea # prefix + jmpi-opcode
  740. code32: .long startup_32 # will be set to %cs+startup_32
  741. .word __BOOT_CS
  742. .code32
  743. startup_32:
  744. movl $(__BOOT_DS), %eax
  745. movl %eax, %ds
  746. movl %eax, %es
  747. movl %eax, %fs
  748. movl %eax, %gs
  749. movl %eax, %ss
  750. xorl %eax, %eax
  751. 1: incl %eax # check that A20 really IS enabled
  752. movl %eax, 0x00000000 # loop forever if it isn't
  753. cmpl %eax, 0x00100000
  754. je 1b
  755. # Jump to the 32bit entry point
  756. jmpl *(code32_start - start + (DELTA_INITSEG << 4))(%esi)
  757. .code16
  758. # Here's a bunch of information about your current kernel..
  759. kernel_version: .ascii UTS_RELEASE
  760. .ascii " ("
  761. .ascii LINUX_COMPILE_BY
  762. .ascii "@"
  763. .ascii LINUX_COMPILE_HOST
  764. .ascii ") "
  765. .ascii UTS_VERSION
  766. .byte 0
  767. # This is the default real mode switch routine.
  768. # to be called just before protected mode transition
  769. default_switch:
  770. cli # no interrupts allowed !
  771. movb $0x80, %al # disable NMI for bootup
  772. # sequence
  773. outb %al, $0x70
  774. lret
  775. #ifndef CONFIG_X86_VOYAGER
  776. # This routine tests whether or not A20 is enabled. If so, it
  777. # exits with zf = 0.
  778. #
  779. # The memory address used, 0x200, is the int $0x80 vector, which
  780. # should be safe.
  781. A20_TEST_ADDR = 4*0x80
  782. a20_test:
  783. pushw %cx
  784. pushw %ax
  785. xorw %cx, %cx
  786. movw %cx, %fs # Low memory
  787. decw %cx
  788. movw %cx, %gs # High memory area
  789. movw $A20_TEST_LOOPS, %cx
  790. movw %fs:(A20_TEST_ADDR), %ax
  791. pushw %ax
  792. a20_test_wait:
  793. incw %ax
  794. movw %ax, %fs:(A20_TEST_ADDR)
  795. call delay # Serialize and make delay constant
  796. cmpw %gs:(A20_TEST_ADDR+0x10), %ax
  797. loope a20_test_wait
  798. popw %fs:(A20_TEST_ADDR)
  799. popw %ax
  800. popw %cx
  801. ret
  802. #endif /* CONFIG_X86_VOYAGER */
  803. # This routine checks that the keyboard command queue is empty
  804. # (after emptying the output buffers)
  805. #
  806. # Some machines have delusions that the keyboard buffer is always full
  807. # with no keyboard attached...
  808. #
  809. # If there is no keyboard controller, we will usually get 0xff
  810. # to all the reads. With each IO taking a microsecond and
  811. # a timeout of 100,000 iterations, this can take about half a
  812. # second ("delay" == outb to port 0x80). That should be ok,
  813. # and should also be plenty of time for a real keyboard controller
  814. # to empty.
  815. #
  816. empty_8042:
  817. pushl %ecx
  818. movl $100000, %ecx
  819. empty_8042_loop:
  820. decl %ecx
  821. jz empty_8042_end_loop
  822. call delay
  823. inb $0x64, %al # 8042 status port
  824. testb $1, %al # output buffer?
  825. jz no_output
  826. call delay
  827. inb $0x60, %al # read it
  828. jmp empty_8042_loop
  829. no_output:
  830. testb $2, %al # is input buffer full?
  831. jnz empty_8042_loop # yes - loop
  832. empty_8042_end_loop:
  833. popl %ecx
  834. ret
  835. # Read the cmos clock. Return the seconds in al
  836. gettime:
  837. pushw %cx
  838. movb $0x02, %ah
  839. int $0x1a
  840. movb %dh, %al # %dh contains the seconds
  841. andb $0x0f, %al
  842. movb %dh, %ah
  843. movb $0x04, %cl
  844. shrb %cl, %ah
  845. aad
  846. popw %cx
  847. ret
  848. # Delay is needed after doing I/O
  849. delay:
  850. outb %al,$0x80
  851. ret
  852. # Descriptor tables
  853. #
  854. # NOTE: The intel manual says gdt should be sixteen bytes aligned for
  855. # efficiency reasons. However, there are machines which are known not
  856. # to boot with misaligned GDTs, so alter this at your peril! If you alter
  857. # GDT_ENTRY_BOOT_CS (in asm/segment.h) remember to leave at least two
  858. # empty GDT entries (one for NULL and one reserved).
  859. #
  860. # NOTE: On some CPUs, the GDT must be 8 byte aligned. This is
  861. # true for the Voyager Quad CPU card which will not boot without
  862. # This directive. 16 byte aligment is recommended by intel.
  863. #
  864. .align 16
  865. gdt:
  866. .fill GDT_ENTRY_BOOT_CS,8,0
  867. .word 0xFFFF # 4Gb - (0x100000*0x1000 = 4Gb)
  868. .word 0 # base address = 0
  869. .word 0x9A00 # code read/exec
  870. .word 0x00CF # granularity = 4096, 386
  871. # (+5th nibble of limit)
  872. .word 0xFFFF # 4Gb - (0x100000*0x1000 = 4Gb)
  873. .word 0 # base address = 0
  874. .word 0x9200 # data read/write
  875. .word 0x00CF # granularity = 4096, 386
  876. # (+5th nibble of limit)
  877. gdt_end:
  878. .align 4
  879. .word 0 # alignment byte
  880. idt_48:
  881. .word 0 # idt limit = 0
  882. .word 0, 0 # idt base = 0L
  883. .word 0 # alignment byte
  884. gdt_48:
  885. .word gdt_end - gdt - 1 # gdt limit
  886. .word 0, 0 # gdt base (filled in later)
  887. # Include video setup & detection code
  888. #include "video.S"
  889. # Setup signature -- must be last
  890. setup_sig1: .word SIG1
  891. setup_sig2: .word SIG2
  892. # After this point, there is some free space which is used by the video mode
  893. # handling code to store the temporary mode table (not used by the kernel).
  894. modelist:
  895. .text
  896. endtext:
  897. .data
  898. enddata:
  899. .bss
  900. endbss: