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

dskprefix.S 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* NOTE: this boot sector contains instructions that need at least an 80186.
  2. * Yes, as86 has a bug somewhere in the valid instruction set checks.
  3. *
  4. */
  5. /* floppyload.S Copyright (C) 1991, 1992 Linus Torvalds
  6. * modified by Drew Eckhardt
  7. * modified by Bruce Evans (bde)
  8. *
  9. * floppyprefix.S is loaded at 0x0000:0x7c00 by the bios-startup routines.
  10. *
  11. * It then loads the system at SYSSEG<<4, using BIOS interrupts.
  12. *
  13. * The loader has been made as simple as possible, and continuous read errors
  14. * will result in a unbreakable loop. Reboot by hand. It loads pretty fast by
  15. * getting whole tracks at a time whenever possible.
  16. */
  17. FILE_LICENCE ( GPL2_ONLY )
  18. .equ BOOTSEG, 0x07C0 /* original address of boot-sector */
  19. .equ SYSSEG, 0x1000 /* system loaded at SYSSEG<<4 */
  20. .org 0
  21. .arch i386
  22. .text
  23. .section ".prefix", "ax", @progbits
  24. .code16
  25. .globl _dsk_start
  26. _dsk_start:
  27. jmp $BOOTSEG, $go /* reload cs:ip to match relocation addr */
  28. go:
  29. movw $0x2000-12, %di /* 0x2000 is arbitrary value >= length */
  30. /* of bootsect + room for stack + 12 for */
  31. /* saved disk parm block */
  32. movw $BOOTSEG, %ax
  33. movw %ax,%ds
  34. movw %ax,%es
  35. movw %ax,%ss /* put stack at BOOTSEG:0x4000-12. */
  36. movw %di,%sp
  37. /* Many BIOS's default disk parameter tables will not recognize multi-sector
  38. * reads beyond the maximum sector number specified in the default diskette
  39. * parameter tables - this may mean 7 sectors in some cases.
  40. *
  41. * Since single sector reads are slow and out of the question, we must take care
  42. * of this by creating new parameter tables (for the first disk) in RAM. We
  43. * will set the maximum sector count to 36 - the most we will encounter on an
  44. * ED 2.88. High doesn't hurt. Low does.
  45. *
  46. * Segments are as follows: ds=es=ss=cs - BOOTSEG
  47. */
  48. xorw %cx,%cx
  49. movw %cx,%es /* access segment 0 */
  50. movw $0x78, %bx /* 0:bx is parameter table address */
  51. pushw %ds /* save ds */
  52. /* 0:bx is parameter table address */
  53. ldsw %es:(%bx),%si /* loads ds and si */
  54. movw %ax,%es /* ax is BOOTSECT (loaded above) */
  55. movb $6, %cl /* copy 12 bytes */
  56. cld
  57. pushw %di /* keep a copy for later */
  58. rep
  59. movsw /* ds:si is source, es:di is dest */
  60. popw %di
  61. movb $36,%es:4(%di)
  62. movw %cx,%ds /* access segment 0 */
  63. xchgw %di,(%bx)
  64. movw %es,%si
  65. xchgw %si,2(%bx)
  66. popw %ds /* restore ds */
  67. movw %di, dpoff /* save old parameters */
  68. movw %si, dpseg /* to restore just before finishing */
  69. pushw %ds
  70. popw %es /* reload es */
  71. /* Note that es is already set up. Also cx is 0 from rep movsw above. */
  72. xorb %ah,%ah /* reset FDC */
  73. xorb %dl,%dl
  74. int $0x13
  75. /* Get disk drive parameters, specifically number of sectors/track.
  76. *
  77. * It seems that there is no BIOS call to get the number of sectors. Guess
  78. * 36 sectors if sector 36 can be read, 18 sectors if sector 18 can be read,
  79. * 15 if sector 15 can be read. Otherwise guess 9.
  80. */
  81. movw $disksizes, %si /* table of sizes to try */
  82. probe_loop:
  83. lodsb
  84. cbtw /* extend to word */
  85. movw %ax, sectors
  86. cmpw $disksizes+4, %si
  87. jae got_sectors /* if all else fails, try 9 */
  88. xchgw %cx,%ax /* cx = track and sector */
  89. xorw %dx,%dx /* drive 0, head 0 */
  90. movw $0x0200, %bx /* address after boot sector */
  91. /* (512 bytes from origin, es = cs) */
  92. movw $0x0201, %ax /* service 2, 1 sector */
  93. int $0x13
  94. jc probe_loop /* try next value */
  95. got_sectors:
  96. movw $msg1end-msg1, %cx
  97. movw $msg1, %si
  98. call print_str
  99. /* ok, we've written the Loading... message, now we want to load the system */
  100. movw $SYSSEG, %ax
  101. movw %ax,%es /* segment of SYSSEG<<4 */
  102. pushw %es
  103. call read_it
  104. /* This turns off the floppy drive motor, so that we enter the kernel in a
  105. * known state, and don't have to worry about it later.
  106. */
  107. movw $0x3f2, %dx
  108. xorb %al,%al
  109. outb %al,%dx
  110. call print_nl
  111. pop %es /* = SYSSEG */
  112. /* Restore original disk parameters */
  113. movw $0x78, %bx
  114. movw dpoff, %di
  115. movw dpseg, %si
  116. xorw %ax,%ax
  117. movw %ax,%ds
  118. movw %di,(%bx)
  119. movw %si,2(%bx)
  120. /* Everything now loaded. %es = SYSSEG, so %es:0000 points to
  121. * start of loaded image.
  122. */
  123. /* Jump to loaded copy */
  124. ljmp $SYSSEG, $start_runtime
  125. endseg: .word SYSSEG
  126. .section ".zinfo.fixup", "a", @progbits /* Compressor fixups */
  127. .ascii "ADDW"
  128. .long endseg
  129. .long 16
  130. .long 0
  131. .previous
  132. /* This routine loads the system at address SYSSEG<<4, making sure no 64kB
  133. * boundaries are crossed. We try to load it as fast as possible, loading whole
  134. * tracks whenever we can.
  135. *
  136. * in: es - starting address segment (normally SYSSEG)
  137. */
  138. read_it:
  139. movw $0,sread /* load whole image including prefix */
  140. movw %es,%ax
  141. testw $0x0fff, %ax
  142. die: jne die /* es must be at 64kB boundary */
  143. xorw %bx,%bx /* bx is starting address within segment */
  144. rp_read:
  145. movw %es,%ax
  146. movw %bx,%dx
  147. movb $4, %cl
  148. shrw %cl,%dx /* bx is always divisible by 16 */
  149. addw %dx,%ax
  150. cmpw endseg, %ax /* have we loaded all yet? */
  151. jb ok1_read
  152. ret
  153. ok1_read:
  154. movw sectors, %ax
  155. subw sread, %ax
  156. movw %ax,%cx
  157. shlw $9, %cx
  158. addw %bx,%cx
  159. jnc ok2_read
  160. je ok2_read
  161. xorw %ax,%ax
  162. subw %bx,%ax
  163. shrw $9, %ax
  164. ok2_read:
  165. call read_track
  166. movw %ax,%cx
  167. addw sread, %ax
  168. cmpw sectors, %ax
  169. jne ok3_read
  170. movw $1, %ax
  171. subw head, %ax
  172. jne ok4_read
  173. incw track
  174. ok4_read:
  175. movw %ax, head
  176. xorw %ax,%ax
  177. ok3_read:
  178. movw %ax, sread
  179. shlw $9, %cx
  180. addw %cx,%bx
  181. jnc rp_read
  182. movw %es,%ax
  183. addb $0x10, %ah
  184. movw %ax,%es
  185. xorw %bx,%bx
  186. jmp rp_read
  187. read_track:
  188. pusha
  189. pushw %ax
  190. pushw %bx
  191. pushw %bp /* just in case the BIOS is buggy */
  192. movw $0x0e2e, %ax /* 0x2e = . */
  193. movw $0x0007, %bx
  194. int $0x10
  195. popw %bp
  196. popw %bx
  197. popw %ax
  198. movw track, %dx
  199. movw sread, %cx
  200. incw %cx
  201. movb %dl,%ch
  202. movw head, %dx
  203. movb %dl,%dh
  204. andw $0x0100, %dx
  205. movb $2, %ah
  206. pushw %dx /* save for error dump */
  207. pushw %cx
  208. pushw %bx
  209. pushw %ax
  210. int $0x13
  211. jc bad_rt
  212. addw $8, %sp
  213. popa
  214. ret
  215. bad_rt: pushw %ax /* save error code */
  216. call print_all /* ah = error, al = read */
  217. xorb %ah,%ah
  218. xorb %dl,%dl
  219. int $0x13
  220. addw $10, %sp
  221. popa
  222. jmp read_track
  223. /* print_all is for debugging purposes. It will print out all of the registers.
  224. * The assumption is that this is called from a routine, with a stack frame like
  225. * dx
  226. * cx
  227. * bx
  228. * ax
  229. * error
  230. * ret <- sp
  231. */
  232. print_all:
  233. call print_nl /* nl for readability */
  234. movw $5, %cx /* error code + 4 registers */
  235. movw %sp,%bp
  236. print_loop:
  237. pushw %cx /* save count left */
  238. cmpb $5, %cl
  239. jae no_reg /* see if register name is needed */
  240. movw $0x0007, %bx /* page 0, attribute 7 (normal) */
  241. movw $0xe05+0x41-1, %ax
  242. subb %cl,%al
  243. int $0x10
  244. movb $0x58, %al /* 'X' */
  245. int $0x10
  246. movb $0x3A, %al /* ':' */
  247. int $0x10
  248. no_reg:
  249. addw $2, %bp /* next register */
  250. call print_hex /* print it */
  251. movb $0x20, %al /* print a space */
  252. int $0x10
  253. popw %cx
  254. loop print_loop
  255. call print_nl /* nl for readability */
  256. ret
  257. print_str:
  258. movw $0x0007, %bx /* page 0, attribute 7 (normal) */
  259. movb $0x0e, %ah /* write char, tty mode */
  260. prloop:
  261. lodsb
  262. int $0x10
  263. loop prloop
  264. ret
  265. print_nl:
  266. movw $0x0007, %bx /* page 0, attribute 7 (normal) */
  267. movw $0xe0d, %ax /* CR */
  268. int $0x10
  269. movb $0xa, %al /* LF */
  270. int $0x10
  271. ret
  272. /* print_hex prints the word pointed to by ss:bp in hexadecimal. */
  273. print_hex:
  274. movw (%bp),%dx /* load word into dx */
  275. movb $4, %cl
  276. movb $0x0e, %ah /* write char, tty mode */
  277. movw $0x0007, %bx /* page 0, attribute 7 (normal) */
  278. call print_digit
  279. call print_digit
  280. call print_digit
  281. /* fall through */
  282. print_digit:
  283. rol %cl,%dx /* rotate so that lowest 4 bits are used */
  284. movb $0x0f, %al /* mask for nybble */
  285. andb %dl,%al
  286. addb $0x90, %al /* convert al to ascii hex (four instructions) */
  287. daa
  288. adcb $0x40, %al
  289. daa
  290. int $0x10
  291. ret
  292. sread: .word 0 /* sectors read of current track */
  293. head: .word 0 /* current head */
  294. track: .word 0 /* current track */
  295. sectors:
  296. .word 0
  297. dpseg: .word 0
  298. dpoff: .word 0
  299. disksizes:
  300. .byte 36,18,15,9
  301. msg1:
  302. .ascii "Loading ROM image"
  303. msg1end:
  304. .org 510, 0
  305. .word 0xAA55
  306. start_runtime:
  307. /* Install iPXE */
  308. call install
  309. /* Set up real-mode stack */
  310. movw %bx, %ss
  311. movw $_estack16, %sp
  312. /* Jump to .text16 segment */
  313. pushw %ax
  314. pushw $1f
  315. lret
  316. .section ".text16", "awx", @progbits
  317. 1:
  318. pushl $main
  319. pushw %cs
  320. call prot_call
  321. popl %ecx /* discard */
  322. /* Uninstall iPXE */
  323. call uninstall
  324. /* Boot next device */
  325. int $0x18