0

I'm trying to get right status of Hard disk registers before read or write the hard disk using LBA equations.

So there is the little code:

0x8392 <waitdisk+7>     mov    eax,0x1f7                                                                                                                            │
│   0x8397 <waitdisk+12>    mov    edx,eax                                                                                                                              │
│   0x8399 <waitdisk+14>    in     al,dx                                                                                                                                │
│   0x839a <waitdisk+15>    mov    WORD PTR [ebp-0x2],ax                                                                                                                │
│   0x839e <waitdisk+19>    movzx  eax,WORD PTR [ebp-0x2]                                                                                                               │
│   0x83a2 <waitdisk+23>    cmp    ax,0x40                                                                                                                              │
│   0x83a6 <waitdisk+27>    jne    0x8392 <waitdisk+7> 

So you can see that i'm waiting for right status ( 0x40 ) to end into the loop.

There is the info registers before taking harddisk/floppy status :

eax            0x1f7               503
ecx            0x40                64
edx            0x1f7               503
ebx            0x0                 0
esp            0xff88              0xff88
ebp            0xff98              0xff98
esi            0x0                 0
edi            0x8548              34120
eip            0x8399              0x8399 <waitdisk+14>
eflags         0x6                 [ IOPL=0 PF ]

And there is after taking status :

eax            0x100               256
ecx            0x40                64
edx            0x1f7               503
ebx            0x0                 0
esp            0xff88              0xff88
ebp            0xff98              0xff98
esi            0x0                 0
edi            0x8548              34120
eip            0x839a              0x839a <waitdisk+15>
eflags         0x6                 [ IOPL=0 PF ]

0x100 taken by ax means nothing , ...

i'm using qemu emulator , so i think that maybe i have mistaken when i have created disk image or i have gived bad parameters for qemu-system-x86_64 , but nothing :

dd if=/dev/zero of=disk.img bs=512 count=2880
    dd if=bin/boot1.img of=disk.img bs=512 conv=notrunc
    dd if=bin/boot2.img of=disk.img seek=1 bs=512 conv=notrunc
    dd if=bin/kernel.elf of=disk.img bs=512 seek=5 conv=notrunc

qemu-system-x86_64 -fda disk.img -no-shutdown -no-reboot -d cpu_reset -d int file:serial.log

So where i have done mistake ?? Is it the bad code or i should not use inline assembly ??

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Esaïe Njongssi
  • 63
  • 2
  • 13
  • 2
    Why are you storing and reloading a word after you only did `in al, dx`? The `0x100` is from a `0x1` in AH that you left unmodified. I suppose this is un-optimized compiler-generated asm, from C with inline asm? That would explain putting a number in EAX and then copying it to EDX; GCC does that kind of nonsense. – Peter Cordes Jun 01 '21 at 05:42
  • But , if you look at after `in al,dx` , the value of `al` means nothing ... `0x0` means nothing – Esaïe Njongssi Jun 01 '21 at 08:05
  • I'm not trying to answer the whole question (or I would have posted an answer instead of commenting), just explain why you left high garbage in AX when you didn't need to. I don't know about obsolete ATA PIO programming (which seems to be what this is, according to a google on your IO port number). I don't see a mention of status-code meanings in https://wiki.osdev.org/ATA_PIO_Mode, but don't you normally have to send a command first before you can expect any result in the status register (read of 0x1F7)? – Peter Cordes Jun 01 '21 at 08:13
  • send command before of after to getting status registers give me the same result , .... – Esaïe Njongssi Jun 01 '21 at 09:29
  • 1
    At powerup, `DRDY` is cleared. This means the device can accept only a limited set of commands (diagnostics and init) but I would expect the BIOS to init the disk. Also, it is generally advised to make a 400us pause before reading the status register (o reading it multiple times discarding all but the last value). Finally, if the selected drive doesn't exist, `DRDY` will be 0 (IIRC). In short, it's possible the status register is 0, it depends on what you did before reading it. – Margaret Bloom Jun 01 '21 at 11:32

0 Answers0