Forráskód Böngészése

feat: support fat32 lowercase filename

greatbridf 2 éve
szülő
commit
8e1293cd43
4 módosított fájl, 29 hozzáadás és 15 törlés
  1. 6 6
      CMakeLists.txt
  2. 16 6
      src/fs/fat.cpp
  3. 2 2
      src/kernel/process.cpp
  4. 5 1
      user-space-program/lazybox.c

+ 6 - 6
CMakeLists.txt

@@ -121,12 +121,12 @@ add_custom_target(boot.img
     COMMAND dd if=/dev/zero of=boot.img bs=`expr 512 \\* 1024 \\* 1024` count=0 seek=1
     COMMAND sh -c \"echo n\; echo\; echo\; echo\; echo\; echo a\; echo w\" | ${FDISK_BIN} boot.img
     COMMAND mkfs.fat --offset=2048 -v -n SYSTEM boot.img
-    COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/hello-world.out ::hello.out
-    COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/interrupt-test.out ::int.out
-    COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/stack-test.out ::stack.out
-    COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/init.out ::init.elf
-    COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/sh.out ::sh.elf
-    COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/priv-test.out ::priv.elf
+    COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/hello-world.out ::hello
+    COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/interrupt-test.out ::int
+    COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/stack-test.out ::stack
+    COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/init.out ::init
+    COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/sh.out ::sh
+    COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/priv-test.out ::priv
     COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/lazybox.out ::lazybox
     COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/lazybox.out ::pwd
 )

+ 16 - 6
src/fs/fat.cpp

@@ -1,4 +1,5 @@
 #include <assert.h>
+#include <ctype.h>
 #include <fs/fat.hpp>
 #include <kernel/mem.h>
 #include <kernel/mm.hpp>
@@ -9,6 +10,9 @@
 #include <types/hash_map.hpp>
 #include <types/status.h>
 
+#define VFAT_FILENAME_LOWERCASE (0x08)
+#define VFAT_EXTENSION_LOWERCASE (0x10)
+
 namespace fs::fat {
 // buf MUST be larger than 512 bytes
 inline void fat32::_raw_read_sector(void* buf, uint32_t sector_no)
@@ -73,8 +77,10 @@ int fat32::inode_readdir(fs::inode* dir, size_t offset, fs::vfs::filldir_func fi
         offset = 0;
         auto* end = d + (sectors_per_cluster * SECTOR_SIZE / sizeof(directory_entry));
         for (; d < end && d->filename[0]; ++d) {
-            if (d->attributes.volume_label)
+            if (d->attributes.volume_label) {
+                nread += sizeof(directory_entry);
                 continue;
+            }
 
             fs::ino_t ino = _rearrange(d);
             auto* ind = get_inode(ino);
@@ -92,16 +98,20 @@ int fat32::inode_readdir(fs::inode* dir, size_t offset, fs::vfs::filldir_func fi
             for (int i = 0; i < 8; ++i) {
                 if (d->filename[i] == ' ')
                     break;
-                fname += d->filename[i];
+                if (d->_reserved & VFAT_FILENAME_LOWERCASE)
+                    fname += tolower(d->filename[i]);
+                else
+                    fname += toupper(d->filename[i]);
             }
-            if (d->extension[0] != ' ') {
+            if (d->extension[0] != ' ')
                 fname += '.';
-                fname += d->extension[0];
-            }
             for (int i = 1; i < 3; ++i) {
                 if (d->extension[i] == ' ')
                     break;
-                fname += d->extension[i];
+                if (d->_reserved & VFAT_EXTENSION_LOWERCASE)
+                    fname += tolower(d->extension[i]);
+                else
+                    fname += toupper(d->extension[i]);
             }
             auto ret = filldir(fname.c_str(), 0, ind->ino,
                 (ind->flags.in.directory || ind->flags.in.mount_point) ? DT_DIR : DT_REG);

+ 2 - 2
src/kernel/process.cpp

@@ -277,11 +277,11 @@ void NORETURN _kernel_init(void)
     current_process->attr.system = 0;
     current_thread->attr.system = 0;
 
-    const char* argv[] = { "/mnt/INIT.ELF", "/mnt/SH.ELF", nullptr };
+    const char* argv[] = { "/mnt/init", "/mnt/sh", nullptr };
     const char* envp[] = { nullptr };
 
     types::elf::elf32_load_data d;
-    d.exec = "/mnt/INIT.ELF";
+    d.exec = "/mnt/init";
     d.argv = argv;
     d.envp = envp;
     d.system = false;

+ 5 - 1
user-space-program/lazybox.c

@@ -133,6 +133,9 @@ const char* find_file_name(const char* path)
 
 int parse_applet(const char* name)
 {
+    if (!name)
+        return -1;
+
     for (size_t i = 0; i < (sizeof(applets) / sizeof(struct applet)); ++i) {
         if (strcmpi(applets[i].name, name) == 0) {
             return i;
@@ -144,6 +147,7 @@ int parse_applet(const char* name)
 
 int main(int argc, const char** argv)
 {
+    (void)argc;
     int offset = 0;
     const char* name = find_file_name(argv[offset++]);
     int type = -1;
@@ -155,7 +159,7 @@ run:
         return -1;
     }
 
-    if (type == 0 && argc != 1) {
+    if (type == 0 && offset == 1) {
         name = argv[offset++];
         goto run;
     }