Bootloader

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.
shikhin
Member
Member
Posts: 274
Joined: Sat Oct 09, 2010 3:35 am
Libera.chat IRC: shikhin
Contact:

Bootloader

Post by shikhin »

Hi Ladies and Gentlemen,

Let me introduce myself before I introduce my problem. That would create a base for you. I am Shikhin Sethi, a 12 year old programmer, with expeience in C, C++, Java and a little of MySQL. Currently I took up Assembly Language. After a while I got on with trying to make up a OS. Believe me, I know it would take upto 4-6 years, and I also know it wouldnt be the next Windows. Also I am sure I would be pretty simplistic with my design.

So I just started with reading up various articles by searching on google, and various other sites. After much reading on the basics, I decided to go practical. I found up this site at the end.

Even though this site was based on Floppy Disks, I was pretty sure I would manage to cope up. This hope vanished when I reached the 5th/6th part of tutorial.

These both tutorial focused on multi-stage bootloaders using floppies, and includes a OEM block for the formatting. Well it couldn't work on CD Rom even when I tried to change the OEM block specification to a CD ROM.

Probably you could tell me what shall I do to make it work. I even tried emulating a floppy disk and used mkisofs but still it didnt work. I also tried using the floppy code only but that also didnt work. I hope you could give me some suggestion; the source code; the tutorial link; anything but I WANT HELP. :(

So do you guys have any ideas on what shall I do on developing this mutli stage bootloaders. Probably and ideas on this OEM block. Probably some other good tutorials.

Regards,
Shikhin
Last edited by shikhin on Tue Dec 04, 2012 11:17 am, edited 1 time in total.
http://shikhin.in/

Current status: Gandr.
User avatar
quanganht
Member
Member
Posts: 301
Joined: Fri May 16, 2008 7:13 pm
Location: Hanoi, Vietnam

Re: Bootloader

Post by quanganht »

You have to be specific. What kind of problems do you have? How your computer (or virtual machine) behaves? Have you tried Bochs and see what you've got in the debugger? Don't just scream "HELP" because we won't be able to help you unless *you* help yourself first, by doing more readings and try various methods to see what's going on.

Anyway, You're good at that age. Welcome to the community =D>
QA

PS: I also recommend learning basic Assembly so u can really *understand* why the code have to be like that, not like this :wink:
"Programmers are tools for converting caffeine into code."
shikhin
Member
Member
Posts: 274
Joined: Sat Oct 09, 2010 3:35 am
Libera.chat IRC: shikhin
Contact:

Re: Bootloader

Post by shikhin »

Hi QA,

Sorry if I wasnt speific. Tried my best. :(

As for the reading part, believe me, I have read loads in the past 24 hours. So my problem is this:

I took up assembly and tried to make a simple bootloader.

When I reached a stage where the bootloader reads a Stage 2 file from the disk, I couldnt go any further. This is because everytime I try it displays: "Error: File not found". I guess this is because the tutorial I am trying (whose link I have given) uses Floppy disks as a base, while I am using CD ROM's. If you want I can even attach the code used up here, even though it is availaible for download on the second link I have provided.

So I tried emulating a floppy diskette, and used mkisofs to build a floppy iso, but that also didnt solve the problem, and it still displays error message. :'(

So I guess, maybe the problem is that I am using CD ROM's while they are using Floppies as a base. They have included a OEM block in their code, which defines all the specification as that in a Floppy diskette.

So I want some help on where to find how to make a bootloader which would load a Stage2 file from the CD. I hope someone would be able to help.

I hope this defines my problem clearly. If not, dont hesitate, tell me, and I would spend some more time, and would explain each part of my problem.

Regards,
Shikhin
http://shikhin.in/

Current status: Gandr.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Bootloader

Post by JamesM »

Hi,
Shikhin wrote:Hi QA,

Sorry if I wasnt speific. Tried my best. :(

As for the reading part, believe me, I have read loads in the past 24 hours. So my problem is this:

I took up assembly and tried to make a simple bootloader.

When I reached a stage where the bootloader reads a Stage 2 file from the disk, I couldnt go any further. This is because everytime I try it displays: "Error: File not found". I guess this is because the tutorial I am trying (whose link I have given) uses Floppy disks as a base, while I am using CD ROM's. If you want I can even attach the code used up here, even though it is availaible for download on the second link I have provided.

So I tried emulating a floppy diskette, and used mkisofs to build a floppy iso, but that also didnt solve the problem, and it still displays error message. :'(

So I guess, maybe the problem is that I am using CD ROM's while they are using Floppies as a base. They have included a OEM block in their code, which defines all the specification as that in a Floppy diskette.

So I want some help on where to find how to make a bootloader which would load a Stage2 file from the CD. I hope someone would be able to help.

I hope this defines my problem clearly. If not, dont hesitate, tell me, and I would spend some more time, and would explain each part of my problem.

Regards,
Shikhin
CD-ROMs are different from floppy disks in that they can't just be formatted with any old filesystem - they use the ISO 9660 filesystem (with possible Joliet extensions).

If you want to enable your bootloader to support CD-ROMs, the links will get you going; however the iso9660 filesystem is nontrivial - may I suggest one of the two alternative options?

* Use a pre-made bootloader that supports ISO9660 (like GRUB), start by coding a kernel, then once you've become more proficient (and you still want to!) go back and code a bootloader.

* Use an emulator such as qemu to boot your bootloader while playing around with it. I know this doesn't have anywhere near the satisfaction of seeing your OS run on a real system, so I'd suggest the other ^ option personally.

James
shikhin
Member
Member
Posts: 274
Joined: Sat Oct 09, 2010 3:35 am
Libera.chat IRC: shikhin
Contact:

Re: Bootloader

Post by shikhin »

Hi James,

Well I guess then thats the way as for now. I guess after some time, I would understand this more efficiently, and would then go on to the bootloader part.

For your suggestion, can you tell me where can I read about how to use GRUB with a CD ROM to boot of the second stage file.
http://shikhin.in/

Current status: Gandr.
HitmanYesman
Member
Member
Posts: 47
Joined: Fri Apr 23, 2010 8:27 am

Re: Bootloader

Post by HitmanYesman »

A couple things you could do:
- Read up on the ISO 9660 Filesystem, and learn from that how to read off of a cd. Also this link may be helpful: http://wiki.osdev.org/ISO_9660 . At the bottom of the page it tells you step by step what to do (without the code of course).
- How are you attempting to emulate a floppy disk? Because just saying it "doesn't work" isn't very helpful. I had a lot of trouble getting it to emulate as well, but in the end I ended up doing a few things. Now assuming that you are using Cygwin along with Makefile / GCC, compiled your files into .bin . Download a program called BFI, and using that it will create a floppy image. Be sure to have a folder so you that BFI can inject any extra files into the floppy image (such as the second file). Then just use mkisofs to do the rest.
shikhin
Member
Member
Posts: 274
Joined: Sat Oct 09, 2010 3:35 am
Libera.chat IRC: shikhin
Contact:

Re: Bootloader

Post by shikhin »

HitmanYesman wrote:A couple things you could do:
- Read up on the ISO 9660 Filesystem, and learn from that how to read off of a cd. Also this link may be helpful: http://wiki.osdev.org/ISO_9660 . At the bottom of the page it tells you step by step what to do (without the code of course).
- How are you attempting to emulate a floppy disk? Because just saying it "doesn't work" isn't very helpful. I had a lot of trouble getting it to emulate as well, but in the end I ended up doing a few things. Now assuming that you are using Cygwin along with Makefile / GCC, compiled your files into .bin . Download a program called BFI, and using that it will create a floppy image. Be sure to have a folder so you that BFI can inject any extra files into the floppy image (such as the second file). Then just use mkisofs to do the rest.

I am doing exactly the same. :)
http://shikhin.in/

Current status: Gandr.
User avatar
quanganht
Member
Member
Posts: 301
Joined: Fri May 16, 2008 7:13 pm
Location: Hanoi, Vietnam

Re: Bootloader

Post by quanganht »

Can you post your bootloader code up here? If you are testing with a floopy image formated in FAT, then there can be 2 source of the error "File not found": it's either your bootloader code got problem or you havent put a second stage into that image.
And I highly doubt that u can create a floopy image with mkisofs. Sounds unlikely. For better OS developing environtment, you should grab a copy of Linux instead of Windoze :wink:
"Programmers are tools for converting caffeine into code."
shikhin
Member
Member
Posts: 274
Joined: Sat Oct 09, 2010 3:35 am
Libera.chat IRC: shikhin
Contact:

Re: Bootloader

Post by shikhin »

Well here is the code:

Code: Select all

;*********************************************

;	Boot1.asm

;		- A Simple Bootloader

;

;	Operating Systems Development Series

;*********************************************



bits	16						; we are in 16 bit real mode



org	0						; we will set regisers later



start:	jmp	main					; jump to start of bootloader



;*********************************************

;	BIOS Parameter Block

;*********************************************



; BPB Begins 3 bytes from start. We do a far jump, which is 3 bytes in size.

; If you use a short jump, add a "nop" after it to offset the 3rd byte.



bpbOEM			db "My OS   "			; OEM identifier (Cannot exceed 8 bytes!)

bpbBytesPerSector:  	DW 512

bpbSectorsPerCluster: 	DB 1

bpbReservedSectors: 	DW 1

bpbNumberOfFATs: 	DB 2

bpbRootEntries: 	DW 224

bpbTotalSectors: 	DW 2880

bpbMedia: 		DB 0xf8  ;; 0xF1

bpbSectorsPerFAT: 	DW 9

bpbSectorsPerTrack: 	DW 18

bpbHeadsPerCylinder: 	DW 2

bpbHiddenSectors: 	DD 0

bpbTotalSectorsBig:     DD 0

bsDriveNumber: 	        DB 0

bsUnused: 		DB 0

bsExtBootSignature: 	DB 0x29

bsSerialNumber:	        DD 0xa0a1a2a3

bsVolumeLabel: 	        DB "MOS FLOPPY "

bsFileSystem: 	        DB "FAT12   "



;************************************************;

;	Prints a string

;	DS=>SI: 0 terminated string

;************************************************;

Print:

			lodsb				; load next byte from string from SI to AL

			or	al, al			; Does AL=0?

			jz	PrintDone		; Yep, null terminator found-bail out

			mov	ah, 0eh			; Nope-Print the character

			int	10h

			jmp	Print			; Repeat until null terminator found

	PrintDone:

			ret				; we are done, so return



;************************************************;

; Reads a series of sectors

; CX=>Number of sectors to read

; AX=>Starting sector

; ES:BX=>Buffer to read to

;************************************************;



ReadSectors:

     .MAIN

          mov     di, 0x0005                          ; five retries for error

     .SECTORLOOP

          push    ax

          push    bx

          push    cx

          call    LBACHS                              ; convert starting sector to CHS

          mov     ah, 0x02                            ; BIOS read sector

          mov     al, 0x01                            ; read one sector

          mov     ch, BYTE [absoluteTrack]            ; track

          mov     cl, BYTE [absoluteSector]           ; sector

          mov     dh, BYTE [absoluteHead]             ; head

          mov     dl, BYTE [bsDriveNumber]            ; drive

          int     0x13                                ; invoke BIOS

          jnc     .SUCCESS                            ; test for read error

          xor     ax, ax                              ; BIOS reset disk

          int     0x13                                ; invoke BIOS

          dec     di                                  ; decrement error counter

          pop     cx

          pop     bx

          pop     ax

          jnz     .SECTORLOOP                         ; attempt to read again

          int     0x18

     .SUCCESS

          mov     si, msgProgress

          call    Print

          pop     cx

          pop     bx

          pop     ax

          add     bx, WORD [bpbBytesPerSector]        ; queue next buffer

          inc     ax                                  ; queue next sector

          loop    .MAIN                               ; read next sector

          ret



;************************************************;

; Convert CHS to LBA

; LBA = (cluster - 2) * sectors per cluster

;************************************************;



ClusterLBA:

          sub     ax, 0x0002                          ; zero base cluster number

          xor     cx, cx

          mov     cl, BYTE [bpbSectorsPerCluster]     ; convert byte to word

          mul     cx

          add     ax, WORD [datasector]               ; base data sector

          ret

     

;************************************************;

; Convert LBA to CHS

; AX=>LBA Address to convert

;

; absolute sector = (logical sector / sectors per track) + 1

; absolute head   = (logical sector / sectors per track) MOD number of heads

; absolute track  = logical sector / (sectors per track * number of heads)

;

;************************************************;



LBACHS:

          xor     dx, dx                              ; prepare dx:ax for operation

          div     WORD [bpbSectorsPerTrack]           ; calculate

          inc     dl                                  ; adjust for sector 0

          mov     BYTE [absoluteSector], dl

          xor     dx, dx                              ; prepare dx:ax for operation

          div     WORD [bpbHeadsPerCylinder]          ; calculate

          mov     BYTE [absoluteHead], dl

          mov     BYTE [absoluteTrack], al

          ret



;*********************************************

;	Bootloader Entry Point

;*********************************************



main:



     ;----------------------------------------------------

     ; code located at 0000:7C00, adjust segment registers

     ;----------------------------------------------------

     

          cli						; disable interrupts

          mov     ax, 0x07C0				; setup registers to point to our segment

          mov     ds, ax

          mov     es, ax

          mov     fs, ax

          mov     gs, ax



     ;----------------------------------------------------

     ; create stack

     ;----------------------------------------------------

     

          mov     ax, 0x0000				; set the stack

          mov     ss, ax

          mov     sp, 0xFFFF

          sti						; restore interrupts



     ;----------------------------------------------------

     ; Display loading message

     ;----------------------------------------------------

     

          mov     si, msgLoading

          call    Print

          

     ;----------------------------------------------------

     ; Load root directory table

     ;----------------------------------------------------



     LOAD_ROOT:

     

     ; compute size of root directory and store in "cx"

     

          xor     cx, cx

          xor     dx, dx

          mov     ax, 0x0020                           ; 32 byte directory entry

          mul     WORD [bpbRootEntries]                ; total size of directory

          div     WORD [bpbBytesPerSector]             ; sectors used by directory

          xchg    ax, cx

          

     ; compute location of root directory and store in "ax"

     

          mov     al, BYTE [bpbNumberOfFATs]            ; number of FATs

          mul     WORD [bpbSectorsPerFAT]               ; sectors used by FATs

          add     ax, WORD [bpbReservedSectors]         ; adjust for bootsector

          mov     WORD [datasector], ax                 ; base of root directory

          add     WORD [datasector], cx

          

     ; read root directory into memory (7C00:0200)

     

          mov     bx, 0x0200                            ; copy root dir above bootcode

          call    ReadSectors



     ;----------------------------------------------------

     ; Find stage 2

     ;----------------------------------------------------



     ; browse root directory for binary image

          mov     cx, WORD [bpbRootEntries]             ; load loop counter

          mov     di, 0x0200                            ; locate first root entry

     .LOOP:

          push    cx

          mov     cx, 0x000B                            ; eleven character name

          mov     si, ImageName                         ; image name to find

          push    di

     rep  cmpsb                                         ; test for entry match

          pop     di

          je      LOAD_FAT

          pop     cx

          add     di, 0x0020                            ; queue next directory entry

          loop    .LOOP

          jmp     FAILURE



     ;----------------------------------------------------

     ; Load FAT

     ;----------------------------------------------------



     LOAD_FAT:

     

     ; save starting cluster of boot image

     

          mov     si, msgCRLF

          call    Print

          mov     dx, WORD [di + 0x001A]

          mov     WORD [cluster], dx                  ; file's first cluster

          

     ; compute size of FAT and store in "cx"

     

          xor     ax, ax

          mov     al, BYTE [bpbNumberOfFATs]          ; number of FATs

          mul     WORD [bpbSectorsPerFAT]             ; sectors used by FATs

          mov     cx, ax



     ; compute location of FAT and store in "ax"



          mov     ax, WORD [bpbReservedSectors]       ; adjust for bootsector

          

     ; read FAT into memory (7C00:0200)



          mov     bx, 0x0200                          ; copy FAT above bootcode

          call    ReadSectors



     ; read image file into memory (0050:0000)

     

          mov     si, msgCRLF

          call    Print

          mov     ax, 0x0050

          mov     es, ax                              ; destination for image

          mov     bx, 0x0000                          ; destination for image

          push    bx



     ;----------------------------------------------------

     ; Load Stage 2

     ;----------------------------------------------------



     LOAD_IMAGE:

     

          mov     ax, WORD [cluster]                  ; cluster to read

          pop     bx                                  ; buffer to read into

          call    ClusterLBA                          ; convert cluster to LBA

          xor     cx, cx

          mov     cl, BYTE [bpbSectorsPerCluster]     ; sectors to read

          call    ReadSectors

          push    bx

          

     ; compute next cluster

     

          mov     ax, WORD [cluster]                  ; identify current cluster

          mov     cx, ax                              ; copy current cluster

          mov     dx, ax                              ; copy current cluster

          shr     dx, 0x0001                          ; divide by two

          add     cx, dx                              ; sum for (3/2)

          mov     bx, 0x0200                          ; location of FAT in memory

          add     bx, cx                              ; index into FAT

          mov     dx, WORD [bx]                       ; read two bytes from FAT

          test    ax, 0x0001

          jnz     .ODD_CLUSTER

          

     .EVEN_CLUSTER:

     

          and     dx, 0000111111111111b               ; take low twelve bits

         jmp     .DONE

         

     .ODD_CLUSTER:

     

          shr     dx, 0x0004                          ; take high twelve bits

          

     .DONE:

     

          mov     WORD [cluster], dx                  ; store new cluster

          cmp     dx, 0x0FF0                          ; test for end of file

          jb      LOAD_IMAGE

          

     DONE:

     

          mov     si, msgCRLF

          call    Print

          push    WORD 0x0050

          push    WORD 0x0000

          retf

          

     FAILURE:

     

          mov     si, msgFailure

          call    Print

          mov     ah, 0x00

          int     0x16                                ; await keypress

          int     0x19                                ; warm boot computer

     

     absoluteSector db 0x00

     absoluteHead   db 0x00

     absoluteTrack  db 0x00

     

     datasector  dw 0x0000

     cluster     dw 0x0000

     ImageName   db "krnldr  sys"

     msgLoading  db 0x0D, 0x0A, "Loading Boot Image ", 0x0D, 0x0A, 0x00

     msgCRLF     db 0x0D, 0x0A, 0x00

     msgProgress db ".", 0x00

     msgFailure  db 0x0D, 0x0A, "ERROR : Press Any Key to Reboot", 0x0A, 0x00

     

          TIMES (512-2) -($-$$) DB 0
 
          DW 0xAA55
By the way, since everyone is saying maybe I did a problem in making the image, can somebody tell me how to do it exactly, with all the things in detail, starting from making my file, to burning it. [P.S. I dont want that link to the wiki as I have already read that and I THINK I did everything according to it]
http://shikhin.in/

Current status: Gandr.
shikhin
Member
Member
Posts: 274
Joined: Sat Oct 09, 2010 3:35 am
Libera.chat IRC: shikhin
Contact:

Re: Bootloader

Post by shikhin »

Can anyone also tell me where to find how to use GRUB in your own OS and use it to boot off the disk?
http://shikhin.in/

Current status: Gandr.
Gaidheal
Member
Member
Posts: 51
Joined: Mon Oct 04, 2010 6:23 pm

Re: Bootloader

Post by Gaidheal »

GRUB documentation is quite extensive; start there.

Also, I'd suggest learning to write basic applications with your preferred dialect of assembler before attempting an ambitious project using it.
shikhin
Member
Member
Posts: 274
Joined: Sat Oct 09, 2010 3:35 am
Libera.chat IRC: shikhin
Contact:

Re: Bootloader

Post by shikhin »

Gaidheal wrote:GRUB documentation is quite extensive; start there.

Also, I'd suggest learning to write basic applications with your preferred dialect of assembler before attempting an ambitious project using it.
Opening GRUB documentation simultaenously....

And by the way, believe me, I know what I am doing. :) If I didnt, I wouldnt be here.
http://shikhin.in/

Current status: Gandr.
Gaidheal
Member
Member
Posts: 51
Joined: Mon Oct 04, 2010 6:23 pm

Re: Bootloader

Post by Gaidheal »

Shikhin wrote:And by the way, believe me, I know what I am doing. :) If I didnt, I wouldnt be here.
You'd be surprised... ;¬)
shikhin
Member
Member
Posts: 274
Joined: Sat Oct 09, 2010 3:35 am
Libera.chat IRC: shikhin
Contact:

Re: Bootloader

Post by shikhin »

Gaidheal wrote:
Shikhin wrote:And by the way, believe me, I know what I am doing. :) If I didnt, I wouldnt be here.
You'd be surprised... ;¬)
Lets see.... :roll:
http://shikhin.in/

Current status: Gandr.
shikhin
Member
Member
Posts: 274
Joined: Sat Oct 09, 2010 3:35 am
Libera.chat IRC: shikhin
Contact:

Re: Bootloader

Post by shikhin »

Hello All,

Another problem has got up with me. I just set up my GRUB bootloader, and now its showing up the *so-called* [as i read in other forums] dreaded Error 13: Executable Format not supported by GRUB. I was trying GRUB to boot strap a file known as "KERNEL.SYS". On this file format it showed this message. Can anyone tell me what all executable formats are supported by GRUB, and what shall I do?
http://shikhin.in/

Current status: Gandr.
Post Reply