OS Dev Beginner

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.
Post Reply
rhughes
Posts: 16
Joined: Thu Apr 22, 2010 7:01 am

OS Dev Beginner

Post by rhughes »

Hello All

Thanks for your time reading my post :D

I have always wanted to make an OS, no matter how simple. I stubled upon this site yesterday which made me very happy! I looked around and found the bare bones tutorials which I found very helpful.

I am having a problem with the C tutorial though.

My dev environment is Windows 7 x64. I'm using nasm and gcc.

I am getting a common error it seems when using Bochs - Error: 13 Invalid or unsupported executable format

I have searched this forum for the solution and get confused about the answer. They seem to suggest using COFF for NASM and insering AOUT kludge into my loader. I have googled AOUT kludge and can't seem to see anything I can understand about it.

Here is the relavent code and batch files i'm using. Attached are screen shots of Bochs:

build.bat

(I made pad.txt using C++ writing 750 bytes to a file)

Code: Select all

"C:\Program Files (x86)\nasm\nasm" -f elf -o loader.o loader.s

C:\MinGW\bin\gcc -o kernel.o -c kernel.c -Wall -Wextra -nostdlib -nostartfiles -nodefaultlibs

C:\MinGW\bin\ld -T linker.ld -o kernel.bin loader.o kernel.o

del floppy.img
copy /b stage1 + stage2 + pad.txt + kernel.bin floppy.img
bochsrc.txt

Code: Select all

boot: floppy
floppya: 1_44="floppy.img", status=inserted
loader.s

Code: Select all

global loader           ; making entry point visible to linker
extern _kmain            ; kmain 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  _kmain                       ; call kernel proper
 
   cli
hang:
   hlt                                ; halt machine should kernel return
   jmp   hang
 
section .bss
align 4
stack:
   resb STACKSIZE                     ; reserve 16k stack on a doubleword boundary
kernel.c

Code: Select all

void kmain( void* mbd, unsigned int magic )
{
   if ( magic != 0x2BADB002 )
   {
      /* Something went not according to specs. Print an error */
      /* message and halt, but do *not* rely on the multiboot */
      /* data structure. */
   }
 
   /* You could either use multiboot.h */
   /* (http://www.gnu.org/software/grub/manual/multiboot/multiboot.html#multiboot_002eh) */
   /* or do your offsets yourself. The following is merely an example. */ 
   char * boot_loader_name =(char*) ((long*)mbd)[16];
 
   /* Print a letter to screen to see everything is working: */
   unsigned char *videoram = (unsigned char *) 0xb8000;
   videoram[0] = 65; /* character 'A' */
   videoram[1] = 0x07; /* forground, background color. */
 
   /* Write your kernel here. */
}
linker.ld

Code: Select all

ENTRY (loader)

SECTIONS{
    . = 0x00100000;

    .text :{
        *(.text)
    }

    .rodata ALIGN (0x1000) : {
        *(.rodata)
    }

    .data ALIGN (0x1000) : {
        *(.data)
    }

    .bss : {
        sbss = .;
        *(COMMON)
        *(.bss)
        ebss = .;
    }
}
Thank you all very much for your time and help - I really appreciate this!
Attachments
Screen shot of Bochs error and the input I have Bochs
Screen shot of Bochs error and the input I have Bochs
Screen shot of Bochs info
Screen shot of Bochs info
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: OS Dev Beginner

Post by Creature »

Code: Select all

"C:\Program Files (x86)\nasm\nasm" -f elf -o loader.o loader.s

C:\MinGW\bin\gcc -o kernel.o -c kernel.c -Wall -Wextra -nostdlib -nostartfiles -nodefaultlibs

C:\MinGW\bin\ld -T linker.ld -o kernel.bin loader.o kernel.o

del floppy.img
copy /b stage1 + stage2 + pad.txt + kernel.bin floppy.img
AFAIK, MinGW on Windows outputs PE/COFF executables. The problem is that if you want to use the ELF executable format (which you are using for NASM), you will need a version of GCC that is able to output to the same format. Unfortunately, this means you will need to build a version of GCC yourself that will do just that. The article you're looking for is GCC Cross-Compiler. You will probably need Cygwin so you can build GCC yourself. When you are finished building, use that version of GCC instead of MinGW (don't forget DLL dependancies like cygwin1.dll and such).
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: OS Dev Beginner

Post by Love4Boobies »

You have 3 alternatives:
  • To install Cygwin and build a cross-compiler and cross-binutils using its packages. This is what most Windows developers on this forum do because it is easy and things usually work out-of-the-box. The disadvantage is that Cygwin will basically become your environment and it can be difficult for someone who is not used to UNIX-like tools.
  • To install MinGW (which you already have) + MSYS. Unlike Cygwin, MinGW will be able to build you a native cross-compiler, not one that works on top of Cygwin's emulation layer. What you end up is a slightly faster set of tools. You can use what you've built using this technique on any Windows PC. The problem here is that MinGW and MSYS are difficult to set up (tens of individual archives that you have to download off SourceForge), unless you use the automated installers or minw-get which will only install really old versions of the GNU tools (you don't want that).
  • Few people seem to know this but the NT kernel works with subsystems which basically offer functionality to the applications. You can thus use the POSIX API natively on Windows without any workarounds. All you have to do is install SUA (Subsystem for UNIX-based Aplications) - provided by Microsoft for free; you can install it directly from somewhere in your control panel. You still need to build a cross-compiler but you can use GNU's tools as they are: UNIX tools. This is not documented on our wiki yet.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
rhughes
Posts: 16
Joined: Thu Apr 22, 2010 7:01 am

Re: OS Dev Beginner

Post by rhughes »

Thank you very much guys. It works now!! :D

I have a questions though. When I load in the kernel in Bochs, why does it load if I enter:

kernel 200+10

- and -

kernel 200+16

I thought the +x was the starting point of the kernel?

Thank you all very much for your time and help,

Richard Hughes
Attachments
It works :)
It works :)
OS Works.PNG (23.36 KiB) Viewed 1542 times
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: OS Dev Beginner

Post by Love4Boobies »

The second number is the size of the kernel, in blocks.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
rhughes
Posts: 16
Joined: Thu Apr 22, 2010 7:01 am

Re: OS Dev Beginner

Post by rhughes »

OK. So if my kernel is 10 blocks long, but I specifiy 8, will only 8 blocks get loaded? If I specifiy longer, will there be noise at the end?

Thanks,

Richard Hughes
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: OS Dev Beginner

Post by Love4Boobies »

Yes and yes. The term for noise is garbage. But it doesn't make much difference - you load more from the disk but whatever is in RAM if you load less at that location is still garbage. :)
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Post Reply