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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. #include "compiler.h"
  2. /* NOTE: this boot sector contains instructions that need at least an 80186.
  3. * Yes, as86 has a bug somewhere in the valid instruction set checks.
  4. *
  5. * SYS_SIZE is the number of clicks (16 bytes) to be loaded.
  6. */
  7. .equ SYSSIZE, 8192 # 8192 * 16 bytes = 128kB maximum size of .ROM file
  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. start_runtime:
  124. #ifdef COMPRESS
  125. /* Decompress runtime image. %es:0000 points to decompressed
  126. * image on exit.
  127. */
  128. lcall $SYSSEG, $decompress16
  129. #endif
  130. /* Set up internal environment. Address of entry-point
  131. * function is returned in %es:di.
  132. */
  133. pushw %es /* setup16 says %ds:0000 must point to image */
  134. popw %ds
  135. movw $setup16, %di
  136. pushw %cs
  137. call ljmp_to_es_di
  138. /* Call to arch_main. Register INT19 as an exit path. This
  139. * call will never return.
  140. */
  141. movl $exit_via_int19, %eax
  142. pushl $arch_main
  143. pushl %eax /* Dummy return address */
  144. /* Do the equivalent of ljmp *%es:di */
  145. ljmp_to_es_di:
  146. pushw %es
  147. pushw %di
  148. lret
  149. /* This routine loads the system at address SYSSEG<<4, making sure no 64kB
  150. * boundaries are crossed. We try to load it as fast as possible, loading whole
  151. * tracks whenever we can.
  152. *
  153. * in: es - starting address segment (normally SYSSEG)
  154. */
  155. read_it:
  156. movw $1,sread /* don't reload the prefix */
  157. movw %es,%ax
  158. testw $0x0fff, %ax
  159. die: jne die /* es must be at 64kB boundary */
  160. xorw %bx,%bx /* bx is starting address within segment */
  161. rp_read:
  162. movw %es,%ax
  163. movw %bx,%dx
  164. movb $4, %cl
  165. shrw %cl,%dx /* bx is always divisible by 16 */
  166. addw %dx,%ax
  167. cmpw $SYSSEG+SYSSIZE, %ax /* have we loaded all yet? */
  168. jb ok1_read
  169. ret
  170. ok1_read:
  171. movw sectors, %ax
  172. subw sread, %ax
  173. movw %ax,%cx
  174. shlw $9, %cx
  175. addw %bx,%cx
  176. jnc ok2_read
  177. je ok2_read
  178. xorw %ax,%ax
  179. subw %bx,%ax
  180. shrw $9, %ax
  181. ok2_read:
  182. call read_track
  183. movw %ax,%cx
  184. addw sread, %ax
  185. cmpw sectors, %ax
  186. jne ok3_read
  187. movw $1, %ax
  188. subw head, %ax
  189. jne ok4_read
  190. incw track
  191. ok4_read:
  192. movw %ax, head
  193. xorw %ax,%ax
  194. ok3_read:
  195. movw %ax, sread
  196. shlw $9, %cx
  197. addw %cx,%bx
  198. jnc rp_read
  199. movw %es,%ax
  200. addb $0x10, %ah
  201. movw %ax,%es
  202. xorw %bx,%bx
  203. jmp rp_read
  204. read_track:
  205. pusha
  206. pushw %ax
  207. pushw %bx
  208. pushw %bp /* just in case the BIOS is buggy */
  209. movw $0x0e2e, %ax /* 0x2e = . */
  210. movw $0x0007, %bx
  211. int $0x10
  212. popw %bp
  213. popw %bx
  214. popw %ax
  215. movw track, %dx
  216. movw sread, %cx
  217. incw %cx
  218. movb %dl,%ch
  219. movw head, %dx
  220. movb %dl,%dh
  221. andw $0x0100, %dx
  222. movb $2, %ah
  223. pushw %dx /* save for error dump */
  224. pushw %cx
  225. pushw %bx
  226. pushw %ax
  227. int $0x13
  228. jc bad_rt
  229. addw $8, %sp
  230. popa
  231. ret
  232. bad_rt: pushw %ax /* save error code */
  233. call print_all /* ah = error, al = read */
  234. xorb %ah,%ah
  235. xorb %dl,%dl
  236. int $0x13
  237. addw $10, %sp
  238. popa
  239. jmp read_track
  240. /* print_all is for debugging purposes. It will print out all of the registers.
  241. * The assumption is that this is called from a routine, with a stack frame like
  242. * dx
  243. * cx
  244. * bx
  245. * ax
  246. * error
  247. * ret <- sp
  248. */
  249. print_all:
  250. call print_nl /* nl for readability */
  251. movw $5, %cx /* error code + 4 registers */
  252. movw %sp,%bp
  253. print_loop:
  254. pushw %cx /* save count left */
  255. cmpb $5, %cl
  256. jae no_reg /* see if register name is needed */
  257. movw $0x0007, %bx /* page 0, attribute 7 (normal) */
  258. movw $0xe05+0x41-1, %ax
  259. subb %cl,%al
  260. int $0x10
  261. movb $0x58, %al /* 'X' */
  262. int $0x10
  263. movb $0x3A, %al /* ':' */
  264. int $0x10
  265. no_reg:
  266. addw $2, %bp /* next register */
  267. call print_hex /* print it */
  268. movb $0x20, %al /* print a space */
  269. int $0x10
  270. popw %cx
  271. loop print_loop
  272. call print_nl /* nl for readability */
  273. ret
  274. print_str:
  275. movw $0x0007, %bx /* page 0, attribute 7 (normal) */
  276. movb $0x0e, %ah /* write char, tty mode */
  277. prloop:
  278. lodsb
  279. int $0x10
  280. loop prloop
  281. ret
  282. print_nl:
  283. movw $0x0007, %bx /* page 0, attribute 7 (normal) */
  284. movw $0xe0d, %ax /* CR */
  285. int $0x10
  286. movb $0xa, %al /* LF */
  287. int $0x10
  288. ret
  289. /* print_hex prints the word pointed to by ss:bp in hexadecimal. */
  290. print_hex:
  291. movw (%bp),%dx /* load word into dx */
  292. movb $4, %cl
  293. movb $0x0e, %ah /* write char, tty mode */
  294. movw $0x0007, %bx /* page 0, attribute 7 (normal) */
  295. call print_digit
  296. call print_digit
  297. call print_digit
  298. /* fall through */
  299. print_digit:
  300. rol %cl,%dx /* rotate so that lowest 4 bits are used */
  301. movb $0x0f, %al /* mask for nybble */
  302. andb %dl,%al
  303. addb $0x90, %al /* convert al to ascii hex (four instructions) */
  304. daa
  305. adcb $0x40, %al
  306. daa
  307. int $0x10
  308. ret
  309. sread: .word 0 /* sectors read of current track */
  310. head: .word 0 /* current head */
  311. track: .word 0 /* current track */
  312. sectors:
  313. .word 0
  314. dpseg: .word 0
  315. dpoff: .word 0
  316. disksizes:
  317. .byte 36,18,15,9
  318. msg1:
  319. .ascii "Loading ROM image"
  320. msg1end:
  321. .org 510, 0
  322. .word 0xAA55