Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

dskprefix.S 8.1KB

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