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.

pxeprefix.S 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. #define PXENV_UNDI_SHUTDOWN 0x0005
  2. #define PXENV_UNDI_GET_NIC_TYPE 0x0012
  3. #define PXENV_STOP_UNDI 0x0015
  4. #define PXENV_UNLOAD_STACK 0x0070
  5. .text
  6. .arch i386
  7. .org 0
  8. .section ".prefix", "ax", @progbits
  9. .section ".prefix.data", "aw", @progbits
  10. .code16
  11. #include <undi.h>
  12. /*****************************************************************************
  13. * Entry point: set operating context, print welcome message
  14. *****************************************************************************
  15. */
  16. .section ".prefix"
  17. /* Set up our non-stack segment registers */
  18. jmp $0x7c0, $1f
  19. 1: movw %cs, %ax
  20. movw %ax, %ds
  21. movw $0x40, %ax /* BIOS data segment access */
  22. movw %ax, %fs
  23. /* Record PXENV+ and !PXE nominal addresses */
  24. movw %es, %ax /* PXENV+ address */
  25. movw %ax, pxenv_segment
  26. movw %bx, pxenv_offset
  27. popl %eax /* Discard return address */
  28. popl ppxe_segoff /* !PXE address */
  29. /* Set up stack just below 0x7c00 */
  30. xorw %ax, %ax
  31. movw %ax, %ss
  32. movw $0x7c00, %sp
  33. /* Clear direction flag, for the sake of sanity */
  34. cld
  35. /* Print welcome message */
  36. movw $10f, %si
  37. xorw %di, %di
  38. call print_message
  39. .section ".prefix.data"
  40. 10: .asciz "PXE->EB:"
  41. .previous
  42. /*****************************************************************************
  43. * Verify PXENV+ structure and record parameters of interest
  44. *****************************************************************************
  45. */
  46. detect_pxenv:
  47. /* Signature check */
  48. les pxenv_segoff, %bx
  49. cmpl $0x4e455850, %es:(%bx) /* 'PXEN' signature */
  50. jne no_pxenv
  51. cmpw $0x2b56, %es:4(%bx) /* 'V+' signature */
  52. jne no_pxenv
  53. /* Record entry point and UNDI segments */
  54. pushl %es:0x0a(%bx) /* Entry point */
  55. popl entry_segoff
  56. pushw %es:0x24(%bx) /* UNDI code segment */
  57. pushw %es:0x26(%bx) /* UNDI code size */
  58. popl undi_code_segoff
  59. pushw %es:0x20(%bx) /* UNDI data segment */
  60. pushw %es:0x22(%bx) /* UNDI data size */
  61. popl undi_data_segoff
  62. /* Print "PXENV+ at <address>" */
  63. movw $10f, %si
  64. call print_message
  65. call print_segoff
  66. movb $',', %al
  67. call print_character
  68. jmp 99f
  69. .section ".prefix.data"
  70. 10: .asciz " PXENV+ at "
  71. .previous
  72. no_pxenv:
  73. xorl %eax, %eax
  74. movl %eax, pxenv_segoff
  75. 99:
  76. /*****************************************************************************
  77. * Verify !PXE structure and record parameters of interest
  78. *****************************************************************************
  79. */
  80. detect_ppxe:
  81. /* Signature check */
  82. les ppxe_segoff, %bx
  83. cmpl $0x45585021, %es:(%bx) /* '!PXE' signature */
  84. jne no_ppxe
  85. /* Record structure address, entry point, and UNDI segments */
  86. pushw %es
  87. popw ppxe_segment
  88. movw %bx, ppxe_offset
  89. pushl %es:0x10(%bx) /* Entry point */
  90. popl entry_segoff
  91. pushw %es:0x30(%bx) /* UNDI code segment */
  92. pushw %es:0x36(%bx) /* UNDI code size */
  93. popl undi_code_segoff
  94. pushw %es:0x28(%bx) /* UNDI data segment */
  95. pushw %es:0x2e(%bx) /* UNDI data size */
  96. popl undi_data_segoff
  97. /* Print "!PXE at <address>" */
  98. movw $10f, %si
  99. call print_message
  100. call print_segoff
  101. movb $',', %al
  102. call print_character
  103. jmp 99f
  104. .section ".prefix.data"
  105. 10: .asciz " !PXE at "
  106. .previous
  107. no_ppxe:
  108. xorl %eax, %eax
  109. movl %eax, ppxe_segoff
  110. 99:
  111. /*****************************************************************************
  112. * Sanity check: we must have an entry point
  113. *****************************************************************************
  114. */
  115. check_have_stack:
  116. /* Check for entry point */
  117. movl entry_segoff, %eax
  118. testl %eax, %eax
  119. jnz 99f
  120. /* No entry point: print message and skip everything else */
  121. movw $10f, %si
  122. call print_message
  123. jmp finished
  124. .section ".prefix.data"
  125. 10: .asciz " No PXE stack found!\n"
  126. .previous
  127. 99:
  128. /*****************************************************************************
  129. * Calculate base memory usage by UNDI
  130. *****************************************************************************
  131. */
  132. find_undi_basemem_usage:
  133. movw undi_code_segment, %ax
  134. movw undi_code_size, %bx
  135. movw undi_data_segment, %cx
  136. movw undi_data_size, %dx
  137. cmpw %ax, %cx
  138. ja 1f
  139. xchgw %ax, %cx
  140. xchgw %bx, %dx
  141. 1: /* %ax:%bx now describes the lower region, %cx:%dx the higher */
  142. shrw $6, %ax /* Round down to nearest kB */
  143. movw %ax, undi_fbms_start
  144. addw $0x0f, %dx /* Round up to next segment */
  145. shrw $4, %dx
  146. addw %dx, %cx
  147. addw $((1024 / 16) - 1), %cx /* Round up to next kB */
  148. shrw $6, %cx
  149. movw %cx, undi_fbms_end
  150. /*****************************************************************************
  151. * Print information about detected PXE stack
  152. *****************************************************************************
  153. */
  154. print_structure_information:
  155. /* Print entry point */
  156. movw $10f, %si
  157. call print_message
  158. les entry_segoff, %bx
  159. call print_segoff
  160. .section ".prefix.data"
  161. 10: .asciz " entry point at "
  162. .previous
  163. /* Print UNDI code segment */
  164. movw $10f, %si
  165. call print_message
  166. les undi_code_segoff, %bx
  167. call print_segoff
  168. .section ".prefix.data"
  169. 10: .asciz "\n UNDI code segment "
  170. .previous
  171. /* Print UNDI data segment */
  172. movw $10f, %si
  173. call print_message
  174. les undi_data_segoff, %bx
  175. call print_segoff
  176. .section ".prefix.data"
  177. 10: .asciz ", data segment "
  178. .previous
  179. /* Print UNDI memory usage */
  180. movw $10f, %si
  181. call print_message
  182. movw undi_fbms_start, %ax
  183. call print_word
  184. movb $'-', %al
  185. call print_character
  186. movw undi_fbms_end, %ax
  187. call print_word
  188. movw $20f, %si
  189. call print_message
  190. .section ".prefix.data"
  191. 10: .asciz " ("
  192. 20: .asciz "kB)\n"
  193. .previous
  194. /*****************************************************************************
  195. * Determine physical device
  196. *****************************************************************************
  197. */
  198. get_physical_device:
  199. /* Issue PXENV_UNDI_GET_NIC_TYPE */
  200. movw $PXENV_UNDI_GET_NIC_TYPE, %bx
  201. call pxe_call
  202. jnc 1f
  203. call print_pxe_error
  204. jmp no_physical_device
  205. 1: /* Determine physical device type */
  206. movb ( pxe_parameter_structure + 0x02 ), %al
  207. cmpb $2, %al
  208. je pci_physical_device
  209. jmp no_physical_device
  210. pci_physical_device:
  211. /* Record PCI bus:dev.fn and vendor/device IDs */
  212. movl ( pxe_parameter_structure + 0x03 ), %eax
  213. movl %eax, pci_vendor
  214. movw ( pxe_parameter_structure + 0x0b ), %ax
  215. movw %ax, pci_busdevfn
  216. movw $10f, %si
  217. call print_message
  218. call print_pci_busdevfn
  219. movb $0x0a, %al
  220. call print_character
  221. jmp 99f
  222. .section ".prefix.data"
  223. 10: .asciz " UNDI device is PCI "
  224. .previous
  225. no_physical_device:
  226. /* No device found, or device type not understood */
  227. movw $10f, %si
  228. call print_message
  229. .section ".prefix.data"
  230. 10: .asciz " Unable to determine UNDI physical device\n"
  231. .previous
  232. 99:
  233. /*****************************************************************************
  234. * Leave NIC in a safe state
  235. *****************************************************************************
  236. */
  237. shutdown_nic:
  238. /* Issue PXENV_UNDI_SHUTDOWN */
  239. movw $PXENV_UNDI_SHUTDOWN, %bx
  240. call pxe_call
  241. jnc 1f
  242. call print_pxe_error
  243. 1:
  244. /*****************************************************************************
  245. * Unload PXE base code
  246. *****************************************************************************
  247. */
  248. unload_base_code:
  249. /* Issue PXENV_UNLOAD_STACK */
  250. movw $PXENV_UNLOAD_STACK, %bx
  251. call pxe_call
  252. jnc 1f
  253. call print_pxe_error
  254. jmp 99f
  255. 1: /* Free base memory used by PXE base code */
  256. movw undi_fbms_start, %ax
  257. movw %fs:(0x13), %bx
  258. call free_basemem
  259. 99:
  260. /*****************************************************************************
  261. * Unload UNDI driver
  262. *****************************************************************************
  263. */
  264. #ifndef PXELOADER_KEEP_UNDI
  265. unload_undi:
  266. /* Issue PXENV_STOP_UNDI */
  267. movw $PXENV_STOP_UNDI, %bx
  268. call pxe_call
  269. jnc 1f
  270. call print_pxe_error
  271. jmp 99f
  272. 1: /* Free base memory used by UNDI */
  273. movw undi_fbms_end, %ax
  274. movw undi_fbms_start, %bx
  275. call free_basemem
  276. /* Clear UNDI_FL_STARTED */
  277. andw $~UNDI_FL_STARTED, flags
  278. 99:
  279. #endif /* PXELOADER_KEEP_UNDI */
  280. /*****************************************************************************
  281. * Print remaining free base memory
  282. *****************************************************************************
  283. */
  284. print_free_basemem:
  285. movw $10f, %si
  286. call print_message
  287. movw %fs:(0x13), %ax
  288. call print_word
  289. movw $20f, %si
  290. call print_message
  291. .section ".prefix.data"
  292. 10: .asciz " "
  293. 20: .asciz "kB free base memory after PXE unload\n"
  294. .previous
  295. /*****************************************************************************
  296. * Exit point
  297. *****************************************************************************
  298. */
  299. finished:
  300. jmp run_gpxe
  301. /*****************************************************************************
  302. * Subroutine: print segment:offset address
  303. *
  304. * Parameters:
  305. * %es:%bx : segment:offset address to print
  306. * %ds:di : output buffer (or %di=0 to print to console)
  307. * Returns:
  308. * %ds:di : next character in output buffer (if applicable)
  309. *****************************************************************************
  310. */
  311. print_segoff:
  312. /* Preserve registers */
  313. pushw %ax
  314. /* Print "<segment>:offset" */
  315. movw %es, %ax
  316. call print_hex_word
  317. movb $':', %al
  318. call print_character
  319. movw %bx, %ax
  320. call print_hex_word
  321. /* Restore registers and return */
  322. popw %ax
  323. ret
  324. /*****************************************************************************
  325. * Subroutine: print decimal word
  326. *
  327. * Parameters:
  328. * %ax : word to print
  329. * %ds:di : output buffer (or %di=0 to print to console)
  330. * Returns:
  331. * %ds:di : next character in output buffer (if applicable)
  332. *****************************************************************************
  333. */
  334. print_word:
  335. /* Preserve registers */
  336. pushw %ax
  337. pushw %bx
  338. pushw %cx
  339. pushw %dx
  340. /* Build up digit sequence on stack */
  341. movw $10, %bx
  342. xorw %cx, %cx
  343. 1: xorw %dx, %dx
  344. divw %bx, %ax
  345. pushw %dx
  346. incw %cx
  347. testw %ax, %ax
  348. jnz 1b
  349. /* Print digit sequence */
  350. 1: popw %ax
  351. call print_hex_nibble
  352. loop 1b
  353. /* Restore registers and return */
  354. popw %dx
  355. popw %cx
  356. popw %bx
  357. popw %ax
  358. ret
  359. /*****************************************************************************
  360. * Subroutine: zero 1kB block of base memory
  361. *
  362. * Parameters:
  363. * %bx : block to zero (in kB)
  364. * Returns:
  365. * Nothing
  366. *****************************************************************************
  367. */
  368. zero_kb:
  369. /* Preserve registers */
  370. pushw %ax
  371. pushw %cx
  372. pushw %di
  373. pushw %es
  374. /* Zero block */
  375. movw %bx, %ax
  376. shlw $6, %ax
  377. movw %ax, %es
  378. movw $0x400, %cx
  379. xorw %di, %di
  380. xorw %ax, %ax
  381. rep stosb
  382. /* Restore registers and return */
  383. popw %es
  384. popw %di
  385. popw %cx
  386. popw %ax
  387. ret
  388. /*****************************************************************************
  389. * Subroutine: free and zero base memory
  390. *
  391. * Parameters:
  392. * %ax : Desired new free base memory counter (in kB)
  393. * %bx : Expected current free base memory counter (in kB)
  394. * %fs : BIOS data segment (0x40)
  395. * Returns:
  396. * None
  397. *
  398. * The base memory from %bx kB to %ax kB is unconditionally zeroed.
  399. * It will be freed if and only if the expected current free base
  400. * memory counter (%bx) matches the actual current free base memory
  401. * counter in 0x40:0x13; if this does not match then the memory will
  402. * be leaked.
  403. *****************************************************************************
  404. */
  405. free_basemem:
  406. /* Zero base memory */
  407. pushw %bx
  408. 1: cmpw %bx, %ax
  409. je 2f
  410. call zero_kb
  411. incw %bx
  412. jmp 1b
  413. 2: popw %bx
  414. /* Free base memory */
  415. cmpw %fs:(0x13), %bx /* Update FBMS only if "old" value */
  416. jne 1f /* is correct */
  417. 1: movw %ax, %fs:(0x13)
  418. ret
  419. /*****************************************************************************
  420. * Subroutine: make a PXE API call. Works with either !PXE or PXENV+ API.
  421. *
  422. * Parameters:
  423. * %bx : PXE API call number
  424. * %ds:pxe_parameter_structure : Parameters for PXE API call
  425. * Returns:
  426. * %ax : PXE status code (not exit code)
  427. * CF set if %ax is non-zero
  428. *****************************************************************************
  429. */
  430. pxe_call:
  431. /* Preserve registers */
  432. pushw %di
  433. pushw %es
  434. /* Set up registers for PXENV+ API. %bx already set up */
  435. pushw %ds
  436. popw %es
  437. movw $pxe_parameter_structure, %di
  438. /* Set up stack for !PXE API */
  439. pushw %es
  440. pushw %di
  441. pushw %bx
  442. /* Make the API call */
  443. lcall *entry_segoff
  444. /* Reset the stack */
  445. addw $6, %sp
  446. movw pxe_parameter_structure, %ax
  447. clc
  448. testw %ax, %ax
  449. jz 1f
  450. stc
  451. 1: /* Restore registers and return */
  452. popw %es
  453. popw %di
  454. ret
  455. /*****************************************************************************
  456. * Subroutine: print PXE API call error message
  457. *
  458. * Parameters:
  459. * %ax : PXE status code
  460. * %bx : PXE API call number
  461. * Returns:
  462. * Nothing
  463. *****************************************************************************
  464. */
  465. print_pxe_error:
  466. pushw %si
  467. movw $10f, %si
  468. call print_message
  469. xchgw %ax, %bx
  470. call print_hex_word
  471. movw $20f, %si
  472. call print_message
  473. xchgw %ax, %bx
  474. call print_hex_word
  475. movw $30f, %si
  476. call print_message
  477. popw %si
  478. ret
  479. .section ".prefix.data"
  480. 10: .asciz " UNDI API call "
  481. 20: .asciz " failed: status code "
  482. 30: .asciz "\n"
  483. .previous
  484. /*****************************************************************************
  485. * PXE data structures
  486. *****************************************************************************
  487. */
  488. pxe_parameter_structure: .fill 20
  489. undi_code_segoff:
  490. undi_code_size: .word 0
  491. undi_code_segment: .word 0
  492. undi_data_segoff:
  493. undi_data_size: .word 0
  494. undi_data_segment: .word 0
  495. /* The following fields are part of a struct undi_device */
  496. undi_device:
  497. pxenv_segoff:
  498. pxenv_offset: .word 0
  499. pxenv_segment: .word 0
  500. ppxe_segoff:
  501. ppxe_offset: .word 0
  502. ppxe_segment: .word 0
  503. entry_segoff:
  504. entry_offset: .word 0
  505. entry_segment: .word 0
  506. undi_fbms_start: .word 0
  507. undi_fbms_end: .word 0
  508. pci_busdevfn: .word UNDI_NO_PCI_BUSDEVFN
  509. isapnp_csn: .word UNDI_NO_ISAPNP_CSN
  510. isapnp_read_port: .word UNDI_NO_ISAPNP_READ_PORT
  511. pci_vendor: .word 0
  512. pci_device: .word 0
  513. flags: .word UNDI_FL_STARTED
  514. .equ undi_device_size, ( . - undi_device )
  515. /*****************************************************************************
  516. * Run gPXE main code
  517. *****************************************************************************
  518. */
  519. run_gpxe:
  520. /* Install gPXE */
  521. call install
  522. /* Set up real-mode stack */
  523. movw %bx, %ss
  524. movw $_estack16, %sp
  525. #ifdef PXELOADER_KEEP_UNDI
  526. /* Copy our undi_device structure to the preloaded_undi variable */
  527. movw %bx, %es
  528. movw $preloaded_undi, %di
  529. movw $undi_device, %si
  530. movw $undi_device_size, %cx
  531. rep movsb
  532. #endif
  533. /* Jump to .text16 segment with %ds pointing to .data16 */
  534. movw %bx, %ds
  535. pushw %ax
  536. pushw $1f
  537. lret
  538. .section ".text16", "ax", @progbits
  539. 1:
  540. /* Run main program */
  541. pushl $main
  542. pushw %cs
  543. call prot_call
  544. popl %ecx /* discard */
  545. /* Uninstall gPXE */
  546. call uninstall
  547. /* Boot next device */
  548. int $0x18
  549. .previous