Writing a Bootloader into usb Flash

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
User avatar
RezaNoei
Member
Member
Posts: 40
Joined: Sun Jun 01, 2014 1:39 pm

Writing a Bootloader into usb Flash

Post by RezaNoei »

Hi,
in the last 2 week I have tried so much to write and run a bootloader from my usb flash memory.
Everything is good, my bootloader could work on qemu emulator and even I tested my
bootloader on a real floppy disk, when i have try to run my bootloader from usb flash it seems
something goes wrong, interrups doesn't work, labels are useless. now, only thing that i could do is to
write some value on the vga memory at 0xb8000 and see the result.
i even placed bpb in the first of my code (fat 12's bpb).
whats is my problem?
help me please :oops:
Octocontrabass
Member
Member
Posts: 5588
Joined: Mon Mar 25, 2013 7:01 pm

Re: Writing a Bootloader into usb Flash

Post by Octocontrabass »

Help us help you. (At least read the section titled "details".)
User avatar
RezaNoei
Member
Member
Posts: 40
Joined: Sun Jun 01, 2014 1:39 pm

Re: Writing a Bootloader into usb Flash

Post by RezaNoei »

Octocontrabass wrote:Help us help you. (At least read the section titled "details".)
Excuse me,
I Working At Linux Os,
And I Assembled my bootloader in Nasm with this Command:

Code: Select all

nasm -f bin [myfile.asm] -o [myfile.bin]
then i moved this bootloader to my usb flash memory in this way :

Code: Select all

sudo dd if=[mybootloader] of=/dev/sdc (my flash memory) bs=512 count=1
my workable bootloader is something like this :

Code: Select all

[org 0x7c00]
start:          jmp loader

TIMES 0Bh-$+start DB 0
 
bpbBytesPerSector:  	DW 512
bpbSectorsPerCluster: 	DB 1
bpbReservedSectors: 	DW 1
bpbNumberOfFATs: 	DB 2
bpbRootEntries: 	DW 224
bpbTotalSectors: 	DW 2880
bpbMedia: 	        DB 0xF0
bpbSectorsPerFAT: 	DW 9
bpbSectorsPerTrack: 	DW 18
bpbHeadsPerCylinder: 	DW 2
bpbHiddenSectors:       DD 0
bpbTotalSectorsBig:     DD 0
bsDriveNumber: 	        DB 0
bsUnused: 	        DB 0
bsExtBootSignature: 	DB 0x29
bsSerialNumber:	        DD 0xa0a1a2a3
bsVolumeLabel: 	        DB "MOS FLOPPY "
bsFileSystem: 	        DB "FAT12   "

loader :
;========================== Main Code
mov ax,0xb800
mov es,ax
mov di,0
mov ah,0x3
mov al,'R'
stosw
;==========================
Times 510-($-$$) db 0
dw 0xaa55
if i replace Main code in above with following code my bootloader will not work correctly :

Code: Select all

mov ah,0x0e
mov si,string    ; Labels Will Not Work 
_loop:
          lodsb
          int 10h ; this interrupt will not work too
          cmp al,0
          jne _loop
jmp $
string:
          db "Hello World",0
thank you my friend for helping.
Last edited by RezaNoei on Tue Mar 03, 2015 1:58 pm, edited 1 time in total.
Octocontrabass
Member
Member
Posts: 5588
Joined: Mon Mar 25, 2013 7:01 pm

Re: Writing a Bootloader into usb Flash

Post by Octocontrabass »

Your code doesn't work because you aren't setting all of the input registers (and flags).

For example, "lodsb" uses DS, SI, and the Direction flag as inputs. You've already put a value in SI using "mov si,string". You still need to put a value in DS and the Direction flag before lodsb will work properly.

The same thing is happening with "int 10h". It calls the BIOS, which uses many registers as input. You'll need a reference such as RBIL to figure out which registers you need to set.
User avatar
BrightLight
Member
Member
Posts: 901
Joined: Sat Dec 27, 2014 9:11 am
Location: Maadi, Cairo, Egypt
Contact:

Re: Writing a Bootloader into usb Flash

Post by BrightLight »

You need to set up the stack and the segments. Interrupts (like INT 10h) use the stack and the segments determine locations of labels. LODSB doesn't just get a byte from SI, it gets it from DS:SI, so you need to set the segments and stack.

Code: Select all

bits 16
org 0x7C00

main:
	cli
	cld
	mov ax, 0
	mov ss, ax
	mov ds, ax
	mov es, ax
	mov sp, 0x7C00
	sti

	mov si, string
	; do whatever you want now

string	db "Hello, world!",0
What's more is that INT 10h doesn't just print a character to the screen. You need to set the registers according to the function you want to call. In your case, function 0Eh is the print character function. See this.
You know your OS is advanced when you stop using the Intel programming guide as a reference.
User avatar
RezaNoei
Member
Member
Posts: 40
Joined: Sun Jun 01, 2014 1:39 pm

Re: Writing a Bootloader into usb Flash

Post by RezaNoei »

thank you friends,
my fault was in setting segment register.

once i placed following code to the bootloader, my usb flash memory, booted very well:

Code: Select all

mov ax,0x0
mov ds,ax
again, thank you :)
Post Reply