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 15KB

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