syscalls: done with open, write, close syscalls
This commit is contained in:
Binary file not shown.
+31
-27
@@ -1,29 +1,33 @@
|
|||||||
section .text
|
|
||||||
global _start ;must be declared for using gcc
|
|
||||||
_start: ;tell linker entry point
|
|
||||||
mov edx, len ;message length
|
|
||||||
mov ecx, msg ;message to write
|
|
||||||
mov ebx, 1 ;file descriptor (stdout)
|
|
||||||
mov eax, 4 ;system call number (sys_write)
|
|
||||||
int 0x80 ;call kernel
|
|
||||||
|
|
||||||
mov eax, 1 ;system call number (sys_exit)
|
|
||||||
int 0x80 ;call kernel
|
|
||||||
|
|
||||||
mov eax, 4
|
|
||||||
mov ebx, 1
|
|
||||||
mov ecx, msg2
|
|
||||||
mov edx, len2
|
|
||||||
int 0x80
|
|
||||||
|
|
||||||
mov eax, 1
|
|
||||||
int 0x80
|
|
||||||
|
|
||||||
jmp $
|
|
||||||
|
|
||||||
section .data
|
section .data
|
||||||
|
file_path db '/ebalo/huy.txt', 0 ; Specific file path, null-terminated
|
||||||
|
message db 'Hello, World from KatauOS!', 10, 0 ; Message to write, with newline
|
||||||
|
message_len equ $ - message - 1 ; Length of message (14 bytes, excluding null)
|
||||||
|
|
||||||
msg db 'Hello, world!',0x0 ;our dear string
|
section .text
|
||||||
len equ $ - msg ;length of our dear string
|
global _start
|
||||||
msg2 db 'seems like KatauOS just executed this ELF-file!!',0x0
|
|
||||||
len2 equ $ - msg2
|
_start:
|
||||||
|
; Open the file
|
||||||
|
mov eax, 5 ; System call number for sys_open
|
||||||
|
mov ebx, file_path ; Pointer to file path
|
||||||
|
mov ecx, 65 ; Flags: O_WRONLY (1) | O_CREAT (64)
|
||||||
|
mov edx, 0x1A4 ; Mode: 0644 (rw-r--r--)
|
||||||
|
int 0x80 ; Invoke system call
|
||||||
|
mov edi, eax ; Save file descriptor in edi
|
||||||
|
|
||||||
|
; Write to the file
|
||||||
|
mov eax, 4 ; System call number for sys_write
|
||||||
|
mov ebx, edi ; File descriptor
|
||||||
|
mov ecx, message ; Pointer to message
|
||||||
|
mov edx, message_len ; Number of bytes to write
|
||||||
|
int 0x80 ; Invoke system call
|
||||||
|
|
||||||
|
; Close the file
|
||||||
|
mov eax, 6 ; System call number for sys_close
|
||||||
|
mov ebx, edi ; File descriptor
|
||||||
|
int 0x80 ; Invoke system call
|
||||||
|
|
||||||
|
; Exit the program
|
||||||
|
mov eax, 1 ; System call number for sys_exit
|
||||||
|
xor ebx, ebx ; Return status 0
|
||||||
|
int 0x80 ; Invoke system call
|
||||||
|
|||||||
Binary file not shown.
+57
-5
@@ -1,13 +1,50 @@
|
|||||||
#include "../include/syscalls.h"
|
#include "../include/syscalls.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include "../include/stdio.h"
|
#include "../include/stdio.h"
|
||||||
|
#include "../include/liballoc.h"
|
||||||
|
|
||||||
#define STDIN_FILENO 0
|
#define STDIN_FILENO 0
|
||||||
#define STDOUT_FILENO 1
|
#define STDOUT_FILENO 1
|
||||||
#define STDERR_FILENO 2
|
#define STDERR_FILENO 2
|
||||||
|
|
||||||
#define EBADF 9
|
#define EBADF 9
|
||||||
typedef unsigned short umode_t
|
|
||||||
|
const char* flags_to_mode_str(int flags) {
|
||||||
|
static char mode[4] = {0};
|
||||||
|
int accmode = flags & 3;
|
||||||
|
|
||||||
|
if (accmode == 0) { // O_RDONLY
|
||||||
|
mode[0] = 'r';
|
||||||
|
mode[1] = '\0';
|
||||||
|
} else if (accmode == 1) { // O_WRONLY
|
||||||
|
if (flags & 0x400) { // O_APPEND
|
||||||
|
mode[0] = 'a';
|
||||||
|
mode[1] = '\0';
|
||||||
|
} else {
|
||||||
|
mode[0] = 'w';
|
||||||
|
mode[1] = '\0';
|
||||||
|
}
|
||||||
|
} else if (accmode == 2) { // O_RDWR
|
||||||
|
if (flags & 0x400) { // O_APPEND
|
||||||
|
mode[0] = 'a';
|
||||||
|
mode[1] = '+';
|
||||||
|
mode[2] = '\0';
|
||||||
|
} else if (flags & 0x200) { // O_TRUNC
|
||||||
|
mode[0] = 'w';
|
||||||
|
mode[1] = '+';
|
||||||
|
mode[2] = '\0';
|
||||||
|
} else {
|
||||||
|
mode[0] = 'r';
|
||||||
|
mode[1] = '+';
|
||||||
|
mode[2] = '\0';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
mode[0] = 'r';
|
||||||
|
mode[1] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
return mode;
|
||||||
|
}
|
||||||
|
|
||||||
int sys_write(TrapFrame *tf)
|
int sys_write(TrapFrame *tf)
|
||||||
{
|
{
|
||||||
@@ -32,18 +69,30 @@ int sys_write(TrapFrame *tf)
|
|||||||
|
|
||||||
int sys_open(TrapFrame *tf)
|
int sys_open(TrapFrame *tf)
|
||||||
{
|
{
|
||||||
const char* filename = tf->ebx;
|
const char* filename = (const char*)tf->ebx;
|
||||||
int flags = tf->ecx;
|
int flags = tf->ecx;
|
||||||
umode_t mode = tf->edx;
|
|
||||||
|
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
|
const char* mode_str = flags_to_mode_str(flags);
|
||||||
|
|
||||||
|
debug_log("\n====filename: %s\n", filename);
|
||||||
|
debug_log("\n====mode_str: %s\n", mode_str);
|
||||||
|
|
||||||
for(int i = 3; i < MAX_OPEN_FILES; i++)
|
for(int i = 3; i < MAX_OPEN_FILES; i++)
|
||||||
{
|
{
|
||||||
|
debug_log("\n====i: %X\n", i);
|
||||||
if(current->file_descriptors[i] == NULL)
|
if(current->file_descriptors[i] == NULL)
|
||||||
{
|
{
|
||||||
fat_fopen(current->file_descriptors[i], filename, (char)mode);
|
current->file_descriptors[i] = malloc(sizeof(file_t)); // Kernel malloc
|
||||||
|
debug_log("\n====FILE_DESCRIPTOR: %X\n", i);
|
||||||
|
int result = fat_fopen(current->file_descriptors[i], filename, mode_str);
|
||||||
|
if (result < 0) {
|
||||||
|
free(current->file_descriptors[i]); // Free on failure
|
||||||
|
current->file_descriptors[i] = NULL;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
fd = i;
|
fd = i;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,7 +104,10 @@ int sys_close(TrapFrame *tf)
|
|||||||
int fd = tf->ebx;
|
int fd = tf->ebx;
|
||||||
if(current->file_descriptors[fd] != NULL)
|
if(current->file_descriptors[fd] != NULL)
|
||||||
{
|
{
|
||||||
return fat_fclose(current->file_descriptors[fd]);
|
int result = fat_fclose(current->file_descriptors[fd]);
|
||||||
|
free(current->file_descriptors[fd]);
|
||||||
|
current->file_descriptors[fd] = NULL;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user