"bootloader" in C?

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
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Post by 01000101 »

It's not that GRUB doesn't have desired functionality, it's simply that it's not mine and cannot be released with my OS when it goes through the copyright process. I have (basically) everything I need already done and setup through PMode.

Within my kernel I have setup my gdt, idt, isrs, A20, drivers, ect...

I just need to be able to get a loader that sets up a pseudo gdt and pop me into 32 bit mode so that I can load my OS at the 1meg mark and then my kernel can load the 'actual' gdt and ect...
User avatar
ucosty
Member
Member
Posts: 271
Joined: Tue Aug 08, 2006 7:43 am
Location: Sydney, Australia

Post by ucosty »

Grub uses the GPL license, which doesnt prohibit distribution alongside closed source binaries.
The cake is a lie | rackbits.com
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Post by 01000101 »

That's a big negatory.

Exerpts from the GNU General Public Liscence:
You have a GPL'ed program that I'd like to link with my code to build a proprietary program. Does the fact that I link with your program mean I have to GPL my program?

Yes.
If so, is there any chance I could get a license of your program under the Lesser GPL?

You can ask, but most authors will stand firm and say no. The idea of the GPL is that if you want to include our code in your program, your program must also be free software. It is supposed to put pressure on you to release your program in a way that makes it part of our community.
You always have the legal alternative of not using our code.
What is the difference between "mere aggregation" and "combining two modules into one program"?

Mere aggregation of two programs means putting them side by side on the same CD-ROM or hard disk. We use this term in the case where they are separate programs, not parts of a single program. In this case, if one of the programs is covered by the GPL, it has no effect on the other program.
Combining two modules means connecting them together so that they form a single larger program. If either part is covered by the GPL, the whole combination must also be released under the GPL--if you can't, or won't, do that, you may not combine them.
I want to keep my source code closed. Thus the reason I need to develop my own bootloader.
User avatar
AndrewAPrice
Member
Member
Posts: 2309
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post by AndrewAPrice »

Don't distribute it with Grub, rather say you need a multiboot-compliant bootloader, and recommend Grub.
My OS is Perception.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Post by 01000101 »

that would work, except it cant in my situation.

ok, ill list the specifics.

* DiNS gets shipped on proprietary hardware ready-to-go.
* there is no video output or keyboard support (no-UI, security reasons)
* It must be my code so that no liscencing gets tangled up.

also, GRUB is waayy too loaded for what I need a bootloader to do, its overkill. I just need a simple 16-bit loader to load my OS off of a disk to an arbitrary location in memory (below 1m obviously).

Then my OS will take over, load the GDT, IDT, ISRs, switch into PMode and re-load the OS somewhere else in higher memory.

I am having a realllly hard time finding a good bootloader example source that loades a BIN off of a floppy. If I could find a decent one I could learn from that and fit whatever else I know about it together to create one of my own.
User avatar
AndrewAPrice
Member
Member
Posts: 2309
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post by AndrewAPrice »

What you need is part 1 of the 99% of osdev tutorials out there.
My OS is Perception.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Post by 01000101 »

yea, tell me about it.
I developed my OS backwards.

the things that I should know first, I learned last.

Do you have any fitting tutorials in mind?
User avatar
AndrewAPrice
Member
Member
Posts: 2309
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post by AndrewAPrice »

None that come to me straight away, but I am sure I have seen plenty, since most OSdev tutes don't get any further than writing a bootloader that loads a C "Hello World" kernel from a FAT12 disk.

I found the following bootloader that might be what you need (I didn't write this code):

Code: Select all

; aiorOS bootsector
; <[email protected]>
;
; Memory structure
;
;|            |               |      /\
;|            |               |      ||
;|   KERNEL   |               |      || (max 4GB)
;| (max 4GB)  |               |      ||
;|(cod.& dat.)|               | KERNEL CODE, DATA & STACK
;|            |               |               
;|------------| 0x1000:0x0000 | (pmode 0x08:0x00010000) ----------------
;|            |               |  
;|            |               |  GDT + IDT + PAGEDIR 
;|            |               |      ||
;|------------| 0x07C0:0x0200 |      ||
;| BOOTSECTOR |               |      ||
;|------------| 0x07C0:0x0000 |      || (max 64KB)
;|            |               |      ||
;|------------|               |      ||
;| 16bit BIOS |               |      \/
; ------------  0x0000:0x0000 |-----------------------------------------

KERNEL_SIZE EQU 0x100 ;(512byte sector unit, 0x100 sector = 128KB)
KERNEL_START16 EQU 0x1000;:0
KERNEL_START32 EQU 0x10000
 
[BITS 16]
[ORG 0x7C00]

jmp start

msg_dot        db '.',0
msg_loading db 13,10,'Loading aiorOS kernel...',0

print_message:
   pusha ;save all
   mov ah,0Eh
 string_loop:
   lodsb ;loads a byte from DS:SI to AL
   cmp al,00
   je end_string ;check for terminating null character
   int 10h
   jmp string_loop
 end_string:
   popa ;restore all
   ret;Ret returns

kill_floppy_motor:
   pusha
   mov al, 0x0C
   mov dx, 0x3F2
   out dx, al
   popa
   ret
   
debug:
   pusha
   mov ax, 0x0E44
   int 0x10
   debug_loop: jmp debug_loop
   popa
   ret

start:

   ;setting stack base:offset (SS:SP)
   mov ax, 0x0
   mov ss, ax
   mov ds, ax
   mov ax, 0x7C00
   mov sp, ax 

   ;switch to graph mode VGA 320x200 256col
   ;mov ax, 0x0013
   ;int 10h

   ;loading kernel

   ;setting up data buffer for
   ;kernel size in sectors (512byte) 
   mov si, KERNEL_SIZE
   ;kernel ar ES:BX = KERNEL_START16:0 (flat=KERNEL_START32) 
   mov ax, KERNEL_START16
   mov es, ax
   mov ax, 0x0
   mov bx, ax
   ;CH=cylinder, CL=start sector
   mov cx,0x0002
   ;DH=head number=0, DL=drive number
   mov dx, ax

   push si
   mov si, msg_loading
   call print_message
   pop si

read_sector_dot:
   ; print a dot on the screen
   push si
   mov si, msg_dot
   call print_message
   pop si

read_sector:

   ;AH = 02h, AL=number of sector to read = 1
   mov ax, 0x0201
   ;read
   int 0x13

   ;increment data buffer segment base ES (allow kernel size up to 1Mb)
   mov ax, es
   add ax, 0x20 ;segments overlap every 16bytes, 512/16 = 32 = 0x20
   mov es, ax

   dec si
   jz end_of_read

   ;next sector to read
   inc cl
   ;check if we are at the end of track (18 sect)
   cmp cl, 0x12
   ;jump to read_sector if below or equal
   jbe read_sector
   ;reset sector to 1
   mov cl, 1
   ;heads change in alternate way (0-1-0-1...)
   inc dh ;dh = (dh + 1) MOD 2
   cmp dh, 2
   jne read_sector_dot
   mov dh, 0
   inc ch ;goto next cylinder number
   jmp read_sector_dot

end_of_read:

   call kill_floppy_motor

   ;enabling A20
delay1:
   in al, 0x64
   test al, 2
   jnz delay1
   mov al, 0xD1
   out 0x64, al
delay2:
   in al, 0x64
   and ax, byte 2
   jnz delay2
   mov al, 0xDF
   out 0x60, al

   cli ;disable interrupts
   
   lgdt [gdt_48]
   lidt [idt_48]

   ;enter in protected mode
   mov eax, cr0
   or eax,1
   mov cr0, eax
     
   ;IA 386 suggest to make a jmp after the
   ;swith to pmode in order to clear the pipeline
   jmp 0x8 : protected_mode
   
[BITS 32]   
protected_mode:

   ;init data segment to gdt[2]
   mov ax,0x10
   mov ds, ax
   mov es, ax
   mov fs, ax
   mov gs, ax
   
   ;init stack segment to gdt[2] too
   mov ss, ax
   mov esp, 0xA0000 ;use this area for stack (640KB->0KB)

   jmp 0x8 : KERNEL_START32 ;do not use call

; IDT Register 48bit
; _______________________________________
;|       IDT 0-31       |    SIZE 0-15   |   
; _______________________________________  
idt_48:
   dw 0
   dd 0
   
; GDT Register 48bit
; _______________________________________
;|       GDT 0-31       |    SIZE 0-15   |   
; _______________________________________  
gdt_48:
   dw gdt_end - gdt - 1 ;gdt size
   dd gdt               ;gdt address

; GDT GATE
; 31         24       19           15                   7          0      
; |__________|_________|___________|____________________|__________|
; |BASE 31-24|G|D|0|AVL|LIMIT 16-19|P|DPL|S|    Type    |BASE 16-23|   
; |________________________________________________________________   
; |           BASE 15-0            |          LIMIT 0-15           |
;  ________________________________________________________________
      
gdt:                    ; Address for the GDT

gdt_null:               ; Null Segment
        dd 0
        dd 0

gdt_code:               ; Code segment, read/execute
        dw 0FFFFh       ; LIMIT 0-15 = 0xFFFF
        dw 0            ; BASE 0-15 = 0
        db 0            ; BASE 16-23
        db 10011010b    ; P=1, DPL=00, S=1, Type=1010 (Code Read/Execute)
        db 11001111b    ; G=1 (4kb unit), D=1, 0, AVL=1, LIMIT 16-19 = 0xF
        db 0            ; BASE 31-24 = 0

gdt_data:               ; Data segment, read/write
        dw 0FFFFh       ; LIMIT 0-15 = 0xFFFF
        dw 0            ; BASE 0-15 = 0
        db 0            ; BASE 16-23
        db 10010010b    ; P=1, DPL=00, S=1, Type=0010 (Data Read/Write)
        db 11001111b    ; G=1 (4kb unit), D=1, 0, AVL=1, LIMIT 16-19 = 0xF
        db 0            ; BASE 31-24 = 0 

gdt_end:                ; Used to calculate the size of the GDT      
 
times 510 - ($-$$) db 0
dw 0xAA55
Last edited by AndrewAPrice on Mon Nov 05, 2007 5:49 am, edited 1 time in total.
My OS is Perception.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Post by 01000101 »

eureka!
Finally a bootsector example that shows what I needed to see.

Thank you very much. :D
User avatar
AndrewAPrice
Member
Member
Posts: 2309
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post by AndrewAPrice »

You're welcome!

I have the sources to a few dozen different OS's on my computer. I have no clue why. It makes it easy if I want to look up the best way to do something (e.g. how to read FAT12 disks or handle the paging) since I can bring up their source code to compare within a few clicks.
My OS is Perception.
Post Reply