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.

258 lines
6.7 KiB

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