aboutsummaryrefslogtreecommitdiff
path: root/kernel.c
blob: 5c9f16d2f84332b514117be48c47ba9e1751213a (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
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 */

// Complain if some pleb's using a linux toolchain and not cross-compiling
#if defined(__linux__)
	#error "You're not using a cross-compiler. Good luck with that."
#endif

#include <toast.h>

// vv-- Put me somewhere --vv
uint32_t TOTAL_MEMORY = 0;			// Total memory. Typically set by k_tally_available_memory().
									// It must be initialised to 0 so that if it's not set otherwise, nothing will think there's memory available
bool KERNEL_OPTION_QUIET	= FALSE;
bool KERNEL_OPTION_SILENT	= FALSE;
bool KERNEL_OPTION_ESHELL	= FALSE;
// ^^-- put me somewhere --^

void kernel_main(multiboot_info_t *mbd, uint32_t magic)
{
	//*********************************************************
	// First things first. Parse the command line. Quick guide:
	//
	//  o quiet  - Don't print so much stuff to the screen during boot
	//  o silent - Don't print anything at all during boot (implies quiet)
	//  o eshell - (RESERVED) Will be used to enter emergency shell
	//
	if (string_contains( (char*)(mbd->cmdline) , "quiet"))
		KERNEL_OPTION_QUIET = TRUE;

	if (string_contains( (char*)(mbd->cmdline) , "silent"))
	{
		KERNEL_OPTION_QUIET = TRUE;
		KERNEL_OPTION_SILENT = TRUE;
	}
	if (string_contains( (char*)(mbd->cmdline) , "eshell"))
		KERNEL_OPTION_ESHELL = TRUE;
	//*********************************************************

	//*********************************************************
	// Do some important things before we forget
	k_tally_available_memory(mbd);

	// Init the text console and colours
	console_init();
	console_set_colors(COLOR_BRIGHT_GRAY, COLOR_BLACK);
	console_swap_colors();
	console_set_colors(COLOR_BRIGHT_GRAY, COLOR_BLACK);
	console_clear();
	//*********************************************************

	if (!KERNEL_OPTION_QUIET)
	{
		// Print some stuff about the environment
		console_print("Here is kernel_main()\n");
		console_print("Bootloader: %s\n",mbd->boot_loader_name);
		console_print("Kernel: %s\n",mbd->cmdline);
		console_print("Flags: %b\n",mbd->flags);
		console_print("Boot Device: %x\n",mbd->boot_device);

		// Dump some info about the memory blocks/regions available/reserved
		k_dump_memory_blocks(mbd);
	}
	if (!KERNEL_OPTION_SILENT)
	{
		console_print("Welcome to Toast v%d.%d (nickname '%s')\nMemory: %dMiB",
						KERNEL_VERSION_MAJ,
						KERNEL_VERSION_MIN,
						KERNEL_NICKNAME,
						TOTAL_MEMORY/1048576);
	}
	//panic(0x4655434B); // (TEST) testing panic
}

/*********************************************************
 * Tally/total the amount of available memory and store
 * it for later use. Returns pointer to total mem figure
 ********************************************************/
uint32_t *k_tally_available_memory(multiboot_info_t *mbd)
{
	multiboot_memory_map_t* mmap = (multiboot_memory_map_t*)mbd->mmap_addr;
	TOTAL_MEMORY = 0;

	while((uint32_t)mmap < mbd->mmap_addr + mbd->mmap_length)
	{
		mmap = (multiboot_memory_map_t*)( (uint32_t)mmap + mmap->size + sizeof(uint32_t) );

		if (mmap->type == 1) // available
			TOTAL_MEMORY += (uint32_t)mmap->len;
	}
	return &TOTAL_MEMORY;
}

/*********************************************************
 * Dump information about the blocks of memory. What
 * starts and finishes where and available/reserved
 ********************************************************/
void k_dump_memory_blocks(multiboot_info_t *mbd)
{
	multiboot_memory_map_t* mmap = (multiboot_memory_map_t*)mbd->mmap_addr;

	// While we're pointing to a mem map struct within the supplied range...
	while((uint32_t)mmap < mbd->mmap_addr + mbd->mmap_length)
	{
		mmap = (multiboot_memory_map_t*)( (uint32_t)mmap + mmap->size + sizeof(uint32_t) );
		console_print("[");

		// Save current console colours
		console_swap_colors();

		if (mmap->type == 1)
		{
			console_set_colors(COLOR_GREEN, COLOR_BLACK);
			console_print("   FREE   ");
		} else {
			console_set_colors(COLOR_RED, COLOR_BLACK);
			console_print(" RESERVED ");
		}
		// Restore old console colours
		console_swap_colors();

		console_print("] 0x%x to 0x%x = %d bytes\n",
						(uint32_t)mmap->addr,
						(uint32_t)mmap->addr + (uint32_t)mmap->len-1,
						(uint32_t)mmap->len
		);
	}
}