prepare kernel, fstool
This commit is contained in:
parent
c768211ae6
commit
7c505804f2
@ -19,6 +19,7 @@ endif()
|
|||||||
|
|
||||||
add_subdirectory(src)
|
add_subdirectory(src)
|
||||||
add_subdirectory(boot)
|
add_subdirectory(boot)
|
||||||
|
add_subdirectory(kernel)
|
||||||
|
|
||||||
add_custom_target(
|
add_custom_target(
|
||||||
run
|
run
|
||||||
|
|||||||
@ -1,15 +1,3 @@
|
|||||||
|
|
||||||
add_custom_command(
|
add_vc6asm_bin_target(boot.bin boot.vc6asm)
|
||||||
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/boot.bin
|
|
||||||
DEPENDS asm ${CMAKE_CURRENT_SOURCE_DIR}/boot.vc6asm
|
|
||||||
COMMAND asm ${CMAKE_CURRENT_SOURCE_DIR}/boot.vc6asm -o ${CMAKE_CURRENT_BINARY_DIR}/boot.bin -M
|
|
||||||
DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/boot.bin.d
|
|
||||||
VERBATIM
|
|
||||||
)
|
|
||||||
|
|
||||||
add_custom_target(
|
|
||||||
boot.bin.target
|
|
||||||
ALL
|
|
||||||
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/boot.bin
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|||||||
3
kernel/CMakeLists.txt
Normal file
3
kernel/CMakeLists.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
add_vc6asm_bin_target(kernel.bin kernel.vc6asm)
|
||||||
|
|
||||||
84
kernel/kernel.vc6asm
Normal file
84
kernel/kernel.vc6asm
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
@include "../boot/arch.vc6asm"
|
||||||
|
@include "../boot/macros.vc6asm"
|
||||||
|
|
||||||
|
@org 0x0100
|
||||||
|
|
||||||
|
main:
|
||||||
|
mov r1, 'V'
|
||||||
|
store [VCD_BEGIN], r1
|
||||||
|
mov r1, 'C'
|
||||||
|
store [VCD_BEGIN + 1], r1
|
||||||
|
mov r1, '6'
|
||||||
|
store [VCD_BEGIN + 2], r1
|
||||||
|
|
||||||
|
mov bp, 0x1000
|
||||||
|
mov sp, bp
|
||||||
|
|
||||||
|
mov r1, text
|
||||||
|
mov r2, VCD_BEGIN + 80
|
||||||
|
call println
|
||||||
|
|
||||||
|
mov r1, prompt
|
||||||
|
mov r2, VCD_BEGIN + 120
|
||||||
|
mov r0, ip
|
||||||
|
call println
|
||||||
|
|
||||||
|
mov r0, keydown_handler
|
||||||
|
store [KBD_KEYDOWN_HANDLER], r0
|
||||||
|
mov r1, 0
|
||||||
|
sti
|
||||||
|
.spin:
|
||||||
|
hlt
|
||||||
|
jr .spin
|
||||||
|
|
||||||
|
keydown_handler:
|
||||||
|
load r0, [KBD_SCANCODE]
|
||||||
|
add r0, 93
|
||||||
|
mov r2, VCD_BEGIN + 40 * 3 + 2
|
||||||
|
add r2, r1
|
||||||
|
store [r2], r0
|
||||||
|
add r1, 1
|
||||||
|
sti
|
||||||
|
ret
|
||||||
|
|
||||||
|
println:
|
||||||
|
push bp
|
||||||
|
mov bp, sp
|
||||||
|
sub sp, 2
|
||||||
|
|
||||||
|
store [sp], r1
|
||||||
|
stea r0, r2, sp, 1
|
||||||
|
|
||||||
|
mov r1, 0
|
||||||
|
jr .l0_cond
|
||||||
|
.l0_body:
|
||||||
|
ldea r3, sp, 1
|
||||||
|
add r3, r1
|
||||||
|
load r2, [sp]
|
||||||
|
add r2, r1
|
||||||
|
load r2, [r2]
|
||||||
|
store [r3], r2
|
||||||
|
|
||||||
|
add r1, 1
|
||||||
|
.l0_cond:
|
||||||
|
load r2, [sp]
|
||||||
|
add r2, r1
|
||||||
|
load r2, [r2]
|
||||||
|
clf
|
||||||
|
lr r3, .l0_body
|
||||||
|
cmp r2, '\0'
|
||||||
|
not fl
|
||||||
|
and fl, FL_EQ
|
||||||
|
cmov ip, r3
|
||||||
|
|
||||||
|
.leave:
|
||||||
|
mov sp, bp
|
||||||
|
pop bp
|
||||||
|
ret
|
||||||
|
|
||||||
|
text:
|
||||||
|
dw "hello world\0"
|
||||||
|
prompt:
|
||||||
|
dw "> \0"
|
||||||
|
|
||||||
|
|
||||||
@ -45,5 +45,25 @@ target_link_libraries(fs PRIVATE opts)
|
|||||||
add_executable(fscli fscli.c)
|
add_executable(fscli fscli.c)
|
||||||
target_link_libraries(fscli PRIVATE opts fs)
|
target_link_libraries(fscli PRIVATE opts fs)
|
||||||
|
|
||||||
|
add_executable(fstool fstool.c)
|
||||||
|
target_link_libraries(fstool PRIVATE opts fs)
|
||||||
|
|
||||||
|
function(add_vc6asm_bin_target TARGET_NAME SOURCE_FILE)
|
||||||
|
add_custom_command(
|
||||||
|
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}
|
||||||
|
COMMAND asm ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE_FILE} -o ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME} -M
|
||||||
|
DEPENDS asm ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE_FILE}
|
||||||
|
DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.d
|
||||||
|
VERBATIM
|
||||||
|
)
|
||||||
|
|
||||||
|
add_custom_target(
|
||||||
|
${TARGET_NAME}.target
|
||||||
|
ALL
|
||||||
|
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}
|
||||||
|
)
|
||||||
|
endfunction()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
160
src/asm.c
160
src/asm.c
@ -275,98 +275,65 @@ char *read_text_file(const char *filename)
|
|||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define TOK_LIST \
|
||||||
|
X(Eof, ) \
|
||||||
|
X(Eol, ) \
|
||||||
|
X(Ident, ) \
|
||||||
|
X(Int, ) \
|
||||||
|
X(Hex, ) \
|
||||||
|
X(Bin, ) \
|
||||||
|
X(Char, ) \
|
||||||
|
X(Str, ) \
|
||||||
|
X(LtLt, ) \
|
||||||
|
X(GtGt, ) \
|
||||||
|
X(R0, ) \
|
||||||
|
X(R1, ) \
|
||||||
|
X(R2, ) \
|
||||||
|
X(R3, ) \
|
||||||
|
X(Sp, ) \
|
||||||
|
X(Bp, ) \
|
||||||
|
X(Fl, ) \
|
||||||
|
X(Ip, ) \
|
||||||
|
X(Const, ) \
|
||||||
|
X(Macro, ) \
|
||||||
|
X(End, ) \
|
||||||
|
X(Include, ) \
|
||||||
|
X(Guard, ) \
|
||||||
|
X(Org, ) \
|
||||||
|
X(MacroArg, ) \
|
||||||
|
X(Exclam, = '!') \
|
||||||
|
X(Dollar, = '$') \
|
||||||
|
X(Percent, = '%') \
|
||||||
|
X(Ampersand, = '&') \
|
||||||
|
X(LParen, = '(') \
|
||||||
|
X(RParen, = ')') \
|
||||||
|
X(Asterisk, = '*') \
|
||||||
|
X(Plus, = '+') \
|
||||||
|
X(Comma, = ',') \
|
||||||
|
X(Minus, = '-') \
|
||||||
|
X(Dot, = '.') \
|
||||||
|
X(Slash, = '/') \
|
||||||
|
X(Colon, = ':') \
|
||||||
|
X(At, = '@') \
|
||||||
|
X(LBracket, = '[') \
|
||||||
|
X(RBracket, = ']') \
|
||||||
|
X(Caret, = '^') \
|
||||||
|
X(Pipe, = '|')
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
Tok_Eof,
|
#define X(NAME, VAL) Tok_##NAME VAL,
|
||||||
Tok_Eol,
|
TOK_LIST
|
||||||
Tok_Ident,
|
#undef X
|
||||||
Tok_Int,
|
|
||||||
Tok_Hex,
|
|
||||||
Tok_Bin,
|
|
||||||
Tok_Char,
|
|
||||||
Tok_Str,
|
|
||||||
Tok_LtLt,
|
|
||||||
Tok_GtGt,
|
|
||||||
Tok_R0,
|
|
||||||
Tok_R1,
|
|
||||||
Tok_R2,
|
|
||||||
Tok_R3,
|
|
||||||
Tok_Sp,
|
|
||||||
Tok_Bp,
|
|
||||||
Tok_Fl,
|
|
||||||
Tok_Ip,
|
|
||||||
Tok_Const,
|
|
||||||
Tok_Macro,
|
|
||||||
Tok_End,
|
|
||||||
Tok_Include,
|
|
||||||
Tok_Guard,
|
|
||||||
Tok_MacroArg,
|
|
||||||
Tok_Exclam = '!',
|
|
||||||
Tok_Dollar = '$',
|
|
||||||
Tok_Percent = '%',
|
|
||||||
Tok_Ampersand = '&',
|
|
||||||
Tok_LParen = '(',
|
|
||||||
Tok_RParen = ')',
|
|
||||||
Tok_Asterisk = '*',
|
|
||||||
Tok_Plus = '+',
|
|
||||||
Tok_Comma = ',',
|
|
||||||
Tok_Minus = '-',
|
|
||||||
Tok_Dot = '.',
|
|
||||||
Tok_Slash = '/',
|
|
||||||
Tok_Colon = ':',
|
|
||||||
Tok_At = '@',
|
|
||||||
Tok_LBracket = '[',
|
|
||||||
Tok_RBracket = ']',
|
|
||||||
Tok_Caret = '^',
|
|
||||||
Tok_Pipe = '|',
|
|
||||||
} TokTy;
|
} TokTy;
|
||||||
|
|
||||||
const char *tokty_str(TokTy ty)
|
const char *tokty_str(TokTy ty)
|
||||||
{
|
{
|
||||||
switch (ty) {
|
switch (ty) {
|
||||||
/* clang-format off */
|
#define X(NAME, VAL) \
|
||||||
case Tok_Eof: return "Eof";
|
case Tok_##NAME: \
|
||||||
case Tok_Eol: return "Eol";
|
return #NAME;
|
||||||
case Tok_Ident: return "Ident";
|
TOK_LIST
|
||||||
case Tok_Int: return "Int";
|
#undef X
|
||||||
case Tok_Hex: return "Hex";
|
|
||||||
case Tok_Bin: return "Bin";
|
|
||||||
case Tok_Char: return "Char";
|
|
||||||
case Tok_Str: return "Str";
|
|
||||||
case Tok_LtLt: return "LtLt";
|
|
||||||
case Tok_GtGt: return "GtGt";
|
|
||||||
case Tok_R0: return "R0";
|
|
||||||
case Tok_R1: return "R1";
|
|
||||||
case Tok_R2: return "R2";
|
|
||||||
case Tok_R3: return "R3";
|
|
||||||
case Tok_Sp: return "Sp";
|
|
||||||
case Tok_Bp: return "Bp";
|
|
||||||
case Tok_Fl: return "Fl";
|
|
||||||
case Tok_Ip: return "Ip";
|
|
||||||
case Tok_Const: return "Const";
|
|
||||||
case Tok_Macro: return "Macro";
|
|
||||||
case Tok_End: return "End";
|
|
||||||
case Tok_Include: return "Include";
|
|
||||||
case Tok_Guard: return "Guard";
|
|
||||||
case Tok_MacroArg: return "MacroArg";
|
|
||||||
case Tok_Exclam: return "Exclam";
|
|
||||||
case Tok_Dollar: return "Dollar";
|
|
||||||
case Tok_Percent: return "Percent";
|
|
||||||
case Tok_Ampersand: return "Ampersand";
|
|
||||||
case Tok_LParen: return "LParen";
|
|
||||||
case Tok_RParen: return "RParen";
|
|
||||||
case Tok_Asterisk: return "Asterisk";
|
|
||||||
case Tok_Plus: return "Plus";
|
|
||||||
case Tok_Comma: return "Comma";
|
|
||||||
case Tok_Minus: return "Minus";
|
|
||||||
case Tok_Dot: return "Dot";
|
|
||||||
case Tok_Slash: return "Slash";
|
|
||||||
case Tok_Colon: return "Colon";
|
|
||||||
case Tok_At: return "At";
|
|
||||||
case Tok_LBracket: return "LBracket";
|
|
||||||
case Tok_RBracket: return "RBracket";
|
|
||||||
case Tok_Caret: return "Caret";
|
|
||||||
case Tok_Pipe: return "Pipe";
|
|
||||||
/* clang-format on */
|
|
||||||
}
|
}
|
||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
@ -419,6 +386,7 @@ static TokTy l_ident_ty(const char *text, size_t len)
|
|||||||
"end",
|
"end",
|
||||||
"include",
|
"include",
|
||||||
"guard",
|
"guard",
|
||||||
|
"org",
|
||||||
};
|
};
|
||||||
TokTy tys[] = {
|
TokTy tys[] = {
|
||||||
Tok_R0,
|
Tok_R0,
|
||||||
@ -434,6 +402,7 @@ static TokTy l_ident_ty(const char *text, size_t len)
|
|||||||
Tok_End,
|
Tok_End,
|
||||||
Tok_Include,
|
Tok_Include,
|
||||||
Tok_Guard,
|
Tok_Guard,
|
||||||
|
Tok_Org,
|
||||||
};
|
};
|
||||||
|
|
||||||
constexpr size_t kws_size = sizeof(kws) / sizeof(kws[0]);
|
constexpr size_t kws_size = sizeof(kws) / sizeof(kws[0]);
|
||||||
@ -674,6 +643,7 @@ typedef struct {
|
|||||||
Map dep_str_map;
|
Map dep_str_map;
|
||||||
FILE *dep_file;
|
FILE *dep_file;
|
||||||
int directive_depth;
|
int directive_depth;
|
||||||
|
uint16_t org_offset;
|
||||||
bool second_pass;
|
bool second_pass;
|
||||||
bool print;
|
bool print;
|
||||||
} Assembler;
|
} Assembler;
|
||||||
@ -993,7 +963,7 @@ static int64_t a_parse_operand(Assembler *a)
|
|||||||
}
|
}
|
||||||
if (a->tok.ty == '$') {
|
if (a->tok.ty == '$') {
|
||||||
a_step(a);
|
a_step(a);
|
||||||
return a->ip;
|
return a->ip + a->org_offset;
|
||||||
}
|
}
|
||||||
if (a->tok.ty == '(') {
|
if (a->tok.ty == '(') {
|
||||||
a_step(a);
|
a_step(a);
|
||||||
@ -1236,6 +1206,16 @@ static void a_parse_directive_guard(Assembler *a)
|
|||||||
free(ident);
|
free(ident);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void a_parse_directive_org(Assembler *a)
|
||||||
|
{
|
||||||
|
a_step(a);
|
||||||
|
int64_t offset = a_parse_expr(a);
|
||||||
|
a_assert_eol(a);
|
||||||
|
|
||||||
|
assert(offset >= 0 && offset <= UINT16_MAX);
|
||||||
|
a->org_offset = (uint16_t)offset;
|
||||||
|
}
|
||||||
|
|
||||||
static void a_parse_directive(Assembler *a)
|
static void a_parse_directive(Assembler *a)
|
||||||
{
|
{
|
||||||
assert(a->tok.ty == '@');
|
assert(a->tok.ty == '@');
|
||||||
@ -1254,6 +1234,8 @@ static void a_parse_directive(Assembler *a)
|
|||||||
a_assert_eol(a);
|
a_assert_eol(a);
|
||||||
} else if (a->tok.ty == Tok_Guard) {
|
} else if (a->tok.ty == Tok_Guard) {
|
||||||
a_parse_directive_guard(a);
|
a_parse_directive_guard(a);
|
||||||
|
} else if (a->tok.ty == Tok_Org) {
|
||||||
|
a_parse_directive_org(a);
|
||||||
} else {
|
} else {
|
||||||
print_error_exit(a->tok.loc, "malformed directive");
|
print_error_exit(a->tok.loc, "malformed directive");
|
||||||
}
|
}
|
||||||
@ -1553,7 +1535,7 @@ static void a_parse_label_or_ins(Assembler *a)
|
|||||||
a_step(a);
|
a_step(a);
|
||||||
|
|
||||||
char *ident = strndup(ident_tok.text, ident_tok.len);
|
char *ident = strndup(ident_tok.text, ident_tok.len);
|
||||||
a_sym_define(a, ident, ident_tok.loc, a->ip);
|
a_sym_define(a, ident, ident_tok.loc, a->ip + a->org_offset);
|
||||||
free(ident);
|
free(ident);
|
||||||
|
|
||||||
a->labels_counter += 1;
|
a->labels_counter += 1;
|
||||||
@ -1582,7 +1564,7 @@ static void a_parse_local_label(Assembler *a)
|
|||||||
char *ident = strndup(ident_tok.text, ident_tok.len);
|
char *ident = strndup(ident_tok.text, ident_tok.len);
|
||||||
char *unique_ident = alloc_sprintf(".%s_%d", ident, a->current_label);
|
char *unique_ident = alloc_sprintf(".%s_%d", ident, a->current_label);
|
||||||
|
|
||||||
a_sym_define(a, unique_ident, ident_tok.loc, a->ip);
|
a_sym_define(a, unique_ident, ident_tok.loc, a->ip + a->org_offset);
|
||||||
free(ident);
|
free(ident);
|
||||||
free(unique_ident);
|
free(unique_ident);
|
||||||
|
|
||||||
|
|||||||
60
src/fs.c
60
src/fs.c
@ -24,11 +24,6 @@ constexpr size_t direentry_max_size = 64;
|
|||||||
constexpr size_t inode_ndir_blocks_count = inode_blocks_cap - 1;
|
constexpr size_t inode_ndir_blocks_count = inode_blocks_cap - 1;
|
||||||
constexpr size_t inode_ind_block_idx = inode_ndir_blocks_count;
|
constexpr size_t inode_ind_block_idx = inode_ndir_blocks_count;
|
||||||
|
|
||||||
typedef enum : uint16_t {
|
|
||||||
INode_File,
|
|
||||||
INode_Dir,
|
|
||||||
} INodeTy;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint16_t inode;
|
uint16_t inode;
|
||||||
uint16_t next;
|
uint16_t next;
|
||||||
@ -234,6 +229,15 @@ static void make_dir_with_inode_ids(
|
|||||||
.blocks = { },
|
.blocks = { },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
DirEntry self_entry = {
|
||||||
|
.inode = inode_id,
|
||||||
|
.next = 0,
|
||||||
|
.name_len = 1,
|
||||||
|
.name = { '.' },
|
||||||
|
};
|
||||||
|
size_t self_entry_size = direntry_size(&self_entry);
|
||||||
|
self_entry.next = self_entry_size;
|
||||||
|
|
||||||
DirEntry parent_entry = {
|
DirEntry parent_entry = {
|
||||||
.inode = parent_inode_id,
|
.inode = parent_inode_id,
|
||||||
.next = 0,
|
.next = 0,
|
||||||
@ -243,8 +247,10 @@ static void make_dir_with_inode_ids(
|
|||||||
|
|
||||||
uint16_t entry_block_id = inode_allocate_block(fs, out_inode);
|
uint16_t entry_block_id = inode_allocate_block(fs, out_inode);
|
||||||
Block block = { };
|
Block block = { };
|
||||||
memcpy(
|
memcpy(block, &self_entry, self_entry_size * sizeof(uint16_t));
|
||||||
block, &parent_entry, direntry_size(&parent_entry) * sizeof(uint16_t));
|
memcpy(&block[self_entry_size],
|
||||||
|
&parent_entry,
|
||||||
|
direntry_size(&parent_entry) * sizeof(uint16_t));
|
||||||
write_block(fs, block, entry_block_id);
|
write_block(fs, block, entry_block_id);
|
||||||
out_inode->last_block_size = direntry_size(&parent_entry);
|
out_inode->last_block_size = direntry_size(&parent_entry);
|
||||||
|
|
||||||
@ -339,6 +345,7 @@ void fs_init(Fs *fs, Disk *disk, uint16_t block_offset, uint16_t blocks_count)
|
|||||||
};
|
};
|
||||||
|
|
||||||
fs->sb = (Superblock *)fs->superblock;
|
fs->sb = (Superblock *)fs->superblock;
|
||||||
|
fs->sb->magic = superblock_magic;
|
||||||
|
|
||||||
bitmap_set(*fs->block_bitmap, 0, true);
|
bitmap_set(*fs->block_bitmap, 0, true);
|
||||||
bitmap_set(*fs->block_bitmap, 1, true);
|
bitmap_set(*fs->block_bitmap, 1, true);
|
||||||
@ -350,6 +357,27 @@ void fs_init(Fs *fs, Disk *disk, uint16_t block_offset, uint16_t blocks_count)
|
|||||||
make_dir_with_inode_ids(fs, &root_inode, root_inode_id, root_inode_id);
|
make_dir_with_inode_ids(fs, &root_inode, root_inode_id, root_inode_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void fs_load(Fs *fs, Disk *disk, uint16_t block_offset, uint16_t blocks_count)
|
||||||
|
{
|
||||||
|
assert(blocks_count <= sizeof(Block) * 8);
|
||||||
|
*fs = (Fs) {
|
||||||
|
.disk = disk,
|
||||||
|
.block_offset = block_offset,
|
||||||
|
.blocks_count = blocks_count,
|
||||||
|
.superblock = calloc(1, sizeof(Block)),
|
||||||
|
.sb = nullptr,
|
||||||
|
.block_bitmap = calloc(1, sizeof(Block)),
|
||||||
|
.out_of_sync = false,
|
||||||
|
};
|
||||||
|
|
||||||
|
fs->sb = (Superblock *)fs->superblock;
|
||||||
|
|
||||||
|
read_block(fs, *fs->superblock, 0);
|
||||||
|
read_block(fs, *fs->block_bitmap, 1);
|
||||||
|
|
||||||
|
assert(fs->sb->magic == superblock_magic);
|
||||||
|
}
|
||||||
|
|
||||||
void fs_destroy(Fs *fs)
|
void fs_destroy(Fs *fs)
|
||||||
{
|
{
|
||||||
free(fs->superblock);
|
free(fs->superblock);
|
||||||
@ -483,7 +511,7 @@ void fs_remove(Fs *fs, INode *inode, INode *parent)
|
|||||||
|
|
||||||
struct list_block_entries_fn_args {
|
struct list_block_entries_fn_args {
|
||||||
Fs *fs;
|
Fs *fs;
|
||||||
ListEntry *entries;
|
FsListEntry *entries;
|
||||||
size_t entries_cap;
|
size_t entries_cap;
|
||||||
size_t *entries_size;
|
size_t *entries_size;
|
||||||
};
|
};
|
||||||
@ -507,7 +535,7 @@ static void list_block_entries_fn(uint16_t block_id, void *arg)
|
|||||||
&block[entry.next],
|
&block[entry.next],
|
||||||
MIN(direentry_max_size, block_size - entry.next));
|
MIN(direentry_max_size, block_size - entry.next));
|
||||||
|
|
||||||
ListEntry list_entry = {
|
FsListEntry list_entry = {
|
||||||
.inode = entry.inode,
|
.inode = entry.inode,
|
||||||
.name = { },
|
.name = { },
|
||||||
};
|
};
|
||||||
@ -519,7 +547,7 @@ static void list_block_entries_fn(uint16_t block_id, void *arg)
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t fs_list(
|
size_t fs_list(
|
||||||
Fs *fs, ListEntry *entries, size_t entries_cap, const INode *inode)
|
Fs *fs, FsListEntry *entries, size_t entries_cap, const INode *inode)
|
||||||
{
|
{
|
||||||
size_t entries_size = 0;
|
size_t entries_size = 0;
|
||||||
struct list_block_entries_fn_args args
|
struct list_block_entries_fn_args args
|
||||||
@ -564,9 +592,14 @@ uint16_t fs_read(Fs *fs,
|
|||||||
for (; block_idx < inode->blocks_count && i < buffer_size; ++block_idx) {
|
for (; block_idx < inode->blocks_count && i < buffer_size; ++block_idx) {
|
||||||
Block block;
|
Block block;
|
||||||
read_block(fs, block, *inode_block_at(inode, block_idx, ind_block));
|
read_block(fs, block, *inode_block_at(inode, block_idx, ind_block));
|
||||||
uint16_t chunk_size
|
|
||||||
= MIN((size_t)buffer_size - i, block_size - block_offset);
|
uint16_t chunk_size = buffer_size - i;
|
||||||
memcpy(&buffer[i], &block[block_offset], chunk_size);
|
chunk_size = MIN((size_t)chunk_size, block_size - block_offset);
|
||||||
|
if (block_idx == inode->blocks_count - 1)
|
||||||
|
chunk_size = MIN(chunk_size, inode->last_block_size);
|
||||||
|
|
||||||
|
memcpy(&buffer[i], &block[block_offset], chunk_size * sizeof(uint16_t));
|
||||||
|
block_offset = 0;
|
||||||
i += chunk_size;
|
i += chunk_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -583,6 +616,7 @@ void fs_dump_info(Fs *fs)
|
|||||||
void fs_dump_superblock(Fs *fs)
|
void fs_dump_superblock(Fs *fs)
|
||||||
{
|
{
|
||||||
Superblock *sb = fs->sb;
|
Superblock *sb = fs->sb;
|
||||||
|
printf("magic = 0x%4x\n", sb->magic);
|
||||||
printf("blocks_count = %u\n", sb->blocks_count);
|
printf("blocks_count = %u\n", sb->blocks_count);
|
||||||
printf("inode_blocks_count = %u\n", sb->inodeblocks_count);
|
printf("inode_blocks_count = %u\n", sb->inodeblocks_count);
|
||||||
printf("inode_blocks = [");
|
printf("inode_blocks = [");
|
||||||
|
|||||||
15
src/fs.h
15
src/fs.h
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
constexpr size_t block_size = 512;
|
constexpr size_t block_size = 512;
|
||||||
typedef uint16_t Block[block_size];
|
typedef uint16_t Block[block_size];
|
||||||
constexpr size_t size_of_block = sizeof(Block) / sizeof(uint16_t);
|
constexpr size_t size_of_block = sizeof(Block);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Block *blocks;
|
Block *blocks;
|
||||||
@ -22,6 +22,11 @@ constexpr size_t inodes_cap = inodeblocks_cap * inodes_per_block;
|
|||||||
constexpr size_t inodebitmap_size = inodes_cap / 16;
|
constexpr size_t inodebitmap_size = inodes_cap / 16;
|
||||||
constexpr size_t inode_name_cap = 61;
|
constexpr size_t inode_name_cap = 61;
|
||||||
|
|
||||||
|
typedef enum : uint16_t {
|
||||||
|
INode_File,
|
||||||
|
INode_Dir,
|
||||||
|
} INodeTy;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint16_t id;
|
uint16_t id;
|
||||||
uint16_t mode;
|
uint16_t mode;
|
||||||
@ -32,7 +37,10 @@ typedef struct {
|
|||||||
|
|
||||||
static_assert(sizeof(INode) / sizeof(uint16_t) == inode_size);
|
static_assert(sizeof(INode) / sizeof(uint16_t) == inode_size);
|
||||||
|
|
||||||
|
constexpr uint16_t superblock_magic = 0x5a5a;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
uint16_t magic;
|
||||||
uint16_t blocks_count;
|
uint16_t blocks_count;
|
||||||
uint16_t inodeblocks_count;
|
uint16_t inodeblocks_count;
|
||||||
uint16_t inodeblocks[inodeblocks_cap];
|
uint16_t inodeblocks[inodeblocks_cap];
|
||||||
@ -52,6 +60,7 @@ typedef struct {
|
|||||||
} Fs;
|
} Fs;
|
||||||
|
|
||||||
void fs_init(Fs *fs, Disk *disk, uint16_t block_offset, uint16_t blocks_count);
|
void fs_init(Fs *fs, Disk *disk, uint16_t block_offset, uint16_t blocks_count);
|
||||||
|
void fs_load(Fs *fs, Disk *disk, uint16_t block_offset, uint16_t blocks_count);
|
||||||
void fs_destroy(Fs *fs);
|
void fs_destroy(Fs *fs);
|
||||||
void fs_sync(Fs *fs);
|
void fs_sync(Fs *fs);
|
||||||
void fs_root(Fs *fs, INode *out_inode);
|
void fs_root(Fs *fs, INode *out_inode);
|
||||||
@ -63,10 +72,10 @@ void fs_remove(Fs *fs, INode *inode, INode *parent);
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
uint16_t inode;
|
uint16_t inode;
|
||||||
char name[inode_name_cap + 1];
|
char name[inode_name_cap + 1];
|
||||||
} ListEntry;
|
} FsListEntry;
|
||||||
|
|
||||||
size_t fs_list(
|
size_t fs_list(
|
||||||
Fs *fs, ListEntry *entries, size_t entries_cap, const INode *inode);
|
Fs *fs, FsListEntry *entries, size_t entries_cap, const INode *inode);
|
||||||
|
|
||||||
uint16_t fs_write(
|
uint16_t fs_write(
|
||||||
Fs *fs, INode *inode, const uint16_t *buffer, uint16_t buffer_size);
|
Fs *fs, INode *inode, const uint16_t *buffer, uint16_t buffer_size);
|
||||||
|
|||||||
42
src/fscli.c
42
src/fscli.c
@ -1,4 +1,5 @@
|
|||||||
#include "fs.h"
|
#include "fs.h"
|
||||||
|
#include <assert.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -6,8 +7,9 @@
|
|||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
(void)argc, (void)argv;
|
(void)argc, (void)argv;
|
||||||
// assert(argc >= 2);
|
const char *filename = nullptr;
|
||||||
// const char *output_filename = argv[1];
|
if (argc >= 2)
|
||||||
|
filename = argv[1];
|
||||||
|
|
||||||
constexpr size_t amount_of_blocks = 128;
|
constexpr size_t amount_of_blocks = 128;
|
||||||
|
|
||||||
@ -19,8 +21,19 @@ int main(int argc, char *argv[])
|
|||||||
Fs fs_val;
|
Fs fs_val;
|
||||||
Fs *fs = &fs_val;
|
Fs *fs = &fs_val;
|
||||||
|
|
||||||
fs_init(fs, &disk, 0, amount_of_blocks);
|
if (filename) {
|
||||||
fs_sync(fs);
|
FILE *file = fopen(filename, "r");
|
||||||
|
assert(file);
|
||||||
|
|
||||||
|
fread(
|
||||||
|
disk.blocks, sizeof(uint16_t), block_size * amount_of_blocks, file);
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
fs_load(fs, &disk, 0, amount_of_blocks);
|
||||||
|
} else {
|
||||||
|
fs_init(fs, &disk, 0, amount_of_blocks);
|
||||||
|
fs_sync(fs);
|
||||||
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
char command[32];
|
char command[32];
|
||||||
@ -47,6 +60,7 @@ int main(int argc, char *argv[])
|
|||||||
"ls i list entries in inode[i]\n"
|
"ls i list entries in inode[i]\n"
|
||||||
"w i write to inode[i]\n"
|
"w i write to inode[i]\n"
|
||||||
"r i read from inode[i]\n"
|
"r i read from inode[i]\n"
|
||||||
|
"save save to disk file\n"
|
||||||
"");
|
"");
|
||||||
} else if (strcmp(command, "fs") == 0) {
|
} else if (strcmp(command, "fs") == 0) {
|
||||||
fs_dump_info(fs);
|
fs_dump_info(fs);
|
||||||
@ -122,12 +136,12 @@ int main(int argc, char *argv[])
|
|||||||
goto malformed_command;
|
goto malformed_command;
|
||||||
INode inode;
|
INode inode;
|
||||||
fs_inode(fs, &inode, inode_id);
|
fs_inode(fs, &inode, inode_id);
|
||||||
ListEntry entries[32];
|
FsListEntry entries[32];
|
||||||
size_t entries_size = fs_list(
|
size_t entries_size = fs_list(
|
||||||
fs, entries, sizeof(entries) / sizeof(entries[0]), &inode);
|
fs, entries, sizeof(entries) / sizeof(entries[0]), &inode);
|
||||||
fs_sync(fs);
|
fs_sync(fs);
|
||||||
for (size_t i = 0; i < entries_size; ++i)
|
for (size_t i = 0; i < entries_size; ++i)
|
||||||
printf("%s\n", entries[i].name);
|
printf("%2u %s\n", entries[i].inode, entries[i].name);
|
||||||
} else if (strcmp(command, "w") == 0) {
|
} else if (strcmp(command, "w") == 0) {
|
||||||
uint16_t inode_id;
|
uint16_t inode_id;
|
||||||
if (scanf(" %hu", &inode_id) != 1)
|
if (scanf(" %hu", &inode_id) != 1)
|
||||||
@ -149,10 +163,22 @@ int main(int argc, char *argv[])
|
|||||||
goto malformed_command;
|
goto malformed_command;
|
||||||
INode inode;
|
INode inode;
|
||||||
fs_inode(fs, &inode, inode_id);
|
fs_inode(fs, &inode, inode_id);
|
||||||
uint16_t buffer[64] = { 0 };
|
uint16_t *buffer = calloc(65535, sizeof(uint16_t));
|
||||||
size_t buffer_size = fs_read(fs, &inode, buffer, 64, 0);
|
size_t buffer_size = fs_read(fs, &inode, buffer, 65535, 0);
|
||||||
|
printf("buffer_size = %zu\n", buffer_size);
|
||||||
for (size_t i = 0; i < buffer_size; ++i)
|
for (size_t i = 0; i < buffer_size; ++i)
|
||||||
fputc(buffer[i], stdout);
|
fputc(buffer[i], stdout);
|
||||||
|
free(buffer);
|
||||||
|
} else if (strcmp(command, "save") == 0) {
|
||||||
|
fs_sync(fs);
|
||||||
|
FILE *file = fopen(filename, "w");
|
||||||
|
assert(file);
|
||||||
|
size_t words_written = fwrite(disk.blocks,
|
||||||
|
sizeof(uint16_t),
|
||||||
|
block_size * amount_of_blocks,
|
||||||
|
file);
|
||||||
|
fclose(file);
|
||||||
|
printf("wrote %zu words to %s\n", words_written, filename);
|
||||||
} else {
|
} else {
|
||||||
printf("error: unknown command '%s'\n", command);
|
printf("error: unknown command '%s'\n", command);
|
||||||
}
|
}
|
||||||
|
|||||||
251
src/fstool.c
Normal file
251
src/fstool.c
Normal file
@ -0,0 +1,251 @@
|
|||||||
|
#include "fs.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
constexpr size_t amount_of_blocks = 128;
|
||||||
|
|
||||||
|
static void load(Fs *fs, Disk *disk, const char *filename)
|
||||||
|
{
|
||||||
|
*disk = (Disk) {
|
||||||
|
.blocks = calloc(amount_of_blocks, sizeof(Block)),
|
||||||
|
.blocks_count = amount_of_blocks,
|
||||||
|
};
|
||||||
|
|
||||||
|
FILE *file = fopen(filename, "r");
|
||||||
|
if (file) {
|
||||||
|
fread(disk->blocks,
|
||||||
|
sizeof(uint16_t),
|
||||||
|
block_size * amount_of_blocks,
|
||||||
|
file);
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
fs_load(fs, disk, 0, amount_of_blocks);
|
||||||
|
} else {
|
||||||
|
printf(
|
||||||
|
"warning: could not find '%s'. one will be created.\n", filename);
|
||||||
|
fs_init(fs, disk, 0, amount_of_blocks);
|
||||||
|
fs_sync(fs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void save(Fs *fs, Disk *disk, const char *filename)
|
||||||
|
{
|
||||||
|
fs_sync(fs);
|
||||||
|
|
||||||
|
FILE *file = fopen(filename, "w");
|
||||||
|
assert(file);
|
||||||
|
size_t words_written = fwrite(
|
||||||
|
disk->blocks, sizeof(uint16_t), block_size * amount_of_blocks, file);
|
||||||
|
fclose(file);
|
||||||
|
printf("wrote %zu words to %s\n", words_written, filename);
|
||||||
|
|
||||||
|
fs_destroy(fs);
|
||||||
|
free(disk->blocks);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void close_without_saving(Fs *fs, Disk *disk)
|
||||||
|
{
|
||||||
|
fs_destroy(fs);
|
||||||
|
free(disk->blocks);
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint16_t search_entries(
|
||||||
|
FsListEntry entries[], size_t entries_size, const char *name)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < entries_size; ++i)
|
||||||
|
if (strcmp(entries[i].name, name) == 0)
|
||||||
|
return entries[i].inode;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void resolve_path(Fs *fs, INode *inode, const char *path)
|
||||||
|
{
|
||||||
|
assert(path[0] == '/');
|
||||||
|
char *path_copy = strdup(path);
|
||||||
|
fs_root(fs, inode);
|
||||||
|
|
||||||
|
char *tok = strtok(path_copy, "/");
|
||||||
|
while (tok) {
|
||||||
|
constexpr size_t entries_cap = 16;
|
||||||
|
FsListEntry entries[entries_cap];
|
||||||
|
|
||||||
|
size_t entries_size = fs_list(fs, entries, entries_cap, inode);
|
||||||
|
assert(entries_size < entries_cap);
|
||||||
|
|
||||||
|
uint16_t inode_id = search_entries(entries, entries_size, tok);
|
||||||
|
assert(inode_id && "not found");
|
||||||
|
|
||||||
|
fs_inode(fs, inode, inode_id);
|
||||||
|
|
||||||
|
tok = strtok(nullptr, "/");
|
||||||
|
}
|
||||||
|
free(path_copy);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *help = ""
|
||||||
|
"h help\n"
|
||||||
|
"mkdir <path>/<name> create dir with name at path\n"
|
||||||
|
"mkfile <path>/<name> create file with name at path\n"
|
||||||
|
"ls <path> print contents of dir\n"
|
||||||
|
"import <path> <path> copy host file to fs\n"
|
||||||
|
"importb <path> <path> copy host file to fs, u8 -> u16\n"
|
||||||
|
"";
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
assert(argc >= 3);
|
||||||
|
const char *disk_filename = argv[1];
|
||||||
|
const char *cmd = argv[2];
|
||||||
|
|
||||||
|
Disk disk;
|
||||||
|
Fs fs_val, *fs = &fs_val;
|
||||||
|
|
||||||
|
if (strcmp(cmd, "h") == 0) {
|
||||||
|
printf("%s", help);
|
||||||
|
} else if (strcmp(cmd, "mkdir") == 0) {
|
||||||
|
if (argc < 4)
|
||||||
|
goto malformed_command;
|
||||||
|
const char *path = argv[3];
|
||||||
|
|
||||||
|
load(fs, &disk, disk_filename);
|
||||||
|
|
||||||
|
char *path_copy = strdup(path);
|
||||||
|
char *name = strrchr(path_copy, '/');
|
||||||
|
*name = '\0';
|
||||||
|
name += 1;
|
||||||
|
|
||||||
|
INode parent;
|
||||||
|
resolve_path(fs, &parent, path_copy);
|
||||||
|
|
||||||
|
printf("creating directory '%s' in %d.\n", name, parent.id);
|
||||||
|
INode inode;
|
||||||
|
fs_make_dir(fs, &inode, &parent, name);
|
||||||
|
|
||||||
|
free(path_copy);
|
||||||
|
save(fs, &disk, disk_filename);
|
||||||
|
} else if (strcmp(cmd, "mkfile") == 0) {
|
||||||
|
if (argc < 4)
|
||||||
|
goto malformed_command;
|
||||||
|
const char *path = argv[3];
|
||||||
|
|
||||||
|
load(fs, &disk, disk_filename);
|
||||||
|
|
||||||
|
char *path_copy = strdup(path);
|
||||||
|
char *name = strrchr(path_copy, '/');
|
||||||
|
*name = '\0';
|
||||||
|
name += 1;
|
||||||
|
|
||||||
|
INode parent;
|
||||||
|
resolve_path(fs, &parent, path_copy);
|
||||||
|
|
||||||
|
printf("creating file %s in %s.\n", name, path_copy);
|
||||||
|
INode inode;
|
||||||
|
fs_make_file(fs, &inode, &parent, name);
|
||||||
|
|
||||||
|
free(path_copy);
|
||||||
|
save(fs, &disk, disk_filename);
|
||||||
|
} else if (strcmp(cmd, "ls") == 0) {
|
||||||
|
if (argc < 4)
|
||||||
|
goto malformed_command;
|
||||||
|
const char *path = argv[3];
|
||||||
|
|
||||||
|
load(fs, &disk, disk_filename);
|
||||||
|
|
||||||
|
INode parent;
|
||||||
|
resolve_path(fs, &parent, path);
|
||||||
|
|
||||||
|
constexpr size_t entries_cap = 16;
|
||||||
|
FsListEntry entries[entries_cap];
|
||||||
|
|
||||||
|
size_t entries_size = fs_list(fs, entries, entries_cap, &parent);
|
||||||
|
for (size_t i = 0; i < entries_size; ++i) {
|
||||||
|
INode inode;
|
||||||
|
fs_inode(fs, &inode, entries[i].inode);
|
||||||
|
bool is_dir = i >= 2 && inode.mode == INode_Dir;
|
||||||
|
printf("%s%s\n", entries[i].name, is_dir ? "/" : "");
|
||||||
|
}
|
||||||
|
|
||||||
|
close_without_saving(fs, &disk);
|
||||||
|
} else if (strcmp(cmd, "import") == 0) {
|
||||||
|
if (argc < 5)
|
||||||
|
goto malformed_command;
|
||||||
|
const char *rpath = argv[3];
|
||||||
|
const char *vpath = argv[4];
|
||||||
|
|
||||||
|
load(fs, &disk, disk_filename);
|
||||||
|
|
||||||
|
INode inode;
|
||||||
|
resolve_path(fs, &inode, vpath);
|
||||||
|
|
||||||
|
FILE *file = fopen(rpath, "r");
|
||||||
|
assert(file);
|
||||||
|
fseek(file, 0, SEEK_END);
|
||||||
|
long file_size = ftell(file);
|
||||||
|
assert(file_size > 0);
|
||||||
|
fseek(file, 0, SEEK_SET);
|
||||||
|
|
||||||
|
size_t buffer_size = (file_size + 1) / 2;
|
||||||
|
uint16_t *buffer = calloc(buffer_size, sizeof(uint16_t));
|
||||||
|
size_t words_read = fread(buffer, sizeof(uint16_t), buffer_size, file);
|
||||||
|
printf("file_size = %zu, words_read = %zu, buffer_size = %zu\n",
|
||||||
|
file_size,
|
||||||
|
words_read,
|
||||||
|
buffer_size);
|
||||||
|
assert(words_read == buffer_size);
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
fs_write(fs, &inode, buffer, buffer_size);
|
||||||
|
|
||||||
|
printf("wrote %zu words to file %s.\n", words_read, vpath);
|
||||||
|
free(buffer);
|
||||||
|
save(fs, &disk, disk_filename);
|
||||||
|
} else if (strcmp(cmd, "importb") == 0) {
|
||||||
|
if (argc < 5)
|
||||||
|
goto malformed_command;
|
||||||
|
const char *rpath = argv[3];
|
||||||
|
const char *vpath = argv[4];
|
||||||
|
|
||||||
|
load(fs, &disk, disk_filename);
|
||||||
|
|
||||||
|
INode inode;
|
||||||
|
resolve_path(fs, &inode, vpath);
|
||||||
|
|
||||||
|
FILE *file = fopen(rpath, "r");
|
||||||
|
assert(file);
|
||||||
|
fseek(file, 0, SEEK_END);
|
||||||
|
long file_size = ftell(file);
|
||||||
|
assert(file_size > 0);
|
||||||
|
fseek(file, 0, SEEK_SET);
|
||||||
|
|
||||||
|
size_t buffer_size = file_size;
|
||||||
|
uint16_t *buffer = calloc(buffer_size, sizeof(uint16_t));
|
||||||
|
size_t words_read = 0;
|
||||||
|
|
||||||
|
int ch;
|
||||||
|
while ((ch = fgetc(file)) != EOF) {
|
||||||
|
buffer[words_read] = ch;
|
||||||
|
words_read += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(words_read == buffer_size);
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
fs_write(fs, &inode, buffer, buffer_size);
|
||||||
|
|
||||||
|
printf("wrote %zu words to file %s.\n", words_read, vpath);
|
||||||
|
free(buffer);
|
||||||
|
save(fs, &disk, disk_filename);
|
||||||
|
} else {
|
||||||
|
printf("error: uknown command '%s'\n", cmd);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
malformed_command:
|
||||||
|
printf("error: malformed %s command\n", cmd);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
@ -32,6 +32,7 @@ syn match vcPreProc '@if'
|
|||||||
syn match vcPreProc '@elif'
|
syn match vcPreProc '@elif'
|
||||||
syn match vcPreProc '@else'
|
syn match vcPreProc '@else'
|
||||||
syn match vcPreProc '@guard'
|
syn match vcPreProc '@guard'
|
||||||
|
syn match vcPreProc '@org'
|
||||||
|
|
||||||
syn match vcNumber '0'
|
syn match vcNumber '0'
|
||||||
syn match vcNumber '[1-9][0-9]*'
|
syn match vcNumber '[1-9][0-9]*'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user