flat assembler
Message board for the users of flat assembler.

Index > OS Construction > 16Bits OS Bootloader and Kernel ? Or Stage 2 here?

Author
Thread Post new topic Reply to topic
Kitsune



Joined: 17 Apr 2023
Posts: 18
Kitsune 19 Nov 2023, 23:15
Hi,

I have fond some code online and assembly it in 2 files (Bootloader and "Kernel" or Stage 2 don't really know if I must use a second boot file really,

It boot from a floppy for now..And I want a way to put this kind of Kernel on a disk and make it bootable but I don't know how to do it and I need a tool first for format hard disk before copying it I think if the disk is not formated...
...So I don't find a tutorial about this kind of manipulation in assembly. Is there a book I can read it's talking about OS development through assembly language (16bits for now) It can be an old school book.

Here the files tested on PCEm:

Bootloader.asm
Code:
use16
ORG 0x7C00

mov si, text_string
call print_string

jmp $

text_string db 'Launching KitsuneOS. Loading Kernel...Please Wait.',13,10,0

print_string:
        mov ah, 0x0E ; int 10h 'print char' function
_repeat:
        lodsb
        cmp al,0
        je loadkern     ; If char is zero, end of string
        int 10h                 ; Otherwise, print it
        jmp _repeat

loadkern: 
cld                ; Clear direction flag to ensure instructions like
                   ;    lodsb use forward movement.

xor ax, ax         ; AX=0 (use for segment)
mov ss, ax
mov sp, 0x7c00     ; Set up stack to grow down from 0x0000:0x07c00
                   ;    in region below the bootloader

mov es, ax         ; Read sector starting at 0x0000:0x7e00 (ES:BX)
mov bx, 0x7e00

mov ah, 2
mov al, 1
mov ch, 0
mov cl, 2
mov dh, 0
; mov dl, 0        ; Don't force to zero. Use value in DL passed by BIOS
                   ;     to our bootloader
int 13h

jmp 0x0000:0x7e00  ; Jump to physical address 0x07e00 by setting CS
                   ;     to 0 and IP to 0x7e00

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


Kernel.asm
Code:
use16
org 0x0000         ; Since CS will be 0x07e0 we need an origin of 0x0000
                   ;     0x07e0:0x0000 also represents phys addres 0x07e00
                   ;     0x07e0<<4 + 0x0000 = 0x7e00

; jmp main         ; Not necessary since main is next instruction

main:
mov ax, 0x07e0
mov ds, ax         ; Make sure segments are 0x07e0
mov es, ax

mov si, msg
call print
cli                ; Do CLI/HLT after we return from print
hlt                ;    Prevent code from getting beyond this point

print:
lodsb
cmp al, 0
je done
mov ah, 0eh
int 10h
jmp print
done:
ret

;Place data here. msg is a message we want to print
msg: db "Kernel Loaded. Welcome to Kitsune OS!"
    


Compile it on Linux with:
Code:
fasm bootloader.asm boot.bin
fasm kernel.asm kernel.bin
dd if=boot.bin of=KitsuneOS.img bs=512 conv=notrunc
dd if=kernel.bin of=KitsuneOS.img bs=512 conv=notrunc seek=1
    

_________________
Kitsune
Post 19 Nov 2023, 23:15
View user's profile Send private message Reply with quote
macgub



Joined: 11 Jan 2006
Posts: 346
Location: Poland
macgub 20 Nov 2023, 20:16
Just curious - what hardware you have???
If you are cd/dvd owner - you can burn cd in floppy emulation mode.. Many software tools have this option (for sure ex. CD burner XP WIndows tool).
Post 20 Nov 2023, 20:16
View user's profile Send private message Visit poster's website Reply with quote
SeproMan



Joined: 11 Oct 2009
Posts: 70
Location: Belgium
SeproMan 20 Nov 2023, 21:15
You found this code online, but I doubt it's a good example!

Code:
cld                ; Clear direction flag to ensure instructions like
                   ;    lodsb use forward movement.

xor ax, ax         ; AX=0 (use for segment)
mov ss, ax
mov sp, 0x7c00     ; Set up stack to grow down from 0x0000:0x07c00
                   ;    in region below the bootloader

mov es, ax
    


The above instructions really belong to directly below the 'ORG 0x7C00' directive. Also the 'mov ds, ax' instruction is still missing, and the print_string procedure relies on it being set up properly!



Do notice that the first comment in KERNEL.ASM

Code:
use16
org 0x0000         ; Since CS will be 0x07e0 we need an origin of 0x0000
    


does not match the far jump in the BOOTLOADER.ASM

Code:
jmp 0x0000:0x7e00  ; Jump to physical address 0x07e00 by setting CS
                   ;     to 0 and IP to 0x7e00
    



Quote:
Bootloader and "Kernel" or Stage 2 don't really know if I must use a second boot file really,


The bootloader code must fit in only 512 bytes. If your kernel is to be any serious, then it won't be possible to stay within those 512 bytes. So yes, you need 2 files.

_________________
Real Address Mode.
Post 20 Nov 2023, 21:15
View user's profile Send private message Reply with quote
Kitsune



Joined: 17 Apr 2023
Posts: 18
Kitsune 20 Nov 2023, 21:50
macgub wrote:
Just curious - what hardware you have???
If you are cd/dvd owner - you can burn cd in floppy emulation mode.. Many software tools have this option (for sure ex. CD burner XP WIndows tool).


@macgub
I have a laptop computer with Debian Linux installed on it for compile the asm code and create floppy from command line, and PCem as a virtual machine software.


SeproMan wrote:

...
The above instructions really belong to directly below the 'ORG 0x7C00' directive. Also the 'mov ds, ax' instruction is still missing, and the print_string procedure relies on it being set up properly!

Do notice that the first comment in KERNEL.ASM

Code:
use16
org 0x0000         ; Since CS will be 0x07e0 we need an origin of 0x0000
    


does not match the far jump in the BOOTLOADER.ASM

Code:
jmp 0x0000:0x7e00  ; Jump to physical address 0x07e00 by setting CS
                   ;     to 0 and IP to 0x7e00
    



Quote:
Bootloader and "Kernel" or Stage 2 don't really know if I must use a second boot file really,


The bootloader code must fit in only 512 bytes. If your kernel is to be any serious, then it won't be possible to stay within those 512 bytes. So yes, you need 2 files.



@SeproMan
Have you ressources I can learn about 16bits OS creation in assembly?

This source code works despite what you said about the jump to adress command
I have tested it.

In the meantime, I found a Youtube "tutorial" on creating an OS, maybe that's a better way to learn it.

But if you have good books about this kind of knowlege (16bits OS in assembly in DOS era) it's welcome!

_________________
Kitsune
Post 20 Nov 2023, 21:50
View user's profile Send private message Reply with quote
SeproMan



Joined: 11 Oct 2009
Posts: 70
Location: Belgium
SeproMan 20 Nov 2023, 22:21
Good online resources include:

A more theoretical approach: https://wiki.osdev.org/Bootloader

A more hands-on approach: https://stackoverflow.com/questions/tagged/assembly
Then use the search function filling in [Bootloader][x86-16]
You'll see that many of the best answers there were contributed by users Michael Petch and Sep Roland, (amongst others of course).
On the stackoverflow site you can also ask for help with your programming troubles.


Quote:
This source code works despite what you said about the jump to adress command
I have tested it.


I did not say that anything was wrong with the far jump itself. It's the comments that go along with it that are contradicting each other. Wink

_________________
Real Address Mode.
Post 20 Nov 2023, 22:21
View user's profile Send private message Reply with quote
Kitsune



Joined: 17 Apr 2023
Posts: 18
Kitsune 20 Nov 2023, 23:03
The above code is from this page:
https://forum.osdev.org/viewtopic.php?f=1&t=32452
It's on the same website you mentionned.

I will look at these links.
Thanks you for the ressources and help.

Good night.
Post 20 Nov 2023, 23:03
View user's profile Send private message Reply with quote
bzt



Joined: 09 Nov 2018
Posts: 77
bzt 15 Jan 2024, 23:28
Kitsune wrote:
And I want a way to put this kind of Kernel on a disk and make it bootable but I don't know how to do it and I need a tool first for format hard disk before copying it I think if the disk is not formated...
As luck would have it, I've just implemented such a disk image creation tool, see Easyboot.

You invoke it as easyboot folder imagefile. It will then create a bootable disk image, where
- the master boot record comes from boot_x86.asm (512 bytes). Written with fasm, it is capable to load a 64-bit PE/COFF executable with a long mode entry point.
- then GPT partitioning table is created, with an EFI System Boot Partition (ESP) formatted as FAT32
- all files from folder are copied to that ESP boot partition (among other things, the kernel file)
- then the actual loader (Stage 2, named \EFI\BOOT\BOOTx86.EFI) is placed on the ESP, which is very small, 64K only (the mbr sector also loads this file). This is the one that is capable to load the actual kernel (which could be several megabytes in size, might need higher-half mapping etc. etc. etc.)
- the tool has NO dependencies (no libraries, no loopback devices, no fdisk, no mtools, nothing needed), it is totally self-containted and does not require root privileges. Compiles under Linux and Windows as well.

Few notes:
- my boot sector could load your kernel as-is, if it's not bigger than 64k
- you don't have to use my loader at all with this disk creator tool, it is GPL licensed and you can fork it and rewrite it as you please
- the file data.h contains the blobs (boot_x86_bin is he boot sector, loader_x86_efi is the actual loader), just replace these byte arrays with whatever binary you'd like, and you can still use the disk creator part to produce your imagefile.
- the path and filename where the Stage 2 is placed on the FAT32 boot partition can be changed here (not configurable, you'll have to modify it in the source).
- as a last step, you should write the generated imagefile to your hard disk using dd (or USBImager) or boot it in a virtual machine

Hope this helps,
bzt
Post 15 Jan 2024, 23:28
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 921
Location: Russia
macomics 16 Jan 2024, 00:00
Disk images can be created immediately in fasm (up to 512 MB). For example, creating FDD.IMG for 1.44 with a boot sector capable of loading a file up to 630 kb into memory and adding the corresponding files to the root directory of the file system of this image.


Description: List it in the FILELIST.INC all files that should be included in the floppy disk image.
Download
Filename: FDD_gen.zip
Filesize: 7.95 KB
Downloaded: 59 Time(s)

Post 16 Jan 2024, 00:00
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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.