boot.asm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. ; yeah, it's really 0x7c00 because I don't want to use other memory.
  15. BOOTDRIVER equ 0x7c00
  16. _start:
  17. xor ax, ax
  18. mov ds, ax
  19. mov es, ax
  20. mov ss, ax
  21. mov sp, 0x7c00
  22. ; save startup driver
  23. mov [BOOTDRIVER], dl
  24. ; display message 'Loading System'
  25. mov si, boot_msg
  26. call print
  27. xor ax, ax
  28. mov bx, 0x7e00
  29. .read_it:
  30. inc ax
  31. mov cl, 1
  32. push ax
  33. push bx
  34. call read_sector
  35. pop bx
  36. pop ax
  37. add bx, 512
  38. cmp ax, 64
  39. jne .read_it
  40. jmp 0x0000:0x7e00
  41. print:
  42. cld
  43. lodsb
  44. test al,al
  45. jz .ret
  46. mov ah, 0x0e
  47. mov bx, 0x0f
  48. int 0x10
  49. jmp print
  50. .ret:
  51. ret
  52. read_sector:
  53. ; LBA address in AX, and sectors count in CL, buffer in ES:BX
  54. ; LBA to CHS:
  55. ; TEMP = LBA / SECTORS_PER_TRACK
  56. ; SECTOR = (LBA % SECTORS_PER_TRACK) + 1
  57. ; CYLINDER = TEMP >> 1
  58. ; HEAD = TEMP & 1
  59. ; DIV: unsigned divide
  60. ; 12345 / 123:
  61. ; mov ax, 12345 : dividend
  62. ; mov bl, 123 : divisor
  63. ; AL: quotient
  64. ; AH: remainder
  65. ; 18: sectors per track for 1.44M floppy (and for my u-disk)
  66. ; save bx first
  67. push bx
  68. mov bl, 18
  69. div bl
  70. ; BIOS standard interrupt: 0x13 AH = 0x02: read disk in CHS mode
  71. ; AL: sectors count
  72. ; CH: cylinder number low
  73. ; CL: 0-5b: sector number, 6-7b: cylinder number (disk only)
  74. ; DH: head
  75. ; DL: driver
  76. ; ES:BX: buffer
  77. ; SECTOR = AH + 1
  78. ; but CL used to save sectors count, so we save it first.
  79. push cx
  80. ; we inc ah because it's shorter than inc cl
  81. inc ah
  82. mov cl, ah
  83. ; CH: CYLINDER = (LBA / SECTORS_PER_TRACK) >> 1
  84. mov dh, al
  85. and dh, 1
  86. shr al, 1
  87. mov ch, al
  88. ; for 'push cx'
  89. pop ax
  90. ; for 'push bx'
  91. pop bx
  92. mov dl, [BOOTDRIVER]
  93. ; okay, now we read it
  94. mov ah, 2
  95. int 0x13
  96. jc read_failed
  97. ret
  98. read_failed:
  99. mov si, readdisk_fail_msg
  100. call print
  101. .die: hlt
  102. jmp .die
  103. ; it never go here
  104. boot_msg: db "Loading System...", 0x0d, 0x0a, 0x00
  105. readdisk_fail_msg: db "panic: read disk failed. ", 0x0d, 0x0a, 0x00
  106. times 510 - ($ - $$) db 0x00
  107. db 0x55, 0xaa