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.

334 lines
8.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. ORG 0x7C00
  2. BITS 16
  3. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4. ; SLRboot
  5. ; Bootloader for SingOS
  6. ; version 0.2.0.3-exp
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  8. cli
  9. jmp long 0x0000:start
  10. start:
  11. mov ax, 0x1000
  12. mov ss, ax
  13. mov sp, 0xb000
  14. mov ax, 0
  15. mov ds, ax
  16. mov es, ax
  17. mov [disk_identifier], dl
  18. sti
  19. mov si, 0x7c00
  20. mov di, 0x600
  21. mov cx, 0x100
  22. rep movsw
  23. mov bx, relocated
  24. sub bx, 0x7600 ; Calculate the offset.
  25. push bx
  26. ret ; Return far
  27. ;jmp long 0:bx
  28. relocated:
  29. mov si, message ; Put address of the null-terminated string to output into 'si'
  30. call print ; Call our string-printing routine
  31. ; Check for more than one active partition that we can start from.
  32. xor bx, bx
  33. xor cx, cx
  34. mov ax, [partition_1.active_partition]
  35. cmp ax, 0x80
  36. jne check_p_2
  37. mov bx, 0x01 ; Partion 1 active
  38. inc cx
  39. check_p_2:
  40. mov ax, [partition_2.active_partition]
  41. cmp ax, 0x80
  42. jne check_p_3
  43. inc cx
  44. mov bx, 0x02 ; Partion 2 active
  45. check_p_3:
  46. mov ax, [partition_3.active_partition]
  47. cmp ax, 0x80
  48. jne check_p_4
  49. inc cx
  50. mov bx, 0x03 ; Partion 3 active
  51. check_p_4:
  52. mov ax, [partition_4.active_partition]
  53. cmp ax, 0x80
  54. jne eval_active_partion_number
  55. inc cx
  56. mov bx, 0x04 ; Partion 4 active
  57. eval_active_partion_number:
  58. cmp cx, 1
  59. je boot_partition
  60. ; here goes wait call, for the user to enter debug mode.
  61. ; Wating for 2 seconds:
  62. mov ah, 0x86 ; code for waiting interupt call
  63. mov cx, 0x001e ; high count of microseconds
  64. mov dx, 0x8480 ; low count
  65. int 0x15
  66. .busy_wait_for_key:
  67. xor ax, ax
  68. mov ah, 0x01 ; BIOS call to wait for key
  69. int 0x16
  70. boot_partition:
  71. ; IMPORTANT bx, has to hold the value 1-4 for the partiotion that wanted to be booted.
  72. mov bx, 2
  73. mov ax, partition_1
  74. .loop:
  75. cmp bx, 1
  76. je break
  77. add ax, 0x10
  78. ;call dumpax
  79. dec bx
  80. jmp .loop
  81. break:
  82. add ax, 8 ; This is the offset to fetch the LBA start adress of the partition record
  83. mov bx, ax
  84. xor ax, ax
  85. mov ax, [bx] ; first part of LBA
  86. mov WORD [DAPACK.lba_addr_dw_low], ax
  87. call dumpax
  88. add bx, 2 ; next part
  89. mov ax, [bx]
  90. mov WORD [DAPACK.lba_addr_dw_low + 1], ax
  91. call dumpax
  92. cli
  93. hlt
  94. mov WORD [DAPACK.blkcnt], 0x01
  95. mov WORD [DAPACK.db_addr_segment], 0x0000
  96. mov WORD [DAPACK.db_addr_offset], 0x7c00
  97. mov si, DAPACK ; address of "disk address packet"
  98. xor ax, ax
  99. mov ah, 0x42 ; READ
  100. mov dl, [disk_identifier]
  101. int 0x13
  102. jmp long 0x0:0x7c00
  103. cli
  104. hlt
  105. %IF 0
  106. cli
  107. jmp long 0x0000:start
  108. start:
  109. xor ax, ax
  110. mov ds, ax
  111. mov es, ax
  112. mov ss, ax
  113. mov [disk_identifier], dl
  114. ; initialize stack
  115. ; Set up 4K stack after this bootloader
  116. ; [Remember: Effective Address = Segment*16 + Offset]
  117. mov ax, 0x7C0 ; Set 'ax' equal to the location of this bootloader divided by 16
  118. add ax, 0x20 ; Skip over the size of the bootloader divided by 16
  119. mov ss, ax ; Set 'ss' to this location (the beginning of our stack region)
  120. mov sp, 8192 ; Set 'ss:sp' to the top of our 8K stack
  121. sti
  122. mov si, message ; Put address of the null-terminated string to output into 'si'
  123. call print ; Call our string-printing routine
  124. mov si, enter_debug_mode
  125. call print
  126. call printCRLF
  127. ;%IF 0
  128. ; here goes wait call, for the user to enter debug mode.
  129. ; Wating for 2 seconds:
  130. mov ah, 0x86 ; code for waiting interupt call
  131. mov cx, 0x001e ; high count of microseconds
  132. mov dx, 0x8480 ; low count
  133. int 0x15
  134. .busy_wait_for_key:
  135. xor ax, ax
  136. mov ah, 0x01 ; BIOS call to wait for key
  137. int 0x16
  138. jnz debug_mode
  139. ; entering system check:
  140. mov si, enter_system_check
  141. mov si, sys_check_ok ; Put address of the null-terminated string to output into 'si'
  142. call print ; Call our string-printing routine
  143. mov si, boot_system ; Put address of the null-terminated string to output into 'si'
  144. call print ; Call our string-printing routine
  145. ;This goes first as to now overwrite %ah and %al.
  146. ;mov ax, 0x0050
  147. ;mov es, ax ;Section to write into
  148. ;mov ah, 0x2 ;Read sectors from drive
  149. ;mov al, 0x08 ;Number of sectors to read (31 * 512 = 15872 bytes)
  150. ;mov ch, 0x0 ;Low 8 bits of cylinder
  151. ;mov cl, 0x11 ;First sector to read (bits 0-5), upper bits of cylinder (bits 6-7)
  152. ;mov dh, 0x0 ;Head number
  153. ;mov dl, [disk_identifier] ;Drive number
  154. ;mov bx, 0x0000 ;Offset into section
  155. ;int 0x13 ;Low level disk services
  156. ;jnc notcarry
  157. ;mov si, error_str
  158. ;call print
  159. ;jmp endcarrycheck
  160. notcarry:
  161. ;mov si, success_str
  162. ;call print
  163. ;call printCRLF
  164. mov ax, 0x0
  165. mov ds, ax
  166. mov ax, 89 ; lba adress
  167. jmp 0x50:0x0000 ; Jump to the kernel
  168. endcarrycheck:
  169. cli ; Clear the Interrupt Flag (disable external interrupts)
  170. hlt ; Halt the CPU (until the next external interrupt)
  171. debug_mode:
  172. mov si, .welcome_debug
  173. call print
  174. cli ; Clear the Interrupt Flag (disable external interrupts)
  175. hlt
  176. .welcome_debug db 'This is debug mode', 0
  177. printCRLF:
  178. mov ah, 0xE
  179. mov al, 13
  180. int 0x10
  181. mov al, 10
  182. int 0x10
  183. ret
  184. %ENDIF
  185. ;Routine for printing a 'ax' as hex
  186. dumpax:
  187. pusha ; save registers
  188. mov bx, ax
  189. mov ah, 0xE ; Teletype output
  190. mov cx, 4 ; 4 nipples in a 16 bit word
  191. .loop:
  192. rol bx, 4 ; rotate to next nipple
  193. mov al, bl ; we copy to al because we need to mask only the low 4 bits
  194. and al, 1111b ; Do the masking
  195. add al, '0' ; convert to ASCII
  196. cmp al, '9' ; If we are greater than 9 ascii, we add 7 to make digit 10 be represented as 'A'
  197. jbe .skip ; -|-
  198. add al, 7 ; -|-
  199. .skip: ; -|-
  200. int 0x10 ; BIOS call 'output'
  201. loop .loop
  202. popa ; restore registers
  203. ret
  204. ; Routine for outputting string in 'si' register to screen
  205. print:
  206. mov ah, 0xE ; Specify 'int 0x10' 'teletype output' function
  207. ; [AL = Character, BH = Page Number, BL = Colour (in graphics mode)]
  208. .printchar:
  209. lodsb ; Load byte at address SI into AL, and increment SI
  210. cmp al, 0
  211. je .done ; If the character is zero (NUL), stop writing the string
  212. int 0x10 ; Otherwise, print the character via 'int 0x10'
  213. jmp .printchar ; Repeat for the next character
  214. .done:
  215. ret
  216. data:
  217. message db 'SLRboot for SingOS! v0.2.0.3-exp',13,10,0
  218. ;enter_debug_mode db 'Press d to enter bootloader debug mode',13,10,0
  219. ;enter_system_check db 'Performing system check:',13,10,0
  220. ;sys_check_ok db 'System check ok', 13, 10, 0
  221. ;boot_system db 'Read SingOS from disk', 13, 10, 0
  222. ;error_str db 'Error', 0
  223. ;success_str db 'Success', 0
  224. boot_this_partition: dw 0
  225. disk_identifier db 0
  226. DAPACK:
  227. .dap_Size: db 0x10 ; This is always 16 bytes (0x10)
  228. .rev_byte: db 0x0 ; reserved byte, should always be zero
  229. .blkcnt: dw 0x0 ; int 13 resets this to # of blocks actually read/written
  230. .db_addr_offset: dw 0x0 ; memory buffer destination address (0:7c00)
  231. .db_addr_segment: dw 0x0 ; in memory page zero
  232. .lba_addr_dw_low: dd 0x0 ; put the lba to read in this spot
  233. .lba_addr_dw_high: dd 0x0 ; more storage bytes only for big lba's ( > 4 bytes )
  234. ; Pad to 510 bytes (boot sector size minus 2) with 0s, and finish with the two-byte standard boot signature
  235. times 446-($-$$) db 0 ; First partion entry
  236. partition_1:
  237. .active_partition db 0x80
  238. .start_CHS db 0x00, 0x00, 0x11;0x20, 0x21, 0x00
  239. .disk_type db 0x83
  240. .end_CHS db 0x8C, 0x3D, 0x0F
  241. .start_LBA db 0x10, 0x00, 0x00, 0x00
  242. .size db 0x00, 0xC8, 0x03, 0x00
  243. partition_2:
  244. .active_partition db 0x80
  245. .start_CHS db 0x8C, 0x3E, 0x0F
  246. .disk_type db 0x83
  247. .end_CHS db 0xFF, 0xFF, 0xFF
  248. .start_LBA db 0x00, 0xD0, 0x03, 0x00
  249. .size db 0x00, 0x28, 0x1C, 0x03
  250. partition_3:
  251. .active_partition db 0x00
  252. .start_CHS db 0x00, 0x00, 0x00
  253. .disk_type db 0x00
  254. .end_CHS db 0x00, 0x00, 0x00
  255. .start_LBA db 0x00, 0x00, 0x00, 0x00
  256. .size db 0x00, 0x00, 0x00, 0x00
  257. partition_4:
  258. .active_partition db 0x00
  259. .start_CHS db 0x00, 0x00, 0x00
  260. .disk_type db 0x00
  261. .end_CHS db 0x00, 0x00, 0x00
  262. .start_LBA db 0x00, 0x00, 0x00, 0x00
  263. .size db 0x00, 0x00, 0x00, 0x00
  264. times 510-($-$$) db 0
  265. dw 0xAA55 ; => 0x55 0xAA (little endian byte order)
  266. ; bootloder debug_mode goes here
  267. times 8192-($-$$) db 0
  268. ; mov si, 0x7be
  269. ; cmp byte [si], al
  270. ; jne 0x33
  271. ; add si, 0x10
  272. ; cmp si, 0x7fe
  273. ; jne 0x24
  274. ; jmp 0x49
  275. ; mov ah, 2
  276. ; mov al, 1
  277. ; mov bx, 0x7c00
  278. ; mov dl, 0x80
  279. ; mov dh, byte [si + 1]
  280. ; mov cx, word [si + 2]
  281. ; int 0x13
  282. ; jmp long 0:0x7c00
  283. ; jmp 0x49