c,filesystem..

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
frank

c,filesystem..

Post by frank »

Hi,
I've got a few "problems".

Code: Select all


asm("jmp start");
 char c=5;

void start()
{
 while (1)
  {
   }
}

this code creates a big binary, 'cause I set c to 5.
gcc adds a lot of "add [eax],al" commands to my code instead of using the code segment, how do I change that?
(a c code without char c=5; function works fine...)

I'm using this compile line:

Code: Select all

gcc -o4 -c kern.c
ld -o kern -Ttext 0x1000 -Tdata 0x1200 -e 0x0 -N kern.o
objcopy  -R .note -R .comment -S -O binary kern kern.bin

Also,
how do I create a file system?
for example:

I wrote an header which can write and read sectors of the disk,
so how do I open file?...
the files start at the end of the disk,
if you read a file, how does it know which disk it should use,
and how does it get the sector data from the disk? (the filesytem)
Tim

Re:c,filesystem..

Post by Tim »

frank wrote:gcc adds a lot of "add [eax],al" commands to my code instead of using the code segment, how do I change that?
(a c code without char c=5; function works fine...)
Note that "add [eax], al" equates to a string of zero bytes. Which is normal: you're going to have 05 followed by (padding-1) bytes of zeroes, where padding is the section alignment. You can't avoid that. Padding the data section with zeroes is completely normal.
I wrote an header which can write and read sectors of the disk,
so how do I open file?...
the files start at the end of the disk,
if you read a file, how does it know which disk it should use,
and how does it get the sector data from the disk? (the filesytem)
A disk contains some raw data in some format: think of it as an array of, say, 4 gigabytes. You've got to translate that data somehow, which is where the file system comes in. First read a boot on OS development to get the general idea (e.g. OS: D&I by Tanenbaum), then read some real file system specs (e.g. FAT or ext2 specs on http://www.nondot.org/sabre/os/).
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:c,filesystem..

Post by Pype.Clicker »

A very simple filesystem you can imagine : you have at a well-known place a sector containing the directory of your disk : n entries having a symbolic name, a first sector, a sector count and a byte count.

You need to lookup the directory for the file's name (open operation) and usually store the lookup result (size & first sector) in a structure that you will use as file handle.
Then if you want to access datas at ith byte of your file, you just read first_sector + (i mod sector_size) and get the wanted byte out of it (well, a small cache of 1 or 2 sectors per file handle will quickly become your friend ;)

In this scheme, it's quite hard to extend files or to remove them because you have nothing to keep track of freed sectors (allocation must be done top-down or bottom-up so that you're sure to have contiguous sectors for your file) but this is no problem for a boot partition that is rather static...
Post Reply