Page 1 of 1

Boot from flash

Posted: Tue Nov 12, 2013 6:09 am
by kemosparc
Hi,

I have built my custom boot loader that loads a 64-bit kernel, and it is working fine on emulated/virtualized environments: QEMU/KVM, Bochs, and Virtual Box.

The boot loader is installed on a virtual floppy image. My kernel image got relatively big about 15 KB, so what the bootloader does is that it loads the next 17 sectors to the boot sector from the floppy, and then changes the head and loads the next 18 sectos

Code: Select all

;----------Second Stage Boot Loader----------;
SecondStage:
	mov ah, 0               ; RESET-command
	int 13h                   ; Call interrupt 13h
	mov [drive], dl        ; Store boot disk
	or ah, ah               ; Check for error code after interrupt for reset
	jnz cannot_read_sector  ; Try again if ah != 0 -> failure on reset


	mov ax, 0x0
	mov es, ax
	mov bx, 0xA000       ; Destination address to store my kernle at 0000:A000

	mov ah, 02h             ; READ SECTOR-command
	mov al, 11h             ; Number of sectors to read (0x11 = 17 sectors)
	mov dl, [drive]         ; Load boot disk
	mov ch, 0               ; Cylinder = 0
	mov cl, 2               ; Starting Sector = 3
	mov dh, 0               ; Head = 1
	int 13h                 ; Call interrupt 13h
	jnz cannot_read_sector ; print error message if failed to read
	
	mov ax, 0
	mov es, ax
	add bx, 0x2200          ; Add to the destination address 17*512 bytes whic is 2200h Destination address now is 0000:C200 where the rest of my code will resiode

	mov ah, 02h             ; READ SECTOR-command
	mov al, 12h             ; Number of sectors to read (0x12 = 18 sectors)
	mov dl, [drive]         ; Load boot disk
	mov ch, 0               ; Cylinder = 0
	mov cl, 1               ; Starting Sector = 3
	mov dh, 1               ; Head = 2
	int 13h                 ; Call interrupt 13h

	or ah, ah               ; Check for error code
	jnz cannot_read_sector ; print error message if failed to read
ret
Now I would like to boot my os from a real laptop that does not have a floppy and I would like to install everything on a bootable USB flash that can start my computer up like in qemu.

So my questions are:
1. What do I need to change in the above code to accomodate that.
2. My laptop supports USB in FDD mode, and if so do I still have do worry about #1. It also suppots HDD mode so which way should i go.
3. Can you please direct me to a tutorial or a diocumentation on how to convert a floppy image and install it on a flash and make it bootable using linux tools?

What I would like to acheive is atlease compile the babystep2 http://wiki.osdev.org/Babystep2 on osdev tutorials and put it on a flash drive that I can start my real laptop on. I already have this tutorial up compiled and writting on floppy.img and working well on emulated/virtualized environments.

I have tried a somethings like writing my floppy.img on the flash using dd but the computer did not even boot and displayed the initial welcome message from my bootloader (I mean before even loading or jumping to my kernel code)


Thanks for the help
Karim.

Re: Boot from flash

Posted: Tue Nov 12, 2013 10:20 am
by kemosparc
Hi,

I figured out my problem.

The problem was that as shown above I was trying to read from a floppy and not from a normal disk, so I was loading half of the code as the secons 18 sectors were not read correctly


Here is my new code that accounts for both floppy and normal disk:

Code: Select all

;----------Set Sectors To Read----------;
SetSectorsToRead:
	pushf
	push ds
	push es
	push di
	push si
	push ax
	push bx

	mov ax,[drive]                   ; get drive that booted from
    cmp ax,0x0                            ; see if the drive is the floppy
    je SetSectorsToReadDone         ; if so do nothing and leave default to 11h sectors
    mov bl,23h                            ; else set the right number of sectors to read from the drive
    mov [sectors_to_read],bl         ; store the value for later

    SetSectorsToReadDone:
    pop bx
    pop ax
	pop si
	pop di
	pop es
	pop ds
	popf
ret


;----------Second Stage Boot Loader----------;
SecondStage:
	mov ah, 0               ; RESET-command
	int 13h                 ; Call interrupt 13h
	mov [drive], dl         ; Store boot disk
	or ah, ah               ; Check for error code
	jnz cannot_read_sector  ; Try again if ah != 0
    call SetSectorsToRead

	mov ax, 0x0
	mov es, ax
	mov bx, 0xA000       ; Destination address = 0000:1000


	mov ah, 02h             ; READ SECTOR-command
	mov al, [sectors_to_read] ; Number of sectors to read (0x12 = 18 sectors)
	mov dl, [drive]         ; Load boot disk
	mov ch, 0               ; Cylinder = 0
	mov cl, 2               ; Starting Sector = 3
	mov dh, 0               ; Head = 1
	int 13h                 ; Call interrupt 13h
	or ah, ah               ; Check for error code
	jnz cannot_read_sector

	mov al,[drive]   
	cmp al,0x0                             ; if drive is not floppy so we have readeverything, just jump to routine exit
    jne SecondStageDone

    mov ax, 0
    mov es, ax
    add bx, 0x2200          ; Destination address = 0000:C200

	mov ah, 02h             ; READ SECTOR-command
	mov al, 12h             ; Number of sectors to read (0x12 = 18 sectors)
	mov dl, [drive]         ; Load boot disk
	mov ch, 0               ; Cylinder = 0
	mov cl, 1               ; Starting Sector = 3
	mov dh, 1               ; Head = 1
	int 13h                 ; Call interrupt 13h

	or ah, ah               ; Check for error code
	jnz cannot_read_sector

	SecondStageDone:
ret


sectors_to_read     db  11h  ; number of sectors to read in the first read atempt
drive db 0                      ; Used to store boot device


Thanks a lot
Karim