[SOLVED] Bootloader fails on a real pc

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.
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: Bootloader fails on a real pc

Post by DavidCooper »

Bietje wrote:
DavidCooper wrote:Another trick would be to poke some code into the memory at 7e00 from within the boot sector so that that poked code will run after the far jump if the second sector fails to load in on top of it. You could try the following set of bytes (all decimal): 191 160 128 184 0 176 142 192 184 88 14 171 - it should print a yellow X to the start of the second line of the screen.
I have got this working now. Still prints the square after the X tho. So this proves that I can read and write to the memory and that there is some bug in reading the correct sector..
It shows that it definitely hasn't loaded in where it should, if it's been loaded in at all, which I doubt. I think the table's essential.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
Bietje
Member
Member
Posts: 100
Joined: Wed Apr 20, 2011 6:57 am

Re: Bootloader fails on a real pc

Post by Bietje »

I used the table of the link you gave me, and I can't get it to work. That is probably because I have no clue what I should ajust..
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: Bootloader fails on a real pc

Post by DavidCooper »

Bietje wrote:I used the table of the link you gave me, and I can't get it to work. That is probably because I have no clue what I should ajust..
Have you added the table into your boot sector? If so, how? Your code needs to start with a jump past the table, and that jump has to manifest itself as a 235 instruction (EB in hex) which takes a single-byte jump distance to get past the table to where your program code begins. You then need a nop for the third byte of the sector, and then the table has to follow with all the entires in it lined up correctly. The BIOS will read the table to work out how to access the USB device, so if you have it set up to look like a floppy disk it will be treated like one. Again you need to post your source code and/or make a hex file of the boot sector available.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
Bietje
Member
Member
Posts: 100
Joined: Wed Apr 20, 2011 6:57 am

Re: Bootloader fails on a real pc

Post by Bietje »

It doesn't seem to jump past the table with an EB jump..

Code: Select all

0000000 3ce9 9000 2020 2020 2020 2020 0200 0201
0000010 0200 00e0 0b40 09f0 1200 0200 0000 0000
0000020 0000 0000 0000 2900 0000 0000 2020 2020
0000030 2020 2020 2020 4620 5441 3231 2020 ea20
0000040 7c44 0000 c031 d88e c08e 8efa bcd0 7c00
0000050 88fb 2516 317c 8ac0 2516 cd7c 7213 b4f6
0000060 b002 3101 b1c9 8a02 2516 307c 31f6 8edb
0000070 bbc3 7e00 13cd e772 e484 d975 013c d575
0000080 00b8 8eb8 31c0 b0ff b441 ab0c 00ea 007e
0000090 0000 0000 0000 0000 0000 0000 0000 0000
00000a0 0000 0000 0000 0000 0000 0000 0000 0000
00000b0 0000 0000 0000 0000 0000 0000 0000 0000
00000c0 0000 0000 0000 0000 0000 0000 0000 0000
00000d0 0000 0000 0000 0000 0000 0000 0000 0000
00000e0 0000 0000 0000 0000 0000 0000 0000 0000
00000f0 0000 0000 0000 0000 0000 0000 0000 0000
0000100 0000 0000 0000 0000 0000 0000 0000 0000
0000110 0000 0000 0000 0000 0000 0000 0000 0000
0000120 0000 0000 0000 0000 0000 0000 0000 0000
0000130 0000 0000 0000 0000 0000 0000 0000 0000
0000140 0000 0000 0000 0000 0000 0000 0000 0000
0000150 0000 0000 0000 0000 0000 0000 0000 0000
0000160 0000 0000 0000 0000 0000 0000 0000 0000
0000170 0000 0000 0000 0000 0000 0000 0000 0000
0000180 0000 0000 0000 0000 0000 0000 0000 0000
0000190 0000 0000 0000 0000 0000 0000 0000 0000
00001a0 0000 0000 0000 0000 0000 0000 0000 0000
00001b0 0000 0000 0000 0000 0000 0000 0000 0000
00001c0 0000 0000 0000 0000 0000 0000 0000 0000
00001d0 0000 0000 0000 0000 0000 0000 0000 0000
00001e0 0000 0000 0000 0000 0000 0000 0000 0000
00001f0 0000 0000 0000 0000 0000 0000 0000 aa55
0000200 00b8 8eb8 31c0 b0ff b446 ab0c fde9 00ff
0000210 0000 0000 0000 0000 0000 0000 0000 0000
Sources:

Code: Select all

LOADOFF equ 0x7C00
BUFFER equ 0x600

[bits 16]
[org 0x7c00]

jmp _start 
  nop 
  db '        ' 
  dw 512                        ; bytes per sector 
  db 1                          ; sectors per cluster 
  dw 2                          ; reserved sector count 
  db 2                          ; number of FATs 
  dw 16*14                      ; root directory entries 
  dw 18*2*80                    ; sector count 
  db 0F0h                       ; media byte 
  dw 9                          ; sectors per fat 
  dw 18                         ; sectors per track 
  dw 2                          ; number of heads 
  dd 0                          ; hidden sector count 
  dd 0                          ; number of sectors huge 
 bootdisk db 0                          ; drive number 
  db 0                          ; reserved 
  db 29h                        ; signature 
  dd 0                          ; volume ID 
  db '           '              ; volume label 
  db 'FAT12   '                 ; file system type

_start: ; entry point
	jmp 0x0:.flush ; some buggy bioses load us in at 0x7c0:0x0 instead of 0x0:0x7c00
.flush:
	xor ax, ax
	mov ds, ax
	mov es, ax
	cli
	mov ss, ax
	mov sp, LOADOFF		; stack setup
	sti

	mov [bootdisk], dl ; boot drive number given by the bios in dl

.reset:
	xor ax, ax		; reset the disk
	mov dl, [bootdisk]
	int 0x13
	jc .reset

.read:
	mov ah, 0x2
	mov al, 1 ; read 1 sector
	xor cx, cx ; cylinder 0
	mov cl, 2 ; sector 2
	mov dl, [bootdisk]
	xor dh, dh ; head 0
	
	
	; setup buffer
	xor bx, bx
	mov es, bx
	mov bx, 0x7e00 ; chain load it
	int 0x13
	jc .read

	test ah, ah
	jnz .reset

	cmp al, 0x1 ; is there one and only one sector loaded?
	jne .reset

	mov ax, 0xb800
	mov es, ax
	xor di, di
	mov al, 65 ; capital A
	mov ah, 0xc 
	stosw

; 	mov ax, 0x7c0
; 	mov es, ax
; 	xor di, di
; 	
; 	mov [es:di], byte 191          ; this code proves that my far jump does its work
; 	mov [es:di+1], byte 160       ; if you would like to test, comment it out.
; 	mov [es:di+2], byte 128
; 	mov [es:di+3], byte 184
; 	mov [es:di+4], byte 0
; 	mov [es:di+5], byte 176
; 	mov [es:di+6], byte  142
; 	mov [es:di+7], byte 192
; 	mov [es:di+8], byte 184
; 	mov [es:di+9], byte 88
; 	mov [es:di+10], byte 14
; 	mov [es:di+11], byte 171

	jmp 0x0:0x7e00		; execute the loaded sector

times 510 - ($ - $$) db 0
dw 0xAA55

Stage 2

Code: Select all

ORG 0x7e00
[BITS 16]

stage2:
	mov ax, 0xb800
	mov es, ax
	xor di, di
	mov al, 70
	mov ah, 0xc
	stosw
	jmp $

times 512 - ($ - $$) db 0
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: Bootloader fails on a real pc

Post by DavidCooper »

Most bootsectors start with something like 235 60 144 followed by the table, but if you can't make your assembler use the 235 instruction, it'll use a two-byte jump distance instead, and that means you don't need the nop (the 144), so getting rid of it should correct the alignment of the table. I haven't checked the contents of the table yet, but I'll look at it now.

Edit: those entries look correct, so they just need to be realigned by one byte by removing the nop. Hopefully then it will work. If it doesn't, we'll need some help from someone else.

Edit 2: Hey, why have you put the commented-out code that pokes the yellow X stuff into 7E00 after the code that tries to load the second sector? I had assumed that you'd put it in first so that if the sector was succesfully loaded it would replace it, but if you poke it to 7E00 after the sector's been loaded it'll replace the succesfully loaded code and make it look as if it failed!
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
Bietje
Member
Member
Posts: 100
Joined: Wed Apr 20, 2011 6:57 am

Re: Bootloader fails on a real pc

Post by Bietje »

I moved the code up now, and the X does not appear (only if I comment out the disk read code). So there is something loaded 0x7e00 after I wrote some code there..
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: Bootloader fails on a real pc

Post by DavidCooper »

No green F then?
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
Bietje
Member
Member
Posts: 100
Joined: Wed Apr 20, 2011 6:57 am

Re: Bootloader fails on a real pc

Post by Bietje »

No, I write code for an yellow X at the second line to mem address 0x7e00. Then this code gets overwritten, because it only shows me the red A. So the disk reader (int 0x13) is doing something, but not what it should be.. I think.
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: Bootloader fails on a real pc

Post by DavidCooper »

I see it's actually a red F now. I take it you've moved the yellow X code into the right place now and that you've made sure putting it in front of the read hasn't messed up any registers in the process.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
Bietje
Member
Member
Posts: 100
Joined: Wed Apr 20, 2011 6:57 am

Re: Bootloader fails on a real pc

Post by Bietje »

Yep.. When the es register is needed again it is set to the correct value..

Code: Select all

[bits 16]
[org 0x7c00]

jmp _start 
  db '        ' 
  dw 512                        ; bytes per sector 
  db 1                          ; sectors per cluster 
  dw 2                          ; reserved sector count 
  db 2                          ; number of FATs 
  dw 16*14                      ; root directory entries 
  dw 18*2*80                    ; sector count 
  db 0F8h                       ; media byte 
  dw 9                          ; sectors per fat 
  dw 18                         ; sectors per track 
  dw 2                          ; number of heads 
  dd 0                          ; hidden sector count 
  dd 0                          ; number of sectors huge 
 bootdisk db 0                          ; drive number 
  db 0                          ; reserved 
  db 29h                        ; signature 
  dd 0                          ; volume ID 
  db '           '              ; volume label 
  db 'FAT12   '                 ; file system type

_start: ; entry point
	jmp 0x0:.flush
.flush:
	xor ax, ax
	mov ds, ax
	mov es, ax
	cli
	mov ss, ax
	mov sp, 0x7c00		; stack setup
	sti

	mov [bootdisk], dl ; boot drive number given by the bios in dl

.pokecode:

	mov ax, 0x7e0
	mov es, ax
	xor di, di
	
	mov [es:di], byte 191          ; this code proves that my far jump does its work
	mov [es:di+1], byte 160       ; if you would like to test, comment it out.
	mov [es:di+2], byte 128
	mov [es:di+3], byte 184
	mov [es:di+4], byte 0
	mov [es:di+5], byte 176
	mov [es:di+6], byte  142
	mov [es:di+7], byte 192
	mov [es:di+8], byte 184
	mov [es:di+9], byte 88
	mov [es:di+10], byte 14
	mov [es:di+11], byte 171

.reset:
	xor ax, ax		; reset the disk
	mov dl, [bootdisk]
	int 0x13
	jc .reset
; 
.read:
	mov ah, 0x2
	mov al, 1 ; read 1 sector
	xor cx, cx ; cylinder 0
	mov cl, 2 ; sector 2
	mov dl, [bootdisk]
	xor dh, dh ; head 0
	
	
	; setup buffer
	xor bx, bx
	mov es, bx
	mov bx, 0x7e00
	int 0x13
	jc .read

	test ah, ah
	jnz .reset

	cmp al, 0x1 ; is there one and only one sector loaded?
	jne .reset

	mov ax, 0xb800
	mov es, ax
	xor di, di
	mov al, 65 ; capital A
	mov ah, 0xc 
	stosw

	jmp 0x0:0x7e00		; execute the loaded sector

times 510 - ($ - $$) db 0
dw 0xAA55
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: Bootloader fails on a real pc

Post by DavidCooper »

Media byte should be F0, not F8. It was right in the earlier version.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
Bietje
Member
Member
Posts: 100
Joined: Wed Apr 20, 2011 6:57 am

Re: Bootloader fails on a real pc

Post by Bietje »

I tested both.. F0 is removable media, f8 is fixed disk.
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: Bootloader fails on a real pc

Post by DavidCooper »

I'm out of ideas for the moment.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
Bietje
Member
Member
Posts: 100
Joined: Wed Apr 20, 2011 6:57 am

Re: Bootloader fails on a real pc

Post by Bietje »

Me to.. I just tried to move stage 2 to sector 20 and read that one in case sector two is a bit corrupt or buggy but that didn't solve anything. Now its time to get some sleep for me.. If anyone could tell me why I cannot execute another sector, tell me.
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: Bootloader fails on a real pc

Post by DavidCooper »

It may be that the BIOS is treating it as a hard drive regardless and the sector numbers are wrong. That other site I sent you to said this:-
USB Booting Secret #2: What happens in the boot sector, stays in the boot sector!
When it comes to USB booting there are basically 4 types of computers:

PC without USB, or with USB and can't boot USB because of old BIOS firmware.
PC that always boots USB with floppy disk emulation.
PC that always boots USB with hard drive emulation.
PC that examines the boot sector to determine the type of emulation and/or with BIOS selection setup.
So I'm not sure what happens if it's the third of those.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
Post Reply