FAT floppy loading code causes panic in bochs

Programming, for all ages and all languages.
Post Reply
thestew42
Member
Member
Posts: 38
Joined: Thu Jun 19, 2008 6:20 pm
Location: San Diego, CA

FAT floppy loading code causes panic in bochs

Post by thestew42 »

This is really maddening. So until I started this OS project, I had never touched assembly which was definitely a mistake. Anyway, I needed to write a boot loader in assembly so I looked up some tutorials and tried to build one off of example code (another mistake). Now I have a first stage boot loader that works fine and loads the second stage without problems. So I started to work on the part of the second stage boot loader that loads the kernel. Naturally, being an assembly novice, I just copied the FAT code from my fist stage loader and changed the file name that it looks for. It seems like it should work because the first stage does, but it doesn't; the code crashed bochs with this error

Code: Select all

00000870863p[BIOS ] >>PANIC<< BIOS panic at rombios.c, line 7609
00000870863i[SYS  ] Last time is 1228337342
00000870863i[CPU  ] real mode
00000870863i[CPU  ] CS.d_b = 16 bit
00000870863i[CPU  ] SS.d_b = 16 bit
00000870863i[CPU  ] | EAX=000f1db9  EBX=0000f600  ECX=000f0001  EDX=00000400
00000870863i[CPU  ] | ESP=0000ffef  EBP=00000000  ESI=00000899  EDI=00000000
00000870863i[CPU  ] | IOPL=0 NV UP DI PL ZR NA PE NC
00000870863i[CPU  ] | SEG selector     base    limit G D
00000870863i[CPU  ] | SEG sltr(index|ti|rpl)     base    limit G D
00000870863i[CPU  ] |  DS:0000( 0000| 0|  0) 00000000 0000ffff 0 0
00000870863i[CPU  ] |  ES:0090( 0000| 0|  0) 00000900 0000ffff 0 0
00000870863i[CPU  ] |  FS:07c0( 0000| 0|  0) 00007c00 0000ffff 0 0
00000870863i[CPU  ] |  GS:07c0( 0000| 0|  0) 00007c00 0000ffff 0 0
00000870863i[CPU  ] |  SS:0000( 0000| 0|  0) 00000000 0000ffff 0 0
00000870863i[CPU  ] |  CS:f000( 0000| 0|  0) 000f0000 0000ffff 0 0
00000870863i[CPU  ] | EIP=00008e9f (00008e9e)
00000870863i[CPU  ] | CR0=0x60000010 CR1=0x00000000 CR2=0x00000000
00000870863i[CPU  ] | CR3=0x00000000 CR4=0x00000000
00000870863i[     ] restoring default signal behavior
00000870863i[CTRL ] quit_sim called with exit code 1
That's all the information that is given. So I have no idea why this won't work. What exactly causes that error, and what can I try? Thanks for any suggestions.
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: FAT floppy loading code causes panic in bochs

Post by Combuster »

1) You can still use grub
2) What are the lines preceding the panic
3) What version of bochs are you using - most likely not the latest stable
"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 ]
thestew42
Member
Member
Posts: 38
Joined: Thu Jun 19, 2008 6:20 pm
Location: San Diego, CA

Re: FAT floppy loading code causes panic in bochs

Post by thestew42 »

1) I know, but I like to understand what all of the code is doing and how it is doing it.
2) I didn't post the lines above the panic because they seem trivial to me. They are present even when bochs does not crash and the code executes without error. But here's the whole log:

Code: Select all

00000000000i[MEM0 ] allocated memory at 01360020. after alignment, vector=01361000
00000000000i[MEM0 ] 4.00MB
00000000000i[MEM0 ] rom at 0xf0000/65536 ('bios/BIOS-bochs-latest')
00000000000i[MEM0 ] rom at 0xc0000/32769 ('bios/VGABIOS-elpin-2.40')
00000000000i[CMOS ] Setting initial clock to: Wed Dec 03 15:49:01 2008 (time0=1228337341)
00000000000i[DMA  ] channel 4 used by cascade
00000000000i[DMA  ] channel 2 used by Floppy Drive
00000000000i[FDD  ] fd0: 'floppy.img' ro=0, h=2,t=80,spt=18
00000000000i[VGA  ] interval=30000
00000000000i[VGA  ] VBE Bochs Display Extension Enabled
00000000000i[     ] init_mem of 'harddrv' plugin device by virtual method
00000000000i[     ] init_mem of 'keyboard' plugin device by virtual method
00000000000i[     ] init_mem of 'serial' plugin device by virtual method
00000000000i[     ] init_mem of 'parallel' plugin device by virtual method
00000000000i[     ] init_dev of 'harddrv' plugin device by virtual method
00000000000i[HD   ] Boot device will be 'a'
00000000000i[HD   ] Floppy boot signature check is enabled
00000000000i[     ] init_dev of 'keyboard' plugin device by virtual method
00000000000i[KBD  ] will paste characters every 1000 keyboard ticks
00000000000i[     ] init_dev of 'serial' plugin device by virtual method
00000000000i[SER  ] com1 at 0x3f8/8 irq 4
00000000000i[     ] init_dev of 'parallel' plugin device by virtual method
00000000000i[PAR  ] parallel port 1 at 0x378
00000000000i[     ] reset of 'harddrv' plugin device by virtual method
00000000000i[     ] reset of 'keyboard' plugin device by virtual method
00000000000i[     ] reset of 'serial' plugin device by virtual method
00000000000i[     ] reset of 'parallel' plugin device by virtual method
00000004256i[BIOS ]  rombios.c,v 1.85.2.1 2003/01/16 21:58:42 cbothamy Exp $
00000330043i[KBD  ] reset-disable command received
00000506792e[HD   ] device set to 0 which does not exist
00000507085e[HD   ] device set to 1 which does not exist
00000870863p[BIOS ] >>PANIC<< BIOS panic at rombios.c, line 7609
00000870863i[SYS  ] Last time is 1228337342
00000870863i[CPU  ] real mode
00000870863i[CPU  ] CS.d_b = 16 bit
00000870863i[CPU  ] SS.d_b = 16 bit
00000870863i[CPU  ] | EAX=000f1db9  EBX=0000f600  ECX=000f0001  EDX=00000400
00000870863i[CPU  ] | ESP=0000ffef  EBP=00000000  ESI=00000899  EDI=00000000
00000870863i[CPU  ] | IOPL=0 NV UP DI PL ZR NA PE NC
00000870863i[CPU  ] | SEG selector     base    limit G D
00000870863i[CPU  ] | SEG sltr(index|ti|rpl)     base    limit G D
00000870863i[CPU  ] |  DS:0000( 0000| 0|  0) 00000000 0000ffff 0 0
00000870863i[CPU  ] |  ES:0090( 0000| 0|  0) 00000900 0000ffff 0 0
00000870863i[CPU  ] |  FS:07c0( 0000| 0|  0) 00007c00 0000ffff 0 0
00000870863i[CPU  ] |  GS:07c0( 0000| 0|  0) 00007c00 0000ffff 0 0
00000870863i[CPU  ] |  SS:0000( 0000| 0|  0) 00000000 0000ffff 0 0
00000870863i[CPU  ] |  CS:f000( 0000| 0|  0) 000f0000 0000ffff 0 0
00000870863i[CPU  ] | EIP=00008e9f (00008e9e)
00000870863i[CPU  ] | CR0=0x60000010 CR1=0x00000000 CR2=0x00000000
00000870863i[CPU  ] | CR3=0x00000000 CR4=0x00000000
00000870863i[     ] restoring default signal behavior
00000870863i[CTRL ] quit_sim called with exit code 1
3) Probably not: 2.0.2. I can try 2.3.7
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: FAT floppy loading code causes panic in bochs

Post by Combuster »

thestew42 wrote:3) Probably not: 2.0.2. I can try 2.3.7
Not just try it, forget about 2.0.2 altogether. That saves you from the many omissions and bugs in the older versions, and us from getting confused over weird messages that do not exist :wink:
"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 ]
thestew42
Member
Member
Posts: 38
Joined: Thu Jun 19, 2008 6:20 pm
Location: San Diego, CA

Re: FAT floppy loading code causes panic in bochs

Post by thestew42 »

Well... not much better. I installed 2.3.7 and it loads the second stage, gets to the part where its supposed to load the kernel, then crashes. It happens while loading the sectors of the file. Now it tells me that there "is no bootable device", which makes no sense because it already booted :roll:

This is the log:

Code: Select all

00000000000i[     ] Bochs x86 Emulator 2.3.7
00000000000i[     ]   Build from CVS snapshot, on June 3, 2008
00000000000i[     ] System configuration
00000000000i[     ]   processors: 1 (cores=1, HT threads=1)
00000000000i[     ]   A20 line support: yes
00000000000i[     ]   APIC support: yes
00000000000i[     ] CPU configuration
00000000000i[     ]   level: 6
00000000000i[     ]   SMP support: no
00000000000i[     ]   FPU support: yes
00000000000i[     ]   MMX support: yes
00000000000i[     ]   SSE support: 2
00000000000i[     ]   CLFLUSH support: yes
00000000000i[     ]   VME support: yes
00000000000i[     ]   3dnow! support: no
00000000000i[     ]   PAE support: yes
00000000000i[     ]   PGE support: yes
00000000000i[     ]   PSE support: yes
00000000000i[     ]   x86-64 support: yes
00000000000i[     ]   SEP support: yes
00000000000i[     ]   MWAIT support: no
00000000000i[     ]   XSAVE support: no
00000000000i[     ]   AES support: no
00000000000i[     ] Optimization configuration
00000000000i[     ]   Guest2HostTLB support: yes
00000000000i[     ]   RepeatSpeedups support: yes
00000000000i[     ]   Icache support: yes
00000000000i[     ]   Trace cache support: yes
00000000000i[     ]   Fast function calls: yes
00000000000i[     ] Devices configuration
00000000000i[     ]   ACPI support: yes
00000000000i[     ]   NE2000 support: yes
00000000000i[     ]   PCI support: yes
00000000000i[     ]   SB16 support: yes
00000000000i[     ]   USB support: yes
00000000000i[     ]   VGA extension support: vbe cirrus
00000000000i[MEM0 ] allocated memory at 024B0020. after alignment, vector=024B1000
00000000000i[MEM0 ] 32.00MB
00000000000i[MEM0 ] rom at 0xfffe0000/131072 ('C:\Program Files\Bochs-2.3.7/BIOS-bochs-latest')
00000000000i[MEM0 ] rom at 0xc0000/38400 ('C:\Program Files\Bochs-2.3.7/VGABIOS-lgpl-latest')
00000000000i[APIC?] set APIC ID to 0
00000000000i[APIC0] 80686
00000000000i[APIC0] local apic in CPU apicid=00 initializing
00000000000i[IOAP ] initializing I/O APIC
00000000000i[IOAP ] set APIC ID to 1
00000000000i[MEM0 ] Register memory access handlers: 0xfec00000 - 0xfec00fff
00000000000i[CMOS ] Using local time for initial clock
00000000000i[CMOS ] Setting initial clock to: Wed Dec 03 20:46:47 2008 (time0=1228355207)
00000000000i[DMA  ] channel 4 used by cascade
00000000000i[DMA  ] channel 2 used by Floppy Drive
00000000000i[FDD  ] fd0: 'floppy.img' ro=0, h=2,t=80,spt=18
00000000000i[PCI  ] 440FX Host bridge present at device 0, function 0
00000000000i[PCI  ] PIIX3 PCI-to-ISA bridge present at device 1, function 0
00000000000i[MEM0 ] Register memory access handlers: 0x000a0000 - 0x000bffff
00000000000i[WGUI ] Desktop Window dimensions: 1680 x 1050
00000000000i[WGUI ] Number of Mouse Buttons = 8
00000000000i[WGUI ] IME disabled
00000000000i[MEM0 ] Register memory access handlers: 0xe0000000 - 0xe07fffff
00000000000i[CLVGA] VBE Bochs Display Extension Enabled
00000000000i[CLVGA] interval=300000
00000000000i[     ] init_mem of 'harddrv' plugin device by virtual method
00000000000i[     ] init_mem of 'keyboard' plugin device by virtual method
00000000000i[     ] init_mem of 'serial' plugin device by virtual method
00000000000i[     ] init_mem of 'parallel' plugin device by virtual method
00000000000i[     ] init_mem of 'extfpuirq' plugin device by virtual method
00000000000i[     ] init_mem of 'gameport' plugin device by virtual method
00000000000i[     ] init_mem of 'speaker' plugin device by virtual method
00000000000i[     ] init_mem of 'pci_ide' plugin device by virtual method
00000000000i[     ] init_mem of 'acpi' plugin device by virtual method
00000000000i[     ] init_dev of 'harddrv' plugin device by virtual method
00000000000i[HD   ] Using boot sequence floppy, none, none
00000000000i[HD   ] Floppy boot signature check is enabled
00000000000i[     ] init_dev of 'keyboard' plugin device by virtual method
00000000000i[KBD  ] will paste characters every 1000 keyboard ticks
00000000000i[     ] init_dev of 'serial' plugin device by virtual method
00000000000i[SER  ] com1 at 0x03f8 irq 4
00000000000i[     ] init_dev of 'parallel' plugin device by virtual method
00000000000i[PAR  ] parallel port 1 at 0x0378 irq 7
00000000000i[     ] init_dev of 'extfpuirq' plugin device by virtual method
00000000000i[     ] init_dev of 'gameport' plugin device by virtual method
00000000000i[     ] init_dev of 'speaker' plugin device by virtual method
00000000000i[     ] init_dev of 'pci_ide' plugin device by virtual method
00000000000i[PCI  ] PIIX3 PCI IDE controller present at device 1, function 1
00000000000i[     ] init_dev of 'acpi' plugin device by virtual method
00000000000i[PCI  ] ACPI Controller present at device 1, function 3
00000000000i[     ] register state of 'harddrv' plugin device by virtual method
00000000000i[     ] register state of 'keyboard' plugin device by virtual method
00000000000i[     ] register state of 'serial' plugin device by virtual method
00000000000i[     ] register state of 'parallel' plugin device by virtual method
00000000000i[     ] register state of 'extfpuirq' plugin device by virtual method
00000000000i[     ] register state of 'gameport' plugin device by virtual method
00000000000i[     ] register state of 'speaker' plugin device by virtual method
00000000000i[     ] register state of 'pci_ide' plugin device by virtual method
00000000000i[     ] register state of 'acpi' plugin device by virtual method
00000000000i[SYS  ] bx_pc_system_c::Reset(HARDWARE) called
00000000000i[CPU0 ] cpu hardware reset
00000000000i[APIC0] local apic in CPU 0 initializing
00000000000i[     ] reset of 'harddrv' plugin device by virtual method
00000000000i[     ] reset of 'keyboard' plugin device by virtual method
00000000000i[     ] reset of 'serial' plugin device by virtual method
00000000000i[     ] reset of 'parallel' plugin device by virtual method
00000000000i[     ] reset of 'extfpuirq' plugin device by virtual method
00000000000i[     ] reset of 'gameport' plugin device by virtual method
00000000000i[     ] reset of 'speaker' plugin device by virtual method
00000000000i[     ] reset of 'pci_ide' plugin device by virtual method
00000000000i[     ] reset of 'acpi' plugin device by virtual method
00000003302i[BIOS ] $Revision: 1.209 $ $Date: 2008/06/02 20:08:10 $
00000320071i[KBD  ] reset-disable command received
00000443645i[VBIOS] VGABios $Id: vgabios.c,v 1.67 2008/01/27 09:44:12 vruppert Exp $

00000443716i[CLVGA] VBE known Display Interface b0c0
00000443748i[CLVGA] VBE known Display Interface b0c4
00000446673i[VBIOS] VBE Bios $Id: vbe.c,v 1.60 2008/03/02 07:47:21 vruppert Exp $
00000769674i[BIOS ] Starting rombios32
00000770501i[BIOS ] ram_size=0x02000000
00000870157i[BIOS ] Found 1 cpu(s)
00000886458i[BIOS ] bios_table_addr: 0x000fb778 end=0x000fcc00
00000886525i[PCI  ] 440FX PMC write to PAM register 59 (TLB Flush)
00001345332i[PCI  ] 440FX PMC write to PAM register 59 (TLB Flush)
00001804819i[P2I  ] PCI IRQ routing: PIRQA# set to 0x0b
00001804866i[P2I  ] PCI IRQ routing: PIRQB# set to 0x09
00001804913i[P2I  ] PCI IRQ routing: PIRQC# set to 0x0b
00001804960i[P2I  ] PCI IRQ routing: PIRQD# set to 0x09
00001804976i[P2I  ] write: ELCR2 = 0x0a
00001805913i[BIOS ] PIIX3 init: elcr=00 0a
00001826168i[BIOS ] PCI: bus=0 devfn=0x00: vendor_id=0x8086 device_id=0x1237
00001829405i[BIOS ] PCI: bus=0 devfn=0x08: vendor_id=0x8086 device_id=0x7000
00001832138i[BIOS ] PCI: bus=0 devfn=0x09: vendor_id=0x8086 device_id=0x7010
00001832599i[PIDE ] new BM-DMA address: 0xc000
00001833482i[BIOS ] region 4: 0x0000c000
00001836058i[BIOS ] PCI: bus=0 devfn=0x0b: vendor_id=0x8086 device_id=0x7113
00001836558i[ACPI ] new irq line = 11
00001836595i[ACPI ] new PM base address: 0xb000
00001836657i[ACPI ] new SM base address: 0xb100
00001837125i[CPU0 ] Enter to System Management Mode
00001837135i[CPU0 ] RSM: Resuming from System Management Mode
00001837169i[PCI  ] setting SMRAM control register to 0x4a
00001837451i[PCI  ] setting SMRAM control register to 0x0a
00001860732i[BIOS ] MP table addr=0x000fb850 MPC table addr=0x000fb780 size=0xd0
00001862935i[BIOS ] SMBIOS table addr=0x000fb860
00001865979i[BIOS ] ACPI tables: RSDP addr=0x000fb970 ACPI DATA addr=0x01ff0000 size=0x9d8
00001885512i[PCI  ] 440FX PMC write to PAM register 59 (TLB Flush)
00001886524i[BIOS ] bios_table_cur_addr: 0x000fb994
00003000000i[WGUI ] dimension update x=720 y=400 fontheight=16 fontwidth=9 bpp=8
00032315724i[BIOS ] Booting from 0000:7c00
00053914789p[BIOS ] >>PANIC<< No bootable device.
00053914789i[CPU0 ] CPU is in real mode (active)
00053914789i[CPU0 ] CS.d_b = 16 bit
00053914789i[CPU0 ] SS.d_b = 16 bit
00053914789i[CPU0 ] EFER   = 0x00000000
00053914789i[CPU0 ] | RAX=000000000000040a  RBX=000000000000cd04
00053914789i[CPU0 ] | RCX=0000000000000004  RDX=0000000000000402
00053914789i[CPU0 ] | RSP=000000000000ffa8  RBP=000000000000ffac
00053914789i[CPU0 ] | RSI=00000000ffff08d4  RDI=0000000000080000
00053914789i[CPU0 ] |  R8=0000000000000000   R9=0000000000000000
00053914789i[CPU0 ] | R10=0000000000000000  R11=0000000000000000
00053914789i[CPU0 ] | R12=0000000000000000  R13=0000000000000000
00053914789i[CPU0 ] | R14=0000000000000000  R15=0000000000000000
00053914789i[CPU0 ] | IOPL=0 id vip vif ac vm rf nt of df if tf sf ZF af PF cf
00053914789i[CPU0 ] | SEG selector     base    limit G D
00053914789i[CPU0 ] | SEG sltr(index|ti|rpl)     base    limit G D
00053914789i[CPU0 ] |  CS:f000( 0004| 0|  0) 000f0000 0000ffff 0 0
00053914789i[CPU0 ] |  DS:0000( 0005| 0|  0) 00000000 0000ffff 0 0
00053914789i[CPU0 ] |  SS:0000( 0005| 0|  0) 00000000 0000ffff 0 0
00053914789i[CPU0 ] |  ES:0090( 0005| 0|  0) 00000900 0000ffff 0 0
00053914789i[CPU0 ] |  FS:07c0( 0005| 0|  0) 00007c00 0000ffff 0 0
00053914789i[CPU0 ] |  GS:07c0( 0005| 0|  0) 00007c00 0000ffff 0 0
00053914789i[CPU0 ] |  MSR_FS_BASE:0000000000007c00
00053914789i[CPU0 ] |  MSR_GS_BASE:0000000000007c00
00053914789i[CPU0 ] | RIP=0000000000000560 (000000000000055f)
00053914789i[CPU0 ] | CR0=0x60000010 CR1=0x0 CR2=0x0000000000000000
00053914789i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
00053914789i[CPU0 ] >> out dx, al : EE
00053914789i[CMOS ] Last time is 1228355212 (Wed Dec 03 20:46:52 2008)
00053914789i[     ] restoring default signal behavior
00053914789i[CTRL ] quit_sim called with exit code 1
And thanks combuster for your help.
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: FAT floppy loading code causes panic in bochs

Post by Combuster »

From what I gathered, you're calling int19 - why, and can you show some code.

And please do enable the boot signature check if only to force you to write something that dosn't work on bochs only.
"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 ]
tantrikwizard
Member
Member
Posts: 153
Joined: Sun Jan 07, 2007 9:40 am
Contact:

Re: FAT floppy loading code causes panic in bochs

Post by tantrikwizard »

thestew42 wrote:Well... not much better.
IMO the only reason to use bochs is for the debugger. Learn about the debugger commands, specifically break points, register, cpu, stack and memory inspection. Set a break point on the method thats crashing and step through it to discover what is going on. Post the code that isnt working to get more accurate answers, we cant very well know what is happening without the code.
thestew42
Member
Member
Posts: 38
Joined: Thu Jun 19, 2008 6:20 pm
Location: San Diego, CA

Re: FAT floppy loading code causes panic in bochs

Post by thestew42 »

I call int 0x19 only if something fails. When I do, I print a message and wait for a keypress to reboot, so from what I see when I run bochs, it isn't calling int 0x19. I do call int 0x18 in the read sector code. When I take that out, the program freezes, but doesn't give the boot device error that I was getting. Of course, by removing that code I probably just introduced a new error, so the response is probably completely unrelated. Here's the code with working parts omitted:

Code: Select all

[BITS 16]      ;16 bit code generation
[ORG 0x500]

start:
	;Adjust data segment
	cli
	xor ax, ax
	mov ds, ax
	
	;Print status message
	mov si, msgLoading
	call WriteMessage
	
	;Check for compatible processor
	call detect_cpu
	
	;Enable A20 Line
	call enableA20
	
	;Reset the disk
	mov     ah, 0                   ;0050:000b 0x50b
	mov     dl, BYTE [DriveNumber]
    int     0x13
	
	;Load the kernel
	call loadkernel
	
	;Enter protected mode
	cli                     ; Disable interrupts, we want to be alone

    xor ax, ax
    mov ds, ax              ; Set DS-register to 0 - used by lgdt

    lgdt [gdt_desc]         ; Load the GDT descriptor

    mov eax, cr0            ; Copy the contents of CR0 into EAX
    or eax, 1               ; Set bit 0
    mov cr0, eax            ; Copy the contents of EAX into CR0

    ;Far jump to 32 bit code
    jmp 08h:clear_pipe

;*********************Load kernel with FAT*****************************
loadkernel:
;****************Begin using FAT file system to find OS Boot Program***********
	
	;compute size of root directory and store in cx
	xor cx, cx
	xor dx, dx
	mov ax, 0x0020				;32 byte directory entry
	mul WORD [MaxRootEntries]	;total size of directory
	div WORD [BytesPerSector]	;sectors used by directory
	xchg ax, cx
	
	;compute location of root directory and store in ax
	mov al, BYTE [TotalFATs]
	mul WORD [SectorsPerFAT]
	add ax, WORD [ReservedSectors]
	mov WORD [datasector], ax
	add WORD [datasector], cx
	
	;read root directory into memory (7C00:0200) above boot code
	mov bx, 0x1000
	call ReadSectors ;0x54e
	
	;browse root directory for binary image
	mov cx, WORD [MaxRootEntries]
	mov di, 0x1000
	
	.LOOP
		push cx
		mov cx, 0x000B
		mov si, ImageName	;Image name to find
		push di
	rep cmpsb		;Test entry for match
		pop di
		je LOAD_FAT
		pop cx
		add di, 0x0020
		loop .LOOP
		jmp FAILURE
	
	LOAD_FAT:
	; save starting cluster of boot image
     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 [TotalFATs]                ; number of FATs
     mul     WORD [SectorsPerFAT]                ; sectors used by FATs
     mov     cx, ax
	 
	; compute location of FAT and store in "ax"
     mov     ax, WORD [ReservedSectors]          ; adjust for bootsector
	 
	; read FAT into memory (7C00:0200)
     mov     bx, 0x1000                          ; copy FAT above bootcode
     call    ReadSectors
	; read image file into memory (0090:0000)
     mov     si, msgCRLF
     call    WriteMessage
     mov     ax, 0x0090
     mov     es, ax                              ; destination for image
     mov     bx, 0x0000                          ; destination for image
     push    bx
	 
	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 [SectorsPerCluster]        ; 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, 0x1000                          ; 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, msgSuccess
     call    WriteMessage
	 ret
FAILURE:
     mov     si, msgFailure
     call    WriteMessage
     mov     ah, 0x00
     int     0x16                                ; await keypress
     int     0x19                                ; warm boot computer
	
;*****OMITTED CODE********
	
;*************************************************************************
; PROCEDURE ReadSectors
; reads "cx" sectors from disk starting at "ax" into memory location "es:bx"
;*************************************************************************
ReadSectors:
.MAIN
     mov     di, 0x0005                          ; five retries for error
.SECTORLOOP
     push    ax
     push    bx
     push    cx
     call    LBACHS
     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 [DriveNumber]              ; 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
     pop     cx
     pop     bx
     pop     ax
     add     bx, WORD [BytesPerSector]           ; queue next buffer
     inc     ax                                  ; queue next sector
     loop    .MAIN                               ; read next sector
     
     ret

;*************************************************************************
; PROCEDURE ClusterLBA
; convert FAT cluster into LBA addressing scheme
; LBA = (cluster - 2) * sectors per cluster
;*************************************************************************
ClusterLBA:
     sub     ax, 0x0002                          ; zero base cluster number
     xor     cx, cx
     mov     cl, BYTE [SectorsPerCluster]        ; convert byte to word
     mul     cx
     add     ax, WORD [datasector]               ; base data sector
     ret

;*************************************************************************
; PROCEDURE LBACHS
; convert "ax2; LBA addressing scheme to CHS addressing scheme
; 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 [SectorsPerTrack]              ; calculate
     inc     dl                                  ; adjust for sector 0
     mov     BYTE [absoluteSector], dl
     xor     dx, dx                              ; prepare dx:ax for operation
     div     WORD [NumHeads]                     ; calculate
     mov     BYTE [absoluteHead], dl
     mov     BYTE [absoluteTrack], al
     ret

;*************************Protected Mode Code************************
[BITS 32]                       ; We now need 32-bit instructions
clear_pipe:
        mov ax, 10h             ; Save data segment identifyer
        mov ds, ax              ; Move a valid data segment into the data segment register
        mov ss, ax              ; Move a valid data segment into the stack segment register
        mov esp, 090000h        ; Move the stack pointer to 090000h
		
		mov byte [ds:0B8000h], 'P'      ; Move the ASCII-code of 'P' into first video memory
        mov byte [ds:0B8001h], 1Bh      ; Assign a color code

hang:
        jmp hang                ; Loop, self-jump

;**********************Global Descriptor Table***********************
gdt:                    ; Address for the GDT

gdt_null:               ; Null Segment
        dd 0
        dd 0

gdt_code:               ; Code segment, read/execute, nonconforming
        dw 0FFFFh
        dw 0
        db 0
        db 10011010b
        db 11001111b
        db 0

gdt_data:               ; Data segment, read/write, expand down
        dw 0FFFFh
        dw 0
        db 0
        db 10010010b
        db 11001111b
        db 0

gdt_end:                ; Used to calculate the size of the GDT



gdt_desc:                       ; The GDT descriptor
        dw gdt_end - gdt - 1    ; Limit (size)
        dd gdt                  ; Address of the GDT
	
;**********************Our "data section"*****************************
msgLoading	db 13, 10, "Running boot code", 13, 10, 0
msgProcessor db "Checking for compatible CPU", 13, 10, 0
msgProcGood db "Compatible processor found", 13, 10, 0
msgProcBad db "ERROR: No compatible processor. Press any key to reboot", 13, 10, 0
msgA20Warn db "WARNING: Non-standard method used to enable A20", 13, 10, 0
msgA20Succeed db "A20 Line successfully enabled", 13, 10, 0
msgA20Fail db "ERROR: Could not enable A20 line", 13, 10, 0
msgFailure db "ERROR: Failed to load the kernel", 13, 10, 0
msgSuccess db "Loaded kernel", 13, 10, 0
msgTest db "TEST!!!", 13, 10, 0

absoluteSector	db 0x00
absoluteHead	db 0x00
absoluteTrack	db 0x00

datasector  dw 0x0000
cluster     dw 0x0000
msgCRLF		db 13, 10, 0
ImageName   db "KERNEL  BIN"

;***********************FAT12 data*****************************
OEM_ID				db "StOSBOOT"
BytesPerSector		dw 0x0200
SectorsPerCluster	db 0x01
ReservedSectors		dw 0x0001
TotalFATs			db 0x02
MaxRootEntries		dw 0x00E0
TotalSectorsSmall	dw 0x0B40
MediaDescriptor		db 0xF0
SectorsPerFAT		dw 0x0009
SectorsPerTrack		dw 0x0012
NumHeads			dw 0x0002
HiddenSectors		dd 0x00000000
TotalSectorsLarge	dd 0x00000000
DriveNumber			db 0x00
Flags				db 0x00
Signature			db 0x29
VolumeID			dd 0xFFFFFFFF
VolumeLabel			db "StOS   BOOT"
SystemID			db "FAT12   "
Alright I'll enable that, but I do have a boot signature :)
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: FAT floppy loading code causes panic in bochs

Post by Combuster »

What kind of programmer are you when you don't know how something works :shock:
I mean, I looked up int 0x18 in RBIL and I seriously think you should too.

In other words, that's not a bug.
"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 ]
thestew42
Member
Member
Posts: 38
Joined: Thu Jun 19, 2008 6:20 pm
Location: San Diego, CA

Re: FAT floppy loading code causes panic in bochs

Post by thestew42 »

LOL :mrgreen: I know that its meant to reboot the computer. I guess my problem has changed grounds, maybe I should have clarified, in that I no longer am confused as to what triggers the reboot, but rather why it hits that code in the first place. That code is part of a tutorial that I read somewhere and is used (successfully) in my first stage loader. It attempts to read from the floppy 5 times, and if it fails the 5th attempt, it reboots. I don't see that there is a problem with the actual disk image or this readsector code because the loader IS able to load the FAT table. The error occurs in loading the file's clusters. Now correct me if I'm wrong but, that would place the error in the sector/track/cylinder indices I suppose but I know my conversion code works because it was used to load the second stage. So what gives? Does that mean that the filesystem on the image points to nonexistant sectors? Or am I missing something? Thanks.
tantrikwizard
Member
Member
Posts: 153
Joined: Sun Jan 07, 2007 9:40 am
Contact:

Re: FAT floppy loading code causes panic in bochs

Post by tantrikwizard »

You lack the fundamental understanding of computer architecture and programming to write an OS. You're in protected mode and attempting to issue BIOS interrupts.
thestew42
Member
Member
Posts: 38
Joined: Thu Jun 19, 2008 6:20 pm
Location: San Diego, CA

Re: FAT floppy loading code causes panic in bochs

Post by thestew42 »

Ummmm... You're kidding right. If you understood the sequential execution of code, maybe you would see that I don't enter protected mode until after I load the kernel from the file system. Seriously, don't start that.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: FAT floppy loading code causes panic in bochs

Post by neon »

ReadSectors seems to read to es:bx but you never set es to any valid segment. Is es valid?

I suspect its dying within a bios interrupt call considering the value of cs in the bochs log. I would check the return values from the int handlers to determine the exact cause of the error if it occurs. INT 0x13 function 0x2 (read sectors) return the status in ah and is one of the values listed here.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
thestew42
Member
Member
Posts: 38
Joined: Thu Jun 19, 2008 6:20 pm
Location: San Diego, CA

Re: FAT floppy loading code causes panic in bochs

Post by thestew42 »

Oh :oops: you're right, neon. All I had to do was set es with ds at the beginning. Thanks a lot!
Post Reply