summaryrefslogtreecommitdiffhomepage
path: root/src/loader/a20.c
blob: ecb4594b4066484a47b9b71a42d7fe49cac1a740 (plain) (blame)
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
// MIT License, Copyright (c) 2021 Marvin Borner
// https://wiki.osdev.org/A20_Line

#include <a20.h>
#include <cpu.h>
#include <def.h>
#include <panic.h>
#include <real.h>

static u8 a20_check(void)
{
	if (mminw(0x7dfe) != mminw(0x7dfe + 0x100000))
		return 1;

	mmoutw(0x7dfe, ~mminw(0x7dfe));

	if (mminw(0x7dfe) != mminw(0x7dfe + 0x100000))
		return 1;

	return 0;
}

void a20_enable(void)
{
	if (a20_check())
		return;

	// BIOS method
	struct real_regs r = { 0 };
	r.eax = 0x2401;
	real_int(0x15, &r, &r);

	if (a20_check())
		return;

	// Keyboard controller method
	while (inb(0x64) & 2)
		;
	outb(0x64, 0xad);
	while (inb(0x64) & 2)
		;
	outb(0x64, 0xd0);
	while (!(inb(0x64) & 1))
		;
	u8 b = inb(0x60);
	while (inb(0x64) & 2)
		;
	outb(0x64, 0xd1);
	while (inb(0x64) & 2)
		;
	outb(0x60, b | 2);
	while (inb(0x64) & 2)
		;
	outb(0x64, 0xae);
	while (inb(0x64) & 2)
		;

	assert(a20_check());
}