Bochs - why won't this work!

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.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Bochs - why won't this work!

Post by pcmattman »

I'm using Bochs so I don't have to restart every time I want to test my OS. I'm using a virtual floppy drive (for speed's sake) with drive letter B:. I've tried formatting it as a MS-DOS Startup Disk, which works properly. However, my operating system just prints out garbled characters and whenever a key is pressed does it again.

Any help would be greatly appreciated.

The Bochs display is below:

Code: Select all

Plex86/Bochs VGABios 0.6a 19 Aug 2006
This VGA/VBE Bios is released under the GNU LGPL

Please visit :
 . http://bochs.sourceforge.net
 . http://www.nongnu.org/vgabios

Bochs VBE Display Adapter enabled

Bochs BIOS - build: 08/11/06
$Revision: 1.166 $ $Date: 2006/08/11 17:34:12 $
Options: apmbios pcibios eltorito


Booting from Floppy...
0ÀˆFú°ˆFù¸Booting from Floppy...
0ÀˆFú°ˆFù¸Booting from Floppy...
0ÀˆFú°ˆFù¸Booting from Floppy...
0ÀˆFú°ˆFù¸
Last edited by pcmattman on Mon Jan 15, 2007 6:56 pm, edited 1 time in total.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

Wow, Thats such a detailed error report.

One second while we enable our ESP psychic powers to know what you're talking about. :roll:
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

I realized that and just as you posted I edited the first post and put up the output. Is there anything else you need me to post?
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

Well, It's pretty obvious there is an error in your boot loader...

Posting the source and asking someone to help or reviewing the code yourself is advisable.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

I'm not 100% sure about that, as everything boots properly when I physically restart the computer and load the OS off the hardware (not virtual) floppy...

I'm thinking it might be a conflict with the location of the Bochs bios image.

Or could it be a problem with the interrupts I'm using? Does Bochs emulate real-mode behaviour with BIOS interrupts - because the pmode interrupts are likely to be different?

Here is the bootloader code:

Code: Select all

; Mattise Boot Sector
; Assemble with NASM

            bits 16
            org 0x7C00

start:      jmp short begin
            nop
bsOEM       db "Mattise1"               ; OEM String
bsSectSize  dw 512                      ; Bytes per sector
bsClustSize db 1                        ; Sectors per cluster
bsRessect   dw 1                        ; # of reserved sectors
bsFatCnt    db 2                        ; # of fat copies
bsRootSize  dw 224                      ; size of root directory
bsTotalSect dw 2880                     ; total # of sectors if < 32 meg
bsMedia     db 0xF0                     ; Media Descriptor
bsFatSize   dw 9                        ; Size of each FAT
bsTrackSect dw 18                       ; Sectors per track
bsHeadCnt   dw 2                        ; number of read-write heads
bsHidenSect dd 0                        ; number of hidden sectors
bsHugeSect  dd 0                        ; if bsTotalSect is 0 this value is
                                        ; the number of sectors
bsBootDrv   db 0                        ; holds drive that the bs came from
bsReserv    db 0                        ; not used for anything
bsBootSign  db 29h                      ; boot signature 29h
bsVolID     dd 0                        ; Disk volume ID also used for temp
                                        ; sector # / # sectors to load
bsVoLabel   db "MATTISEOS  "            ; Volume Label
bsFSType    db "FAT12   "               ; File System type

begin:      cli                         ; disable interrupts
            mov [bsBootDrv],dl          ; save drive number
            mov ax,0x9000               ; put stack at 0x98000
            mov ss,ax
            mov sp,0x8000

            mov cx,[bsTrackSect]        ; update int 1E FDC param table
            mov bx,0x0078
            lds si,[ds:bx]
            mov byte [si+4], cl
            mov byte [si+9], 0x0F

            sti                         ; enable interrupts
            push ds
            mov dl,[bsBootDrv]          ; reset controller
            xor ax,ax
            int 0x13
            pop ds
            jc bootfail2                ; display error message
            jmp _l1
bootfail2:  jmp bootfail
_l1:
            mov ax,0x0000
            mov es,ax
            mov ds,ax

            mov si,MsgLoad              ; display load message
            call putstr

            ; find the root directory

            xor ax,ax
            mov al,[bsFatCnt]
            mov bx,[bsFatSize]
            mul bx
            add ax,word [bsHidenSect]
            adc ax,word [bsHidenSect+2]
            add ax,word [bsRessect]     ; ax holds root directory location
            mov word [BootSig],ax

            call checkroot

            xor ax,ax
            add ax,word [start]
            add ax,word [bsVolID]       ; sector number
            add ax,word [BootSig]
            sub ax,2                    ; correction for a mis-calc
            mov cx,word [bsVolID+2]     ; number of sectors

            mov bx,0x8000
            mov es,bx


nextsector: push ax                     ; save registers
            push cx
            push dx
            push es

            xor bx,bx                   ; set zero offset
            call readsect               ; read a sector

            mov si,MsgDot               ; display a dot
            call putstr

            pop es                      ; restore registers
            pop dx
            pop cx
            pop ax
            mov bx,es
            add bx,20h                  ; increment address 512 bytes
            mov es,bx
            inc ax                      ; read next sector
            loopnz nextsector

            mov ax,0x8000               ; set segment registers and jump
            mov es,ax
            mov ds,ax
            push ax
            mov ax,0
            push ax
            retf

checkroot:
            push ax                     ; save registers
            push bx
            push cx
            push dx
            push si
            push di
            
            mov ax,0x8000               ; put root directory at 0x80000
            mov es,ax
            mov ax,32                   ; AX = ((32*RootSize)/512) + 2
            mul word [bsRootSize]
            div word [bsSectSize]
            mov cx,ax                   ; cx holds # of sectors in root
            mov word [start],ax
            mov ax,word [BootSig]       ; get prev. saved loc. for root dir

r1:         xor bx,bx
            push cx                     ; save count
            push ax                     ; save sector number
            push es
            push dx
            call readsect
            xor bx,bx
l_1:        mov di,bx                   ; set address to check from
            mov cx,11                   ; check 11 bytes
            mov si,FileName             ; address of string to check with
            repz cmpsb
            je foundit
            add bx,32                   ; check next entry
            cmp bx,[bsSectSize]         ; end of sector?
            je l_2
            jmp l_1
l_2:        pop dx                      ; restore registers
            pop es
            pop ax
            pop cx
            inc ax                      ; read next sector
            loopnz r1
            jmp bootfail
foundit:    pop dx                      ; get these off the stack
            pop es
            pop ax
            pop cx

            mov di,0x1A                 ; get clustor #
            add di,bx
            push bx                     ; save bx for finding # of sectors
            mov ax,[es:di]
            xor bx,bx                   ; calculate sector #
            mov bl,[bsClustSize]
            mul bx                      ; ax holds sector #
            mov word [bsVolID],ax

            pop bx                      ; get location of directory entry
            mov di,0x1C
            add di,bx
            mov ax,[es:di]              ; put number of bytes in ax
            xor dx,dx
            mov bx,[bsClustSize]        ; # of bytes / 512
            div bx
            inc ax
            mov word [bsVolID+2],ax     ; save number of sectors to load

            pop di                      ; restore registers
            pop si
            pop dx
            pop cx
            pop bx
            pop ax
            
            ret                         ; return to caller
            
putstr:     ; SI = address of string to display
            lodsb
            or al,al
            jz short putstrd
            mov ah,0x0E
            mov bx,0x0007
            int 0x10
            jmp putstr
putstrd:    retn                        ; return to caller

bootfail:   ; display failure message
            mov si,MsgBad               ; display error message
            call putstr
            xor ax,ax                   ; wait for keypress
            int 0x16
            int 0x19                    ; reboot

readsect:   ; ES:BX = Location ; AX = Sector
            mov si,[bsTrackSect]
            div si                      ; divide logical sect by track size
            inc dl                      ; sector # begins at 1
            mov [bsReserv],dl           ; sector to read
            xor dx,dx                   ; logical track left in ax
            div word [bsHeadCnt]        ; leaves head in dl, cyl in ax
            mov dh, [bsBootDrv]         ;
            xchg dl,dh                  ; head to dh, drive to dl
            mov cx,ax                   ; cyl to cx
            xchg cl,ch                  ; low 8 bits of cyl to ch, hi 2 bits
            shl cl,6                    ; shifted to bits 6 and 7
            or cl, byte [bsReserv]      ; or with sector number
            mov al,1                    ; number of sectors
            mov ah,2                    ; use read function of int 0x13
            int 0x13                    ; read sector
            jc bootfail                 ; display error message
            ret                         ; return to caller

FileName    db "MATTISE COM"
MsgBad      db "Could not boot!",13,10,0
MsgDot      db ".",0
Newline	    db 13,10,0
MsgLoad     db "Mattise Loading...",13,10,"Reading files",0
padding     times 22 db 0
BootSig     db 0x55, 0xAA
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

Maybe you should try it with QEMU to see if it's a problem with Bochs, or your code.
C8H10N4O2 | #446691 | Trust the nodes.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Ok... I looked into QEMU and there didn't seem to be much in the way of Windows binaries or source.

What I'm doing now is writing a simple bootloader to find where the problem was. The code for the current bootloader is below (NASM):

Code: Select all

bits 16
org 0x7C00

start:
	cli
	mov ax,0x9000               ; put stack at 0x98000
	mov ss,ax
	mov sp,0x8000
	sti

	mov ax,0x0000			; set segment regs
	mov es,ax
	mov ds,ax

	hang:				; infinite loop
	jmp hang

padding times 491 db 0
BootSig db 0x55, 0xAA
Bochs fails with the following text in the log:

Code: Select all

00001082311p[CPU0 ] >>PANIC<< PUSHAD: eSP < 16
00001082311i[SYS  ] Last time is 1168918235
00001082311i[CPU0 ] real mode
00001082311i[CPU0 ] CS.d_b = 16 bit
00001082311i[CPU0 ] SS.d_b = 16 bit
00001082311i[CPU0 ] | EAX=00000004  EBX=00000000  ECX=0000fef1  EDX=00000000
00001082311i[CPU0 ] | ESP=00000001  EBP=00000000  ESI=00002cfc  EDI=00000004
00001082311i[CPU0 ] | IOPL=0 id vip vif ac vm rf nt of DF IF tf sf zf af pf cf
00001082311i[CPU0 ] | SEG selector     base    limit G D
00001082311i[CPU0 ] | SEG sltr(index|ti|rpl)     base    limit G D
00001082311i[CPU0 ] |  CS:0000( 1e00| 0|  0) 00000000 0000ffff 0 0
00001082311i[CPU0 ] |  DS:0000( 0000| 0|  0) 00000000 0000ffff 0 0
00001082311i[CPU0 ] |  SS:0000( 0000| 0|  0) 00000000 0000ffff 0 0
00001082311i[CPU0 ] |  ES:0000( 0000| 0|  0) 00000000 0000ffff 0 0
00001082311i[CPU0 ] |  FS:0000( 0000| 0|  0) 00000000 0000ffff 0 0
00001082311i[CPU0 ] |  GS:0000( 0000| 0|  0) 00000000 0000ffff 0 0
00001082311i[CPU0 ] | EIP=0000fd37 (0000fd36)
00001082311i[CPU0 ] | CR0=0x00000010 CR1=0 CR2=0x00000000
00001082311i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
00001082311i[CPU0 ] >> pusha  : 60
m
Member
Member
Posts: 67
Joined: Sat Nov 25, 2006 6:33 am
Location: PRC

Post by m »

Hi.

The code works fine for Bochs on my machine.

In bochsrc.txt I wrote:

Code: Select all

....
floppya: 1_44=out,status=inserted
#out is the file I use as the boot sector
....
boot:a
....
And also in bochsout.txt I found nothing wrong.
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

It might be your version of bochs. I have heard of some bochs handling pusha wrong....

Edit: Just tested the code above in QEMU. All goes well. (As in, it just sits there.)
Last edited by Alboin on Mon Jan 15, 2007 10:14 pm, edited 1 time in total.
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

Why don't people ever google?

http://www.h7.dion.ne.jp/~qemu-win/

They made Windows binaries, QEMU on windows isn't exactly the best though.. but It's still a good idea to test your code in various emulations and real hardware.

Good luck.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Shame on me, I overlooked the link to QEMU's windows version... I am now in the process of staring blankly at my screen while wondering why I didn't click on all the links I could in an effort to find it...

Edit: Thankyou very much for all of your assistance, I have now got it working.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Hi,

This may save you some time - you seem to be manually putting in the 'padding' value on every version of your boot loader. If you use a macro like:

Code: Select all

times 510-($-$$) db 0
...and then put the boot sig in, you will not have to keep updating your code.

Cheers,
Adam
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:

Post by Combuster »

I get the idea that bochs is looking at the wrong floppy data. Have you checked that:
- You indeed wrote your bootloader to the correct location on the floppy
- That you are referencing the correct data from bochs

For the first you need to load the floppy into an image and check its contents.
Regarding the second, you should check your bochsrc.

In any case, i dont really recommend using bochs with a real floppy, I prefer disk images.
"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 ]
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

Combuster wrote:I get the idea that bochs is looking at the wrong floppy data. Have you checked that:
- You indeed wrote your bootloader to the correct location on the floppy
- That you are referencing the correct data from bochs

For the first you need to load the floppy into an image and check its contents.
Regarding the second, you should check your bochsrc.

In any case, i dont really recommend using bochs with a real floppy, I prefer disk images.
I agree Combuster..

In regards to pcmattman, I don't see how using a real floppy drive is better than an image. (It's not very reliable media..) Most emulators are able to load floppy images a bit faster then a real drive.

But yes, Be sure the file is 512bytes and you are writing it to the first 512byes of the floppy image.
Last edited by Brynet-Inc on Tue Jan 16, 2007 11:46 am, edited 1 time in total.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
Tyler
Member
Member
Posts: 514
Joined: Tue Nov 07, 2006 7:37 am
Location: York, England

Post by Tyler »

Brynet-Inc wrote:
Combuster wrote:I get the idea that bochs is looking at the wrong floppy data. Have you checked that:
- You indeed wrote your bootloader to the correct location on the floppy
- That you are referencing the correct data from bochs

For the first you need to load the floppy into an image and check its contents.
Regarding the second, you should check your bochsrc.

In any case, i dont really recommend using bochs with a real floppy, I prefer disk images.
Never considered that :P, In any sense I don't see how using a real floppy drive is better than an image. (It's not very reliable media..) Most emulators are able to load floppy images a bit faster then a real drive.

But yes, Be sure the file is 512bytes and you are writing it to the first 512byes of the floppy image.
He said images are better than real... so now we all argue to agree as well?
Post Reply