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