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.

libprefix.S 23KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. *
  23. */
  24. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
  25. .arch i386
  26. /* Image compression enabled */
  27. #define COMPRESS 1
  28. /* Protected mode flag */
  29. #define CR0_PE 1
  30. /* Allow for DBG()-style messages within libprefix */
  31. #ifdef NDEBUG
  32. .macro progress message
  33. .endm
  34. #else
  35. .macro progress message
  36. pushfl
  37. pushw %ds
  38. pushw %si
  39. pushw %di
  40. pushw %cs
  41. popw %ds
  42. xorw %di, %di
  43. movw $progress_\@, %si
  44. call print_message
  45. popw %di
  46. popw %si
  47. popw %ds
  48. popfl
  49. .section ".prefix.data", "aw", @progbits
  50. progress_\@:
  51. .asciz "\message"
  52. .size progress_\@, . - progress_\@
  53. .previous
  54. .endm
  55. #endif
  56. /*****************************************************************************
  57. * Utility function: print character (with LF -> LF,CR translation)
  58. *
  59. * Parameters:
  60. * %al : character to print
  61. * %ds:di : output buffer (or %di=0 to print to console)
  62. * Returns:
  63. * %ds:di : next character in output buffer (if applicable)
  64. *****************************************************************************
  65. */
  66. .section ".prefix.lib", "awx", @progbits
  67. .code16
  68. .globl print_character
  69. print_character:
  70. /* Preserve registers */
  71. pushw %ax
  72. pushw %bx
  73. pushw %bp
  74. /* If %di is non-zero, write character to buffer and exit */
  75. testw %di, %di
  76. jz 1f
  77. movb %al, %ds:(%di)
  78. incw %di
  79. jmp 3f
  80. 1: /* Print character */
  81. movw $0x0007, %bx /* page 0, attribute 7 (normal) */
  82. movb $0x0e, %ah /* write char, tty mode */
  83. cmpb $0x0a, %al /* '\n'? */
  84. jne 2f
  85. int $0x10
  86. movb $0x0d, %al
  87. 2: int $0x10
  88. /* Restore registers and return */
  89. 3: popw %bp
  90. popw %bx
  91. popw %ax
  92. ret
  93. .size print_character, . - print_character
  94. /*****************************************************************************
  95. * Utility function: print space
  96. *
  97. * Parameters:
  98. * %ds:di : output buffer (or %di=0 to print to console)
  99. * Returns:
  100. * %ds:di : next character in output buffer (if applicable)
  101. *****************************************************************************
  102. */
  103. .section ".prefix.lib", "awx", @progbits
  104. .code16
  105. .globl print_space
  106. print_space:
  107. /* Preserve registers */
  108. pushw %ax
  109. /* Print space */
  110. movb $( ' ' ), %al
  111. call print_character
  112. /* Restore registers and return */
  113. popw %ax
  114. ret
  115. .size print_space, . - print_space
  116. /*****************************************************************************
  117. * Utility function: print a NUL-terminated string
  118. *
  119. * Parameters:
  120. * %ds:si : string to print
  121. * %ds:di : output buffer (or %di=0 to print to console)
  122. * Returns:
  123. * %ds:si : character after terminating NUL
  124. * %ds:di : next character in output buffer (if applicable)
  125. *****************************************************************************
  126. */
  127. .section ".prefix.lib", "awx", @progbits
  128. .code16
  129. .globl print_message
  130. print_message:
  131. /* Preserve registers */
  132. pushw %ax
  133. /* Print string */
  134. 1: lodsb
  135. testb %al, %al
  136. je 2f
  137. call print_character
  138. jmp 1b
  139. 2: /* Restore registers and return */
  140. popw %ax
  141. ret
  142. .size print_message, . - print_message
  143. /*****************************************************************************
  144. * Utility functions: print hex digit/byte/word/dword
  145. *
  146. * Parameters:
  147. * %al (low nibble) : digit to print
  148. * %al : byte to print
  149. * %ax : word to print
  150. * %eax : dword to print
  151. * %ds:di : output buffer (or %di=0 to print to console)
  152. * Returns:
  153. * %ds:di : next character in output buffer (if applicable)
  154. *****************************************************************************
  155. */
  156. .section ".prefix.lib", "awx", @progbits
  157. .code16
  158. .globl print_hex_dword
  159. print_hex_dword:
  160. rorl $16, %eax
  161. call print_hex_word
  162. rorl $16, %eax
  163. /* Fall through */
  164. .size print_hex_dword, . - print_hex_dword
  165. .globl print_hex_word
  166. print_hex_word:
  167. xchgb %al, %ah
  168. call print_hex_byte
  169. xchgb %al, %ah
  170. /* Fall through */
  171. .size print_hex_word, . - print_hex_word
  172. .globl print_hex_byte
  173. print_hex_byte:
  174. rorb $4, %al
  175. call print_hex_nibble
  176. rorb $4, %al
  177. /* Fall through */
  178. .size print_hex_byte, . - print_hex_byte
  179. .globl print_hex_nibble
  180. print_hex_nibble:
  181. /* Preserve registers */
  182. pushw %ax
  183. /* Print digit (technique by Norbert Juffa <norbert.juffa@amd.com> */
  184. andb $0x0f, %al
  185. cmpb $10, %al
  186. sbbb $0x69, %al
  187. das
  188. call print_character
  189. /* Restore registers and return */
  190. popw %ax
  191. ret
  192. .size print_hex_nibble, . - print_hex_nibble
  193. /*****************************************************************************
  194. * Utility function: print PCI bus:dev.fn
  195. *
  196. * Parameters:
  197. * %ax : PCI bus:dev.fn to print
  198. * %ds:di : output buffer (or %di=0 to print to console)
  199. * Returns:
  200. * %ds:di : next character in output buffer (if applicable)
  201. *****************************************************************************
  202. */
  203. .section ".prefix.lib", "awx", @progbits
  204. .code16
  205. .globl print_pci_busdevfn
  206. print_pci_busdevfn:
  207. /* Preserve registers */
  208. pushw %ax
  209. /* Print bus */
  210. xchgb %al, %ah
  211. call print_hex_byte
  212. /* Print ":" */
  213. movb $( ':' ), %al
  214. call print_character
  215. /* Print device */
  216. movb %ah, %al
  217. shrb $3, %al
  218. call print_hex_byte
  219. /* Print "." */
  220. movb $( '.' ), %al
  221. call print_character
  222. /* Print function */
  223. movb %ah, %al
  224. andb $0x07, %al
  225. call print_hex_nibble
  226. /* Restore registers and return */
  227. popw %ax
  228. ret
  229. .size print_pci_busdevfn, . - print_pci_busdevfn
  230. /*****************************************************************************
  231. * Utility function: clear current line
  232. *
  233. * Parameters:
  234. * %ds:di : output buffer (or %di=0 to print to console)
  235. * Returns:
  236. * %ds:di : next character in output buffer (if applicable)
  237. *****************************************************************************
  238. */
  239. .section ".prefix.lib", "awx", @progbits
  240. .code16
  241. .globl print_kill_line
  242. print_kill_line:
  243. /* Preserve registers */
  244. pushw %ax
  245. pushw %cx
  246. /* Print CR */
  247. movb $( '\r' ), %al
  248. call print_character
  249. /* Print 79 spaces */
  250. movw $79, %cx
  251. 1: call print_space
  252. loop 1b
  253. /* Print CR */
  254. call print_character
  255. /* Restore registers and return */
  256. popw %cx
  257. popw %ax
  258. ret
  259. .size print_kill_line, . - print_kill_line
  260. /****************************************************************************
  261. * copy_bytes
  262. *
  263. * Copy bytes
  264. *
  265. * Parameters:
  266. * %ds:esi : source address
  267. * %es:edi : destination address
  268. * %ecx : length
  269. * Returns:
  270. * %ds:esi : next source address
  271. * %es:edi : next destination address
  272. * Corrupts:
  273. * None
  274. ****************************************************************************
  275. */
  276. .section ".prefix.lib", "awx", @progbits
  277. .code16
  278. copy_bytes:
  279. pushl %ecx
  280. rep addr32 movsb
  281. popl %ecx
  282. ret
  283. .size copy_bytes, . - copy_bytes
  284. /****************************************************************************
  285. * zero_bytes
  286. *
  287. * Zero bytes
  288. *
  289. * Parameters:
  290. * %es:edi : destination address
  291. * %ecx : length
  292. * Returns:
  293. * %es:edi : next destination address
  294. * Corrupts:
  295. * None
  296. ****************************************************************************
  297. */
  298. .section ".prefix.lib", "awx", @progbits
  299. .code16
  300. zero_bytes:
  301. pushl %ecx
  302. pushw %ax
  303. xorw %ax, %ax
  304. rep addr32 stosb
  305. popw %ax
  306. popl %ecx
  307. ret
  308. .size zero_bytes, . - zero_bytes
  309. /****************************************************************************
  310. * process_bytes
  311. *
  312. * Call memcpy()-like function
  313. *
  314. * Parameters:
  315. * %esi : source physical address
  316. * %edi : destination physical address
  317. * %ecx : length
  318. * %bx : memcpy()-like function to call, passing parameters:
  319. * %ds:esi : source address
  320. * %es:edi : destination address
  321. * %ecx : length
  322. * and returning:
  323. * %ds:esi : next source address
  324. * %es:edi : next destination address
  325. * Returns:
  326. * %esi : next source physical address
  327. * %edi : next destination physical address
  328. * Corrupts:
  329. * None
  330. ****************************************************************************
  331. */
  332. .section ".prefix.lib", "awx", @progbits
  333. .code16
  334. process_bytes:
  335. #ifndef KEEP_IT_REAL
  336. /* Preserve registers */
  337. pushl %eax
  338. pushl %ebp
  339. /* Construct GDT on stack (since .prefix may not be writable) */
  340. .equ PM_DS, 0x18 /* Flat data segment */
  341. pushl $0x00cf9300
  342. pushl $0x0000ffff
  343. .equ PM_SS, 0x10 /* Stack segment based at %ss:0000 */
  344. pushl $0x008f0930
  345. pushw %ss
  346. pushw $0xffff
  347. .equ PM_CS, 0x08 /* Code segment based at %cs:0000 */
  348. pushl $0x008f09b0
  349. pushw %cs
  350. pushw $0xffff
  351. pushl $0 /* Base and length */
  352. pushw %ss
  353. pushw $0x1f
  354. movzwl %sp, %ebp
  355. shll $4, 0x02(%bp)
  356. addl %ebp, 0x02(%bp)
  357. shll $4, 0x0a(%bp)
  358. shll $4, 0x12(%bp)
  359. subw $8, %sp
  360. sgdt -8(%bp)
  361. /* Switch to protected mode */
  362. pushw %gs
  363. pushw %fs
  364. pushw %es
  365. pushw %ds
  366. pushw %ss
  367. pushw %cs
  368. pushw $2f
  369. cli
  370. data32 lgdt (%bp)
  371. movl %cr0, %eax
  372. orb $CR0_PE, %al
  373. movl %eax, %cr0
  374. ljmp $PM_CS, $1f
  375. 1: movw $PM_SS, %ax
  376. movw %ax, %ss
  377. movw $PM_DS, %ax
  378. movw %ax, %ds
  379. movw %ax, %es
  380. movw %ax, %fs
  381. movw %ax, %gs
  382. #ifdef NDEBUG
  383. /* Call memcpy()-like function */
  384. call *%bx
  385. #endif
  386. /* Return to (flat) real mode */
  387. movl %cr0, %eax
  388. andb $0!CR0_PE, %al
  389. movl %eax, %cr0
  390. lret
  391. 2: /* lret will ljmp to here */
  392. popw %ss
  393. popw %ds
  394. popw %es
  395. popw %fs
  396. popw %gs
  397. #ifndef NDEBUG
  398. /* Call memcpy()-like function in flat real mode (to allow for
  399. * debug output via INT 10).
  400. */
  401. pushw %ds
  402. pushw %es
  403. xorw %ax, %ax
  404. movw %ax, %ds
  405. movw %ax, %es
  406. call *%bx
  407. popw %es
  408. popw %ds
  409. #endif
  410. /* Restore GDT */
  411. data32 lgdt -8(%bp)
  412. addw $( 8 /* saved GDT */ + ( PM_DS + 8 ) /* GDT on stack */ ), %sp
  413. /* Restore registers and return */
  414. popl %ebp
  415. popl %eax
  416. ret
  417. #else /* KEEP_IT_REAL */
  418. /* Preserve registers */
  419. pushl %eax
  420. pushw %ds
  421. pushw %es
  422. /* Convert %esi and %edi to %ds:esi and %es:edi */
  423. shrl $4, %esi
  424. movw %si, %ds
  425. xorw %si, %si
  426. shll $4, %esi
  427. shrl $4, %edi
  428. movw %di, %es
  429. xorw %di, %di
  430. shll $4, %edi
  431. /* Call memcpy()-like function */
  432. call *%bx
  433. /* Convert %ds:esi and %es:edi back to physical addresses */
  434. xorl %eax, %eax
  435. movw %ds, %cx
  436. shll $4, %eax
  437. addl %eax, %esi
  438. xorl %eax, %eax
  439. movw %es, %cx
  440. shll $4, %eax
  441. addl %eax, %edi
  442. /* Restore registers and return */
  443. popw %es
  444. popw %ds
  445. popl %eax
  446. ret
  447. #endif /* KEEP_IT_REAL */
  448. .size process_bytes, . - process_bytes
  449. /****************************************************************************
  450. * install_block
  451. *
  452. * Install block to specified address
  453. *
  454. * Parameters:
  455. * %esi : source physical address (must be a multiple of 16)
  456. * %edi : destination physical address (must be a multiple of 16)
  457. * %ecx : length of (decompressed) data
  458. * %edx : total length of block (including any uninitialised data portion)
  459. * Returns:
  460. * %esi : next source physical address (will be a multiple of 16)
  461. * %edi : next destination physical address (will be a multiple of 16)
  462. * Corrupts:
  463. * none
  464. ****************************************************************************
  465. */
  466. .section ".prefix.lib", "awx", @progbits
  467. .code16
  468. install_block:
  469. /* Preserve registers */
  470. pushl %ecx
  471. pushw %bx
  472. /* Decompress (or copy) source to destination */
  473. #if COMPRESS
  474. movw $decompress16, %bx
  475. #else
  476. movw $copy_bytes, %bx
  477. #endif
  478. call process_bytes
  479. /* Zero .bss portion */
  480. negl %ecx
  481. addl %edx, %ecx
  482. movw $zero_bytes, %bx
  483. call process_bytes
  484. /* Round up %esi and %edi to start of next blocks */
  485. addl $0xf, %esi
  486. andl $~0xf, %esi
  487. addl $0xf, %edi
  488. andl $~0xf, %edi
  489. /* Restore registers and return */
  490. popw %bx
  491. popl %ecx
  492. ret
  493. .size install_block, . - install_block
  494. /****************************************************************************
  495. * alloc_basemem
  496. *
  497. * Allocate space for .text16 and .data16 from top of base memory.
  498. * Memory is allocated using the BIOS free base memory counter at
  499. * 0x40:13.
  500. *
  501. * Parameters:
  502. * none
  503. * Returns:
  504. * %ax : .text16 segment address
  505. * %bx : .data16 segment address
  506. * Corrupts:
  507. * none
  508. ****************************************************************************
  509. */
  510. .section ".prefix.lib", "awx", @progbits
  511. .code16
  512. .globl alloc_basemem
  513. alloc_basemem:
  514. /* Preserve registers */
  515. pushw %fs
  516. /* FBMS => %ax as segment address */
  517. pushw $0x40
  518. popw %fs
  519. movw %fs:0x13, %ax
  520. shlw $6, %ax
  521. /* Calculate .data16 segment address */
  522. subw $_data16_memsz_pgh, %ax
  523. pushw %ax
  524. /* Calculate .text16 segment address. Round down to ensure
  525. * low bits are zero, to speed up mode transitions under KVM.
  526. */
  527. subw $_text16_memsz_pgh, %ax
  528. andb $~0x03, %al
  529. pushw %ax
  530. /* Update FBMS */
  531. shrw $6, %ax
  532. movw %ax, %fs:0x13
  533. /* Retrieve .text16 and .data16 segment addresses */
  534. popw %ax
  535. popw %bx
  536. /* Restore registers and return */
  537. popw %fs
  538. ret
  539. .size alloc_basemem, . - alloc_basemem
  540. /****************************************************************************
  541. * free_basemem
  542. *
  543. * Free space allocated with alloc_basemem.
  544. *
  545. * Parameters:
  546. * none (.text16 segment address is implicit in %cs)
  547. * Returns:
  548. * %ax : 0 if successfully freed
  549. * Corrupts:
  550. * none
  551. ****************************************************************************
  552. */
  553. .section ".text16", "ax", @progbits
  554. .code16
  555. .globl free_basemem
  556. free_basemem:
  557. /* Preserve registers */
  558. pushw %fs
  559. pushw %ax
  560. /* Check FBMS counter */
  561. movw %cs, %ax
  562. shrw $6, %ax
  563. pushw $0x40
  564. popw %fs
  565. cmpw %ax, %fs:0x13
  566. jne 1f
  567. /* Check hooked interrupt count */
  568. cmpw $0, %cs:hooked_bios_interrupts
  569. jne 1f
  570. /* OK to free memory */
  571. movw %cs, %ax
  572. addw $_text16_memsz_pgh, %ax
  573. addw $_data16_memsz_pgh, %ax
  574. shrw $6, %ax
  575. movw %ax, %fs:0x13
  576. xorw %ax, %ax
  577. 1: /* Restore registers and return */
  578. popw %ax
  579. popw %fs
  580. ret
  581. .size free_basemem, . - free_basemem
  582. .section ".text16.data", "aw", @progbits
  583. .globl hooked_bios_interrupts
  584. hooked_bios_interrupts:
  585. .word 0
  586. .size hooked_bios_interrupts, . - hooked_bios_interrupts
  587. /****************************************************************************
  588. * install
  589. *
  590. * Install all text and data segments.
  591. *
  592. * Parameters:
  593. * none
  594. * Returns:
  595. * %ax : .text16 segment address
  596. * %bx : .data16 segment address
  597. * Corrupts:
  598. * none
  599. ****************************************************************************
  600. */
  601. .section ".prefix.lib", "awx", @progbits
  602. .code16
  603. .globl install
  604. install:
  605. progress "install:\n"
  606. /* Preserve registers */
  607. pushl %esi
  608. pushl %edi
  609. pushl %ebp
  610. /* Allocate space for .text16 and .data16 */
  611. call alloc_basemem
  612. /* Image source = %cs:0000 */
  613. xorl %esi, %esi
  614. /* Image destination = default */
  615. xorl %edi, %edi
  616. /* Allow arbitrary relocation */
  617. orl $0xffffffff, %ebp
  618. /* Install text and data segments */
  619. call install_prealloc
  620. /* Restore registers and return */
  621. popl %ebp
  622. popl %edi
  623. popl %esi
  624. ret
  625. .size install, . - install
  626. /****************************************************************************
  627. * install_prealloc
  628. *
  629. * Install all text and data segments.
  630. *
  631. * Parameters:
  632. * %ax : .text16 segment address
  633. * %bx : .data16 segment address
  634. * %esi : Image source physical address (or zero for %cs:0000)
  635. * %edi : Decompression temporary area physical address (or zero for default)
  636. * %ebp : Maximum end address for relocation
  637. * - 0xffffffff for no maximum
  638. * - 0x00000000 to inhibit use of INT 15,e820 and INT 15,e801
  639. * Corrupts:
  640. * none
  641. ****************************************************************************
  642. */
  643. .section ".prefix.lib", "awx", @progbits
  644. .code16
  645. .globl install_prealloc
  646. install_prealloc:
  647. progress "install_prealloc:\n"
  648. /* Save registers on external stack */
  649. pushal
  650. pushw %ds
  651. pushw %es
  652. cld /* Sanity: clear the direction flag asap */
  653. /* Switch to temporary stack in .bss16 */
  654. pushw %ss
  655. popw %ds
  656. movl %esp, %ecx
  657. movw %bx, %ss
  658. movl $_data16_memsz, %esp
  659. pushw %ds
  660. pushl %ecx
  661. /* Set up %ds for (read-only) access to .prefix */
  662. pushw %cs
  663. popw %ds
  664. /* Save decompression temporary area physical address */
  665. pushl %edi
  666. /* Install .text16.early and calculate %ecx as offset to next block */
  667. progress " .text16.early\n"
  668. pushl %esi
  669. xorl %esi, %esi
  670. movw %cs, %si
  671. shll $4, %esi
  672. pushl %esi /* Save original %cs:0000 */
  673. addl $_text16_early_lma, %esi
  674. movzwl %ax, %edi
  675. shll $4, %edi
  676. movl $_text16_early_filesz, %ecx
  677. movl $_text16_early_memsz, %edx
  678. call install_block /* .text16.early */
  679. popl %ecx /* Calculate offset to next block */
  680. subl %esi, %ecx
  681. negl %ecx
  682. popl %esi
  683. #ifndef KEEP_IT_REAL
  684. /* Access high memory by enabling the A20 gate. (We will
  685. * already have 4GB segment limits as a result of calling
  686. * install_block.)
  687. */
  688. progress " access_highmem\n"
  689. pushw %cs
  690. pushw $1f
  691. pushw %ax
  692. pushw $access_highmem
  693. lret
  694. 1: /* Die if we could not access high memory */
  695. jnc 3f
  696. movw $a20_death_message, %si
  697. xorw %di, %di
  698. call print_message
  699. 2: jmp 2b
  700. .section ".prefix.data", "aw", @progbits
  701. a20_death_message:
  702. .asciz "\nHigh memory inaccessible - cannot continue\n"
  703. .size a20_death_message, . - a20_death_message
  704. .previous
  705. 3:
  706. #endif
  707. /* Open payload (which may not yet be in memory) */
  708. progress " open_payload\n"
  709. pushw %cs
  710. pushw $1f
  711. pushw %ax
  712. pushw $open_payload
  713. lret
  714. 1: /* Die if we could not access the payload */
  715. jnc 3f
  716. xorw %di, %di
  717. movl %esi, %eax
  718. call print_hex_dword
  719. call print_space
  720. movl %ecx, %eax
  721. call print_hex_dword
  722. movw $payload_death_message, %si
  723. call print_message
  724. 2: /* Halt system */
  725. cli
  726. hlt
  727. jmp 2b
  728. .section ".prefix.data", "aw", @progbits
  729. payload_death_message:
  730. .asciz "\nPayload inaccessible - cannot continue\n"
  731. .size payload_death_message, . - payload_death_message
  732. .previous
  733. 3:
  734. /* Calculate physical address of payload (i.e. first source) */
  735. testl %esi, %esi
  736. jnz 1f
  737. movw %cs, %si
  738. shll $4, %esi
  739. 1: addl %ecx, %esi
  740. /* Install .text16.late and .data16 */
  741. progress " .text16.late\n"
  742. movl $_text16_late_filesz, %ecx
  743. movl $_text16_late_memsz, %edx
  744. call install_block /* .text16.late */
  745. progress " .data16\n"
  746. movzwl %bx, %edi
  747. shll $4, %edi
  748. movl $_data16_filesz, %ecx
  749. movl $_data16_filesz, %edx /* do not zero our temporary stack */
  750. call install_block /* .data16 */
  751. /* Set up %ds for access to .data16 */
  752. movw %bx, %ds
  753. /* Restore decompression temporary area physical address */
  754. popl %edi
  755. #ifndef KEEP_IT_REAL
  756. /* Find a suitable decompression temporary area, if none specified */
  757. pushl %eax
  758. testl %edi, %edi
  759. jnz 1f
  760. /* Use INT 15,88 to find the highest available address via INT
  761. * 15,88. This limits us to around 64MB, which should avoid
  762. * all of the POST-time memory map failure modes.
  763. */
  764. movb $0x88, %ah
  765. int $0x15
  766. movw %ax, %di
  767. addl $0x400, %edi
  768. subl $_textdata_memsz_kb, %edi
  769. shll $10, %edi
  770. /* Sanity check: if we have ended up below 1MB, use 1MB */
  771. cmpl $0x100000, %edi
  772. jae 1f
  773. movl $0x100000, %edi
  774. 1: popl %eax
  775. /* Install .text and .data to temporary area in high memory,
  776. * prior to reading the E820 memory map and relocating
  777. * properly.
  778. */
  779. progress " .textdata\n"
  780. pushl %edi
  781. movl $_textdata_filesz, %ecx
  782. movl $_textdata_memsz, %edx
  783. call install_block
  784. popl %edi
  785. #endif /* KEEP_IT_REAL */
  786. /* Switch back to original stack and zero .bss16 */
  787. lss %ss:(%esp), %esp
  788. pushl %edi
  789. pushw %es
  790. movw %bx, %es
  791. movl $_data16_filesz, %edi
  792. movl $_data16_memsz, %ecx
  793. subl %edi, %ecx
  794. call zero_bytes
  795. popw %es
  796. popl %edi
  797. #ifndef KEEP_IT_REAL
  798. /* Initialise librm at current location */
  799. progress " init_librm\n"
  800. movw %ax, (init_librm_vector+2)
  801. lcall *init_librm_vector
  802. /* Inhibit INT 15,e820 and INT 15,e801 if applicable */
  803. testl %ebp, %ebp
  804. jnz 1f
  805. incb memmap_post
  806. decl %ebp
  807. 1:
  808. /* Call relocate() to determine target address for relocation.
  809. * relocate() will return with %esi, %edi and %ecx set up
  810. * ready for the copy to the new location.
  811. */
  812. progress " relocate\n"
  813. movw %ax, (prot_call_vector+2)
  814. pushl $relocate
  815. lcall *prot_call_vector
  816. popl %edx /* discard */
  817. /* Copy code to new location */
  818. progress " copy\n"
  819. pushl %edi
  820. pushw %bx
  821. movw $copy_bytes, %bx
  822. call process_bytes
  823. popw %bx
  824. popl %edi
  825. /* Initialise librm at new location */
  826. progress " init_librm\n"
  827. lcall *init_librm_vector
  828. #else /* KEEP_IT_REAL */
  829. /* Initialise libkir */
  830. movw %ax, (init_libkir_vector+2)
  831. lcall *init_libkir_vector
  832. #endif /* KEEP_IT_REAL */
  833. /* Close access to payload */
  834. progress " close_payload\n"
  835. movw %ax, (close_payload_vector+2)
  836. lcall *close_payload_vector
  837. /* Restore registers */
  838. popw %es
  839. popw %ds
  840. popal
  841. ret
  842. .size install_prealloc, . - install_prealloc
  843. /* Vectors for far calls to .text16 functions. Must be in
  844. * .data16, since .prefix may not be writable.
  845. */
  846. .section ".data16", "aw", @progbits
  847. #ifdef KEEP_IT_REAL
  848. init_libkir_vector:
  849. .word init_libkir
  850. .word 0
  851. .size init_libkir_vector, . - init_libkir_vector
  852. #else
  853. init_librm_vector:
  854. .word init_librm
  855. .word 0
  856. .size init_librm_vector, . - init_librm_vector
  857. prot_call_vector:
  858. .word prot_call
  859. .word 0
  860. .size prot_call_vector, . - prot_call_vector
  861. #endif
  862. close_payload_vector:
  863. .word close_payload
  864. .word 0
  865. .size close_payload_vector, . - close_payload_vector
  866. /* Dummy routines to open and close payload */
  867. .section ".text16.early.data", "aw", @progbits
  868. .weak open_payload
  869. .weak close_payload
  870. open_payload:
  871. close_payload:
  872. clc
  873. lret
  874. .size open_payload, . - open_payload
  875. .size close_payload, . - close_payload
  876. /****************************************************************************
  877. * uninstall
  878. *
  879. * Uninstall all text and data segments.
  880. *
  881. * Parameters:
  882. * none (.text16 segment address is implicit in %cs)
  883. * Returns:
  884. * none
  885. * Corrupts:
  886. * none
  887. ****************************************************************************
  888. */
  889. .section ".text16", "ax", @progbits
  890. .code16
  891. .globl uninstall
  892. uninstall:
  893. call free_basemem
  894. ret
  895. .size uninstall, . - uninstall
  896. /* File split information for the compressor */
  897. #if COMPRESS
  898. #define PACK_OR_COPY "PACK"
  899. #else
  900. #define PACK_OR_COPY "COPY"
  901. #endif
  902. .section ".zinfo", "a", @progbits
  903. .ascii "COPY"
  904. .long _prefix_lma
  905. .long _prefix_filesz
  906. .long _max_align
  907. .ascii PACK_OR_COPY
  908. .long _text16_early_lma
  909. .long _text16_early_filesz
  910. .long _max_align
  911. .ascii "PAYL"
  912. .long 0
  913. .long 0
  914. .long _payload_align
  915. .ascii "COPY"
  916. .long _pprefix_lma
  917. .long _pprefix_filesz
  918. .long _max_align
  919. .ascii PACK_OR_COPY
  920. .long _text16_late_lma
  921. .long _text16_late_filesz
  922. .long _max_align
  923. .ascii PACK_OR_COPY
  924. .long _data16_lma
  925. .long _data16_filesz
  926. .long _max_align
  927. .ascii PACK_OR_COPY
  928. .long _textdata_lma
  929. .long _textdata_filesz
  930. .long _max_align
  931. .weak _payload_align
  932. .equ _payload_align, 1