boot.asm 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. ; Copyright (C) 2021 Rain
  2. ; This file is part of XNIX.
  3. ; XNIX is free software: you can redistribute it and/or modify
  4. ; it under the terms of the GNU General Public License as published by
  5. ; the Free Software Foundation, either version 3 of the License, or
  6. ; (at your option) and later version.
  7. ; XNIX is distributed in the hope that it will be useful,
  8. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. ; GNU General Public License for more details.
  11. ; You should have received a copy of the GNU General Public License
  12. ; along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  13. org 0x7c00
  14. cyldsneed equ 10
  15. ; yeah, it's really 0x7c00 because I don't want to use other memory.
  16. BOOTDRIVER equ 0x7c00
  17. _start:
  18. xor ax, ax
  19. mov ds, ax
  20. mov es, ax
  21. mov ss, ax
  22. mov sp, 0x7c00
  23. ; save startup driver
  24. mov [BOOTDRIVER], dl
  25. ; display message 'Loading System'
  26. mov si, boot_msg
  27. call print
  28. call read_it
  29. .set_vga:
  30. mov ax, 0x0013
  31. int 0x10
  32. jmp 0x0000:0x7e00
  33. print:
  34. cld
  35. lodsb
  36. test al,al
  37. jz .ret
  38. mov ah, 0x0e
  39. mov bx, 0x0f
  40. int 0x10
  41. jmp print
  42. .ret:
  43. ret
  44. ; NOTE: QEMU's HD only supports LBA mode. So this MBR can't load it successfully. :-)
  45. read_it:
  46. ; BIOS 0x13, AH = 0x02: read disk in CHS mode
  47. ; AL: sectors will read
  48. ; CH: cylinder
  49. ; CL: sector start
  50. ; DH: head
  51. ; DL: driver
  52. ; BX: buffer address (segment register ES)
  53. ; segment address
  54. mov ax, 0x07e0
  55. mov es, ax
  56. mov ch, 0
  57. mov dh, 0
  58. ; sector 1 is MBR (this code)
  59. mov cl, 2
  60. .retry:
  61. mov ah, 0x02
  62. ; only read one sectors
  63. mov al, 1
  64. mov bx, 0x0000
  65. mov dl, [BOOTDRIVER]
  66. int 0x13
  67. jc read_failed
  68. .next_sector:
  69. mov ax, es
  70. ; add 512 for memory address
  71. add ax, 512 / 16
  72. mov es, ax
  73. ; sector++
  74. add cl, 1
  75. ; if sector > 18
  76. cmp cl, 18
  77. jbe .retry
  78. ; next head
  79. mov cl, 1
  80. ; head++
  81. add dh, 1
  82. ; if head == 2 (head can only be 0 or 1)
  83. cmp dh, 2
  84. jb .retry
  85. ; next cylinder
  86. mov dh, 0
  87. add ch, 1
  88. ; if cylinder == CYLINDERS_NEED
  89. cmp ch, cyldsneed
  90. jb .retry
  91. ret
  92. read_failed:
  93. mov si, read_fail_msg
  94. call print
  95. .die: hlt
  96. jmp .die
  97. boot_msg: db "Loading System...", 0x0d, 0x0a, 0x00
  98. read_fail_msg: db "panic: read disk failed. ", 0x0d, 0x0a, 0x00
  99. times 510 - ($ - $$) db 0x00
  100. db 0x55, 0xaa