OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Sep 17, 2019 2:45 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Sector count register for the ATA/ATAPI read command
PostPosted: Mon Aug 26, 2019 6:34 am 
Offline

Joined: Fri Aug 23, 2019 8:27 am
Posts: 4
What is the use of "sector count" register for the ATA/ATAPI read command ?
The description given by ATA/ATAPI specification seems quite straightforward :

Quote:
Sector Count - number of sectors to be transferred. A value of 00h specifies that 256 sectors are to be transferred.


However, all tutorial and code examples I could find show that we are supposed to read one sector at a time and control ourselves the number of time we do so.
for example, taken from https://wiki.osdev.org/ATA_PIO_Mode
The pio28_read procedure reads one sector and decrements ebx that contains initially the sector count :

Code:
   
    mov cx, 256
    rep insw        ; gulp one 512b sector into edi

...
   
    dec ebx         ; decrement the "sectors to read" count


And pio28_read is called until ebx reaches 0


Code:

.read28:
    test ebx, ebx       ; no more sectors to read?
    je short .r_don
    call pio28_read     ; read up to 256 more sectors, updating registers
    je short .read28    ; if successful, is there more to read?   
.r_don:



Why the ATA controller expects we provide the sector count if we need to control it ourselves anyway ?
(of course it applies to the write function as well. )


Top
 Profile  
 
 Post subject: Re: Sector count register for the ATA/ATAPI read command
PostPosted: Mon Aug 26, 2019 9:04 am 
Offline
Member
Member
User avatar

Joined: Fri Aug 07, 2015 6:13 am
Posts: 1045
I don't know about PIO, since I use DMA.
With DMA you just specify how many sectors you want to read and read them in.
You should consider writing a DMA only driver, PIO is basically a pointless, painfully slow method of communicating with the drive.
DMA is much faster and allows for sequential reads up to 65536 sectors at a time.

_________________
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader


Top
 Profile  
 
 Post subject: Re: Sector count register for the ATA/ATAPI read command
PostPosted: Mon Aug 26, 2019 9:40 am 
Offline

Joined: Fri Aug 23, 2019 8:27 am
Posts: 4
Thanks for your answer.

I am very new to OS developement. So far I just have a simple piece of code that runs in pmode and I assumed it was easier to start experimenting with PIO, an that it would not be a big problem on booting. I am aware of DMA vs PIO, and I expect to experiment with that as well.

According to your answer, I guess that the sector count is for DMA only, but the documentation is not really clear on that.
It seems that everbody sets this value while using PIO mode and I don't unserstand why.

regards


Top
 Profile  
 
 Post subject: Re: Sector count register for the ATA/ATAPI read command
PostPosted: Mon Aug 26, 2019 9:51 am 
Offline
Member
Member

Joined: Sat Mar 10, 2018 10:16 am
Posts: 131
Sector count is for defined how many sectors you want read. In PIO48 you can define this for 65536 sectors.

Using PIO in bootloader isnt very good idea. Try use BIOS interrupt in real mode e.g.:
Code:
  ;read usb/cdrom sectors 0 to 50
  mov ax, 0x1000  ;load kernel to 10000h < segment
  mov es, ax  ;load kernel to 10000h
  mov bx, 0  ;offset 0 < segment offset
  mov ah, 0x02  ;read function
  mov al, 50  ;50 sectors < add here your value
  mov ch, 0  ;track 0
  mov cl, 2  ;start sector is 2
  mov dh, 0  ;head 0
  mov dl, 0x00  ;bootable usb/cdrom
  int 13h  ;read


and after that jump into protected mode and run kernel with jmp 0x10000.

PIO vs DMA? Difference is with this: PIO use ports to read/write and eat processor time. DMA is read/write to memory without processor. But if you have good scheduler, PIO can give good results and maybe you dont have to use DMA!

//EDIT: If you looking for code for learn, my operation system is open source https://github.com/Klaykap/LightningOS/blob/master/source/drivers/ata.c


Top
 Profile  
 
 Post subject: Re: Sector count register for the ATA/ATAPI read command
PostPosted: Mon Aug 26, 2019 10:13 am 
Offline

Joined: Fri Aug 23, 2019 8:27 am
Posts: 4
Thank you,

I just had a look at your code and it brings me back to my very first question :


you tell the controller the number of sectors to read/write


Code:
outb(base+2, (unsigned char)(number_of_sectors >> 8));




and yet you need to iterate on this same value

Code:
    for(int i=0; i<number_of_sectors; i++) {
        for (int j=0; j<256; j++) {
            if(type==READ) {
                buffer[offset] = inw(base + 0);
            }
            else {
                outw(base + 0, buffer[offset]);
            }
            offset++;
        }
    }



why the ATA controller needs this value ?
What would be the effect of providing a different value on sector count register (base+2) and number_of_sectors in your loop ?

regards


Top
 Profile  
 
 Post subject: Re: Sector count register for the ATA/ATAPI read command
PostPosted: Mon Aug 26, 2019 11:06 am 
Offline
Member
Member

Joined: Sat Mar 10, 2018 10:16 am
Posts: 131
At first, little comment to my code, I dont choose drive, because you should choose drive only one. You can choose master drive(hard disc are usually on it) with:
Code:
//base is 0x1F0
outb(base+6, 0x40);

on start you operation system.

ABOUT NUMBERS OF SECTORS

If you want read/write from hard disc, you must tell it three things:
1. First sector who you want read
2. Numbers of sectors from first sector
3. If you want read or write

Now read driver code:
Code:
    outb(base+2, (unsigned char)(number_of_sectors >> 8));
    outb(base+2, (unsigned char)number_of_sectors);

I am sending number of sectors. If I want read only 1 sector, same result is with:
Code:
    outb(base+2, 0); //0x0001 >> 8 = 0x00
    outb(base+2, 1); //0x0001 & 0xFF = 0x01


Next code is send first sector and command for read/write.

Now hard disc have all values and it start work. In PIO48 hard disc use port base+0 (0x1F0) for sending data if we reading or getting data if we writing.

One sector have 512 bytes, and port base+0 is word type, than we must read for one sector port base+0 256 times. But sometimes we have in numbers of sectors more sectors than 1 and we must read/write all data. I use two cycles, but too same is:
Code:
   for(int i=0; i< (number_of_sectors * 256); i++) {  //one sector is reading/writing 256 times
           if(type==READ) {
               buffer[offset] = inw(base + 0);
           }
           else {
               outw(base + 0, buffer[offset]);
           }
         offset++;
   }


E. g. You want read 10 sectors. One sector must be read from port base+0 256 times, so you must read base+0 2560 times. Did you see how it work?

Summary: Number of sectors defines how times you must read output for read/ write input for write to port base+0.

If you have heavy head from something, ask me.


Top
 Profile  
 
 Post subject: Re: Sector count register for the ATA/ATAPI read command
PostPosted: Mon Aug 26, 2019 12:59 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 1636
Rivusqc wrote:
However, all tutorial and code examples I could find show that we are supposed to read one sector at a time and control ourselves the number of time we do so.

The drive has an internal buffer where it must hold the sector before you can read it. Very old drives could only hold one sector in the buffer, so you had to wait for the drive to refill the buffer after each sector you read.

For example, to read two sectors, you would set the sector count register to 2, then send your "read" command to the drive, then wait for the buffer to fill, then read one sector, then wait for the buffer to fill again, then read the second sector.

Modern drives have much larger buffers, but they still make you wait after each sector for backwards compatibility.


Top
 Profile  
 
 Post subject: Re: Sector count register for the ATA/ATAPI read command
PostPosted: Tue Aug 27, 2019 4:26 am 
Offline

Joined: Fri Aug 23, 2019 8:27 am
Posts: 4
That makes sense,
thank you for your answer!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group