kern
view src/fs.c @ 91:f83f50c17c3b
continuing with the fs
added strtol and strstr to klibc
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 09 Dec 2011 15:29:54 +0200 |
parents | 2f555c81ae67 |
children | 083849df660b |
line source
1 /* This code is used by the kernel AND by userspace filesystem-related tools.
2 * The kernel-specific parts are conditionally compiled in #ifdef KERNEL blocks
3 * the rest of the code should be independent.
4 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <assert.h>
8 #include <errno.h>
9 #include "fs.h"
10 #include "part.h"
12 #ifdef KERNEL
13 #include "ata.h"
14 #include "panic.h"
15 #endif
17 struct filesys {
18 int dev;
19 struct partition part;
21 struct superblock *sb;
23 struct filesys *next;
24 };
26 static int find_rootfs(struct filesys *fs);
27 static int readblock(int dev, uint32_t blk, void *buf);
28 static int writeblock(int dev, uint32_t blk, void *buf);
30 /* root device & partition */
31 static struct filesys *fslist;
33 int sys_mount(char *mtpt, char *devname, unsigned int flags)
34 {
35 if(strcmp(mtpt, "/") != 0) {
36 printf("mount: only root can be mounted at the moment\n");
37 return -EBUG;
38 }
40 /* mounting root filesystem */
41 if(fslist) {
43 }
45 void init_fs(void)
46 {
47 root.sb = malloc(512);
48 assert(root.sb);
50 #ifdef KERNEL
51 if(find_rootfs(&root) == -1) {
52 panic("can't find root filesystem\n");
53 }
54 #endif
55 }
58 #ifdef KERNEL
59 #define PART_TYPE 0xcc
60 static int find_rootfs(struct filesys *fs)
61 {
62 int i, num_dev, partid;
63 struct partition *plist, *p;
65 num_dev = ata_num_devices();
66 for(i=0; i<num_dev; i++) {
67 plist = p = get_part_list(i);
69 partid = 0;
70 while(p) {
71 if(get_part_type(p) == PART_TYPE) {
72 /* found the correct partition, now read the superblock
73 * and make sure it's got the correct magic id
74 */
75 readblock(i, p->start_sect / 2 + 1, fs->sb);
77 if(fs->sb->magic == MAGIC) {
78 printf("found root ata%dp%d\n", i, partid);
79 fs->dev = i;
80 fs->part = *p;
81 return 0;
82 }
83 }
84 p = p->next;
85 partid++;
86 }
87 free_part_list(plist);
88 }
89 return -1;
90 }
92 #define NSECT (BLKSZ / 512)
94 static int readblock(struct block_device *bdev, uint32_t blk, void *buf)
95 {
96 return blk_read(bdev, blk, buf);
97 }
99 static int writeblock(struct block_device *bdev, uint32_t blk, void *buf)
100 {
101 return blk_write(bdev, blk, buf);
102 }
103 #else
105 /* if this is compiled as part of the user-space tools instead of the kernel
106 * forward the call to a user read/write block function supplied by the app.
107 */
108 int user_readblock(uint32_t, void*);
109 int user_writeblock(uint32_t, void*);
111 static int readblock(struct block_device *bdev, uint32_t blk, void *buf)
112 {
113 return user_readblock(blk, buf);
114 }
116 static int writeblock(struct block_device *bdev, uint32_t blk, void *buf)
117 {
118 return user_writeblock(blk, buf);
119 }
120 #endif /* KERNEL */