forked from bonzini/qboot
-
Notifications
You must be signed in to change notification settings - Fork 3
/
code16.c
122 lines (93 loc) · 2.08 KB
/
code16.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
asm(".code16gcc");
#include <stddef.h>
#include "bios.h"
#include "segment.h"
#include "ioport.h"
#include "processor-flags.h"
#include "e820.h"
static inline void set_fs(uint16_t seg)
{
asm volatile("movw %0,%%fs" : : "rm" (seg));
}
static inline uint8_t rdfs8(unsigned long addr)
{
uint8_t v;
asm volatile("addr32 movb %%fs:%1,%0" : "=q" (v) : "m" (*(uint8_t *)addr));
return v;
}
static inline uint32_t rdfs32(unsigned long addr)
{
uint32_t v;
asm volatile("addr32 movl %%fs:%1,%0" : "=r" (v) : "m" (*(uint32_t *)addr));
return v;
}
static inline uint16_t rdcs16(void *p)
{
uint32_t addr = ((uintptr_t) p) & 65535;
uint16_t v;
asm volatile("addr32 movw %%cs:%1,%0" : "=r" (v) : "m" (*(uint32_t *)addr));
return v;
}
uint16_t e820_seg;
bioscall void e820_query_map(struct biosregs *regs)
{
uint32_t map_size;
uint32_t ndx;
set_fs(rdcs16(&e820_seg));
ndx = regs->ebx;
map_size = rdfs32(offsetof(struct e820map, nr_map));
if (ndx < map_size) {
uint32_t start;
unsigned int i;
uint8_t *p;
start = offsetof(struct e820map, map[ndx]);
p = (void *) regs->edi;
for (i = 0; i < sizeof(struct e820entry); i++)
*p++ = rdfs8(start + i);
}
regs->eax = SMAP;
regs->ecx = sizeof(struct e820entry);
regs->ebx = ++ndx;
if (ndx >= map_size)
regs->ebx = 0; /* end of map */
}
bioscall void int15_handler(struct biosregs *regs)
{
switch (regs->eax) {
case 0xe820:
e820_query_map(regs);
break;
default:
/* Set CF to indicate failure. */
regs->eflags |= X86_EFLAGS_CF;
break;
}
}
/*
* It's probably much more useful to make this print to the serial
* line rather than print to a non-displayed VGA memory
*/
static inline void int10_putchar(struct biosregs *args)
{
uint8_t al = args->eax & 0xFF;
outb(0x3f8, al);
}
#define VBE_STATUS_OK 0x004F
#define VBE_STATUS_FAIL 0x014F
static void int10_vesa(struct biosregs *args)
{
args->eax = VBE_STATUS_FAIL;
}
bioscall void int10_handler(struct biosregs *args)
{
uint8_t ah;
ah = (args->eax & 0xff00) >> 8;
switch (ah) {
case 0x0e:
int10_putchar(args);
break;
case 0x4f:
int10_vesa(args);
break;
}
}