Code: Select all
#include <stdio.h>
#include <ctype.h>
#include <HAL.h>
#include <exception.h>
#include <mboot.h>
#include <phys_manager.h>
#include <virt_manager.h>
#include <keyboard.h>
#include <string.h>
#include <floppy.h>
#include <fat12.h>
#include <kheap.h>
#define DEFAULT_COLOR 0x07
void InitializeSystem          (multiboot_info *);
void PrintLogo                 ();
void RunInteractiveCommandLine ();
void GetCommand                (char *, int, bool);
bool RunCommand                (char *);
void ReadDiskSector            ();
void InitCallbacks             (int (__cdecl *)(char *));
int  InsertCallback            (int (__cdecl *)(char *), char *cmd_name);
int  LookupCallback            (char *cmd);
void make_lower                (char *str);
void make_upper				   (char *str);
//callbacks
int no_funct_present (char *args);
int get_diskinfo (char *args);
int read         (char *args);
int cls          (char *args);
int help         (char *args);
int exit         (char *args);
int echo         (char *args);
int color        (char *args);
int edit         (char *args);
void printmem (uint8_t *mem, uint16_t count)
{
	while (count-- > 0)
		putc32 (*mem++);
}
struct memory_region
{
	uint32_t start_lo;
	uint32_t start_hi;
	uint32_t size_lo;
	uint32_t size_hi;
	uint32_t type;
	uint32_t acpi_3_0;
};
char *strMemoryTypes[] = {
	{ "Available" },
	{ "Reserved" },
	{ "ACPI Reclaim" },
	{ "ACPI NVS Memory" }
};
diskinfo diskdat;
uint16_t cmd_count = 0;
uint32_t kernelSize = 0;
uint32_t size = 0;
bool echo_is_on = true;
uint8_t colors = DEFAULT_COLOR;
int (*function_list[100])(char *);
char names[100][10];
char abs_name[11];
int _cdecl main (multiboot_info* bootinfo)
{
	_asm mov word ptr [kernelSize],dx
	InitializeSystem (bootinfo);
	//initialize the callback system for the command line
	InitCallbacks (no_funct_present);
	//set the callback list up
	InsertCallback (get_diskinfo, "diskinfo");
	InsertCallback (read, "read");
	InsertCallback (cls, "cls");
	InsertCallback (help, "help");
	InsertCallback (exit, "exit");
	InsertCallback (echo, "echo");
	InsertCallback (color, "color");
	//InsertCallback (edit, "edit");
	setpos (0, 0);
	PrintLogo ();
	
	printf ("Operating System Command Line Interface online...\n");
	printf ("Commands: \"Help\": List commands.\n");
	printf ("          \"Exit\": Shutdown System\n");
	printf ("          \"Cls\": Clear the display\n");
	printf ("          \"Echo\": Get current echo status. Type echo help for more info.\n");
	printf ("          \"Read\": Read a disk sector\n");
	RunInteractiveCommandLine ();
	printf ("\nThe system has been halted because shutdown is not available yet.\n");
	printf ("You may now shut down your system safely.");
	for (;;);
	return 0;
}
void _cdecl kernel_entry (multiboot_info* bootinfo)
{
#ifdef ARCH_X86
	_asm {
		cli
		
		mov ax,0x10
		mov ds,ax
		mov es,ax
		mov gs,ax
		mov fs,ax
	}
#endif
	main (bootinfo);
#ifdef ARCH_X86
	_asm cli
	_asm hlt
#endif
}
void InitializeSystem (multiboot_info *bootinfo)
{
	clrscr32 (colors);
	setpos (0, 0);
	setcolor (colors);
	InitializeHAL ();
	enable ();
	setvect (0,(void (__cdecl &)(void))divide_by_zero_fault);
	setvect (1,(void (__cdecl &)(void))single_step_trap);
	setvect (2,(void (__cdecl &)(void))nmi_trap);
	setvect (3,(void (__cdecl &)(void))breakpoint_trap);
	setvect (4,(void (__cdecl &)(void))overflow_trap);
	setvect (5,(void (__cdecl &)(void))bounds_check_fault);
	setvect (6,(void (__cdecl &)(void))invalid_opcode_fault);
	setvect (7,(void (__cdecl &)(void))no_device_fault);
	setvect (8,(void (__cdecl &)(void))double_fault_abort);
	setvect (10,(void (__cdecl &)(void))invalid_tss_fault);
	setvect (11,(void (__cdecl &)(void))no_segment_fault);
	setvect (12,(void (__cdecl &)(void))stack_fault);
	setvect (13,(void (__cdecl &)(void))general_protection_fault);
	setvect (14,(void (__cdecl &)(void))page_fault);
	setvect (16,(void (__cdecl &)(void))fpu_fault);
	setvect (17,(void (__cdecl &)(void))alignment_check_fault);
	setvect (18,(void (__cdecl &)(void))machine_check_abort);
	setvect (19,(void (__cdecl &)(void))simd_fpu_fault);
	uint32_t memSize = 1024 + bootinfo->m_memoryLo + bootinfo->m_memoryHi * 64;
	pmmgr_init (memSize, 0xC0000000 + kernelSize*512);
	memory_region *region = (memory_region *) 0x1000;
	for (int i=0; i<10; ++i) {
		if (region[i].type>4)
			break;
		if (i>0 && region[i].start_lo==0)
			break;
		pmmgr_init_region (region[i].start_lo, region[i].size_lo);
	}
	pmmgr_deinit_region (0xC0000000, kernelSize*512);
	vmmgr_initialize ();
	kkybrd_install (33);
	flpydsk_set_working_drive (0);
	flpydsk_install (38);
	kheap = create_heap (KHEAP_START, KHEAP_START+KHEAP_INITIAL_SIZE, KHEAP_START+KHEAP_INITIAL_SIZE, 0, 0);
}
void RunInteractiveCommandLine ()
{
	char cmd_buf[100];
	while (1)
	{
		GetCommand (cmd_buf, 95, echo_is_on);
		if (RunCommand (cmd_buf) == true)
			break;
	}
}
void sleep (int ms)
{
	static int ticks = ms + get_tick_count ();
	while (ticks > get_tick_count ())
		;
}
KEYCODE getch ()
{
	KEYCODE key = KEY_UNKNOWN;
	while (key==KEY_UNKNOWN)
		key = kkybrd_get_last_key ();
	kkybrd_discard_last_key ();
	return key;
}
void CommandPrompt ()
{
	printf ("(NO DRIVE SUPPORTED YET):\\>");
}
void GetCommand (char *buf, int n, bool prompt)
{
	if (prompt)
		CommandPrompt ();
	KEYCODE key = KEY_UNKNOWN;
	int i = 0;
	bool buf_char = false;
	while (i < n)
	{
		buf_char = true;
		key = getch ();
		if (key == KEY_RETURN)
			break;
		if (key == KEY_BACKSPACE)
		{
			buf_char = false;
			if (i > 0)
			{
				unsigned x, y;
				getpos (&x, &y);
				if (x > 0)
					setpos (--x, y);
				else
				{
					y--;
					x = get_horizontal ();
				}
				putc32 (' ');
				setpos (x, y);
				i--;
			}
		}
		if (buf_char)
		{
			char c = kkybrd_key_to_ascii (key);
			if (c)
			{
				putc32 (c);
				buf[i++] = c;
			}
		}
		sleep (10);
	}
	buf[i] = '\0';
}
bool RunCommand (char *buf)
{
	int result = function_list[LookupCallback (buf)] (buf);
	if (result == 1)
	{
		int loc = LocateFile (buf);
		if (!loc)
		{
			printf ("Bad command or filename. Please try again.\n");
			return false;
		}
		else
		{
			printf ("File found. To edit this file, use the edit command.\n");
			/* int len = GetFileLength (loc);
			char *buf = (char *) alloc (len, 0, kheap);
			
			if (buf)
			{
				ReadFile (loc, len, sizeof (char), buf);
				printf ("File successfully loaded!\n");
			}
			else
				printf ("Could not allocate space!\n");
			*/
			return false;
		}
	}
	else if (result == -1)
		return true;
	else
		return false;
}
void ReadDiskSector ()
{
	uint32_t sectornum = 0;
	char sectnumbuf[4];
	uint8_t *sector = 0;
	printf ("\nEnter sector number:> ");
	GetCommand (sectnumbuf, 3, false);
	sectornum = atoi (sectnumbuf);
	printf ("\n\rSector %i contents:\n\n\r", sectornum);
	sector = flpydsk_read_sector (sectornum);
	if (sector != 0)
	{
		int i = 0;
		for (int c = 0; c < 4; c++)
		{
			for (int j = 0; j < 128; j++)
				printf ("0x%x ", sector[i+j]);
			i += 128;
			printf ("\nPress any key to continue...\n");
			getch ();
		}
	}
	else
		printf ("Error reading disk sector %d\n", sectornum);
	printf ("\nDone.");
}
void PrintLogo (void)
{
	printf ("\n");
	printf ("      _____   _____   _____                                               \n"
			"     / / \\ \\ / __  \\ / ___/------------------------------------------  \n"
			"    / /__/ // /  / // /_      Ben's Microcomputer Operating System        \n"
			"   / /  \\ \\/ /  / / \\__ \\    Windows 3.1 Style Interface Coming Soon  \n"
			"  / /___/ / /__/ /____/ /  ------------------------------------------     \n"
			" /_______/ \\____//_____/                                                 \n");
}
int LookupCallback (char *cmd)
{
	char *temp = (char *) alloc (strlen (cmd), 0, kheap);
	strcpy (temp, cmd);
	make_lower (temp);
	char *command = strtok (temp, " ");
	int i = 0;
	while (i < 100)
	{
		if (strcmp (temp, names[i]) == 0)
			return i;
		i++;
	}
	return 0;
}
void InitCallbacks (int (*def_proc)(char *))
{
	int i = 0;
	while (i < 100)
		function_list[i++] = def_proc;
	i = 0;
	while (i < 100)
		strcpy (names[i++], "default");
}
int InsertCallback (int (*funct)(char *), char *cmd)
{
	cmd_count++;
	function_list[cmd_count] = funct;
	strcpy (names[cmd_count], cmd);
	return cmd_count;
}
void make_lower (char *str)
{
	while (tolower(*str++))
		;
}
void make_upper (char *str)
{
	while (toupper (*str++))
		;
}
int no_funct_present (char *args)
{
	return 1;
}
int get_diskinfo (char *args)
{
	InitDiskStruct (&diskdat);
	printf ("\nNot done yet...\n");
	return 0;
}
int read (char *args)
{
	ReadDiskSector ();
	return 0;
}
int cls (char *args)
{
	clrscr32 (colors);
	PrintLogo ();
	return 0;
}
int help (char *args)
{
	clrscr32 (colors);
	PrintLogo ();
	printf ("Commands: \"Help\": List commands.\n");
	printf ("          \"Exit\": Shutdown System\n");
	printf ("          \"Cls\" : Clear the display\n");
	printf ("          \"Echo\": Get current echo status. Type echo help for more info.\n");
	printf ("          \"Read\": Read a disk sector\n");
	return 0;
}
int exit (char *args)
{
	return -1;
}
int echo (char *args)
{
	char *param = strstr (args, " ");
	param = strtok (param, "\0");
	
	if (!param)
		printf ("\nECHO is %s", echo_is_on ? "on" : "off");
	else if (strcmp (param, "on") == 0)
		echo_is_on = true;
	else if (strcmp (param, "off") == 0)
		echo_is_on = false;
	else
		printf ("\n%s", param);
	printf ("\n");
	return 0;
}
int color (char *args)
{
	char *new_color = strstr (args, " ");
	new_color = strtok (new_color, " \0");
	if (!new_color)
	{
		clrscr32 (DEFAULT_COLOR);
		//setcolor (DEFAULT_COLOR);
		return 0;
	}
	unsigned short new_colors = atoi (new_color);
	colors = new_colors;
	clrscr32 (new_colors);
	//setcolor (new_colors);
	return 0;
}
int edit (char *args)
{
	char *fname = strstr (args, " ");
	fname = strtok (args, " \n\0");
	return 0;
}

