-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboot-floppy.S
146 lines (113 loc) · 2.12 KB
/
boot-floppy.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
.section .driver, "ax"
#define FAT_bytes_per_sector 0xb
#define FAT_sectors_per_cluster 0xd
#define FAT_reserved_sectors 0xe
#define FAT_fat_copies 0x10
#define FAT_root_entries 0x11
#define FAT_sectors_per_fat 0x16
#define FAT_sectors_per_track 0x18
#define FAT_filename 0
#define FAT_filename_ext 0x8
#define FAT_file_attr 0xb
#define FAT_file_cluster 0x1a
#define FAT_file_sze 0x1c
.global floppy_driver
.global dev_driver
/* Noreturn */
floppy_driver:
dev_driver:
sub $0x208, %esp
lea 8(%esp), %ebx
push %ebx
/* Fat16 load root */
//mov $0x7c00, %ecx
xor %ecx, %ecx
mov $0x7c, %ch
mov FAT_fat_copies(%ecx), %al
cbw
imul FAT_sectors_per_fat(%ecx), %ax
add FAT_reserved_sectors(%ecx), %ax
push %eax
//dec %eax
call load_sector
/* TODO: Verify this */
//mov $13, %eax
pop %ecx // root_begin
pop %ebp
/* Root loaded starting at ebp */
/* TODO: iterate over root (and maybe subdirs) to find kernel */
mov FAT_file_sze(%ebp), %edi
neg %edi
lea (%ebp, %edi), %esp
neg %edi
ljmp $0x18, $1f
1:
.code16
//xor %edx, %edx
//xor %ebx, %ebx
//mov (0x7c00 + FAT_sectors_per_cluster), %bl
//divb %bx, %ax
mov FAT_file_cluster(%bp), %ax
mulb (0x7c00 + FAT_sectors_per_cluster)
mov %ax, %si
/* cx = root_begin, si = C * S/C */
mov (0x7c00 + FAT_root_entries), %ax
mov (0x7c00 + FAT_bytes_per_sector), %bx
shr $5, %bx
xor %dx, %dx
div %bx, %ax
/* ax = root_size = root_entries * 0x20 / bytes/sectors */
mov %sp, %bp
add %si, %ax
add %cx, %ax
dec %ax
dec %ax
//dec %ax
mov %bp, %bx
ljmp $0x8, $1f
1:
.code32
push %ebx
mov %edi, %ecx
shr $9, %ecx
inc %ecx
load_file:
push %ecx
push %ebx
push %eax
call load_sector
// add 512, ebx
pop %eax
inc %eax
pop %ebx
add $512, %ebx
pop %ecx
loop load_file
mov $0x0e44, %ax
mov $0x10, %ebp
call bios_int
pop %edx
call loader_elf
mov $0x00007BF0, %esp
jmp *%eax
load_sector:
/* ebx = buf, %eax = sector number */
xor %ecx, %ecx
mov $0x7c, %ch
mov FAT_sectors_per_track(%ecx), %cx
xor %edx, %edx
//dec %cx
div %cx, %ax
inc %edx
mov %edx, %ecx // Sector in cl
// Hardcoded handling of 2 heads
xor %edx, %edx
mov %al, %ch
mov %ch, %dh
and $1, %dh
shr %ch
mov $0x201, %eax
//mov $2, %ecx
mov $0x13, %ebp
call bios_int
ret