stage1.asm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. ;; This file is part of asmc, a bootstrapping OS with minimal seed
  2. ;; Copyright (C) 2018 Giovanni Mascellani <gio@debian.org>
  3. ;; https://gitlab.com/giomasce/asmc
  4. ;; This program is free software: you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. ;; This file includes snippets taken from http://wiki.osdev.org; by
  15. ;; https://wiki.osdev.org/OSDev_Wiki:Copyrights they are to be
  16. ;; considered in the public domain, or that the CC0 license applies, so
  17. ;; their inclusion in a GPL-3+ project should be ok.
  18. bits 16
  19. org 0x7c00
  20. SERIAL_PORT equ 0x3f8
  21. boot_disk equ 0x500
  22. PART1_START_LBA equ 0x7c00 + 0x1be + 8
  23. PART1_LENGTH equ 0x7c00 + 0x1be + 12
  24. PART2_START_LBA equ 0x7c00 + 0x1ce + 8
  25. PART2_LENGTH equ 0x7c00 + 0x1ce + 12
  26. PART3_START_LBA equ 0x7c00 + 0x1de + 8
  27. PART3_LENGTH equ 0x7c00 + 0x1de + 12
  28. PART4_START_LBA equ 0x7c00 + 0x1ee + 8
  29. PART4_LENGTH equ 0x7c00 + 0x1ee + 12
  30. cli
  31. mov ax, 0
  32. mov ds, ax
  33. mov es, ax
  34. mov fs, ax
  35. mov gs, ax
  36. mov ss, ax
  37. jmp 0:segments_set_up
  38. segments_set_up:
  39. mov sp, 0x7c00
  40. mov [boot_disk], dl
  41. call serial_setup16
  42. mov si, str_hello
  43. call print_string16
  44. ;; Check int 0x13 extensions
  45. mov si, str_check_13_exts
  46. call print_string16
  47. mov ah, 0x41
  48. mov bx, 0x55aa
  49. mov dl, 0x80
  50. int 0x13
  51. jc error16
  52. mov si, str_boot_disk
  53. call print_string16
  54. mov al, [boot_disk]
  55. call print_hex_char16
  56. mov si, str_newline
  57. call print_string16
  58. mov si, str_loading
  59. call print_string16
  60. mov eax, [PART1_START_LBA]
  61. mov [lba], eax
  62. load_stage2:
  63. cmp DWORD [PART1_LENGTH], 0
  64. je boot_stage2
  65. sub DWORD [PART1_LENGTH], 1
  66. call read_sect
  67. jc error16
  68. mov al, '.'
  69. call print_char16
  70. mov di, [buf]
  71. add WORD [buf], 512
  72. add WORD [lba], 1
  73. jmp load_stage2
  74. read_sect:
  75. mov dl, [boot_disk]
  76. mov si, dap
  77. mov ah, 0x42
  78. int 0x13
  79. ret
  80. boot_stage2:
  81. mov si, str_newline
  82. call print_string16
  83. mov si, str_booting
  84. call print_string16
  85. jmp stage2
  86. ;; Print character in AL
  87. print_char16:
  88. call serial_write_char16
  89. mov ah, 0x0e
  90. mov bh, 0x00
  91. mov bl, 0x07
  92. int 0x10
  93. ret
  94. ;; Print string pointed by SI
  95. print_string16:
  96. mov al, [si]
  97. inc si
  98. or al, al
  99. jz print_string16_ret
  100. call print_char16
  101. jmp print_string16
  102. print_string16_ret:
  103. ret
  104. print_hex_nibble16:
  105. and al, 0xf
  106. cmp al, 0xa
  107. jl print_hex_nibble16_decimal
  108. add al, 'a' - 10
  109. jmp print_hex_nibble16_print
  110. print_hex_nibble16_decimal:
  111. add al, '0'
  112. print_hex_nibble16_print:
  113. call print_char16
  114. ret
  115. print_hex_char16:
  116. mov cl, al
  117. shr al, 4
  118. call print_hex_nibble16
  119. mov al, cl
  120. call print_hex_nibble16
  121. ret
  122. platform16_panic:
  123. error16:
  124. mov si, str_panic
  125. call print_string16
  126. jmp $
  127. ;; void serial_setup()
  128. serial_setup16:
  129. ;; Send command as indicated in https://wiki.osdev.org/Serial_Port
  130. mov dx, SERIAL_PORT + 1
  131. mov ax, 0x00
  132. out dx, al
  133. mov dx, SERIAL_PORT + 3
  134. mov ax, 0x80
  135. out dx, al
  136. mov dx, SERIAL_PORT
  137. mov ax, 0x03
  138. out dx, al
  139. mov dx, SERIAL_PORT + 1
  140. mov ax, 0x00
  141. out dx, al
  142. mov dx, SERIAL_PORT + 3
  143. mov ax, 0x03
  144. out dx, al
  145. mov dx, SERIAL_PORT + 2
  146. mov ax, 0xc7
  147. out dx, al
  148. mov dx, SERIAL_PORT + 4
  149. mov ax, 0x0b
  150. out dx, al
  151. ;; mov ax, 11100011b
  152. ;; mov dx, 0
  153. ;; int 0x14
  154. ret
  155. ;; void serial_write_char16(al)
  156. ;; AL is preserved
  157. serial_write_char16:
  158. mov bl, al
  159. ;; Test until the serial is available for transmit
  160. mov dx, SERIAL_PORT + 5
  161. in al, dx
  162. and ax, 0x20
  163. cmp ax, 0
  164. je serial_write_char16
  165. ;; Actually write the char
  166. mov dx, SERIAL_PORT
  167. mov al, bl
  168. out dx, al
  169. ;; mov ah, 0x01
  170. ;; mov dx, 0
  171. ;; int 0x14
  172. ret
  173. dap:
  174. db 16
  175. db 0
  176. dw 1
  177. buf:
  178. dw 0x7e00
  179. dw 0
  180. lba:
  181. dd 0
  182. dd 0
  183. str_hello:
  184. db 'Into stage1!'
  185. str_newline:
  186. db 0xa, 0xd, 0
  187. str_panic:
  188. db 'PANIC!', 0xa, 0xd, 0
  189. str_loading:
  190. db 'Loading stage2', 0
  191. str_booting:
  192. db 'Booting stage2...', 0xa, 0xd, 0
  193. str_boot_disk:
  194. db 'Boot disk: ', 0
  195. str_check_13_exts:
  196. db 'Check int 0x13 exts...', 0xa, 0xd, 0
  197. times 510 - ($ - $$) db 0
  198. dw 0xAA55