OSDev.org https://forum.osdev.org/ |
|
File I/O from Kernel https://forum.osdev.org/viewtopic.php?f=1&t=31556 |
Page 1 of 2 |
Author: | ReznicencuBogdan [ Thu Apr 20, 2017 5:16 pm ] |
Post subject: | File I/O from Kernel |
I know there are lots of questions like this , but the answers were of no help . I followed the instructions from the Bare Bones - OSDev Wiki http://wiki.osdev.org/Bare_Bones (create a bootloader and load a kernel to output a string) .Everything went fine . But I thought i could create a simple log file for a very basic game .And on google all the answers used some external libraries (<linux/kernel.h> , <fcntl.h> ,<sys/types.h>... etc.) which I don't have or don't know how to link to the kernel. Notice : A lot of people said that it's not recommended/possible (due to some policy reasons) doing I/O calls from Kernel but also a lot of people say that it is possible and that there's no problem and show some code that isn't C compatible. I am pretty stuck here .Any suggestion is of help . Thank you . |
Author: | StudlyCaps [ Thu Apr 20, 2017 5:47 pm ] |
Post subject: | Re: File I/O from Kernel |
The thing is, when you first start writing a kernel, there are no files. You first need some sort of disk driver, to read and write to whatever storage device you're using. That will give you chucks of binary data. Then you need a file system driver, which allows you to interpret the raw binary into files and directories. Then you will have commands equivalent to read and write from Linux. If you're still in real mode, you can use int 13H instead of writing a native disk driver, but the file system driver you'll have to do yourself. |
Author: | no92 [ Thu Apr 20, 2017 6:04 pm ] |
Post subject: | Re: File I/O from Kernel |
The thing with operating system development is that you start with absolutely nothing. You have to write everything. It means "[...] that there is no C standard library, only what we provide ourselves." If you want any sort of function that does I/O, you have to write it yourself. End of story. If you want more specific help you'll have to actually ask a question. For that, you'll need to provide more information. What type of I/O do you want? What did you check out? What exactly are you trying to do? Also, please tell me, is <linux/kernel.h> a library? |
Author: | Brendan [ Thu Apr 20, 2017 6:34 pm ] |
Post subject: | Re: File I/O from Kernel |
Hi, ReznicencuBogdan wrote: I know there are lots of questions like this , but the answers were of no help . I followed the instructions from the Bare Bones - OSDev Wiki http://wiki.osdev.org/Bare_Bones (create a bootloader and load a kernel to output a string) .Everything went fine . But I thought i could create a simple log file for a very basic game .And on google all the answers used some external libraries (<linux/kernel.h> , <fcntl.h> ,<sys/types.h>... etc.) which I don't have or don't know how to link to the kernel. When you read a file there are multiple steps at multiple layers. For the highest level:
For the next level (file systems); in general, you get a request to fetch something from VFS (directory info, or file data) and convert them into requests to fetch something from a storage device driver (blocks/sectors from a partition). This depends on what kind of file system, and it involves caching things that VFS doesn't already cache (e.g. the "cluster allocation table" for FAT). Of course this also needs to be asynchronous, and should keep track of "pending requests" and perform them in an optimised order (and not necessarily the order requests arrive) taking into account the IO priorities. For the next level (storage device drivers); in general, you get a request to fetch something from a file system (or kernel for swap space) and convert them into requests for the hardware itself to do. This also needs to be asynchronous, and should keep track of "pending requests" and perform them in an optimised order (and not necessarily the order requests arrive) taking into account the IO priorities. Now.... Before you can write a storage device driver, you will need:
Cheers, Brendan |
Author: | MichaelFarthing [ Fri Apr 21, 2017 1:24 am ] |
Post subject: | Re: File I/O from Kernel |
How to persuade someone to give up in one long post!!! What Brendan describes is the ultimate end after some years experience and improvements. What you actually need first is a routine to read or write a group of contiguous sectors to and from a disc unit somewhere on your system that you have dedicated to your OS. I have a partition on a hard disc not used by any other OS. That's the first target. Once you can do that you can read in, edit and write out. Magic. You'll feel great. It doesn't need any of Brendan's optimisations (yet). It doesn't need to have queues and it can be as synchronous as you like. There's only you using it and you're only doing one thing at once at the moment and you're not using a lot of data. It's fine. Keep it simple. It will be fast enough for your needs. A file system would be nice of course. A first stab that'll cope with a dozen different files is easily set up in five minutes with a sheet of A4 paper and a ballpoint pen on which you have three columns: File Name. Start Cluster. Finish cluster Make each file a lot bigger than it needs to be: delays the time when you will need to decide what to do when you want to resave a file, but it has grown beyond the space allocated to it. Now the first of these dozen files (or even the seventh if you want to be unconventional) could be used as a file index to replace your sheet of A4 paper which is getting a bit scruffy and has a lot of crossings out and alterations. Keep this first file (if you want you can rename is as a file index rather than a file if that makes you feel more professional, but in reality it's perfectly OK being just a file) - now what was I saying - keep this first file simple. You don't NEED directories (yet). You can now write code to consult this file for a filename and use the information to read it in and write it back out again. Then you can add a routine to create a new file and to rename a file. And put a listing on screen.. And... The secret is that each stage is just a gentle improvement on the previous one. And don't be worried that what you do today won't be compatible with what you need tomorrow. Tomorrow you'll have a lot more experience and will feel confident about throwing your juvenile code away and doing everything a lot better second time round. |
Author: | dozniak [ Fri Apr 21, 2017 2:17 am ] |
Post subject: | Re: File I/O from Kernel |
Long story short: if you have a game in your kernel and it needs to write a log file you're doing it all WRONG. It's not an OS, it's a bare-metal game and have little to do with OSdev. |
Author: | Velko [ Fri Apr 21, 2017 3:45 am ] |
Post subject: | Re: File I/O from Kernel |
Question to OP: do you really need to write a log file? Or just a simple way to write log? I would suggest to send your log via serial port to another computer and view/store it in more mature OS (Linux, Windows). And if you run your OS/game in emulator, you can capture serial output as well. Driver for serial port is pretty simple to make. |
Author: | ReznicencuBogdan [ Fri Apr 21, 2017 5:49 am ] |
Post subject: | Re: File I/O from Kernel |
I apreciate the sugestions . But i don't thing i want to get into the file system for the moment.Isn't there some easy way to write some chunks of data (some gibberish for the moment like some system color(store default color),system properties(some other silly thing)) on some empty sector on the harddisk ? It might be a silly question , but I am not really that deep with the info.Still have a lot to learn. Quote: What you actually need first is a routine to read or write a group of contiguous sectors to and from a disc unit somewhere on your system that you have dedicated to your OS. I have a partition on a hard disc not used by any other OS. Ok . How can i achieve this ?Can you please provide me some references , online documents?Searched but couldn't find something usefull. Thank you. |
Author: | iansjack [ Fri Apr 21, 2017 6:23 am ] |
Post subject: | Re: File I/O from Kernel |
I don't think you have searched hard enough. There is loads of information about this on this wiki and the wider Internet. I don't think people are going to do your research for you. |
Author: | ReznicencuBogdan [ Fri Apr 21, 2017 7:15 am ] |
Post subject: | Re: File I/O from Kernel |
iansjack wrote: I don't think you have searched hard enough. There is loads of information about this on this wiki and the wider Internet. I don't think people are going to do your research for you. Every single person on this forum did the same thing .Said "there is lots of information about this .... you didn't search enough." and that was all. I didn't say there isn't . I said i found but couldn't implement , and so I asked for advice on this topic (some low level). So all you did was to fill up this page for nothing .Your intervention in this case was useless. You spent some minutes of your life for nothing . I asked for some consistent support , not useless activity. If you were to be In my place , how would you feel when someone like you would post an answer like yours after having spent hours looking for an answer , a good one? |
Author: | sleephacker [ Fri Apr 21, 2017 8:20 am ] |
Post subject: | Re: File I/O from Kernel |
ReznicencuBogdan wrote: I apreciate the sugestions . But i don't thing i want to get into the file system for the moment.Isn't there some easy way to write some chunks of data (some gibberish for the moment like some system color(store default color),system properties(some other silly thing)) on some empty sector on the harddisk ? It might be a silly question , but I am not really that deep with the info.Still have a lot to learn. Quote: What you actually need first is a routine to read or write a group of contiguous sectors to and from a disc unit somewhere on your system that you have dedicated to your OS. I have a partition on a hard disc not used by any other OS. Ok . How can i achieve this ?Can you please provide me some references , online documents?Searched but couldn't find something useful. You need to learn how to search. First of all, let me give you my definition of useful information: Anything that either helps to understand how to accomplish my goal (r/w to hard disk, easy-mode), or points me to information that does. This includes anything that might contain useful links (the internet is full of them!) or even keywords I can throw at google that I didn't know before. With that in mind, the general steps I use when looking (OSDev related) things up is:
Using this strategy, pretending I didn't know anything, I would have found the following things:
Spoiler alert: it doesn't get easier than int 13h Note that it is fine to do everything easy-mode when you're getting started, but if you want to write a proper OS you should take the advice that others have given both on the wiki and here on the forum: do it the best way, even if that's harder than you'd like because it will pay off (instead of hurting you in the long run, which is what the easy but not best way does). |
Author: | SpyderTL [ Fri Apr 21, 2017 9:37 am ] |
Post subject: | Re: File I/O from Kernel |
ReznicencuBogdan wrote: Ok . How can i achieve this ? Can you please provide me some references , online documents?Searched but couldn't find something usefull. If you followed the barebones tutorial, then you are using C and are in 32-bit protected mode when your kernel runs. This means that using the BIOS (INT 13h) isn't really an option. This only leaves the option of sending commands directly to the ATA controller. You can find more information on the wiki here: http://wiki.osdev.org/ATA_read/write_sectors But most of the examples on the wiki are written in Assembly. If you want a C version, the only one I could find on here is this forum post: viewtopic.php?f=1&p=167798#p167798 See if you can at least write some code to write and read registers on the ATA controller properly, and then we can help you communicate with the hard drive. |
Author: | ReznicencuBogdan [ Fri Apr 21, 2017 9:46 am ] |
Post subject: | Re: File I/O from Kernel |
Thank you . I appreciate your help. |
Author: | iansjack [ Sat Apr 22, 2017 1:52 am ] |
Post subject: | Re: File I/O from Kernel |
ReznicencuBogdan wrote: I'm not sure If you're aware of the fact that this is a forum for OS developers. Thank you for enlightening me as to the purpose of these forums. |
Author: | codyd51 [ Sun Apr 30, 2017 1:10 pm ] |
Post subject: | Re: File I/O from Kernel |
sleephacker wrote: You need to learn how to search. First of all, let me give you my definition of useful information: Anything that either helps to understand how to accomplish my goal (r/w to hard disk, easy-mode), or points me to information that does. This includes anything that might contain useful links (the internet is full of them!) or even keywords I can throw at google that I didn't know before. What a great response! I don't have anything to add to OP's question that hasn't already been covered. I just wanted to note that you and MichaelFarthing provided great, relevant, and approachable responses. Thanks to MichaelFarthing's blueprint, I now have an ATA driver (using PIO, unfortunately) and an extremely basic on-disk filesystem. No directories yet, but as he notes, gentle improvements are the name of the game Brendan gives great info as always, just out of the scope of what OP was looking for. This forum is great. Thanks for everything guys |
Page 1 of 2 | All times are UTC - 6 hours |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |