Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers: add basic framebuffer support #259

Merged
merged 7 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ ifeq ($(CONFIG_ACPICA),y)
COMMON_FLAGS += -DKTF_ACPICA
endif

AFLAGS := $(COMMON_FLAGS) -D__ASSEMBLY__ -nostdlib -nostdinc
AFLAGS := $(COMMON_FLAGS) -DASM_FILE -D__ASSEMBLY__ -nostdlib -nostdinc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is ASM_FILE?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the official multiboot.h file we use uses ASM_FILE to distinguish between C and asm. We use __ASSEMBLY__, but I do not want to modify the original file.

CFLAGS := $(COMMON_FLAGS) -std=gnu99 -O2 -g -Wall -Wextra -ffreestanding -nostdlib -nostdinc
CFLAGS += -mno-red-zone -mno-mmx -mno-sse -mno-sse2
CFLAGS += -fno-stack-protector -fno-exceptions -fno-builtin -fomit-frame-pointer -fcf-protection="none"
Expand Down
31 changes: 27 additions & 4 deletions arch/x86/boot/boot.S
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright © 2020 Amazon.com, Inc. or its affiliates.
* Copyright © 2022 Open Source Security, Inc.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand All @@ -23,16 +24,38 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <asm-macros.h>
#include <multiboot.h>

SECTION(.multiboot, "a", 4)
#define MULTIBOOT_PAGE_ALIGN (1<<0)
#define MULTIBOOT_MEMORY_INFO (1<<1)
#define MULTIBOOT_HEADER_MAGIC (0x1BADB002)
#define MULTIBOOT_HEADER_FLAGS (MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO)
#define MULTIBOOT_HEADER_FLAGS (MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_VIDEO_MODE)
#define MULTIBOOT_CHECKSUM -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)

#define MULTIBOOT_VIDEO_MODE_GFX 0
#define MULTIBOOT_VIDEO_MODE_TEXT 1

#define MULTIBOOT_MODE_TYPE MULTIBOOT_VIDEO_MODE_GFX

#define MULTIBOOT_HEADER_ADDR 0
#define MULTIBOOT_LOAD_ADDRESS 0
#define MULTIBOOT_LOAD_END_ADDRESS 0
#define MULTIBOOT_BSS_END_ADDRESS 0
#define MULTIBOOT_ENTRY_ADDRESS 0

#define MULTIBOOT_FB_WIDTH 1024
#define MULTIBOOT_FB_HEIGHT 768
#define MULTIBOOT_FB_BPP 32

.code32
multiboot_header:
.long MULTIBOOT_HEADER_MAGIC
.long MULTIBOOT_HEADER_FLAGS
.long MULTIBOOT_CHECKSUM
.long MULTIBOOT_HEADER_ADDR
.long MULTIBOOT_LOAD_ADDRESS
.long MULTIBOOT_LOAD_END_ADDRESS
.long MULTIBOOT_BSS_END_ADDRESS
.long MULTIBOOT_ENTRY_ADDRESS
.long MULTIBOOT_MODE_TYPE
.long MULTIBOOT_FB_WIDTH
.long MULTIBOOT_FB_HEIGHT
.long MULTIBOOT_FB_BPP
16 changes: 16 additions & 0 deletions arch/x86/boot/multiboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,19 @@ int mbi_get_memory_range(paddr_t pa, addr_range_t *r) {

return 0;
}

bool mbi_has_framebuffer(void) {
if (!has_mbi_flag(MULTIBOOT_INFO_FRAMEBUFFER_INFO))
return false;

switch (multiboot_info->framebuffer_type) {
case MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED:
case MULTIBOOT_FRAMEBUFFER_TYPE_RGB:
return true;
case MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT:
default:
return false;
}

return false;
}
5 changes: 5 additions & 0 deletions common/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <spinlock.h>
#include <string.h>

#include <drivers/fb.h>
#include <drivers/serial.h>
#include <drivers/vga.h>

Expand Down Expand Up @@ -98,6 +99,10 @@ void vga_console_write(void *vga_memory, const char *buf, size_t len) {
vga_write(vga_memory, buf, len, VGA_WHITE);
}

void fb_console_write(void *fb_memory, const char *buf, size_t len) {
fb_write(fb_memory, buf, len, FB_WHITE);
}

void register_console_callback(console_callback_t cb, void *arg) {
console_callbacks[num_console_callbacks].cb = cb;
console_callbacks[num_console_callbacks++].arg = arg;
Expand Down
10 changes: 8 additions & 2 deletions common/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include <smp/mptables.h>
#include <smp/smp.h>

#include <drivers/fb.h>
#include <drivers/hpet.h>
#include <drivers/pic.h>
#include <drivers/pit.h>
Expand Down Expand Up @@ -137,6 +138,8 @@ static void display_cpu_info(void) {
printk("Frequency: %lu MHz\n", freq / MHZ(1));
}

static void display_banner(void) { draw_logo(); }

static void __text_init init_vga_console(void) {
if (!boot_flags.vga)
return;
Expand Down Expand Up @@ -194,6 +197,11 @@ void __noreturn __text_init kernel_start(uint32_t multiboot_magic,
if (opt_debug)
dump_pagetables();

if (init_framebuffer(mbi))
display_banner();
else
init_vga_console();

init_percpu();

init_traps(get_bsp_cpu_id());
Expand All @@ -213,8 +221,6 @@ void __noreturn __text_init kernel_start(uint32_t multiboot_magic,
BUG();
}

init_vga_console();

init_apic(get_bsp_cpu_id(), APIC_MODE_XAPIC);

init_tasks();
Expand Down
181 changes: 181 additions & 0 deletions drivers/fb/fb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
* Copyright © 2022 Open Source Security, Inc.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <console.h>
#include <drivers/fb.h>
#include <drivers/logo.h>
#include <ktf.h>
#include <multiboot.h>
#include <page.h>
#include <string.h>

extern uint64_t fonts[];

static uint32_t width;
static uint32_t height;
static uint32_t pitch;
static uint8_t bpp;

static size_t buffer_size;
static size_t banner_size;
static void *video_memory;

static void (*put_pixel)(uint32_t x, uint32_t y, uint32_t color);

static void map_fb_area(paddr_t start, size_t size) {
for (mfn_t video_mfn = paddr_to_mfn(start); video_mfn < paddr_to_mfn(start + size);
video_mfn++) {
vmap_4k(mfn_to_virt(video_mfn), video_mfn, L1_PROT_NOCACHE);
kmap_4k(video_mfn, L1_PROT_NOCACHE);
}
}

static void put_pixel8(uint32_t x, uint32_t y, uint32_t color) {
*(uint8_t *) (video_memory + (y * pitch) + x) = color;
}

static void put_pixel16(uint32_t x, uint32_t y, uint32_t color) {
*(uint16_t *) (video_memory + (y * pitch) + (x * 2)) = color;
}

static void put_pixel24(uint32_t x, uint32_t y, uint32_t color) {
uint32_t *px = video_memory + (y * pitch) + (x * 3);

*px = (color & 0x00ffffff) | (*px & 0xff000000);
}

static void put_pixel32(uint32_t x, uint32_t y, uint32_t color) {
*(uint32_t *) (video_memory + (y * pitch) + (x * 4)) = color;
}

bool init_framebuffer(const multiboot_info_t *mbi) {
if (!mbi_has_framebuffer())
return false;

video_memory = (void *) mbi->framebuffer_addr;
if (!video_memory)
return false;

width = mbi->framebuffer_width;
height = mbi->framebuffer_height;
pitch = mbi->framebuffer_pitch;
bpp = mbi->framebuffer_bpp;

buffer_size = (size_t) width * height;
banner_size = (size_t) width * LOGO_HEIGHT;

switch (bpp) {
case 0 ... 7:
printk("FB: Unsupported framebuffer BPP: %u\n", bpp);
return false;
case 8:
put_pixel = put_pixel8;
break;
case 15:
case 16:
put_pixel = put_pixel16;
buffer_size *= 2;
banner_size *= 2;
break;
case 24:
put_pixel = put_pixel24;
buffer_size *= 3;
banner_size *= 3;
break;
case 32:
put_pixel = put_pixel32;
buffer_size *= 4;
banner_size *= 4;
break;
default:
BUG();
}

map_fb_area(mbi->framebuffer_addr, buffer_size);
memset(video_memory, 0, buffer_size);

register_console_callback(fb_console_write, video_memory);
return true;
}

void put_char(char c, uint32_t x, uint32_t y, uint32_t color) {
uint64_t font = fonts[(uint8_t) c];

for (int yy = 0; yy < 8; yy++) {
for (int xx = 0; xx < 8; xx++, font >>= 1) {
if (font & 1)
put_pixel(x + xx, y + yy, color);
}
}
}

void draw_line(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t color) {
while (x1 <= x2 || y1 <= y2) {
put_pixel(x1, y1, color);
if (x1 <= x2)
x1++;
if (y1 <= y2)
y1++;
}
}

void draw_logo(void) {
uint32_t *px = (uint32_t *) logo;

for (int y = LOGO_HEIGHT; y > 0; y--) {
for (int x = 0; x < LOGO_WIDTH; x++)
put_pixel(x, y, *px++);
}

draw_line(0, LOGO_HEIGHT + 4, width, LOGO_HEIGHT + 4, FB_WHITE);
}

static void clear_screen(void *fb_addr) {
memset((uint8_t *) video_memory + banner_size, 0, buffer_size - banner_size);
}

void fb_write(void *fb_addr, const char *buf, size_t len, uint32_t color) {
static uint32_t row = LOGO_HEIGHT + 8, col = 0;

for (unsigned int i = 0; i < len; i++) {
char c = buf[i];

if ((col + 8) > width || c == '\n') {
row += sizeof(fonts[0]);
col = 0;
}

if ((row + 8) > height) {
clear_screen(fb_addr);
row = LOGO_HEIGHT + 8;
col = 0;
}

if (c == '\n')
82marbag marked this conversation as resolved.
Show resolved Hide resolved
continue;

put_char(c, col, row, color);
col += sizeof(fonts[0]);
}
}
Loading