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

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