Bootloader on usb key

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
Keweed
Posts: 1
Joined: Wed Nov 19, 2008 8:12 am

Bootloader on usb key

Post by Keweed »

Hi,

I have a probleme to boot on my usb key. I have developped a boot sector and I have placed it in sector 0 of my usb key (with command dd if=/home/user/bootsector of=/dev/sdb count=1) and after I have develloped a kernel and I have placed it in sector 1 (dd if=/home/user/bootsector of=/dev/sdb count=1 seek=1).

But only my boot sector is functionnal, the message "Chargement du kernel" display on the boot but not the message "kernel is speaking".
I think that the kernel is not loading correctly in the memory in the boot sector.
I would like to know if the interruption 0x13 is compatible with usb key and if it's compatible, where does my code is false.
Thanks and sorry for my bad english =)

My boot sector code :

Code: Select all

[BITS 16] ; mode reel 16 bits
[ORG 0x0] 

mov [bootdev], dl
mov ax, 0x07C0
mov ds, ax
mov es, ax
mov ax, 0x8000
mov ss, ax
mov sp, 0xF000
mov si, msgDebut
call afficher
xor ax, ax
int 0x13 ; initialise la cle usb
push es
mov ax, 0x0100
mov es, ax
mov ah, 0x02; fonction lecture cle usb
mov al, 0x01; 1 secteur a lire
mov cx, 0x0002
mov dh, 0
mov dl, [bootdev]
mov bx, 0
int 0x13; copie le deuxieme secteur du disque en memoire (adresse 0X10000)
pop es

jmp dword 0100:0

afficher:
	push ax
	push bx
	.debut
		lodsb
		cmp al, 0
		jz .fin
		mov ah, 0x0E
		mov bx, 0x07
		int 0x10
		jmp .debut

	.fin
	pop bx
	pop ax
	ret

msgDebut db "Chargement du kernel ...", 13, 10, 0
bootdev db 0

times 510-($-$$) db 144
dw 0xAA55 
My kernel code :

Code: Select all

[BITS 16] ; mode reel 16 bits
[ORG 0x0] 

mov ax, 0X0100
mov ds, ax
mov es, ax
mov ax, 0x8000
mov ss, ax
mov sp, 0xF000
mov si, msg00
call afficher

end:
	jmp end;

afficher:
	push ax
	push bx
	.debut
		lodsb
		cmp al, 0
		jz .fin
		mov ah, 0x0E
		mov bx, 0x07
		int 0x10
		jmp .debut

	.fin
	pop bx
	pop ax
	ret

msg00 db "Kernel is spiking", 10, 0
djmauretto
Member
Member
Posts: 116
Joined: Wed Oct 22, 2008 2:21 am
Location: Roma,Italy

Re: Bootloader on usb key

Post by djmauretto »

Ciao,

Code: Select all

jmp dord 0x7c0:start
start:
mov ax, 0x07C0
mov ds, ax
mov es, ax
mov ax, 0x8000
mov ss, ax
mov sp, 0xF000
mov si, msgDebut
mov [bootdev], dl
call afficher
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: Bootloader on usb key

Post by Schol-R-LEA »

Djmauretto: While I agree with you that this is a good idea (as not every PC quite follows the standard on this, and it would ensure that the CS is where you think it is), this doesn't seem to be the cause of the reported problem. Keweed specifically stated that the printout function (afficher) was working correctly in the boot sector, and that the problems only occur after the jump to the loaded second stage.

Keweed: AFAIK, the standard INT 0x13 routines should work with any type of disk that a boot sector can be loaded from. However, unlike with a floppy disk, most other types of disk require a Master Boot Record and a partition table so that they can be treated as hard disks by the BIOS. Writing to the second physical sector of the drive directly may not work properly. This post describes someone having problems similar to the ones you're describing, and part of the solution was to put the second stage after the partition table.

Have you tested a version of this with a floppy disk first, to see if it works correctly under a known environment? (If you don't have a FDD, you can use an emulator such as Bochs which can set a file to act as a simulated FDD). This would help eliminate any possible problems with the basic code before you test it with a configuration you're not entirely certain of.

I would add an INT 0x13, AH=00 call to reset the drive first (not really necessary with a USB drive, but still a good idea) followed by a INT 0x13, AH=01 check to make sure that the drive is ready before trying to load from it, and again after the read to make sure it worked before actually jumping to the second stage.

(On a side note, you should also disable interrupts while you're setting up the SS; while it is unlikely, it is possible to get an interrupt between setting the top of the stack and setting the stack segment, which would be a Bad Thing. But this is a side issue.)
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
djmauretto
Member
Member
Posts: 116
Joined: Wed Oct 22, 2008 2:21 am
Location: Roma,Italy

Re: Bootloader on usb key

Post by djmauretto »

Ciao,
do you have noted only this code ?

Code: Select all

jmp dword 0x7c0:start
look ahead and see that I have moved also this code:

Code: Select all

mov [bootdev], dl
The code above must run after

Code: Select all

mov ax, 0x07C0
mov ds, ax
:wink:
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: Bootloader on usb key

Post by Schol-R-LEA »

Ah! Yes, I'd missed that. You are absolutely right - that would make recovering the bootdev variable impossible.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
Post Reply