Write bootloader to USB

Programming, for all ages and all languages.
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Write bootloader to USB

Post by mangaluve »

Has anyone any simple software to recommend for writing a image to a usb stick (so I can boot my OS from it).
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Write bootloader to USB

Post by Troy Martin »

Partcopy on windows, just put the boot sector onto it like a floppy, I suppose.

*calls Dex over* Dex, I think you know what to do for USB sticks, right?
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Re: Write bootloader to USB

Post by mangaluve »

Thanks! Where can I find partcopy and how is it used?

I found it, but I dont know what to use for the last argument, to get it on USB
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Re: Write bootloader to USB

Post by Dex »

Heres what you need to know:
1. When you boot from a usb key fob, in most case it will emulate ether a fdd or a hdd, you need to know which one your BIOS users.
because it not a simple case of load the first scetor and jump to it.
If you use a floppy image for a bios that emulates hdd, it will not boot, its as simple as that.

2. To see what your BIOS user's, put any usb fob in your PC and reboot, then enter bios and go to boot order see if, it says usb fdd or usb hdd.
If it does not say ether fdd or hdd and just usb stick or something like that its most likely to be hdd.

3. Now to boot as a floppy its as simple as geting a bootable floppy image and puting it on the usb device with a program like dd for windows or dd in linux.
For Hdd you most first format it fat16 (unless it a big fob) and than make it bootable and add your boot sector, see my tut here:
http://www.dex4u.com/USBboot.htm
Note: on later vers of HxD you can not use delete, but you use replace instead, so if you follow that tut and delete does not work use the replace option.

4. Last but not least, you are much better of to start with, using dos to load your OS by making it in a format that Dos can load eg: mz exe.
Then all you need to do is format it and than click make bootable, the work under win98, but under xp you need to use the HPformat and maybe freedos files.

I have written a windows program to do this for you but, have not got round to finishing it.
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Re: Write bootloader to USB

Post by mangaluve »

Thank you! In my BIOS it seems like I can choose between USB as fdd / hdd / cd. The thing is, my boot loader loads the kernel from the boot-disk using BIOS, int 13h (where BIOS automatically sets the disk to read from to the same as where I boot from). Should I still care whether it's a floppy or HDD and so on? You talk about floppy image / hdd image and so on, but does that really matter when I load the kernel this way? Couldn't I use the exact same disk image for a floppy and the hdd?

It's strange, because even though I choose to boot from USB, emulated as a floppy, the computer doesn't seem to try to read the USB stick at all (it does not blink as it should)
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Re: Write bootloader to USB

Post by Dex »

Right, so when you say it does not read it, how are you trying to read it, eg: have you set you boot order to read usb before your IDE etc, or have you pressed say F11 to get a boot menu ?.
I have found it common for the BIOS to miss reading the usb even if you have set it, things to try is to switch off fast booting or go into the BIOS first, then when you come out its more chance of reading it.
Also make sure it got a valid boot sector for the emulation you want.

Now for your other ?, your boot loader will work fine using int 13h as long as the code is right for the emulation.
eg: if its a floppy you would use the same as for any normal floppy.
For hdd you would need to use a fat16 or fat32 for reading the floppy and the right driver letter (80h).
This is assuming your using FAT.
And if you want window or linux to read/write to the usb in the normal way, you will need to put a floppy image, so its formated right etc.
For hdd you only need to format it fat16/fat32 then put your boot sector on and make sure you change the MBR as per the link to my usb boot page.

PS: I am going to release a windows program to put your own image on to usb fobs to make them bootable, as fdd or hdd.
This should be available in the next two days or so.
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: Write bootloader to USB

Post by AndrewAPrice »

I use a tool called Flashnul in Windows to write raw images (usually install discs for Linux) to USB so I don't have to burn it to CD. Then boot the USB if your BIOS supports it.

I also heard Cygwin can do it, but I've had no success at that.
My OS is Perception.
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Re: Write bootloader to USB

Post by mangaluve »

I dont really understand what you mean (Im kinda new to this). I don't use FAT or anything like that , I dont even have a file system right now. I just have my boot loader on the first 512 bytes in a file. Immediately after that, is the kernel. So what my boot loader does is to load some sectors from the second sector and forward, using INT 13h and the value of DL that BIOS sets. Now, why does it matter if it's stored on a floppy or a hard drive (it does not in Bochs). Sorry if I'm a little slow, but as I said, Im pretty new to all of this. I mean, if I put my image on a floppy and boots from it, BIOS will set DL to the proper value so the kernel is loaded from the floppy. If I put it on hard drive, BIOS will set the DL to a value which will result in the kernel being read from the hard disk.
User avatar
kmtdk
Member
Member
Posts: 263
Joined: Sat May 17, 2008 4:05 am
Location: Cyperspace, Denmark
Contact:

Re: Write bootloader to USB

Post by kmtdk »

well
use HxD, and transfere the img to it, and then try boot it.
and about dl, that is mostly important, since it may varies from bios to bios.(when using usb), and to read from hdd, it will have to be antoher value (so save it, and then use it)

KMT dk
well, what to say, to much to do in too little space.
when it goes up hill, increase work, when it goes straight, test yourself but when going down, slow down.
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Re: Write bootloader to USB

Post by mangaluve »

Im getting really confused here..won't BIOS provide the correct value of DL if I boot from a USB, emulating a floppy?

As for my computer not even reading the USB stick, I've tried to change to boot order in BIOS. I also have a Boot Menu (using F12), and I've tried all possible alternatives involving USB. Does it matter which USB port I use?
User avatar
kmtdk
Member
Member
Posts: 263
Joined: Sat May 17, 2008 4:05 am
Location: Cyperspace, Denmark
Contact:

Re: Write bootloader to USB

Post by kmtdk »

well
first
is it a new computer( since old can be a little buggy )
port does not matter.
BIOS provide the currect dl value, so use it.

i have no problems with it.

KMT dk
well, what to say, to much to do in too little space.
when it goes up hill, increase work, when it goes straight, test yourself but when going down, slow down.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: Write bootloader to USB

Post by earlz »

is that really supported from a USB flash drive? why not use the flash drive as hdd emulation?

oh, and sadly, floppy emulation is not the answer to all bootloader problems
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Re: Write bootloader to USB

Post by Dex »

First you are right in the way you think, but this is the mistake most people make with USB booting, if you think logicaly it should not mater what is on the boot sector, as it is load and jump to etc.
But it simple is not the case if you are using a real PC for usb booting, as for some reason, it read's the BPB in the floppy case.
The BIOS Parameter Block (BPB) maybe needed for usb fdd booting
Just give this a try to see if it works

Code: Select all

;************************************
; By Dex
; Assemble with fasm 
; c:\fasm Pmode.asm Pmode.bin
;
;************************************
org 0x7C00 
use16
;****************************
; Realmode startup code.
;****************************
start1:
	jmp		start
	nop

                db 'IBM  5.0'   ; OEM label
                dw 512          ; bytes per sector
                db 1            ; sectors per cluster
                dw 1            ; reserved sectors
                db 2            ; number of FATs
                dw 224          ; root directory entries
                dw 80 * 36      ; total sectors on disk
                db 0xF0         ; media descriptor
                dw 9            ; sectors per 1 FAT copy
                dw 18           ; sectors per track
                dw 2            ; number of heads
                dd 0            ; hidden sectors
                dd 0            ; big total sectors
                db 0            ; boot unit
                db 0            ; reserved
                db 0x29         ; extended boot record id
                dd 0x12345678   ; volume serial number
                db 'NO NAME    '; volume label
                db 'FAT12   '   ; filesystem id

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Boot sector code starts here ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

start:
        xor   ax,ax
        mov   ds,ax
        mov   es,ax
        mov   ss,ax
        mov   sp,0x7C00 
;*****************************
; Setting up, to enter pmode.
;*****************************
        cli 
        lgdt  [gdtr]
        mov   eax, cr0
        or    al,0x1 
        mov   cr0,eax
        jmp   0x10: protected
;*****************************
; Pmode. ;-)
;*****************************
use32
protected:
        mov   ax,0x8 
        mov   ds,ax
        mov   es,ax
        mov   ss,ax
        mov   esp,0x7C00
;*****************************
; Turn floppy off (if space).
;*****************************
        mov   dx,3F2h
        mov   al,0
        out   dx,al
;*********************************
; Print T in the right hand corner
;*********************************
        mov   byte [es:0xB809E], "T"
;*********************************
; Just loop for now
;*********************************
        jmp   $
;*************************************
; GDT. 
;*************************************
gdt:        dw    0x0000, 0x0000, 0x0000, 0x0000
sys_data:   dw    0xFFFF, 0x0000, 0x9200, 0x00CF
sys_code:   dw    0xFFFF, 0x0000, 0x9800, 0x00CF
gdt_end:

gdtr:       dw gdt_end - gdt - 1
            dd gdt
;*************************************
; Make program 510 byte's + 0xaa55
;*************************************
times 510- ($-start1)  db 0
dw 0xaa55


User avatar
kmtdk
Member
Member
Posts: 263
Joined: Sat May 17, 2008 4:05 am
Location: Cyperspace, Denmark
Contact:

Re: Write bootloader to USB

Post by kmtdk »

well
that makes sence Dex, since i have includede a BPB ( looks like floppy) and all bios's have said "FDD" usb.
so changing the BPB may result in another emulation ?
( maby i test it, )

KMT dk

Fast test results:
chaning data within BPB does not change my old computers view on the usb ( still usb FDD)[however this bios is not so good for usb, since Cuqe wont load ( sectors above 100 since to be a problem) ]
, and my laptop does not chek for type, it just boots fine, still not depedning on the BPB
and this ^ happens to the other computers i have tested it on.
well, what to say, to much to do in too little space.
when it goes up hill, increase work, when it goes straight, test yourself but when going down, slow down.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Write bootloader to USB

Post by Troy Martin »

Is there a wiki article about this? If not, there should be one. This question has been asked more than stuff like "how do I read from the floppy in pmode" and the like.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
Post Reply