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

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