phil-opp / blog_os

Writing an OS in Rust
http://os.phil-opp.com
Apache License 2.0
14.27k stars 1.01k forks source link

Is there any way to interact with the filesystem through the bootloader or other crates? #1238

Closed SortaUnknown closed 9 months ago

SortaUnknown commented 10 months ago

Firstly, I want to thank Phil Opp and other contributors to this series, you've introduced me and gotten me hooked to a field of programming that looked like absolute wizardry to me before. I've read and gotten through all of Edition 2 and then upgraded everything to the latest version with the help of the unfinished Edition 3 series. I've set up a very basic shell and I am now trying to give my kernel some "real" functionality, but I am running into a problem, I don't know how to create or read files so I have no way to permanently save data. I know the bootloader has to interact with the filesystem in some way in order to boot up the kernel ELF, but is there any way to read or write files with the current tools or not yet?

tsatke commented 10 months ago

To persist data, you would basically have to mount a drive through your emulator (which you do with -drive in qemu). You would then have to write a driver for the attached device (for example an ATA driver), which can read and write from the device. With that, you would be able to read and write flat bytes (or sectors) from the hard drive.

Next, you would decide on a file system format that is on the hard drive, let's say ext2 (which is simple enough to implement in its basic form). The file you mount in your emulator as device would have to be an ext2 image. You can then implement an ext2 driver, that understands the format within the bytes you've read from the device.

All in all, this is no trivial task, and afaik the bootloader uses other crates, which works for loading the kernel image, but is probably not too well suited for an actual OS use case (correct me if I'm wrong anyone, please).

You can check out my implementation as a (somewhat working) example here: https://github.com/tsatke/devos/tree/main/kernel/src/io/vfs

bjorn3 commented 10 months ago

Also note that on UEFI the bootloader crate uses boot services to load files from the fs. Boot services are no longer available when the kernel starts as the bootloader tells the UEFI firmware to exit boot services. If that wasn't done you wouldn't be able to do change the page tables or use more than one cpu core in the kernel.

SortaUnknown commented 9 months ago

Thanks for the guidance!