OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Oct 20, 2020 1:27 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: ATAPI driver reads nothing
PostPosted: Thu Feb 13, 2020 1:18 am 
Offline

Joined: Thu Feb 13, 2020 1:06 am
Posts: 10
Hello ! I wrote some in assembly (NASM) code for reading CDROM, interrupts work but it reads nothing to memory. I checked memory dumps in qemu and in bochs but both of them are empty. I don't know how to fix this problem.
This is the code for reading :
Code:
; ebx - base port of selected ATA/IDE channel
; eax - lba value
; ecx - sectors to read
; edi - address where to load data
command_packet times 12 db 0
load_sector_lba_cd :
    push esi
    push edx
    push eax
    push ecx

    mov dx, bx
    add dx, ERROR_FEATURES_OFFSET
    xor al, al
    out dx, al
    sub dx, ERROR_FEATURES_OFFSET

    add dx, LBA_MIDDLE_OFFSET
    mov ax, ATAPI_SECTOR_SIZE
    out dx, al
    sub dx, LBA_MIDDLE_OFFSET

    add dx, LBA_HIGH_OFFSET
    shr ax, 8
    out dx, al
    sub dx, LBA_HIGH_OFFSET

    add dx, COMMAND_STATUS_OFFSET
    mov al, 0xa0 ;ATAPI_COMMAND_PACKET
    out dx, al
    call ata_status_wait
poll_for_drq_1 :
    in al, dx
    test al, ATA_ERROR_BIT
    jnz end_of_reading
    test al, ATA_DRQ_BIT
    jz poll_for_drq_1
setup_command_packet :
    pop ecx
    pop eax
    mov byte [command_packet], 0xa8 ;READ_ATAPI_COMMAND
    mov edx, eax
    shr edx, 24
    mov byte [command_packet + 2], dl
    mov edx, eax
    shr edx, 16
    mov byte [command_packet + 3], dl
    mov edx, eax
    shr edx, 8
    mov byte [command_packet + 4], dl
    mov edx, eax
    shr edx, 0
    mov byte [command_packet + 5], dl

    ;mov edx, ecx
    ;shr edx, 16
    ;mov byte [command_packet + 6], dl
    ;mov edx, ecx
    ;shr edx, 8
    ;mov byte [command_packet + 7], dl
    ;mov edx, ecx
    ;shr edx, 0
   ; mov byte [command_packet + 8], dl
    mov byte [command_packet + 9], 1
    push eax
    push ecx
writing_command :
    mov dx, bx
    mov ecx, 6
    mov esi, command_packet
    rep outsw
wait_irq_atapi :
    cmp byte [IRQ_received], 0
    je wait_irq_atapi
    mov byte [IRQ_received], 0
get_length_sector :
    xor eax, eax
    mov dx, bx
    add dx, LBA_HIGH_OFFSET
    in al, dx
    shl ax, 8
    sub dx, LBA_HIGH_OFFSET
    add dx, LBA_MIDDLE_OFFSET
    in al, dx
    sub dx, LBA_MIDDLE_OFFSET
    push eax
    add dx, COMMAND_STATUS_OFFSET
    in al, dx
read_data :
    pop eax
    mov dx, 0
    mov cx, 2
    div cx
    mov ecx, eax
    mov dx, bx
    rep insw
wait_irq2_atapi :
    cmp byte [IRQ_received], 0
    je wait_irq2_atapi
    mov byte [IRQ_received], 0
wait_drq_bsy_clear :
    add dx, COMMAND_STATUS_OFFSET
    call ata_status_wait
poll_for_drq_2:
    in al, dx
    test al, ATA_ERROR_BIT
    jnz end_of_reading
    test al, ATA_DRQ_BIT
    jz end_of_reading
    jnz poll_for_drq_2
end_of_reading :
    pop ecx
    pop eax
    pop edx
    pop esi
    ret

and from my main file :
Code:
mov ebx, 0x170
mov al, 0x0
mov edx, 0x176
out dx, al
mov edi, KERNEL_OFFSET
mov ecx, 1
mov eax, 36
reading_loop :
inc eax
call load_sector_lba_cd
cmp eax, 66 ; some value beyond 40 kb size
jne reading_loop
call 0x200000
jmp $

KERNEL_OFFSET equ 0x200000

does somebody know a possible reason for that ?

upd. i have a question about cdrom splitting by sectors. what offset in bytes has second sector from start of cdrom ? and how bios can read only 512 bytes if cdrom's sector size is 2048 ?


Last edited by alicedeveloper on Sun Feb 16, 2020 5:51 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: ATAPI driver reads nothing
PostPosted: Thu Feb 13, 2020 8:05 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 2397
alicedeveloper wrote:
does somebody know a possible reason for that ?

I haven't checked to see if your code is correct, but it looks like you're trying to read LBA 12. If that sector is filled with zeroes, and the memory you're reading it into is also filled with zeroes, how will you be able to tell if the read succeeded? Perhaps try with LBA 16, which has a lot of non-zero data on typical CDs.

alicedeveloper wrote:
what offset in bytes has second sector from start of cdrom ?

LBA 1 has an offset of 2048 bytes.

alicedeveloper wrote:
and how bios can read only 512 bytes if cdrom's sector size is 2048 ?

The BIOS reads 2048 bytes and throws away 1536 bytes. It translates the LBA based on instructions in the El Torito structures on the disk, so when you ask the BIOS for LBA 0, you are not reading the first 512 bytes of the CD.


Top
 Profile  
 
 Post subject: Re: ATAPI driver reads nothing
PostPosted: Thu Feb 13, 2020 8:13 am 
Offline
Member
Member

Joined: Sat Nov 21, 2009 5:11 pm
Posts: 682
Sector 12 is most likely empty. ISO 9660 volume descriptors are contained in sectors 16 through 23 from the start of the track. Also, don't assume that the data track starts at sector 0. There is also no such thing as waiting for DRQ. When the BSY bit is cleared and the DRQ bit is cleared, it means that the command has completed.

Int 13h function 42h will read entire sectors (2048 bytes) when not in emulation mode.


Top
 Profile  
 
 Post subject: Re: ATAPI driver reads nothing
PostPosted: Thu Feb 13, 2020 7:15 pm 
Offline

Joined: Thu Feb 13, 2020 1:06 am
Posts: 10
i tried many different values include 1 but no one of them works
also i tried to read sectors from 1 to 20 in a loop
can it be connected with that i use xorriso to generate iso image from binary ?
thank you for your replies


Top
 Profile  
 
 Post subject: Re: ATAPI driver reads nothing
PostPosted: Sat Feb 15, 2020 5:45 pm 
Offline

Joined: Thu Feb 13, 2020 1:06 am
Posts: 10
okay, i can now read values from 16 sector of the disk but when i try to read other sectors i still get zeroes values in the memory
how can it be possible if i write my kernel (37 kb size) after bootloader to iso image ?

updated : i found non-zero values from other disk sectors but it's connected with xorriso output and i still cant read my kernel


Top
 Profile  
 
 Post subject: Re: ATAPI driver reads nothing
PostPosted: Sat Feb 15, 2020 8:02 pm 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 714
Location: USA
alicedeveloper wrote:
how can it be possible if i write my kernel (37 kb size) after bootloader to iso image ?

That question puzzles me. An .iso image does not have a bootloader as you might think there would be. i.e.: The BIOS doesn't load the first sector of the disc and jump to it.

There are two ways you can boot from a CD-ROM. Emulated and non-emulated. If you were using the BIOS to read from the disc, in emulated mode, it would read 512-byte sectors. However, since you are using the hardware to read from the disc, emulation has nothing to do with the actual reading from the disc.

So, here are a few questions/statements:
1) Are you booting from the cd-rom?
1a) If so, do you use the BIOS to read the 37k of kernel into memory? (Why not?)
1b) Did you use emulated mode?
2) If you are using emulated mode, it will be a variation of a floppy image (1.44, 2.88, etc), or a hard drive image.
3) If you are not using emulated mode, the BIOS will load a specified count of (512-byte) sectors to a specified location and jump to it.
4) Have you used a dump utility to check if there really is anything on the cd-rom image?

Also, there are numerous other things that need to be considered when reading from a cd-rom.
1) Does it use the MMC-5 commands? What about SBC-3?
2) Is a packet 12 or 16 bytes in length?
3) Do you use the 10-, 12-, or 16-byte variation of each command?

Please describe in a little more detail what you are actually trying to do. If you are booting from the cd-rom, this is considerably different than if you are just trying to read from the cd-rom.

Ben
- http://www.fysnet.net/media_storage_devices.htm


Top
 Profile  
 
 Post subject: Re: ATAPI driver reads nothing
PostPosted: Sat Feb 15, 2020 11:38 pm 
Offline

Joined: Thu Feb 13, 2020 1:06 am
Posts: 10
thank you for new ideas !
Quote:
1) Are you booting from the cd-rom?
1a) If so, do you use the BIOS to read the 37k of kernel into memory? (Why not?)

yes, im booting from cd-rom, but i want to use second stage of bootloader because first stage of bootloader is too small and it cant load kernel beyond first megabyte
even if i will load all kernel to memory from bios in real mode i will be needed to have an atapi driver (i wrote some c code for that but it works as this code in assembly - reads nothing from the disk)

i use bochs and qemu
i'm trying to write something like that : https://wiki.osdev.org/ATAPI ("x86 Examples") but in assembly so i use scsi commands, 12-byte packet and 12-byte command

i found my kernel code at 37 sector. can you explain why it's located so far from start of the cdrom ?

p.s. i can read and call my kernel but it misbehaves, produces wrong output and crushes with an error when i send keyboard interrupt despite the fact when it's loaded by bios kernel works normally
p.p.s i've edited code from my first post


Top
 Profile  
 
 Post subject: Re: ATAPI driver reads nothing
PostPosted: Sun Feb 16, 2020 9:57 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 2397
alicedeveloper wrote:
i found my kernel code at 37 sector. can you explain why it's located so far from start of the cdrom ?

How did you put your kernel on the CD?


Top
 Profile  
 
 Post subject: Re: ATAPI driver reads nothing
PostPosted: Sun Feb 16, 2020 11:01 am 
Offline

Joined: Thu Feb 13, 2020 1:06 am
Posts: 10
this is a part of my makefile :
Code:
os-image : boot/stage1/boot.bin boot/stage2/boot.bin kernel/kernel.bin
   cat $^ > os-image
all : os-image
all-hdd : all
   dd if=/dev/zero of=disk.img bs=1024 count=2880
   dd if=os-image of=disk.img conv=notrunc
   cp disk.img cd_img
   xorriso -as mkisofs -o mybootcd.iso -V MyOSName -b disk.img cd_img

cd_img - empty directory which is required by xorriso
mybootcd.iso - iso image for booting in an emulator
2880 x 1024 bytes is maximal size of a diskette


Top
 Profile  
 
 Post subject: Re: ATAPI driver reads nothing
PostPosted: Sun Feb 16, 2020 12:02 pm 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 714
Location: USA
alicedeveloper wrote:
Quote:
1) Are you booting from the cd-rom?

yes, im booting from cd-rom

So, I must ask again, are you booting in emulation mode or not? It makes a big difference.

In emulation mode, you will have a 1.44 meg floppy or a hard drive image within the first part of the iso image. It just so happens that your kernel might be at around sector 72 (512-byte sectors) within the emulated image which places it at about sector 37 (2048-byte sectors) on the iso image.

When booting in emulation mode, you can still load your 2nd stage above the 1 Meg mark. Emulation mode makes it (almost) exactly as if you booted from a floppy or hard drive image. The BIOS makes it so you can't tell the difference.

As stated earlier by a fellow poster, you can use the BIOS extended read services to read all of your 2nd stage, kernel, etc., above the 1 Meg mark.

It is best to use the BIOS (or efi firmware) to load most of your boot stage(s) and possibly the kernel, then letting the kernel load an ATA(PI) driver.

If you are not booting in emulation mode, then you can still use the BIOS to read from the disc, however now you must now have a ISO filesystem driver within your boot code to find the 2nd stage, kernel, etc.

One of the advantages of the El Torito boot is that you can place multiple boot sectors on the disc, allowing the BIOS to choose which one to boot depending on the architecture of the system it is being booted on.

Anyway, take advantage of the firmware's functions. That is what it is there for.

Ben


Top
 Profile  
 
 Post subject: Re: ATAPI driver reads nothing
PostPosted: Sun Feb 16, 2020 2:03 pm 
Offline

Joined: Thu Feb 13, 2020 1:06 am
Posts: 10
i dont know how to determine whether emulation mode is turned on or not

file with my bochs settings :

Code:
megs: 2048
romimage: file=/usr/share/bochs/BIOS-bochs-latest, address=0xfffe0000
vgaromimage: file=/usr/share/bochs/VGABIOS-lgpl-latest
log: bochsout.txt
mouse: enabled=0
display_library: x, options="gui_debug"
ata1-master: type=cdrom, path=mybootcd.iso, status=inserted
ata0-master: type=disk, path=hdd.img, mode=flat, biosdetect=auto, translation=auto
floppy_bootsig_check : disabled=1
boot:cdrom
clock: sync=realtime, time0=local
ata2: enabled=1, ioaddr1=0x1e8, ioaddr2=0x3e0, irq=11
ata3: enabled=1, ioaddr1=0x168, ioaddr2=0x360, irq=10
pci: enabled=1, chipset=i440fx

if you could explain how to do that i will check


Top
 Profile  
 
 Post subject: Re: ATAPI driver reads nothing
PostPosted: Sun Feb 16, 2020 3:49 pm 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 714
Location: USA
alicedeveloper wrote:
i dont know how to determine whether emulation mode is turned on or not

The emulation is determined by the format of the ISO image, not the bochrc.txt file.

Therefore, might I suggest that you read up on the EL TORITO specs.

If emulation is indicated:
- The BIOS treats a section of the cd-rom (ISO image) as a floppy or hard drive image.
- The BIOS will not read outside of this section. i.e.: If floppy emulation is indicated, only 720 (2048-byte) sectors are accessible via the BIOS. Period.
- The BIOS will read the first (512-byte) sector of this section to 0x07C00 and jump to it.
- The BIOS will emulate all disk services as if it were a floppy or hard drive.
- The BIOS will return an error if you try to read outside of the emulated image.
- All sector reads are 512-byte sectors. i.e.: Reading LBA 4 will read the first 512 bytes of the second physical sector of this section.
- All reads are as if it was a floppy or hard drive image, so you have to know what kind of image it is. i.e.: does it have a MBR, GPT, VBR, what file system is on each partition, etc.

If emulation is not indicated:
- The BIOS will read a count of sectors (1 to 255) from a specified location, to a specified location (20 bit addresses only. Below 1 Meg), and then jump to it.
- Using the BIOS extended read services, you can read all of the CD-ROM, sector for sector.
- Using the BIOS extended read services, you can read sectors to above the 1 MEG mark (if EDD 3.0+ is found)
- If the CD-ROM is a valid ISO 9660 format, you now have to know the ISO 9660 file system to be able to find the remaining files to load.

Read up on the El Torito format a bit to know what kind of format you will be creating. An emulated format is the simplest to use, though (using the BIOS) most of the cd-rom is wasted and/or redundant. This is not to say that after you have booted in emulated mode, your code cannot load a valid ATA(PI) driver and read the rest of the CDROM. This is perfectly acceptable.

May I ask, must you use an ISO image to boot? Since you (currently) don't understand the El Torito specs, can you move to a floppy or hard drive image? This would be much easier to start out with. The only reason I can think of to boot an ISO is to place it in real hardware being that most hardware now-a-days won't even have a floppy disk. If this is the case, how about a cheap thumb drive and rufus?

Ben


Top
 Profile  
 
 Post subject: Re: ATAPI driver reads nothing
PostPosted: Mon Feb 17, 2020 6:15 am 
Offline

Joined: Thu Feb 13, 2020 1:06 am
Posts: 10
i was using emulation mode but yesterday i've decide to use non-emulation mode because it seems more interesting
so i've switch to extended read function (int 0x13, ah=0x42)
unfortunately, my code doesnt work and crushes with an error

Code:
load_disk_lba :
     push ax
     push si
     mov ah, 0x42
     mov si, lba_read_packet
     mov byte [lba_read_packet], 0x10
     mov word [lba_read_packet + 2], cx
     mov word [lba_read_packet + 4], di
     mov word [lba_read_packet + 6], ds
     mov word [lba_read_packet + 8], bx
     int 0x13
     jc disk_error
     pop si
     pop ax
     ret


Code:
ata1-0: atapi_cmd_error: key=05 asc=21
int13_cdrom: function 42, status 03 !


i dont know how to fix that

upd. i've replaced 10 to 8 and bx to bl in main function so now it's working!


Last edited by alicedeveloper on Mon Feb 17, 2020 9:40 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: ATAPI driver reads nothing
PostPosted: Mon Feb 17, 2020 6:20 am 
Offline

Joined: Thu Feb 13, 2020 1:06 am
Posts: 10
Quote:
May I ask, must you use an ISO image to boot? Since you (currently) don't understand the El Torito specs, can you move to a floppy or hard drive image? This would be much easier to start out with. The only reason I can think of to boot an ISO is to place it in real hardware being that most hardware now-a-days won't even have a floppy disk. If this is the case, how about a cheap thumb drive and rufus?


i want to have an ability to load kernel from cdrom, usb, hard drive
but i read in osdev wiki that reading from usb device is very complex so i left it to further time


Top
 Profile  
 
 Post subject: Re: ATAPI driver reads nothing
PostPosted: Mon Feb 17, 2020 10:03 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 2397
alicedeveloper wrote:
Code:
   xorriso -as mkisofs -o mybootcd.iso -V MyOSName -b disk.img cd_img

With this command, you're using floppy disk emulation since you didn't specify otherwise. Your bootloader can use INT 0x13 to access the kernel exactly the same way it does when you write disk.img to a floppy disk. If you want to access the contents of disk.img without using the BIOS emulation, you will need to parse the El Torito structures to locate the boot image, or parse the filesystem to find the correct file.

Since you've switched to no-emulation mode, you'll have to parse the filesystem to locate your kernel since there is no longer a virtual floppy disk containing the kernel. I agree that it's more interesting this way!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 12 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