-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboot.S
213 lines (185 loc) · 2.65 KB
/
boot.S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#define ROOT_CNT 0xE0
.code16
.section .text
.globl start
start:
jmp _start
nop
BPB:
MANUFACTRUE:
.ascii "DUDU "
BYTE_PER_SEC:
.word 0x200
SECS_PER_CLUS:
.byte 0x1
RSVED_SEC_CNT:
.word 0x1
FAT_NUMS:
.byte 0x02
ROOT_ENTRY_CNT:
.word ROOT_CNT
TLT_SECS:
.word 0xB40
MEDIA_DESC:
.byte 0xF0
SECS_PER_FAT:
.word 0x9
SECS_PER_TRACK:
.word 0x12
HEADS_CNT:
.word 0x02
HIDDEN_SECS:
.long 0x0
TTL_SECS_32:
.long 0x0
DRIVE_NUM:
.byte 0x0
RESERVED:
.byte 0x0
EXPAND_TAG:
.byte 0x29
VOL_LABEL:
.ascii "dudu"
VOL_ID:
.long 0x0
FILE_SYSTEM_TYPE:
.ascii "FAT16 "
BOOT_FILE:
.ascii "BOOT TXT"
#define DATA_AREA 0x9000
#define STACK_START 0x7c00
#define ROOT_DIR_SEC_START 0x13
#define ROOT_DIR_SECS (ROOT_CNT*32+511)/512
#define DATA_SEC_START (ROOT_DIR_SEC_START+ROOT_DIR_SECS-2)
#define SETUP_SEG 0x4000
_start:
real_start:
mov %cs,%ax
mov %ax,%es
mov %ax,%ds
mov %ax,%ss
mov $loading,%si
call print_msg
mov $STACK_START,%bp
mov $STACK_START,%sp
mov $DATA_AREA,%ebx
mov $ROOT_DIR_SEC_START,%eax
mov $ROOT_DIR_SECS,%ecx
call read_secs
mov $DATA_AREA,%esi
mov $BOOT_FILE,%edi
xor %ebx,%ebx
mov $0xb,%cx
call find_file
push %eax
mov $0x1,%eax
mov $DATA_AREA,%ebx
mov SECS_PER_FAT,%cx
call read_secs
pop %eax
mov $SETUP_SEG,%ebx
read_loader:
push %ebx
push %eax
cmp $0xfff,%eax
je 2f
add $DATA_SEC_START,%eax
mov $0x1,%ecx
call read_secs
pop %eax
mov $DATA_AREA,%ebx
call find_next_sec
pop %ebx
add $0x200,%ebx
jmp read_loader
2:
add $0x08,%esp
mov $SETUP_SEG,%si
call print_msg
jmp .
1:
mov $0x0e,%ah
movw $0x0010,%bx
int $0x10
print_msg:
lodsb
cmpb $0,%al
jne 1b
ret
2:
mov $0xb,%cx
inc %bx
mov $0x20,%eax
mul %bx
add %eax,%esi
cmp $ROOT_CNT,%bx
jge finish
mov $BOOT_FILE,%edi
mov $0x0b,%cx
jmp find_file
finish:
mov $0,%ax
ret
find_file:
lodsb
dec %esi
cmpb %al,(%di)
jne 2b
inc %esi
inc %di
sub $0x1,%cx
cmp $0,%cx
jne find_file
add $0x0f,%si
mov (%si),%ax
ret
find_next_sec:
mov %eax,%edx
mov $0x03,%ecx
mul %cl
shr $0x01,%eax
mov %eax,%esi
mov (%bx,%si),%cl
mov 1(%bx,%si),%ch
and $0x01,%edx
cmp $0,%edx
je EVEN
shrl $0x04,%ecx
jmp DONE
EVEN:
and $0x00000fff,%ecx
DONE:
mov %ecx,%eax
ret
read_secs:
xor %edx,%edx
push %cx
movb SECS_PER_TRACK,%cl
div %cl
mov %ah,%cl
inc %cl
mov %al,%ch
shrb $1,%ch
mov %al,%dh
andb $0x01,%dh
xor %dl,%dl
pop %ax
mov $0x02,%ah
int $0x13
jc error
mov $success,%si
call print_msg
ret
error:
push %ax
push %dx
mov $error_msg,%si
call print_msg
pop %dx
pop %ax
nop
success: .ascii ".. \0"
loading: .ascii "SpaceDUDU:loading...\r\n\0"
error_msg: .ascii "SpaceDUDU:error ... try again\r\n\0"
.org 510
.word 0xaa55