-
Notifications
You must be signed in to change notification settings - Fork 3
/
ext2_shell.c
332 lines (256 loc) · 8.04 KB
/
ext2_shell.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include "ext2_shell.h"
typedef struct {
char * address;
}DISK_MEMORY;
static SHELL_FILE_OPERATIONS g_file =
{
fs_create,
fs_remove,
fs_read,
fs_write
};
static SHELL_FS_OPERATIONS g_fsOprs =
{
fs_read_dir,
NULL,
fs_mkdir,
fs_rmdir,
fs_lookup,
&g_file,
NULL
};
static SHELL_FILESYSTEM g_fat =
{
"EXT2",
fs_mount,
fs_umount,
fs_format
};
/******************************************************************************/
/* */
/* FS_COMMAND */
/* */
/******************************************************************************/
int fs_mount(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, SHELL_ENTRY* root)
{
EXT2_FILESYSTEM* fs;
EXT2_NODE ext2_entry;
int result;
*fsOprs = g_fsOprs;
fsOprs->pdata = malloc(sizeof(EXT2_FILESYSTEM));
fs = FSOPRS_TO_EXT2FS(fsOprs);
ZeroMemory(fs, sizeof(EXT2_FILESYSTEM));
fs->disk = disk;
result = read_superblock(fs, &ext2_entry);
if (result == EXT2_SUCCESS)
{
printf("number of groups : %d\n", NUMBER_OF_GROUPS);
printf("blocks per group : %d\n", fs->sb.block_per_group);
printf("bytes per block : %d\n", disk->bytesPerSector);
printf("free block count : %d\n", fs->sb.free_block_count);
printf("free inode count : %d\n", fs->sb.free_inode_count);
printf("first non reserved inode : %d\n", fs->sb.first_non_reserved_inode);
printf("inode structure size : %d\n", fs->sb.inode_structure_size);
printf("first data block number : %d\n\n", fs->sb.first_data_block_each_group);
}
ext2_entry_to_shell_entry(&ext2_entry, root);
return result;
}
void fs_umount(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs)
{
return;
}
int fs_format(DISK_OPERATIONS* disk)
{
printf("formatting as a EXT2\n");
ext2_format(disk);
return 1;
}
int fs_read( DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, const SHELL_ENTRY* parent, SHELL_ENTRY* entry, unsigned long offset, unsigned long length, char* buffer )
{
EXT2_NODE EXT2Entry;
shell_entry_to_ext2_entry( entry, &EXT2Entry );
return ext2_read( &EXT2Entry, offset, length, buffer );
}
int fs_write(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, const SHELL_ENTRY* parent, SHELL_ENTRY* entry, unsigned long offset, unsigned long length, const char* buffer)
{
EXT2_NODE EXT2Entry;
shell_entry_to_ext2_entry(entry, &EXT2Entry);
return ext2_write(&EXT2Entry, offset, length, buffer);
}
int fs_create(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, const SHELL_ENTRY* parent, const char* name, SHELL_ENTRY* retEntry)
{
EXT2_NODE EXT2Parent;
EXT2_NODE EXT2Entry;
int result;
shell_entry_to_ext2_entry(parent, &EXT2Parent);
result = ext2_create(&EXT2Parent, name, &EXT2Entry);
ext2_entry_to_shell_entry(&EXT2Entry, retEntry);
return result;
}
int fs_mkdir(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, const SHELL_ENTRY* parent, const char* name, SHELL_ENTRY* retEntry)
{
EXT2_NODE EXT2_Parent;
EXT2_NODE EXT2_Entry;
int result;
if (is_exist(disk, fsOprs, parent, name))
return EXT2_ERROR;
shell_entry_to_ext2_entry(parent, &EXT2_Parent);
result = ext2_mkdir(&EXT2_Parent, name, &EXT2_Entry);
ext2_entry_to_shell_entry(&EXT2_Entry, retEntry);
return result;
}
int fs_lookup(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, const SHELL_ENTRY* parent, SHELL_ENTRY* entry, const char* name)
{
EXT2_NODE EXT2Parent;
EXT2_NODE EXT2Entry;
int result;
shell_entry_to_ext2_entry(parent, &EXT2Parent);
if (result = ext2_lookup(&EXT2Parent, name, &EXT2Entry))
return result;
ext2_entry_to_shell_entry(&EXT2Entry, entry);
return result;
}
int fs_read_dir(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, const SHELL_ENTRY* parent, SHELL_ENTRY_LIST* list)
{
EXT2_NODE entry;
if (list->count)
release_entry_list(list);
shell_entry_to_ext2_entry(parent, &entry);
ext2_read_dir(&entry, adder, list);
return EXT2_SUCCESS;
}
int fs_rmdir(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, const SHELL_ENTRY* parent, const char* name)
{
EXT2_NODE EXT2Parent;//insert modify
EXT2_NODE dir;
int result;
shell_entry_to_ext2_entry(parent, &EXT2Parent);
result = ext2_lookup(&EXT2Parent, name, &dir);
if (result)
return EXT2_ERROR;
return ext2_rmdir(&EXT2Parent , &dir);
}
int fs_remove(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, const SHELL_ENTRY* parent, const char* name)
{
EXT2_NODE EXTParent;
EXT2_NODE rmfile;
int result;
shell_entry_to_ext2_entry(parent, &EXTParent);
result=ext2_lookup(&EXTParent, name, &rmfile);
if (result)
return EXT2_ERROR;
return ext2_remove(&EXTParent, &rmfile);
}
/******************************************************************************/
/* */
/* UTILITY FUNCTIONS */
/* */
/******************************************************************************/
int shell_entry_to_ext2_entry(const SHELL_ENTRY* shell_entry, EXT2_NODE* fat_entry)
{
EXT2_NODE* entry = (EXT2_NODE*)shell_entry->pdata;
*fat_entry = *entry;
return EXT2_SUCCESS;
}
int ext2_entry_to_shell_entry(const EXT2_NODE* ext2_entry, SHELL_ENTRY* shell_entry)
{
EXT2_NODE* entry = ( EXT2_NODE* )shell_entry->pdata;
BYTE* str;
UINT32 inodeno;
inodeno=ext2_entry->entry.inode;
INODE ino;
get_inode(ext2_entry->fs, inodeno, &ino);
memset( shell_entry, 0, sizeof( SHELL_ENTRY ) );
if(inodeno!=2)
{
str = shell_entry->name;
str = my_strncpy( str, ext2_entry->entry.name, 8 );
if( ext2_entry->entry.name[8] != 0x20 )
{
str = my_strncpy( str, ".", 1 );
str = my_strncpy( str, &ext2_entry->entry.name[8], 3 );
}
}
if(((ino.mode>>12)==(0x4000>>12)) || inodeno==2)
shell_entry->isDirectory = 1;
else
shell_entry->isDirectory=0;
shell_entry->size=ino.size;
*entry = *ext2_entry;
return EXT2_SUCCESS;
}
int my_strnicmp(const char* str1, const char* str2, int length)
{
char c1, c2;
while (((*str1 && *str1 != 0x20) || (*str2 && *str2 != 0x20)) && length-- > 0)
{
c1 = toupper(*str1);
c2 = toupper(*str2);
if (c1 > c2)
return -1;
else if (c1 < c2)
return 1;
str1++;
str2++;
}
return 0;
}
int is_exist(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, const SHELL_ENTRY* parent, const char* name)
{
SHELL_ENTRY_LIST list;
SHELL_ENTRY_LIST_ITEM* current;
init_entry_list(&list);
fs_read_dir(disk, fsOprs, parent, &list);
current = list.first;
while (current)
{
if (my_strnicmp((char*)current->entry.name, name, 12) == 0)
{
release_entry_list(&list);
return EXT2_ERROR;
}
current = current->next;
}
release_entry_list(&list);
return EXT2_SUCCESS;
}
char* my_strncpy( char* dest, const char* src, int length )
{
while( *src && *src != 0x20 && length-- > 0 )
*dest++ = *src++;
return dest;
}
void printFromP2P(char * start, char * end)
{
int start_int, end_int;
start_int = (int)start;
end_int = (int)end;
printf("start address : %#x , end address : %#x\n\n", start, end - 1);
start = (char *)(start_int &= ~(0xf));
end = (char *)(end_int |= 0xf);
while (start <= end)
{
if ((start_int & 0xf) == 0)
fprintf(stdout, "\n%#08x ", start);
fprintf(stdout, "%02X ", *(unsigned char *)start);
start++;
start_int++;
}
printf("\n\n");
}
int adder(void* list, EXT2_NODE* entry)
{
SHELL_ENTRY_LIST* entryList = (SHELL_ENTRY_LIST*)list;
SHELL_ENTRY newEntry;
ext2_entry_to_shell_entry(entry, &newEntry);
add_entry_list(entryList, &newEntry);
return EXT2_SUCCESS;
}
void shell_register_filesystem(SHELL_FILESYSTEM* fs)
{
*fs = g_fat;
}