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.

dskprefix.S 8.1KB

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