aboutsummaryrefslogtreecommitdiff
path: root/src/kernel.c
blob: 97a42b501974960a6adad50a5b8f81c7e78cf2e9 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* Copyright (c) 2026 Arslaan Pathan
This software is licensed under the ARPL. See LICENSE for details. */

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "font8x8_basic.h"

#define KB_DATA_PORT 0x60
#define KB_STATUS_PORT 0x64
#define KB_OBF 0x01

/* check if compiler thinks we are targeting incorrect OS. */
#if defined(__linux__)
#error "you are not using a cross-compiler, this is bad. use an elf cross-compiler for ix86 targets, for example, i386-elf-gcc"
#endif

/* OS only works on 32bit ix86 */
#if !defined(__i386__)
#error "this operating system is only supported on ix86 targets. use an elf cross-compiler for ix86 targets, for example, i386-elf-gcc"
#endif

static int term_col = 0;
static int term_row = 0;
static uint32_t *g_framebuffer = NULL;
static uint32_t g_width = 0;
static uint32_t g_height = 0;
static uint32_t g_default_color = 0xFFFFFF;
static bool kbd_shifted = false;

/* what in the dark magic? ok just dont touch one can hope */
struct multiboot_info {
	uint32_t flags;
	uint32_t mem_lower;
	uint32_t mem_upper;
	uint32_t boot_device;
	uint32_t cmdline;
	uint32_t mods_count;
	uint32_t mods_addr;
	uint32_t syms[4];
	uint32_t mmap_length;
	uint32_t mmap_addr;
	uint32_t drives_length;
	uint32_t drives_addr;
	uint32_t config_table;
	uint32_t boot_loader_name;
	uint32_t apm_table;
	uint32_t vbe_control_info;
	uint32_t vbe_mode_info;
	uint16_t vbe_mode;
	uint16_t vbe_interface_seg;
	uint16_t vbe_interface_off;
	uint16_t vbe_interface_len;
	uint64_t framebuffer_addr;
	uint32_t framebuffer_pitch;
	uint32_t framebuffer_width;
	uint32_t framebuffer_height;
	uint8_t  framebuffer_bpp;
	uint8_t  framebuffer_type;
	uint8_t  color_info[6];
} __attribute__((packed));

static const char scancode_to_ascii[128] __attribute__((section(".text"))) = {
	[0x29] = '`',
	[0x02] = '1',
	[0x03] = '2',
	[0x04] = '3',
	[0x05] = '4',
	[0x06] = '5',
	[0x07] = '6',
	[0x08] = '7',
	[0x09] = '8',
	[0x0A] = '9',
	[0x0B] = '0',
	[0x0C] = '-',
	[0x0D] = '=',
	[0x0E] = '\b',

	[0x10] = 'q',
	[0x11] = 'w',
	[0x12] = 'e',
	[0x13] = 'r',
	[0x14] = 't',
	[0x15] = 'y',
	[0x16] = 'u',
	[0x17] = 'i',
	[0x18] = 'o',
	[0x19] = 'p',

	[0x1A] = '[',
	[0x1B] = ']',
	[0x1C] = '\n',
	[0x2B] = '\\',

	[0x1E] = 'a',
	[0x1F] = 's',
	[0x20] = 'd',
	[0x21] = 'f',
	[0x22] = 'g',
	[0x23] = 'h',
	[0x24] = 'j',
	[0x25] = 'k',
	[0x26] = 'l',

	[0x27] = ';',
	[0x28] = '\'',

	[0x2C] = 'z',
	[0x2D] = 'x',
	[0x2E] = 'c',
	[0x2F] = 'v',
	[0x30] = 'b',
	[0x31] = 'n',
	[0x32] = 'm',
	[0x33] = ',',
	[0x34] = '.',
	[0x35] = '/',

	[0x39] = ' ',
};

static const char scancode_to_ascii_shifted[128] __attribute__((section(".text"))) = {
	[0x29] = '~',
	[0x02] = '!',
	[0x03] = '@',
	[0x04] = '#',
	[0x05] = '$',
	[0x06] = '%',
	[0x07] = '^',
	[0x08] = '&',
	[0x09] = '*',
	[0x0A] = '(',
	[0x0B] = ')',
	[0x0C] = '_',
	[0x0D] = '+',
	[0x0E] = '\b',

	[0x10] = 'Q',
	[0x11] = 'W',
	[0x12] = 'E',
	[0x13] = 'R',
	[0x14] = 'T',
	[0x15] = 'Y',
	[0x16] = 'U',
	[0x17] = 'I',
	[0x18] = 'O',
	[0x19] = 'P',

	[0x1A] = '{',
	[0x1B] = '}',
	[0x1C] = '\n',
	[0x2B] = '|',

	[0x1E] = 'A',
	[0x1F] = 'S',
	[0x20] = 'D',
	[0x21] = 'F',
	[0x22] = 'G',
	[0x23] = 'H',
	[0x24] = 'J',
	[0x25] = 'K',
	[0x26] = 'L',

	[0x27] = ':',
	[0x28] = '"',

	[0x2C] = 'Z',
	[0x2D] = 'X',
	[0x2E] = 'C',
	[0x2F] = 'V',
	[0x30] = 'B',
	[0x31] = 'N',
	[0x32] = 'M',
	[0x33] = '<',
	[0x34] = '>',
	[0x35] = '?',

	[0x39] = ' ',
};

static inline void outb(uint16_t port, uint8_t val) {
	asm volatile("outb %0, %1" : : "a"(val), "Nd"(port));
}

static inline uint8_t inb(uint16_t port) {
	uint8_t ret;
	asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port));
	return ret;
}

uint8_t keyboard_read_scan_code() {
	while (!(inb(KB_STATUS_PORT) & KB_OBF));
	return inb(KB_DATA_PORT);
}
void draw_char(int x, int y, int c, uint32_t color, uint32_t *framebuffer, uint32_t width)
{
	char *glyph = font8x8_basic[(unsigned char)c];

	for (int row = 0; row < 8; row++) {
		for (int col = 0; col < 8; col++) {
			if (glyph[row] & (1 << col)) {
				framebuffer[(y + row) * width + (x + col)] = color;
			}
		}
	}
}

void draw_string(int x, int y, const char *str, uint32_t color, uint32_t *framebuffer, uint32_t width)
{
	while (*str) {
		draw_char(x, y, *str, color, framebuffer, width);
		x += 8;
		str++;
	}
}

void term_init(uint32_t *framebuffer, uint32_t width, uint32_t height)
{
	g_framebuffer = framebuffer;
	g_width = width;
	g_height = height;
	term_col = 0;
	term_row = 0;
}

void term_set_color(uint32_t color)
{
	g_default_color = color;
}

void term_printf(const char *str)
{
	while (*str) {
		if (*str == '\n') {
			term_col = 0;
			term_row++;
			str++;
			continue;
		}

		if ((term_col * 8) >= g_width) {
			term_col = 0;
			term_row++;
		}

		if ((term_row * 8) >= g_height) {
			term_row = 0; // just simple wrap around for now, scroll later
		}

		draw_char(term_col * 8, term_row * 8, *str, g_default_color, g_framebuffer, g_width);

		term_col++;
		str++;
	}
}

void shell() {
	term_set_color(0xFCD24D);
	term_printf("\n> ");
	term_set_color(0xFFFFFF);
	while (1) {
		uint8_t scancode = keyboard_read_scan_code();
	
		// shift or no shift????
		if (scancode == 0x2A) kbd_shifted = true;
		if (scancode == 0xAA) kbd_shifted = false;

		// we dont care abt key releases
		if (scancode & 0x80) continue;

		char c = kbd_shifted ? scancode_to_ascii_shifted[scancode] : scancode_to_ascii[scancode];

		if (c) {
			if (c == '\n') {
				// enter, interpret command
				// we dont know what to do with command yet so new prompt and pretend we did something
				// we also need to add like handling of the current command, do that soon!
				term_set_color(0xFCD24D);
				term_printf("\n> ");
				term_set_color(0xFFFFFF);
			}
			if (c == '\b') {
				// stupid nut keep trying to use backspace when its not done yet, just add a handler placeholder
				term_printf("\n---\nyou stupid nut!\n");
				term_printf("we dont have backspace yet, stop trying to when testing! get better\n");
				term_set_color(0xFCD24D);
				term_printf("\n> ");
				term_set_color(0xFFFFFF);
			}
			else {
				term_printf(&c);
			}
		}
	}
}

void kernel_main(struct multiboot_info *mbi) 
{
	// check if framebuffer info is available (bit 12)
	if (!(mbi->flags & (1 << 12))) {
		while(1) __asm__("hlt");
	}

	// get the framebuffer
	uint32_t *framebuffer = (uint32_t*)(uintptr_t)mbi->framebuffer_addr;
	uint32_t width = mbi->framebuffer_width;
	uint32_t height = mbi->framebuffer_height;

	// draw text to the framebuffer
	term_init(framebuffer, width, height);
	term_printf("welcome to ");
	term_set_color(0xFCD24D);
	term_printf("FrenchToastOS!\n");
	term_set_color(0xFFFFFF);
	term_printf("developed by ");
	term_set_color(0x967BB6);
	term_printf("Arslaan Pathan\n");
	term_set_color(0xFFFFFF);
	term_printf("---\n");
	term_printf("https://arslaancodes.com\n");

	term_printf("\n---\nShell:\n");
	shell();

	// if theres nothing left to do, halt the cpu or else cooked
	// our boot.s already does this but better to be safe than sorry
	while (1) {
		__asm__ __volatile__ ("hlt");
	}
}