Add basic ls

This commit is contained in:
Freywar Ulvnaudgari
2026-06-17 19:20:08 +03:00
parent b38154d468
commit a9b8cbb5fe
10 changed files with 136 additions and 8 deletions
+5 -3
View File
@@ -13,7 +13,9 @@ dd if=/dev/zero of=build/os.img bs="${sector_size}" count="$((partition_offset +
dd if=build/mbr.bin of=build/os.img bs="${sector_size}" count=1 conv=notrunc dd if=build/mbr.bin of=build/os.img bs="${sector_size}" count=1 conv=notrunc
dd if=build/bootloader.bin of=build/os.img bs="${sector_size}" seek=1 count="$((partition_offset - 1))" conv=notrunc dd if=build/bootloader.bin of=build/os.img bs="${sector_size}" seek=1 count="$((partition_offset - 1))" conv=notrunc
mkfs.fat -F 16 --offset "${partition_offset}" build/os.img mkfs.fat -F 16 --offset "${partition_offset}" build/os.img
mcopy -i build/os.img@@"$((partition_offset * sector_size))" src ::src mcopy -i build/os.img@@"$((partition_offset * sector_size))" -s src ::src
mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/kernel.bin ::kernel.bin mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/kernel.bin ::kernel.bin
mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/terminal ::terminal mmd -i build/os.img@@"$((partition_offset * sector_size))" ::bin
mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/echo ::echo mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/terminal ::bin/terminal
mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/echo ::bin/echo
mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/ls ::bin/ls
+39 -1
View File
@@ -146,7 +146,6 @@ custom_target(
build_by_default: true, build_by_default: true,
) )
echo_start = custom_target( echo_start = custom_target(
'echo_start', 'echo_start',
input: 'src/user/start.asm', input: 'src/user/start.asm',
@@ -185,3 +184,42 @@ custom_target(
command: ['objcopy', '-O', 'binary', '@INPUT@', '@OUTPUT@'], command: ['objcopy', '-O', 'binary', '@INPUT@', '@OUTPUT@'],
build_by_default: true, build_by_default: true,
) )
ls_start = custom_target(
'ls_start',
input: 'src/user/start.asm',
output: 'ls_start.o',
command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'],
)
ls_syscall = custom_target(
'ls_syscall',
input: 'src/user/syscall.asm',
output: 'ls_syscall.o',
command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'],
)
ls_elf = executable(
'ls.elf',
sources: [ls_start, ls_syscall, lib_sources, 'src/user/app/ls/ls.c'],
c_args: [
'-ffreestanding',
'-nostdlib',
'-fno-stack-protector',
'-mcmodel=large',
'-DVERSION="' + meson.project_version() + '"',
],
link_args: [
'-T', meson.project_source_root() / 'src/user/linker.ld',
'-nostdlib',
],
link_depends: 'src/user/linker.ld',
)
custom_target(
'ls',
input: ls_elf,
output: 'ls',
command: ['objcopy', '-O', 'binary', '@INPUT@', '@OUTPUT@'],
build_by_default: true,
)
+1 -1
View File
@@ -56,7 +56,7 @@ void kernel_main() {
kernel->stdin = keyboard_init(); kernel->stdin = keyboard_init();
kernel->stdout = vga_init(); kernel->stdout = vga_init();
fs_node_t *bin = fs_open_by(kernel->root, "terminal"); fs_node_t *bin = path_open_file(kernel->root, kernel->cwd, "bin/terminal");
ASSERT(bin, "kernel: terminal not found"); ASSERT(bin, "kernel: terminal not found");
+1
View File
@@ -28,6 +28,7 @@ syscall_entry:
push r14 push r14
push r15 push r15
mov r8, r10 ; arg4
mov rcx, rdx ; arg3 mov rcx, rdx ; arg3
mov rdx, rsi ; arg2 mov rdx, rsi ; arg2
mov rsi, rdi ; arg1 mov rsi, rdi ; arg1
+28 -1
View File
@@ -145,11 +145,36 @@ static uint64_t spawn(const char *path, uint64_t argc, const char **argv) {
return 0; return 0;
} }
static uint64_t readdir(const char *path, uint64_t index, uint64_t max, char *to) {
fs_node_t *dir = path_open_directory(current_process->root, current_process->cwd, path);
if (!dir) {
return (uint64_t)-1;
}
fs_node_t *item = fs_open_at(dir, index);
if (!item) {
if (dir != current_process->root) {
fs_close(dir);
}
return 0;
}
uint64_t length = string_length(item->name);
uint64_t size = length > max - 1 ? max - 1 : length;
memory_copy(item->name, size, to);
to[size] = '\0';
fs_close(item);
if (dir != current_process->root) {
fs_close(dir);
}
return size;
}
static void exit() { static void exit() {
process_switch_to(&current_process->kernel_rsp, current_process->parent->kernel_rsp, VIRT_TO_PHYS(current_process->parent->pml4)); process_switch_to(&current_process->kernel_rsp, current_process->parent->kernel_rsp, VIRT_TO_PHYS(current_process->parent->pml4));
} }
uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t arg3) { uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4) {
switch (func) { switch (func) {
case SYSCALL_READ: case SYSCALL_READ:
return read(arg1, arg2, (char *)arg3); return read(arg1, arg2, (char *)arg3);
@@ -161,6 +186,8 @@ uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t
return chdir((const char *)arg1); return chdir((const char *)arg1);
case SYSCALL_SPAWN: case SYSCALL_SPAWN:
return spawn((const char *)arg1, arg2, (const char **)arg3); return spawn((const char *)arg1, arg2, (const char **)arg3);
case SYSCALL_READDIR:
return readdir((const char *)arg1, arg2, arg3, (char *)arg4);
case SYSCALL_EXIT: case SYSCALL_EXIT:
exit(); exit();
return 0; return 0;
+2 -1
View File
@@ -7,8 +7,9 @@
#define SYSCALL_GETCWD 2 #define SYSCALL_GETCWD 2
#define SYSCALL_CHDIR 3 #define SYSCALL_CHDIR 3
#define SYSCALL_SPAWN 4 #define SYSCALL_SPAWN 4
#define SYSCALL_READDIR 5
#define SYSCALL_EXIT 60 #define SYSCALL_EXIT 60
void syscall_init(); void syscall_init();
uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t arg3); uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4);
+43
View File
@@ -0,0 +1,43 @@
#include "src/lib/string.h"
#include "src/user/syscall.h"
#define WRITE_S(s) write(1, s, sizeof(s) - 1);
#define WRITE_D(s) write(1, s, string_length(s));
uint64_t ls(const char *path) {
uint64_t i = 0;
while (1) {
char out[100];
uint64_t s = readdir(path, i, 100, out);
if (!s) {
return 0;
}
if (s == (uint64_t)-1) {
WRITE_S("ls: path does not exist\n");
return (uint64_t)-1;
}
WRITE_D(out);
WRITE_S("\n");
i++;
}
}
uint64_t main(uint64_t argc, const char **argv) {
if (argc == 1) {
return ls(".");
}
uint64_t code = 0;
for (uint64_t i = 1; i < argc; i++) {
if (argc > 2) {
WRITE_S("\n");
WRITE_D(argv[i]);
WRITE_S(":\n");
}
if (ls(argv[i]) == (uint64_t)-1) {
code = (uint64_t)-1;
}
}
return code;
}
+8 -1
View File
@@ -6,6 +6,8 @@
static void help(uint8_t argc, char **argv); static void help(uint8_t argc, char **argv);
static void cd(uint8_t argc, char **argv); static void cd(uint8_t argc, char **argv);
#define PATH "/bin/"
#define WRITE_S(s) write(1, s, sizeof(s) - 1); #define WRITE_S(s) write(1, s, sizeof(s) - 1);
#define WRITE_D(s) write(1, s, string_length(s)); #define WRITE_D(s) write(1, s, string_length(s));
@@ -117,9 +119,14 @@ static void on_enter_pressed() {
} }
} }
if (i == sizeof(builtins) / sizeof(app_entry_t)) { if (i == sizeof(builtins) / sizeof(app_entry_t)) {
if (spawn(argv[0], argc, (const char **)argv) == (uint64_t)-1){ uint64_t size = sizeof(PATH) + string_length(argv[0]) + 1;
char *path = memory_allocate(size);
memory_copy(PATH, sizeof(PATH), path);
memory_copy(argv[0], size - sizeof(PATH), path + sizeof(PATH) - 1);
if (spawn(path, argc, (const char **)argv) == (uint64_t)-1) {
WRITE_S("terminal: program not found\n"); WRITE_S("terminal: program not found\n");
} }
memory_free(path);
} }
prompt_length = prompt_offset = 0; prompt_length = prompt_offset = 0;
print_prompt(); print_prompt();
+7
View File
@@ -30,6 +30,13 @@ spawn:
syscall syscall
ret ret
global readdir
readdir:
mov r10, rcx
mov rax, 5
syscall
ret
global exit global exit
exit: exit:
mov rax, 60 mov rax, 60
+2
View File
@@ -12,4 +12,6 @@ uint64_t chdir(const char *path);
uint64_t spawn(const char *path, uint64_t argc, const char **argv); uint64_t spawn(const char *path, uint64_t argc, const char **argv);
uint64_t readdir(const char *path, uint64_t index, uint64_t max, char *to);
void exit(); void exit();