annotate cos/kernel/initrd.c @ 42:980b2f14c9be

Merge
author windel
date Fri, 03 Feb 2012 18:01:18 +0100
parents 35cc54e078dd
children e47bfef80baf
rev   line source
32
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
1 /*
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
2 * Initial ramdisk filesystem driver.
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
3 * */
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
4
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
5 #include "kernel.h"
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
6
41
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
7 // Global data struct:
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
8 initrd_file_header_t *files;
32
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
9
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
10 fs_node_t* initialize_initrd(uint64_t location)
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
11 {
41
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
12 uint32_t *u32ptr;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
13
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
14 u32ptr = (uint32_t*)location;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
15 if (*u32ptr != 0x1337babe)
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
16 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
17 printf("Invalid ramdisk magic\n");
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
18 return 0;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
19 }
32
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
20
41
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
21 // Allocate root dir:
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
22 fs_node_t *root = (fs_node_t*)kmalloc(sizeof(fs_node_t));
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
23 memset(root, 0, sizeof(fs_node_t));
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
24 root->flags = FS_DIRECTORY;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
25 root->readdir = initrd_readdir;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
26
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
27 u32ptr++;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
28 root->length = *u32ptr;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
29
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
30 // Allocate an node for each file.
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
31 void *x = kmalloc(sizeof(fs_node_t) * root->length);
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
32 memset(x, 0, sizeof(fs_node_t) * root->length);
32
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
33
41
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
34 for (i = 0; i < root->length; i++)
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
35 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
36
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
37 }
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
38
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
39 return root;
32
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
40 }
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
41
41
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
42 void load_ramdisk()
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
43 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
44 printf("Ramdisk location: %p\n", ramdisk_location);
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
45
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
46 fs_node_t *root = initialize_initrd(ramdisk_location);
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
47
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
48 if (root != 0)
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
49 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
50 fs_node_t *node = 0;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
51 int i = 0;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
52 while ( (node = readdir_fs(root, i)) != 0)
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
53 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
54 printf("File %s\n", node->name);
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
55 }
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
56 }
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
57 }
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
58
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
59 static uint64_t initrd_read(fs_node_t *node, uint64_t offset, uint64_t size, void *buffer)
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
60 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
61 // TODO: check errors
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
62
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
63 if (size > node->length)
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
64 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
65 return 0;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
66 }
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
67
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
68 if (size + offset > node->length)
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
69 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
70 size = node->length - offset;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
71 }
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
72
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
73 memcpy(buffer, node.data + offset, size);
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
74 return size;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
75 }
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
76
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
77 fs_node_t *readdir(fs_node_t *dir, int index)
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
78 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
79 if (index >= dir->length)
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
80 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
81 return 0;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
82 }
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
83 }
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
84