Page 1 of 1

Can't read the disk image

Posted: Sun Apr 29, 2018 10:42 am
by Cyxo
Hello,

Well this kind of question might have already been asked, but I don't know how to formulate it so I make a new forum post (please don't blame me :oops: )

So I'm following this tutorial, I'm currently at part 3.6.4 where we are reading values from the disk.

So I compile the code using NASM 0.98.35 (I don't know if that's an old version and if I should update), using the command

Code: Select all

nasm boot_sect.asm -f bin -o boot_sect.bin
and when I run it using

Code: Select all

qemu boot_sect.bin
, it says "Disk read error".

Though when I create a larger disk image using these commands:

Code: Select all

dd if=/dev/zero of=floppy.img bs=1024 count=1440
dd if=boot_sect.bin of=floppy.img seek=0 count=1 conv=notrunc
and then open the floppy through qemu, there is no error anymore, though it displays 0x0000 twice so I think this is linked to the fact the floppy is filled with zeros.

The Hello, world example works so I don't know what's the cause. It feels as if the last two lines

Code: Select all

times 256 dw 0xdada
times 256 dw 0xface
were not being compiled (and indeed if I hexdump the the boot_sect.bin file, I don't see the 0xdada and 0xface bytes, the program ends with the magic number 0xaa55)

Any idea what I might be doing wrong?

(I also attached my source code if you don't want to check the tutorial, or if you notice any difference I didn't, because I'm a bit blind sometimes :| )

EDIT: forgot to mention that I'm pretty much a beginner to OS creation, and not very advanced in ASM either
EDIT 2: well I changed my code just a little (updated the attached files too) so now hexdump says the file ends like this:

Code: Select all

0000200 dada dada dada dada dada dada dada dada
*
0000400 face face face face face face face face
*
0000600
But it still doesn't work :cry:
And I also don't understand why it didn't put the dada and face bytes at the end when the only thing I removed from the code was a line containing ``HELLO_MSG: db 'Booting OS...', 0`` before the ``BOOT_DRIVE: db 0``

Re: Can't read the disk image

Posted: Sun Apr 29, 2018 2:40 pm
by Octocontrabass
Cyxo wrote:So I'm following this tutorial,
Most tutorials are full of mistakes, since the authors of such tutorials are usually also beginners at OS development.

The biggest mistake I see is that the author makes no mention of segment registers. Every memory access involves one of the segment registers, and their initial value is undefined. The author seems to assume that they'll all be zero. Before you can use a segment register to access memory, you must set it.

Re: Can't read the disk image

Posted: Mon Apr 30, 2018 12:35 pm
by Cyxo
Well, when I set them all to 0 it doesn't work.
When I try to set them to a higher address (e.g 0x8000 as this is supposed to be were the pile is?), I indeed have no error anymore, though it doesn't even print anything... I've done some research on osdev wiki but I still have no idea what values to set them to :?

Do you have any guide on that?

Re: Can't read the disk image

Posted: Tue May 01, 2018 12:36 am
by psid
The lack of 0xdada and 0xface values might be due to the default block size on your dd command when writing to the floppy disk: it's defualt is 512, so when writing to it, you will only write 512 bytes using count=1.

Edit:

Also, sector count starts at sector 0, not 1.

Re: Can't read the disk image

Posted: Tue May 01, 2018 2:23 am
by Cyxo
Oh well thanx, indeed the 0xdada and 0xface were not in the floppy disk, I didn't check that. So it worked when I copied 3 instead of 1 it worked. And I assume it says "disk read error" when I do ``qemu boot_sect.bin`` because there is no actual disk, right?

Also I think he didn't set the segment registers because we were in 16 bits real mode. But in the next chapter we enter 32 bits protected mode and there he sets the segment registers.

Re: Can't read the disk image

Posted: Tue May 01, 2018 4:19 am
by psid
Oh well thanx, indeed the 0xdada and 0xface were not in the floppy disk, I didn't check that. So it worked when I copied 3 instead of 1 it worked. And I assume it says "disk read error" when I do ``qemu boot_sect.bin`` because there is no actual disk, right?
If you only load a 1 sector sized file into QEMU, it will only register as 1 sector.

Code: Select all

jc disk_error     ; Jump if error ( i.e. carry flag set )
pop dx            ; Restore DX from the stack
cmp dh , al       ; if AL ( sectors read ) != DH ( sectors expected )
jne disk_error    ; display error message
ret

disk_error:
mov bx , DISK_ERROR_MSG
call print_string
jmp $

;Variables
DISK_ERROR_MSG db " Disk read error !" , 0
If you try to read any sectors after that first sector, there isn't any, so your code will jump to the error message as the disk is only 1 sector in emulated size.

Re: Can't read the disk image

Posted: Tue May 01, 2018 5:24 am
by Cyxo
Yeah I understand that. Thank you very much for your help :)