From b8d19ee7c2258c1bc4927f838dfae2dc9e803dff Mon Sep 17 00:00:00 2001 From: Mateusz Stadnik <matgla@live.com> Date: Fri, 26 Apr 2024 19:55:20 +0200 Subject: [PATCH] refactoring ongoing --- common/CMakeLists.txt | 10 +- .../filesystem/filesystem_mount_points.hpp | 53 ------ ...le_descriptors.cpp => file_descriptors.cc} | 41 ++-- .../filesystem.cpp2} | 34 ++-- common/source/filesystem_mount_points.cc | 92 +++++++++ common/source/filesystem_mount_points.cpp | 71 ------- cpu/arm/v6-m/common/source/system_stubs.cpp | 170 ++++++++--------- packages.json | 5 + source/fs/fs.cpp | 53 ------ source/fs/fs.cpp2 | 0 source/fs/littlefs.cpp | 177 ------------------ source/fs/littlefs.cpp2 | 37 +++- source/main.cpp2 | 4 +- 13 files changed, 242 insertions(+), 505 deletions(-) delete mode 100644 common/include/common/filesystem/filesystem_mount_points.hpp rename common/source/{file_descriptors.cpp => file_descriptors.cc} (63%) rename common/{include/common/filesystem/file_descriptors.hpp => source/filesystem.cpp2} (63%) create mode 100644 common/source/filesystem_mount_points.cc delete mode 100644 common/source/filesystem_mount_points.cpp delete mode 100644 source/fs/fs.cpp delete mode 100644 source/fs/fs.cpp2 delete mode 100644 source/fs/littlefs.cpp diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index fb1884d..c634b9f 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -24,21 +24,21 @@ set(include_dir ${CMAKE_CURRENT_SOURCE_DIR}/include/common) target_sources(common PUBLIC - ${include_dir}/filesystem/filesystem_mount_points.hpp - ${include_dir}/filesystem/file_descriptors.hpp ${include_dir}/filesystem/disk_parameters.hpp - ${include_dir}/filesystem/filesystem.hpp ${include_dir}/units.hpp PRIVATE source/units.cpp - source/filesystem_mount_points.cpp - source/file_descriptors.cpp + PUBLIC + FILE_SET CXX_MODULES FILES + source/file_descriptors.cc + source/filesystem_mount_points.cc ) use_cppfront( TARGET common MODULE_SOURCES source/disk_parameters.cpp2 + source/filesystem.cpp2 ) target_include_directories(common diff --git a/common/include/common/filesystem/filesystem_mount_points.hpp b/common/include/common/filesystem/filesystem_mount_points.hpp deleted file mode 100644 index 6d4f6c8..0000000 --- a/common/include/common/filesystem/filesystem_mount_points.hpp +++ /dev/null @@ -1,53 +0,0 @@ -/** - * filesystem_mount_points.hpp - * - * Copyright (C) 2024 Mateusz Stadnik <matgla@live.com> - * - * 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/>. - */ - -#pragma once - -#include <map> -#include <memory> -#include <string> -#include <string_view> -#include <utility> - -#include "common/filesystem/filesystem.hpp" - -namespace yasboot::fs -{ - -class FilesystemMountPoints -{ -public: - using MountPoints = std::map<std::string, std::unique_ptr<Filesystem>>; - - int register_mount_point(std::string_view path, - std::unique_ptr<Filesystem> filesystem); - - std::pair<Filesystem *, std::string_view> get_mount_point( - std::string_view pathname); - Filesystem *get_filesystem_for_fd(int fd); - - static FilesystemMountPoints &get(); - -private: - FilesystemMountPoints() = default; - MountPoints mount_points_; -}; - -} // namespace yasboot::fs diff --git a/common/source/file_descriptors.cpp b/common/source/file_descriptors.cc similarity index 63% rename from common/source/file_descriptors.cpp rename to common/source/file_descriptors.cc index ab7e4ac..8b5d0a9 100644 --- a/common/source/file_descriptors.cpp +++ b/common/source/file_descriptors.cc @@ -1,3 +1,4 @@ +module; /** * file_descriptors.cpp * @@ -18,23 +19,36 @@ * <https://www.gnu.org/licenses/>. */ -#include "common/filesystem/file_descriptors.hpp" +#include <bitset> +#include <cstdint> -namespace yasboot::fs -{ +export module yasboot.filesystem.file_descriptors; -FileDescriptors &FileDescriptors::get() -{ +export namespace yasboot::fs { +class FileDescriptors { +public: + static FileDescriptors &get(); + + int8_t allocate(); + void release(int8_t fd); + +private: + FileDescriptors(); + + std::bitset<8> file_descriptors_; +}; +} // namespace yasboot::fs + +namespace yasboot::fs { + +FileDescriptors &FileDescriptors::get() { static FileDescriptors instance; return instance; } -int8_t FileDescriptors::allocate() -{ - for (int8_t i = 0; i < static_cast<int8_t>(file_descriptors_.size()); i++) - { - if (!file_descriptors_.test(i)) - { +int8_t FileDescriptors::allocate() { + for (int8_t i = 0; i < static_cast<int8_t>(file_descriptors_.size()); i++) { + if (!file_descriptors_.test(i)) { file_descriptors_.set(i); return i; } @@ -42,10 +56,7 @@ int8_t FileDescriptors::allocate() return -1; } -void FileDescriptors::release(int8_t fd) -{ - file_descriptors_.reset(fd); -} +void FileDescriptors::release(int8_t fd) { file_descriptors_.reset(fd); } FileDescriptors::FileDescriptors() = default; diff --git a/common/include/common/filesystem/file_descriptors.hpp b/common/source/filesystem.cpp2 similarity index 63% rename from common/include/common/filesystem/file_descriptors.hpp rename to common/source/filesystem.cpp2 index 2079c98..17c3749 100644 --- a/common/include/common/filesystem/file_descriptors.hpp +++ b/common/source/filesystem.cpp2 @@ -1,5 +1,6 @@ +module; /** - * file_descriptors.hpp + * filesystem.cpp2 * * Copyright (C) 2024 Mateusz Stadnik <matgla@live.com> * @@ -18,26 +19,17 @@ * <https://www.gnu.org/licenses/>. */ -#pragma once +export module yasboot.filesystem; -#include <bitset> -#include <cstdint> +yasboot: namespace = { +fs: namespace = { -namespace yasboot::fs -{ +export FileSystem: @interface type = { + mount: (inout this) -> std::expected<void, i32>; + open: (inout this, path: std::string_view, flags: i32) -> std::expected<i8, i8>; + has_fd: (this, fd: i32) -> bool; + read_file: (inout this, fd: i32, buffer: std::span<u8>) -> i32; +} -class FileDescriptors -{ -public: - static FileDescriptors &get(); - - int8_t allocate(); - void release(int8_t fd); - -private: - FileDescriptors(); - - std::bitset<8> file_descriptors_; -}; - -} // namespace yasboot::fs +} // namespace fs +} // namespace yasboot diff --git a/common/source/filesystem_mount_points.cc b/common/source/filesystem_mount_points.cc new file mode 100644 index 0000000..31b6fe5 --- /dev/null +++ b/common/source/filesystem_mount_points.cc @@ -0,0 +1,92 @@ +module; +/** + * filesystem_mount_points.cpp + * + * Copyright (C) 2024 Mateusz Stadnik <matgla@live.com> + * + * 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 <map> +#include <memory> +#include <string> +#include <string_view> +#include <utility> + +export module yasboot.filesystem.filesystem_mount_points; + +import yasboot.filesystem; + +export namespace yasboot::fs { +class FileSystemMountPoints { +public: + using MountPoints = std::map<std::string, std::unique_ptr<FileSystem>>; + + int register_mount_point(std::string_view path, + std::unique_ptr<FileSystem> filesystem); + + std::pair<FileSystem *, std::string_view> + get_mount_point(std::string_view pathname); + + const FileSystem *get_filesystem_for_fd(int fd) const; + + static FileSystemMountPoints &get(); + +private: + FileSystemMountPoints() = default; + MountPoints mount_points_; +}; + +} // namespace yasboot::fs + +namespace yasboot::fs { + +int FileSystemMountPoints::register_mount_point( + std::string_view path, std::unique_ptr<FileSystem> filesystem) { + if (mount_points_.count(std::string(path))) { + return -1; + } + + mount_points_.emplace(std::string(path), std::move(filesystem)); + return 0; +} + +FileSystemMountPoints &FileSystemMountPoints::get() { + static FileSystemMountPoints instance; + return instance; +} + +std::pair<FileSystem *, std::string_view> +FileSystemMountPoints::get_mount_point(std::string_view pathname) { + std::pair<FileSystem *, std::string_view> result{}; + for (const auto &[path, filesystem] : mount_points_) { + if (pathname.starts_with(path) && path.size() > result.second.size()) { + result = std::make_pair(filesystem.get(), pathname.substr(path.size())); + } + } + + return result; +} + +const FileSystem *FileSystemMountPoints::get_filesystem_for_fd(int fd) const { + for (const auto &[path, fs] : mount_points_) { + if (fs->has_fd(fd)) { + return fs.get(); + } + } + return nullptr; +} + +} // namespace yasboot::fs diff --git a/common/source/filesystem_mount_points.cpp b/common/source/filesystem_mount_points.cpp deleted file mode 100644 index 7a3b2f3..0000000 --- a/common/source/filesystem_mount_points.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/** - * filesystem_mount_points.cpp - * - * Copyright (C) 2024 Mateusz Stadnik <matgla@live.com> - * - * 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 "common/filesystem/filesystem_mount_points.hpp" - -namespace yasboot::fs -{ - -int FilesystemMountPoints::register_mount_point( - std::string_view path, std::unique_ptr<Filesystem> filesystem) -{ - if (mount_points_.count(std::string(path))) - { - return -1; - } - - mount_points_.emplace(std::string(path), std::move(filesystem)); - return 0; -} - -FilesystemMountPoints &FilesystemMountPoints::get() -{ - static FilesystemMountPoints instance; - return instance; -} - -std::pair<Filesystem *, std::string_view> FilesystemMountPoints::get_mount_point( - std::string_view pathname) -{ - std::pair<Filesystem *, std::string_view> result{}; - for (const auto &[path, filesystem] : mount_points_) - { - if (pathname.starts_with(path) && path.size() > result.second.size()) - { - result = std::make_pair(filesystem.get(), pathname.substr(path.size())); - } - } - - return result; -} - -Filesystem *FilesystemMountPoints::get_filesystem_for_fd(int fd) -{ - for (const auto &[path, fs] : mount_points_) - { - if (fs->has_fd(fd)) - { - return fs.get(); - } - } - return nullptr; -} - -} // namespace yasboot::fs diff --git a/cpu/arm/v6-m/common/source/system_stubs.cpp b/cpu/arm/v6-m/common/source/system_stubs.cpp index be9d2aa..b42c4c0 100644 --- a/cpu/arm/v6-m/common/source/system_stubs.cpp +++ b/cpu/arm/v6-m/common/source/system_stubs.cpp @@ -20,127 +20,103 @@ #include <unistd.h> -#include "common/filesystem/filesystem_mount_points.hpp" - #include <string_view> import hal.system.io; -extern "C" -{ - void panic(const char *, ...) - { - while (true) - { - } +extern "C" { +void panic(const char *, ...) { + while (true) { } +} - void __unhandled_user_irq() - { - while (true) - { - } +void __unhandled_user_irq() { + while (true) { } +} - void hard_assertion_failure() - { - while (true) - { - } +void hard_assertion_failure() { + while (true) { } +} - int __attribute__((used)) _kill(pid_t, int) - { - return 0; - } +int __attribute__((used)) _kill(pid_t, int) { return 0; } - pid_t __attribute__((used)) _getpid(void) - { - return 0; - } +pid_t __attribute__((used)) _getpid(void) { return 0; } - int __attribute__((used)) _fstat(int, struct stat *) - { - return 0; - } +int __attribute__((used)) _fstat(int, struct stat *) { return 0; } - int __attribute((used)) _isatty(int) - { - return 0; - } +int __attribute((used)) _isatty(int) { return 0; } - void __attribute__((used)) _exit(int) - { - while (true) - { - } - } - - int __attribute__((used)) _close(int fd) - { - auto fs = yasboot::fs::FilesystemMountPoints::get().get_filesystem_for_fd(fd); - if (fs == nullptr) - { - return -1; - } - return fs->close(fd); +void __attribute__((used)) _exit(int) { + while (true) { } +} - off_t __attribute__((used)) _lseek(int, off_t, int) - { - return 0; - } +int __attribute__((used)) _close(int fd) { + // auto fs = + // yasboot::fs::FilesystemMountPoints::get().get_filesystem_for_fd(fd); if (fs + // == nullptr) { + // return -1; + // } + // return fs->close(fd); + static_cast<void>(fd); + return -1; +} - ssize_t __attribute__((used)) _read(int fd, void *buf, size_t size) - { - auto fs = yasboot::fs::FilesystemMountPoints::get().get_filesystem_for_fd(fd); - if (fs == nullptr) - { - return 0; - } - return fs->read_file(fd, std::span<uint8_t>(static_cast<uint8_t *>(buf), size)); - return 0; - } +off_t __attribute__((used)) _lseek(int, off_t, int) { return 0; } + +ssize_t __attribute__((used)) _read(int fd, void *buf, size_t size) { + // auto fs = + // yasboot::fs::FilesystemMountPoints::get().get_filesystem_for_fd(fd); if (fs + // == nullptr) { + // return 0; + // } + // return fs->read_file(fd, + // std::span<uint8_t>(static_cast<uint8_t *>(buf), + // size)); + static_cast<void>(fd); + static_cast<void>(buf); + static_cast<void>(size); + return 0; +} - ssize_t __attribute__((used)) _write(int fd, const char *buf, size_t count) - { - const auto write = yasboot::hal::system::io::get_global_write(); - if ((fd == 1 || fd == 2) && write) - { - return write(std::string_view(buf, count)); - } - return 0; +ssize_t __attribute__((used)) _write(int fd, const char *buf, size_t count) { + const auto write = yasboot::hal::system::io::get_global_write(); + if ((fd == 1 || fd == 2) && write) { + return write(std::string_view(buf, count)); } + return 0; +} - extern char __heap_start__; - extern char __heap_end__; - - static char *current_heap_end = &__heap_start__; +extern char __heap_start__; +extern char __heap_end__; - void *_sbrk(intptr_t incr) - { - if (current_heap_end + incr > &__heap_end__) - { - return nullptr; - } +static char *current_heap_end = &__heap_start__; - char *previous_heap_end = current_heap_end; - current_heap_end += incr; - return static_cast<caddr_t>(previous_heap_end); +void *_sbrk(intptr_t incr) { + if (current_heap_end + incr > &__heap_end__) { + return nullptr; } - int __attribute__((used)) _open(const char *pathname, int flags) - { - auto [fs, path] = - yasboot::fs::FilesystemMountPoints::get().get_mount_point(pathname); + char *previous_heap_end = current_heap_end; + current_heap_end += incr; + return static_cast<caddr_t>(previous_heap_end); +} - if (!fs) - { - printf("Responding with -1\n"); - errno = ENOENT; - return -1; - } +int __attribute__((used)) _open(const char *pathname, int flags) { + // auto [fs, path] = + // yasboot::fs::FilesystemMountPoints::get().get_mount_point(pathname); - return fs->open(path, flags); - } + // if (!fs) { + // printf("Responding with -1\n"); + // errno = ENOENT; + // return -1; + // } + + // return fs->open(path, flags);system_st + static_cast<void>(pathname); + static_cast<void>(flags); + return -1; +} } diff --git a/packages.json b/packages.json index d63dbc6..90cfa5e 100644 --- a/packages.json +++ b/packages.json @@ -77,6 +77,11 @@ "-Wno-cast-qual", "-Wno-shadow" ] + }, + "compile_definitions": { + "PUBLIC": [ + "-DLFS_READONLY=1" + ] } } } diff --git a/source/fs/fs.cpp b/source/fs/fs.cpp deleted file mode 100644 index ab12381..0000000 --- a/source/fs/fs.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/** - * mbr.cpp - * - * Copyright (C) 2023 Mateusz Stadnik <matgla@live.com> - * - * 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 "yasboot/mbr/mbr.hpp" - -namespace yasboot -{ - -MbrParser::MbrParser(const hal::Disk &disk) : disk_{disk}, mbr_{} -{ - disk_.read_sector(0, &mbr_); -} - -bool MbrParser::isValidMbr() const -{ - return mbr_.signature == 0xaa55; -} - -const MbrHeader &MbrParser::mbr() const -{ - return mbr_; -} - -const MbrPartitionEntry *MbrParser::getBootablePartition() const -{ - for (const auto &partition : mbr_.partitions) - { - if ((partition.status & static_cast<uint8_t>(0x80)) != 0) - { - return &partition; - } - } - return nullptr; -} - -} // namespace yasboot diff --git a/source/fs/fs.cpp2 b/source/fs/fs.cpp2 deleted file mode 100644 index e69de29..0000000 diff --git a/source/fs/littlefs.cpp b/source/fs/littlefs.cpp deleted file mode 100644 index d547d41..0000000 --- a/source/fs/littlefs.cpp +++ /dev/null @@ -1,177 +0,0 @@ -/** - * littlefs.cpp - * - * Copyright (C) 2024 Mateusz Stadnik <matgla@live.com> - * - * 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 "yasboot/fs/littlefs.hpp" - -#include <limits> - -#include <fcntl.h> - -#include "common/filesystem/file_descriptors.hpp" - -namespace yasboot::fs -{ - -int LittleFS::lfs_read_callback(const struct lfs_config *c, lfs_block_t block, - lfs_off_t off, void *buffer, lfs_size_t size) -{ - return static_cast<LittleFS *>(c->context) - ->read_from_disk_(block * c->block_size + off, - std::span<uint8_t>(static_cast<uint8_t *>(buffer), size)); -} - -int LittleFS::lfs_write_callback(const struct lfs_config *c, lfs_block_t block, - lfs_off_t off, const void *buffer, lfs_size_t size) -{ - return static_cast<LittleFS *>(c->context) - ->write_to_disk_( - block * c->block_size + off, - std::span<const uint8_t>(static_cast<const uint8_t *>(buffer), size)); -} - -int LittleFS::lfs_erase_callback(const struct lfs_config *c, lfs_block_t block) -{ - return static_cast<LittleFS *>(c->context)->erase_(block); -} - -int LittleFS::lfs_sync_callback(const struct lfs_config *c) -{ - return static_cast<LittleFS *>(c->context)->sync_(); -} - -LittleFS::LittleFS(const DiskParameters &disk, ReadFromDisk read, WriteToDisk write, - Erase erase, Sync sync) - : read_from_disk_{std::move(read)} - , write_to_disk_{std::move(write)} - , erase_{std::move(erase)} - , sync_{std::move(sync)} - , lfs_config_{ - .context = this, - .read = lfs_read_callback, - .prog = lfs_write_callback, - .erase = lfs_erase_callback, - .sync = lfs_sync_callback, - .read_size = disk.read_size, - .prog_size = disk.write_size, - .block_size = disk.block_size, - .block_count = disk.block_count, - .block_cycles = 600, - .cache_size = - 256, // plenty of RAM on RP2040, but make this configurable via KConfig - .lookahead_size = 16, - .compact_thresh = static_cast<lfs_size_t>(0.8 * disk.block_size), - .read_buffer = nullptr, - .prog_buffer = nullptr, - .lookahead_buffer = nullptr, - .name_max = LFS_NAME_MAX, - .file_max = LFS_FILE_MAX, - .attr_max = LFS_ATTR_MAX, - .metadata_max = disk.block_size, - .inline_max = 0 - } - , lfs_{} -{ - printf("Block size: %d, blocks: %d\n", disk.block_size, disk.block_count); -} - -bool LittleFS::mount() -{ - return lfs_mount(&lfs_, &lfs_config_) == LFS_ERR_OK; -} - -constexpr int convertFlags(int flags) -{ - int result = 0; - if ((flags & O_RDONLY) != 0) - { - result |= LFS_O_RDONLY; - } - else if ((flags & O_WRONLY) != 0) - { - result |= LFS_O_WRONLY; - } - else if ((flags & O_RDWR) != 0) - { - result |= LFS_O_RDWR; - } - - if ((flags & O_APPEND) != 0) - { - result |= LFS_O_APPEND; - } - - if ((flags & O_CREAT) != 0) - { - result |= LFS_O_CREAT; - } - return result; -} - -int LittleFS::open(std::string_view path, int flags) -{ - int8_t fd = FileDescriptors::get().allocate(); - if (fd == -1) - { - return fd; - } - files_[fd] = lfs_file_t{}; - - if (lfs_file_open(&lfs_, &files_[fd], path.data(), convertFlags(flags)) != - LFS_ERR_OK) - { - FileDescriptors::get().release(fd); - return -1; - } - - return fd; -} - -int LittleFS::close(int fd) -{ - if (!has_fd(fd)) - { - return -1; - } - - return lfs_file_close(&lfs_, &files_[static_cast<int8_t>(fd)]); -} - -bool LittleFS::has_fd(int fd) const -{ - if (fd < 0 || fd > std::numeric_limits<uint8_t>::max()) - { - return false; - } - return files_.contains(static_cast<int8_t>(fd)); -} - -int LittleFS::read_file(int fd, std::span<uint8_t> buffer) -{ - if (!has_fd(fd)) - { - return -1; - } - - auto r = lfs_file_read(&lfs_, &files_[static_cast<int8_t>(fd)], buffer.data(), - buffer.size()); - return r; -} - -} // namespace yasboot::fs diff --git a/source/fs/littlefs.cpp2 b/source/fs/littlefs.cpp2 index 3c1f40c..0ac3ac5 100644 --- a/source/fs/littlefs.cpp2 +++ b/source/fs/littlefs.cpp2 @@ -24,9 +24,10 @@ module; #include <map> #include <lfs.h> - -#include "common/filesystem/file_descriptors.hpp" - + +import yasboot.filesystem.file_descriptors; +import yasboot.filesystem; + export module yasboot.filesystem.littlefs; export import yasboot.filesystem.disk_parameters; @@ -36,8 +37,9 @@ export yasboot: namespace = { fs: namespace = { LittleFS: type = { ReadFromDisk: type == std::function<int(std::size_t, std::span<u8>)>; + + this: FileSystem = (); - operator=: (out this) = {} operator=: (out this, in disk: filesystem::DiskParameters, in read: ReadFromDisk) = { read_ = read; config_ = lfs_config( @@ -65,7 +67,7 @@ fs: namespace = { ); } - public mount: (inout this) -> std::expected<void, int> = { + public mount: (override inout this) -> std::expected<void, i32> = { ec:= lfs_mount(lfs_&, config_&); if (ec != LFS_ERR_OK) { return std::unexpected(ec); @@ -77,14 +79,10 @@ fs: namespace = { convert_flags: (in flags: i32) -> i32 = { result: i32 = 0; if ((flags & O_RDONLY) != 0) { result |= LFS_O_RDONLY; } - else if ((flags & O_WRONLY) != 0) { result |= LFS_O_WRONLY; } - else if ((flags & O_RDWR) != 0) { result |= LFS_O_RDWR; } - if ((flags & O_APPEND) != 0) { result |= LFS_O_APPEND; } - if ((flags & O_CREAT) != 0) { result |= LFS_O_CREAT; } return result; } - open: (inout this, path: std::string_view, flags: i32) -> std::expected<int8_t, int8_t> = { + open: (override inout this, path: std::string_view, flags: i32) -> std::expected<int8_t, int8_t> = { fd:= yasboot::fs::FileDescriptors::get().allocate(); if (fd == -1) { return std::unexpected(fd); @@ -98,6 +96,25 @@ fs: namespace = { return fd; } + has_fd: (override this, fd: i32) -> bool = { + if (fd < 0 || fd > std::numeric_limits<i8>::max()) + { + return false; + } + return files_.contains(cpp2::unsafe_narrow<i8>(fd)); + } + + read_file: (override inout this, fd: i32, buffer: std::span<u8>) -> i32 = + { + if (!has_fd(fd)) + { + return -1; + } + + return lfs_file_read(lfs_&, files_[cpp2::unsafe_narrow<i8>(fd)]&, buffer.data(), + buffer.size()); + } + lfs_read_callback: (in c: ConstLfsConfig, block: lfs_block_t, off: lfs_off_t, buffer: *void, size: lfs_size_t) -> int = { diff --git a/source/main.cpp2 b/source/main.cpp2 index b981d6f..094a7a2 100644 --- a/source/main.cpp2 +++ b/source/main.cpp2 @@ -54,9 +54,6 @@ App: @copyable type = { } prepare_boot_parition: (this) = { - //f: std::unique_ptr<yasboot::filesystem::Fun> = (); - //fs : new<yasboot::filesystem::LittleFS> = (); - //fs: yasboot::filesystem::LittleFS = (); p:= mbr_.get_bootable_partition(); if (p) { @@ -81,6 +78,7 @@ App: @copyable type = { if (s) { printf("Mounted successfuly\n"); + //yasboot::fs::register_mount_point("/boot", fs); } else {