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

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