Commit ecb7ecc7 authored by Adam Matoušek's avatar Adam Matoušek
Browse files

The magnificent MASYS is hereby born.

parents
Loading
Loading
Loading
Loading

multiboot/Makefile

0 → 100644
+25 −0
Original line number Diff line number Diff line
CC = clang-3.8
LD = gcc
CFLAGS = -ffreestanding -nostdlib -static -fno-stack-protector -m32 -fno-PIC
LDFLAGS = -Wl,-melf_i386
GRUB = $(HOME)/prog/grub/bin/
MKRESCUE = env PATH=$$PATH:$(GRUB) grub-mkrescue

boot.img: a.out
	mkdir -p _boot/boot/grub
	cp a.out _boot/
	cp grub.cfg _boot/boot/grub/
	cp hello.txt _boot/
	$(MKRESCUE) -o $@ _boot
	rm -rf _boot

a.out: boot.S kernel.c
	$(CC) -o boot.o -c $(CFLAGS) -nopie boot.S
	$(CC) -o kernel.o -c $(CFLAGS) -nopie kernel.c
	$(LD) -o $@ -T linkscript $(CFLAGS) $(LDFLAGS) boot.o kernel.o

test: boot.img
	qemu-system-i386 -cdrom boot.img

clean:
	rm -f boot.img a.out *.o

multiboot/boot.S

0 → 100644
+41 −0
Original line number Diff line number Diff line
#define ASM_FILE        1
#define STACK_SIZE      0x4000
#include "multiboot2.h"

        .text

        .globl  start, _start
start:
_start:
        jmp     multiboot_entry

        .align  8 /* required multiboot header alignment */
multiboot_header:
        .long   MULTIBOOT2_HEADER_MAGIC
        .long   MULTIBOOT_ARCHITECTURE_I386
        .long   multiboot_header_end - multiboot_header /* length */
        .long   -( MULTIBOOT2_HEADER_MAGIC + MULTIBOOT_ARCHITECTURE_I386 \
                   + ( multiboot_header_end - multiboot_header ) )

        .align 8 /* each tag must be aligned separately */
        .short MULTIBOOT_HEADER_TAG_END /* tag id */
        .short 0                        /* flags */
        .long 8                         /* tag size */
multiboot_header_end:

multiboot_entry:
        movl    $(stack + STACK_SIZE), %esp /* stack pointer */
        pushl   $0                          
        popf                                /* reset EFLAGS */

        pushl   %ebx /* multiboot information structure */
        pushl   %eax /* multiboot magic value */
        call    main /* let's C */

        pushl   $halt_message
        call    puts
        hlt

halt_message:
        .asciz  "halted"
        .comm   stack, STACK_SIZE /* the stack */

multiboot/grub.cfg

0 → 100644
+4 −0
Original line number Diff line number Diff line
menuentry "a.out" {
    multiboot2 /a.out
    module2 /hello.txt
}

multiboot/hello.txt

0 → 100644
+2 −0
Original line number Diff line number Diff line
Hello
Is it me you're looking for?

multiboot/kernel.c

0 → 100644
+67 −0
Original line number Diff line number Diff line
#include "multiboot2.h"

static volatile unsigned char *video = ( unsigned char * ) 0xB8000;

static void clear( void )
{
    for ( int i = 0; i < 80 * 24 * 2; i ++ )
        video[ i ] = 0;
}

static void putchar( int c )
{
    const int columns = 80;
    const int lines = 24;
    static int x = 0, y = 0;

    if ( c == '\n' || x >= columns )
        x = 0, y++;

    if ( c == '\n' )
        return;

    if ( y >= lines )
        y = 0;

    int idx = ( x + y * columns ) * 2;
    video[ idx ] = c & 0xff;
    video[ idx + 1 ] = 0x07; /* vga attribute */

    ++ x;
}

void puts( const char *str )
{
    do putchar( *str ); while ( *str++ );
    putchar( '\n' );
}

void main( unsigned long magic, unsigned long addr )
{
    clear();

    if ( magic != MULTIBOOT2_BOOTLOADER_MAGIC )
    {
        puts( "invalid magic number :-(" );
        return;
    }

    if ( addr & 7 )
    {
        puts( "unaligned mbi :-(" );
        return;
    }

    puts( "Mame tyto Multiboot moduly:" );

    struct multiboot_tag *tag = addr + 8;

    while ( tag->type != MULTIBOOT_TAG_TYPE_END ) {
        puts( "-" );
        if ( tag->type == MULTIBOOT_TAG_TYPE_MODULE ) {
            struct multiboot_tag_module *tm = tag;
            puts( tm->cmdline );
        }
        tag = ( ( (unsigned long) tag ) + tag->size + 7 ) & 0xfffffff8UL;
    }
}