Page 1 of 1

I can't read sector from CD-ROM

Posted: Fri Aug 22, 2014 12:33 pm
by omar
Help please! I wrote a bootloader called ISOSYS for my OS which i'll call OS32...
It needs to load the second sector to load the stage 2 but CF is always set no matter what I do!
Please help!

This is isosys.asm:

Code: Select all

;===============================================================
;
;	            ISOSYS v1.0 in isosys.asm
;
; Program for booting OS32 and OS32-based systems from CD/DVDs
; using the ElTorito boot standard.
;
; By Omar, 2014
;
; ===============================================================

; set up bios...
bits 16
org 0x7c00

; beginning of sector 1, byte 0:

; jump over functions block
jmp Start

;
; Print()
;		prints a string to standard output
; AL = 0 : terminate
;

Print:
	lodsb				; grab a byte from SI and place it in AL
	cmp al, 0			; is AL = 0?
	jc PrintDone			; yep, we're done so return
	mov ah, 0eh
	int 0x10			; nope, print the character
	jmp Print			; loop until no more characters to be printed
PrintDone:
	ret				; return to wherever we were!

;
; Start()
;		bootloader entry point
;

Start:
	mov si, creditmsg
	call Print			; print the first message
	mov dh, 1			; read 1 sector

.ReadSector:
	cli				; disable interrupts
	push dx				; push this stack value
	sti				; re-enable interrupts

	mov ax, 0x1000			; read sector into address 0x1000
	xor bx, bx			; read sector into offset 0x0
	mov ah, 0x02			; read sector function
	mov al, dh			; read 1 sector
	mov ch, 0x0			; must be 0x0 to read sector
	mov dh, 0			; reading head 0
	mov cl, 0x02			; start reading from sector 2 to load the second stage

	mov [CDDriveNumber], dl		; store our drive number is variable CDDriveNumber
	cli				; disable interrupts
	pusha				; save the stack
	sti				; enable interrupts

	int 0x13			; INT 13h
	jc Error			; handle error because Carry Flag is set

	pop dx				; Restore DX

.ExecuteSector:
	; now that our sector was loaded into address 0x1000:0x0, let's execute it!
	jmp 0x1000:0x0			; jump to execute the sector
;
; Error()
;		handles errors when carry is set
;

Error:
	mov si, errmsg
	call Print			; print an error message

	cli				; disable interrupts to prevent problems
	hlt				; halt the system... :(

; data section

creditmsg db "ISOSYS v1.0 by Omar Mohammad, Init...", 0
errmsg db "Cannot load the second stage. (File not found maybe?)", 0
CDDriveNumber db 0

times 2048 - ($-$$) db 0		; fill in 2048 bytes with zeroes

; end of sector 1! :D Any more code here would take bytes 2048 and 2049 but we don't need that...
And this is the second stage, that is boot.asm

Code: Select all

bits 16
org 0x1000

Main:
	xor bx, bx
	mov ah, 0eh
	mov al, 'A'
	int 0x10

	cli
	hlt

times 2048 - ($-$$) db 0
This is to be booted from a CD-ROM. I use debian Gnu/Linux and I make iso like this:
genisoimage -no-emul-boot -boot-load-size 4 -eltorito-boot ISOSYS -o boot.iso iso/
Then I have a virtual machine on virtualBox and it shows the message Cannot load the second stage file not found maybe?

Please help? Any thing will be greatly appreaciated

Re: I can't read sector from CD-ROM

Posted: Fri Aug 22, 2014 2:42 pm
by Nable
Oh, man it looks like you are doing a very common mistake - one should reserve some well-known amount space after initial jump, as BIOS puts information there before passing control to bootloader. See http://wiki.osdev.org/El-Torito#A_BareB ... tion_Table for details.

Re: I can't read sector from CD-ROM

Posted: Fri Aug 22, 2014 3:50 pm
by omar
Oh thanks :D Aside from that do I have another mistake? Is it OK to build the Iso with no-emul-boot ??? Thanks again :D
Oh and, sorry but it still doesn't work it appears to have made no difference. I still get the error message I wrote. :( :x

Re: I can't read sector from CD-ROM

Posted: Fri Aug 22, 2014 6:18 pm
by Nable
omar wrote:Aside from that do I have another mistake?
There are some wrong things (lack of stack pointer and segment registers initialization, imbalanced stack (push without corresponding pop), mess during passing arguments to int 0x13, etc) + many strange things (e.g. unneeded cli/sti).
It may be very nice for you to study some manuals and wiki materials first. You can start with http://wiki.osdev.org/Required_Knowledge . Or, at least, learn how to use Bochs with its debugger - it's a very cool tool for searching wrong things in OS-related code.

Re: I can't read sector from CD-ROM

Posted: Sat Aug 23, 2014 12:38 pm
by omar
Sorry but I can't use Bochs. I'm not on Windows I'm using Debian 7.6. :(

Re: I can't read sector from CD-ROM

Posted: Sat Aug 23, 2014 12:50 pm
by Nable
omar wrote:Sorry but I can't use Bochs. I'm not on Windows I'm using Debian 7.6. :(
WUT? Bochs by no means tied to Windows - you can easily checkout it's source code and build it under ~all kind of Linux (btw, I'm also using Debian and build Bochs from source when I need it). Why didn't you try finding some information at the wiki: http://wiki.osdev.org/Bochs#Compiling_Bochs_from_Source ?

Re: I can't read sector from CD-ROM

Posted: Sat Aug 23, 2014 12:51 pm
by SpyderTL
Just stared looking through the code above and noticed this in the print function at the top:

Code: Select all

cmp al, 0
jc PrintDone
Shouldn't that be "je"?

Re: I can't read sector from CD-ROM

Posted: Sun Aug 24, 2014 2:43 am
by phillid
SpyderTL wrote:Shouldn't that be "je"?
Judging by the accompanying comments, yes. IIRC, after a `cmp X,Y`, the carry flag is set if X is greater than Y, so in this case, it'd be performing the complete opposite of what's desired... IIRC. Lol.

Re: I can't read sector from CD-ROM

Posted: Sun Aug 24, 2014 3:12 am
by Icee
phillid wrote:Judging by the accompanying comments, yes. IIRC, after a `cmp X,Y`, the carry flag is set if X is greater than Y, so in this case, it'd be performing the complete opposite of what's desired... IIRC. Lol.
If X were greater than Y, there would be no carry (or, rather, borrow since CMP is subtraction). JC is the same as JB, i.e. jump if below (unsigned), i.e. X < Y. Since nothing ever can be less than 0, that loop would be infinite.

Re: I can't read sector from CD-ROM

Posted: Mon Aug 25, 2014 2:55 am
by phillid
I stand corrected :)