Page 1 of 2

Need help to understand Floppy Disk Drive program..

Posted: Mon Mar 14, 2011 5:57 am
by osdevkid
Dear All,

I have attached the Linux source code "bootsect.s", in this file the routine "read_it", it loads the "system image" to the address "0x10000".

The routine "read_it" does the below:
1. First start reading the sectors from 5th sector of head=0, track/cylinder=0, in it's first iteration.
2. It compares with the total number of sectors per track and the number of sectors transferred to memory. if there are not equal than adjust the sector start offset "sread" and destination address ES:BX and reads again.
3. If all sectors in a track are readed, then it is going to read the next head (= 1)/surface of the disk. and completes the task.

The above was my understanding. Now my doubts are:
1. Why this routine not increments the track value for head 0 ?, it increments track value only for head 1.
2. At line number 190, it adjust the destination ES:[BX] value by adding CX(no. of bytes transferred), and it cheks for carry, if CF=1, then it also adjust the base value ES and reset BX value to 0 why ? for example, [BX=0xFFFE] + [CX=0x08] = 0x10006, the value 0x0006 to be updated in BX is it? but it is set it as zero. why?

Re: Need help to understand Floppy Disk Drive program..

Posted: Mon Mar 14, 2011 6:01 am
by Combuster
Do you know what CF=1 (carry set) after a bios call means?

Re: Need help to understand Floppy Disk Drive program..

Posted: Mon Mar 14, 2011 6:04 am
by osdevkid
yes I know, if CF=1, then it is disk read error for BIOS int 0x13.

Let me know, how you want to relate this with my doubts

Are you really meaning BIOS int 0x13 or "how ADD instruction affects Carry Flag".

Re: Need help to understand Floppy Disk Drive program..

Posted: Mon Mar 14, 2011 6:20 am
by Combuster
The point is, what should be the reaction to a read error? What should be the reaction if there was no read error? Can you explain those operations in terms of the listed code?

On another note, "academic thinking" is part of the required knowledge - and especially so for reverse engineering work. I have seen your previous thread and I'm hoping you're not trying to waste a time by getting a detailed tour of linux internals? Write your own bootloader first so you know what tricks are there to perform, then comparing other code will make much more sense - I don't want to spend the time to do your reading for you.

Re: Need help to understand Floppy Disk Drive program..

Posted: Mon Mar 14, 2011 6:48 am
by osdevkid
Dear combuster,
what should be the reaction to a read error? What should be the reaction if there was no read error? Can you explain those operations in terms of the listed code?
In case of read error, it calls the routine "bad_rt" to reset the disk drive and control goes back to routine "read_track" the same thing repeats untill the disk read become success.

In case of read success, it compares the no. of sectors transferred and the total no. of sectors/per track,

=> if they are not equal then, it updates the "sread" sector start value and BX destinatio offset addr and goes back to read.

=> If they are equal then, it updates the head value to 1 and start reading the next surface of the disk.

My actual doubt is "why it is skipping other tracks/cylinder present on head - 0, without reading them, it goes to read head-1."
I don't want to spend the time to do your reading for you
Yes, you are right. however, this linux source code version is 0.11, it is very simple and basic, for a student like me, it will be a good reference to write my own bootloader.

Re: Need help to understand Floppy Disk Drive program..

Posted: Mon Mar 14, 2011 8:38 am
by bewing
Combuster, this is a hardware question in disguise -- not a software question, so your response is unhelpful.
osdevkid wrote: My actual doubt is "why it is skipping other tracks/cylinder present on head - 0, without reading them, it goes to read head-1."
I haven't bothered reading the code (reading linux source is a painful thing to do), but the likely answer is: the Int0x13 call is attempting to read one entire track at a time.
All floppy drives have 2 heads, and therefore there are 2 tracks per cylinder -- because each head creates one track. So, the code tries to read one complete track. If it succeeds, then it does not have any more sectors to read on this track -- the entire track was successfully read into memory. So it switches to the next head to read the next track. There are no other tracks for the head. Each head only creates one. Then the entire head assembly gets moved to a new cylinder, and then each head can be used to create (or read) one more track.

It is very hard for the drive to move the entire head assembly. So you do not want to change cylinders until you are forced to. So, you always read or write sectors first. Then you change heads, in order to read/write the sectors of the other track in the cylinder. And only last do you change cylinders.

As far as adjusting pointers goes, each sector that is successfully read is 512 bytes, or 0x200. So, if you understand how Real Mode segments work, you need to increment either the segment by 0x20, or the "offset" by 0x200 for each sector read. It is usually easiest just to increment the segment, and leave the offset set to 0.

Re: Need help to understand Floppy Disk Drive program..

Posted: Mon Mar 14, 2011 11:08 am
by Brendan
Hi,
bewing wrote:As far as adjusting pointers goes, each sector that is successfully read is 512 bytes, or 0x200. So, if you understand how Real Mode segments work, you need to increment either the segment by 0x20, or the "offset" by 0x200 for each sector read. It is usually easiest just to increment the segment, and leave the offset set to 0.
The actual code looks wrong/broken to me (and not just because of the lack of useful comments in the original):

Code: Select all

	add bx,cx                ;bx = old offset + number of bytes read from disk
	jnc rp_read              ;Only update ES if BX overflowed
	mov ax,es                ;ax = segment
	add ax,#0x1000           ;ax = segment + 0x1000 (where 0x1000 is the adjustment needed when BX overflows)
	mov es,ax                ;Set segment
	xor bx,bx                ;THIS IS WRONG AND SHOULD BE REMOVED!
	jmp rp_read

Cheers,

Brendan

Re: Need help to understand Floppy Disk Drive program..

Posted: Mon Mar 14, 2011 11:12 am
by osdevkid
Dear bewing,

I think, I misunderstood the floppy drive read concept with 0x13.

You mean,
The Floppy has only one track per surface/head, is it?
The BIOS interrupt 0x13 reads the all the tracks of the one surface/head at a time, is it?
The sector and track number we are specifying for 0x13 interrupt are starting offset values, is it?

So, in which case, we required to increment the Track number and read/write the floppy?

Re: Need help to understand Floppy Disk Drive program..

Posted: Mon Mar 14, 2011 11:18 am
by osdevkid
Dear Brendan,
The actual code looks wrong/broken to me (and not just because of the lack of useful comments in the original):
Your statement may be correct, however, for your information, it is a working code, and it was written by Linus Torvalds.

Re: Need help to understand Floppy Disk Drive program..

Posted: Mon Mar 14, 2011 11:38 am
by Brendan
Hi,
osdevkid wrote:Dear Brendan,
The actual code looks wrong/broken to me (and not just because of the lack of useful comments in the original):
Your statement may be correct, however, for your information, it is a working code, and it was written by Linus Torvalds.
Working code? I doubt it - it looks like a piece of crap to me.

Wikipedia says: "In 1991 while attending the University of Helsinki, Torvalds, curious about the operating systems [19] and frustrated by the licensing of MINIX limiting it to educational use only (which prevented any commercial use), began to work on his own operating system which eventually became the Linux kernel."

The copyright at the top of that file says: "(C) 1991 Linus Torvalds"

This is probably extremely early code, possibly from the same year Linus started learning (and possibly written when Linus knew less than most of the beginners on the OSdev forums).


Cheers,

Brendan

Re: Need help to understand Floppy Disk Drive program..

Posted: Mon Mar 14, 2011 1:48 pm
by bewing
osdevkid wrote: The Floppy has only one track per surface/head, is it?
Correct.
The BIOS interrupt 0x13 reads the all the tracks of the one surface/head at a time, is it?
You tell it the number of sectors to read. You have to figure out the number of sectors remaining in the track. Once you have done that, yes, it is usually best to read one entire track at a time, by reading the proper number of sectors.
The sector and track number we are specifying for 0x13 interrupt are starting offset values, is it?
Sort of. The sector number is an offset + 1. The "track" number is called a head number, and is either 0 or 1 on a floppy.
So, in which case, we required to increment the Track number and read/write the floppy?
Yes. Read the track. Either increment the head number from 0 to 1 (if we were reading head 0), or (if we were reading head 1) change the head number back to 0, and increment the cylinder by 1.

... And Linus is not god. Not even a particularly great programmer. You may notice that all his code in Linux has been replaced at this point? There is a reason for that. And ASM programming is a much different skill than C programming, too. What he did well was to get lucky.

Re: Need help to understand Floppy Disk Drive program..

Posted: Mon Mar 14, 2011 3:44 pm
by Gigasoft
Brendan wrote:Hi,
The actual code looks wrong/broken to me (and not just because of the lack of useful comments in the original):

Code: Select all

	add bx,cx                ;bx = old offset + number of bytes read from disk
	jnc rp_read              ;Only update ES if BX overflowed
	mov ax,es                ;ax = segment
	add ax,#0x1000           ;ax = segment + 0x1000 (where 0x1000 is the adjustment needed when BX overflows)
	mov es,ax                ;Set segment
	xor bx,bx                ;THIS IS WRONG AND SHOULD BE REMOVED!
	jmp rp_read
It is an unnecessary instruction, but it doesn't do any harm. BX is already 0.

Re: Need help to understand Floppy Disk Drive program..

Posted: Mon Mar 14, 2011 8:14 pm
by Brendan
Hi,
Gigasoft wrote:
Brendan wrote:Hi,
The actual code looks wrong/broken to me (and not just because of the lack of useful comments in the original):

Code: Select all

	add bx,cx                ;bx = old offset + number of bytes read from disk
	jnc rp_read              ;Only update ES if BX overflowed
	mov ax,es                ;ax = segment
	add ax,#0x1000           ;ax = segment + 0x1000 (where 0x1000 is the adjustment needed when BX overflows)
	mov es,ax                ;Set segment
	xor bx,bx                ;THIS IS WRONG AND SHOULD BE REMOVED!
	jmp rp_read
It is an unnecessary instruction, but it doesn't do any harm. BX is already 0.
You're right - previous code that ensures the read won't cross a 64 KiB boundary also ensures that BX only overflows when it reaches a 64 KiB boundary (and therefore BX is zero when ES needs to be updated).

There's also plenty of other things I don't like about this code too - poor commenting is the main one; but also "512 kB kernel size should be enough", completely inadequate error handling ("infinite loop" with no error messages at all), hard-coded "where to stop loading", hard-coded drive number, stack at 0x0009FF00 (trash the EBDA), not-so-great instruction selection ("jnc ok2_read" then "je ok2_read" rather than a "jbe ok2_read", "mov dx,head" then "mov dl,#0" followed by "and dx,#0x0100", etc), no BPB (for floppies), etc.
bewing wrote:... And Linus is not god. Not even a particularly great programmer. You may notice that all his code in Linux has been replaced at this point? There is a reason for that. And ASM programming is a much different skill than C programming, too. What he did well was to get lucky.
Charisma, leadership skills and great timing...

I'd also assume that after spending 20 years watching other people patch his code, his programming skills would have improved a lot since he wrote this piece of boot code.


Cheers,

Brendan

Re: Need help to understand Floppy Disk Drive program..

Posted: Mon Mar 14, 2011 8:35 pm
by osdevkid
bewing wrote:
osdevkid wrote: The Floppy has only one track per surface/head, is it?
Correct.
I think, a head/surface has more than one tracks, please refer this link http://www.jegsworks.com/Lessons/lesson6/lesson6-3.htm
bewing wrote:
osdevkid wrote: The sector and track number we are specifying for 0x13 interrupt are starting offset values, is it?
Sort of. The sector number is an offset + 1. The "track" number is called a head number, and is either 0 or 1 on a floppy.
The track number and head number are different, (I think so), please see the below info for interrupt 0x13
INT 13h / AH = 02h - read disk sectors into memory.
INT 13h / AH = 03h - write disk sectors.
input:
AL = number of sectors to read/write (must be nonzero)
CH = cylinder number (0..79).
CL = sector number (1..18).
DH = head number (0..1).
DL = drive number (0..3 , for the emulator it depends on quantity of FLOPPY_ files).
ES:BX points to data buffer.

return:
CF set on error.
CF clear if successful.
AH = status (0 - if successful).
AL = number of sectors transferred.

Actually here, the cylinder number is a track number. the term "cylinder" means, tracks present in both side of surface/head.
bewing wrote:
osdevkid wrote: So, in which case, we required to increment the Track number and read/write the floppy?
Yes. Read the track. Either increment the head number from 0 to 1 (if we were reading head 0), or (if we were reading head 1) change the head number back to 0, and increment the cylinder by 1.
Do you mean "head" & "track" are same? I have asked about "track" number increment, but you have answered about "head" number increment.

Re: Need help to understand Floppy Disk Drive program..

Posted: Mon Mar 14, 2011 9:11 pm
by Brendan
Hi,
osdevkid wrote: I think, a head/surface has more than one tracks, please refer this link http://www.jegsworks.com/Lessons/lesson6/lesson6-3.htm
Maybe this diagram will help:

Image

If the head/s are not moved, then a track is all the sectors that pass under one head. If there's 20 heads and none of them are moved, then a cylinder is all the sectors that pass under all the heads (20 tracks).

There isn't really a common name for all of the sectors that a single head could read (including moving the heads). For single-sided and double-sided floppies the word "side" makes sense. For hard drives the word "platter" is close but not right (as each platter can have 2 sides).

osdevkid wrote:Do you mean "head" & "track" are same? I have asked about "track" number increment, but you have answered about "head" number increment.
Read all the sectors on one track (all the sectors that pass under one specific head when the head isn't moved), then increment the head number and read the next track, and keep doing that until you run out of heads (which means the entire cylinder has been read). Then move the heads to the next cylinder and start reading the first track in that cylinder.


Cheers,

Brendan