4

I have developed a basic kernel in assembly/c that runs a basic terminal. I have set it up to run off of an iso with grub.

I would like to continue this OS, but without a file system, I feel as if there's really nothing else I could do. After much time on the internet, I have come up with really nothing I can do to implement this.

People have said implement FAT or make a VFS, but nothing any further, nor tutorials, nor any references to anywhere.

Could someone explain how a file system works, where I can get started/where I can connect a pre-made system, and how to use it?

Also, I do not have access to standard libraries when compiling my os. I use gcc, nasm, ld, and grub-mkrescue(for the disk image). I use qemu for emulation.

EDIT to make less OT

Can someone describe, in detail how a file system works, so when I look at the sources of file systems that have been implemented, like FAT, I can understand how to apply it to my own operating system?

EDIT - Simpler

Even easier. How could I directly access the hard drive? My kernel runs completely in protected mode, so could I switch out and write directly to the hard drive. A file system could be implemented with a file looking like this:

name special char text special char

ie:

hello world.script 0x00 println "Hello, world!!" 0x00

Where you wouldn't need special segmentation, you would just look until you find the file name and the special character (something not in a string like '\0') and then read until you find the second non-string character.

Would there be a way to access the hard drive by switching in and out of protected mode or write a hard disk driver in order to implement this?

Dylan Turner
  • 322
  • 1
  • 12
  • Sorry - Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – too honest for this site Jul 08 '15 at 19:20
  • 3
    You found nothing about, for example, the FAT file system? What about [Design of the FAT file system](https://en.wikipedia.org/wiki/Design_of_the_FAT_file_system)? That's just one of many links obtained with a Google search. Granted, there aren't tutorials that will give you a step-by-step how to code up the file system, but all the details you need about how different file systems are laid out exist with diagrams, etc. – lurker Jul 08 '15 at 19:21
  • 2
    The answer to your question of "how a file system works" depends upon the file system. There's lots of information if you Google "how does a file system work". Here's one article that may be helpful to start since it does explain some things in general with a bit of example: [Files Systems: FAT, NTFS, and HFS+](http://study.com/academy/lesson/files-systems-fat-ntfs-hfs-and-ffs.html). You can find similar articles that talk about Linux file systems (ext2, ext3, etc). Unfortunately, the question is still too broad to be on topic for this forum. – lurker Jul 08 '15 at 19:46
  • Is you OS [TernexOS2](http://blueokiris.webs.com/ternex-os-2-0)? You should have given the URL inside the question! – Basile Starynkevitch Jul 08 '15 at 20:00
  • No. It's a different operating system for the x86 platform. – Dylan Turner Jul 08 '15 at 20:10
  • 1
    If you want your file system to be useful (e.g., allow files to be created, changed, and deleted when necessary), your simplistic scheme will break down. A file system is, among other things, a dynamic memory allocation scheme for a drive medium. Direct access of the hard drive could be through the BIOS, or you'd write your own device driver driver. – lurker Jul 08 '15 at 21:56

2 Answers2

10

First, read wikipage on file systems to have some broad view.

The relevant resource about operating system development is OSdev (but perhaps your question is off-topic here). Kernelnewbies could also help (explaining how Linux is doing). OSdev have wikipages explaining FAT & Ext2 in details.

You could design an OS without any files (but some other persistence machinery). See this answer. You could have persistent processes (read also about application checkpointing, garbage collection, continuations, hibernation).

But you should read some good books about Operating Systems (e.g. by Tanenbaum, or the freely downloadable Operating Systems: Three Easy Pieces book). Be fluent with some existing free software OS, e.g. Linux (& POSIX), so read Advanced Linux Programming (at least to understand many concepts and get a good terminology).

IMHO, the FAT is such an ugly and inefficient file system that it is not worth looking into (except for legacy and compatibility reasons). Ext4 (see here) should be better & the wikipage on Ext2 has a nice picture.

You could adapt some library providing a file system (e.g. libext2) to your kernel.

You could perhaps adapt sqlite to work on a raw disk partition.

You might have a notion of file which is not like MSDOS (or Windows) or POSIX or <stdio.h> files. For example, it might be a sequence of fixed size records (e.g. of 1Kbyte), not a stream of bytes.

You could organize your OS as a microkernel and have file systems given by application code. Look into VSTa and HURD.

You need of course a disk driver, which fetches/writes blocks (of 4Kbytes) from your drive (disk I/O is always by blocks or disk sectors. Old small disks had 512 bytes blocks. New large disks have 4Kbytes ones, see advanced format). It should be interrupt driven and uses DMA. You need a task scheduler. AFAIU, you won't use the BIOS for this (perhaps the UEFI); you need to understand how common hardware (SATA & AHCI) works.

You should publish (today!) your toy OS as free software (e.g. under GPLv3+ on github) to get feedbacks and contributions.

You might copy (if licenses are compatible) existing code from other free software operating systems, and you certainly will study their source code to understand things.

So code some task scheduler, a page fault handler, a virtual memory, then add interrupt driven disk IO, and some file system code above that. Then you'll beginning to understand that an OS cannot be a small toy.... You might consider a microkernel or exokernel approach.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
4

It would be simplest to use an existing open-source filesystem if the licence terms suit your needs. ELM FatFs is one such library, with no usage restrictions whatsoever. You only need to provide the device control interface layer using the provided stubs and examples.

Clifford
  • 88,407
  • 13
  • 85
  • 165