bootloader.asm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. [BITS 16]
  2. [ORG 0x7C00]
  3. start:
  4. ; Set up segments
  5. xor ax, ax
  6. mov ds, ax
  7. mov es, ax
  8. mov ss, ax
  9. mov sp, 0x7C00
  10. ; Save boot drive passed in dl by the BIOS
  11. mov [boot_drive], dl
  12. ; Clear screen
  13. mov ah, 0x00
  14. mov al, 0x03
  15. int 0x10
  16. ; Print boot drive number in dl
  17. mov al, dl
  18. call print_hex_byte
  19. mov ah, 0x0E
  20. mov al, 13
  21. int 0x10
  22. mov al, 10
  23. int 0x10
  24. ; Print startup messages
  25. mov si, msg_start
  26. call print_string
  27. mov si, msg_key
  28. call print_string
  29. mov si, msg_drm
  30. call print_string
  31. mov si, msg_kernel
  32. call print_string
  33. ; Check if Extensions are present
  34. mov dl, [boot_drive]
  35. mov ah, 0x41
  36. mov bx, 0x55AA
  37. int 0x13
  38. jc load_chs
  39. cmp bx, 0xAA55
  40. jne load_chs
  41. ; Load Stage 2 from disk using LBA read
  42. mov dl, [boot_drive]
  43. mov ah, 0x42
  44. mov si, dap
  45. int 0x13
  46. jc disk_error
  47. jmp jump_to_stage2
  48. load_chs:
  49. mov al, 7 ; Read 7 sectors
  50. mov bx, 0x7E00 ; Destination offset
  51. call read_sectors_chs
  52. jc disk_error
  53. jump_to_stage2:
  54. mov dl, [boot_drive]
  55. jmp 0x0000:0x7E00
  56. disk_error:
  57. mov al, ah
  58. call print_hex_byte
  59. mov ah, 0x0E
  60. mov al, 13
  61. int 0x10
  62. mov al, 10
  63. int 0x10
  64. mov si, msg_err
  65. call print_string
  66. halt:
  67. hlt
  68. jmp halt
  69. print_hex_byte:
  70. push ax
  71. push cx
  72. mov cl, 4
  73. shr al, cl
  74. call print_nibble
  75. pop ax
  76. push ax
  77. call print_nibble
  78. pop cx
  79. pop ax
  80. ret
  81. print_nibble:
  82. and al, 0x0F
  83. add al, '0'
  84. cmp al, '9'
  85. jbe .print
  86. add al, 7
  87. .print:
  88. mov ah, 0x0E
  89. int 0x10
  90. ret
  91. ; --- Functions ---
  92. print_string:
  93. mov ah, 0x0E ; Teletype output
  94. .loop:
  95. lodsb
  96. or al, al
  97. jz .done
  98. int 0x10
  99. jmp .loop
  100. .done:
  101. ret
  102. read_sectors_chs:
  103. push es
  104. push ax ; Save number of sectors to read
  105. push bx ; Save destination offset
  106. ; 1. Get drive parameters
  107. mov ah, 0x08
  108. mov dl, [boot_drive]
  109. xor di, di ; ES:DI = 0:0 to prevent BIOS bugs
  110. mov es, di
  111. int 0x13
  112. jc .use_default
  113. ; Save H_count (dh + 1) in bl, and S_track (cl & 0x3F) in bh
  114. movzx bx, dh
  115. inc bx ; bx = H_count
  116. and cx, 0x003F ; cx = S_track
  117. jcxz .use_default
  118. test bx, bx
  119. jz .use_default
  120. jmp .calc_chs
  121. .use_default:
  122. mov cx, 63 ; default sectors per track
  123. mov bx, 16 ; default heads
  124. .calc_chs:
  125. ; 2. Perform LBA to CHS calculation for LBA 34
  126. mov ax, 34 ; ax = LBA
  127. xor dx, dx
  128. div cx ; ax = LBA / S_track, dx = LBA % S_track
  129. ; Sector S = (LBA % S_track) + 1
  130. mov cl, dl
  131. inc cl ; cl = Sector S (bits 0-5)
  132. ; Head H = (LBA / S_track) % H_count
  133. xor dx, dx
  134. div bx ; ax = Cylinder C (should be 0), dx = Head H
  135. mov dh, dl ; dh = Head H
  136. ; Cylinder C is 0, so ch = 0, and cl bits 6-7 are 0
  137. mov ch, 0
  138. ; Restore destination offset to bx, and pop ax
  139. pop bx ; es:bx = destination buffer
  140. pop ax ; al = number of sectors to read (7)
  141. pop es ; Restore original ES
  142. ; Load boot drive to dl
  143. mov dl, [boot_drive]
  144. ; Call CHS Read
  145. mov ah, 0x02
  146. int 0x13
  147. ret
  148. chs_fail:
  149. pop bx
  150. pop ax
  151. pop es
  152. stc
  153. ret
  154. ; --- Data ---
  155. msg_start: db "Welcome to ArkOS Bootloader", 13, 10, 0
  156. msg_key: db "Checking Hardware Secure Key... OK", 13, 10, 0
  157. msg_drm: db "Loading DRM Module... OK", 13, 10, 0
  158. msg_kernel: db "Loading Kernel & Launching OS...", 13, 10, 0
  159. msg_err: db "Disk Error loading Stage 2!", 13, 10, 0
  160. boot_drive: db 0
  161. align 4
  162. dap:
  163. db 0x10 ; size of DAP
  164. db 0 ; zero
  165. dw 7 ; number of sectors to read
  166. dw 0x7E00 ; offset
  167. dw 0x0000 ; segment
  168. dq 34 ; LBA 34 (Stage 2 starts at sector 34 in hybrid GPT layout)
  169. times 446-($-$$) db 0
  170. ; Partition table placeholder (4 partitions * 16 bytes = 64 bytes)
  171. times 64 db 0
  172. ; Boot signature
  173. dw 0xAA55