summaryrefslogtreecommitdiffstats
path: root/01_exercise/bootloader.c
blob: 07b3dccffbc38ae96b16ae1ee2c3ea4a74ec4ccb (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
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
/* needs to stay the first line */
asm(".code16gcc\njmp $0, $main");

/* space for additional code */

char WRITE_CHARACTER_TTY = 0x0E;

// Syscall found here http://www.ctyme.com/intr/rb-0106.htm
void putc(char c) {
    short command = WRITE_CHARACTER_TTY << 8 | c;
    // volatile because there is no output, so the function might get optimized
    // away
    // clang-format off
    asm volatile(
        "int $0x10;"
        ::"a"(command)
    );
    // clang-format on
}

void print(char const *const str) {
    for (int i = 0; str[i] != '\0'; ++i) {
        putc(str[i]);
    }
}

// Syscall found here http://www.ctyme.com/intr/rb-1754.htm
char getc() {
    char ret;
    asm volatile(
        ".no_key:"
        "xor %%eax, %%eax;"
        "mov $0x0100, %%ax;" // check for keystroke:
        "int $0x16;"         // http://www.ctyme.com/intr/rb-1755.htm
        "jz .no_key;"        // wait for a key
        ".done:"

        "xor %%eax, %%eax;"
        "mov $0x1000, %%ax;" // get keystroke: http://www.ctyme.com/intr/rb-1754.htm
        "int $0x16;"

        "mov %%al, %%bl;"  // temp store char in bl
        "mov $0400, %%ax;" // clear keyboard buffer:
        "int $0x16;"       // http://www.ctyme.com/intr/rb-1759.htm
        : "=b"(ret)
        :
        : "eax");
    return ret;
}

void sleep(short ms) {
    long ys = ms * 1000;
    short cx = ys >> 16;
    short dx = ys;

    // http://www.ctyme.com/intr/rb-1525.htm : Int 15/AH=86h bios wait
    char ah = 0x86;
    char al = 0x00;
    short command = ah << 8 | al;

    asm volatile("int $0x15;" : : "a"(command), "cx"(cx), "dx"(dx));
}

void main(void) {
    print("Hello!\n\r");

    while (1) {
        char buf[9];
        for (unsigned char i = 0; i < 8; i++) {
            char c = getc();
            if (c == '\r') {
                if (i == 0) { // empty entry
                    print("Reboot!\n\r");
                    sleep(250); // too see the reboot print out, even if short
                    asm volatile(
                        "cli;"              // disable interrupts
                        "mov $0x0FE, %%al;" // tell the keyboard controller to...
                        "out %%al, $0x64;"  // ...activate the CPU reset line
                        ".halt:"
                        "hlt;"          // some motherboards require this
                        "jmp .halt;" :: // and again, just to be sure
                        : "al");
                    return;
                } else {
                    break;
                }
            }
            buf[i] = c;
            buf[i + 1] = 0;
            putc('*');
        }
        while (1) {
            if (getc() == '\r')
                break;
        }
        print("\n\r");
        print(buf);
        print("\n\r");
    }
}