From ad0a29a314d090ee820694c39ea7d11addb8742f Mon Sep 17 00:00:00 2001
From: "leleraffa97@hotmail.it" <leleraffa97@hotmail.it>
Date: Thu, 31 Aug 2017 17:27:10 +0200
Subject: Some untracked files from last commit

---
 sw/z80/kernel/fs.c     |   5 -
 sw/z80/kernel/fs/fd.c  | 255 +++++++++++++++++++++++++++++++++++++++++++++++++
 sw/z80/kernel/fs/fs.c  | 236 +++++++++++++++++++++++++++++++++++++++++++++
 sw/z80/kernel/memory.c |  29 ++++++
 4 files changed, 520 insertions(+), 5 deletions(-)
 delete mode 100644 sw/z80/kernel/fs.c
 create mode 100644 sw/z80/kernel/fs/fd.c
 create mode 100644 sw/z80/kernel/fs/fs.c

(limited to 'sw/z80/kernel')

diff --git a/sw/z80/kernel/fs.c b/sw/z80/kernel/fs.c
deleted file mode 100644
index 3ce7f42..0000000
--- a/sw/z80/kernel/fs.c
+++ /dev/null
@@ -1,5 +0,0 @@
-#include "fs.h"
-#include "users.h"
-#include "mount.h"
-
-struct fs_mount_point * mount_points[FS_MOUNT_LIMIT];
diff --git a/sw/z80/kernel/fs/fd.c b/sw/z80/kernel/fs/fd.c
new file mode 100644
index 0000000..96603ad
--- /dev/null
+++ b/sw/z80/kernel/fs/fd.c
@@ -0,0 +1,255 @@
+#include "fs/fd.h"
+#include "fs/fdop.h"
+#include "fs/fs.h"
+
+/* from fd.h */
+struct file_desc fd_table[FD_TABLE_SIZE];
+struct uint8_t  fd_bmap[FD_TABLE_SIZE / 8];
+
+/* File descriptors implementation */
+
+int8_t open(const char *path, uint8_t flags)
+{
+    // TODO
+    return 0; 
+}
+
+size_t ls(int8_t fd, struct dirent *buf, uint8_t all)
+{
+    // TODO
+    return 0; 
+}
+
+size_t find(int8_t fd, struct dirent *buf, const char *name)
+{
+    // TODO
+    return 0; 
+}
+
+int8_t mkdir(int8_t fd, const char *name)
+{
+    // TODO
+    return 0; 
+}
+
+int8_t touch(int8_t fd, const char *name)
+{
+    // TODO
+    return 0; 
+}
+
+int8_t ln(int8_t fd, const char *name)
+{
+    // TODO
+    return 0; 
+}
+
+int8_t seek(int8_t fd, fsize_t pos)
+{
+    // TODO
+    return 0; 
+}
+
+size_t readline(int8_t fd, char *buf, char term)
+{
+    // TODO
+    return 0; 
+}
+
+int8_t print(int8_t fd, const char *str)
+{
+    // TODO
+    return 0; 
+}
+
+size_t read(int8_t fd, void *buf, size_t n)
+{
+    // TODO
+    return 0; 
+}
+
+int8_t write(int8_t fd, const void *buf, size_t n)
+{
+    // TODO
+    return 0; 
+}
+
+void close(int8_t fd)
+{
+    // TODO
+    return 0; 
+}
+
+/* Set of file descriptors operations */
+/* Determines if to use the serial interface or the address space */
+
+/* Address space section */
+
+/* File functions */
+
+size_t as_readline(inode_t inode, fsize_t seek, char *buf, char term)
+{
+    // TODO
+    return 0;
+}
+
+size_t as_print(inode_t inode, fsize_t seek, const char *str)
+{
+    // TODO
+    return 0;
+}
+
+size_t as_append(inode_t inode, fsize_t *seek_ptr, const char *str)
+{
+    // TODO
+    return 0;
+}
+
+size_t as_read(inode_t inode, fsize_t seek, void *buf, size_t n)
+{
+    // TODO
+    return 0;
+}
+
+size_t as_write(inode_t inode, fsize_t seek, const void *buf, size_t n)
+{
+    // TODO
+    return 0;
+}
+
+size_t as_bin_append(inode_t inode, fsize_t *seek_ptr, const void *buf, size_t
+n)
+{
+    // TODO
+    return 0;
+}
+
+/* Directory functions */
+
+size_t as_ls(inode_t inode, struct dirent *buf, uint8_t all)
+{
+    // TODO
+    return 0;
+}
+
+size_t as_find(inode_t inode, struct dirent *buf, const char *name)
+{
+    // TODO
+    return 0;
+}
+
+int8_t as_mkdir(inode_t inode, const char *name)
+{
+    // TODO
+    return 0;
+}
+
+int8_t as_rmdir(inode_t inode, const char *name)
+{
+    // TODO
+    return 0;
+}
+
+int8_t as_touch(inode_t inode, const char *name)
+{
+    // TODO
+    return 0;
+}
+
+int8_t as_rm(inode_t inode, const char *name)
+{
+    // TODO
+    return 0;
+}
+
+int8_t as_ln(inode_t inode, const char *path, const char *name)
+{
+    // TODO
+    return 0;
+}
+
+/* Serial Space section */
+/* Warning, it doesn't switch to the right driver */
+
+/* File functions */
+
+size_t ss_readline(inode_t inode, fsize_t seek, char *buf, char term)
+{
+    // TODO
+    return 0;
+}
+
+size_t ss_print(inode_t inode, fsize_t seek, const char *str)
+{
+    // TODO
+    return 0;
+}
+
+size_t ss_append(inode_t inode, fsize_t *seek_ptr, const char *str)
+{
+    // TODO
+    return 0;
+}
+
+size_t ss_read(inode_t inode, fsize_t seek, void *buf, size_t n)
+{
+    // TODO
+    return 0;
+}
+
+size_t ss_write(inode_t inode, fsize_t seek, const void *buf, size_t n)
+{
+    // TODO
+    return 0;
+}
+
+size_t ss_bin_append(inode_t inode, fsize_t *seek_ptr, const void *buf, size_t
+n)
+{
+    // TODO
+    return 0;
+}
+
+/* Directory functions */
+
+size_t ss_ls(inode_t inode, struct dirent *buf, uint8_t all)
+{
+    // TODO
+    return 0;
+}
+
+size_t ss_find(inode_t inode, struct dirent *buf, const char *name)
+{
+    // TODO
+    return 0;
+}
+
+int8_t ss_mkdir(inode_t inode, const char *name)
+{
+    // TODO
+    return 0;
+}
+
+int8_t ss_rmdir(inode_t inode, const char *name)
+{
+    // TODO
+    return 0;
+}
+
+int8_t ss_touch(inode_t inode, const char *name)
+{
+    // TODO
+    return 0;
+}
+
+int8_t ss_rm(inode_t inode, const char *name)
+{
+    // TODO
+    return 0;
+}
+
+int8_t ss_ln(inode_t, const char *path, const char *name)
+{
+    // TODO
+    return 0;
+}
diff --git a/sw/z80/kernel/fs/fs.c b/sw/z80/kernel/fs/fs.c
new file mode 100644
index 0000000..584f934
--- /dev/null
+++ b/sw/z80/kernel/fs/fs.c
@@ -0,0 +1,236 @@
+#include "fs/fs.h"
+#include "fs/users.h"
+#include "fs/dev.h"
+
+/* from users.h */
+struct fs_user c_user;
+
+/* from inode.h */
+inode_t c_inode;
+
+struct fs_dev devices[FS_MOUNT_LIMIT];
+
+/* filesystem basic implementation */
+
+devsize_t fs_block(blk_t block)
+{
+    // TODO
+    return 0;
+}
+
+devsize_t fs_inode(struct fs_inode *buf)
+{
+    struct fs_superblock * sb;
+    devsize_t seek;
+
+    if (FS_USE_ROM(c_inode))
+    {
+        seek = FS_ROM_SPACE;
+        sb = (struct fs_superblock*) seek; 
+
+    } else {
+
+        seek = 0;
+
+        if (c_inode.dev > 0x7)
+            panic("Invalid device");    // TODO, still to implement
+
+        sb = &devices[c_inode.dev].superblock; 
+    }
+
+    if (c_inode.inode >= sb->imap_size)
+    {
+        // TODO, set errno
+        return 0;   // inode doesn't exist
+    }
+
+    seek += sizeof(struct fs_superblock) + 
+            sizeof(struct fs_inode) * c_inode.inode;
+
+    if (FS_USE_ROM(c_inode))
+        *buf = *(struct fs_inode*)seek;
+    else
+    {
+        /* set sio port */
+        sio_port = devices[c_inode.dev].port_no;
+        sio_seek = seek;
+
+        if (sio_read((void *)buf, INODE_META_SIZE) < INODE_META_SIZE)
+        {
+            // TODO, set errno
+            return 0;   // cannot read to device
+        }
+    }
+
+    return seek + INODE_META_SIZE;
+}
+
+/* from fd.h */
+int8_t __fd_new()
+{
+    int8_t fd;
+    for (int8_t = 0; i < FD_TABLE_SIZE / 8; i++)
+    {
+        for (uint8_t bit = 0x80, j = 0; bit > 0; bit >>= 1, j++)
+        {
+            if (!(bit & fd_bmap[i]))
+                return i * 8 + j;
+        }
+    }
+
+    return -1;
+}
+
+/* from fd.h */
+int8_t __open_c_inode(uint8_t flags)
+{
+    /* read meta data */
+    struct fs_inode inode;
+
+    /* init file_desc buffer */
+    struct file_desc desc = {c_inode, 0};
+    
+    desc.begin = fs_inode(&inode);
+    
+    /* bind operations */
+
+    if (flags & OPEN_DIR)
+    {
+        /* dir handler */
+
+        if (inode.type == INODE_TYPE_DIR)
+        {
+            /* bind dir functions */
+            if (FS_USE_ROM(c_inode))
+                /* bind address space functions */
+                FD_BIND_AS_DIR(desc.opers)
+            else
+                /* bind serial space functions */
+                FD_BIND_SS_DIR(desc.opers)
+
+        } else {
+
+            // TODO, set errno
+            return -1;  // not a directory
+        }
+
+    } else if (flags & OPEN_LINK) {
+        
+        /* link handler */
+
+        if (inode.type == INODE_TYPE_LINK)
+        {
+            if (FS_USE_ROM(c_inode))
+                FD_BIND_AS_FILE(desc.opers, flags)
+            else
+                FD_BIND_SS_FILE(desc.opers, flags)
+
+            if (flags & OPEN_ERASE)
+                /* empty file content */
+                __inode_empty(desc.begin);
+
+        } else {
+            
+            // TODO, set errno
+            return -1;  // not a link
+        }
+
+    } else {
+
+        /* file */
+
+        if (inode.type == INODE_TYPE_FILE)
+            ;   // do nothing
+        else if (inode.type == INODE_TYPE_SPECIAL) {
+            
+            // TODO, bind special callbacks
+            
+        } else {
+
+            // TODO, set errno
+            return -1;
+        }
+
+        /* bind operations */
+
+        if (FS_USE_ROM(c_inode))
+        {
+            if (flags & OPEN_BIN)
+                FD_BIND_AS_BINFILE(desc.opers, flags)
+            else
+                FD_BIND_AS_FILE(desc.opers, flags) 
+
+        } else {
+            
+            if (flags & OPEN_BIN)
+                FD_BIND_SS_BINFILE(desc.opers, flags)
+            else
+                FD_BIND_SS_FILE(desc.opers, flags)
+        }
+    }
+
+    /* find a free fd space */
+    
+    int8_t fd = __fd_new();
+
+    if (fd < 0)
+    {
+        // TODO, set errno, fd not available
+
+    } else
+        fd_table[fd] = desc; // copy buffer into the table
+
+    return fd;
+}
+
+inode_t fs_rec_path(const char *path);
+
+inode_t fs_parse_path(const char *path)
+{
+    inode_t ino, cwd = c_inode;
+
+    switch (path[0])
+    {
+        case '0':
+        FS_INODE_NULL(ino)
+        return ino;
+
+        case '/':
+        FS_INODE_ROOT(ino)
+        c_inode = ino;       // set cwd to root
+        path++;
+        break;
+
+        case '~':
+
+        if (path[1] = '/')
+        {
+            c_inode.dev = FS_DEV_ROM;
+            c_inode.inode = c_user.home;  // set cwd to home
+            path += 2;
+        }
+            
+        break;
+   }
+
+   ino = fs_rec_path(path);
+   c_inode = cwd;
+   return ino;
+}
+
+inode_t fs_rec_path(const char *path)
+{
+    devsize_t inode = fs_inode(c_inode.inode); 
+    inode_t rel;
+
+    if (!inode)   // if not exists
+    {
+        FS_INODE_NULL(rel)
+        return rel;
+    }
+
+    // TODO, check if dir or file
+    // TODO, if dir, find name
+    // TODO, set fs_cwd to new inode
+    // TODO, recall fs_rec_path
+}
diff --git a/sw/z80/kernel/memory.c b/sw/z80/kernel/memory.c
index f174906..bcda3c3 100644
--- a/sw/z80/kernel/memory.c
+++ b/sw/z80/kernel/memory.c
@@ -41,3 +41,32 @@ int page_unmap(int page)
     pages_table[page].used = 0;
     return 0;
 }
+
+/* k_malloc manager */
+/*
+struct k_buf_entry k_buf_table[K_BUF_MAX_COUNT];
+
+struct k_buf k_buffers[K_BUF_MAX_COUNT];
+
+void * k_malloc()
+{
+    for (uint8_t i = 0; i < K_BUF_TABLE_COUNT; i++)
+    {
+        for (uint8_t bit = 0x80, j = 0; j < 8; bit >>= 1, j++)
+        {
+            if (bit & k_buf_table[i])
+            {
+                k_buf_table[i] |= bit;
+                return &k_buffers[8 * i + j];
+            }
+        }
+    }
+}
+
+void k_free(void * ptr)
+{
+    uint8_t index = (ptr - &k_buffers[0]) / K_BUF_SIZE;
+
+    if (index < K_BUF_MAX_COUNT)
+        k_buf_table[index / 8] ^= 0x80 >> index % 8;
+}*/
-- 
cgit v1.2.1