Coding General Bootloader & Make File for Windows

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
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Coding General Bootloader & Make File for Windows

Post by Octacone »

Hey everybody today I have two questions. First is a bit harder, other one is simple.

1. I want to know how to code a specific general bootloader:
-I don't want any fancy stuff, yet!
-I want to code a bootloader that just boots my kernel, nothing more at this point.
-Yes I've seen all the wiki topics related to that, that is not what I want.
-I do not want to use GRUB or any premade one, my OS = my bootloader.
-I am not asking for like full source code stuff (all doe wouldn't be bad, :P ).
-I need like a layout of code, line after line, like a description of what to put in each line
I will give you an example for that above:
First of all how I code my ASM script:
1st part -> I call all the functions I want
2nd part -> I code all my functions
3rd part -> I put all the "dynamic" stuff in there, like strings bools ints etc, in asm they are called like db, int 0xH, sub, etc...

Sample idea I want:

Code: Select all

bits, org, etc.......
call myFunction1 ;print text
call myFunction2 ;color stuff etc not important

myFunction1:
mov etc etc 


;now I here I want to know what to put:
[BITS 32]
LoadKernel:
; like what to do in here just to make my bootloader call my Kernel_Main
;idk here set disk offset?? floppy sectors etc
;idk here set or load gdt
;idk here load paging
;idk here flush idt idk
;what is the bare minimum to load my Kernel_Main and also I need to ghost placeholders for me future code initialization 
for e.g ghost placeholder: ;here you need to load this, here you need to load that, but for now it will work without it
extern Kernel_Main
call Kernel_Main

myFunction2:
eax, esp, etc etc etc

StringToPrint db 'test 123', 0
StringTwoEtc db 'test 234332', 0


Now my simple question. I need a make file for Windows, yeah Windows.
I use Windows for OSDev. |NASM| + |Notepad++| + |VirtualBox| + |Cygwin| + |Required Binutils| + |Magic ISO|
It is really hard to compile script by script and make a new iso and boot it with virtualbox every time I change my code.
Can windows even run MakeFiles using Cygwin?
Something like:

Code: Select all

ValuesForC: -std=gnu99 -ffreestanding -O2 -Wall -Wextra     
///to compile .c files I use: gcc -c kernel.c -o kernel.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra

ValuesForASM: -elf32 
//to compile .S files I use: nasm -elf32 bootloader.S -o bootloader.o

ValuesForLinker.LD: I have no Idea on how to do this
//I need something to link my .o files together with some specific values maybe?

Now In here I want to do like:
Files To Compile Every Time:
kernel.c
bootloader.S
linker.ld
somethingelse.c

Stuff to link:
kernel.o
bootloader.S
somethingelse.o

Now I want:
make IMG file from all those linkes stuff
convert IMG to ISO
virtualbox.boot.load that ISO
I really need you to answer this so I can start coding my OS the way it needs to be coded. Also once I finish my bootloader I will be able to code my OS inside C and be much quicker and more efficient. Bootloaders aren't really my field of interest, C is my buddy so to say.

Thanks!
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

Re: Coding General Bootloader & Make File for Windows

Post by max »

Hello,

first of all, what have you tried and where did you fail?

Why should anyone here take the effort and explain again what has been explained on the various wiki articles?
What you want from us is basically developing it for you, because that's what developing is - figuring out by yourself how you have to do it. The wiki pages tell you the basics. You need to figure out yourself how you implement it exactly (after all we're not here to teach programming). If you get stuck in the process and have any concrete question, then you can come back and ask.

To the second question, yes you can run makefiles in Cygwin. Install make using the cygwin setup.

Greets
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: Coding General Bootloader & Make File for Windows

Post by Octacone »

max wrote:Hello,

first of all, what have you tried and where did you fail?

Why should anyone here take the effort and explain again what has been explained on the various wiki articles?
What you want from us is basically developing it for you, because that's what developing is - figuring out by yourself how you have to do it. The wiki pages tell you the basics. You need to figure out yourself how you implement it exactly (after all we're not here to teach programming). If you get stuck in the process and have any concrete question, then you can come back and ask.

To the second question, yes you can run makefiles in Cygwin. Install make using the cygwin setup.

Greets
Hey Max. I tried and failed. I can not link my bootloader to my kernel since all the linkers I found use GRUB stuff inside.
I would really like if you could give me an example link command for Cygwin that will link all my .o files into one img/iso file.

Edit: I can not make a proper linker.ld file.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: Coding General Bootloader & Make File for Windows

Post by onlyonemac »

Even GRUB can't boot Windows directly, so writing your own bootloader for Windows isn't likely to get very far.

If you want to write a bootloader for an existing operating system, try writing a Linux bootloader, as the protocol there is well documented. Alternatively, you could write a multiboot bootloader which can load any multiboot-compliant operating system, although this is a lot more work.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

Re: Coding General Bootloader & Make File for Windows

Post by Octocontrabass »

thehardcoreOS wrote:I can not link my bootloader to my kernel
Why are you trying to link your bootloader to your kernel? Typically the bootloader is separate, since it needs to be placed in a special reserved portion of the disk.
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: Coding General Bootloader & Make File for Windows

Post by Octacone »

onlyonemac wrote:Even GRUB can't boot Windows directly, so writing your own bootloader for Windows isn't likely to get very far.

If you want to write a bootloader for an existing operating system, try writing a Linux bootloader, as the protocol there is well documented. Alternatively, you could write a multiboot bootloader which can load any multiboot-compliant operating system, although this is a lot more work.

What!? No no, I am not writing a bootloader for Windows.
Last edited by Octacone on Sat Jul 09, 2016 5:43 am, edited 1 time in total.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: Coding General Bootloader & Make File for Windows

Post by Octacone »

Octocontrabass wrote:
thehardcoreOS wrote:I can not link my bootloader to my kernel
Why are you trying to link your bootloader to your kernel? Typically the bootloader is separate, since it needs to be placed in a special reserved portion of the disk.
I need to link my bootloader.S and kernel.c via linker.ld
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

Re: Coding General Bootloader & Make File for Windows

Post by Octocontrabass »

I think you're confusing the bootloader and the startup code. The startup code might be named "boot.s" in some tutorials, but it is not a bootloader.
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: Coding General Bootloader & Make File for Windows

Post by Octacone »

Octocontrabass wrote:I think you're confusing the bootloader and the startup code. The startup code might be named "boot.s" in some tutorials, but it is not a bootloader.
Hmm... Isn't bootloader supposed to call Kernel_Main and initialized GDT, IDT, ISR, etc?
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: Coding General Bootloader & Make File for Windows

Post by onlyonemac »

thehardcoreOS wrote:
Octocontrabass wrote:I think you're confusing the bootloader and the startup code. The startup code might be named "boot.s" in some tutorials, but it is not a bootloader.
Hmm... Isn't bootloader supposed to call Kernel_Main and initialized GDT, IDT, ISR, etc?
That depends on the operating system. Some bootloaders initialise those then call the kernel entry point, while others let the kernel initialise them itself. Some kernels expect these things to be initialised already, but then reconfigure them differently.

Also note that the bootloader calls the kernel's *initialisation routine*, which may not be the same as whatever other entry points that you're thinking of.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: Coding General Bootloader & Make File for Windows

Post by Octacone »

onlyonemac wrote:
thehardcoreOS wrote:
Octocontrabass wrote:I think you're confusing the bootloader and the startup code. The startup code might be named "boot.s" in some tutorials, but it is not a bootloader.
Hmm... Isn't bootloader supposed to call Kernel_Main and initialized GDT, IDT, ISR, etc?
That depends on the operating system. Some bootloaders initialise those then call the kernel entry point, while others let the kernel initialise them itself. Some kernels expect these things to be initialised already, but then reconfigure them differently.

Also note that the bootloader calls the kernel's *initialisation routine*, which may not be the same as whatever other entry points that you're thinking of.
I just want to know what is the bare minimum I have to do in order to call my kernel.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: Coding General Bootloader & Make File for Windows

Post by Octacone »

Please help, can not fix this error:
LD: target i386pe not found

I am using Cygwin and when I try to build my OS that error appears.
This is my MakeFile:

Code: Select all

COMPILER = GCC
LINKER = LD
ASSEMBLER = nasm
CFLAGS = -std=gnu99 -ffreestanding -O2 -Wall -Wextra -m32 -c -lgcc
ASFLAGS = -elf32 
LDFLAGS = -m i386pep -T  linker.ld 
EMULATOR = qemu-system-i386 
EMULATOR_FLAGS = -kernel

OBJS = kernel.o bootloader.o
OUTPUT = kernel.bin

run: all
	$(EMULATOR) $(EMULATOR_FLAGS) $(OUTPUT)

all:$(OBJS)
	$(LINKER) $(LDFLAGS) -o $(OUTPUT) $(OBJS) 

bootloader.o:bootloader.S
	$(ASSEMBLER) $(ASFLAGS) -o bootloader.o bootloader.S

kernel.o:kernel.c
	$(COMPILER) $(CFLAGS) -o kernel.c kernel.o

clear:
	rm -f *.o
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Coding General Bootloader & Make File for Windows

Post by iansjack »

thehardcoreOS wrote: I just want to know what is the bare minimum I have to do in order to call my kernel.
1. Load the kernel into memory from whatever device it is stored on.

2. Jump to the start point of your kernel.

You seem to be having a lot of problems because you are using Windows for your development setup. Is there any reason that you don't use Linux, for which the steps are better documented.

I have to agree with others that you seem to be asking people to do the work for you. This is all well documented. Just do a little research; if this is too difficult, reconsider whether OS development is a hobby that you are suited to.
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: Coding General Bootloader & Make File for Windows

Post by Octacone »

iansjack wrote:
thehardcoreOS wrote: I just want to know what is the bare minimum I have to do in order to call my kernel.
1. Load the kernel into memory from whatever device it is stored on.

2. Jump to the start point of your kernel.

You seem to be having a lot of problems because you are using Windows for your development setup. Is there any reason that you don't use Linux, for which the steps are better documented.

I have to agree with others that you seem to be asking people to do the work for you. This is all well documented. Just do a little research; if this is too difficult, reconsider whether OS development is a hobby that you are suited to.
Thanks for answering.
I don't like Linux because GRUB overwrites MBR and then I need to use GRUB to boot Windows. Also if I don't use my keyboard and select Windows, Linux boots. Linux is not really fun to install since you need to create 3 partitions for it... I will consider installing Ubuntu if that will make any difference, but then I need to find a way to recover MBR.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Coding General Bootloader & Make File for Windows

Post by iansjack »

You could always run Linux in a virtual machine. That way it doesn't affect your Windows installation at all.
Post Reply