-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
120 lines (98 loc) · 3.35 KB
/
Copy pathmain.c
File metadata and controls
120 lines (98 loc) · 3.35 KB
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
//
// Created by reindeer002-laptop on 3/12/26.
//
#include "types.h"
#include "page.h"
#include "mode-switch.h"
void kPrintString(int iX, int iY, const char* pcString);
BOOL kInitializeKernel64Area();
BOOL kIsMemoryEnough();
void kCopyKernel64ImageTo2Mbyte();
void main() {
DWORD dwEAX, dwEBX, dwECX, dwEDX;
char vcVendorString[13] = { 0 };
kPrintString(0, 3, "Protected Mode C Language Kernel Start.");
kPrintString(0, 3, "C Language Kernel Started.");
kPrintString(0, 4, "Minimum Memory Size Check.");
if(kIsMemoryEnough() == FALSE) {
kPrintString(45, 4, "FAIL");
kPrintString(0, 5, "Not Enough Memory, REIN OS Requires Over 64MB Memory.");
while(1);
}
kPrintString(45, 4, "PASS");
kPrintString(0, 5, "IA-32e Kernel Area Initialize.");
if(kInitializeKernel64Area() == FALSE) {
kPrintString(45, 5, "FAIL");
kPrintString(0, 6, "Kernel Area Initialization Fail.");
while(1);
}
kPrintString(45, 5, "PASS");
kPrintString(0, 6, "IA-32e Page Tables Initialize.");
kInitializePageTables();
kPrintString(45, 6, "PASS");
kReadCPUID(0, &dwEAX, &dwEBX, &dwECX, &dwEDX);
*(DWORD*) vcVendorString = dwEBX;
*((DWORD*)vcVendorString + 1) = dwEDX;
*((DWORD*)vcVendorString + 2) = dwECX;
kPrintString(0, 7, "CPU Vendor String : ");
kPrintString(45, 7, vcVendorString);
kReadCPUID(0x80000001, &dwEAX, &dwEBX, &dwECX, &dwEDX);
kPrintString(0, 8, "64bit Mode Support Check : ");
if(dwEDX & ( 1 << 29 )) {
kPrintString(45, 8, "PASS");
} else {
kPrintString(45, 8, "FAIL");
kPrintString(0, 9, "64bit Mode Not Supported.");
while(1);
}
kPrintString(0, 9, "Copy IA-32e Kernel To 2M Address.");
kCopyKernel64ImageTo2Mbyte();
kPrintString(45, 9, "PASS");
kPrintString(0, 10, "Switch To IA-32e Mode");
kSwitchAndExecute64bitKernel();
while(1);
}
void kPrintString(int iX, int iY, const char* pcString) {
CHARACTER* pstScreen = ( CHARACTER* ) 0xB8000;
pstScreen += ( iY * 80 ) + iX;
for(int i = 0; pcString[i] != 0; i++) {
pstScreen[i].bCharacter = pcString[i];
}
}
BOOL kInitializeKernel64Area() {
DWORD* pdwCurrentAddress;
pdwCurrentAddress = ( DWORD* ) 0x100000;
while((DWORD) pdwCurrentAddress < 0x600000) {
*pdwCurrentAddress = 0x00;
if(*pdwCurrentAddress != 0) {
return FALSE;
}
pdwCurrentAddress++;
}
return TRUE;
}
BOOL kIsMemoryEnough() {
DWORD* pdwCurrentAddress;
pdwCurrentAddress = (DWORD*) 0x100000;
while((DWORD)pdwCurrentAddress < 0x4000000) {
*pdwCurrentAddress = 0x12345678;
if(*pdwCurrentAddress != 0x12345678) {
return FALSE;
}
pdwCurrentAddress += (0x100000 / 4);
}
return TRUE;
}
void kCopyKernel64ImageTo2Mbyte() {
WORD wKernel32SectorCount, wTotalKernelSectorCount;
DWORD *pdwSourceAddress, *pdwDestinationAddress;
wTotalKernelSectorCount = *((WORD*) 0x7C05);
wKernel32SectorCount = *((WORD*) 0x7C07);
pdwSourceAddress = (DWORD*) (0x10000 + (wKernel32SectorCount * 512));
pdwDestinationAddress = (DWORD*) 0x200000;
for(int i = 0; i < (wTotalKernelSectorCount - wKernel32SectorCount) * 512 / 4; i++) {
*pdwDestinationAddress = *pdwSourceAddress;
pdwDestinationAddress++;
pdwSourceAddress++;
}
}