bootloader loading second stage issues

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
0xC
Posts: 6
Joined: Fri Feb 25, 2011 4:39 am
Location: Durham, United Kingdom

bootloader loading second stage issues

Post by 0xC »

Note: I have a feeling that this will have been covered already in another thread, but I've searched and not found anything, so if there is such a thread, I apologise.

I want to make a two stage bootloader, which uses a floppy disk, but no file system, just raw copying of the binaries onto the disk. I believe this should be possible, I can't think of any reason why it wouldn't be, but whenever I try to load my second stage binary from the next sector, it fails. By fails I mean it is supposed to print a message, but it doesn't.

The code for stage1 is as follows:

Code: Select all

bits 	16 						;we start in 16 bit mode
org 	0x7C00					;and at 0x7C00

entry:							;start here!
		jmp 	load
;; print: print the string that resides in si
print:
		.printstart:
		lodsb					;move a byte from si to al
		or		al, al			;check for a zero
		jz 		.printend
		mov		ah, 0x0E
		int		10h
		jmp		.printstart

		.printend:
		ret
load:
		mov		si, stage1msg
		call 	print

		mov		ax,	0x1000		;this is where the sector is going to be read into
		mov		bx, ax
		xor		ax, ax
		mov		es, ax
		
.reset:
		mov		ah, 0x0			;function 0, reset
		mov		dl, 0x0			;drive 0
		int		0x13
		jc 		.reset			;if the carry is set, something went wrong, try again

.read:
		mov		ah, 0x02		;function 2, read
		mov		al, 1			;read 1 sector
		mov		ch, 1			;the sector past stage1, so still track 1
		mov		cl, 2			;the sector to read
		mov		dh, 0			;the head number
		mov		dl, 0			;the drive number
		int		0x13
		jc		.read

		jmp 0x1000:0x00
		
		cli
		hlt

stage1msg	db "This is stage1", 13, 10, 0		
times 510 - ($ - $$) db 0		;it needs to be 512 bytes
dw 0xAA55						;boot signature
and stage2:

Code: Select all

org 0x1000							; This sector is loaded at 0x1000:0 by the bootloader

start:
	mov 	si, msg
print:
	lodsb
	or	al, al
	jz 	out
	mov	ah, 0x0E
	int	0x13
	jmp	print
out:	
	msg 	db "This is the second stage!", 13, 10, 0
	cli								; just halt the system
	hlt
The following procedure is how i assemble and write to disk:

Code: Select all

nasm stage1.asm -f bin -o stage1
nasm stage2.asm -f bin -o stage2
cat stage1 stage2 > floppy.img
dd if=floppy.img of=/dev/sdc
and then use bochs to run the code by reading the floppy.
Note: /dev/sdc is because I'm using a usb floppy drive.

If anybody has encountered a similar problem or could shed some light on why this is not working it would be much appreciated.

Thanks for your time.

-0xC
xleelz
Posts: 8
Joined: Mon Aug 01, 2011 7:59 pm

Re: bootloader loading second stage issues

Post by xleelz »

Code: Select all

      mov      ax,   0x1000      
      mov      bx, ax                       ;<---Instead of moving 1000 to bx, move it to es and zero out bx
      xor      ax, ax
      mov      es, ax

Code: Select all

.read:
      mov      ah, 0x02      
      mov      al, 1         
      mov      ch, 1         ;still pretty sure track is 0
      mov      cl, 2         
      mov      dh, 0         
      mov      dl, 0         
      int      0x13
      jc      .read

Code: Select all

   mov   ah, 0x0E
   int   0x13                     ;pretty sure you ment int 0x10

Code: Select all

out:   
   msg    db "This is the second stage!", 13, 10, 0                    ;I'm not sure if this would help but I think you should move this below the rest of the code
   cli                        
   hlt                                              ;since you're only reading a sector and not using a File System you should include "times 512-($-$$) db 0" or something similar to zero out the rest of the 512 bytes
Just a couple of careless mistakes
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: bootloader loading second stage issues

Post by Combuster »

Code: Select all

cli                       
hlt
Lock up the computer, which in some emulators means locking up the video card as well. That might result in the last bit of text not showing up on the actual screen.

The other instance does not get executed, why is it there?
"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 ]
0xC
Posts: 6
Joined: Fri Feb 25, 2011 4:39 am
Location: Durham, United Kingdom

Re: bootloader loading second stage issues

Post by 0xC »

Thank you for pointing out those mistakes xleelz, I have made the appropriate changes, yet it still does not print. I cannot think of any reason why it would not work, dd'ing a floppy image to a floppy drive does just write consecutive sectors, right?

I tried taking out the cli and hlt Combuster, to no avail. The cli and hlt still remained in stage1 from when it was a simple print a string to the screen then wait bootloader, and I just never took them out.

Again, thank you for your time :)

[edit]
Scratch that, I've gotten it to work, it was a problem with the jmp, I wasn't setting the right segment value.
Thank you for your help.
[/edit]
M2004
Member
Member
Posts: 65
Joined: Sun Mar 07, 2010 2:12 am

Re: bootloader loading second stage issues

Post by M2004 »

I see no stack being setup properly at the start of your boot code.

regards
Mac2004
0xC
Posts: 6
Joined: Fri Feb 25, 2011 4:39 am
Location: Durham, United Kingdom

Re: bootloader loading second stage issues

Post by 0xC »

Yeah, I was getting to that, set up a routine and called it, forgetting to setup the stack and it worked so I just left it for the time being. I'll get to it though :)

Thanks
-0xC
User avatar
turdus
Member
Member
Posts: 496
Joined: Tue Feb 08, 2011 1:58 pm

Re: bootloader loading second stage issues

Post by turdus »

Combuster wrote:

Code: Select all

cli                       
hlt
Lock up the computer, which in some emulators means locking up the video card as well. That might result in the last bit of text not showing up on the actual screen.

The other instance does not get executed, why is it there?
1st: cli+hlt is common way to put cpu sleep
2nd: doesn't matter because ip never gets there. Otherwise the string would be executed first, which is usually bad idea anyway.
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: bootloader loading second stage issues

Post by Chandra »

turdus wrote:1st: cli+hlt is common way to put cpu sleep
It is, if the execution were to reach that instruction. Did you notice the preceding jmp instruction?
turdus wrote:2nd: doesn't matter because ip never gets there.
That's it. It doesn't get executed and hence, doesn't serve any purpose.
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
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: bootloader loading second stage issues

Post by Combuster »

turdus wrote:cli+hlt is common way to put cpu sleep
No, it's the equivalent of halt-and-catch-fire because you can't wake a CPU from that with normal means, and it has adverse effects on emulators.

The correct low-power loop is hlt+jmp with interrupts enabled. In case of a crash, cli;jmp $ is the correct sequence to see any output you made prior to that.
"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