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.

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