annotate cos/kernel/initrd.c @ 174:3eb06f5fb987

Added memory alloc for locals
author Windel Bouwman
date Fri, 19 Apr 2013 19:22:52 +0200
parents e47bfef80baf
children
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:
43
e47bfef80baf Fixed build by commenting out initrd code
windel
parents: 41
diff changeset
8 //initrd_file_header_t *files;
32
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
9
43
e47bfef80baf Fixed build by commenting out initrd code
windel
parents: 41
diff changeset
10 /*
32
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
11 fs_node_t* initialize_initrd(uint64_t location)
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
12 {
41
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
13 uint32_t *u32ptr;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
14
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
15 u32ptr = (uint32_t*)location;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
16 if (*u32ptr != 0x1337babe)
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
17 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
18 printf("Invalid ramdisk magic\n");
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
19 return 0;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
20 }
32
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
21
41
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
22 // Allocate root dir:
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
23 fs_node_t *root = (fs_node_t*)kmalloc(sizeof(fs_node_t));
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
24 memset(root, 0, sizeof(fs_node_t));
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
25 root->flags = FS_DIRECTORY;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
26 root->readdir = initrd_readdir;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
27
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
28 u32ptr++;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
29 root->length = *u32ptr;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
30
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
31 // Allocate an node for each file.
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
32 void *x = kmalloc(sizeof(fs_node_t) * root->length);
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
33 memset(x, 0, sizeof(fs_node_t) * root->length);
32
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
34
41
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
35 for (i = 0; i < root->length; i++)
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
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
40 return root;
32
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
41 }
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents:
diff changeset
42
43
e47bfef80baf Fixed build by commenting out initrd code
windel
parents: 41
diff changeset
43 */
e47bfef80baf Fixed build by commenting out initrd code
windel
parents: 41
diff changeset
44
41
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
45 void load_ramdisk()
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
46 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
47 printf("Ramdisk location: %p\n", ramdisk_location);
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
48
43
e47bfef80baf Fixed build by commenting out initrd code
windel
parents: 41
diff changeset
49 /*
41
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
50 fs_node_t *root = initialize_initrd(ramdisk_location);
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
51
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
52 if (root != 0)
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
53 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
54 fs_node_t *node = 0;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
55 int i = 0;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
56 while ( (node = readdir_fs(root, i)) != 0)
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
57 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
58 printf("File %s\n", node->name);
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
59 }
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
60 }
43
e47bfef80baf Fixed build by commenting out initrd code
windel
parents: 41
diff changeset
61 */
41
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
62 }
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
63
43
e47bfef80baf Fixed build by commenting out initrd code
windel
parents: 41
diff changeset
64 /*
41
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
65 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
66 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
67 // TODO: check errors
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
68
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
69 if (size > node->length)
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
70 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
71 return 0;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
72 }
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
73
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
74 if (size + offset > node->length)
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
75 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
76 size = node->length - offset;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
77 }
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
78
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
79 memcpy(buffer, node.data + offset, size);
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
80 return size;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
81 }
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
82
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
83 fs_node_t *readdir(fs_node_t *dir, int index)
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
84 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
85 if (index >= dir->length)
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
86 {
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
87 return 0;
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
88 }
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
89 }
43
e47bfef80baf Fixed build by commenting out initrd code
windel
parents: 41
diff changeset
90 */
41
35cc54e078dd Added memory functions
windel
parents: 32
diff changeset
91