Page 1 of 1

Stage2 FAT12 problem?

Posted: Mon Oct 01, 2007 1:04 pm
by LordMage
Using the exact same code from the original bootloader to read the disks root directory I can't seem to read it in Stage2. I get "Reboot and Select proper Boot device or Insert Boot Media in selected Boot device." I will place the code below but I have tried to edit everything I can and still get the same error

Code: Select all

;*******************************************
; LoadRoot ()
;	- Load Root Directory Table to 0x7e00
;*******************************************

LoadRoot:

	pusha							; store registers

     ; compute size of root directory and store in "cx"
     
	xor     cx, cx						; clear registers
 	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						; move into AX

     ; 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]
	mov     WORD [datasector], ax				; base of root directory
	add     WORD [datasector], cx

     ; read root directory into 0x7e00
 
	mov	dx, 0x7e0
	mov	es, dx
	mov     bx, 0x0						; copy root dir

	call    ReadSectors					; read in directory table

	popa							; restore registers and return
	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


;************************************************;
; 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

--> Somewhere before here is where the problem is, it always fails
--> at this point and after 5 tries fall through and waits for a warm
--> boot

          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 [bpbBytesPerSector]        ; queue next buffer
          inc     ax                                  ; queue next sector
          loop    .MAIN                               ; read next sector
          ret

the only things I can think that would help other than that are that originally the above code was called after GateA20 was enabled, I tried removeing the enable command and that didn't help. It is run before protected mode and the same exact code runs in the stage1 boot loader except for one thing, there are three lines that are different.

Code: Select all

	mov	dx, 0x7e0
	mov	es, dx
	mov     bx, 0x0				
in the original version there were simply

Code: Select all

	mov	bx, 0x200
I tried that too and from what I know those lines are just telling where to put the information that is accessed. but they are not even being gotten to. so I dont see how they would matter.

(SOLVED)

Posted: Mon Oct 01, 2007 2:26 pm
by LordMage
Well, call it solved if you want to, I switched from Virtual PC to VMware and now everything works just fine. I wish I knew what the actual problem was. But as long as I can continue I am happy

Re: (SOLVED)

Posted: Mon Oct 01, 2007 8:17 pm
by pcmattman
LordMage wrote:Well, call it solved if you want to, I switched from Virtual PC to VMware and now everything works just fine. I wish I knew what the actual problem was. But as long as I can continue I am happy
If it doesn't work in Virtual PC, there's a high chance it won't work on physical hardware.

I'd say that you should probably try to fix the errors (if you can).

It might help to implement a basic string output function (using int 10h) and print out status information as you go so that you can find out exactly where the bug occurs.

Posted: Tue Oct 02, 2007 8:29 pm
by LordMage
I did that, the error occurs where I pointed to in the code segment I provided. I have tested everything as much as I can and can't find the error. but I does work in VMWare and in Bochs so I am not too worried right now. I have bigger problems then that right now.