Page 1 of 1
How to implement filessystem?
Posted: Sat Jun 15, 2002 4:38 am
by amirsadig
Hi friends,
in order to load module or to execute programm I need to read the disk which include this files. Therefore I want to port fat12 to my kernel.
First I want to beginn by read fat system , if you know any example or good site which describe it, please notice me .
I have choose FAT, because it easy to implement.
this step i needed to implement the FAT system:
1- only support floppy disk
2- only reading
3- s?pport read / write to floppy
4- support hard disk
I need you help, as soon as possible.
Re:How to implement filessystem?
Posted: Sat Jun 15, 2002 1:52 pm
by anubis
hi
i am also trying to write my OS that uses FAT12.
though i havenot written the code to read the fat i found some good manuals at
www.nondot.org\sabre\os\files\FileSystems
also a good book(if u can lay your hands on)is
"IBM PC Assembly language and programming"-by Peter Abel(ISBN 81-203-1037-3)
mail me if you want some excerpts from the book
by the way spare me for my english ::)
Re:How to implement filessystem?
Posted: Sun Jun 16, 2002 3:15 pm
by amirsadig
Hi,
I begann to write a function, which read 1 floppy sector, as test, so that I become familier with assembly, because I have not programmed any hardware before.
in the main function look like:
extern int ReadSector(int d, int c, int h, int s, char *buffer);
int k_main(void)
{
unsigned char buff[255];
int d = 0, c= 0,h=0,s=1, status;
status = ReadSector(d,c,h,s,(char *)buff);
init_video();
kprintf("Hello from Amir Bukhari %s \n", "test"); // does work
kprintf("Hello from Amir Bukhari %s \n", buff);
while(1);
}
the function ReadSector is an assembly file :
[global ReadSector]
ReadSector: push bp
mov bp,sp
mov ah,2
call DiskServ
pop bp
ret
DiskServ: push ax
mov dl,[bp+4]
mov ax,[bp+6]
mov dh,[bp+8]
mov cl,[bp+10]
and cl,00111111b
les bx,[bp+12]
ror ah,1
ror ah,1
and ah,11000000b
mov ch,al
or cl,ah
pop ax
mov al,1
int 13
mov al,ah
xor ah,ah
ret
.................
before I write the function, I have test this ptogramm with hallo ..... and it work. After writing ReadSector
it doesn't work. I do not mean compiling but running.
The Computer after reading the bootsector, reboot very soon it's look like there is memory or register error - I do not know -
Please help me !!!!!
Re:How to implement filessystem?
Posted: Sun Jun 16, 2002 11:34 pm
by Schol-R-LEA
A few issues come to mind immediately.
First off, in order to make any firm conclusions about certain aspects of this, you need to let us know which compiler and assembler you're using, under what OS, and in what executable format. For now, I'll assume you are using DOS DJGPP/gcc and NASM under Windows, probably generating a Windows EXE, though I couldn't say for sure.
The reason that this is relevant is because gcc will only 32-bit protected mode code, which will not run in DOS without a DOS extender (Windows 'console' programs are something entirely different, and again do not run in DOS mode). Conversely, calling a real-mode function from a p-mode program without first switching to v86 mode will invariably cause a general protection fault. This includes DOS and BIOS interrupt calls; these should never be called from a p-mode program.
This is perhaps the most common error that new OS designers make, especially when mixing NASM assembly code and gcc C code, so it is nothing to feel too bad about. However, it does mean that you should look up more information about the different processor modes, and find out what the defaults of your development tools are.
The second issue I see is the name of the function in assembly. By convention, C functions names are prepended with an underscore in the object relocation tables, and the linker, when trying to combine the two object files, would look for a function named _ReadSector, not ReadSector. Again, this is a typical mistake, and an easy one to correct. I'm surprised that it did not cause a linker error, however.
Third - and again, this is assuming you are using gcc - you use the 16-bit BP base pointer, not the EBP, which is used by gcc. Thus, the offsets are going to be incorrect.
Finally, the INT call, unfortunately, will not work as written. First off, the interrupt for the disk services is 13 hex, which is 19 in decimal. Second, while it is not strictly necessary i this case, you should issue a disk rest command, (INT 13h, ah = 0) before calling the read sector function.
I am at a total loss as to why you do this,
mov cl,[bp+10]
and cl,00111111b
; ... a line of unrelated code here
ror ah,1
ror ah,1
and ah,11000000b
mov ch,al
or cl,ah
Perhaps if you explain why you used this approach, I could give more advice. I'm I correct in assuming that DiskServ is meant to work as a generic setup for all the INT 13h functions?
Just my two cents worth. If you are using a different set of tools than I supposed, then parts 1 and 3 can probably be ignored.
Re:How to implement filessystem?
Posted: Mon Jun 17, 2002 1:18 am
by amirsadig
thanks you for your reply,
as assembly I use NASM and gcc compiler for linux an my executable file is elf. as I said before I try to write an OS that means this programm run without dos or linux.
In NASM tutorial I have read that to let the linker find function for C I must make underline under the function, but when I try this the linker (ld) say "undefined reference to `ReadSector' ".
you said "I am at a total loss as to why you do this, ..."
you know that cylinder number will be save (10 bit) in CH and 2 bit in CL , all this code it is only a trick to save the cylinder parameter in ch and cl.
Note that this programm will be loaded from grub bootmannager and I have tested without these section which read sector and I think grub run in p-mode.
This is the first time i hear that bios service work only in real mode. I have said before I am a beginner in programming with assembly and hardware. how can I read sectors in p-mode? I think there is way with I/O port but I don't have written one like this.
The ld command I use:
ld -Ttext=0x100000 -e start -o kernel.bin kernel_start.o diskser.o kernel.c
diskser : is the assembly programm which read sector
kernel : is my simple kernel
kernel_start: this include the start entry which call k_main
Re:How to implement filessystem?
Posted: Mon Jun 17, 2002 12:28 pm
by f2
I have already implemented to read files off of a FAT FS (12 or 16) in my OS and if you would like to see the code (NASM), then just e-mail me and I will reply to the e-mail with the bit of code that reads the file and a brief explanation...
Re:How to implement filessystem?
Posted: Mon Jun 17, 2002 1:53 pm
by Poincaré
Could you send me the code too?
Re:How to implement filessystem?
Posted: Tue Jun 18, 2002 10:41 am
by amirsadig
hi Tommy,
Thank you for your help.
I am waiting .... ;D
have you written it only in Assembly?
you can email me at
[email protected]
Re:How to implement filessystem?
Posted: Tue Jun 18, 2002 11:07 am
by Schol-R-LEA
As I have mentioned elsewhere, the
OS Resources Center has material on interfacing hardware in either real mode or p-mode. It's a good place to start with most stages of OS design, in fact, though hardly exhaustive. Reading the source from other OSes is also helpful, though they are often of mixed quality or readability (the Linux sources are notoriously byzantine).
If you can, borrow or buy a copy of Tannebaum's Minix book, as the source code is written with clarity in mind; the PC editions of the Xinu books by Doug Comer are OK, but you need all three for a complete system IIRC.