[SOLVED] 2 files with floppy image

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.
feare56
Member
Member
Posts: 97
Joined: Sun Dec 23, 2012 5:48 pm

[SOLVED] 2 files with floppy image

Post by feare56 »

I have started OSDeving again finally and I am making my own 2 stage bootloader and I got it all done but when I test it with qemu using my virtual floppy drive it wont boot. I was able to boot when it just a cli and hlt bootoader with only one file. How would I fix this problem with linux is there a command I need to use to make part one of the bootloader go to the first sector of the floppy image? If it helps I am running Red Hat Enterprise Linux 6 which would be the same commands as Fedora
Last edited by feare56 on Mon Apr 15, 2013 6:58 pm, edited 1 time in total.
Prochamber
Member
Member
Posts: 100
Joined: Wed Mar 13, 2013 2:27 am

Re: 2 files with floppy image

Post by Prochamber »

How would I fix this problem with linux is there a command I need to use to make part one of the bootloader go to the first sector of the floppy image?
Grammer aside, after assembling your bootloader you can use the following command under Linux, FreeBSD or OSX to write it to the start of a disk image:

Code: Select all

dd conv=notrunc if=bootload.bin of=image.ima
If you're running Windows you might need some kind of external tool, like PartCopy.

Make sure you have a boot signature, 0xAA55 in last two bytes or it won't boot.
TachyonOS - Violates causality on 95% of attempts. Runs at approximately 1.5c.
feare56
Member
Member
Posts: 97
Joined: Sun Dec 23, 2012 5:48 pm

Re: 2 files with floppy image

Post by feare56 »

Thanks! But now I am running into another problem, when I boot the floppy image it shows up like what the picture shows and it should show

Loading boot image
Preparing to load operating system...

i used

Code: Select all

cp /home/name/Desktop/stage2.sys /media/floppy
to get it on there
Attachments
test.png
test.png (2.98 KiB) Viewed 4714 times
HugeCode
Member
Member
Posts: 112
Joined: Mon Dec 17, 2012 9:12 am

Re: 2 files with floppy image

Post by HugeCode »

In this case you should post your code here. If you're sure it's OK, you should check your org (if using NASM) and positions of your messages. Maybe it would be better to print single charcter to see if code booted or not.

BTW you can use debugger to see from where the wind blows.
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: 2 files with floppy image

Post by Combuster »

Stage 2 is not the bootsector. The bootsector is broken or you're not using your VM properly, since it's not even booting from floppy.
"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 ]
feare56
Member
Member
Posts: 97
Joined: Sun Dec 23, 2012 5:48 pm

Re: 2 files with floppy image

Post by feare56 »

I am using the brokenthorn tutorial since i am not that good at bootloaders right now so my code looks similar to the bootloader 4 demo and i even tried his compiled boot1.bin and KRNLDR.SYS even that didnt work it wouldnt even boot
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: 2 files with floppy image

Post by Mikemk »

What vm are you using? It's not even attempting to boot from the floppy. (Going straight to hard drive)
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
feare56
Member
Member
Posts: 97
Joined: Sun Dec 23, 2012 5:48 pm

Re: 2 files with floppy image

Post by feare56 »

I am using qemu and when i set up a new vm i choose an existing image file and point it to the floppy image. It worked before the bootloader 4 tutorial it is just that one i am having troubles with
Prochamber
Member
Member
Posts: 100
Joined: Wed Mar 13, 2013 2:27 am

Re: 2 files with floppy image

Post by Prochamber »

I'm assuming that you did the emulator bits correctly with 'qemu -fda image.ima' and that you mounted and unmounted the image correctly.

How does the first stage boot the second stage?
Make sure that the first stage is attempting to load the same filename as your second stage and that the second stage is actually on the disk.

There's a million things that could be going wrong and you've not given us much to go on. Post the code on your first and second stage bootloader and explain what you did to compile and run them.
TachyonOS - Violates causality on 95% of attempts. Runs at approximately 1.5c.
feare56
Member
Member
Posts: 97
Joined: Sun Dec 23, 2012 5:48 pm

Re: 2 files with floppy image

Post by feare56 »

When it comes to bootloaders there is room for improvement for me. To compile I used

Code: Select all

nasm boot1.asm -f bin -o boot1.bin
and same with stage2.asm except I name it KRNLDR.SYS.

To put it in the floppy i use

Code: Select all

dd conv=notrunc if boot1.bin of=flow.img
Then to get KRNLDR.SYS i use

Code: Select all

cp /home/name/Desktop/KRNLDR.SYS /media/floppy
Then to run I pick the "Choose an existing image" option then point it to boot it from there

since my internet isn't letting me upload the files i will give you the code from both.

In boot1.asm

Code: Select all

org 0x7c00
bits 16

start: jmp Main

;=================================
;      OEM Paremeter Block
;=================================

TIMES 0Bh-$+start DB 0

bpbOEM db "Flow OS "
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   "

;====================================
;               Calls
;====================================

Print:
	lodsb
	or al, al
	jz PrintDone
	mov ah, 0eh
	int 10h
	jmp Print
PrintDone:
	ret

;************************************************;
; Reads a series of sectors
; CX=>Number of sectors to read
; AX=>Starting sector
; ES:BX=>Buffer to read to
;************************************************;

ReadSectors:
     .MAIN
          mov     di, 0x0005                          ; five retries for error
     .SECTORLOOP
          push    ax
          push    bx
          push    cx
          call    LBACHS                              ; convert starting sector to CHS
          mov     ah, 0x02                            ; BIOS read sector
          mov     al, 0x01                            ; read one sector
          mov     ch, BYTE [absoluteTrack]            ; track
          mov     cl, BYTE [absoluteSector]           ; sector
          mov     dh, BYTE [absoluteHead]             ; head
          mov     dl, BYTE [bsDriveNumber]            ; drive
          int     0x13                                ; invoke BIOS
          jnc     .SUCCESS                            ; test for read error
          xor     ax, ax                              ; BIOS reset disk
          int     0x13                                ; invoke BIOS
          dec     di                                  ; decrement error counter
          pop     cx
          pop     bx
          pop     ax
          jnz     .SECTORLOOP                         ; attempt to read again
          int     0x18
     .SUCCESS
          mov     si, msgProgress
          call    Print
          pop     cx
          pop     bx
          pop     ax
          add     bx, WORD [bpbBytesPerSector]        ; queue next buffer
          inc     ax                                  ; queue next sector
          loop    .MAIN                               ; read next sector
          ret

;************************************************;
; Convert CHS to LBA
; LBA = (cluster - 2) * sectors per cluster
;************************************************;

ClusterLBA:
          sub     ax, 0x0002                          ; zero base cluster number
          xor     cx, cx
          mov     cl, BYTE [bpbSectorsPerCluster]     ; convert byte to word
          mul     cx
          add     ax, WORD [datasector]               ; base data sector
          ret
     
;************************************************;
; Convert LBA to CHS
; AX=>LBA Address to convert
;
; absolute sector = (logical sector / sectors per track) + 1
; absolute head   = (logical sector / sectors per track) MOD number of heads
; absolute track  = logical sector / (sectors per track * number of heads)
;
;************************************************;

LBACHS:
          xor     dx, dx                              ; prepare dx:ax for operation
          div     WORD [bpbSectorsPerTrack]           ; calculate
          inc     dl                                  ; adjust for sector 0
          mov     BYTE [absoluteSector], dl
          xor     dx, dx                              ; prepare dx:ax for operation
          div     WORD [bpbHeadsPerCylinder]          ; calculate
          mov     BYTE [absoluteHead], dl
          mov     BYTE [absoluteTrack], al
          ret

;*********************************************
;	Bootloader Entry Point
;*********************************************

Main:

     ;----------------------------------------------------
     ; code located at 0000:7C00, adjust segment registers
     ;----------------------------------------------------
     
          cli						; disable interrupts
          mov     ax, 0x7c00				; setup registers to point to our segment
          mov     ds, ax
          mov     es, ax
          mov     fs, ax
          mov     gs, ax

     ;----------------------------------------------------
     ; create stack
     ;----------------------------------------------------
     
          mov     ax, 0x0000				; set the stack
          mov     ss, ax
          mov     sp, 0xFFFF
          sti						; restore interrupts

     ;----------------------------------------------------
     ; Display loading message
     ;----------------------------------------------------
     
          mov     si, msgLoading
          call    Print
          
     ;----------------------------------------------------
     ; Load root directory table
     ;----------------------------------------------------

     LOAD_ROOT:
     
     ; compute size of root directory and store in "cx"
     
          xor     cx, cx
          xor     dx, dx
          mov     ax, 0x0020                           ; 32 byte directory entry
          mul     WORD [bpbRootEntries]                ; total size of directory
          div     WORD [bpbBytesPerSector]             ; sectors used by directory
          xchg    ax, cx
          
     ; compute location of root directory and store in "ax"
     
          mov     al, BYTE [bpbNumberOfFATs]            ; number of FATs
          mul     WORD [bpbSectorsPerFAT]               ; sectors used by FATs
          add     ax, WORD [bpbReservedSectors]         ; adjust for bootsector
          mov     WORD [datasector], ax                 ; base of root directory
          add     WORD [datasector], cx
          
     ; read root directory into memory (7C00:0200)
     
          mov     bx, 0x0200                            ; copy root dir above bootcode
          call    ReadSectors

     ;----------------------------------------------------
     ; Find stage 2
     ;----------------------------------------------------

     ; browse root directory for binary image
          mov     cx, WORD [bpbRootEntries]             ; load loop counter
          mov     di, 0x0200                            ; locate first root entry
     .LOOP:
          push    cx
          mov     cx, 0x000B                            ; eleven character name
          mov     si, ImageName                         ; image name to find
          push    di
     rep  cmpsb                                         ; test for entry match
          pop     di
          je      LOAD_FAT
          pop     cx
          add     di, 0x0020                            ; queue next directory entry
          loop    .LOOP
          jmp     FAILURE

     ;----------------------------------------------------
     ; Load FAT
     ;----------------------------------------------------

     LOAD_FAT:
     
     ; save starting cluster of boot image
     
          mov     si, msgCRLF
          call    Print
          mov     dx, WORD [di + 0x001A]
          mov     WORD [cluster], dx                  ; file's first cluster
          
     ; compute size of FAT and store in "cx"
     
          xor     ax, ax
          mov     al, BYTE [bpbNumberOfFATs]          ; number of FATs
          mul     WORD [bpbSectorsPerFAT]             ; sectors used by FATs
          mov     cx, ax

     ; compute location of FAT and store in "ax"

          mov     ax, WORD [bpbReservedSectors]       ; adjust for bootsector
          
     ; read FAT into memory (7C00:0200)

          mov     bx, 0x0200                          ; copy FAT above bootcode
          call    ReadSectors

     ; read image file into memory (0050:0000)
     
          mov     si, msgCRLF
          call    Print
          mov     ax, 0x0050
          mov     es, ax                              ; destination for image
          mov     bx, 0x0000                          ; destination for image
          push    bx

     ;----------------------------------------------------
     ; Load Stage 2
     ;----------------------------------------------------

     LOAD_IMAGE:
     
          mov     ax, WORD [cluster]                  ; cluster to read
          pop     bx                                  ; buffer to read into
          call    ClusterLBA                          ; convert cluster to LBA
          xor     cx, cx
          mov     cl, BYTE [bpbSectorsPerCluster]     ; sectors to read
          call    ReadSectors
          push    bx
          
     ; compute next cluster
     
          mov     ax, WORD [cluster]                  ; identify current cluster
          mov     cx, ax                              ; copy current cluster
          mov     dx, ax                              ; copy current cluster
          shr     dx, 0x0001                          ; divide by two
          add     cx, dx                              ; sum for (3/2)
          mov     bx, 0x0200                          ; location of FAT in memory
          add     bx, cx                              ; index into FAT
          mov     dx, WORD [bx]                       ; read two bytes from FAT
          test    ax, 0x0001
          jnz     .ODD_CLUSTER
          
     .EVEN_CLUSTER:
     
          and     dx, 0000111111111111b               ; take low twelve bits
         jmp     .DONE
         
     .ODD_CLUSTER:
     
          shr     dx, 0x0004                          ; take high twelve bits
          
     .DONE:
     
          mov     WORD [cluster], dx                  ; store new cluster
          cmp     dx, 0x0FF0                          ; test for end of file
          jb      LOAD_IMAGE
          
     DONE:
     
          mov     si, msgCRLF
          call    Print
          push    WORD 0x0050
          push    WORD 0x0000
          retf
          
     FAILURE:
     
          mov     si, msgFailure
          call    Print
          mov     ah, 0x00
          int     0x16                                ; await keypress
          int     0x19                                ; warm boot computer
     
     absoluteSector db 0x00
     absoluteHead   db 0x00
     absoluteTrack  db 0x00
     
     datasector  dw 0x0000
     cluster     dw 0x0000
     ImageName   db "KRNLDR  SYS"
     msgLoading  db "Loading Boot Image ", 0x0D, 0x0A, 0x00
     msgCRLF     db 0x0D, 0x0A, 0x00
     msgProgress db ".", 0x00
     msgFailure  db "ERROR : Press Any Key to Reboot", 0x0A, 0x00
     
          TIMES 510-($-$$) DB 0
          DW 0xAA55
And the stage2.asm

Code: Select all

org 0x0
bits 16

jmp main

;=================================
;             Calls
;=================================

Print:
	lodsb
	or al, al
	jz PrintDone
	mov ah, 0eh
	int 10h
	jmp Print
PrintDone:
	ret

;==================================
;        Second Stage Loader
;==================================

main:
	cli
	push cs
	pop ds
	
	mov si, Msg
	call Print

	cli
	hlt

;====================================
;             Data Section
;====================================

Msg db "Preparing to load operating system...",13,10,0
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: 2 files with floppy image

Post by Mikemk »

I bolded my comments.
feare56 wrote:

Code: Select all

bpbOEM db "Flow OS " [b]; You made the fat12 filesystem?!  Try, "MSWIN4.1"[/b]
...
          mov     ax, 0x7c00				; setup registers [b][wrong][/b] to point to our segment [b]; try 0x7c0[/b]
...
          push    WORD 0x0050 [b]; wth? a simple "call far 0x50:0" doesn't work?[/b]
          push    WORD 0x0000
          retf
And the stage2.asm

Code: Select all

org 0x0 [b]; your starting at the beginning of memory, on top of the ivt?  Up above it looked like you were starting at 0x500 (which is a bad spot to load to anyway)[/b]
...
Print:
	int 10h [b]; so you first overwrite the ivt, then disable interrupts, then call an interrupt?[/b]
...
	cli [b]; no need for disabling interrupts here[/b]
	push cs[b] ; and this is pointless as well.[/b]
	pop ds
...
	call Print [b]; love calling functions that I just broke.[/b]
I have too much patience.
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: 2 files with floppy image

Post by neon »

Hello,

Unfortunately what was posted above aren't errors (well, except "MSWIN4.1" should be used.) Based on the provided image I suspect that the environment isn't configured properly or not recognizing a valid boot image. I recommend single stepping in Bochs and using a hex editor to make sure the disk image is properly formatted and in-tact. It is clear from the provided image the VBR may not be getting executed. If we can verify the VBR gets executed but fails to display anything we could narrow down if the problem is environment related or not.

Out of interest, considering that it worked for previous demo's that lacked a "second stage", do those demo's still work with this current configuration and does this demo work without the "second stage" file copied over? Also, when installing the VBR we recommend against overwriting the BPB installed by the format utility.

*edit: Well, actually, there is an error. In order to prevent a translation by some assemblers of jmp Main, to the short form (thereby missing up the location of the BPB), the VBR should start like this:

Code: Select all

org 0x7c00
bits 16

start: short jmp Main
nop

bpbOEM db "MSWIN4.1"
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: 2 files with floppy image

Post by Mikemk »

@OP, neon would know better than me, since he wrote the tutorial

@neon, running from 0x7c00:0 isn't an error?

@OP again, But other than that, yes, come to think of it most what I put are style errors, not actual errors. PS, some of these bad habits make it harder to debug. Fair warning
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: 2 files with floppy image

Post by Mikemk »

Oh, and neon, I'd just like to say that your tutorial series is excellent.
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
feare56
Member
Member
Posts: 97
Joined: Sun Dec 23, 2012 5:48 pm

Re: 2 files with floppy image

Post by feare56 »

Well in theory shouldn't boot1 display "loading boot image" then it should say "error: press a key to reboot" if it is a problem with stage2? As with the environment i have not installed bochs because of a ton of dependences i have to install and havent gotten that far since qemu worked with previous chapters of the tutorial
Post Reply