Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

romprefix.S 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. stc
  165. movw $0xb101, %ax
  166. int $0x1a
  167. jc 1f
  168. cmpl $PCI_SIGNATURE, %edx
  169. jne 1f
  170. testb %ah, %ah
  171. jnz 1f
  172. movw $init_message_pci, %si
  173. xorw %di, %di
  174. call print_message
  175. movb %bh, %al
  176. call print_hex_nibble
  177. movb $'.', %al
  178. call print_character
  179. movb %bl, %al
  180. call print_hex_byte
  181. cmpb $3, %bh
  182. jae 2f
  183. 1: /* PCI <3.0: set %gs (runtime segment) = %cs (init-time segment) */
  184. pushw %cs
  185. popw %gs
  186. 2: popl %edx
  187. popl %ebx
  188. /* Check for PnP BIOS */
  189. testw $0x0f, %bx /* PnP signature must be aligned - bochs */
  190. jnz no_bbs /* uses unalignment to indicate 'fake' PnP. */
  191. cmpl $PNP_SIGNATURE, %es:0(%bx)
  192. jne no_bbs
  193. /* Is PnP: print PnP message */
  194. movw $init_message_pnp, %si
  195. xorw %di, %di
  196. call print_message
  197. /* Check for BBS */
  198. pushw %es:0x1b(%bx) /* Real-mode data segment */
  199. pushw %ds /* &(bbs_version) */
  200. pushw $bbs_version
  201. pushw $PNP_GET_BBS_VERSION
  202. lcall *%es:0xd(%bx)
  203. addw $8, %sp
  204. testw %ax, %ax
  205. je got_bbs
  206. no_bbs: /* Not BBS-compliant - must hook INT 19 */
  207. movw $init_message_int19, %si
  208. xorw %di, %di
  209. call print_message
  210. xorw %ax, %ax
  211. movw %ax, %es
  212. pushl %es:( 0x19 * 4 )
  213. popl orig_int19
  214. pushw %gs /* %gs contains runtime %cs */
  215. pushw $int19_entry
  216. popl %es:( 0x19 * 4 )
  217. jmp bbs_done
  218. got_bbs: /* BBS compliant - no need to hook INT 19 */
  219. movw $init_message_bbs, %si
  220. xorw %di, %di
  221. call print_message
  222. bbs_done:
  223. /* Check for PMM */
  224. movw $( 0xe000 - 1 ), %bx
  225. pmm_scan:
  226. incw %bx
  227. jz no_pmm
  228. movw %bx, %es
  229. cmpl $PMM_SIGNATURE, %es:0
  230. jne pmm_scan
  231. xorw %dx, %dx
  232. xorw %si, %si
  233. movzbw %es:5, %cx
  234. 1: es lodsb
  235. addb %al, %dl
  236. loop 1b
  237. jnz pmm_scan
  238. /* PMM found: print PMM message */
  239. movw $init_message_pmm, %si
  240. xorw %di, %di
  241. call print_message
  242. /* Try to allocate 2MB block via PMM */
  243. pushw $0x0006 /* Aligned, extended memory */
  244. pushl $0xffffffff /* No handle */
  245. pushl $( 0x00200000 / 16 ) /* 2MB in paragraphs */
  246. pushw $PMM_ALLOCATE
  247. lcall *%es:7
  248. addw $12, %sp
  249. movw %dx, %ax
  250. xorw %di, %di
  251. call print_hex_word
  252. movw %dx, ( image_source + 2 )
  253. testw %dx, %dx /* %ax==0 even on success, since align=2MB */
  254. jz no_pmm
  255. /* PMM allocation succeeded: copy ROM to PMM block */
  256. pushal /* PMM presence implies 1kB stack */
  257. xorw %ax, %ax
  258. movw %ax, %es
  259. movl image_source, %edi
  260. xorl %esi, %esi
  261. movzbl romheader_size, %ecx
  262. shll $9, %ecx
  263. addr32 rep movsb /* PMM presence implies flat real mode */
  264. movl %edi, decompress_to
  265. /* Shrink ROM and update checksum */
  266. xorw %bx, %bx
  267. xorw %si, %si
  268. movw $_prefix_size_sect, %cx
  269. movb %cl, romheader_size
  270. shlw $9, %cx
  271. 1: lodsb
  272. addb %al, %bl
  273. loop 1b
  274. subb %bl, checksum
  275. popal
  276. no_pmm:
  277. /* Copy self to option ROM space. Required for PCI3.0, which
  278. * loads us to a temporary location in low memory. Will be a
  279. * no-op for lower PCI versions.
  280. */
  281. movb $' ', %al
  282. xorw %di, %di
  283. call print_character
  284. movw %gs, %ax
  285. call print_hex_word
  286. movzbw romheader_size, %cx
  287. shlw $9, %cx
  288. movw %ax, %es
  289. xorw %si, %si
  290. xorw %di, %di
  291. cs rep movsb
  292. /* Prompt for POST-time shell */
  293. movw $init_message_prompt, %si
  294. xorw %di, %di
  295. call print_message
  296. /* Empty the keyboard buffer before waiting for input */
  297. empty_keyboard_buffer:
  298. movb $0x01, %ah
  299. int $0x16
  300. jz 1f
  301. xorw %ax, %ax
  302. int $0x16
  303. jmp empty_keyboard_buffer
  304. 1: /* Wait for up to 3s for a key press */
  305. movw $(18 * 3), %cx /* Approx 3s worth of timer ticks */
  306. wait_for_key:
  307. decw %cx
  308. jz no_key_pressed
  309. /* Wait for timer tick to be updated */
  310. movl %fs:(0x6c), %eax
  311. 1: pushf
  312. sti
  313. hlt
  314. popf
  315. cmpl %fs:(0x6c), %eax
  316. je 1b
  317. /* Check to see if a key was pressed */
  318. movb $0x01, %ah
  319. int $0x16
  320. jz wait_for_key
  321. /* Check to see if key was Ctrl-B */
  322. cmpb $0x02, %al
  323. je 1f
  324. /* Key was not Ctrl-B: remove from buffer and stop waiting */
  325. xorw %ax, %ax
  326. int $0x16
  327. jmp no_key_pressed
  328. 1: /* Key was Ctrl-B: leave in keyboard buffer and invoke gPXE.
  329. * The keypress will be picked up by the initial shell
  330. * prompt, and we will drop into a shell.
  331. */
  332. pushw %cs
  333. call exec
  334. no_key_pressed:
  335. /* Print blank lines to terminate messages */
  336. movw $init_message_end, %si
  337. xorw %di, %di
  338. call print_message
  339. /* Restore registers */
  340. popw %gs
  341. popw %fs
  342. popw %es
  343. popw %ds
  344. popaw
  345. /* Indicate boot capability to PnP BIOS, if present */
  346. movw $0x20, %ax
  347. lret
  348. .size init, . - init
  349. init_message:
  350. .asciz "gPXE (http://etherboot.org) - "
  351. .size init_message, . - init_message
  352. init_message_pci:
  353. .asciz " PCI"
  354. .size init_message_pci, . - init_message_pci
  355. init_message_pnp:
  356. .asciz " PnP"
  357. .size init_message_pnp, . - init_message_pnp
  358. init_message_bbs:
  359. .asciz " BBS"
  360. .size init_message_bbs, . - init_message_bbs
  361. init_message_pmm:
  362. .asciz " PMM"
  363. .size init_message_pmm, . - init_message_pmm
  364. init_message_int19:
  365. .asciz " INT19"
  366. .size init_message_int19, . - init_message_int19
  367. init_message_prompt:
  368. .asciz "\nPress Ctrl-B to configure gPXE..."
  369. .size init_message_prompt, . - init_message_prompt
  370. init_message_end:
  371. .asciz "\n\n\n"
  372. .size init_message_end, . - init_message_end
  373. /* ROM image location
  374. *
  375. * May be either within option ROM space, or within PMM-allocated block.
  376. */
  377. image_source:
  378. .long 0
  379. .size image_source, . - image_source
  380. /* Temporary decompression area
  381. *
  382. * May be either at HIGHMEM_LOADPOINT, or within PMM-allocated block.
  383. */
  384. decompress_to:
  385. .long HIGHMEM_LOADPOINT
  386. .size decompress_to, . - decompress_to
  387. /* BBS version
  388. *
  389. * Filled in by BBS BIOS. We ignore the value.
  390. */
  391. bbs_version:
  392. .word 0
  393. .size bbs_version, . - bbs_version
  394. /* Boot Execution Vector entry point
  395. *
  396. * Called by the PnP BIOS when it wants to boot us.
  397. */
  398. bev_entry:
  399. pushw %cs
  400. call exec
  401. lret
  402. .size bev_entry, . - bev_entry
  403. /* INT19 entry point
  404. *
  405. * Called via the hooked INT 19 if we detected a non-PnP BIOS. We
  406. * attempt to return via the original INT 19 vector (if we were able to
  407. * store it).
  408. */
  409. int19_entry:
  410. pushw %cs
  411. call exec
  412. movl %cs:orig_int19, %eax
  413. testl %eax, %eax
  414. je 1f
  415. /* Chain to original INT 19 vector */
  416. ljmp *%cs:orig_int19
  417. 1: /* No chained vector: issue INT 18 as a last resort */
  418. int $0x18
  419. .size int19_entry, . - int19_entry
  420. orig_int19:
  421. .long 0
  422. .size orig_int19, . - orig_int19
  423. /* Execute as a boot device
  424. *
  425. */
  426. exec: /* Set %ds = %cs */
  427. pushw %cs
  428. popw %ds
  429. /* Print message as soon as possible */
  430. movw $prodstr, %si
  431. xorw %di, %di
  432. call print_message
  433. movw $exec_message, %si
  434. call print_message
  435. /* Store magic word on BIOS stack and remember BIOS %ss:sp */
  436. pushl $STACK_MAGIC
  437. movw %ss, %dx
  438. movw %sp, %bp
  439. /* Obtain a reasonably-sized temporary stack */
  440. xorw %ax, %ax
  441. movw %ax, %ss
  442. movw $0x7c00, %sp
  443. /* Install gPXE */
  444. movl image_source, %esi
  445. movl decompress_to, %edi
  446. call alloc_basemem
  447. call install_prealloc
  448. /* Set up real-mode stack */
  449. movw %bx, %ss
  450. movw $_estack16, %sp
  451. /* Jump to .text16 segment */
  452. pushw %ax
  453. pushw $1f
  454. lret
  455. .section ".text16", "awx", @progbits
  456. 1: /* Call main() */
  457. pushl $main
  458. pushw %cs
  459. call prot_call
  460. /* No need to clean up stack; we are about to reload %ss:sp */
  461. /* Restore BIOS stack */
  462. movw %dx, %ss
  463. movw %bp, %sp
  464. /* Check magic word on BIOS stack */
  465. popl %eax
  466. cmpl $STACK_MAGIC, %eax
  467. jne 1f
  468. /* BIOS stack OK: return to caller */
  469. lret
  470. 1: /* BIOS stack corrupt: use INT 18 */
  471. int $0x18
  472. .previous
  473. exec_message:
  474. .asciz " starting execution\n"
  475. .size exec_message, . - exec_message
  476. /* UNDI loader
  477. *
  478. * Called by an external program to load our PXE stack.
  479. */
  480. undiloader:
  481. /* Save registers */
  482. pushl %esi
  483. pushl %edi
  484. pushw %es
  485. pushw %bx
  486. /* UNDI loader parameter structure address into %es:%di */
  487. movw %sp, %bx
  488. movw %ss:16(%bx), %di
  489. movw %ss:18(%bx), %es
  490. /* Install to specified real-mode addresses */
  491. pushw %di
  492. movw %es:12(%di), %bx
  493. movw %es:14(%di), %ax
  494. movl %cs:image_source, %esi
  495. movl %cs:decompress_to, %edi
  496. call install_prealloc
  497. popw %di
  498. /* Call UNDI loader C code */
  499. pushl $pxe_loader_call
  500. pushw %cs
  501. pushw $1f
  502. pushw %ax
  503. pushw $prot_call
  504. lret
  505. 1: popw %bx /* discard */
  506. popw %bx /* discard */
  507. /* Restore registers and return */
  508. popw %bx
  509. popw %es
  510. popl %edi
  511. popl %esi
  512. lret
  513. .size undiloader, . - undiloader