changeset 41:35cc54e078dd

Added memory functions
author windel
date Fri, 03 Feb 2012 17:56:41 +0100
parents 24ce177e01e8
children 980b2f14c9be
files README cos/Makefile cos/README cos/kernel/initrd.c cos/kernel/kernel.c cos/kernel/kernel.h cos/kernel/mm.c cos/makeinitrd.py cos/utils/makeinitrd.py
diffstat 9 files changed, 129 insertions(+), 86 deletions(-) [+]
line wrap: on
line diff
--- a/README	Tue Jan 24 17:54:16 2012 +0100
+++ b/README	Fri Feb 03 17:56:41 2012 +0100
@@ -22,3 +22,22 @@
 $ cd ide
 $ python runide.py
 
+= About the C version of the OS =
+
+To build the C kernel, enter:
+$ cd cos
+$ make
+
+Running the OS with bochs:
+$ bochs -q
+
+required tools:
+- bochs: for simulating the OS
+- mtools: for copying files to the bootdisk
+- nasm: for assembler instructions
+- gcc: for compiling the C sources
+- make: for building the system
+- python 3: for building the initial ramdisk
+
+Enjoy!
+
--- a/cos/Makefile	Tue Jan 24 17:54:16 2012 +0100
+++ b/cos/Makefile	Fri Feb 03 17:56:41 2012 +0100
@@ -2,7 +2,7 @@
 
 all:
 	make -C kernel
-	python makeinitrd.py lcfosinitrd.img hello/hello.bin
+	python utils/makeinitrd.py lcfosinitrd.img hello/hello.bin
 	cp grub/emptybootdisk.img bootdisk.img
 	mcopy -D o -i bootdisk.img kernel/lcfos.bin ::
 	mcopy -D o -i bootdisk.img lcfosinitrd.img ::
--- a/cos/README	Tue Jan 24 17:54:16 2012 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,21 +0,0 @@
-This directory contains the C version of the OS.
-
-To build the kernel, enter:
-$ make
-
-To make the bootdisk, hit:
-$ make bootdisk
-
-Running the OS with bochs:
-$ bochs -q
-
-required tools:
-- bochs: for simulating the OS
-- mtools: for copying files to the bootdisk
-- nasm: for assembler instructions
-- gcc: for compiling the C sources
-- make: for building the system
-- python: for building the initial ramdisk
-
-Enjoy!
-
--- a/cos/kernel/initrd.c	Tue Jan 24 17:54:16 2012 +0100
+++ b/cos/kernel/initrd.c	Fri Feb 03 17:56:41 2012 +0100
@@ -4,22 +4,81 @@
 
 #include "kernel.h"
 
-fs_node_t *initrd_root = 0;
-
-typedef struct
-{
-   uint32_t magic;
-   uint32_t numfiles;
-} initrd_header_t;
+// Global data struct:
+initrd_file_header_t *files;
 
 fs_node_t* initialize_initrd(uint64_t location)
 {
-   // We need a valid malloc here!
-   initrd_root = (fs_node_t*)kmalloc(sizeof(fs_node_t));
+   uint32_t *u32ptr;
+
+   u32ptr = (uint32_t*)location;
+   if (*u32ptr != 0x1337babe)
+   {
+      printf("Invalid ramdisk magic\n");
+      return 0;
+   }
 
-   initrd_header_t *header = (initrd_header_t*)location;
-   initrd_root->length = header->numfiles;
+   // Allocate root dir:
+   fs_node_t *root = (fs_node_t*)kmalloc(sizeof(fs_node_t));
+   memset(root, 0, sizeof(fs_node_t));
+   root->flags = FS_DIRECTORY;
+   root->readdir = initrd_readdir;
+   
+   u32ptr++;
+   root->length = *u32ptr;
+   
+   // Allocate an node for each file.
+   void *x = kmalloc(sizeof(fs_node_t) * root->length);
+   memset(x, 0, sizeof(fs_node_t) * root->length);
 
-   return initrd_root;
+   for (i = 0; i < root->length; i++)
+   {
+      
+   }
+
+   return root;
 }
 
+void load_ramdisk()
+{
+   printf("Ramdisk location: %p\n", ramdisk_location);
+
+   fs_node_t *root = initialize_initrd(ramdisk_location);
+
+   if (root != 0)
+   {
+      fs_node_t *node = 0;
+      int i = 0;
+      while ( (node = readdir_fs(root, i)) != 0)
+      {
+         printf("File %s\n", node->name);
+      }
+   }
+}
+
+static uint64_t initrd_read(fs_node_t *node, uint64_t offset, uint64_t size, void *buffer)
+{
+   // TODO: check errors
+
+   if (size > node->length)
+   {
+      return 0;
+   }
+
+   if (size + offset > node->length)
+   {
+      size = node->length - offset;
+   }
+
+   memcpy(buffer, node.data + offset, size);
+   return size;
+}
+
+fs_node_t *readdir(fs_node_t *dir, int index)
+{
+   if (index >= dir->length)
+   {
+      return 0;
+   }
+}
+
--- a/cos/kernel/kernel.c	Tue Jan 24 17:54:16 2012 +0100
+++ b/cos/kernel/kernel.c	Fri Feb 03 17:56:41 2012 +0100
@@ -13,23 +13,7 @@
    // From here kmalloc can be used.
    keyboard_init();
    timer_init();
-   printf("Ramdisk location: %p\n", ramdisk_location);
-
-   /*
-   fs_node_t *fs_root = 0;
-   
-   fs_root = initialize_initrd(ramdisk_location);
-
-   if (fs_root != 0)
-   {
-      fs_dirent_t *node = 0;
-      int i = 0;
-      while ( (node = readdir_fs(fs_root, i)) != 0)
-      {
-
-      }
-   }
-   */
+   load_ramdisk();
 
    // TODO: make shell a user space program!
    shell(); // Start user shell
--- a/cos/kernel/kernel.h	Tue Jan 24 17:54:16 2012 +0100
+++ b/cos/kernel/kernel.h	Fri Feb 03 17:56:41 2012 +0100
@@ -267,6 +267,8 @@
 // Initial ramdisk functions:
 fs_node_t* initialize_initrd(uint64_t location);
 
+void load_ramdisk(void);
+
 // Variable argument list things:
 #define va_start(v,l)	__builtin_va_start(v,l)
 #define va_end(v)	__builtin_va_end(v)
--- a/cos/kernel/mm.c	Tue Jan 24 17:54:16 2012 +0100
+++ b/cos/kernel/mm.c	Fri Feb 03 17:56:41 2012 +0100
@@ -124,7 +124,7 @@
    }
 
    // Set the created mapping as active:
-   switch_mapping(kernel_map);
+   switch_mapping(kernel_map); // Loads cr3
 
    // TODO: set the use of placement malloc to invalid after here.
    // kernel_heap = create_heap(HEAP_START, HEAP_INITIAL_SIZE);
--- a/cos/makeinitrd.py	Tue Jan 24 17:54:16 2012 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,34 +0,0 @@
-#!/usr/bin/python
-
-# Script for generation of an initial ramdisk that contains 
-# the first programs to run.
-
-import struct
-import sys
-
-if len(sys.argv) < 3:
-   print('Usage: {0} output file1 [file2] ...'.format(sys.argv[0]))
-   sys.exit(1)
-
-outputfilename = sys.argv[1]
-inputFileNames = sys.argv[2:]
-
-with open(outputfilename, 'wb') as outputFile:
-   # Amount of files:
-   outputFile.write(struct.pack('<I', 0x1337babe))
-   outputFile.write(struct.pack('<I', len(inputFileNames)))
-   for inputFileName in inputFileNames:
-      # Magic:
-      outputFile.write(struct.pack('<I', 0xcafebabe))
-
-      # Filename:
-      fn = inputFileName.encode('ascii')
-      outputFile.write(struct.pack('<I', len(fn)))
-      outputFile.write(fn)
-
-      # Data:
-      with open(inputFileName, 'rb') as inputFile:
-         data = inputFile.read()
-      outputFile.write(struct.pack('<I', len(data)))
-      outputFile.write(data)
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cos/utils/makeinitrd.py	Fri Feb 03 17:56:41 2012 +0100
@@ -0,0 +1,34 @@
+#!/usr/bin/python
+
+# Script for generation of an initial ramdisk that contains 
+# the first programs to run.
+
+import struct
+import sys
+
+if len(sys.argv) < 3:
+   print('Usage: {0} output file1 [file2] ...'.format(sys.argv[0]))
+   sys.exit(1)
+
+outputfilename = sys.argv[1]
+inputFileNames = sys.argv[2:]
+
+with open(outputfilename, 'wb') as outputFile:
+   # Amount of files:
+   outputFile.write(struct.pack('<I', 0x1337babe))
+   outputFile.write(struct.pack('<I', len(inputFileNames)))
+   for inputFileName in inputFileNames:
+      # Magic:
+      outputFile.write(struct.pack('<I', 0xcafebabe))
+
+      # Filename:
+      fn = inputFileName.encode('ascii')
+      outputFile.write(struct.pack('<I', len(fn)))
+      outputFile.write(fn)
+
+      # Data:
+      with open(inputFileName, 'rb') as inputFile:
+         data = inputFile.read()
+      outputFile.write(struct.pack('<I', len(data)))
+      outputFile.write(data)
+