Page 1 of 1

Int 0x13: Reading from HD

Posted: Tue Jan 21, 2014 5:55 am
by dunedain1990
I'm trying to reuse de bios interrupt 0x13. I switch protected mode to real mode (http://wiki.osdev.org/Real_Mode) and then I put into the registers de correct value. After that I call the interruption.

But when the bochs(emulator that i'm using) executes the instruction: lidt idt_48; It go crazy. The next instructions do not correspond with the code.
For example, the next one of lidt is mov bp, 0x0133.

What can be wrong?

Code that I'm trying to execute:

Code: Select all

   cli;
	
	pushl %eax;
	movl $0x011, %eax;
	movl %eax,%cr0;
	movl $0,%eax
	movl %eax,%cr3;
	popl %eax;
	
	lgdt gdt_48;
	
	ljmp $0x10,$jj;

jj:
 	mov $0x18,%ax;
 	mov %ax,%ds
	mov %ax,%es
	mov %ax,%fs
 	mov %ax,%gs

	lidt	idt_48;
	
	pushl %eax;
 	movl $0, %eax;
	movl %eax,%cr0;
	popl %eax;

 	ljmp $0,$jumping;

jumping:
 	mov $0x0,%ax;		
	mov %ax,%ds
	mov %ax,%es
	mov %ax,%fs
 	mov %ax,%gs
	
	mov $0x80b0,%ax 
	mov %ax,%sp;    
	
	sti;
	
	movb $2, %ah;
 	movb $0x1,%al;
	movb $1,%dh;
	movb $0x80,%dl;
	movb $1,%cl;
	movb $1,%ch;
	movw $0x7f00,%bx;
	movw $0x0,%si;
	movw %si,%es;
		
	pushw	%dx;
	pushw	%cx;
	pushw	%bx;
	pushw	%ax;
	
	int $0x13;
Thanks!

Re: Int 0x13: Reading from HD

Posted: Tue Jan 21, 2014 6:43 am
by iansjack
As you have discovered, you can't call BIOS functions from protected mode. You really need to write your own driver.

Re: Int 0x13: Reading from HD

Posted: Tue Jan 21, 2014 7:23 am
by dunedain1990
I am switching the mode

Re: Int 0x13: Reading from HD

Posted: Tue Jan 21, 2014 7:52 am
by Combuster
movl $0x011, %eax;
movl %eax,%cr0;
Do you know that you're modifying up to 32 bits, and that changing 90% of those bits is actually not what you want, whereas the bits you want to change according to the description are actually wrong?
lidt idt_48; It go crazy. The next instructions do not correspond with the code.
For example, the next one of lidt is mov bp, 0x0133.
I would also expect that
mov $0x18,%ax;
actually accesses EAX because you didn't tell the assembler to switch the encoding from 16 to 32 bits.

Re: Int 0x13: Reading from HD

Posted: Tue Jan 21, 2014 11:24 am
by dunedain1990
movl $0x011, %eax;
movl %eax,%cr0;
Do you know that you're modifying up to 32 bits, and that changing 90% of those bits is actually not what you want, whereas the bits you want to change according to the description are actually wrong?
The result it's the same as andl. I use less instructions.
lidt idt_48; It go crazy. The next instructions do not correspond with the code.
For example, the next one of lidt is mov bp, 0x0133.
I would also expect that
mov $0x18,%ax;
actually accesses EAX because you didn't tell the assembler to switch the encoding from 16 to 32 bits.
The segment registers have 16 bits. I use the low part of the register that is 0x0018.

Re: Int 0x13: Reading from HD

Posted: Tue Jan 21, 2014 12:08 pm
by iansjack
When loading CR0 you must preserve reserved bits. You don't do so.

When you load 0 to CR0, completely ignoring the reserved bits, you may think that you are returning to real mode. What you think and what actually happens may not coincide. A debugging session might help here; better still, just change the bit(s) that you want to and preserve the others.

Undefined behaviour is rarely good news.

Re: Int 0x13: Reading from HD

Posted: Thu Jan 23, 2014 5:54 am
by dunedain1990
I change this and It isn't still working.

Re: Int 0x13: Reading from HD

Posted: Thu Jan 23, 2014 6:13 am
by iansjack
As most people take the easy way out and write their own driver, I suspect that you are going to have to debug this one yourself. Single-step the code until you call the interrupt and see if memory and registers look correct. In particular, check that any structures required by real-mode BIOS calls haven't been changed. An obvious place to check is that the interrupt vectors are still as the BIOS set them.

Re: Int 0x13: Reading from HD

Posted: Thu Jan 23, 2014 10:08 am
by Combuster
dunedain1990 wrote:I change this and It isn't still working.
Why does my crystal ball say you guessed a change and didn't actually fix anything...?