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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. * SYS_SIZE is the number of clicks (16 bytes) to be loaded.
  5. */
  6. .globl SYSSIZE
  7. .equ SYSSIZE, _load_size_pgh
  8. /* floppyload.S Copyright (C) 1991, 1992 Linus Torvalds
  9. * modified by Drew Eckhardt
  10. * modified by Bruce Evans (bde)
  11. *
  12. * floppyprefix.S is loaded at 0x0000:0x7c00 by the bios-startup routines.
  13. *
  14. * It then loads the system at SYSSEG<<4, using BIOS interrupts.
  15. *
  16. * The loader has been made as simple as possible, and continuous read errors
  17. * will result in a unbreakable loop. Reboot by hand. It loads pretty fast by
  18. * getting whole tracks at a time whenever possible.
  19. */
  20. .equ BOOTSEG, 0x07C0 /* original address of boot-sector */
  21. .equ SYSSEG, 0x1000 /* system loaded at SYSSEG<<4 */
  22. .org 0
  23. .arch i386
  24. .text
  25. .section ".prefix", "ax", @progbits
  26. .code16
  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. /* This routine loads the system at address SYSSEG<<4, making sure no 64kB
  126. * boundaries are crossed. We try to load it as fast as possible, loading whole
  127. * tracks whenever we can.
  128. *
  129. * in: es - starting address segment (normally SYSSEG)
  130. */
  131. read_it:
  132. movw $0,sread /* load whole image including prefix */
  133. movw %es,%ax
  134. testw $0x0fff, %ax
  135. die: jne die /* es must be at 64kB boundary */
  136. xorw %bx,%bx /* bx is starting address within segment */
  137. rp_read:
  138. movw %es,%ax
  139. movw %bx,%dx
  140. movb $4, %cl
  141. shrw %cl,%dx /* bx is always divisible by 16 */
  142. addw %dx,%ax
  143. cmpw $SYSSEG+SYSSIZE, %ax /* have we loaded all yet? */
  144. jb ok1_read
  145. ret
  146. ok1_read:
  147. movw sectors, %ax
  148. subw sread, %ax
  149. movw %ax,%cx
  150. shlw $9, %cx
  151. addw %bx,%cx
  152. jnc ok2_read
  153. je ok2_read
  154. xorw %ax,%ax
  155. subw %bx,%ax
  156. shrw $9, %ax
  157. ok2_read:
  158. call read_track
  159. movw %ax,%cx
  160. addw sread, %ax
  161. cmpw sectors, %ax
  162. jne ok3_read
  163. movw $1, %ax
  164. subw head, %ax
  165. jne ok4_read
  166. incw track
  167. ok4_read:
  168. movw %ax, head
  169. xorw %ax,%ax
  170. ok3_read:
  171. movw %ax, sread
  172. shlw $9, %cx
  173. addw %cx,%bx
  174. jnc rp_read
  175. movw %es,%ax
  176. addb $0x10, %ah
  177. movw %ax,%es
  178. xorw %bx,%bx
  179. jmp rp_read
  180. read_track:
  181. pusha
  182. pushw %ax
  183. pushw %bx
  184. pushw %bp /* just in case the BIOS is buggy */
  185. movw $0x0e2e, %ax /* 0x2e = . */
  186. movw $0x0007, %bx
  187. int $0x10
  188. popw %bp
  189. popw %bx
  190. popw %ax
  191. movw track, %dx
  192. movw sread, %cx
  193. incw %cx
  194. movb %dl,%ch
  195. movw head, %dx
  196. movb %dl,%dh
  197. andw $0x0100, %dx
  198. movb $2, %ah
  199. pushw %dx /* save for error dump */
  200. pushw %cx
  201. pushw %bx
  202. pushw %ax
  203. int $0x13
  204. jc bad_rt
  205. addw $8, %sp
  206. popa
  207. ret
  208. bad_rt: pushw %ax /* save error code */
  209. call print_all /* ah = error, al = read */
  210. xorb %ah,%ah
  211. xorb %dl,%dl
  212. int $0x13
  213. addw $10, %sp
  214. popa
  215. jmp read_track
  216. /* print_all is for debugging purposes. It will print out all of the registers.
  217. * The assumption is that this is called from a routine, with a stack frame like
  218. * dx
  219. * cx
  220. * bx
  221. * ax
  222. * error
  223. * ret <- sp
  224. */
  225. print_all:
  226. call print_nl /* nl for readability */
  227. movw $5, %cx /* error code + 4 registers */
  228. movw %sp,%bp
  229. print_loop:
  230. pushw %cx /* save count left */
  231. cmpb $5, %cl
  232. jae no_reg /* see if register name is needed */
  233. movw $0x0007, %bx /* page 0, attribute 7 (normal) */
  234. movw $0xe05+0x41-1, %ax
  235. subb %cl,%al
  236. int $0x10
  237. movb $0x58, %al /* 'X' */
  238. int $0x10
  239. movb $0x3A, %al /* ':' */
  240. int $0x10
  241. no_reg:
  242. addw $2, %bp /* next register */
  243. call print_hex /* print it */
  244. movb $0x20, %al /* print a space */
  245. int $0x10
  246. popw %cx
  247. loop print_loop
  248. call print_nl /* nl for readability */
  249. ret
  250. print_str:
  251. movw $0x0007, %bx /* page 0, attribute 7 (normal) */
  252. movb $0x0e, %ah /* write char, tty mode */
  253. prloop:
  254. lodsb
  255. int $0x10
  256. loop prloop
  257. ret
  258. print_nl:
  259. movw $0x0007, %bx /* page 0, attribute 7 (normal) */
  260. movw $0xe0d, %ax /* CR */
  261. int $0x10
  262. movb $0xa, %al /* LF */
  263. int $0x10
  264. ret
  265. /* print_hex prints the word pointed to by ss:bp in hexadecimal. */
  266. print_hex:
  267. movw (%bp),%dx /* load word into dx */
  268. movb $4, %cl
  269. movb $0x0e, %ah /* write char, tty mode */
  270. movw $0x0007, %bx /* page 0, attribute 7 (normal) */
  271. call print_digit
  272. call print_digit
  273. call print_digit
  274. /* fall through */
  275. print_digit:
  276. rol %cl,%dx /* rotate so that lowest 4 bits are used */
  277. movb $0x0f, %al /* mask for nybble */
  278. andb %dl,%al
  279. addb $0x90, %al /* convert al to ascii hex (four instructions) */
  280. daa
  281. adcb $0x40, %al
  282. daa
  283. int $0x10
  284. ret
  285. sread: .word 0 /* sectors read of current track */
  286. head: .word 0 /* current head */
  287. track: .word 0 /* current track */
  288. sectors:
  289. .word 0
  290. dpseg: .word 0
  291. dpoff: .word 0
  292. disksizes:
  293. .byte 36,18,15,9
  294. msg1:
  295. .ascii "Loading ROM image"
  296. msg1end:
  297. .org 510, 0
  298. .word 0xAA55
  299. start_runtime:
  300. call install
  301. /* Jump to .text16 segment */
  302. pushw %ax
  303. pushw $1f
  304. lret
  305. .section ".text16", "awx", @progbits
  306. 1:
  307. pushl $main
  308. pushw %cs
  309. call prot_call
  310. popl %eax /* discard */
  311. /* Boot next device */
  312. int $0x18