Browse Source

Fixed errors in ARM linker. Converted all files to Unix format.

master
L. Bradley LaBoon 8 years ago
parent
commit
bf60ad29f4
  1. 68
      Makefile
  2. 66
      arch/arm-none-eabi/boot.s
  3. 84
      arch/arm-none-eabi/linker.ld
  4. 80
      arch/i586-elf/boot.s
  5. 76
      arch/i586-elf/linker.ld
  6. 182
      driver/video/vga.c
  7. 128
      include/video/vga.h
  8. 88
      kernel/brados.c

68
Makefile

@ -1,34 +1,34 @@
# Build environment
PREFIX ?= /home/brad/brados/brados-gcc
ARCH ?= i586-elf
GNU ?= $(PREFIX)/$(ARCH)/bin/$(ARCH)
# Source files
SOURCES_ASM := arch/$(ARCH)/boot.s
SOURCES_C := driver/video/vga.c kernel/brados.c lib/string.c
# Object files
OBJS := $(patsubst %.s,%.o,$(SOURCES_ASM))
OBJS += $(patsubst %.c,%.o,$(SOURCES_C))
# Build flags
CFLAGS = -std=gnu99 -ffreestanding -Iinclude/ -O2 -Wall -Wextra
LDFLAGS = -ffreestanding -O2 -nostdlib -lgcc
# Build rules
all: brados.bin
.PHONY: all clean
brados.bin: $(OBJS) arch/$(ARCH)/linker.ld
$(GNU)-gcc -T arch/$(ARCH)/linker.ld -o $@ $(LDFLAGS) $(OBJS)
clean:
rm -f $(OBJS) brados.bin
# C
%.o: %.c Makefile
$(GNU)-gcc -c $< -o $@ $(CFLAGS)
# Assembly
%.o: %.s Makefile
$(GNU)-as $< -o $@
# Build environment
PREFIX ?= /home/brad/brados/brados-gcc
ARCH ?= i586-elf
GNU ?= $(PREFIX)/$(ARCH)/bin/$(ARCH)
# Source files
SOURCES_ASM := arch/$(ARCH)/boot.s
SOURCES_C := driver/video/vga.c kernel/brados.c lib/string.c
# Object files
OBJS := $(patsubst %.s,%.o,$(SOURCES_ASM))
OBJS += $(patsubst %.c,%.o,$(SOURCES_C))
# Build flags
CFLAGS = -std=gnu99 -ffreestanding -Iinclude/ -O2 -Wall -Wextra
LDFLAGS = -ffreestanding -O2 -nostdlib -lgcc
# Build rules
all: brados.bin
.PHONY: all clean
brados.bin: $(OBJS) arch/$(ARCH)/linker.ld
$(GNU)-gcc -T arch/$(ARCH)/linker.ld -o $@ $(LDFLAGS) $(OBJS)
clean:
rm -f $(OBJS) brados.bin
# C
%.o: %.c Makefile
$(GNU)-gcc -c $< -o $@ $(CFLAGS)
# Assembly
%.o: %.s Makefile
$(GNU)-as $< -o $@

66
arch/arm-none-eabi/boot.s

@ -1,33 +1,33 @@
.section ".text.boot"
.globl Start
Start:
// Setup the stack
mov sp, #0x8000
// Clear out bss
ldr r4, =_bss_start
ldr r9, =_bss_end
mov r5, #0
mov r6, #0
mov r7, #0
mov r8, #0
b 2f
1:
// Store multiple at r4
stmia r4!, {r5-r8}
2:
// If we are still below bss_end, loop
cmp r4, r9
blo 1b
// Call brados_main
ldr r3, =brados_main
blx r3
halt:
// Halt
wfe
b halt
.section ".text.boot"
.globl Start
Start:
// Setup the stack
mov sp, #0x8000
// Clear out bss
ldr r4, =_bss_start
ldr r9, =_bss_end
mov r5, #0
mov r6, #0
mov r7, #0
mov r8, #0
b 2f
1:
// Store multiple at r4
stmia r4!, {r5-r8}
2:
// If we are still below bss_end, loop
cmp r4, r9
blo 1b
// Call brados_main
ldr r3, =brados_main
blx r3
halt:
// Halt
wfe
b halt

84
arch/arm-none-eabi/linker.ld

@ -1,42 +1,42 @@
ENTRY(Start)
SECTIONS
{
/* Starts at LOADER_ADDR */
. = 0x8000
_start = .;
_text_start = .;
.text:
{
KEEP(*(.text.boot))
*(.text)
}
. = ALIGN(4096);
_text_end = .;
_rodata_start = .;
.rodata:
{
*(.rodata)
}
. = ALIGN(4096)
_rodata_end = .;
_data_start = .;
.data:
{
*(.data)
}
. = ALIGN(4096);
_data_end = .;
_bss_start = .;
.bss:
{
bss = .;
*(.bss)
}
. = ALIGN(4096)
_bss_end = .;
_end = .;
}
ENTRY(Start)
SECTIONS
{
/* Starts at LOADER_ADDR */
. = 0x8000;
_start = .;
_text_start = .;
.text :
{
KEEP(*(.text.boot))
*(.text)
}
. = ALIGN(4096);
_text_end = .;
_rodata_start = .;
.rodata :
{
*(.rodata)
}
. = ALIGN(4096);
_rodata_end = .;
_data_start = .;
.data :
{
*(.data)
}
. = ALIGN(4096);
_data_end = .;
_bss_start = .;
.bss :
{
bss = .;
*(.bss)
}
. = ALIGN(4096);
_bss_end = .;
_end = .;
}

80
arch/i586-elf/boot.s

@ -1,40 +1,40 @@
# Declare constants for creating a multiboot header
.set ALIGN, 1<<0
.set MEMINFO, 1<<1
.set FLAGS, ALIGN | MEMINFO
.set MAGIC, 0x1BADB002
.set CHECKSUM, -(MAGIC + FLAGS)
# Declare multiboot header
.section .multiboot
.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM
# Create a 16 KiB stack
.section .bootstrap_stack
stack_bottom:
.skip 16384
stack_top:
# Kernel entry point
.section .text
.global _start
.type _start, @function
_start:
# We are now in kernel mode!
# Setup the stack
movl $stack_top, %esp
# Call the main kernel function
call brados_main
# In case the function returns, enter an endless loop
cli
hlt
.Lhang:
jmp .Lhang
# Set the size of the _start symbol for debugging
.size _start, . - _start
# Declare constants for creating a multiboot header
.set ALIGN, 1<<0
.set MEMINFO, 1<<1
.set FLAGS, ALIGN | MEMINFO
.set MAGIC, 0x1BADB002
.set CHECKSUM, -(MAGIC + FLAGS)
# Declare multiboot header
.section .multiboot
.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM
# Create a 16 KiB stack
.section .bootstrap_stack
stack_bottom:
.skip 16384
stack_top:
# Kernel entry point
.section .text
.global _start
.type _start, @function
_start:
# We are now in kernel mode!
# Setup the stack
movl $stack_top, %esp
# Call the main kernel function
call brados_main
# In case the function returns, enter an endless loop
cli
hlt
.Lhang:
jmp .Lhang
# Set the size of the _start symbol for debugging
.size _start, . - _start

76
arch/i586-elf/linker.ld

@ -1,38 +1,38 @@
/* Kernel entry point */
ENTRY(_start)
/* Define the locations of the object file sections */
SECTIONS
{
/* Sections begin at 1 MiB */
. = 1M;
/* The multiboot header needs to come first, followed by program code */
.text BLOCK(4K) : ALIGN(4K)
{
*(.multiboot)
*(.text)
}
/* Read-only data */
.rodata BLOCK(4K) : ALIGN(4K)
{
*(.rodata)
}
/* Initialized read-write data */
.data BLOCK(4K) : ALIGN(4K)
{
*(.data)
}
/* Uninitialized read-write data and stack */
.bss BLOCK(4K) : ALIGN(4K)
{
*(COMMON)
*(.bss)
*(.bootstrap_stack)
}
/* Add other sections here */
}
/* Kernel entry point */
ENTRY(_start)
/* Define the locations of the object file sections */
SECTIONS
{
/* Sections begin at 1 MiB */
. = 1M;
/* The multiboot header needs to come first, followed by program code */
.text BLOCK(4K) : ALIGN(4K)
{
*(.multiboot)
*(.text)
}
/* Read-only data */
.rodata BLOCK(4K) : ALIGN(4K)
{
*(.rodata)
}
/* Initialized read-write data */
.data BLOCK(4K) : ALIGN(4K)
{
*(.data)
}
/* Uninitialized read-write data and stack */
.bss BLOCK(4K) : ALIGN(4K)
{
*(COMMON)
*(.bss)
*(.bootstrap_stack)
}
/* Add other sections here */
}

182
driver/video/vga.c

@ -1,91 +1,91 @@
#include <stddef.h>
#include <stdint.h>
#include <brados/string.h>
#include <video/vga.h>
// Create a VGA color
uint8_t make_color(enum vga_color fg, enum vga_color bg)
{
return fg | bg << 4;
}
// Create a VGA character
uint16_t make_vgaEntry(char c, uint8_t color)
{
uint16_t c16 = c;
uint16_t color16 = color;
return c16 | color16 << 8;
}
// Initialize a VGA term
void term_init(struct vgastate *state)
{
state->term_row = state->term_col = 0;
state->term_color = make_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
state->term_buf = (uint16_t *) VGA_BUF;
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
state->term_buf[index] = make_vgaEntry(' ', state->term_color);
}
}
}
// Write a character to a VGA term
void term_putChar(struct vgastate *state, char c)
{
if (c == '\n')
state->term_col = VGA_WIDTH - 1;
else
term_putEntryAt(state, c, state->term_color, state->term_col, state->term_row);
if (++state->term_col == VGA_WIDTH) {
state->term_col = 0;
if (++state->term_row == VGA_HEIGHT) {
state->term_row--;
term_scroll(state);
}
}
}
// Place a character in a VGA term's buffer
void term_putEntryAt(struct vgastate *state, char c, uint8_t color, size_t x, size_t y)
{
const size_t index = y * VGA_WIDTH + x;
state->term_buf[index] = make_vgaEntry(c, color);
}
// Scroll the term down 1 line
void term_scroll(struct vgastate *state)
{
// Move each row up
for (size_t y = 1; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
const size_t indexBelow = (y - 1) * VGA_WIDTH + x;
state->term_buf[indexBelow] = state->term_buf[index];
}
}
// Clear the last line
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = (VGA_HEIGHT - 1) * VGA_WIDTH + x;
state->term_buf[index] = ' ';
}
}
// Set VGA term color
void term_setColor(struct vgastate *state, uint8_t color)
{
state->term_color = color;
}
// Write a string to a VGA term
void term_writeStr(struct vgastate *state, const char *data)
{
size_t dataLen = strlen(data);
for (size_t i = 0; i < dataLen; i++)
term_putChar(state, data[i]);
}
#include <stddef.h>
#include <stdint.h>
#include <brados/string.h>
#include <video/vga.h>
// Create a VGA color
uint8_t make_color(enum vga_color fg, enum vga_color bg)
{
return fg | bg << 4;
}
// Create a VGA character
uint16_t make_vgaEntry(char c, uint8_t color)
{
uint16_t c16 = c;
uint16_t color16 = color;
return c16 | color16 << 8;
}
// Initialize a VGA term
void term_init(struct vgastate *state)
{
state->term_row = state->term_col = 0;
state->term_color = make_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
state->term_buf = (uint16_t *) VGA_BUF;
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
state->term_buf[index] = make_vgaEntry(' ', state->term_color);
}
}
}
// Write a character to a VGA term
void term_putChar(struct vgastate *state, char c)
{
if (c == '\n')
state->term_col = VGA_WIDTH - 1;
else
term_putEntryAt(state, c, state->term_color, state->term_col, state->term_row);
if (++state->term_col == VGA_WIDTH) {
state->term_col = 0;
if (++state->term_row == VGA_HEIGHT) {
state->term_row--;
term_scroll(state);
}
}
}
// Place a character in a VGA term's buffer
void term_putEntryAt(struct vgastate *state, char c, uint8_t color, size_t x, size_t y)
{
const size_t index = y * VGA_WIDTH + x;
state->term_buf[index] = make_vgaEntry(c, color);
}
// Scroll the term down 1 line
void term_scroll(struct vgastate *state)
{
// Move each row up
for (size_t y = 1; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
const size_t indexBelow = (y - 1) * VGA_WIDTH + x;
state->term_buf[indexBelow] = state->term_buf[index];
}
}
// Clear the last line
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = (VGA_HEIGHT - 1) * VGA_WIDTH + x;
state->term_buf[index] = ' ';
}
}
// Set VGA term color
void term_setColor(struct vgastate *state, uint8_t color)
{
state->term_color = color;
}
// Write a string to a VGA term
void term_writeStr(struct vgastate *state, const char *data)
{
size_t dataLen = strlen(data);
for (size_t i = 0; i < dataLen; i++)
term_putChar(state, data[i]);
}

128
include/video/vga.h

@ -1,64 +1,64 @@
#ifndef __brados_video_vga_h__
#define __brados_video_vga_h__
#include <stddef.h>
#include <stdint.h>
#define VGA_BUF 0xB8000
#define VGA_WIDTH 80
#define VGA_HEIGHT 24
// VGA State
struct vgastate {
size_t term_row, term_col;
uint8_t term_color;
uint16_t *term_buf;
};
// Color constants
enum vga_color
{
VGA_COLOR_BLACK = 0,
VGA_COLOR_BLUE = 1,
VGA_COLOR_GREEN = 2,
VGA_COLOR_CYAN = 3,
VGA_COLOR_RED = 4,
VGA_COLOR_MAGENTA = 5,
VGA_COLOR_BROWN = 6,
VGA_COLOR_LIGHT_GREY = 7,
VGA_COLOR_DARK_GREY = 8,
VGA_COLOR_LIGHT_BLUE = 9,
VGA_COLOR_LIGHT_GREEN = 10,
VGA_COLOR_LIGHT_CYAN = 11,
VGA_COLOR_LIGHT_RED = 12,
VGA_COLOR_LIGHT_MAGENTA = 13,
VGA_COLOR_LIGHT_BROWN = 14,
VGA_COLOR_WHITE = 15
};
// Create a VGA color
uint8_t make_color(enum vga_color fg, enum vga_color bg);
// Create a VGA character
uint16_t make_vgaEntry(char c, uint8_t color);
// Initialize a VGA term
void term_init(struct vgastate *state);
// Write a character to a VGA term
void term_putChar(struct vgastate *state, char c);
// Place a character in a VGA term's buffer
void term_putEntryAt(struct vgastate *state, char c, uint8_t color, size_t x, size_t y);
// Scroll the term down 1 line
void term_scroll(struct vgastate *state);
// Set VGA term color
void term_setColor(struct vgastate *state, uint8_t color);
// Write a string to a VGA term
void term_writeStr(struct vgastate *state, const char *data);
#endif
#ifndef __brados_video_vga_h__
#define __brados_video_vga_h__
#include <stddef.h>
#include <stdint.h>
#define VGA_BUF 0xB8000
#define VGA_WIDTH 80
#define VGA_HEIGHT 24
// VGA State
struct vgastate {
size_t term_row, term_col;
uint8_t term_color;
uint16_t *term_buf;
};
// Color constants
enum vga_color
{
VGA_COLOR_BLACK = 0,
VGA_COLOR_BLUE = 1,
VGA_COLOR_GREEN = 2,
VGA_COLOR_CYAN = 3,
VGA_COLOR_RED = 4,
VGA_COLOR_MAGENTA = 5,
VGA_COLOR_BROWN = 6,
VGA_COLOR_LIGHT_GREY = 7,
VGA_COLOR_DARK_GREY = 8,
VGA_COLOR_LIGHT_BLUE = 9,
VGA_COLOR_LIGHT_GREEN = 10,
VGA_COLOR_LIGHT_CYAN = 11,
VGA_COLOR_LIGHT_RED = 12,
VGA_COLOR_LIGHT_MAGENTA = 13,
VGA_COLOR_LIGHT_BROWN = 14,
VGA_COLOR_WHITE = 15
};
// Create a VGA color
uint8_t make_color(enum vga_color fg, enum vga_color bg);
// Create a VGA character
uint16_t make_vgaEntry(char c, uint8_t color);
// Initialize a VGA term
void term_init(struct vgastate *state);
// Write a character to a VGA term
void term_putChar(struct vgastate *state, char c);
// Place a character in a VGA term's buffer
void term_putEntryAt(struct vgastate *state, char c, uint8_t color, size_t x, size_t y);
// Scroll the term down 1 line
void term_scroll(struct vgastate *state);
// Set VGA term color
void term_setColor(struct vgastate *state, uint8_t color);
// Write a string to a VGA term
void term_writeStr(struct vgastate *state, const char *data);
#endif

88
kernel/brados.c

@ -1,44 +1,44 @@
#include <stddef.h>
#include <brados/string.h>
#include <video/vga.h>
// Make sure we are using the right compiler
#if defined(__linux__)
#error "You are using the wrong compiler."
#endif
// Test various VGA functionality
void vgaTests(struct vgastate *term)
{
// Test line wrapping
term_writeStr(term, "Welcome to the desert of the real. I thought what I'd do was I'd pretend I was one of those deaf mutes.\n\n");
// Test colors
term_setColor(term, make_color(VGA_COLOR_BLACK, VGA_COLOR_WHITE));
term_writeStr(term, "This text should be inverse.\n");
term_setColor(term, make_color(VGA_COLOR_LIGHT_BLUE, VGA_COLOR_GREEN));
term_writeStr(term, "This text should be colorful.\n\n");
// Test scrolling
term_setColor(term, make_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK));
for (size_t i = 0; i < 20; i++) {
term_writeStr(term, "Printing some lines.\n");
for (volatile size_t j = 0; j < 100000000; j++);
}
}
// Main kernel function
void brados_main()
{
struct vgastate term;
term_init(&term);
// Welcome message
term_writeStr(&term, "Welcome to BRaDOS!\n");
term_writeStr(&term, "written by L. Bradley LaBoon\n\n");
vgaTests(&term);
term_writeStr(&term, "Done.");
}
#include <stddef.h>
#include <brados/string.h>
#include <video/vga.h>
// Make sure we are using the right compiler
#if defined(__linux__)
#error "You are using the wrong compiler."
#endif
// Test various VGA functionality
void vgaTests(struct vgastate *term)
{
// Test line wrapping
term_writeStr(term, "Welcome to the desert of the real. I thought what I'd do was I'd pretend I was one of those deaf mutes.\n\n");
// Test colors
term_setColor(term, make_color(VGA_COLOR_BLACK, VGA_COLOR_WHITE));
term_writeStr(term, "This text should be inverse.\n");
term_setColor(term, make_color(VGA_COLOR_LIGHT_BLUE, VGA_COLOR_GREEN));
term_writeStr(term, "This text should be colorful.\n\n");
// Test scrolling
term_setColor(term, make_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK));
for (size_t i = 0; i < 20; i++) {
term_writeStr(term, "Printing some lines.\n");
for (volatile size_t j = 0; j < 100000000; j++);
}
}
// Main kernel function
void brados_main()
{
struct vgastate term;
term_init(&term);
// Welcome message
term_writeStr(&term, "Welcome to BRaDOS!\n");
term_writeStr(&term, "written by L. Bradley LaBoon\n\n");
vgaTests(&term);
term_writeStr(&term, "Done.");
}

Loading…
Cancel
Save