Release version 0.5
[thunix.git] / fs / ext2 / ram_mke2fs.c
blobb0b41d6d2a9e89cd3cc4b54639f5a003474490d3
1 /**
2 * thunix/fs/ramfs/ram_mke2fs.c
4 * make an ext2 ram fs
5 * yeah, it's far-fetched, all the things done here
6 * are by ourself, all the things here are static.
8 * but, it just a TEST for ext2_file system,
9 * and that's enough.
11 * Copyright (c) 2008 - 2009 by Aleaxander
13 * Aleaxander@gmail.com
17 #include <fs_ext2.h>
18 #include <thunix.h>
19 #include <rd.h>
20 #include <string.h>
23 void ram_ext2_init_super_block()
25 struct ext2_super_block *sb;
27 sb = (struct ext2_super_block *) (ram_start + EXT2_BLOCK_SIZE);
30 /* just init. some nessesary filed */
31 sb->s_inodes_count = 184;
32 sb->s_blocks_count = 1440;
33 sb->s_r_blocks_count = 72;
34 sb->s_free_blocks_count = 1393;
35 sb->s_free_inodes_count = 173;
36 sb->s_first_data_block = 1;
37 sb->s_log_block_size = 0;
38 sb->s_log_frag_size = 0;
39 sb->s_blocks_per_group = 8192;
40 sb->s_frags_per_group = 8192;
41 sb->s_inodes_per_group = 184;
43 sb->s_magic = 0x0ef53;
47 void ram_ext2_init_group_desc()
49 struct ext2_group_desc *desc;
51 desc = (struct ext2_group_desc *)(ram_start + EXT2_BLOCK_SIZE * 2);
53 desc->bg_block_bitmap = 8;
54 desc->bg_inode_bitmap = 9;
55 desc->bg_inode_table = 10;
56 desc->bg_free_blocks_count = 1393;
57 desc->bg_free_inodes_count = 173;
58 desc->bg_used_dirs_count = 2;
60 /* we just make it to be so, but in fact ,
61 * i think this are be done by ext_mkdir,
62 * ext2_alloc_block or something of that
63 * sort.
65 * we just do it for test.
70 void ram_ext2_init_bitmap()
71 {/*
72 struct ext2_group_desc *desc;
73 desc = ext2_get_group(0);
75 unsigned long *block_bitmap;
76 unsigned char *inode_bitmap;
78 block_bitmap = (unsigned long *)(ram_start + EXT2_BLOCK_SIZE * 8);
79 inode_bitmap = (unsigned char* )(ram_start + EXT2_BLOCK_SIZE * 9);
81 *block_bitmap = 0xffffffff;
82 *(++block_bitmap) = 0x3fff;
84 *inode_bitmap = 0xff;
85 *(++inode_bitmap) = 0xf0;
89 void ram_ext2_init_root_inode()
91 struct ext2_inode *root_inode;
92 unsigned long inode_table;
94 inode_table = ram_start + EXT2_BLOCK_SIZE * 10;
96 root_inode = (struct ext2_inode *)inode_table + (ROOT_INODE - 1) ;
97 memset(root_inode, 0, sizeof (struct ext2_inode));
99 root_inode->i_size = 0x400; /* 1K */
100 root_inode->i_links_count = 3;
101 root_inode->i_blocks = 2;
102 root_inode->i_block[0] = 33;
109 void ram_ext2fs_init()
111 EXT2_DEBUG();
113 ram_ext2_init_super_block();
114 ram_ext2_init_group_desc();
115 ram_ext2_init_bitmap();
116 ram_ext2_init_root_inode();
121 struct m_inode * ram_ext2_get_root_inode()
123 struct ext2_inode *root_inode;
124 unsigned long inode_table;
126 inode_table = ram_start + EXT2_BLOCK_SIZE * 10;
128 root_inode = (struct ext2_inode *)inode_table + (ROOT_INODE - 1) ;
130 return (struct m_inode*)root_inode;
133 void ram_mke2fs()
135 int block;
136 void * block_buffer;
138 struct m_inode *root_inode;
139 struct ext2_dir_entry root = {2,12,1,2,"."};
140 struct ext2_dir_entry root_dot_dot = {2,12,2,2,".."};
141 struct ext2_dir_entry lost = {11,20,10,2,"lost+found"};
143 EXT2_DEBUG();
145 root_inode = ram_ext2_get_root_inode ();
147 block = root_inode->inode.i_block[0];
148 block_buffer = (void *)(ram_start + EXT2_BLOCK_SIZE * block);
150 memcpy(block_buffer, &root, EXT2_DIR_REC_LEN(1));
151 block_buffer += EXT2_DIR_REC_LEN(1);
153 memcpy(block_buffer, &root_dot_dot, EXT2_DIR_REC_LEN(1));
154 block_buffer += EXT2_DIR_REC_LEN(2);
156 memcpy(block_buffer, &lost, EXT2_DIR_REC_LEN(10));