Home arrow Forums
OSDEV Forums  


EddyDean
Visitor
 
Compilation, linking and using grub - 2007/06/21 15:20 Hello everyone,

First of all I'd like to introduce myself. Hello, I'm EddyDean (well, not really...), 15 years old, living in the Netherlands. I started programming when I was 11, in QBasic. Later I switched to several other languages including C, C++, Perl, PHP, ASM, Java and Visual Basic.
A few days ago I decided that it was a great, if not brilliant plan to start creating my own OS. I have a few great plans in my mind, but I know I might never finish it.

I have one problem though: I don't have a floppy drive, and I can't seem to be able to tell grub to load my kernel.
The "kernel" I'm using is a copy-paste of one of the tutorials I read (don't worry, I understand the code). It's one part ASM:

loader.s:
Code:

  global _loader           making entry point visible to linker extern _main             _main is defined elsewhere setting up the Multiboot header see GRUB docs for details MODULEALIGN equ  1<<0                   align loaded modules on page boundaries MEMINFO     equ  1<<1                   provide memory map FLAGS       equ  MODULEALIGN MEMINFO  this is the Multiboot 'flag' field MAGIC       equ    0x1BADB002           'magic number' lets bootloader find the header CHECKSUM    equ -(MAGIC FLAGS)        ; checksum required section .text align 4 MultiBootHeader:    dd MAGIC    dd FLAGS    dd CHECKSUM reserve initial kernel stack space STACKSIZE equ 0x4000          that's 16k. _loader:    mov esp, stack+STACKSIZE           ; set up the stack    push eax                           ; pass Multiboot magic number    push ebx                           ; pass Multiboot info structure    call  _main                        ; call kernel proper                hlt                    ; halt machine should kernel return section .bss align 32 stack:    resb STACKSIZE      ; reserve 16k stack on a quadword boundary



And one part C (umm... Yeah...)
kernel.c
Code:

  void _mainvoidmbdunsigned int magic ) {    // write your kernel here }



And there is a linker script:
linker.ld:
Code:

  ENTRY (_loader) SECTIONS{     . = 0x00100000;     .text :{         *(.text)     }     .rodata ALIGN (0x1000) : {         *(.rodata)     }     .data ALIGN (0x1000) : {         *(.data)     }     .bss : {         _sbss = .;         *(COMMON)         *(.bss)         _ebss = .;     } }



I compile the ASM code with nasm (to ELF)
The C code withy gcc (with loads of parameters to not include any default libraries)
The I link the while thing together using ld.
Now I have a file named booter (o yeah)
dd if=/dev/zero of=floppy.disk bs=18k count=80
cat stage1 stage2 booter >files
dd if=files of=floppy.disk conv=notrunc

Then I boot floppy.disk in Bochs, it shows me a nice grub prompt, and then... Nothing. I can't do root (fd0) and then kernel /booter because it can't find the file
find * shows nothing. Where did my kernel go?
As I said before: I do not have a floppy drive. I do have a CD-ROM drive that can use rewriteable CDs. I would actually prefer creating a partition (fat12/16/32 will do as a filesystem, I don't need anything fancy), where I can just copy the kernel and grub to and boot from it. How hard is this to do, and how do I find more information about it?

I am using a fully updated FreeBSD 6.1-RELEASE system.

Thanks in advance,
EddyDean
  | | The administrator has disabled public write access.
OSDEV
Community
Advertisement
   
gaf
User

Platinum Osdever
Posts: 153
graph
Karma: 10  
Re:Compilation, linking and using grub - 2007/06/22 13:22 Welcome to the forums !

Then I boot floppy.disk in Bochs, it shows me a nice grub prompt, and then... Nothing. I can't do root (fd0) and then kernel /booter because it can't find the file find * shows nothing.

Since you haven't created a filesystem GRUB has no way to locate your kernel:

(0) Once the computer is started the BIOS loads stage1 from the first sector of the disk
(1) Stage1 must not be larger than 512 bytes,therefore all it does is loading stage2
(2) Stage2 mounts the filesystem and searches for the GRUB configuration file
(3) The boot entries listed in the config file are displayed in the prompt
(4) Once a system has been selected the kernel is loaded from the path specified in the configuration file

The osdev wiki provides some detailed info on how to install GRUB to a floppy image. Have a look at this (slightly modified) list of instructions that are needed for the setup:

Code:

 dd if=/dev/zero of=floppy.img bs=512 count=2880    // Create an empty image file losetup /dev/loop0 floppy.img                      // Connect it to a loop device mkfs  -t msdos /dev/loop0                          // Write a filesystem to the image mkdir -/mnt/image/                               // Create a directory to mount the image mount -t msdos /dev/loop0 /mnt/image/              // Mount the image as /mnt/image/ mkdir -/mnt/image/boot/grub/                     // Create a folder for the GRUB files cp stage1 stage2 /mnt/image/boot/grub/             // Copy stage1 and stage2 into the folder vim/nano/emacs menu.lst                            // Edit the configuration file timeout 5                                        // Lenght of the countdown in seconds > default 0                                        // By default the 1st boot entry will be loaded >  > title HobbyOS                                    // The name of your operating system root (fd0)                                       // Set floppy as root device kernel /kernel.elf                               // Relative path to your kernel cp menu.lst /mnt/image/boot/grub/                  // Copy the menu file to the image cp kernel.elf /mnt/image/                          // Copy your kernel to the loop device grub --batch --device-map=/dev/null << EOF         // Start GRUB setup terminal device (fd0) /dev/loop0                          // Create a device map root   (fd0)                                     // Set a boot device setup  (fd0)                                     // Install the bootloader quit > > EOF                                              // Leave the terminal umount /dev/loop0                                  // Unmount the image losetup -/dev/loop0                              // Detach loop device bochs -q                                           // Start bochs



This look much more complex than it really is

I would actually prefer creating a partition (fat12/16/32 will do as a filesystem, I don't need anything fancy), where I can just copy the kernel and grub to and boot from it. How hard is this to do, and how do I find more information about it?

In case that you're already using GRUB to load BSD there should be no need to create a separate partition. By editing the configuration file (/boot/grub/menu.lst) you can tell GRUB to load you kernel from anywhere on your BSD filesystem:

Code:

 title           Debian GNU/Linuxkernel 2.6.18-4-686 root            (hd0,0) kernel          /boot/vmlinuz-2.6.18-4-686 root=/dev/hda1 ro vga=791 splash=silent  initrd          /boot/initrd.img-2.6.18-4-686 savedefault title           Debian GNU/Linuxkernel 2.6.18-4-686 (single-user mode) root            (hd0,0) kernel          /boot/vmlinuz-2.6.18-4-686 root=/dev/hda1 ro vga=791 splash=silent single initrd          /boot/initrd.img-2.6.18-4-686 savedefault # Edit: Append the following lines to the original file title           HobbyOS                         # This will be displayed as a boot entry root            (hd00)                        # The GRUB format is (hard-disk number, partition number) kernel          /home/user/../kernel.elf        # Path to your multiboot kernel



Just ask again if you still want to create a separate partition..

regards,
gaf
  | | The administrator has disabled public write access.
EddyDean
User

Fresh Osdever
Posts: 1
graphgraph
Karma: 0  
Re:Compilation, linking and using grub - 2007/06/23 10:39 Thank you. This is what I ended up with (FreeBSD is a bit different from Linux)

Code:

 nasm -f aout loader.-o loader.o gcc -o kernel.-c kernel.-Wall -Werror -nostdlib -nostartfiles -nodefaultlibs ld -T linker.ld loader.o kernel.o dd if=/dev/zero of=floppy.img bs=512 count=2880 mdconfig --t vnode -f floppy.img newfs_msdos -f 1440 /dev/md0 mkdir /mnt/floppy mount -t msdosfs /dev/md0 /mnt/floppy mkdir /mnt/floppy/boot mkdir /mnt/floppy/boot/grub cp /boot/stage1 /mnt/floppy/boot/grub cp /boot/stage1 /mnt/floppy/boot/grub cp /boot/fat_stage1_5 /mnt/floppy/boot/grub cp a.out /mnt/floppy/kernel.elf grub --batch --device-map=/dev/null -> device (fd0) /dev/md0 -> root (fd0) -> setup (fd0) -> quit umount /mnt/floppy mdconfig --u md0 bochs -q



This is without menu.lst.

Now let's start creating something... Whew...

Thanks a lot!
  | | The administrator has disabled public write access.

A WebArticles site. Sponsored by Evoleto. Motorola V525 / Business Directory / Delaware Incorporation / Home Made Bazaar