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

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