You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

romprefix.S 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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 PMM_ALLOCATE 0x0000
  15. #define PMM_FIND 0x0001
  16. #define PMM_HANDLE_BASE ( ( ( 'F' - 'A' + 1 ) << 26 ) + \
  17. ( ( 'E' - 'A' + 1 ) << 21 ) + \
  18. ( ( 'N' - 'A' + 1 ) << 16 ) )
  19. #define PMM_HANDLE_BASE_IMAGE_SOURCE \
  20. ( PMM_HANDLE_BASE | 0x00001000 )
  21. #define PMM_HANDLE_BASE_DECOMPRESS_TO \
  22. ( PMM_HANDLE_BASE | 0x00002000 )
  23. #define PCI_FUNC_MASK 0x07
  24. /* ROM banner timeout. Based on the configurable BANNER_TIMEOUT in
  25. * config.h, but converted to a number of (18Hz) timer ticks, and
  26. * doubled to allow for BIOSes that switch video modes immediately
  27. * beforehand, so rendering the message almost invisible to the user.
  28. */
  29. #define ROM_BANNER_TIMEOUT ( 2 * ( 18 * BANNER_TIMEOUT ) / 10 )
  30. /* Allow payload to be excluded from ROM size
  31. */
  32. #if ROMPREFIX_EXCLUDE_PAYLOAD
  33. #define ZINFO_TYPE_ADxB "ADHB"
  34. #define ZINFO_TYPE_ADxW "ADHW"
  35. #else
  36. #define ZINFO_TYPE_ADxB "ADDB"
  37. #define ZINFO_TYPE_ADxW "ADDW"
  38. #endif
  39. /* Allow ROM to be marked as containing multiple images
  40. */
  41. #if ROMPREFIX_MORE_IMAGES
  42. #define INDICATOR 0x00
  43. #else
  44. #define INDICATOR 0x80
  45. #endif
  46. .text
  47. .code16
  48. .arch i386
  49. .section ".prefix", "ax", @progbits
  50. .globl _rom_start
  51. _rom_start:
  52. .org 0x00
  53. romheader:
  54. .word 0xAA55 /* BIOS extension signature */
  55. romheader_size: .byte 0 /* Size in 512-byte blocks */
  56. jmp init /* Initialisation vector */
  57. checksum:
  58. .byte 0
  59. .org 0x10
  60. .word ipxeheader
  61. .org 0x16
  62. .word undiheader
  63. .org 0x18
  64. .word pciheader
  65. .org 0x1a
  66. .word pnpheader
  67. .size romheader, . - romheader
  68. .section ".zinfo.fixup", "a", @progbits /* Compressor fixups */
  69. .ascii ZINFO_TYPE_ADxB
  70. .long romheader_size
  71. .long 512
  72. .long 0
  73. .previous
  74. pciheader:
  75. .ascii "PCIR" /* Signature */
  76. .word pci_vendor_id /* Vendor identification */
  77. .word pci_device_id /* Device identification */
  78. .word 0x0000 /* Device list pointer */
  79. .word pciheader_len /* PCI data structure length */
  80. .byte 0x03 /* PCI data structure revision */
  81. .byte 0x02, 0x00, 0x00 /* Class code */
  82. pciheader_image_length:
  83. .word 0 /* Image length */
  84. .word 0x0001 /* Revision level */
  85. .byte 0x00 /* Code type */
  86. .byte INDICATOR /* Last image indicator */
  87. pciheader_runtime_length:
  88. .word 0 /* Maximum run-time image length */
  89. .word 0x0000 /* Configuration utility code header */
  90. .word 0x0000 /* DMTF CLP entry point */
  91. .equ pciheader_len, . - pciheader
  92. .size pciheader, . - pciheader
  93. .section ".zinfo.fixup", "a", @progbits /* Compressor fixups */
  94. .ascii ZINFO_TYPE_ADxW
  95. .long pciheader_image_length
  96. .long 512
  97. .long 0
  98. .ascii ZINFO_TYPE_ADxW
  99. .long pciheader_runtime_length
  100. .long 512
  101. .long 0
  102. .previous
  103. /* PnP doesn't require any particular alignment, but IBM
  104. * BIOSes will scan on 16-byte boundaries rather than using
  105. * the offset stored at 0x1a
  106. */
  107. .align 16
  108. pnpheader:
  109. .ascii "$PnP" /* Signature */
  110. .byte 0x01 /* Structure revision */
  111. .byte ( pnpheader_len / 16 ) /* Length (in 16 byte increments) */
  112. .word 0x0000 /* Offset of next header */
  113. .byte 0x00 /* Reserved */
  114. .byte 0x00 /* Checksum */
  115. .long 0x00000000 /* Device identifier */
  116. .word mfgstr /* Manufacturer string */
  117. .word prodstr /* Product name */
  118. .byte 0x02 /* Device base type code */
  119. .byte 0x00 /* Device sub-type code */
  120. .byte 0x00 /* Device interface type code */
  121. .byte 0xf4 /* Device indicator */
  122. .word 0x0000 /* Boot connection vector */
  123. .word 0x0000 /* Disconnect vector */
  124. .word bev_entry /* Boot execution vector */
  125. .word 0x0000 /* Reserved */
  126. .word 0x0000 /* Static resource information vector*/
  127. .equ pnpheader_len, . - pnpheader
  128. .size pnpheader, . - pnpheader
  129. /* Manufacturer string */
  130. mfgstr:
  131. .asciz "http://ipxe.org"
  132. .size mfgstr, . - mfgstr
  133. /* Product string
  134. *
  135. * Defaults to PRODUCT_SHORT_NAME. If the ROM image is writable at
  136. * initialisation time, it will be filled in to include the PCI
  137. * bus:dev.fn number of the card as well.
  138. */
  139. prodstr:
  140. .ascii PRODUCT_SHORT_NAME
  141. prodstr_separator:
  142. .byte 0
  143. .ascii "(PCI "
  144. prodstr_pci_id:
  145. .asciz "xx:xx.x)" /* Filled in by init code */
  146. .size prodstr, . - prodstr
  147. .globl undiheader
  148. .weak undiloader
  149. undiheader:
  150. .ascii "UNDI" /* Signature */
  151. .byte undiheader_len /* Length of structure */
  152. .byte 0 /* Checksum */
  153. .byte 0 /* Structure revision */
  154. .byte 0,1,2 /* PXE version: 2.1.0 */
  155. .word undiloader /* Offset to loader routine */
  156. .word _data16_memsz /* Stack segment size */
  157. .word _data16_memsz /* Data segment size */
  158. .word _text16_memsz /* Code segment size */
  159. .ascii "PCIR" /* Bus type */
  160. .equ undiheader_len, . - undiheader
  161. .size undiheader, . - undiheader
  162. ipxeheader:
  163. .ascii "iPXE" /* Signature */
  164. .byte ipxeheader_len /* Length of structure */
  165. .byte 0 /* Checksum */
  166. shrunk_rom_size:
  167. .byte 0 /* Shrunk size (in 512-byte blocks) */
  168. .byte 0 /* Reserved */
  169. build_id:
  170. .long _build_id /* Randomly-generated build ID */
  171. .equ ipxeheader_len, . - ipxeheader
  172. .size ipxeheader, . - ipxeheader
  173. .section ".zinfo.fixup", "a", @progbits /* Compressor fixups */
  174. .ascii "ADHB"
  175. .long shrunk_rom_size
  176. .long 512
  177. .long 0
  178. .previous
  179. /* Initialisation (called once during POST)
  180. *
  181. * Determine whether or not this is a PnP system via a signature
  182. * check. If it is PnP, return to the PnP BIOS indicating that we are
  183. * a boot-capable device; the BIOS will call our boot execution vector
  184. * if it wants to boot us. If it is not PnP, hook INT 19.
  185. */
  186. init:
  187. /* Preserve registers, clear direction flag, set %ds=%cs */
  188. pushaw
  189. pushw %ds
  190. pushw %es
  191. pushw %fs
  192. pushw %gs
  193. cld
  194. pushw %cs
  195. popw %ds
  196. /* Shuffle some registers around. We need %di available for
  197. * the print_xxx functions, and in a register that's
  198. * addressable from %es, so shuffle as follows:
  199. *
  200. * %di (pointer to PnP structure) => %bx
  201. * %bx (runtime segment address, for PCI 3.0) => %gs
  202. */
  203. movw %bx, %gs
  204. movw %di, %bx
  205. /* Store PCI bus:dev.fn address */
  206. movw %ax, init_pci_busdevfn
  207. /* Print message as early as possible */
  208. movw $init_message, %si
  209. xorw %di, %di
  210. call print_message
  211. call print_pci_busdevfn
  212. /* Fill in product name string, if possible */
  213. movw $prodstr_pci_id, %di
  214. call print_pci_busdevfn
  215. movb $( ' ' ), prodstr_separator
  216. /* Print segment address */
  217. movb $( ' ' ), %al
  218. xorw %di, %di
  219. call print_character
  220. movw %cs, %ax
  221. call print_hex_word
  222. /* Check for PCI BIOS version */
  223. pushl %ebx
  224. pushl %edx
  225. pushl %edi
  226. stc
  227. movw $0xb101, %ax
  228. int $0x1a
  229. jc no_pci3
  230. cmpl $PCI_SIGNATURE, %edx
  231. jne no_pci3
  232. testb %ah, %ah
  233. jnz no_pci3
  234. movw $init_message_pci, %si
  235. xorw %di, %di
  236. call print_message
  237. movb %bh, %al
  238. call print_hex_nibble
  239. movb $( '.' ), %al
  240. call print_character
  241. movb %bl, %al
  242. call print_hex_byte
  243. cmpb $3, %bh
  244. jb no_pci3
  245. /* PCI >=3.0: leave %gs as-is if sane */
  246. movw %gs, %ax
  247. cmpw $0xa000, %ax /* Insane if %gs < 0xa000 */
  248. jb pci3_insane
  249. movw %cs, %bx /* Sane if %cs == %gs */
  250. cmpw %bx, %ax
  251. je 1f
  252. movzbw romheader_size, %cx /* Sane if %cs+len <= %gs */
  253. shlw $5, %cx
  254. addw %cx, %bx
  255. cmpw %bx, %ax
  256. jae 1f
  257. movw %cs, %bx /* Sane if %gs+len <= %cs */
  258. addw %cx, %ax
  259. cmpw %bx, %ax
  260. jbe 1f
  261. pci3_insane: /* PCI 3.0 with insane %gs value: print error and ignore %gs */
  262. movb $( '!' ), %al
  263. call print_character
  264. movw %gs, %ax
  265. call print_hex_word
  266. no_pci3:
  267. /* PCI <3.0: set %gs (runtime segment) = %cs (init-time segment) */
  268. pushw %cs
  269. popw %gs
  270. 1: popl %edi
  271. popl %edx
  272. popl %ebx
  273. /* Check for PnP BIOS. Although %es:di should point to the
  274. * PnP BIOS signature on entry, some BIOSes fail to do this.
  275. */
  276. movw $( 0xf000 - 1 ), %bx
  277. pnp_scan:
  278. incw %bx
  279. jz no_pnp
  280. movw %bx, %es
  281. cmpl $PNP_SIGNATURE, %es:0
  282. jne pnp_scan
  283. xorw %dx, %dx
  284. xorw %si, %si
  285. movzbw %es:5, %cx
  286. 1: es lodsb
  287. addb %al, %dl
  288. loop 1b
  289. jnz pnp_scan
  290. /* Is PnP: print PnP message */
  291. movw $init_message_pnp, %si
  292. xorw %di, %di
  293. call print_message
  294. jmp pnp_done
  295. no_pnp: /* Not PnP-compliant - hook INT 19 */
  296. #ifdef NONPNP_HOOK_INT19
  297. movw $init_message_int19, %si
  298. xorw %di, %di
  299. call print_message
  300. xorw %ax, %ax
  301. movw %ax, %es
  302. pushl %es:( 0x19 * 4 )
  303. popl orig_int19
  304. pushw %gs /* %gs contains runtime %cs */
  305. pushw $int19_entry
  306. popl %es:( 0x19 * 4 )
  307. #endif /* NONPNP_HOOK_INT19 */
  308. pnp_done:
  309. /* Check for PMM */
  310. movw $( 0xe000 - 1 ), %bx
  311. pmm_scan:
  312. incw %bx
  313. jz no_pmm
  314. movw %bx, %es
  315. cmpl $PMM_SIGNATURE, %es:0
  316. jne pmm_scan
  317. xorw %dx, %dx
  318. xorw %si, %si
  319. movzbw %es:5, %cx
  320. 1: es lodsb
  321. addb %al, %dl
  322. loop 1b
  323. jnz pmm_scan
  324. /* PMM found: print PMM message */
  325. movw $init_message_pmm, %si
  326. xorw %di, %di
  327. call print_message
  328. /* We have PMM and so a 1kB stack: preserve whole registers */
  329. pushal
  330. /* Allocate image source PMM block. Round up the size to the
  331. * nearest 4kB (8 512-byte sectors) to work around AMI BIOS bugs.
  332. */
  333. movzbl romheader_size, %ecx
  334. addw extra_size, %cx
  335. addw $0x0007, %cx /* Round up to multiple of 8 512-byte sectors */
  336. andw $0xfff8, %cx
  337. shll $5, %ecx
  338. movl $PMM_HANDLE_BASE_IMAGE_SOURCE, %ebx
  339. movw $get_pmm_image_source, %bp
  340. call get_pmm
  341. movl %esi, image_source
  342. jz 1f
  343. /* Copy ROM to image source PMM block */
  344. pushw %es
  345. xorw %ax, %ax
  346. movw %ax, %es
  347. movl %esi, %edi
  348. xorl %esi, %esi
  349. movzbl romheader_size, %ecx
  350. shll $7, %ecx
  351. addr32 rep movsl /* PMM presence implies flat real mode */
  352. popw %es
  353. /* Shrink ROM */
  354. movb shrunk_rom_size, %al
  355. movb %al, romheader_size
  356. 1: /* Allocate decompression PMM block. Round up the size to the
  357. * nearest 128kB and use the size within the PMM handle; this
  358. * allows the same decompression area to be shared between
  359. * multiple iPXE ROMs even with differing build IDs
  360. */
  361. movl $_textdata_memsz_pgh, %ecx
  362. addl $0x00001fff, %ecx
  363. andl $0xffffe000, %ecx
  364. movl %ecx, %ebx
  365. shrw $12, %bx
  366. orl $PMM_HANDLE_BASE_DECOMPRESS_TO, %ebx
  367. movw $get_pmm_decompress_to, %bp
  368. call get_pmm
  369. movl %esi, decompress_to
  370. /* Restore registers */
  371. popal
  372. no_pmm:
  373. /* Update checksum */
  374. xorw %bx, %bx
  375. xorw %si, %si
  376. movzbw romheader_size, %cx
  377. shlw $9, %cx
  378. 1: lodsb
  379. addb %al, %bl
  380. loop 1b
  381. subb %bl, checksum
  382. /* Copy self to option ROM space. Required for PCI3.0, which
  383. * loads us to a temporary location in low memory. Will be a
  384. * no-op for lower PCI versions.
  385. */
  386. movb $( ' ' ), %al
  387. xorw %di, %di
  388. call print_character
  389. movw %gs, %ax
  390. call print_hex_word
  391. movzbw romheader_size, %cx
  392. shlw $9, %cx
  393. movw %ax, %es
  394. xorw %si, %si
  395. xorw %di, %di
  396. cs rep movsb
  397. /* Skip prompt if this is not the first PCI function */
  398. testb $PCI_FUNC_MASK, init_pci_busdevfn
  399. jnz no_shell
  400. /* Prompt for POST-time shell */
  401. movw $init_message_prompt, %si
  402. xorw %di, %di
  403. call print_message
  404. movw $prodstr, %si
  405. call print_message
  406. movw $init_message_dots, %si
  407. call print_message
  408. /* Wait for Ctrl-B */
  409. movw $0xff02, %bx
  410. call wait_for_key
  411. /* Clear prompt */
  412. pushf
  413. xorw %di, %di
  414. call print_kill_line
  415. movw $init_message_done, %si
  416. call print_message
  417. popf
  418. jnz no_shell
  419. /* Ctrl-B was pressed: invoke iPXE. The keypress will be
  420. * picked up by the initial shell prompt, and we will drop
  421. * into a shell.
  422. */
  423. xorl %ebp, %ebp /* Inhibit use of INT 15,e820 and INT 15,e801 */
  424. pushw %cs
  425. call exec
  426. no_shell:
  427. movb $( '\n' ), %al
  428. xorw %di, %di
  429. call print_character
  430. /* Restore registers */
  431. popw %gs
  432. popw %fs
  433. popw %es
  434. popw %ds
  435. popaw
  436. /* Indicate boot capability to PnP BIOS, if present */
  437. movw $0x20, %ax
  438. lret
  439. .size init, . - init
  440. /* Attempt to find or allocate PMM block
  441. *
  442. * Parameters:
  443. * %ecx : size of block to allocate, in paragraphs
  444. * %ebx : PMM handle base
  445. * %bp : routine to check acceptability of found blocks
  446. * %es:0000 : PMM structure
  447. * Returns:
  448. * %ebx : PMM handle
  449. * %esi : allocated block address, or zero (with ZF set) if allocation failed
  450. */
  451. get_pmm:
  452. /* Preserve registers */
  453. pushl %eax
  454. pushw %di
  455. movw $( ' ' ), %di
  456. get_pmm_find:
  457. /* Try to find existing block */
  458. pushl %ebx /* PMM handle */
  459. pushw $PMM_FIND
  460. lcall *%es:7
  461. addw $6, %sp
  462. pushw %dx
  463. pushw %ax
  464. popl %esi
  465. /* Treat 0xffffffff (not supported) as 0x00000000 (not found) */
  466. incl %esi
  467. jz get_pmm_allocate
  468. decl %esi
  469. jz get_pmm_allocate
  470. /* Block found - check acceptability */
  471. call *%bp
  472. jnc get_pmm_done
  473. /* Block not acceptable - increment handle and retry */
  474. incl %ebx
  475. jmp get_pmm_find
  476. get_pmm_allocate:
  477. /* Block not found - try to allocate new block */
  478. pushw $0x0002 /* Extended memory */
  479. pushl %ebx /* PMM handle */
  480. pushl %ecx /* Length */
  481. pushw $PMM_ALLOCATE
  482. lcall *%es:7
  483. addw $12, %sp
  484. pushw %dx
  485. pushw %ax
  486. popl %esi
  487. movw $( '+' ), %di /* Indicate allocation attempt */
  488. get_pmm_done:
  489. /* Print block address */
  490. movw %di, %ax
  491. xorw %di, %di
  492. call print_character
  493. movl %esi, %eax
  494. call print_hex_dword
  495. /* Treat 0xffffffff (not supported) as 0x00000000 (allocation
  496. * failed), and set ZF to indicate a zero result.
  497. */
  498. incl %esi
  499. jz 1f
  500. decl %esi
  501. 1: /* Restore registers and return */
  502. popw %di
  503. popl %eax
  504. ret
  505. .size get_pmm, . - get_pmm
  506. /* Check acceptability of image source block */
  507. get_pmm_image_source:
  508. pushw %es
  509. xorw %ax, %ax
  510. movw %ax, %es
  511. movl build_id, %eax
  512. addr32 cmpl %es:build_id(%esi), %eax
  513. je 1f
  514. stc
  515. 1: popw %es
  516. ret
  517. .size get_pmm_image_source, . - get_pmm_image_source
  518. /* Check acceptability of decompression block */
  519. get_pmm_decompress_to:
  520. clc
  521. ret
  522. .size get_pmm_decompress_to, . - get_pmm_decompress_to
  523. /*
  524. * Note to hardware vendors:
  525. *
  526. * If you wish to brand this boot ROM, please do so by defining the
  527. * strings PRODUCT_NAME and PRODUCT_SHORT_NAME in config/general.h.
  528. *
  529. * While nothing in the GPL prevents you from removing all references
  530. * to iPXE or http://ipxe.org, we prefer you not to do so.
  531. *
  532. * If you have an OEM-mandated branding requirement that cannot be
  533. * satisfied simply by defining PRODUCT_NAME and PRODUCT_SHORT_NAME,
  534. * please contact us.
  535. *
  536. * [ Including an ASCII NUL in PRODUCT_NAME is considered to be
  537. * bypassing the spirit of this request! ]
  538. */
  539. init_message:
  540. .ascii "\n"
  541. .ascii PRODUCT_NAME
  542. .ascii "\n"
  543. .asciz "iPXE (http://ipxe.org) "
  544. .size init_message, . - init_message
  545. init_message_pci:
  546. .asciz " PCI"
  547. .size init_message_pci, . - init_message_pci
  548. init_message_pnp:
  549. .asciz " PnP"
  550. .size init_message_pnp, . - init_message_pnp
  551. init_message_pmm:
  552. .asciz " PMM"
  553. .size init_message_pmm, . - init_message_pmm
  554. init_message_int19:
  555. .asciz " INT19"
  556. .size init_message_int19, . - init_message_int19
  557. init_message_prompt:
  558. .asciz "\nPress Ctrl-B to configure "
  559. .size init_message_prompt, . - init_message_prompt
  560. init_message_dots:
  561. .asciz "..."
  562. .size init_message_dots, . - init_message_dots
  563. init_message_done:
  564. .asciz "\n\n"
  565. .size init_message_done, . - init_message_done
  566. /* PCI bus:dev.fn
  567. *
  568. */
  569. init_pci_busdevfn:
  570. .word 0
  571. .size init_pci_busdevfn, . - init_pci_busdevfn
  572. /* Image source area
  573. *
  574. * May be either zero (indicating to use option ROM space as source),
  575. * or within a PMM-allocated block.
  576. */
  577. .globl image_source
  578. image_source:
  579. .long 0
  580. .size image_source, . - image_source
  581. /* Additional image source size (in 512-byte sectors)
  582. *
  583. */
  584. extra_size:
  585. .word 0
  586. .size extra_size, . - extra_size
  587. /* Temporary decompression area
  588. *
  589. * May be either zero (indicating to use default decompression area in
  590. * high memory), or within a PMM-allocated block.
  591. */
  592. .globl decompress_to
  593. decompress_to:
  594. .long 0
  595. .size decompress_to, . - decompress_to
  596. /* Boot Execution Vector entry point
  597. *
  598. * Called by the PnP BIOS when it wants to boot us.
  599. */
  600. bev_entry:
  601. orl $0xffffffff, %ebp /* Allow arbitrary relocation */
  602. pushw %cs
  603. call exec
  604. lret
  605. .size bev_entry, . - bev_entry
  606. /* INT19 entry point
  607. *
  608. * Called via the hooked INT 19 if we detected a non-PnP BIOS. We
  609. * attempt to return via the original INT 19 vector (if we were able
  610. * to store it).
  611. */
  612. int19_entry:
  613. pushw %cs
  614. popw %ds
  615. /* Prompt user to press B to boot */
  616. movw $int19_message_prompt, %si
  617. xorw %di, %di
  618. call print_message
  619. movw $prodstr, %si
  620. call print_message
  621. movw $int19_message_dots, %si
  622. call print_message
  623. movw $0xdf4e, %bx
  624. call wait_for_key
  625. pushf
  626. xorw %di, %di
  627. call print_kill_line
  628. movw $int19_message_done, %si
  629. call print_message
  630. popf
  631. jz 1f
  632. /* Leave keypress in buffer and start iPXE. The keypress will
  633. * cause the usual initial Ctrl-B prompt to be skipped.
  634. */
  635. orl $0xffffffff, %ebp /* Allow arbitrary relocation */
  636. pushw %cs
  637. call exec
  638. 1: /* Try to call original INT 19 vector */
  639. movl %cs:orig_int19, %eax
  640. testl %eax, %eax
  641. je 2f
  642. ljmp *%cs:orig_int19
  643. 2: /* No chained vector: issue INT 18 as a last resort */
  644. int $0x18
  645. .size int19_entry, . - int19_entry
  646. orig_int19:
  647. .long 0
  648. .size orig_int19, . - orig_int19
  649. int19_message_prompt:
  650. .asciz "Press N to skip booting from "
  651. .size int19_message_prompt, . - int19_message_prompt
  652. int19_message_dots:
  653. .asciz "..."
  654. .size int19_message_dots, . - int19_message_dots
  655. int19_message_done:
  656. .asciz "\n\n"
  657. .size int19_message_done, . - int19_message_done
  658. /* Execute as a boot device
  659. *
  660. */
  661. exec: /* Set %ds = %cs */
  662. pushw %cs
  663. popw %ds
  664. /* Print message as soon as possible */
  665. movw $prodstr, %si
  666. xorw %di, %di
  667. call print_message
  668. movw $exec_message_pre_install, %si
  669. call print_message
  670. /* Store magic word on BIOS stack and remember BIOS %ss:sp */
  671. pushl $STACK_MAGIC
  672. movw %ss, %cx
  673. movw %sp, %dx
  674. /* Obtain a reasonably-sized temporary stack */
  675. xorw %bx, %bx
  676. movw %bx, %ss
  677. movw $0x7c00, %sp
  678. /* Install iPXE */
  679. call alloc_basemem
  680. movl image_source, %esi
  681. movl decompress_to, %edi
  682. call install_prealloc
  683. /* Print message indicating successful installation */
  684. movw $exec_message_post_install, %si
  685. xorw %di, %di
  686. call print_message
  687. /* Set up real-mode stack */
  688. movw %bx, %ss
  689. movw $_estack16, %sp
  690. /* Jump to .text16 segment */
  691. pushw %ax
  692. pushw $1f
  693. lret
  694. .section ".text16", "awx", @progbits
  695. 1: /* Call main() */
  696. pushl $main
  697. pushw %cs
  698. call prot_call
  699. popl %eax /* discard */
  700. /* Uninstall iPXE */
  701. call uninstall
  702. /* Restore BIOS stack */
  703. movw %cx, %ss
  704. movw %dx, %sp
  705. /* Check magic word on BIOS stack */
  706. popl %eax
  707. cmpl $STACK_MAGIC, %eax
  708. jne 1f
  709. /* BIOS stack OK: return to caller */
  710. lret
  711. 1: /* BIOS stack corrupt: use INT 18 */
  712. int $0x18
  713. .previous
  714. exec_message_pre_install:
  715. .asciz " starting execution..."
  716. .size exec_message_pre_install, . - exec_message_pre_install
  717. exec_message_post_install:
  718. .asciz "ok\n"
  719. .size exec_message_post_install, . - exec_message_post_install
  720. /* Wait for key press specified by %bl (masked by %bh)
  721. *
  722. * Used by init and INT19 code when prompting user. If the specified
  723. * key is pressed, it is left in the keyboard buffer.
  724. *
  725. * Returns with ZF set iff specified key is pressed.
  726. */
  727. wait_for_key:
  728. /* Preserve registers */
  729. pushw %cx
  730. pushw %ax
  731. 1: /* Empty the keyboard buffer before waiting for input */
  732. movb $0x01, %ah
  733. int $0x16
  734. jz 2f
  735. xorw %ax, %ax
  736. int $0x16
  737. jmp 1b
  738. 2: /* Wait for a key press */
  739. movw $ROM_BANNER_TIMEOUT, %cx
  740. 3: decw %cx
  741. js 99f /* Exit with ZF clear */
  742. /* Wait for timer tick to be updated */
  743. call wait_for_tick
  744. /* Check to see if a key was pressed */
  745. movb $0x01, %ah
  746. int $0x16
  747. jz 3b
  748. /* Check to see if key was the specified key */
  749. andb %bh, %al
  750. cmpb %al, %bl
  751. je 99f /* Exit with ZF set */
  752. /* Not the specified key: remove from buffer and stop waiting */
  753. pushfw
  754. xorw %ax, %ax
  755. int $0x16
  756. popfw /* Exit with ZF clear */
  757. 99: /* Restore registers and return */
  758. popw %ax
  759. popw %cx
  760. ret
  761. .size wait_for_key, . - wait_for_key
  762. /* Wait for timer tick
  763. *
  764. * Used by wait_for_key
  765. */
  766. wait_for_tick:
  767. pushl %eax
  768. pushw %fs
  769. movw $0x40, %ax
  770. movw %ax, %fs
  771. movl %fs:(0x6c), %eax
  772. 1: pushf
  773. sti
  774. hlt
  775. popf
  776. cmpl %fs:(0x6c), %eax
  777. je 1b
  778. popw %fs
  779. popl %eax
  780. ret
  781. .size wait_for_tick, . - wait_for_tick