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

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