/* 
 * Copyright 2010 Stefan Lankes, Chair for Operating Systems,
 *                               RWTH Aachen University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * This file is part of MetalSVM. 
 */

#include <metalsvm/stddef.h>
#include <metalsvm/stdio.h>
#include <metalsvm/string.h>
#include <metalsvm/time.h>
#include <metalsvm/mmu.h>
#include <metalsvm/tasks.h>
#include <metalsvm/processor.h>
#include <metalsvm/fs.h>
#include <metalsvm/errno.h>
#include <metalsvm/init.h>
#include <asm/irq.h>
#include <asm/irqflags.h>
#include <asm/kb.h>
#ifdef CONFIG_ROCKCREEK
#include <asm/icc.h>
#endif

extern int test_init(void);

/* 
 * Note that linker symbols are not variables, they have no memory allocated for
 * maintaining a value, rather their address is their value.
 */
extern const void kernel_start;
extern const void kernel_end;
extern char __BUILD_DATE;
extern char __BUILD_TIME;

static void list_fs(vfs_node_t* node, uint32_t depth)
{
	int j, i = 0;
	dirent_t* dirent = NULL;

	while ((dirent = readdir_fs(node, i)) != 0) {
		for(j=0; j<depth; j++)
			kputs("  ");
		kprintf("%s\n", dirent->name);

		if (strcmp(dirent->name, ".") && strcmp(dirent->name, "..")) {
			vfs_node_t *new_node = finddir_fs(node, dirent->name);
			if (new_node) {
				if (new_node->type == FS_FILE) {
					char buff[16] = {[0 ... 15] = 0x00};

					read_fs(new_node, (uint8_t*)buff, 8, 0);
					for(j=0; j<depth+1; j++)
						kputs("  ");
					kprintf("content: %s\n", buff);
				} else list_fs(new_node, depth+1);
			}
		}

		i++;
	}
}

static void list_root(void) {
	kprintf("List of the file system:\n/\n");
	list_fs(fs_root, 1);
}

int main(void)
{
	lowlevel_init();

	pushbg(COL_BLUE);
	kprintf("This is MetalSVM %s Build %u, %u\n", 
			METALSVM_VERSION, &__BUILD_DATE, &__BUILD_TIME);
	popbg();

	system_init();
	irq_init();
	timer_init();
#ifdef CONFIG_KEYBOARD
	keyboard_init();
#endif
	multitasking_init();
	mmu_init();
#ifdef CONFIG_ROCKCREEK
	icc_init();
#endif
   	initrd_init();

	irq_enable();

	kprintf("Kernel starts at %p and ends at %p\n", &kernel_start, &kernel_end);
		
	system_calibration();
	network_init();

	kprintf("Processor frequency: %u MHz\n", get_cpu_frequency());
	kprintf("Total memory: %u MBytes\n", atomic_int32_read(&total_pages)/((1024*1024)/PAGE_SIZE));
	kprintf("Current allocated memory: %u KBytes\n", atomic_int32_read(&total_allocated_pages)*(PAGE_SIZE/1024));
	kprintf("Current available memory: %u MBytes\n", atomic_int32_read(&total_available_pages)/((1024*1024)/PAGE_SIZE));

//	sleep(5);
//	list_root();
	test_init();

	per_core(current_task)->status = TASK_IDLE;
	reschedule();

	while(1) { 
#ifdef CONFIG_ROCKCREEK
		icc_halt();
#else
		HALT;
#endif
	}

	return 0;
}