Int 0x13: Reading from HD

Programming, for all ages and all languages.
Post Reply
dunedain1990
Posts: 4
Joined: Tue Jan 21, 2014 5:19 am

Int 0x13: Reading from HD

Post 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!
Last edited by dunedain1990 on Tue Jan 21, 2014 7:23 am, edited 1 time in total.
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Int 0x13: Reading from HD

Post by iansjack »

As you have discovered, you can't call BIOS functions from protected mode. You really need to write your own driver.
dunedain1990
Posts: 4
Joined: Tue Jan 21, 2014 5:19 am

Re: Int 0x13: Reading from HD

Post by dunedain1990 »

I am switching the mode
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Int 0x13: Reading from HD

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
dunedain1990
Posts: 4
Joined: Tue Jan 21, 2014 5:19 am

Re: Int 0x13: Reading from HD

Post 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.
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Int 0x13: Reading from HD

Post 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.
dunedain1990
Posts: 4
Joined: Tue Jan 21, 2014 5:19 am

Re: Int 0x13: Reading from HD

Post by dunedain1990 »

I change this and It isn't still working.
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Int 0x13: Reading from HD

Post 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.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Int 0x13: Reading from HD

Post 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...?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply