flat assembler
Message board for the users of flat assembler.

Index > OS Construction > BootLoader help

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
SheldonCompton



Joined: 07 Feb 2015
Posts: 14
SheldonCompton 07 Feb 2015, 16:12
I've been trying to write a usb bootloader in fasm for the last week or so. It boots, but for some reason it doesn't seem to want to load and jump to the kernel. No flags seem to be set. Any suggestions will gratefully be suggested.


Description:
Download
Filename: BootLoader0.03.asm
Filesize: 767 Bytes
Downloaded: 636 Time(s)

Description: Kernel for testing
Download
Filename: Kernel0.01.asm
Filesize: 199 Bytes
Downloaded: 866 Time(s)

Post 07 Feb 2015, 16:12
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 07 Feb 2015, 23:25
I think your DAP segment and offset are reversed, just looking at it. Try it with the 200h in the DW above, and make the second DW 0. I'll try to look more later, but I think that should get you where you want to be.
Post 07 Feb 2015, 23:25
View user's profile Send private message Reply with quote
SheldonCompton



Joined: 07 Feb 2015
Posts: 14
SheldonCompton 07 Feb 2015, 23:55
Unfortunately that wasn't the problem. Currently it displays 1 and then nothing after.
This is the build script I've been using.
-----------------------------------------------------------------------------------------------------------------------------------
#!/bin/bash

cd /home/sheldon/Desktop/fasm

./fasm ../Git/ProgrammingClubGame/BootLoader0.03.asm
./fasm ../Git/ProgrammingClubGame/Kernel0.01.asm

cd /home/sheldon/Desktop/Git/ProgrammingClubGame

dd if=/home/sheldon/Desktop/Git/ProgrammingClubGame/BootLoader0.03.bin of=/dev/sdb bs=512 count=1
dd if=/home/sheldon/Desktop/Git/ProgrammingClubGame/Kernel0.01.bin of=/dev/sdb seek=2 bs=512 count=1

rm /home/sheldon/Desktop/Git/ProgrammingClubGame/BootLoader0.03.bin
rm /home/sheldon/Desktop/Git/ProgrammingClubGame/Kernel0.01.bin
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Any suggestions will gratefully be excepted.
Post 07 Feb 2015, 23:55
View user's profile Send private message Reply with quote
SheldonCompton



Joined: 07 Feb 2015
Posts: 14
SheldonCompton 08 Feb 2015, 00:03
Sorry, wrong version of the build script. Here is the one I'm really using.
--------------------------------------------------------------------------------------------------------------
#!/bin/bash

cd /home/sheldon/Desktop/fasm

./fasm ../Git/ProgrammingClubGame/BootLoader0.03.asm
./fasm ../Git/ProgrammingClubGame/Kernel0.01.asm

cd /home/sheldon/Desktop/Git/ProgrammingClubGame

dd if=/home/sheldon/Desktop/Git/ProgrammingClubGame/BootLoader0.03.bin of=/dev/sdb bs=512 count=1
dd if=/home/sheldon/Desktop/Git/ProgrammingClubGame/Kernel0.01.bin of=/dev/sdb obs=512 seek=1 bs=512 count=1

rm /home/sheldon/Desktop/Git/ProgrammingClubGame/BootLoader0.03.bin
rm /home/sheldon/Desktop/Git/ProgrammingClubGame/Kernel0.01.bin
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Post 08 Feb 2015, 00:03
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 08 Feb 2015, 12:43
Yeah, I have no idea what those scripts are...I'm assuming that's Linux.

The drive you are placing it on should get on the very first sector the binary BootLoader0.03.bin, then the second sector on the disk should get the kernel binary. Although, I'm looking at your INT 13h, and that should be:

Code:
    MOV DL,[BootDriveNumber]            ; DL from BOOT
                                        ; you are placing a 1 here, don't, remove your DL, it is already set at boot.
    MOV AH,42h                          ; Read from drive function
    MOV SI,DiskAddressPacket            ; Load SI with address of the DAP
    INT 13h                             ; Call read sector from drive
    JC Failure                          ; Nope, hosed, get out
    


At least in theory, I haven't tried a USB drive with these. This does work with a CD-ROM however, which I've tested it on, and assuming that the USB emulates through BIOS INT 13h, should be the very same.
Post 08 Feb 2015, 12:43
View user's profile Send private message Reply with quote
SheldonCompton



Joined: 07 Feb 2015
Posts: 14
SheldonCompton 08 Feb 2015, 16:20
I tried this, but now I'm getting an error.


Description:
Download
Filename: BootLoader0.03.asm
Filesize: 840 Bytes
Downloaded: 585 Time(s)

Post 08 Feb 2015, 16:20
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 09 Feb 2015, 06:34
Now you have

Code:
mov dl,0    
Comment that line out entirely. When you boot, the DL contains the boot drive number.
Post 09 Feb 2015, 06:34
View user's profile Send private message Reply with quote
SheldonCompton



Joined: 07 Feb 2015
Posts: 14
SheldonCompton 09 Feb 2015, 12:57
This is where I am right now. It displays 1 then nothing.
Code:
org 7C00h

;;;;;;;;;;;;;;;;;;
;DO NOT MODIFY DL;
;;;;;;;;;;;;;;;;;;

jmp start

start:

cli
mov ax, 0x0200  ;start the stack after the bootloader
mov ss, ax      
mov ax, 0xC00   ;make the stack 3k big later this will need to be changed and hardware detection added
mov ax, sp      ;but for now this will work.
mov ax, ds
sti

mov ah, 0x0e
mov al, '1'
int 0x10

mov ah, 0x42
mov si, DiskAddressPacket
int 0x13
jc error


jmp     0xA00:0x0200

error:
mov ah, 0x0e
mov al, 'E'     
int 0x10


DiskAddressPacket:
db 0x10         ;size of packet
db 0x0          ;reserved
dw 0x1          ;number of blocks to transfer
dw 0x200        ;offset
dw 0xA00        ;where in memory to write to
dq 0x1          ;starting block to read
dd 0x0

times 510-($-$$) db 0 ;Fill rest of sector up with 0s to make this 512B (a sector)
dw 0xAA55             ;Let BIOS know this is an OS

    

The kernel hasn't changed.
Post 09 Feb 2015, 12:57
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 09 Feb 2015, 14:34
I'll see if I can modify a thumb drive tonight and try it, when I get a chance after getting home from work. The only other thing I would consider, is that the block may be wrong, I cannot recall if it starts at 0, or 1. I suspect since you are only getting one '1' on the screen, that it is loading it and jumping to the correct locale. Let do a quick look at your kernel code...based on the code, it should fill the screen up with 2s. I found this note:
http://wiki.osdev.org/ATA_in_x86_RealMode_%28BIOS%29 wrote:
Note: Always remember that Sector is 1-based, and not 0-based ... this detail causes many problems.
It doesn't make sense to me why the screen doesn't fill with 1s, but try making it starting block to read 2.
Post 09 Feb 2015, 14:34
View user's profile Send private message Reply with quote
SheldonCompton



Joined: 07 Feb 2015
Posts: 14
SheldonCompton 09 Feb 2015, 18:00
I changed the sector it's reading to 2, but got the same result.
Code:

DiskAddressPacket:
db 0x10         ;size of packet
db 0x0          ;reserved
dw 0x1          ;number of blocks to transfer
dw 0xA00        ;where in memory to write to
dw 0x200        ;offset
dq 0x2          ;starting block to read
dd 0x0

    
Post 09 Feb 2015, 18:00
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 09 Feb 2015, 23:22
Yep, I loaded it on a 1 GB flash drive and on my machine it stopped after showing a 1 with the cursor just after. However, when I created a HDD with VirtualBox of the USB drive, and loaded it in VirtualBox it worked, showed 2s across the screen. So, in theory it works and as to why on the USB drive on a real machine is beyond me at the moment. There must be something perculaiar to an USB drive boot.
Post 09 Feb 2015, 23:22
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 10 Feb 2015, 01:10
I don't have time right now to look at this further, but you might have to create a partition table the tells BIOS that you are emulating a HDD, because I suspect that the BIOS is floppy emulating the drive, also may need a BIOS Parameter Block too, in the book sector of the partition to load. In other words, talking absolute sectors, 1 = code to start, which holds a Master Boot Record (MBR), in the first MBR entry place it to point to absolute sector 2, where Bootloader goes, and in absolute sector 3, place the kernel code. Try that and see what happens.
Post 10 Feb 2015, 01:10
View user's profile Send private message Reply with quote
SheldonCompton



Joined: 07 Feb 2015
Posts: 14
SheldonCompton 10 Feb 2015, 01:26
I'll have to try that. As of right now though I put it on a spare hard drive, booted without using a vm, and I got '1E1E'.
Post 10 Feb 2015, 01:26
View user's profile Send private message Reply with quote
SheldonCompton



Joined: 07 Feb 2015
Posts: 14
SheldonCompton 10 Feb 2015, 02:52
I got it to work booting from a usb without a vm. I had to format it to fat16 first then write to it. I'm not exactly sure why this works, but it does.
Post 10 Feb 2015, 02:52
View user's profile Send private message Reply with quote
SheldonCompton



Joined: 07 Feb 2015
Posts: 14
SheldonCompton 10 Feb 2015, 12:52
I should probably give credit and say I found this information here: http://forum.osdev.org/viewtopic.php?f=13&t=19357
Post 10 Feb 2015, 12:52
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 10 Feb 2015, 15:19
Kewl! So, which emulation is being used? So, I have seen other boot sectors that they say have worked. In those I did notice that they will test for BIOS EDD, and if you don't have EDD, then you may need to use the standard disk read function under INT 13h (whatever it is). I use EDD for my CD-ROM bootable CD, although I also test it first. Too, what is in your DL? This will give you a clue as to your emulation. I will check mine later this evening once I get home from work. I need a flash drive boot sector so this is a good exercise for me. Although I will be using FAT 32 though, and loading an DOS MZ EXE as my OS kernel. I've done a FAT 12 FDD and CDFS, now it is time for flash (ala HDD style).

Can you post your final code?
Post 10 Feb 2015, 15:19
View user's profile Send private message Reply with quote
SheldonCompton



Joined: 07 Feb 2015
Posts: 14
SheldonCompton 11 Feb 2015, 01:35
Final Code:
Code:
org 7C00h

;;;;;;;;;;;;;;;;;;
;DO NOT MODIFY DL;
;;;;;;;;;;;;;;;;;;

jmp start



start:

cli
mov ax, 0x0200  ;start the stack after the bootloader
mov ss, ax      
mov ax, 0xC00   ;make the stack 3k big later this will need to be changed and hardware detection added
mov ax, sp      ;but for now this will work.
mov ax, ds
sti

mov ah, 0x0e
mov al, '1'
int 0x10

mov ah, 0x42
mov si, DiskAddressPacket
int 0x13
jc error


jmp     0xA00:0x0200

error:
mov ah, 0x0e
mov al, 'E'     
int 0x10


DiskAddressPacket:
db 0x10         ;size of packet
db 0x0          ;reserved
dw 0x1          ;number of blocks to transfer
dw 0xA00        ;where in memory to write to
dw 0x200        ;offset
dq 0x2          ;starting block to read
dd 0x0

times 510-($-$$) db 0 ;Fill rest of sector up with 0s to make this 512B (a sector)
dw 0xAA55             ;Let BIOS know this is an OS
    

Kernel
Code:
jmp main

main:
cli
mov ax, 0x9000 ;Set up stack
mov ss, ax     ;Tell processor where stack is
mov sp, 0xFB00 ;Set stack offset
sti

mov al, '2'
mov ah, 0x0e
int 0x10

jmp main

times 512-($-$$) db 0
    

Build script
Code:
cd /home/sheldon/Desktop/fasm

./fasm ../Git/ProgrammingClubGame/BootLoader0.03.asm
./fasm ../Git/ProgrammingClubGame/Kernel0.01.asm
 
cd /home/sheldon/Desktop/Git/ProgrammingClubGame

dd if=/home/sheldon/Desktop/Git/ProgrammingClubGame/BootLoader0.03.bin of=/dev/sdc obs=512 bs=512 count=1
dd if=/home/sheldon/Desktop/Git/ProgrammingClubGame/Kernel0.01.bin of=/dev/sdc obs=512 bs=512 count=1

rm /home/sheldon/Desktop/Git/ProgrammingClubGame/BootLoader0.03.bin
rm /home/sheldon/Desktop/Git/ProgrammingClubGame/Kernel0.01.bin
    


It's using HDD emulation, but soon I will have to write one for FDD since different bioses (Not sure what the plural of bios is) see usb drives differently. I believe that it won't work with FAT 32. I was using FAT 32 when it wasn't working and when I formated it to FAT 16 it did.
Post 11 Feb 2015, 01:35
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 11 Feb 2015, 01:54
Thanks man, I'm swamped right now, but once I get the opportunity I'll put it to the test.
Post 11 Feb 2015, 01:54
View user's profile Send private message Reply with quote
SheldonCompton



Joined: 07 Feb 2015
Posts: 14
SheldonCompton 12 Feb 2015, 04:22
Shamefully I have to admit I made a mistake. The bootloader never worked. I accidentally wrote the kernel to the MBR which I perceived as working.
Post 12 Feb 2015, 04:22
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 12 Feb 2015, 14:11
So, we know that it loads absolute sector 1, but after that is a mystery. Smile
Post 12 Feb 2015, 14:11
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.