選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

romprefix.S 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /* At entry, the processor is in 16 bit real mode and the code is being
  2. * executed from an address it was not linked to. Code must be pic and
  3. * 32 bit sensitive until things are fixed up.
  4. *
  5. * Also be very careful as the stack is at the rear end of the interrupt
  6. * table so using a noticeable amount of stack space is a no-no.
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER )
  9. #include <config/general.h>
  10. #define PNP_SIGNATURE ( '$' + ( 'P' << 8 ) + ( 'n' << 16 ) + ( 'P' << 24 ) )
  11. #define PMM_SIGNATURE ( '$' + ( 'P' << 8 ) + ( 'M' << 16 ) + ( 'M' << 24 ) )
  12. #define PCI_SIGNATURE ( 'P' + ( 'C' << 8 ) + ( 'I' << 16 ) + ( ' ' << 24 ) )
  13. #define STACK_MAGIC ( 'L' + ( 'R' << 8 ) + ( 'E' << 16 ) + ( 'T' << 24 ) )
  14. #define PNP_GET_BBS_VERSION 0x60
  15. #define PMM_ALLOCATE 0x0000
  16. #define PMM_DEALLOCATE 0x0002
  17. /* ROM banner timeout. Based on the configurable BANNER_TIMEOUT in
  18. * config.h, but converted to a number of (18Hz) timer ticks, and
  19. * doubled to allow for BIOSes that switch video modes immediately
  20. * beforehand, so rendering the message almost invisible to the user.
  21. */
  22. #define ROM_BANNER_TIMEOUT ( 2 * ( 18 * BANNER_TIMEOUT ) / 10 )
  23. .text
  24. .code16
  25. .arch i386
  26. .section ".prefix", "ax", @progbits
  27. .org 0x00
  28. romheader:
  29. .word 0xAA55 /* BIOS extension signature */
  30. romheader_size: .byte 0 /* Size in 512-byte blocks */
  31. jmp init /* Initialisation vector */
  32. checksum:
  33. .byte 0
  34. .org 0x16
  35. .word undiheader
  36. .org 0x18
  37. .word pciheader
  38. .org 0x1a
  39. .word pnpheader
  40. .size romheader, . - romheader
  41. .section ".zinfo.fixup", "a", @progbits /* Compressor fixups */
  42. .ascii "ADDB"
  43. .long romheader_size
  44. .long 512
  45. .long 0
  46. .previous
  47. pciheader:
  48. .ascii "PCIR" /* Signature */
  49. .word pci_vendor_id /* Vendor identification */
  50. .word pci_device_id /* Device identification */
  51. .word 0x0000 /* Device list pointer */
  52. .word pciheader_len /* PCI data structure length */
  53. .byte 0x03 /* PCI data structure revision */
  54. .byte 0x02, 0x00, 0x00 /* Class code */
  55. pciheader_image_length:
  56. .word 0 /* Image length */
  57. .word 0x0001 /* Revision level */
  58. .byte 0x00 /* Code type */
  59. .byte 0x80 /* Last image indicator */
  60. pciheader_runtime_length:
  61. .word 0 /* Maximum run-time image length */
  62. .word 0x0000 /* Configuration utility code header */
  63. .word 0x0000 /* DMTF CLP entry point */
  64. .equ pciheader_len, . - pciheader
  65. .size pciheader, . - pciheader
  66. .section ".zinfo.fixup", "a", @progbits /* Compressor fixups */
  67. .ascii "ADDW"
  68. .long pciheader_image_length
  69. .long 512
  70. .long 0
  71. .ascii "ADDW"
  72. .long pciheader_runtime_length
  73. .long 512
  74. .long 0
  75. .previous
  76. pnpheader:
  77. .ascii "$PnP" /* Signature */
  78. .byte 0x01 /* Structure revision */
  79. .byte ( pnpheader_len / 16 ) /* Length (in 16 byte increments) */
  80. .word 0x0000 /* Offset of next header */
  81. .byte 0x00 /* Reserved */
  82. .byte 0x00 /* Checksum */
  83. .long 0x00000000 /* Device identifier */
  84. .word mfgstr /* Manufacturer string */
  85. .word prodstr /* Product name */
  86. .byte 0x02 /* Device base type code */
  87. .byte 0x00 /* Device sub-type code */
  88. .byte 0x00 /* Device interface type code */
  89. .byte 0xf4 /* Device indicator */
  90. .word 0x0000 /* Boot connection vector */
  91. .word 0x0000 /* Disconnect vector */
  92. .word bev_entry /* Boot execution vector */
  93. .word 0x0000 /* Reserved */
  94. .word 0x0000 /* Static resource information vector*/
  95. .equ pnpheader_len, . - pnpheader
  96. .size pnpheader, . - pnpheader
  97. /* Manufacturer string */
  98. mfgstr:
  99. .asciz "http://etherboot.org"
  100. .size mfgstr, . - mfgstr
  101. /* Product string
  102. *
  103. * Defaults to PRODUCT_SHORT_NAME. If the ROM image is writable at
  104. * initialisation time, it will be filled in to include the PCI
  105. * bus:dev.fn number of the card as well.
  106. */
  107. prodstr:
  108. .ascii PRODUCT_SHORT_NAME
  109. prodstr_separator:
  110. .byte 0
  111. .ascii "(PCI "
  112. prodstr_pci_id:
  113. .asciz "xx:xx.x)" /* Filled in by init code */
  114. .size prodstr, . - prodstr
  115. .globl undiheader
  116. .weak undiloader
  117. undiheader:
  118. .ascii "UNDI" /* Signature */
  119. .byte undiheader_len /* Length of structure */
  120. .byte 0 /* Checksum */
  121. .byte 0 /* Structure revision */
  122. .byte 0,1,2 /* PXE version: 2.1.0 */
  123. .word undiloader /* Offset to loader routine */
  124. .word _data16_memsz /* Stack segment size */
  125. .word _data16_memsz /* Data segment size */
  126. .word _text16_memsz /* Code segment size */
  127. .ascii "PCIR" /* Bus type */
  128. .equ undiheader_len, . - undiheader
  129. .size undiheader, . - undiheader
  130. /* Initialisation (called once during POST)
  131. *
  132. * Determine whether or not this is a PnP system via a signature
  133. * check. If it is PnP, return to the PnP BIOS indicating that we are
  134. * a boot-capable device; the BIOS will call our boot execution vector
  135. * if it wants to boot us. If it is not PnP, hook INT 19.
  136. */
  137. init:
  138. /* Preserve registers, clear direction flag, set %ds=%cs */
  139. pushaw
  140. pushw %ds
  141. pushw %es
  142. pushw %fs
  143. pushw %gs
  144. cld
  145. pushw %cs
  146. popw %ds
  147. /* Shuffle some registers around. We need %di available for
  148. * the print_xxx functions, and in a register that's
  149. * addressable from %es, so shuffle as follows:
  150. *
  151. * %di (pointer to PnP structure) => %bx
  152. * %bx (runtime segment address, for PCI 3.0) => %gs
  153. */
  154. movw %bx, %gs
  155. movw %di, %bx
  156. /* Print message as early as possible */
  157. movw $init_message, %si
  158. xorw %di, %di
  159. call print_message
  160. call print_pci_busdevfn
  161. /* Fill in product name string, if possible */
  162. movw $prodstr_pci_id, %di
  163. call print_pci_busdevfn
  164. movb $( ' ' ), prodstr_separator
  165. /* Print segment address */
  166. movb $( ' ' ), %al
  167. xorw %di, %di
  168. call print_character
  169. movw %cs, %ax
  170. call print_hex_word
  171. /* Check for PCI BIOS version */
  172. pushl %ebx
  173. pushl %edx
  174. pushl %edi
  175. stc
  176. movw $0xb101, %ax
  177. int $0x1a
  178. jc no_pci3
  179. cmpl $PCI_SIGNATURE, %edx
  180. jne no_pci3
  181. testb %ah, %ah
  182. jnz no_pci3
  183. movw $init_message_pci, %si
  184. xorw %di, %di
  185. call print_message
  186. movb %bh, %al
  187. call print_hex_nibble
  188. movb $( '.' ), %al
  189. call print_character
  190. movb %bl, %al
  191. call print_hex_byte
  192. cmpb $3, %bh
  193. jb no_pci3
  194. /* PCI >=3.0: leave %gs as-is if sane */
  195. movw %gs, %ax
  196. cmpw $0xa000, %ax /* Insane if %gs < 0xa000 */
  197. jb pci3_insane
  198. movw %cs, %bx /* Sane if %cs == %gs */
  199. cmpw %bx, %ax
  200. je 1f
  201. movzbw romheader_size, %cx /* Sane if %cs+len <= %gs */
  202. shlw $5, %cx
  203. addw %cx, %bx
  204. cmpw %bx, %ax
  205. jae 1f
  206. movw %cs, %bx /* Sane if %gs+len <= %cs */
  207. addw %cx, %ax
  208. cmpw %bx, %ax
  209. jbe 1f
  210. pci3_insane: /* PCI 3.0 with insane %gs value: print error and ignore %gs */
  211. movb $( '!' ), %al
  212. call print_character
  213. movw %gs, %ax
  214. call print_hex_word
  215. no_pci3:
  216. /* PCI <3.0: set %gs (runtime segment) = %cs (init-time segment) */
  217. pushw %cs
  218. popw %gs
  219. 1: popl %edi
  220. popl %edx
  221. popl %ebx
  222. /* Check for PnP BIOS. Although %es:di should point to the
  223. * PnP BIOS signature on entry, some BIOSes fail to do this.
  224. */
  225. movw $( 0xf000 - 1 ), %bx
  226. pnp_scan:
  227. incw %bx
  228. jz no_pnp
  229. movw %bx, %es
  230. cmpl $PNP_SIGNATURE, %es:0
  231. jne pnp_scan
  232. xorw %dx, %dx
  233. xorw %si, %si
  234. movzbw %es:5, %cx
  235. 1: es lodsb
  236. addb %al, %dl
  237. loop 1b
  238. jnz pnp_scan
  239. /* Is PnP: print PnP message */
  240. movw $init_message_pnp, %si
  241. xorw %di, %di
  242. call print_message
  243. /* Check for BBS */
  244. pushw %es:0x1b /* Real-mode data segment */
  245. pushw %ds /* &(bbs_version) */
  246. pushw $bbs_version
  247. pushw $PNP_GET_BBS_VERSION
  248. lcall *%es:0xd
  249. addw $8, %sp
  250. testw %ax, %ax
  251. je got_bbs
  252. no_pnp: /* Not PnP-compliant - therefore cannot be BBS-compliant */
  253. no_bbs: /* Not BBS-compliant - must hook INT 19 */
  254. movw $init_message_int19, %si
  255. xorw %di, %di
  256. call print_message
  257. xorw %ax, %ax
  258. movw %ax, %es
  259. pushl %es:( 0x19 * 4 )
  260. popl orig_int19
  261. pushw %gs /* %gs contains runtime %cs */
  262. pushw $int19_entry
  263. popl %es:( 0x19 * 4 )
  264. jmp bbs_done
  265. got_bbs: /* BBS compliant - no need to hook INT 19 */
  266. movw $init_message_bbs, %si
  267. xorw %di, %di
  268. call print_message
  269. bbs_done:
  270. /* Check for PMM */
  271. movw $( 0xe000 - 1 ), %bx
  272. pmm_scan:
  273. incw %bx
  274. jz no_pmm
  275. movw %bx, %es
  276. cmpl $PMM_SIGNATURE, %es:0
  277. jne pmm_scan
  278. xorw %dx, %dx
  279. xorw %si, %si
  280. movzbw %es:5, %cx
  281. 1: es lodsb
  282. addb %al, %dl
  283. loop 1b
  284. jnz pmm_scan
  285. /* PMM found: print PMM message */
  286. movw $init_message_pmm, %si
  287. xorw %di, %di
  288. call print_message
  289. /* We have PMM and so a 1kB stack: preserve upper register halves */
  290. pushal
  291. /* Calculate required allocation size in %esi */
  292. movzbl romheader_size, %eax
  293. shll $9, %eax
  294. addl $_textdata_memsz, %eax
  295. orw $0xffff, %ax /* Ensure allocation size is at least 64kB */
  296. bsrl %eax, %ecx
  297. subw $15, %cx /* Round up and convert to 64kB count */
  298. movw $1, %si
  299. shlw %cl, %si
  300. pmm_loop:
  301. /* Try to allocate block via PMM */
  302. pushw $0x0006 /* Aligned, extended memory */
  303. pushl $0xffffffff /* No handle */
  304. movzwl %si, %eax
  305. shll $12, %eax
  306. pushl %eax /* Allocation size in paragraphs */
  307. pushw $PMM_ALLOCATE
  308. lcall *%es:7
  309. addw $12, %sp
  310. /* Abort if allocation fails */
  311. testw %dx, %dx /* %ax==0 even on success, since align>=64kB */
  312. jz pmm_fail
  313. /* If block has A20==1, free block and try again with twice
  314. * the allocation size (and hence alignment).
  315. */
  316. testw $0x0010, %dx
  317. jz got_pmm
  318. pushw %dx
  319. pushw $0
  320. pushw $PMM_DEALLOCATE
  321. lcall *%es:7
  322. addw $6, %sp
  323. addw %si, %si
  324. jmp pmm_loop
  325. got_pmm: /* PMM allocation succeeded */
  326. movw %dx, ( image_source + 2 )
  327. movw %dx, %ax
  328. xorw %di, %di
  329. call print_hex_word
  330. movb $( '@' ), %al
  331. call print_character
  332. movw %si, %ax
  333. call print_hex_byte
  334. pmm_copy:
  335. /* Copy ROM to PMM block */
  336. xorw %ax, %ax
  337. movw %ax, %es
  338. movl image_source, %edi
  339. xorl %esi, %esi
  340. movzbl romheader_size, %ecx
  341. shll $9, %ecx
  342. addr32 rep movsb /* PMM presence implies flat real mode */
  343. movl %edi, decompress_to
  344. /* Shrink ROM */
  345. movb $_prefix_memsz_sect, romheader_size
  346. #ifdef SHRINK_WITHOUT_PMM
  347. jmp pmm_done
  348. pmm_fail:
  349. /* Print marker and copy ourselves to high memory */
  350. movl $HIGHMEM_LOADPOINT, image_source
  351. xorw %di, %di
  352. movb $( '!' ), %al
  353. call print_character
  354. jmp pmm_copy
  355. pmm_done:
  356. #else
  357. pmm_fail:
  358. #endif
  359. /* Restore upper register halves */
  360. popal
  361. no_pmm:
  362. /* Update checksum */
  363. xorw %bx, %bx
  364. xorw %si, %si
  365. movzbw romheader_size, %cx
  366. shlw $9, %cx
  367. 1: lodsb
  368. addb %al, %bl
  369. loop 1b
  370. subb %bl, checksum
  371. /* Copy self to option ROM space. Required for PCI3.0, which
  372. * loads us to a temporary location in low memory. Will be a
  373. * no-op for lower PCI versions.
  374. */
  375. movb $( ' ' ), %al
  376. xorw %di, %di
  377. call print_character
  378. movw %gs, %ax
  379. call print_hex_word
  380. movzbw romheader_size, %cx
  381. shlw $9, %cx
  382. movw %ax, %es
  383. xorw %si, %si
  384. xorw %di, %di
  385. cs rep movsb
  386. /* Prompt for POST-time shell */
  387. movw $init_message_prompt, %si
  388. xorw %di, %di
  389. call print_message
  390. movw $prodstr, %si
  391. call print_message
  392. movw $init_message_dots, %si
  393. call print_message
  394. /* Wait for Ctrl-B */
  395. movw $0xff02, %bx
  396. call wait_for_key
  397. /* Clear prompt */
  398. pushf
  399. xorw %di, %di
  400. call print_kill_line
  401. movw $init_message_done, %si
  402. call print_message
  403. popf
  404. jnz 2f
  405. /* Ctrl-B was pressed: invoke gPXE. The keypress will be
  406. * picked up by the initial shell prompt, and we will drop
  407. * into a shell.
  408. */
  409. pushw %cs
  410. call exec
  411. 2:
  412. /* Restore registers */
  413. popw %gs
  414. popw %fs
  415. popw %es
  416. popw %ds
  417. popaw
  418. /* Indicate boot capability to PnP BIOS, if present */
  419. movw $0x20, %ax
  420. lret
  421. .size init, . - init
  422. /*
  423. * Note to hardware vendors:
  424. *
  425. * If you wish to brand this boot ROM, please do so by defining the
  426. * strings PRODUCT_NAME and PRODUCT_SHORT_NAME in config/general.h.
  427. *
  428. * While nothing in the GPL prevents you from removing all references
  429. * to gPXE or http://etherboot.org, we prefer you not to do so.
  430. *
  431. * If you have an OEM-mandated branding requirement that cannot be
  432. * satisfied simply by defining PRODUCT_NAME and PRODUCT_SHORT_NAME,
  433. * please contact us.
  434. *
  435. * [ Including an ASCII NUL in PRODUCT_NAME is considered to be
  436. * bypassing the spirit of this request! ]
  437. */
  438. init_message:
  439. .ascii "\n"
  440. .ascii PRODUCT_NAME
  441. .ascii "\n"
  442. .asciz "gPXE (http://etherboot.org) - "
  443. .size init_message, . - init_message
  444. init_message_pci:
  445. .asciz " PCI"
  446. .size init_message_pci, . - init_message_pci
  447. init_message_pnp:
  448. .asciz " PnP"
  449. .size init_message_pnp, . - init_message_pnp
  450. init_message_bbs:
  451. .asciz " BBS"
  452. .size init_message_bbs, . - init_message_bbs
  453. init_message_pmm:
  454. .asciz " PMM"
  455. .size init_message_pmm, . - init_message_pmm
  456. init_message_int19:
  457. .asciz " INT19"
  458. .size init_message_int19, . - init_message_int19
  459. init_message_prompt:
  460. .asciz "\nPress Ctrl-B to configure "
  461. .size init_message_prompt, . - init_message_prompt
  462. init_message_dots:
  463. .asciz "..."
  464. .size init_message_dots, . - init_message_dots
  465. init_message_done:
  466. .asciz "\n\n"
  467. .size init_message_done, . - init_message_done
  468. /* ROM image location
  469. *
  470. * May be either within option ROM space, or within PMM-allocated block.
  471. */
  472. .globl image_source
  473. image_source:
  474. .long 0
  475. .size image_source, . - image_source
  476. /* Temporary decompression area
  477. *
  478. * May be either at HIGHMEM_LOADPOINT, or within PMM-allocated block.
  479. */
  480. .globl decompress_to
  481. decompress_to:
  482. .long HIGHMEM_LOADPOINT
  483. .size decompress_to, . - decompress_to
  484. /* BBS version
  485. *
  486. * Filled in by BBS BIOS. We ignore the value.
  487. */
  488. bbs_version:
  489. .word 0
  490. .size bbs_version, . - bbs_version
  491. /* Boot Execution Vector entry point
  492. *
  493. * Called by the PnP BIOS when it wants to boot us.
  494. */
  495. bev_entry:
  496. pushw %cs
  497. call exec
  498. lret
  499. .size bev_entry, . - bev_entry
  500. /* INT19 entry point
  501. *
  502. * Called via the hooked INT 19 if we detected a non-PnP BIOS. We
  503. * attempt to return via the original INT 19 vector (if we were able
  504. * to store it).
  505. */
  506. int19_entry:
  507. pushw %cs
  508. popw %ds
  509. /* Prompt user to press B to boot */
  510. movw $int19_message_prompt, %si
  511. xorw %di, %di
  512. call print_message
  513. movw $prodstr, %si
  514. call print_message
  515. movw $int19_message_dots, %si
  516. call print_message
  517. movw $0xdf4e, %bx
  518. call wait_for_key
  519. pushf
  520. xorw %di, %di
  521. call print_kill_line
  522. movw $int19_message_done, %si
  523. call print_message
  524. popf
  525. jz 1f
  526. /* Leave keypress in buffer and start gPXE. The keypress will
  527. * cause the usual initial Ctrl-B prompt to be skipped.
  528. */
  529. pushw %cs
  530. call exec
  531. 1: /* Try to call original INT 19 vector */
  532. movl %cs:orig_int19, %eax
  533. testl %eax, %eax
  534. je 2f
  535. ljmp *%cs:orig_int19
  536. 2: /* No chained vector: issue INT 18 as a last resort */
  537. int $0x18
  538. .size int19_entry, . - int19_entry
  539. orig_int19:
  540. .long 0
  541. .size orig_int19, . - orig_int19
  542. int19_message_prompt:
  543. .asciz "Press N to skip booting from "
  544. .size int19_message_prompt, . - int19_message_prompt
  545. int19_message_dots:
  546. .asciz "..."
  547. .size int19_message_dots, . - int19_message_dots
  548. int19_message_done:
  549. .asciz "\n\n"
  550. .size int19_message_done, . - int19_message_done
  551. /* Execute as a boot device
  552. *
  553. */
  554. exec: /* Set %ds = %cs */
  555. pushw %cs
  556. popw %ds
  557. /* Print message as soon as possible */
  558. movw $prodstr, %si
  559. xorw %di, %di
  560. call print_message
  561. movw $exec_message, %si
  562. call print_message
  563. /* Store magic word on BIOS stack and remember BIOS %ss:sp */
  564. pushl $STACK_MAGIC
  565. movw %ss, %dx
  566. movw %sp, %bp
  567. /* Obtain a reasonably-sized temporary stack */
  568. xorw %ax, %ax
  569. movw %ax, %ss
  570. movw $0x7c00, %sp
  571. /* Install gPXE */
  572. movl image_source, %esi
  573. movl decompress_to, %edi
  574. call alloc_basemem
  575. call install_prealloc
  576. /* Set up real-mode stack */
  577. movw %bx, %ss
  578. movw $_estack16, %sp
  579. /* Jump to .text16 segment */
  580. pushw %ax
  581. pushw $1f
  582. lret
  583. .section ".text16", "awx", @progbits
  584. 1: /* Call main() */
  585. pushl $main
  586. pushw %cs
  587. call prot_call
  588. popl %ecx /* discard */
  589. /* Uninstall gPXE */
  590. call uninstall
  591. /* Restore BIOS stack */
  592. movw %dx, %ss
  593. movw %bp, %sp
  594. /* Check magic word on BIOS stack */
  595. popl %eax
  596. cmpl $STACK_MAGIC, %eax
  597. jne 1f
  598. /* BIOS stack OK: return to caller */
  599. lret
  600. 1: /* BIOS stack corrupt: use INT 18 */
  601. int $0x18
  602. .previous
  603. exec_message:
  604. .asciz " starting execution\n"
  605. .size exec_message, . - exec_message
  606. /* Wait for key press specified by %bl (masked by %bh)
  607. *
  608. * Used by init and INT19 code when prompting user. If the specified
  609. * key is pressed, it is left in the keyboard buffer.
  610. *
  611. * Returns with ZF set iff specified key is pressed.
  612. */
  613. wait_for_key:
  614. /* Preserve registers */
  615. pushw %cx
  616. pushw %ax
  617. 1: /* Empty the keyboard buffer before waiting for input */
  618. movb $0x01, %ah
  619. int $0x16
  620. jz 2f
  621. xorw %ax, %ax
  622. int $0x16
  623. jmp 1b
  624. 2: /* Wait for a key press */
  625. movw $ROM_BANNER_TIMEOUT, %cx
  626. 3: decw %cx
  627. js 99f /* Exit with ZF clear */
  628. /* Wait for timer tick to be updated */
  629. call wait_for_tick
  630. /* Check to see if a key was pressed */
  631. movb $0x01, %ah
  632. int $0x16
  633. jz 3b
  634. /* Check to see if key was the specified key */
  635. andb %bh, %al
  636. cmpb %al, %bl
  637. je 99f /* Exit with ZF set */
  638. /* Not the specified key: remove from buffer and stop waiting */
  639. pushfw
  640. xorw %ax, %ax
  641. int $0x16
  642. popfw /* Exit with ZF clear */
  643. 99: /* Restore registers and return */
  644. popw %ax
  645. popw %cx
  646. ret
  647. .size wait_for_key, . - wait_for_key
  648. /* Wait for timer tick
  649. *
  650. * Used by wait_for_key
  651. */
  652. wait_for_tick:
  653. pushl %eax
  654. pushw %fs
  655. movw $0x40, %ax
  656. movw %ax, %fs
  657. movl %fs:(0x6c), %eax
  658. 1: pushf
  659. sti
  660. hlt
  661. popf
  662. cmpl %fs:(0x6c), %eax
  663. je 1b
  664. popw %fs
  665. popl %eax
  666. ret
  667. .size wait_for_tick, . - wait_for_tick