Page 1 of 1

reading a file?

Posted: Sun Oct 20, 2002 11:42 am
by gtsphere
hey all, well i'm at the point where i am once again stumped... seems just like yesterday i was as well, heh, anyways

I currently have my kernel up and running, keyboard handler working great, got a prompt, able to compare input for commands now, etc.

now my question is how would I read a file to the screen, like dump its contents and then write to another file. I am more interested so far in just reading a file, IE: fstream.h. but i'm guessing either ways its the same.

also now i am wondering, how do I actually execute another program on the floppy disk? And just leave my kernel sitting there, no scheduling or anything, just having the other program take over completely.

any advice would be great, thanks so much :-)

Re:reading a file?

Posted: Sun Oct 20, 2002 1:03 pm
by Tom
Time for the Late-Great File system: Your File System. I'm soon coming to that point. You should ask frank about HD stuff, he's working on that stuff.

Re:reading a file?

Posted: Mon Oct 21, 2002 12:31 pm
by Schol-R-LEA
As Tom says, what you are really asking about it how to manipulate - and, possibly, how to design - a file system. This in turn breaks down to three parts: the low-level disk drivers, the logical file system interface, and last, the actual file system structure.

On the low level, you need to have a solid floppy disk driver written before you can seriously consider the file system itself. Assuming you're starting with a floppy disk, the basic functions needed are
  • engage drive x
  • seek to sector n on drive x
  • verify sector n on drive x as good
  • format sector n on drive x
  • read sector n on drive x
  • write sector n on drive x, and
  • reset the disk controller
This by itself is a major undertaking, but not an overwhelming one. Floppy disks and hard disks each have their own peculiarities; for example, in ever system I've seen, legacy DMA channel 2 is used the floppy controller to transfer sector data to memory (it is supposed to be possible to write PIO floppy drivers, but I've never seen one that worked), while hard disks require you to keep track of partitions as well as physical disks.

My suggestion is to start by writing some simple, self-booting programs (load them the same way as you do your existing kernel - or simply add them to the kernel itself, as a hack while developing the system) which act as minimal disk drivers, and test it out on scratch disks. Practice at reading and writing sectors, and perhaps write a simple sector dump/editor utility. When you are sure your code works correctly and safely, throw it away and write it over, as a driver for your kernel. Work on floppies first, and only when you have those down move on to hard disks, as the HDs are more complicated to work with.

For documentation on writing the drivers, IPHB has excellent coverage on programming the disk controllers, and various tutorials are available at OSRC and similar locations.

Once you have the disk drivers written (and when you are confident that, when it becomes necessary - and it will - you can rewrite them to fit your overall driver system), you need to use them to write the FS. However, you should first give some consideration to how your system is going to interface with it - and in particular how to make the disks look alike even when the use different physical file systems. This is what is under Linux is called the Virtual File System (I cannot recall the term used by Win2K offhand), and it is important in that it means that you aren't tied to a particular implementation. The usual abstraction under Unix and Windows is of a stream of bytes, though both also support 'block I/O' (basically, an abstraction of the sectors/clusters) as well. How this will work wil be up to you, but you should at least consider the issue before actually implement a working FS, or you could paint yourself into a corner.

continuing from the last post (was RE:reading a file?)

Posted: Mon Oct 21, 2002 12:31 pm
by Schol-R-LEA
With all that out of the way, you should be finally ready to consider the design and implementation of a file system ::). I strongly recommend that, even if you intend to design a Revolutionary Hypermedia Control System[sup]?[/sup], you would be well served by using a tried (or perhaps tired) and true FS to begin with; if nothing else, it will show you what you'll need to have in your own.

FAT12 is an obvious choice for what is presumably a floppy based system at this time. It is supported by every OS on the x86 platform and some which aren't, it is about as simple as file system could be, and despite Microsoft's notorious secrecy about some things, it is known inside and out. However, it doesn't work well with GRUB, and would require at the very least a rewrite of most other boot sectors, as well as very likely of major parts of the kernel itself. It makes certain assumptions about the disk layout - particularly of the first cylinder and especially the boot sector and the sectors immediately following it. If you are currently situating your kernel in sector 2, you'll need to move it.

If you are using GRUB, then you may want to consider ext2FS instead, which was until quite recently the default Linux FS and quite well supported by GRUB.

Either way, you will probably want to rework your kernel so that it is treated as a file by the FS, rather than as a fixed, special group of sectors outside of the FS proper. This will make it a lot easier to maintain in the future, especially as you start to do development work in the OS itself. This in turn means that you bootstrap will have to be able to read the file system's indices, or at least find the beginning of the data area (a little cheat used by the FAT12/FAT16 boot sectors to locate IO.SYS).

Mind you, this is all advice, and not written in stone; how you do it is your call. Also, I've intentionally keep things vague, as I don't know just what you've already done and how you want to proceed; more detailed advice can be given (up to a point, as I haven't written disk drivers mysefl yet) as needed. HTH. C&CW.