Floppy reads zeros only?

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
User avatar
TyrelHaveman
Member
Member
Posts: 40
Joined: Thu Sep 20, 2007 11:20 pm
Location: Bellingham, WA
Contact:

Floppy reads zeros only?

Post by TyrelHaveman »

Hey all,
First of all, I have a multitasking IA-32 kernel set up and working alright. I'm starting to create drivers now. I'm starting with the floppy driver.

I have it all coded up, but it doesn't seem to be working right.

I'm using DMA... when I try to read a track, the region of physical memory where I expect the track's data to appear does get written -- but not with the data that's on that track on the disk (as far as I know). Instead every byte seems to be zeroed (they did not all contain zero before). Any ideas what could be causing that?

My guess it that the DMA interaction is working -- otherwise the data here wouldn't get written over at all. But, after I complete my track read, the top two bits of ST0 in the results phase are zero, seemingly indicating that there was no error. Yet I just get zeroes.

Ideas?

Thanks,
Tyrel

(Edit: Changed subject to "floppy" instead of "floopy" :) )
User avatar
TyrelHaveman
Member
Member
Posts: 40
Joined: Thu Sep 20, 2007 11:20 pm
Location: Bellingham, WA
Contact:

Re: Floppy reads zeros only?

Post by TyrelHaveman »

Additional details:
The previous was from running in VMware Server using the read-track command.
If I use the read-sector command with identical parameters in VMware Server, the zeros do not get written to memory -- it looks like nothing really happens at all, but I get success anyway.

If I use read-track, Bochs says [FDD ] io_write: 0x3f5: invalid floppy command 0x42
If I use read-sector, Bochs never sends me an interrupt from the FDD when the read is complete.

Does this help at all? It just confuses me even more.

Thanks again,
Tyrel
User avatar
cr2
Member
Member
Posts: 162
Joined: Fri Jun 27, 2008 8:05 pm
Location: ND, USA

Re: Floppy reads zeros only?

Post by cr2 »

are you using a real floppy or an image?
OS-LUX V0.0
Working on...
Memory management: the Pool
User avatar
TyrelHaveman
Member
Member
Posts: 40
Joined: Thu Sep 20, 2007 11:20 pm
Location: Bellingham, WA
Contact:

Re: Floppy reads zeros only?

Post by TyrelHaveman »

cr2 wrote:are you using a real floppy or an image?
I'm using an image. Are you suspecting that it may be reading a different sector than I'm trying to read? This doesn't appear to be the case based on the returns from the read commands, but is possible I suppose...? I dunno.
User avatar
kmtdk
Member
Member
Posts: 263
Joined: Sat May 17, 2008 4:05 am
Location: Cyperspace, Denmark
Contact:

Re: Floppy reads zeros only?

Post by kmtdk »

Well, the best thing would be to test it on a real floppy, so you can be sure about the error. Secound how have you set the dma ???


KMT dk
well, what to say, to much to do in too little space.
when it goes up hill, increase work, when it goes straight, test yourself but when going down, slow down.
User avatar
Stevo14
Member
Member
Posts: 179
Joined: Fri Mar 07, 2008 3:40 am
Location: Arad, Romania

Re: Floppy reads zeros only?

Post by Stevo14 »

It would help if we could see some code. Specifically the part where you send the read command. Also, if you haven't already, take a look at one of the floppy controller data sheets here.
User avatar
TyrelHaveman
Member
Member
Posts: 40
Joined: Thu Sep 20, 2007 11:20 pm
Location: Bellingham, WA
Contact:

Re: Floppy reads zeros only?

Post by TyrelHaveman »

Stevo14 wrote:It would help if we could see some code. Specifically the part where you send the read command. Also, if you haven't already, take a look at one of the floppy controller data sheets here.
Here is the floppy reading code. READ_TRACK is defined to 0x42, device is 0, DRIVE_BASE(device) is 0x3f0 (in this case). floppy_drives[device].cylinder is the last cylinder picked up from a sense interrupt or other command result (in this case, the seek), and is equal to zero. The floppy_info structure was read from the BIOS data at 0x000fefc7 in physical memory.

Code: Select all

	write_floppy_command(DRIVE_BASE(device), READ_TRACK);
	write_floppy_command(DRIVE_BASE(device), device);
	write_floppy_command(DRIVE_BASE(device), floppy_drives[device].cylinder);
	write_floppy_command(DRIVE_BASE(device), 0); // head
	write_floppy_command(DRIVE_BASE(device), 1); // sector
	write_floppy_command(DRIVE_BASE(device), floppy_info.bytes_per_sector);  // sector size = 128*2^size
	write_floppy_command(DRIVE_BASE(device), floppy_info.sectors_per_track); // last sector
	write_floppy_command(DRIVE_BASE(device), floppy_info.gap_length);
	write_floppy_command(DRIVE_BASE(device), floppy_info.data_length);

	wait_for_interrupt();

	wait_floppy_data(DRIVE_BASE(device));
	floppy_drives[device].st0 = inb(DRIVE_BASE(device) + DATA_REGISTER); // ST0
	inb(DRIVE_BASE(device) + DATA_REGISTER); // ST1
	inb(DRIVE_BASE(device) + DATA_REGISTER); // ST2
	floppy_drives[device].cylinder = inb(DRIVE_BASE(device) + DATA_REGISTER); // cylinder
	inb(DRIVE_BASE(device) + DATA_REGISTER); // head
	inb(DRIVE_BASE(device) + DATA_REGISTER); // sector
	inb(DRIVE_BASE(device) + DATA_REGISTER); // sector size
Note that I've tried the sector number being 0 and 1, and I get the same result.
kmtdk wrote:Well, the best thing would be to test it on a real floppy, so you can be sure about the error. Secound how have you set the dma ???
I have yet to locate an actual floppy disk in my house. I may have to go to my parents house to grab one from my younger days... :) But I intend to do that as soon as I can. I also only have one (of four) computer that actually has a floppy drive -- luckily it's the one I'm running VMware on.

As far the the DMA... In the floppy's initialization I have this:

Code: Select all

map_dma(2, 0x2400);
And at the beginning of the read function, I have this:

Code: Select all

prepare_dma(DMA_MODE_READ | DMA_MODE_AUTO | DMA_MODE_SINGLE, 2);
These are defined as follows. Helpful things for reading this code: dma_first_page is set to 16 (the 1 MB mark). This is where I begin my DMA stuff. I have 64 KB allocated for the first three usable channels and 128 KB for the other three usable channels. So here, when we're using channel 2, we should be getting page 17, which should be at 0x00110000 (physical).

Code: Select all

int map_dma(int channel_num, uint32 bytes)
{
	if (dma_channels[channel_num].mapped || !dma_channels[channel_num].valid)
	{
		return 0;
	}
	
	int controller = dma_channels[channel_num].controller;
	uint8 address = dma_channels[channel_num].address_reg;
	uint8 counter = dma_channels[channel_num].count_reg;
	uint8 page = dma_channels[channel_num].page_reg;
	uint8 relchan = channel_num;
	if (controller == 1)
	{
		relchan -= 4;
	}
	
	// Turn off the channel...
	outb(controller == 0 ? 0x0A : 0xD4, 0x4 + relchan);
	
	reset_flipflop();
	outb(address, 0); // address low byte
	outb(address, 0); // address high byte
	
	reset_flipflop();
	outb(counter, (bytes - 1) & 0xFF); // count low byte
	outb(counter, ((bytes - 1) & 0xFF00) >> 2); // count high byte
	
	outb(page, dma_channels[channel_num].page + dma_first_page);
	
	// Turn the channel back on...
	outb(controller == 0 ? 0x0A : 0xD4, relchan);
	
	return 1; // all good
}

int prepare_dma(int channel_num, uint8 modes)
{
	int controller = dma_channels[channel_num].controller;
	uint8 relchan = channel_num;
	if (controller == 1)
	{
		relchan -= 4;
	}
	outb(controller == 0 ? 0x0A : 0xD4, 0x4 + relchan);
	outb(controller == 0 ? 0x0B : 0xD6, modes | relchan);
	outb(controller == 0 ? 0x0A : 0xD4, relchan);
	return 1;
}
Let me know if there's anything else I can provide that would be helpful (other than ALL of the code... I don't really want to do that).

I will be trying a real floppy if I can and let you know how that goes.

Thanks guys,
Tyrel
User avatar
TyrelHaveman
Member
Member
Posts: 40
Joined: Thu Sep 20, 2007 11:20 pm
Location: Bellingham, WA
Contact:

Re: Floppy reads zeros only?

Post by TyrelHaveman »

Alright, I tried with a real floppy drive as promised. I have to say, I am flabbergasted. It worked. My track read on the real floppy drive read the real track and it got put into memory right where I wanted it.

So now I have three different results for the read-track command: VMware with floppy images, gets zeroes. VMware with real floppy, gets the correct data. Bochs with floppy image, doesn't seem to understand the read-track command (0x42).

Ideas? How can I get all of these to work properly, not just the real drive?

Thanks,
Tyrel
User avatar
TyrelHaveman
Member
Member
Posts: 40
Joined: Thu Sep 20, 2007 11:20 pm
Location: Bellingham, WA
Contact:

Re: Floppy reads zeros only?

Post by TyrelHaveman »

Anyone? :(
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: Floppy reads zeros only?

Post by Combuster »

Broken timing?
"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 ]
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Re: Floppy reads zeros only?

Post by Dex »

Do you use a BPB as part of your code ?, if so check that they are not filled with 0 under emulators, instead of the BPB.
Also if you have a "LBAcHs" function in your code, some emulators need you to zero dx here:

Code: Select all

 ;----------------------------------------------------;
 ; LBAcHs.                                            ;
 ;----------------------------------------------------;
LBAcHs:
	xor   dx,dx
User avatar
TyrelHaveman
Member
Member
Posts: 40
Joined: Thu Sep 20, 2007 11:20 pm
Location: Bellingham, WA
Contact:

Re: Floppy reads zeros only?

Post by TyrelHaveman »

Dex wrote:Do you use a BPB as part of your code ?, if so check that they are not filled with 0 under emulators, instead of the BPB.
Also if you have a "LBAcHs" function in your code, some emulators need you to zero dx here:

Code: Select all

 ;----------------------------------------------------;
 ; LBAcHs.                                            ;
 ;----------------------------------------------------;
LBAcHs:
	xor   dx,dx
I apologize for being significantly confused by your reply... I'm not sure what a BPB is, so I don't know if I have one as part of my code, but I'm guessing not because I should know if I did.. Also, I don't have any sort of LBAcHs code... Even if I did, I'm not sure why VMware would care what I do with DX or any other register, for that matter, inside of my functions?
User avatar
TyrelHaveman
Member
Member
Posts: 40
Joined: Thu Sep 20, 2007 11:20 pm
Location: Bellingham, WA
Contact:

Re: Floppy reads zeros only?

Post by TyrelHaveman »

Combuster wrote:Broken timing?
I'm just using the timing values provided to me by the CMOS, which I presumed it would provide to be correct. I would think that the emulated floppy disk would run faster than the real floppy disk, anyway... and, it's sending me an interrupt, which I would assume would mean it is done (it says it is...).
User avatar
mystran
Member
Member
Posts: 670
Joined: Thu Mar 08, 2007 11:08 am

Re: Floppy reads zeros only?

Post by mystran »

Maybe this would prove helpful: http://forum.osdev.org/viewtopic.php?t=13538

It explains how to read 2 tracks at a time (well, one track with both heads to be exact). It's known to work on both real hardware and emulators, attempts to explain why things are done the way they are done, and is somewhat simplified and extensively commented for easier understanding. It also has extra information in the (forum) comments.

IIRC Bochs doesn't support READ_TRACK but then again READ_TRACK can't do multi-tracking anyway...
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
User avatar
TyrelHaveman
Member
Member
Posts: 40
Joined: Thu Sep 20, 2007 11:20 pm
Location: Bellingham, WA
Contact:

Re: Floppy reads zeros only?

Post by TyrelHaveman »

mystran wrote:Maybe this would prove helpful: http://forum.osdev.org/viewtopic.php?t=13538

It explains how to read 2 tracks at a time (well, one track with both heads to be exact). It's known to work on both real hardware and emulators, attempts to explain why things are done the way they are done, and is somewhat simplified and extensively commented for easier understanding. It also has extra information in the (forum) comments.

IIRC Bochs doesn't support READ_TRACK but then again READ_TRACK can't do multi-tracking anyway...
I just copied all of the code from that tutorial to try it out. Other than formatting changes and slight modifications to get it to compile and use my existing DMA code, I've got the same code... and it's having a problem.

VMware is crashing -- the log says "NOT IMPLEMENTED" in their floppy.c file. Obviously their floppy controller is implemented, and something is wrong in my code. But I don't know what it could be. I've got the DMA set up just like I used to (which worked), the motor is spinning, and the seek completed successfully. I thought maybe I had the direction (READ vs WRITE) wrong for DMA, so I changed it -- which only slightly helped the situation (no crash, no errors, but no data copied from the floppy to the buffer).

This is the code that it's crashing at:

Code: Select all

		floppy_write_cmd(base, cmd);  // set above for current direction
		floppy_write_cmd(base, 0);    // 0:0:0:0:0:HD:US1:US0 = head and drive
		floppy_write_cmd(base, cyl);  // cylinder
		floppy_write_cmd(base, 0);    // first head (should match with above)
		floppy_write_cmd(base, 1);    // first sector, strangely counts from 1
		floppy_write_cmd(base, 2);    // bytes/sector, 128*2^x (x=2 -> 512)
		floppy_write_cmd(base, 18);   // number of tracks to operate on
		floppy_write_cmd(base, 0x1b); // GAP3 length, 27 is default for 3.5"
		floppy_write_cmd(base, 0xff); // data length (0xff if B/S != 0)
(This is exactly the code that's in the tutorial)
Note that I checked 'cyl' by printing it out, and it's set to 0 like I intend. cmd is set above to 0xC6.

Any ideas? Your help is greatly appreciated!

Thanks again.
Post Reply