diff options
author | Wojtek Kosior <kwojtus@protonmail.com> | 2019-12-28 13:14:33 +0100 |
---|---|---|
committer | Wojtek Kosior <kwojtus@protonmail.com> | 2019-12-28 13:14:33 +0100 |
commit | 5b5ed23b44ae9eb845ebbac284e0b27822e45ed1 (patch) | |
tree | 431fd793491ea36131b09cd5877c5229f01356ce /ramfs.c | |
parent | 311ddd85042170f35736b9491ce952ee51e21db1 (diff) | |
download | rpi-MMU-example-5b5ed23b44ae9eb845ebbac284e0b27822e45ed1.tar.gz rpi-MMU-example-5b5ed23b44ae9eb845ebbac284e0b27822e45ed1.zip |
add ramfs driver
Diffstat (limited to 'ramfs.c')
-rw-r--r-- | ramfs.c | 65 |
1 files changed, 65 insertions, 0 deletions
@@ -0,0 +1,65 @@ +// driver for the read-only ramfs +// see makefs.c for details + +#include <stdint.h> +#include "ramfs.h" + +static int strcmp(char const *str1, char const *str2) +{ + while (1) + { + int c1 = (unsigned char) *str1, c2 = (unsigned char) *str2; + + if (!c1 && !c2) + return 0; + + if (c1 != c2) + return c1 - c2; + + str1++; str2++; + } +} + +static uint32_t strlen(char const *str1) +{ + uint32_t len = 0; + + while (str1[len]) + len++; + + return len; +} + +static inline char *align4(char *addr) +{ + return (char*) (((uint32_t) addr - 1) & ~0b11) + 4; +} + +int find_file(void *ramfs, char *filename, struct ramfile *buf) +{ + char *fs_file = ramfs; + + while (*fs_file) + { + uint32_t *fs_file_size = (uint32_t*) + align4(fs_file + strlen(fs_file) + 1); + + char *fs_file_contents = (char*) (fs_file_size + 1); + + if (!strcmp(fs_file, filename)) + { + buf->file_size = *fs_file_size; + + buf->file_name = fs_file; + + buf->file_contents = fs_file_contents; + + return 0; + } + + // move to the next file in ramfs + fs_file = align4(fs_file_contents + *fs_file_size); + } + + return -1; // reached end of ramfs; file not found +} |