diff options
Diffstat (limited to 'src/fs/initrd.c')
-rw-r--r-- | src/fs/initrd.c | 67 |
1 files changed, 47 insertions, 20 deletions
diff --git a/src/fs/initrd.c b/src/fs/initrd.c index 44e5493..de00a4a 100644 --- a/src/fs/initrd.c +++ b/src/fs/initrd.c @@ -1,3 +1,23 @@ +/** + * @file initrd.c + * Filesystem module for handling the initrd + * + * Copyright (C) 2018 Clyne Sullivan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + #include <stdint.h> #include <kernel/heap.h> @@ -5,7 +25,6 @@ typedef struct { char *address; - uint32_t pos; uint32_t size; } initrd_info; @@ -16,8 +35,9 @@ static const uint8_t *initrd_start = (uint8_t *)_binary_initrd_img_start; static const uint32_t initrd_size = (uint32_t)_binary_initrd_img_size; void *initrd_open(const char *file); -uint32_t initrd_read(void *info, uint32_t count, uint8_t *buffer); -int initrd_close(void *info); +uint32_t initrd_read(vfs_file_info *info, uint32_t count, uint8_t *buffer); +int initrd_close(vfs_file_info *info); +int initrd_seek(vfs_file_info *info, uint32_t offset, int whence); char *initrd_getfile(uint32_t offset); @@ -26,7 +46,8 @@ static const vfs_volume_funcs initrd_funcs = { initrd_close, initrd_read, 0, // write - 0 // readdir + 0, // readdir + initrd_seek }; int initrd_strncmp(const char *a, const char *b, unsigned int n) @@ -53,7 +74,6 @@ void *initrd_open(const char *file) initrd_info *file = (initrd_info *)malloc( sizeof(initrd_info)); file->address = ptr + len + 8; - file->pos = 0; file->size = *(uint32_t *)(ptr + len + 4); return file; } @@ -62,41 +82,48 @@ void *initrd_open(const char *file) return 0; } -int initrd_close(void *info) +int initrd_close(vfs_file_info *info) { // Nothing to do - free(info); + free(info->fsinfo); return 0; } -uint32_t initrd_read(void *info, uint32_t count, uint8_t *buffer) +uint32_t initrd_read(vfs_file_info *info, uint32_t count, uint8_t *buffer) { - initrd_info *iinfo = (initrd_info *)info; + initrd_info *iinfo = (initrd_info *)info->fsinfo; if (iinfo == 0 || iinfo->address == 0) return 0; uint32_t i; for (i = 0; i < count; i++) { - if (iinfo->pos >= iinfo->size) + if (info->pos >= iinfo->size) break; - buffer[iinfo->pos] = iinfo->address[iinfo->pos]; - iinfo->pos++; + buffer[info->pos] = iinfo->address[info->pos]; + info->pos++; } return i; } -/*char *readfile(const char *name) +int initrd_seek(vfs_file_info *info, uint32_t offset, int whence) { - char *ptr; - for (uint32_t i = 0; ptr = getfile(i), ptr != 0; i++) { - uint32_t len = *((uint32_t *)ptr); - if (!strncmp(name, ptr + 4, len)) - return ptr + len + 8; - } + initrd_info *iinfo = (initrd_info *)info->fsinfo; + if (iinfo == 0 || iinfo->address == 0) + return 0; + + if (whence == SEEK_SET) + info->pos = offset; + else if (whence == SEEK_CUR) + info->pos += offset; + else if (whence == SEEK_END) + info->pos = iinfo->size + offset; + else + return -1; + return 0; -}*/ +} char *initrd_getfile(uint32_t offset) { |