Skip to content

Commit 7a3956c

Browse files
author
Yuma Arakawa
committed
fs,common: ソースコードを分割
1 parent f575d38 commit 7a3956c

File tree

6 files changed

+121
-86
lines changed

6 files changed

+121
-86
lines changed

kernel/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CFLAGS += -Iinclude
77
.c.o:
88
gcc $(CFLAGS) -o $@ $<
99

10-
kernel.bin: sys.o cpu.o intr.o excp.o memory.o sched.o timer.o console_io.o queue.o debug.o main.o kern_task_init.o
10+
kernel.bin: sys.o cpu.o intr.o excp.o memory.o sched.o fs.o timer.o console_io.o queue.o common.o debug.o main.o kern_task_init.o
1111
ld -o $@ $+ -Map System.map -s -T sys.ld -x
1212

1313
sys.o: sys.S
@@ -22,12 +22,16 @@ memory.o: memory.c
2222

2323
sched.o: sched.c
2424

25+
fs.o: fs.c
26+
2527
timer.o: timer.c
2628

2729
console_io.o: console_io.c
2830

2931
queue.o: queue.c
3032

33+
common.o: common.c
34+
3135
debug.o: debug.c
3236

3337
main.o: main.c

kernel/common.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <common.h>
2+
3+
int str_compare(const char *src, const char *dst)
4+
{
5+
char is_equal = 1;
6+
7+
for (; (*src != '\0') && (*dst != '\0'); src++, dst++) {
8+
if (*src != *dst) {
9+
is_equal = 0;
10+
break;
11+
}
12+
}
13+
14+
if (is_equal) {
15+
if (*src != '\0') {
16+
return 1;
17+
} else if (*dst != '\0') {
18+
return -1;
19+
} else {
20+
return 0;
21+
}
22+
} else {
23+
return (int)(*src - *dst);
24+
}
25+
}
26+
27+
void copy_mem(const void *src, void *dst, unsigned int size)
28+
{
29+
unsigned char *d = (unsigned char *)dst;
30+
unsigned char *s = (unsigned char *)src;
31+
32+
for (; size > 0; size--) {
33+
*d = *s;
34+
d++;
35+
s++;
36+
}
37+
}

kernel/fs.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <fs.h>
2+
#include <stddef.h>
3+
#include <memory.h>
4+
#include <list.h>
5+
#include <queue.h>
6+
#include <common.h>
7+
8+
struct file_head fhead;
9+
struct file fshell, fuptime;
10+
11+
void fs_init(void *fs_base_addr)
12+
{
13+
queue_init((struct list *)&fhead);
14+
fhead.num_files = *(unsigned char *)fs_base_addr;
15+
16+
fshell.name = (char *)fs_base_addr + PAGE_SIZE;
17+
fshell.data_base_addr = (char *)fs_base_addr + PAGE_SIZE + 32;
18+
queue_enq((struct list *)&fshell, (struct list *)&fhead);
19+
20+
fuptime.name = (char *)fs_base_addr + (PAGE_SIZE * 2);
21+
fuptime.data_base_addr = (char *)fs_base_addr + (PAGE_SIZE * 2) + 32;
22+
queue_enq((struct list *)&fuptime, (struct list *)&fhead);
23+
}
24+
25+
struct file *fs_open(const char *name)
26+
{
27+
struct file *f;
28+
29+
/* 将来的には、struct fileのtask_idメンバにopenしたタスクの
30+
* TASK_IDを入れるようにする。そして、openしようとしているファ
31+
* イルのtask_idが既に設定されていれば、fs_openはエラーを返す
32+
* ようにする */
33+
34+
for (f = (struct file *)fhead.lst.next; f != (struct file *)&fhead; f = (struct file *)f->lst.next) {
35+
if (!str_compare(name, f->name))
36+
return f;
37+
}
38+
39+
return NULL;
40+
}
41+
42+
int fs_close(struct file *f)
43+
{
44+
/* 将来的には、fidに対応するstruct fileのtask_idメンバーを設定
45+
* なし(0)にする。 */
46+
return 0;
47+
}

kernel/include/common.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef _COMMON_H_
2+
#define _COMMON_H_
3+
4+
int str_compare(const char *src, const char *dst);
5+
void copy_mem(const void *src, void *dst, unsigned int size);
6+
7+
#endif /* _COMMON_H_ */

kernel/include/fs.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef _FS_H_
2+
#define _FS_H_
3+
4+
#include <list.h>
5+
6+
struct file_head {
7+
struct list lst;
8+
unsigned char num_files;
9+
};
10+
11+
struct file {
12+
struct list lst;
13+
char *name;
14+
void *data_base_addr;
15+
};
16+
17+
extern struct file fshell;
18+
19+
void fs_init(void *fs_base_addr);
20+
struct file *fs_open(const char *name);
21+
int fs_close(struct file *f);
22+
23+
#endif /* _FS_H_ */

kernel/main.c

Lines changed: 2 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,19 @@
66
#include <console_io.h>
77
#include <timer.h>
88
#include <kernel.h>
9+
#include <fs.h>
910
#include <sched.h>
1011
#include <kern_task.h>
1112
#include <shell_init.h>
1213
#include <uptime_init.h>
1314
#include <list.h>
1415
#include <queue.h>
16+
#include <common.h>
1517

1618
#define GDT_IDX_OFS 3
1719
#define APP_ENTRY_POINT 0x20000020
1820
#define APP_STACK_BASE 0x20002000
1921

20-
struct file_head {
21-
struct list lst;
22-
unsigned char num_files;
23-
} fhead;
24-
25-
struct file {
26-
struct list lst;
27-
char *name;
28-
void *data_base_addr;
29-
} fshell, fuptime;
30-
3122
unsigned short task_id_counter = 1;
3223

3324
void kern_lock(unsigned char *if_bit)
@@ -47,42 +38,6 @@ void kern_unlock(unsigned char *if_bit)
4738
sti();
4839
}
4940

50-
static int str_compare(const char *src, const char *dst)
51-
{
52-
char is_equal = 1;
53-
54-
for (; (*src != '\0') && (*dst != '\0'); src++, dst++) {
55-
if (*src != *dst) {
56-
is_equal = 0;
57-
break;
58-
}
59-
}
60-
61-
if (is_equal) {
62-
if (*src != '\0') {
63-
return 1;
64-
} else if (*dst != '\0') {
65-
return -1;
66-
} else {
67-
return 0;
68-
}
69-
} else {
70-
return (int)(*src - *dst);
71-
}
72-
}
73-
74-
static void copy_mem(const void *src, void *dst, unsigned int size)
75-
{
76-
unsigned char *d = (unsigned char *)dst;
77-
unsigned char *s = (unsigned char *)src;
78-
79-
for (; size > 0; size--) {
80-
*d = *s;
81-
d++;
82-
s++;
83-
}
84-
}
85-
8641
static void task_init(struct file *f)
8742
{
8843
struct page_directory_entry *pd_base_addr, *pde;
@@ -169,44 +124,6 @@ static void task_init(struct file *f)
169124
sched_runq_enq(new_task);
170125
}
171126

172-
void fs_init(void *fs_base_addr)
173-
{
174-
queue_init((struct list *)&fhead);
175-
fhead.num_files = *(unsigned char *)fs_base_addr;
176-
177-
fshell.name = (char *)fs_base_addr + PAGE_SIZE;
178-
fshell.data_base_addr = (char *)fs_base_addr + PAGE_SIZE + 32;
179-
queue_enq((struct list *)&fshell, (struct list *)&fhead);
180-
181-
fuptime.name = (char *)fs_base_addr + (PAGE_SIZE * 2);
182-
fuptime.data_base_addr = (char *)fs_base_addr + (PAGE_SIZE * 2) + 32;
183-
queue_enq((struct list *)&fuptime, (struct list *)&fhead);
184-
}
185-
186-
struct file *fs_open(const char *name)
187-
{
188-
struct file *f;
189-
190-
/* 将来的には、struct fileのtask_idメンバにopenしたタスクの
191-
* TASK_IDを入れるようにする。そして、openしようとしているファ
192-
* イルのtask_idが既に設定されていれば、fs_openはエラーを返す
193-
* ようにする */
194-
195-
for (f = (struct file *)fhead.lst.next; f != (struct file *)&fhead; f = (struct file *)f->lst.next) {
196-
if (!str_compare(name, f->name))
197-
return f;
198-
}
199-
200-
return NULL;
201-
}
202-
203-
int fs_close(struct file *f)
204-
{
205-
/* 将来的には、fidに対応するstruct fileのtask_idメンバーを設定
206-
* なし(0)にする。 */
207-
return 0;
208-
}
209-
210127
unsigned int do_syscall(unsigned int syscall_id, unsigned int arg1, unsigned int arg2, unsigned int arg3)
211128
{
212129
unsigned int result = -1;

0 commit comments

Comments
 (0)