Skip to content

Commit 7330e8f

Browse files
김중수김중수
김중수
authored and
김중수
committed
modified create root
1 parent 3bbc340 commit 7330e8f

File tree

2 files changed

+162
-116
lines changed

2 files changed

+162
-116
lines changed

disk.h

+2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ typedef struct DISK_OPERATIONS
77
{
88
int ( *read_sector )( struct DISK_OPERATIONS*, SECTOR, void* );
99
int ( *write_sector )( struct DISK_OPERATIONS*, SECTOR, const void* );
10+
// 블록의 갯수
1011
SECTOR numberOfSectors;
12+
// 섹터당 1024바이트다
1113
int bytesPerSector;
1214
void* pdata;
1315
} DISK_OPERATIONS;

ext2.c

+160-116
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
typedef struct
22
{
3-
char* address;
3+
char* address;
44
} DISK_MEMORY;
55

66
#include "ext2.h"
7-
#define MIN( a, b ) ( ( a ) < ( b ) ? ( a ) : ( b ) )
8-
#define MAX( a, b ) ( ( a ) > ( b ) ? ( a ) : ( b ) )
7+
#define MIN( a, b ) ( ( a ) < ( b ) ? ( a ) : ( b ) )
8+
#define MAX( a, b ) ( ( a ) > ( b ) ? ( a ) : ( b ) )
99

1010
int ext2_write(EXT2_NODE* file, unsigned long offset, unsigned long length, const char* buffer)
1111
{
@@ -15,148 +15,192 @@ UINT32 get_free_inode_number(EXT2_FILESYSTEM* fs);
1515

1616
int ext2_format(DISK_OPERATIONS* disk)
1717
{
18-
EXT2_SUPER_BLOCK sb;
19-
EXT2_GROUP_DESCRIPTOR gd;
20-
EXT2_GROUP_DESCRIPTOR gd_another_group;
18+
EXT2_SUPER_BLOCK sb;
19+
EXT2_GROUP_DESCRIPTOR gd;
20+
EXT2_GROUP_DESCRIPTOR gd_another_group;
2121

22-
QWORD sector_num_per_group = (disk->numberOfSectors - 1) / NUMBER_OF_GROUPS;
23-
int i, gi, j;
24-
const int BOOT_SECTOR_BASE = 1;
25-
char sector[MAX_SECTOR_SIZE];
22+
QWORD sector_num_per_group = (disk->numberOfSectors - 1) / NUMBER_OF_GROUPS;
23+
int i, gi, j;
24+
const int BOOT_SECTOR_BASE = 1;
25+
char sector[MAX_SECTOR_SIZE];
2626

27-
if (fill_super_block(&sb, disk->numberOfSectors, disk->bytesPerSector) != EXT2_SUCCESS)
28-
return EXT2_ERROR;
27+
if (fill_super_block(&sb, disk->numberOfSectors, disk->bytesPerSector) != EXT2_SUCCESS)
28+
return EXT2_ERROR;
2929

30-
ZeroMemory(sector, sizeof(sector));
31-
memcpy(sector, &sb, sizeof(sb));
32-
disk->write_sector(disk, BOOT_SECTOR_BASE + 0, sector);
30+
ZeroMemory(sector, sizeof(sector));
31+
memcpy(sector, &sb, sizeof(sb));
32+
disk->write_sector(disk, BOOT_SECTOR_BASE + 0, sector);
3333

34-
if (fill_descriptor_block(&gd, &sb, disk->numberOfSectors, disk->bytesPerSector) != EXT2_SUCCESS)
35-
return EXT2_ERROR;
34+
if (fill_descriptor_block(&gd, &sb, disk->numberOfSectors, disk->bytesPerSector) != EXT2_SUCCESS)
35+
return EXT2_ERROR;
3636

37-
gd_another_group = gd;
38-
gd_another_group.free_inodes_count = NUMBER_OF_INODES / NUMBER_OF_GROUPS;
39-
gd_another_group.free_blocks_count = sb.free_block_count / NUMBER_OF_GROUPS;
37+
gd_another_group = gd;
38+
gd_another_group.free_inodes_count = NUMBER_OF_INODES / NUMBER_OF_GROUPS;
39+
gd_another_group.free_blocks_count = sb.free_block_count / NUMBER_OF_GROUPS;
4040

41-
ZeroMemory(sector, sizeof(sector));
42-
for (j = 0; j < NUMBER_OF_GROUPS; j++)
43-
{
44-
if (j == 0)memcpy(sector + j * sizeof(gd), &gd, sizeof(gd));
45-
else memcpy(sector + j * sizeof(gd_another_group), &gd_another_group, sizeof(gd_another_group));
46-
}
41+
ZeroMemory(sector, sizeof(sector));
42+
for (j = 0; j < NUMBER_OF_GROUPS; j++)
43+
{
44+
if (j == 0)memcpy(sector + j * sizeof(gd), &gd, sizeof(gd));
45+
else memcpy(sector + j * sizeof(gd_another_group), &gd_another_group, sizeof(gd_another_group));
46+
}
4747

48-
disk->write_sector(disk, BOOT_SECTOR_BASE + 1, sector);
48+
disk->write_sector(disk, BOOT_SECTOR_BASE + 1, sector);
4949

50-
// block bitmap
51-
ZeroMemory(sector, sizeof(sector));
50+
// block bitmap
51+
ZeroMemory(sector, sizeof(sector));
5252

53-
sector[0] = 0xff;
54-
sector[1] = 0xff;
55-
sector[2] = 0x01;
56-
disk->write_sector(disk, BOOT_SECTOR_BASE + 2, sector);
53+
sector[0] = 0xff;
54+
sector[1] = 0xff;
55+
sector[2] = 0x01;
56+
disk->write_sector(disk, BOOT_SECTOR_BASE + 2, sector);
5757

58-
// inode bitmap
59-
ZeroMemory(sector, sizeof(sector));
58+
// inode bitmap
59+
ZeroMemory(sector, sizeof(sector));
6060

61-
sector[0] = 0xff;
62-
sector[1] = 0x03;
63-
disk->write_sector(disk, BOOT_SECTOR_BASE + 3, sector);
61+
sector[0] = 0xff;
62+
sector[1] = 0x03;
63+
disk->write_sector(disk, BOOT_SECTOR_BASE + 3, sector);
6464

65-
// inode table
66-
ZeroMemory(sector, sizeof(sector));
65+
// inode table
66+
ZeroMemory(sector, sizeof(sector));
6767

68-
for (i = 4; i < sb.first_data_block_each_group; i++)
69-
disk->write_sector(disk, BOOT_SECTOR_BASE + i, sector);
68+
for (i = 4; i < sb.first_data_block_each_group; i++)
69+
disk->write_sector(disk, BOOT_SECTOR_BASE + i, sector);
7070

71-
for (gi = 1; gi < NUMBER_OF_GROUPS; gi++)
72-
{
73-
sb.block_group_number = gi;
71+
for (gi = 1; gi < NUMBER_OF_GROUPS; gi++)
72+
{
73+
sb.block_group_number = gi;
7474

75-
ZeroMemory(sector, sizeof(sector));
76-
memcpy(sector, &sb, sizeof(sb));
75+
ZeroMemory(sector, sizeof(sector));
76+
memcpy(sector, &sb, sizeof(sb));
7777

78-
disk->write_sector(disk, sector_num_per_group * gi + BOOT_SECTOR_BASE, sector);
78+
disk->write_sector(disk, sector_num_per_group * gi + BOOT_SECTOR_BASE, sector);
7979

80-
ZeroMemory(sector, sizeof(sector));
81-
for (j = 0; j < NUMBER_OF_GROUPS; j++)
82-
{
83-
memcpy(sector + j * sizeof(gd), &gd, sizeof(gd));
84-
}
85-
disk->write_sector(disk, sector_num_per_group * gi + BOOT_SECTOR_BASE + 1, sector);
80+
ZeroMemory(sector, sizeof(sector));
81+
for (j = 0; j < NUMBER_OF_GROUPS; j++)
82+
{
83+
memcpy(sector + j * sizeof(gd), &gd, sizeof(gd));
84+
}
85+
disk->write_sector(disk, sector_num_per_group * gi + BOOT_SECTOR_BASE + 1, sector);
8686

87-
// block bitmap
88-
ZeroMemory(sector, sizeof(sector));
89-
sector[0] = 0xff;
90-
sector[1] = 0xff;
91-
sector[2] = 0x01;
92-
disk->write_sector(disk, sector_num_per_group * gi + BOOT_SECTOR_BASE + 2, sector);
87+
// block bitmap
88+
ZeroMemory(sector, sizeof(sector));
89+
sector[0] = 0xff;
90+
sector[1] = 0xff;
91+
sector[2] = 0x01;
92+
disk->write_sector(disk, sector_num_per_group * gi + BOOT_SECTOR_BASE + 2, sector);
9393

94-
//inode bitmap
95-
ZeroMemory(sector, sizeof(sector));
94+
//inode bitmap
95+
ZeroMemory(sector, sizeof(sector));
9696

97-
disk->write_sector(disk, sector_num_per_group * gi + BOOT_SECTOR_BASE + 3, sector);
97+
disk->write_sector(disk, sector_num_per_group * gi + BOOT_SECTOR_BASE + 3, sector);
9898

99-
// inode table
100-
ZeroMemory(sector, sizeof(sector));
101-
for (i = 4; i < sb.first_data_block_each_group; i++)
102-
disk->write_sector(disk, sector_num_per_group * gi + BOOT_SECTOR_BASE + i, sector);
103-
}
99+
// inode table
100+
ZeroMemory(sector, sizeof(sector));
101+
for (i = 4; i < sb.first_data_block_each_group; i++)
102+
disk->write_sector(disk, sector_num_per_group * gi + BOOT_SECTOR_BASE + i, sector);
103+
}
104104

105-
PRINTF("max inode count : %u\n", sb.max_inode_count);
106-
PRINTF("total block count : %u\n", sb.block_count);
107-
PRINTF("byte size of inode structure : %u\n", sb.inode_structure_size);
108-
PRINTF("block byte size : %u\n", MAX_BLOCK_SIZE);
109-
PRINTF("total sectors count : %u\n", NUMBER_OF_SECTORS);
110-
PRINTF("sector byte size : %u\n", MAX_SECTOR_SIZE);
111-
PRINTF("\n");
105+
PRINTF("max inode count : %u\n", sb.max_inode_count);
106+
PRINTF("total block count : %u\n", sb.block_count);
107+
PRINTF("byte size of inode structure : %u\n", sb.inode_structure_size);
108+
PRINTF("block byte size : %u\n", MAX_BLOCK_SIZE);
109+
PRINTF("total sectors count : %u\n", NUMBER_OF_SECTORS);
110+
PRINTF("sector byte size : %u\n", MAX_SECTOR_SIZE);
111+
PRINTF("\n");
112112

113-
create_root(disk, &sb);
113+
create_root(disk, &sb);
114114

115-
return EXT2_SUCCESS;
115+
return EXT2_SUCCESS;
116116
}
117117

118118
int fill_super_block(EXT2_SUPER_BLOCK * sb, SECTOR numberOfSectors, UINT32 bytesPerSector)
119119
{
120-
ZeroMemory(sb, sizeof(EXT2_SUPER_BLOCK));
121-
122-
sb->max_inode_count = NUMBER_OF_INODES;
123-
sb->block_count = numberOfSectors;
124-
sb->reserved_block_count = 0;
125-
sb->free_block_count = numberOfSectors - (17 * NUMBER_OF_GROUPS) - 1;
126-
sb->free_inode_count = NUMBER_OF_INODES - 10;
127-
sb->first_data_block = 1;
128-
sb->log_block_size = 0;
129-
sb->log_fragmentation_size = 0;
130-
sb->block_per_group = (numberOfSectors - 1) / NUMBER_OF_GROUPS;
131-
sb->fragmentation_per_group = 0;
132-
sb->inode_per_group = NUMBER_OF_INODES / NUMBER_OF_GROUPS;
133-
sb->magic_signature = 0xEF53;
134-
sb->errors = 0;
135-
sb->first_non_reserved_inode = 11;
136-
sb->inode_structure_size = 128;
137-
sb->block_group_number = 0;
138-
sb->first_data_block_each_group = 1 + 1 + 1 + 1 + 13;
139-
140-
return EXT2_SUCCESS;
120+
ZeroMemory(sb, sizeof(EXT2_SUPER_BLOCK));
121+
122+
sb->max_inode_count = NUMBER_OF_INODES; // 아이노드의 갯수 (200)
123+
sb->block_count = numberOfSectors; // 4097개
124+
sb->reserved_block_count = 0; // 예약된 블록의 갯수
125+
sb->free_block_count = numberOfSectors - (17 * NUMBER_OF_GROUPS) - 1; //
126+
sb->free_inode_count = NUMBER_OF_INODES - 10;// 아이노드중에서 10개는 이미 사용중
127+
sb->first_data_block = 1; //
128+
sb->log_block_size = 0;
129+
sb->log_fragmentation_size = 0;
130+
sb->block_per_group = (numberOfSectors - 1) / NUMBER_OF_GROUPS; // 2048개
131+
sb->fragmentation_per_group = 0;
132+
sb->inode_per_group = NUMBER_OF_INODES / NUMBER_OF_GROUPS; // 그룹당 아이노드의 갯수 100개
133+
sb->magic_signature = 0xEF53; // 슈퍼블록인것을 알기 위한 단서
134+
sb->errors = 0; // 에러의 갯수
135+
sb->first_non_reserved_inode = 11; // 아이노드중에서 10개는 사용중이라서 11번째 부터 사용가능하다
136+
sb->inode_structure_size = 128; // 아이노드의 크기는 128바이트이다
137+
sb->block_group_number = 0;
138+
sb->first_data_block_each_group = 1 + 1 + 1 + 1 + 13; // 첫번째 데이터 블록
139+
return EXT2_SUCCESS;
141140
}
142141

143142
int fill_descriptor_block(EXT2_GROUP_DESCRIPTOR * gd, EXT2_SUPER_BLOCK * sb, SECTOR numberOfSectors, UINT32 bytesPerSector)
144143
{
145-
ZeroMemory(gd, sizeof(EXT2_GROUP_DESCRIPTOR));
144+
ZeroMemory(gd, sizeof(EXT2_GROUP_DESCRIPTOR));
146145

147-
gd->start_block_of_block_bitmap = 2;
148-
gd->start_block_of_inode_bitmap = 3;
149-
gd->start_block_of_inode_table = 4;
150-
gd->free_blocks_count = (UINT16)(sb->free_block_count / NUMBER_OF_GROUPS + sb->free_block_count % NUMBER_OF_GROUPS);
151-
gd->free_inodes_count = (UINT16)(((sb->free_inode_count) + 10) / NUMBER_OF_GROUPS - 10);
152-
gd->directories_count = 0;
146+
gd->start_block_of_block_bitmap = 2;
147+
gd->start_block_of_inode_bitmap = 3;
148+
gd->start_block_of_inode_table = 4;
149+
gd->free_blocks_count = (UINT16)(sb->free_block_count / NUMBER_OF_GROUPS + sb->free_block_count % NUMBER_OF_GROUPS);
150+
gd->free_inodes_count = (UINT16)(((sb->free_inode_count) + 10) / NUMBER_OF_GROUPS - 10);
151+
gd->directories_count = 0;
153152

154-
return EXT2_SUCCESS;
153+
return EXT2_SUCCESS;
155154
}
156155

157156
int create_root(DISK_OPERATIONS* disk, EXT2_SUPER_BLOCK * sb)
158-
{}
159-
157+
{
158+
EXT2_DIR_ENTRY * entry ;
159+
BYTE sector[MAX_SECTOR_SIZE];
160+
INODE *id;
161+
EXT2_GROUP_DESCRIPTOR * gd;
162+
SECTOR rootsector =0;
163+
EXT2_SUPER_BLOCK * sb2;
164+
ZeroMemory(sector,MAX_SECTOR_SIZE);
165+
entry= (EXT2_DIR_ENTRY *)sector;
166+
memcpy(entry->name,VOLUME_LABLE,13);
167+
entry->name_len=VOLUME_LABLE;
168+
entry->inode=2;
169+
entry++;
170+
entry->name[0]= DIR_ENTRY_NO_MORE;
171+
rootsector= 1+sb->first_data_block_each_group; // 부트 코드 땜에 1 더해줌
172+
disk->write_sector(disk,rootsector,sector);
173+
// 지금까지 해당 엔트리를 만들어서 넣었고 이제 inode 테이블하고 superblock하고 groupdescriptor 내용 바꾸는 일 해야함
174+
ZeroMemory(sector,MAX_SECTOR_SIZE);
175+
sb2= (EXT2_SUPER_BLOCK *)sector ;
176+
disk->read_sector(disk,1,sector);
177+
sb2->free_block_count --;
178+
sb2->free_inode_count --;
179+
disk->write_sector(disk,1,sb2);
180+
disk->write_sector(disk,1+sb2->block_per_group,sb2);
181+
gd= (EXT2_GROUP_DESCRIPTOR *)sector ;
182+
ZeroMemory(sector,MAX_SECTOR_SIZE);
183+
disk->read_sector(disk,2,gd);
184+
gd-> free_blocks_count --;
185+
gd-> free_inodes_count --;
186+
gd-> directories_count ++;
187+
for(int i=0; i<NUMBER_OF_GROUPS; i++)
188+
disk-> write_sector(disk,2+i*sb->block_per_group,gd);// 그룹 디스크립터 할당
189+
ZeroMemory(sector, sizeof(sector));
190+
disk->read_sector(disk, 3,sector);
191+
sector[2] |= 0x02;
192+
disk->write_sector(disk,3,sector); // 블록 비트맵 할당
193+
ZeroMemory(sector, MAX_SECTOR_SIZE);
194+
id= (INODE *)sector;
195+
disk->read_sector(disk,5,sector); // 아이노드 테이블
196+
id ++;
197+
id->mode = 0x4000 ;
198+
id->size =0 ;
199+
id->uid = 0;
200+
id->block[0]= 1+sb->first_data_block_each_group ;
201+
disk->write_sector(disk,5,sector);
202+
return EXT2_SUCCESS ;
203+
}
160204
void process_meta_data_for_inode_used(EXT2_NODE * retEntry, UINT32 inode_num, int fileType)
161205
{
162206
}
@@ -197,11 +241,11 @@ int isdigit(unsigned char ch);
197241

198242
void upper_string(char* str, int length)
199243
{
200-
while (*str && length-- > 0)
201-
{
202-
*str = toupper(*str);
203-
str++;
204-
}
244+
while (*str && length-- > 0)
245+
{
246+
*str = toupper(*str);
247+
str++;
248+
}
205249
}
206250

207251
int format_name(EXT2_FILESYSTEM* fs, char* name)
@@ -267,10 +311,10 @@ int read_dir_from_sector(EXT2_FILESYSTEM* fs, BYTE* sector, EXT2_NODE_ADD adder,
267311

268312
char* my_strncpy(char* dest, const char* src, int length)
269313
{
270-
while (*src && *src != 0x20 && length-- > 0)
271-
*dest++ = *src++;
314+
while (*src && *src != 0x20 && length-- > 0)
315+
*dest++ = *src++;
272316

273-
return dest;
317+
return dest;
274318
}
275319

276320
int ext2_mkdir(const EXT2_NODE* parent, const char* entryName, EXT2_NODE* retEntry)

0 commit comments

Comments
 (0)