Why, oh why doesn't it work!?

Programming, for all ages and all languages.
Post Reply
kenna
Posts: 8
Joined: Fri Apr 13, 2007 1:26 pm

Why, oh why doesn't it work!?

Post by kenna »

No wonder I've spent quite some time designing my own 16/32-bit computer, sure, it's about as good as a 368, but I never inteded to outdoo AMD or VIA O_O. I can't understand why the IA-32 platform has to be so damn messy, poorly designed, and absolutely awful...!!!!! I'm so angry! *Pant, pant*

I am a complete newb/noob when it comes to OS development, so I don't expect much, but this is getting ridiculous, I just wanted to try @_@

Anyway, why the #(%&WNBF¤"!¤"#% doesn't this work, I've stared at the one and same MBR and web page explaining it for hours, yet I do not understand why it blood ydoesn't work.........

The drive image was created with bximage.exe (bochs) and has 2 cylinders, 16 tracks, and 63 spt.

It's a lot easier if you load this into a binary editor ^^

Here's the main boot record (boot1)

Code: Select all

FA 33 C0 8E D0 BC 00 7C 8B F4 50 07 50 1F FB FC BF 00 06 B9 00 01 F2 A5 EA 1D 06 00 00 BE BE 07 B3 04 80 3C 80 74 0E 80 3C 00 75 1C 83 C6 10 FE CB 75 EF CD 18 8B 14 8B 4C 02 8B EE 83 C6 10 FE CB 74 1A 80 3C 00 74 F4 BE 8B 06 AC 3C 00 74 0B 56 BB 07 00 B4 0E CD 10 5E EB F0 EB FE BF 05 00 BB 00 7C B8 01 02 57 CD 13 5F 73 0C 33 C0 CD 13 4F 75 ED BE A3 06 EB D3 BE C2 06 BF FE 7D 81 3D 55 AA 75 C7 8B F5 EA 00 7C 00 00 49 6E 76 61 6C 69 64 20 70 61 72 74 69 74 69 6F 6E 20 74 61 62 6C 65 00 45 72 72 6F 72 20 6C 6F 61 64 69 6E 67 20 6F 70 65 72 61 74 69 6E 67 20 73 79 73 74 65 6D 00 4D 69 73 73 69 6E 67 20 6F 70 65 72 61 74 69 6E 67 20 73 79 73 74 65 6D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 00 00 02 FF 01 00 00 00 00 00 01 00 00 03 F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AA
and here's the partition boot record (boot2), only supposed to display a string and restart when a key is pressed

Code: Select all

B8 03 00 CD 10 B8 01 13 BB 07 00 B9 23 00 BD 20 00 81 C5 00 7C CD 10 B4 00 CD 16 EA 00 00 FF FF 70 52 33 73 53 20 34 6E 59 20 6B 33 79 20 32 20 6B 30 6E 37 31 6E 75 33 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AA
Please help me before I rip my brains out O_O

Bochs always complain about "Error loading operating system"
User avatar
mathematician
Member
Member
Posts: 437
Joined: Fri Dec 15, 2006 5:26 pm
Location: Church Stretton Uk

Post by mathematician »

I know it's terribly old fashioned of me, but I prefer source code to raw machine code; it's just a little bit easier to read. Even so, just a glance at the above tells me that the 55AA is positioned after the end of the boot sector(s), rather than being the final two bytes in the boot sector.
kenna
Posts: 8
Joined: Fri Apr 13, 2007 1:26 pm

Post by kenna »

I know it's terribly old fashioned of me, but I prefer source code to raw machine code; it's just a little bit easier to read. Even so, just a glance at the above tells me that the 55AA is positioned after the end of the boot sector(s), rather than being the final two bytes in the boot sector.
Hmm...isn't it the final 2 bytes...?? Ugh, I'm getting dizzy...

BTW, sorry for the rant, but I was getting pretty pissed ^^

this is the source code for the boot1 program, a standard MBR I found on the internet.

Code: Select all

7C00 FA            CLI                  ; Disable maskable Interrupts
7C01 33C0          XOR	AX,AX           ; Zero out both the Accumulator and
7C03 8ED0          MOV	SS,AX           ;    the Stack Segment register.
7C05 BC007C        MOV	SP,7C00	        ; Set Stack Pointer to 0000:7C00
7C08 8BF4          MOV	SI,SP           ; Source Index: Copy from here...
7C0A 50            PUSH	AX
7C0B 07            POP	ES              ; Zero-out Extra Segment
7C0C 50            PUSH	AX
7C0D 1F            POP	DS              ; Zero-out  Data Segment
7C0E FB            STI                  ; Enable Interrupts again

7C0F FC            CLD                  ; Clear Direction Flag (df=0).
7C10 BF0006        MOV	DI,0600         ; Destination Index: New copy of
                                        ;  code will begin at 0000:0600
7C13 B90001        MOV	CX,0100         ; Copy 256 Words (512 bytes)
                                        ;    (100 hex = 256 decimal)
7C16 F2 (*)        REP (*)              ; REPeat the following MOVSW
                                        ; instruction for 'CX' times(*).
7C17 A5            MOVSW                ;  Copy two bytes at a time.

7C18 EA1D060000    JMP	0000:061D       ; Jump to new copy of code...

061D BEBE07        MOV	SI,07BE           ; Location of first entry
                                          ; in the partition table
                                          ; (see Sample Table below).
0620 B304          MOV	BL,04             ; Maximum of 4 Table Entries.
0622 803C80        CMP	BYTE PTR [SI],80  ; Is this one bootable (80h)?
0625 740E          JE	0635              ; Yes, so jump to next test!
0627 803C00        CMP	BYTE PTR [SI],00  ;  No; is it a 00? If not, it's
062A 751C          JNE	0648              ;   an Invalid partition table.
062C 83C610        ADD	SI,+10            ; Check next entry for 80h ...
                                          ; (10h = 16 bytes per entry)
062F FECB          DEC	BL                ; Subtract 1 from Entry Counter.
0631 75EF          JNZ	0622              ; Have all entries been tested?
0633 CD18          INT  18                ; Yes, and NONE of them were
                                          ;    bootable, so start...
                                          ; ROM-BASIC (only available on
                                          ; some IBM machines!) Many BIOS
                                          ; simply display "PRESS A
                                          ; KEY TO REBOOT" when an
                                          ; Interrupt 18h is executed.

0635 8B14          MOV	DX,[SI]           ; Drive  -> DL /   Head -> DH
                                          ; For the standard MBR code,
          ; DL will always be 80h; which means ONLY the first drive can
          ; be bootable! [ This part of the code is often changed by MBR
          ; replacements to boot from another (second, etc.) drive! ]

0637 8B4C02        MOV	CX,[SI+02]        ; Sector -> CL / Cylinder -> CH

063A 8BEE          MOV	BP,SI             ; Save offset of Active Entry
                                          ;  pass to Volume Boot Sector.
063C 83C610        ADD	SI,+10            ; Do next Table Entry
063F FECB          DEC	BL                ; Is this the last entry?
0641 741A          JZ	065D              ; All Entries look OK, so...
                                          ; -> Jump to Boot-routine!
0643 803C00        CMP	BYTE PTR [SI],00  ; Non-bootable entry (00h)?
0646 74F4          JE	063C              ; Yes, so check next entry.

0648 BE8B06        MOV	SI,068B       ; -> "Invalid partition table"

064B AC            LODSB              ; Load byte at [SI] into AL ...
                                      ;   and increment the SI value.
064C 3C00          CMP	AL,00         ; Is it a zero-byte yet ?
064E 740B          JE	065B          ; If yes, were done.  If not ...
0650 56            PUSH	SI            ;  Store string pointer on stack.
0651 BB0700        MOV	BX,0007       ; Use Function 0E (Write Text) of
0654 B40E          MOV	AH,0E         ;  DOS Interrupt 10 to send the
0656 CD10          INT	10            ;  character in AL to the screen.
0658 5E            POP	SI
0659 EBF0          JMP	064B

065B EBFE          JMP  065B          ; Infinite Loop.  You must
                                      ;    power-down or Reboot!

065D BF0500        MOV	DI,0005       ; Retry 5 times (if necessary)...
0660 BB007C        MOV	BX,7C00       ; Load OS Boot Sector to 0000:7C00
0663 B80102        MOV	AX,0201       ; Function 02h. Read 1 sector.
0666 57            PUSH	DI
0667 CD13          INT  13            ;   Note: This old INT 13 Read
                                      ; is limited to 1024 cylinders.
0669 5F            POP	DI
066A 730C          JNC	0678          ; Carry Flag set? If no, jump!
066C 33C0          XOR	AX,AX         ; Yes, so we had an error! Must
066E CD13          INT  13            ; ...reset drive (Function 00h)
0670 4F            DEC	DI            ; Decrement counter (if > 0)
0671 75ED          JNZ	0660          ;   and try again...

0673 BEA306        MOV	SI,06A3       ; Or, declare: "Error loading
0676 EBD3          JMP	064B          ; operating system" and hang!

0678 BEC206        MOV	SI,06C2         ; -> "Missing operating system"
                                        ; Set up SI, in case we have an
                                        ; error in the Boot Sector read.
067B BFFE7D        MOV	DI,7DFE         ; Point to the last Word of Boot
                                        ; Sector. It should be AA55 Hex.

067E 813D55AA      CMP	WORD PTR [DI],AA55 ; Is it? ('Signature' Check.)
0682 75C7          JNE	064B               ; If not, display Error Message
                                           ;         and 'lock-up' system.

0684 8BF5          MOV	SI,BP	        ;   SI=BP ->  Both are equal to...
                                        ; offset of Active Partition Entry
                                        ; which is used by OS Boot code.
0686 EA007C0000    JMP	0000:7C00       ;   Jump to OS Boot Sector code
                                        ;       and continue booting-up!

068B                                   49 6E 76 61 6C              Inval
0690  69 64 20 70 61 72 74 69-74 69 6F 6E 20 74 61 62   id partition tab
06A0  6C 65 00 45 72 72 6F 72-20 6C 6F 61 64 69 6E 67   le.Error loading
06B0  20 6F 70 65 72 61 74 69-6E 67 20 73 79 73 74 65    operating syste
06C0  6D 00 4D 69 73 73 69 6E-67 20 6F 70 65 72 61 74   m.Missing operat
06D0  69 6E 67 20 73 79 73 74-65 6D 00                  ing system.

and here's the source code for the boot2 program, the boot sector of the partition:

Code: Select all

START:
     MOV AX,0x0003
     INT 0x10

PRINT_STRING:
     MOV AX,0x1301
     MOV BX,0x0007
     MOV CX,0x23

     MOV BP,MSG
     ADD BP,0x7C00

     INT 0x10

WAIT_FOR_KEY_PRESS:
     MOV AH,0x00
     INT 0x16

REBOOT:
     DB 0xEA
     DW 0x0000
     DW 0xFFFF

MSG  DB 'pR3sS 4nY k3y 2 k0n71nu3',13,10,'btw, ph33r',0

     TIMES 510-($-$$) DB 0

SIGNATURE DW 0xAA55
and this is the partition table, i.e. the last 80 bytes of the boot sector:

Code: Select all

00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 00 00 02 FF 01 00 00 00 00 00 01 00 00 03 F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 AA 55
and a simplified partition table:

Code: Select all

0x00	0x80 // bootable
0x01	0x00-00-02 // CHS of first block of partition
    	           // cylinder 0, track 0, block 2 (sector)
    	           // i.e. the second 512 bytes (block/sector)
    	           // the first 512 byts being the MBR
0x04	0xff // partition type (something not nil)
0x05	0x01-00-00 // CHS of last block of partition
    	           // cylinder 1, track 0, block 0 (sector)
    	           // i.e. the 1008th 512 bytes (block/sector)
    	           // NOTE: don't understand why block/sector all of a sudden
    	           // start from 0 rather than 1
    	           // or perhaps the docs I've been reading are wrong
    	           // need to figure out format of CHS
    	           // C0, H0, S0?? = cylinder 1, track 1, block 1 (sector)
0x08	0x00-00-00-01 // LBA of first block of partition
0x0c	0x00-00-03-f0 // length of partition in blocks
User avatar
mathematician
Member
Member
Posts: 437
Joined: Fri Dec 15, 2006 5:26 pm
Location: Church Stretton Uk

Post by mathematician »

kenna wrote: [/code]

and here's the source code for the boot2 program, the boot sector of the partition:

Code: Select all

START:
     MOV AX,0x0003
     INT 0x10

PRINT_STRING:
     MOV AX,0x1301
     MOV BX,0x0007
     MOV CX,0x23

     MOV BP,MSG
     ADD BP,0x7C00

     INT 0x10

WAIT_FOR_KEY_PRESS:
     MOV AH,0x00
     INT 0x16

REBOOT:
     DB 0xEA
     DW 0x0000
     DW 0xFFFF

MSG  DB 'pR3sS 4nY k3y 2 k0n71nu3',13,10,'btw, ph33r',0

     TIMES 510-($-$$) DB 0

SIGNATURE DW 0xAA55
I haven't looked at the mbr, but in the above you do not seem to have specified row and column numbers in the dx register, or initialised es to zero so that es:bp points to the string.

Code: Select all

START:
     MOV AX,0x0003
     INT 0x10
You will generally find that that bit is taken for granted in boot sector code, because the BIOS will have put it into text mode for its own purposes.
kenna
Posts: 8
Joined: Fri Apr 13, 2007 1:26 pm

Post by kenna »

mathematician wrote:
kenna wrote: and here's the source code for the boot2 program, the boot sector of the partition:

Code: Select all

START:
     MOV AX,0x0003
...

SIGNATURE DW 0xAA55
I haven't looked at the mbr, but in the above you do not seem to have specified row and column numbers in the dx register, or initialised es to zero so that es:bp points to the string.

Code: Select all

START:
     MOV AX,0x0003
     INT 0x10
You will generally find that that bit is taken for granted in boot sector code, because the BIOS will have put it into text mode for its own purposes.
The thing is, if I boot it in Bochs from a floppy it works just fine, but when I try to boot it from a hard disk it fails...so even though there are errors (as you pointed out), it can't be the only thing keeping it from booting.
User avatar
mathematician
Member
Member
Posts: 437
Joined: Fri Dec 15, 2006 5:26 pm
Location: Church Stretton Uk

Post by mathematician »

and this is the partition table, i.e. the last 80 bytes of the boot sector:

Code:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 00 00 02 FF 01 00 00 00 00 00 01 00 00 03 F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 AA 55

Code: Select all

067E 813D55AA      CMP   WORD PTR [DI],AA55 ; Is it? ('Signature' Check.)
0682 75C7              JNE   064B                          ; If not, display Error Message
                                                                       ;and 'lock-up' system.
Well you are as good as your word in locking up the system. AA55 and db AA, 55 might look the same, but they are not because AA is the most significant byte, and, at least in the Intel architecture, ought to come after 55 in memory: db 55, AA
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Post by Brendan »

Hi,
kenna wrote:this is the source code for the boot1 program, a standard MBR I found on the internet.
I thought I recognised this MBR - it's stolen from DOS, and therefore copyrighted by Microsoft.

My main suggestion is to delete it fast before men in black suits arrive! ;)

As an alternative, why not use an existing boot manager (like GRUB, LILO, etc)? Anything that's open source and supports chainloading should be fine.

BTW the DOS MBR is mostly broken anyway - it won't handle dual boot or larger disk sizes.

I'd also suggest starting with boot floppies - they're much easier (no messing about with MBRs, large disk support or partition tables), and it's easier for other people to test your OS without worrying about trashing their hard drives.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply