boot.lst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. 1 ; Copyright (C) 2021 Rain
  2. 2
  3. 3 ; This file is part of XNIX.
  4. 4 ; XNIX is free software: you can redistribute it and/or modify
  5. 5 ; it under the terms of the GNU General Public License as published by
  6. 6 ; the Free Software Foundation, either version 3 of the License, or
  7. 7 ; (at your option) and later version.
  8. 8
  9. 9 ; XNIX is distributed in the hope that it will be useful,
  10. 10 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. 11 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. 12 ; GNU General Public License for more details.
  13. 13
  14. 14 ; You should have received a copy of the GNU General Public License
  15. 15 ; along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20 org 0x7c00
  21. 21
  22. 22 cyldsneed equ 10
  23. 23
  24. 24
  25. 25 ; yeah, it's really 0x7c00 because I don't want to use other memory.
  26. 26
  27. 27 BOOTDRIVER equ 0x7c00
  28. 28
  29. 29 _start:
  30. 30 00000000 31C0 xor ax, ax
  31. 31
  32. 32 00000002 8ED8 mov ds, ax
  33. 33 00000004 8EC0 mov es, ax
  34. 34
  35. 35 00000006 8ED0 mov ss, ax
  36. 36 00000008 BC007C mov sp, 0x7c00
  37. 37
  38. 38 ; save startup driver
  39. 39 0000000B 8816007C mov [BOOTDRIVER], dl
  40. 40
  41. 41 ; display message 'Loading System'
  42. 42
  43. 43 0000000F BE[7400] mov si, boot_msg
  44. 44 00000012 E80800 call print
  45. 45
  46. 46 00000015 E81500 call read_it
  47. 47
  48. 48 00000018 EA007E0000 jmp 0x0000:0x7e00
  49. 49
  50. 50
  51. 51
  52. 52 print:
  53. 53 0000001D FC cld
  54. 54 0000001E AC lodsb
  55. 55
  56. 56 0000001F 84C0 test al,al
  57. 57 00000021 7409 jz .ret
  58. 58
  59. 59 00000023 B40E mov ah, 0x0e
  60. 60 00000025 BB0F00 mov bx, 0x0f
  61. 61 00000028 CD10 int 0x10
  62. 62
  63. 63 0000002A EBF1 jmp print
  64. 64
  65. 65 .ret:
  66. 66 0000002C C3 ret
  67. 67
  68. 68
  69. 69 ; NOTE: QEMU's HD only supports LBA mode. So this MBR can't load it successfully. :-)
  70. 70
  71. 71 read_it:
  72. 72 ; BIOS 0x13, AH = 0x02: read disk in CHS mode
  73. 73 ; AL: sectors will read
  74. 74 ; CH: cylinder
  75. 75 ; CL: sector start
  76. 76 ; DH: head
  77. 77 ; DL: driver
  78. 78 ; BX: buffer address (segment register ES)
  79. 79
  80. 80 ; segment address
  81. 81 0000002D B8E007 mov ax, 0x07e0
  82. 82 00000030 8EC0 mov es, ax
  83. 83
  84. 84 00000032 B500 mov ch, 0
  85. 85 00000034 B600 mov dh, 0
  86. 86
  87. 87 ; sector 1 is MBR (this code)
  88. 88 00000036 B102 mov cl, 2
  89. 89
  90. 90 .retry:
  91. 91 00000038 B402 mov ah, 0x02
  92. 92
  93. 93 ; only read one sectors
  94. 94 0000003A B001 mov al, 1
  95. 95 0000003C BB0000 mov bx, 0x0000
  96. 96
  97. 97 0000003F 8A16007C mov dl, [BOOTDRIVER]
  98. 98
  99. 99 00000043 CD13 int 0x13
  100. 100
  101. 101 00000045 7224 jc read_failed
  102. 102
  103. 103 .next_sector:
  104. 104 00000047 8CC0 mov ax, es
  105. 105 ; add 512 for memory address
  106. 106 00000049 83C020 add ax, 512 / 16
  107. 107 0000004C 8EC0 mov es, ax
  108. 108
  109. 109 ; sector++
  110. 110 0000004E 80C101 add cl, 1
  111. 111 ; if sector > 18
  112. 112 00000051 80F912 cmp cl, 18
  113. 113 00000054 76E2 jbe .retry
  114. 114
  115. 115 ; next head
  116. 116 00000056 B101 mov cl, 1
  117. 117 ; head++
  118. 118 00000058 80C601 add dh, 1
  119. 119 ; if head == 2 (head can only be 0 or 1)
  120. 120 0000005B 80FE02 cmp dh, 2
  121. 121 0000005E 72D8 jb .retry
  122. 122
  123. 123 ; next cylinder
  124. 124 00000060 B600 mov dh, 0
  125. 125 00000062 80C501 add ch, 1
  126. 126
  127. 127 ; if cylinder == CYLINDERS_NEED
  128. 128 00000065 80FD0A cmp ch, cyldsneed
  129. 129 00000068 72CE jb .retry
  130. 130
  131. 131 0000006A C3 ret
  132. 132
  133. 133
  134. 134 read_failed:
  135. 135 0000006B BE[8800] mov si, read_fail_msg
  136. 136 0000006E E8ACFF call print
  137. 137
  138. 138 00000071 F4 .die: hlt
  139. 139 00000072 EBFD jmp .die
  140. 140
  141. 141
  142. 142 00000074 4C6F6164696E672053- boot_msg: db "Loading System...", 0x0d, 0x0a, 0x00
  143. 142 0000007D 797374656D2E2E2E0D-
  144. 142 00000086 0A00
  145. 143 00000088 70616E69633A207265- read_fail_msg: db "panic: read disk failed. ", 0x0d, 0x0a, 0x00
  146. 143 00000091 6164206469736B2066-
  147. 143 0000009A 61696C65642E200D0A-
  148. 143 000000A3 00
  149. 144
  150. 145
  151. 146 000000A4 00<rep 15Ah> times 510 - ($ - $$) db 0x00
  152. 147 000001FE 55AA db 0x55, 0xaa
  153. 148