-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.c
259 lines (201 loc) · 5.66 KB
/
kernel.c
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <stdint.h>
#include <gdt.h>
#include <screen.h>
#include <io.h>
/* defined in kernel_helpers.s */
extern uint32_t get_eax(void);
extern uint32_t get_ebx(void);
extern uint32_t get_ecx(void);
extern uint32_t get_edx(void);
extern uint32_t get_esi(void);
extern uint32_t get_edi(void);
#define BOOT_ADDR 0x7c00
/* outputs a character to the debug console, if "port_e9_hack: enabled=1" is set in config */
#define BOCHS_PRINT_CHAR(c) outportb(0xe9, c)
/* stops simulation and breaks into the debug console */
#define BOCHS_BREAK() outportw(0x8A00,0x8A00); outportw(0x8A00,0x08AE0);
typedef struct {
/* jmp short + nop + oem */
uint8_t jmp; /* jmp instruction */
uint8_t jmp_target; /* target of jmp instruction */
uint8_t nop; /* the nop instruction */
uint8_t oem[8]; /* oem name, 8 bytes */
/* BPB 2.0 */
uint16_t bytes_per_logical_sector;
uint8_t logical_sectors_per_cluster;
uint16_t reserved_logical_sectors;
uint8_t number_of_fats;
uint16_t root_directory_entries;
uint16_t total_logical_sectors;
uint8_t media_descriptor;
uint16_t logical_sectors_per_fat;
/* End BPB 2.0 */
/* BPB 3.31 */
uint16_t physical_sectors_per_track;
uint16_t heads_per_cylinder;
uint32_t hidden_sectors_count;
uint32_t total_logical_sectors_including_hidden;
/* End BPB 3.31 */
/* EBPB - Extended BIOS Parameter Block */
uint8_t drive_number;
uint8_t reserved;
uint8_t extended_boot_signature; /* should be 0x29 */
uint8_t volume_id[4];
uint8_t partition_volume_label[11];
uint8_t filesystem_type[8];
} __attribute__((packed)) bpb_t;
void read_bpb() {
bpb_t* bpb = (bpb_t*)BOOT_ADDR;
printstr("OEM: <");
printstrl((char*)&bpb->oem, 8);
printstr(">\n");
printstr("Bytes per logical sector: ");
printw(bpb->bytes_per_logical_sector);
printstr("\n");
printstr("Logical sectors per cluster: ");
printb(bpb->logical_sectors_per_cluster);
printstr("\n");
printstr("Reserved logical sectors: ");
printw(bpb->reserved_logical_sectors);
printstr("\n");
printstr("Number of FATs: ");
printb(bpb->number_of_fats);
printstr("\n");
printstr("Root directory entries: ");
printw(bpb->root_directory_entries);
printstr("\n");
printstr("Total logical sectors: ");
printw(bpb->total_logical_sectors);
printstr("\n");
printstr("Media descriptor: 0x");
printhw(bpb->media_descriptor);
printstr("\n");
printstr("Logical sectors per FAT: ");
printw(bpb->logical_sectors_per_fat);
printstr("\n");
printstr("Physical sectors per track: ");
printw(bpb->physical_sectors_per_track);
printstr("\n");
printstr("Heads per cylinder: ");
printw(bpb->heads_per_cylinder);
printstr("\n");
printstr("Hidden sectors count: ");
printl(bpb->hidden_sectors_count);
printstr("\n");
printstr("Total logical sectors including hidden: ");
printl(bpb->total_logical_sectors_including_hidden);
printstr("\n");
printstr("Drive number: ");
printb(bpb->drive_number);
printstr("\n");
printstr("Boot signature number: 0x");
printhb(bpb->extended_boot_signature);
printstr("\n");
printstr("Volume label: <");
printstrl((char*)&bpb->partition_volume_label, 11);
printstr(">\n");
printstr("Filesystem type: <");
printstrl((char*)&bpb->filesystem_type, 8);
printstr(">\n");
}
void dump_registers() {
printstr("EAX: ");
uint32_t eax = get_eax();
printhl(eax);
printstr(" EBX: ");
uint32_t ebx = get_ebx();
printhl(ebx);
printstr(" ECX: ");
uint32_t ecx = get_ecx();
printhl(ecx);
printstr(" EDX: ");
uint32_t edx = get_edx();
printhl(edx);
printstr("\n");
printstr("ESI: ");
uint32_t esi = get_esi();
printhl(esi);
printstr(" EDI: ");
uint32_t edi = get_edi();
printhl(edi);
printstr("\n");
}
void read_gdt() {
gdtr_t gdtr;
get_gdt(&gdtr);
printstr("Size of GDT: ");
printw(gdtr.size + 1);
printstr(" bytes\n");
uint16_t i, entries = (gdtr.size + 1)/sizeof(gdt_t);
/* entry 0 is useless -- null descriptor */
for (i = 1; i < entries; i++) {
printstr("Entry: ");
printw(i);
printstr(": ");
gdt_t* entry = &gdtr.entries[i];
uint32_t limit = ((entry->flags & 0xF) << 16) | entry->limit1;
uint32_t base = (entry->base3 << 24) | (entry->base2 << 16) | entry->base1;
printstr("Base: 0x");
printhl(base);
printstr(" Limit: 0x");
printhl(limit);
printstr(" Acc: 0x");
printhb(entry->access_byte);
printstr(" Flags: 0x");
printhb(entry->flags >> 4);
printstr("\n");
}
}
void init_gdt() {
gdt_t entries[3];
entries[1].limit1 = 0xffff;
entries[1].base1 = 0x0000;
entries[1].base2 = 0x00;
entries[1].access_byte = 0x9a;
entries[1].flags = 0xcf;
entries[1].base3 = 0x00;
entries[2].limit1 = 0xffff;
entries[2].base1 = 0x0000;
entries[2].base2 = 0x00;
entries[2].access_byte = 0x92;
entries[2].flags = 0xcf;
entries[2].base3 = 0x00;
gdtr_t gdtr = {
.size = 23,
.entries = &entries[0]
};
gdt_create_entry(&entries[1], 0x00000000, 0x000fffff, 0x92, 0xcf);
gdt_create_entry(&entries[2], 0x00000000, 0x000fffff, 0x92, 0xcf);
set_gdt(&gdtr, 0x08, 0x10);
}
void kernel_main()
{
clear_screen();
/* magic breakpoint for bochs */
__asm__ __volatile__("xchg %bx, %bx");
read_gdt();
/*char s[] = "Welcome to DavidOS!";
uint8_t i;
for (i = 0; i < 19; i++) {
screen_print(s[i], row, col++);
}
row++;
col = 0;*/
// printstr("Welcome to David OS.\n");
/*static char* s2 = "Welcome to David OS!\n";
printstr(s2);*/
read_bpb();
dump_registers();
uint32_t u, v, w;
for (w = 0; w < 5; w++) {
for (u = 0; u < 10; u++) {
for (v = 0; v < 0xFFFFFF; v++) {
/* NOP */
}
}
printstr("Delay ");
printk(w);
printstr("\n");
}
while(1);
}