boot.asm 2.5 KB

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