Filesystem read/write freezes+ recommended resources

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
eax
Member
Member
Posts: 57
Joined: Mon Jun 23, 2008 6:45 am

Filesystem read/write freezes+ recommended resources

Post by eax »

Hi I am currently looking into reading/writing sectors to my floppy disk, the thing is I have never done it before , so I went to osdev file tutorial and tried to adapt my code. Theres alot Im unsure of for what values should be in certain variables or what types to put.

The code I tried to adapt is below , basically when I press the number one on the KB it reads a sector and when I press 2 its supposed to write a sector, however it freezes, also should the drive variable contain a 0 for a floppy? and what should the temp var contain? (How do I get it)

Code: Select all

#include <system.h>

#define NUML 0x2 
#define CAPSL 0x4

/* KBDUS means US Keyboard Layout. This is a scancode table

*  used to layout a standard US keyboard. I have left some

*  comments in to give you an idea of what key is what, even

*  though I set it's array index to 0. You can change that to

*  whatever you want using a macro, if you wish! */

unsigned char kbdus[128] =

{

    0,  27, '1', '2', '3', '4', '5', '6', '7', '8',	/* 9 */

  '9', '0', '-', '=', '\b',	/* Backspace */

  '\t',			/* Tab */

  'q', 'w', 'e', 'r',	/* 19 */

  't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',		/* Enter key */

    0,			/* 29   - Control */

  'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',	/* 39 */

 '\'', '`',   0,		/* Left shift */

 '\\', 'z', 'x', 'c', 'v', 'b', 'n',			/* 49 */

  'm', ',', '.', '/',   0,					/* Right shift */

  '*',

    0,	/* Alt */

  ' ',	/* Space bar */

    CAPSL,	/* Caps lock */

    0,	/* 59 - F1 key ... > */

    0,   0,   0,   0,   0,   0,   0,   0,

    0,	/* < ... F10 */

    NUML,	/* 69 - Num lock*/

    0,	/* Scroll Lock */

    0,	/* Home key */

    0,	/* Up Arrow */

    0,	/* Page Up */

  '-',

    0,	/* Left Arrow */

    0,

    0,	/* Right Arrow */

  '+',

    0,	/* 79 - End key*/

    0,	/* Down Arrow */

    0,	/* Page Down */

    0,	/* Insert Key */

    0,	/* Delete Key */

    0,   0,   0,

    0,	/* F11 Key */

    0,	/* F12 Key */

    0,	/* All other keys are undefined */

};

unsigned char vardbus[0] = {'*'};
unsigned char buffer[];


volatile unsigned short flags[0]={0};




/* Handles the keyboard interrupt */

void keyboard_handler(struct regs *r)

{
criticalregion(CSTOP);


    volatile unsigned char scancode,al,handled,cycle;
    volatile unsigned char numlock,capslock,scrolllock;
    
    if(flags[0] == 0x0) {
puts("Back to zero");    
}





    /* Read from the keyboard's data buffer */

    scancode = inportb(0x60);
   



    /* If the top bit of the byte we read from the keyboard is

    *  set, that means that a key has just been released */

    if (scancode & 0x80)

    {

        /* You can use this one to see if the user released the

        *  shift, alt, or control keys... */

    }

    else

    {

        /* Here, a key was just pressed. Please note that if you

        *  hold a key down, you will get repeated key press

        *  interrupts. */



        /* Just to show you how this works, we simply translate

        *  the keyboard scancode into an ASCII value, and then

        *  display it to the screen. You can get creative and

        *  use some flags to see if a shift is pressed and use a

        *  different layout, or you can add another 128 entries

        *  to the above layout to correspond to 'shift' being

        *  held. If shift is held using the larger lookup table,

        *  you would add 128 to the scancode when you look for it */

     			if(kbdus[scancode] == '1')
			{
			unsigned char addr = 0x0;
			unsigned char drive = 0;
			unsigned char idx = 0;
			unsigned char tmpword = 0;
			//read sector LBA28
			outportb(0x1F1, 0x00);		
			outportb(0x1F2, 0x01);
			outportb(0x1F3, (unsigned char)addr);
			outportb(0x1F4, (unsigned char)addr >> 8);
			outportb(0x1F5, (unsigned char)addr >> 16);
			outportb(0x1F6, 0xE0 | (drive << 4) | ((addr >> 24) & 0x0F));
			outportb(0x1F7, 0x20);
			
			while (!(inportb(0x1F7) & 0x08)) {}

			//And then read/write your data from/to port 0x1F0:

			// for read:

				for (idx = 0; idx < 256; idx++)

				{

				  tmpword = inw(0x1F0);

				buffer[idx * 2] = (unsigned char)tmpword;

				buffer[idx * 2 + 1] = (unsigned char)(tmpword >> 8);

				}
			}

			if(kbdus[scancode] == '2')
			{
				//check drive
				outportb(0x1F6, 0xA0); // use 0xB0 instead of 0xA0 to test the second drive on the controller

				timer_wait(1); 

				unsigned short tmpword = inportb(0x1F7); // read the status port

				if (tmpword & 0x40) // see if the busy bit is set

				{
				puts("Primary master exists\n");
				}
			}

			if(kbdus[scancode] == 0x4)  //capslock pressed
			{
				
				puts("in caps \n");
				
				unsigned char caps = IsKeyOnOff(CAPSL);
				  
				ibfull(); //write
				obfull(); //read
				ibfull(); //write
				outportb(0x60, flags[0]);
				puts("CAPS SWITCH now! \n");
				
			}

//*****************NUML SC
			if(kbdus[scancode] == 0x2)  //NUMlock pressed
			{
				puts("in num \n");
				
				unsigned char caps = IsKeyOnOff(NUML);
				 
				ibfull(); //write
				obfull(); //read
				ibfull(); //write
				outportb(0x60, flags[0]);
				puts("NUM SWITCHED now! \n");
				 

			}
//*****************NUML END

	
	}
putch(kbdus[scancode]);
puts("N \n"); //this is rotated just to make sure my make file is behaving


    }



unsigned char ibfull()
{
 while((inportb(0x64) & 2) != 0);





}
unsigned char obfull()
{
unsigned char al,info;
	outportb (0x60, 0xED); 
	//rdy to write
	al = 0;
	while(al != 0xFA){
	al= inportb (0x60);
	info = al;
	
	 if ((inportb (0x64) & 1) != 0){
		puts("READING DATA \n");
		 // if so, read it -- hopefully it's the ACK!
		 al= inportb (0x60);
		if(al == 0xFF) {
		puts("keyboard error \n");		
		}
		if(al == 0x00) {
		puts("Keyboard Error! To MANY KEYS PRESSED AT ONCE!\n");		
		}
		if(al == 0xEE) {
		puts("Result of an echo command \n");		
		}
		if(al == 0xFC) {
		puts("BAT Failed, go to petshop for new BAT N.B these errors are less meaningful than my comments! \n");		
		}
		if(al == 0xFE){
		puts("Resend data because Im a KB that cant handle data properly! \n");		
		}
		
		 
		   
	} 
		
	}
return info;
}

unsigned char IsKeyOnOff(unsigned char keyTcheckONOFF) {
unsigned char maskCL,maskNL,keep;
int index;
//locflags = 0x00;
maskCL = 0x04;
maskNL = 0x02;


//locflags = flags[0];
keep = 0;

 
	if(keyTcheckONOFF == CAPSL)
	{
		//is capslock so check if on - if so turn off 
		if((flags[0] >> 2) == 0x1) 
		{
		//comes in on
		//turn off
		flags[0] = flags[0] ^ 0x4;
		if((flags[0] >> 1) == 2) 
		{
		puts("flags IS TWOafter caps turned ON\n");
		}
			if(flags[0] == 0x1)
			{
			puts("flags is 1\n");
			}
			if(flags[0] == 0x2)
			{
			puts("flags is 2\n");
			}
			if(flags[0] == 0x4)
			{
			puts("flags is 4\n");
			}
			if(flags[0] == 0x6)
			{
			puts("flags is 6\n");
			}
			if(flags[0] > 0x6)
			{
			puts("Greater than SIX \n");
			}
			if(flags[0] < 0x0)
			{
			puts("LESS than ZERO \n");
			}
			if(flags[0] > 0x0)
			{
			puts("GREAT than ZERO \n");
			}
			if(flags[0] == 0x0)
			{
			puts("EQUAL ZERO \n");
			}
		puts("CAPSOFFFFF \n");

		return 0;
		}

		//is capslock so check if off - if so turn on
		if((flags[0] >> 2)  == 0x0 ) 
		{
			//comes in off
			flags[0] = (flags[0] | 0x4);
			//flags[0] = locflags;
			if(flags[0] == 0x1)
			{
			puts("flags is 1\n");
			}
			if(flags[0] == 0x2)
			{
			puts("flags is 2\n");
			}
			if(flags[0] == 0x4)
			{
			puts("flags is 4\n");
			}
			if(flags[0] == 0x6)
			{
			puts("flags is 6\n");
			}
			puts("Caps gets turned ON flags set to XARGHH\n");
			return 0;
		}

	}
//C   N   S
//4   2   1
//1   0   0                CON
//shr 1

//1   1   0
//1   1   0         CON,NON

//***************NUML
	if(keyTcheckONOFF == NUML)
	{
	puts("YesNUM but need more \n");
		unsigned char tempnl = flags[0];
		//is NUMlock so check if on - if so turn off 
		if((flags[0] >> 1)  == 0x1) 
		{
		flags[0] = (flags[0] ^ 0x2);
		puts("NUML Hopefully ObFFFffffb \n");
		//flags[0] = locflags;
			if(flags[0] == 0x1)
			{
			puts("flags is 1\n");
			}
			if(flags[0] == 0x2)
			{
			puts("flags is 2\n");
			}
			if(flags[0] == 0x4)
			{
			puts("flags is 4\n");
			}
			if(flags[0] == 0x6)
			{
			puts("flags is 6\n");
			}
			if(flags[0] < 0x0)
			{
			puts("LESS than ZERONMAO \n");
			}
			if(flags[0] > 0x0)
			{
			puts("GREAT than ZEROAO \n");
			}
			if(flags[0] == 0x0)
			{
			puts("EQUAL ZEROAO \n");
			}
		return 0; 
		}

		if((flags[0] >> 1) == 0x0) 
		{
		flags[0] = (flags[0] | 0x2);
		puts("NUML gets turned ONNNNN\n");
		//flags[0] = locflags;
			if(flags[0] == 0x1)
			{
			puts("flags is 1\n");
			}
			if(flags[0] == 0x2)
			{
			puts("flags is 2\n");
			}
			if(flags[0] == 0x4)
			{
			puts("flags is 4\n");
			}
			if(flags[0] == 0x6)
			{
			puts("flags is 6\n");
			}
		return 0;
		}
		//100
		//010 = 2
		if((flags[0] >> 1) == 0x2)
		{ //numlock is off to come on when caps is on
		//4 or 2
		//110
		//010
		//110
		flags[0] = (flags[0] | 0x2);
		puts("NUM turned ON \n");
		return 0;
                }
	
		if(flags[0] >> 1 == 0x3) 
		{
		flags[0] = (flags[0] ^ 0x2);
		puts("Num turned OFF \n");
		return 0;
		
		}
		//
		
		
		if((flags[0] >> 1)  < 0) 
		{
		puts("flags less than zero \n");
		}
		if((flags[0] >> 1)  > 0) 
		{
		puts("flags greater than zero \n");
		}
		if((flags[0] >> 1)   == 2) 
		{
		puts("flags IS TWOaa\n");
		}
	}
//**************NUML END
if(keyTcheckONOFF != NUML){
puts("NotEQUAL NUML \n");
}
if(keyTcheckONOFF != CAPSL){
puts("NotEQUAL CAPSL \n");
}

if(keyTcheckONOFF == CAPSL){
puts("EQUAL CAPSL but problem \n");

}

if(keyTcheckONOFF == NUML){
puts("EQUAL NUML but problem \n");

}

puts("problem if gets here \n");

}



/* Installs the keyboard handler into IRQ1 */

void keyboard_install()

{

    irq_install_handler(1, keyboard_handler);
    

}

void criticalregion(unsigned char order)
{
if(order == CSTART)
{
__asm__ __volatile__ ("sti");

}else if(order == CSTOP)
{
__asm__ __volatile__ ("cli");
}else if(order != CSTART | CSTOP)
{
puts("Incorrect Parameter! to critical region function \n");
}

}

void testbwise()
{

unsigned char ctest = 0;
puts("lets try BW test \n");
ctest = 0x04;
if((ctest>> 2) == 1)
{
puts("ctest value initially was 4 but move right 2 equ 1\n");
ctest++;
if(ctest == 5) {
puts("shifting in if doesnt affect value permenant \n");

}
ctest = ctest >> 1;

if(ctest == 2)
{
puts("value five shifted 1 is two WAYA\n");
}
}
 
//now for timer.c
void timer_wait(int ticks)

{

    //is called for 1 tick to allow for timeout
    unsigned long eticks;



    eticks = timer_ticks + ticks;

    while(timer_ticks < eticks);

}

}
//if anyone can recommend further reading please say or explain what the variables are which are relevant the floppy drive. BTW this is just a mini exercise to get to know a bit then move onto memory management
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Re: Filesystem read/write freezes+ recommended resources

Post by suthers »

When you say it 'freezes' do you mean it goes into an infinite loop?
And do you know approximately where the error occurs?
(Use the bochs debugger to dissasemble the current instructions then step through the loop, that should tell you where you are...)
Jules
eax
Member
Member
Posts: 57
Joined: Mon Jun 23, 2008 6:45 am

Re: Filesystem read/write freezes+ recommended resources

Post by eax »

Hey jules no error occurs, but I THINK I may know what it is.I disabled interrupts right throughout the keyboard file when I was testing some things yesterday so Im going to only disable ints when reading the input buffer then re-enable. You see my call to my wait function relies on the timer which stops for x amount of ticks and with me having ints disabled well Im thinking that my wait function goes in an infinite loop?

edit :

yeah looks like it is actually because my wait function never comes out the loop, Ok sorted the timer function now when I press 2 I get a message "primary master exists" next I press 1 to read a sector of disk and it hangs in the for loop

edit:

Ok changed idx from an unsigned char to an unsigned int and wham both parts of code run with no errors but I would still like someway to prove its actually read a sector and some document with similar code which explains all the ports. Also I declare my buffer like

unsigned char [] buffer; //Is this OK?

Also whats the drive numbers represented in a way thats compatible with relevant ports?i,e is it simply 0,1 and 2? If so which number represents which drive. Another thing is this variable addy and tmpword? I just set addy to 0x0 but Im not sure what I should put in here? would 0x0 be sector 1 and 512 dec be sector 2? Im just guessing here though. Also whats tmpword reading in and what data is getting put in the buffer array? To me it looks like its reading in a sector every 2 bytes or something?(hmm actually if I have changed idx to an int that means its reading in 64bits? is that wrong.)

I haven't done file manipulation at this level and out of the code submitted here those are the aspects I would like to know more about, you see I want to read some sectors of a drive and then write some sectors to somewhere where I can check it all happened OK

I have also been reading very seriously that indispensable hardware book and Im on chapter 6 now but Im shattered now lol , I think it is a very good book thus far and Im just about to hit the chapters that start explaining paging in some more detail. (Just I remember you were asking about that book but give me more time and I will be able to let you know more about it)

Im actually away for a break as Im about falling asleep here lol but any information you can give me or refer me to that will help me handle what starts as extremely basic file io in protected mode would be helpful and specifically what those parts of the code are doing that I don't understand. When I have had a sleep I think I am going to start using the brokenthorn site again and see if there anything on there regarding file IO without real mode ints using IN and outs with hopefully a good explanation of ports.

Ok sleepy time :) hehe Cheers for reply :)

btw heres the tutorial code I tried to port into my code as a test
http://www.osdever.net/tutorials/lba.php

^^ LBA HDD Access via PIO

I actually want to read of the hdd and write back to the FDD , I like to see things work in a way that 100% proves code is working so I had a perhaps risky idea of adopting the code to write a sector , which is also on the site. What I would do is read the bootsector from my floppy then write it back to floppy, if the floppy didnt boot I know my code inst working. Either way I want to be sure I understand exactly what all the read code variables are doing, how they relate to ports before making guesses and perhaps nuking a physical HDD instead of writing to floppy, I run through vmware emulator so if the code behaves as it does on real hardware then maybe it wouldn't matter but Im unsure :)

and heres some inline assembly in main that creates a compatible C inw function to make it work in c

Code: Select all

unsigned char inw (int _port) {
unsigned char data,rv;
   __asm__ __volatile__ ("inb %w1,%0" : "=a" (rv) : "d" (_port));
return rv;
}
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Re: Filesystem read/write freezes+ recommended resources

Post by suthers »

using unsigned char [] buffer, shouldn't work, firstly on gcc at least you have to put the [] after the declaration...
and secondly, you cannot define a buffer with no size (well you used to I think on older compilers....) or infinite size, I think gcc assumes that the array size is 1 i you do that... as for the problem with understanding what's what, somebody else will have to do that, I should have done my floppy driver about a week and a half ago now, but problems with making my cross compiler have slowed me down... (still working on making it work... :cry: )
Jules
eax
Member
Member
Posts: 57
Joined: Mon Jun 23, 2008 6:45 am

Re: Filesystem read/write freezes+ recommended resources

Post by eax »

Sorry was half asleep earlier :) I actually set up buffer like

unsigned char buffer[];

I figured that the size wouldnt necessary be known at runtime but in hindsight that seems kinda silly for a buffer I guess :) (Lack of sleep I guess!) btw I do get a warning about an array size assumed as element of one so I guess I will need to put a size on it. Unfortunatly I kinda need the whys as well as just the code itself so Im going to check the brokenthorn site. Hopefully the indispensable hardware book will have some info on it but Im going through it from cover to cover atm. Also the code does compile and execute, what its doing in reality though is kinda hard for me to know at this time. Theres actually code to write a sector as well but I think that would be utterly stupid for me to even implement that without knowing exactly what data relates to what drive in the drive variable as well as the other variables I mentioned.(Envisage no sane partition on my HDD or something lol)

Anyway Im away to see what I can find about ports again. Maybe an idea for the future for OSDev to get some articles on port manipulation in real terms i.e common code example coupled with explanation.

btw your cross compiler was giving an error about not finding CC , CC should be set with an export command to your system C compiler should it not? so in my case /usr/local/bin/gcc as an example? This maybe has nothing to do with it? and I may be wrong as I was lucky enough that my cross compiler for my situation just worked. (I know how frustrating it can be when you do google and still get no joy so I hope someone helps you with that who has experience with the problem you are having specific to your situation)
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Filesystem read/write freezes+ recommended resources

Post by Combuster »

Hmm... Why do I see harddisk code while you are trying to access the floppy drive? :?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply