Added iso9660 driver (not yet working).
[planlOS.git] / system / kernel / ke / start.c
blobb512726f7e5e7c83e5afc3a2bf0bacf7f87813ae
1 /*
2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #include "ke/debug.h"
23 #include "ke/gdt.h"
24 #include "ke/apic.h"
25 #include "ke/multiboot.h"
26 #include "ke/interrupts.h"
27 #include "ke/smp.h"
28 #include "ke/timer.h"
29 #include "ke/process.h"
30 #include "mm/memory.h"
31 #include "ke/module.h"
32 #include "fs/fs.h"
33 #include "fs/devfs.h"
34 #include <string.h>
35 #include <stdio.h>
37 static MultibootInfo *mbinfo;
39 void kernel_phys_start();
40 void kernel_phys_end();
42 static void keInit2(void)
44 keInitKernelSymbols(mbinfo);
45 fsInit();
46 fsInitDevFS();
47 FsFileSystemDriver *devfs = fsGetDriver("devfs");
48 if (!devfs)
50 // TODO: Panic
51 kePrint("devfs not available.\n");
52 while (1);
54 fsMount(devfs, "/dev/", 0, 0);
55 keInitModules(mbinfo);
56 kePrint("Opening test file.\n");
57 FsFileHandle *testfile = fsOpen("/dev/cdrom0", 0);
58 if (!testfile)
60 kePrint("Could not open test file.\n");
62 else
64 unsigned char test[512];
65 fsSeek(testfile, 0x8000, 0);
66 int read = fsRead(testfile, test, 512, 1);
67 kePrint("Read %d bytes.\n", read);
68 if (read != -1)
70 uint32_t i;
71 for (i = 0; i < 512; i++)
73 char buffer[2];
74 snprintf(buffer, 2, "%c", test[i]);
75 kePrint("%s", buffer);
77 kePrint("\n");
80 kePrint("Closing test file.\n");
81 fsClose(testfile);
84 // Map CDROM
85 FsFileSystemDriver *iso9660 = fsGetDriver("iso9660");
86 if (!iso9660)
88 // TODO: Panic
89 kePrint("iso9660 not available.\n");
90 while (1);
92 fsMount(iso9660, "/", "/dev/cdrom0", 0);
94 kePrint("Sleep (500)\n");
95 keSleep(500);
96 kePrint("Sleep (1000)\n");
97 keSleep(1000);
98 kePrint("Done.\n");
100 while (1);
103 void keInit(int magic, MultibootInfo *info)
105 char *vidmem = (char*)0xC00B8000;
106 memset(vidmem, 0, 160 * 25);
108 kePrintInit();
109 keInitPIC();
110 if (!CHECK_FLAG(info->flags, 5))
112 kePrint("Error: no symbols available.\n");
113 while (1);
116 uintptr_t kernel_begin = ((uintptr_t)kernel_phys_start & ~0xFFF) + 0xC0000000;
117 uintptr_t kernel_end = (((uintptr_t)kernel_phys_end + 4095) & ~0xFFF) + 0xC0000000;
118 mmInitMemoryManager(info, kernel_begin, kernel_end);
119 // Remap multiboot struct
120 uintptr_t info_addr = mmFindFreeKernelPages(MM_MAX_KERNEL_PAGE,
121 MM_MIN_KERNEL_PAGE, 0, 0x1000);
122 mmMapKernelMemory((uintptr_t)info & ~0xFFF, info_addr,
123 MM_MAP_READ | MM_MAP_WRITE);
124 info_addr += (uintptr_t)info & 0xFFF;
125 info = (void*)info_addr;
126 mbinfo = info;
128 keAPICInit();
129 keInitSMP();
130 keInstallGDT();
131 keInitTimer(100);
132 keInitProcessManager();
134 // Test process
135 KeProcess *testprocess = keCreateProcess();
136 keLockSpinlock(mmGetMemoryLock());
137 uintptr_t progpaddr = mmAllocPhysicalMemory(0, 0, 0x1000);
138 uintptr_t progkerneladdr = mmFindFreeKernelPages(MM_MAX_KERNEL_PAGE,
139 MM_MIN_KERNEL_PAGE, 0, 0x1000);
140 mmMapKernelMemory(progpaddr, progkerneladdr, MM_MAP_READ | MM_MAP_WRITE);
141 ((uint8_t*)progkerneladdr)[0] = 0xEB;
142 ((uint8_t*)progkerneladdr)[1] = 0xFE;
143 mmMapKernelMemory(0, progkerneladdr, 0);
144 mmMapMemory(&testprocess->memory, progpaddr, 0x400000, MM_MAP_READ | MM_MAP_EXECUTE);
145 keUnlockSpinlock(mmGetMemoryLock());
146 KeThread *testthread = keCreateThread(testprocess, 0x400000, 0);
147 keUnlockSpinlock(&testprocess->lock);
149 // Initial kernel thread
150 KeThread *initthread = keCreateKernelThread((uintptr_t)keInit2, 0);
152 // Start kernel
153 keInstallTimer();
154 keStartAPs();
155 asm("sti");
157 while (1) asm("hlt");