Skip to content
Snippets Groups Projects
Commit e7a985c4 authored by Byron Lathi's avatar Byron Lathi
Browse files

work on adding arguments, separate file types

parent 5e420234
No related branches found
No related tags found
No related merge requests found
#include "syscall.h"
int main()
int main(int argc, char** argv)
{
char argcs = argc + '0';
sys_write(1, &argcs, 1); // so that works
char* arg0 = argv[0]; // but this shouldn't
sys_write(1, arg0, 1);
uint8_t test[64];
int ret;
while (1){
......
#include <types.h>
#include <lib.h>
#include <Syscalls/execute.h>
#include "directory.h"
#include "filedescriptor.h"
static fops_t directory_ops = {directory_open, directory_close, directory_read, directory_write};
int32_t directory_read(int32_t fd, void* buf, int32_t len)
{
}
int32_t directory_write(int32_t fd, const void* buf, int32_t len)
{
}
int32_t directory_open(const uint8_t* name)
{
printf("directory_open\n");
return 2;
}
int32_t directory_close(int32_t fd)
{
}
#ifndef _DIRECTORY_H
#define _DIRECTORY_H
#include <types.h>
int32_t directory_read(int32_t fd, void* buf, int32_t len);
int32_t directory_write(int32_t fd, const void* buf, int32_t len);
int32_t directory_open(const uint8_t* name);
int32_t directory_close(int32_t fd);
#endif
\ No newline at end of file
#include <types.h>
#include <lib.h>
#include <Syscalls/execute.h>
#include <Syscalls/syscalls.h>
#include "file.h"
#include "filedescriptor.h"
static fops_t file_ops = {file_open, file_close, file_read, file_write};
int32_t file_read(int32_t fd, void* buf, int32_t len)
{
}
int32_t file_write(int32_t fd, const void* buf, int32_t len)
{
}
int32_t file_open(const uint8_t* name)
{
printf("file_open\n");
int32_t fd = find_new_fd();
pcb_t* pcb = get_pcb_ptr();
pcb->file_desc_array[fd].file_pos = 0;
pcb->file_desc_array[fd].flags |= FD_INUSE;
pcb->file_desc_array[fd].file_ops = &file_ops;
pcb->file_desc_array[fd].inode = ext2_parse_path_to_inode_num((int8_t*)name);
return fd;
}
int32_t file_close(int32_t fd)
{
}
#ifndef _FILE_H
#define _FILE_H
#include <types.h>
int32_t file_read(int32_t fd, void* buf, int32_t len);
int32_t file_write(int32_t fd, const void* buf, int32_t len);
int32_t file_open(const uint8_t* name);
int32_t file_close(int32_t fd);
#endif
\ No newline at end of file
#include <types.h>
#include <Syscalls/execute.h>
#include "filedescriptor.h"
int32_t find_new_fd()
{
pcb_t* pcb = get_pcb_ptr();
int i;
for (i = 2; i < MAX_FD; i++) {
if (!pcb->file_desc_array[i].flags & 1) {
return i;
}
}
}
#ifndef _FILEDESCRIPTOR_H
#define _FILEDESCRIPTOR_H
#include <types.h>
#define FD_INUSE 1
/* file operations struct */
typedef struct fops_t {
int32_t (*open)(const uint8_t* filename);
int32_t (*close)(int32_t fd);
int32_t (*read)(int32_t fd, void* buf, int32_t nbytes);
int32_t (*write)(int32_t fd, const void* buf, int32_t nbytes);
} fops_t;
/* file descriptors struct */
typedef struct file_desc_t {
fops_t* file_ops;
uint32_t inode;
uint32_t file_pos;
uint32_t flags;
} file_desc_t;
int32_t find_new_fd();
#endif
\ No newline at end of file
......@@ -49,13 +49,73 @@ pcb_t* get_pcb_ptr()
}
int32_t _sys_exec(uint8_t* name)
int32_t _sys_exec(uint8_t* cmd)
{
union {
uint16_t data16[512];
uint8_t data8[1024];
} data;
// ignore leading white space
while (*cmd == ' ') {
*cmd = NULL;
cmd++;
}
uint32_t cmdlen = strlen((int8_t*)cmd);
// stop at the first space after that
int len = 0;
while (*(cmd + len) != ' ' && *(cmd + len) != NULL) {
len++;
}
uint8_t name[len+1];
strncpy((int8_t*)cmd, (int8_t*)name, len);
name[len] = NULL;
uint8_t* tmpargs = cmd;
uint32_t argc = 1;
while (*tmpargs != NULL) {
if (*tmpargs != ' ' && *(tmpargs-1) == ' ') {
argc++;
}
tmpargs++;
}
printf("argc: %d\n", argc);
uint8_t* argv[argc];
// This will not work for arguments which need spaces
int i;
for (i = 0; i < cmdlen; i++) {
if (cmd[i] == ' ')
cmd[i] = NULL;
}
//argv[0] = name;
tmpargs = cmd;
for (i = 0; i < argc; i++) {
while (*tmpargs == NULL) {
tmpargs++;
if (tmpargs - cmd > cmdlen)
break;
}
argv[i] = tmpargs;
while (*tmpargs) {
tmpargs++;
if (tmpargs - cmd > cmdlen)
break;
}
}
for (i = 0; i < argc; i++) {
printf("arg%d: %s\n", i, argv[i]);
}
uint32_t parsed_inode = ext2_parse_path_to_inode_num((int8_t*)name);
if (parsed_inode == -1)
......@@ -91,13 +151,19 @@ int32_t _sys_exec(uint8_t* name)
uint32_t status;
uint32_t* user_argc = (uint32_t*)0x83ffff8;
uint8_t*** user_argv = (uint8_t***)0x83ffffc;
*user_argc = argc;
*user_argv = argv;
/* push use data segment, user stack pointer,
* flags, user code segment, entry point, then iret */
asm volatile(" \n\
movl %%ebp, %0 \n\
movl %%esp, %1 \n\
pushl $0x2B \n\
pushl $0x83ffffc \n\
pushl $0x83ffff8 \n\
pushfl \n\
pop %%eax \n\
orl $0x200, %%eax \n\
......
......@@ -3,6 +3,8 @@
#include <types.h>
#include <Filesystem/filedescriptor.h>
#define EXEC_NOT_FOUND -1
#define EXEC_NOT_ELF -2
......@@ -66,26 +68,11 @@ typedef struct elf_header{
uint16_t e_shstrndx;
} elf_header_t;
/* file operations struct */
typedef struct fops_t {
int32_t (*open)(const uint8_t* filename);
int32_t (*close)(int32_t fd);
int32_t (*read)(int32_t fd, void* buf, int32_t nbytes);
int32_t (*write)(int32_t fd, const void* buf, int32_t nbytes);
} fops_t;
/* file descriptors struct */
typedef struct file_desc_t {
fops_t* file_ops;
uint32_t inode;
uint32_t file_pos;
uint32_t flags;
} file_desc_t;
#define MAX_FD 8
typedef struct pcb {
file_desc_t file_desc_array[8];
int32_t is_vidmapped;
uint8_t args[128];
uint32_t execute_return;
int32_t pid;
int32_t parent_pid;
......
#include <types.h>
#include <lib.h>
#include <Filesystem/ext2.h>
#include <Filesystem/file.h>
#include <Filesystem/directory.h>
#include "syscalls.h"
#include "execute.h"
......@@ -28,14 +30,18 @@ int32_t sys_open(uint8_t* name)
return -1;
}
int32_t fd = -1;
ext2_get_inode(inode_num, &inode);
printf("Inode %x type %x ", inode_num, inode.type_permissions);
printf("Inode %x type %x\n", inode_num, inode.type_permissions);
if (inode.type_permissions & EXT2_DIR) {
printf("is a directory.\n");
fd = directory_open(name);
} else if (inode.type_permissions & EXT2_FILE) {
printf("is a file.\n");
fd = file_open(name);
}
return -1;
return fd;
}
int32_t sys_close(int32_t fd)
......
......@@ -306,6 +306,22 @@ uint32_t strcpy(const int8_t* src, int8_t* dest)
return 0;
}
uint32_t strncpy(const int8_t* src, int8_t* dest, uint32_t n)
{
if (!src || !dest)
return -1;
int i;
for (i = 0; i < n; i++) {
dest[i] = src[i];
if (!src[i]) {
return 0;
}
}
return 0;
}
/*
* These font data were read out of video memory during text mode and
......
......@@ -32,6 +32,7 @@ int8_t *strrev(int8_t* s);
uint32_t strlen(const int8_t* s);
uint32_t strncmp(const int8_t* a, const int8_t* b, uint32_t n);
uint32_t strcpy(const int8_t* src, int8_t* dest);
uint32_t strncpy(const int8_t* src, int8_t* dest, uint32_t n);
void clear(void);
extern unsigned char font_data[256][16];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment