-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_io.c
96 lines (91 loc) · 1.84 KB
/
base_io.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
#include "base_io.h"
#include "screen_ctrl.h"
unsigned int GetEFLAGS(void)
{
unsigned int r;
asm volatile(
"pushf\n\t"
"pop %%eax\n\t"
: "=a"(r)
:
);
return r;
}
void SetEFLAGS(unsigned int eflags)
{
asm volatile(
"push %%eax\n\t"
"popf\n\t"
:
: "a"(eflags)
);
}
void CloseInterrupt()
{
asm volatile("cli\n\t");
}
void StartInterrupt()
{
asm volatile("sti\n\t");
}
unsigned char ReadPort(unsigned short int Port)
{
unsigned char res;
asm volatile("in %%dx,%%al\n\t"
: "=a"(res)
: "d"(Port));
return res;
}
void WritePort(unsigned short int port, unsigned char data)
{
asm volatile("out %%al,%%dx"
:
: "a"(data), "d"(port));
}
void WaitForInterrupt()
{
asm volatile("hlt\n\t");
}
void ReadSector(void *buffer, unsigned int sector_id, unsigned char need_num)
{
unsigned char state;
WritePort(0x1F2, need_num);
WritePort(0x1F3, sector_id & 0xFF);
WritePort(0x1F4, (sector_id & 0xFF00) >> 8);
WritePort(0x1F5, (sector_id & 0xFF0000) >> 16);
WritePort(0x1F6, ((sector_id & 0xF000000) >> 24) | 0xE0);
WritePort(0x1F7, 0x20); // 请求硬盘读
//asm volatile ("xchg %bx,%bx");
do
{
state = ReadPort(0x1F7);
} while ((state & 0x88) != 0x08);
//asm volatile ("xchg %bx,%bx");
unsigned short int *ptr = (unsigned short int *)buffer;
for (unsigned int i = 0; i < need_num * 256; i++)
{
asm volatile(
"mov $0x1F0,%%dx\n\t"
"in %%dx,%%ax\n\t"
"mov %%ax,(%%ecx)\n\t"
:
: "c"(ptr)
:"%edx","%eax");
ptr += 1;
}
//asm volatile ("xchg %bx,%bx");
}
void FlushCR3(void)
{
asm volatile(
"push %%eax\r\n"
"mov %%cr3,%%eax\r\n"
"mov %%eax,%%cr3\r\n"
"pop %%eax\r\n"
:
:);
}
void CloseCPU()
{
__asm__ volatile("cli;hlt;");
}