From ffbe5260a5d74ea1651e9536ab82f18376138511 Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 13 Nov 2022 11:25:15 +0100 Subject: [PATCH] Add Framebuffer stuff --- kernel/CMakeLists.txt | 3 + kernel/src/Framebuffer.cpp | 83 +++++++++++++++++++ kernel/src/Framebuffer.h | 18 +++++ kernel/src/bootboot.h | 162 +++++++++++++++++++++++++++++++++++++ kernel/src/main.cpp | 5 ++ 5 files changed, 271 insertions(+) create mode 100644 kernel/src/Framebuffer.cpp create mode 100644 kernel/src/Framebuffer.h create mode 100644 kernel/src/bootboot.h diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 18553a08..c2fa7223 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -1,6 +1,7 @@ set(SOURCES src/main.cpp src/string.cpp + src/Framebuffer.cpp src/arch/Serial.cpp ) @@ -27,6 +28,8 @@ add_link_options(-lgcc -Wl,--build-id=none -z max-page-size=0x1000 -mno-red-zone add_executable(moon ${SOURCES}) +set_target_properties(moon PROPERTIES CXX_STANDARD 20) + target_include_directories(moon PUBLIC ${LUNA_ROOT}/luna) target_include_directories(moon PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src) diff --git a/kernel/src/Framebuffer.cpp b/kernel/src/Framebuffer.cpp new file mode 100644 index 00000000..6328c33b --- /dev/null +++ b/kernel/src/Framebuffer.cpp @@ -0,0 +1,83 @@ +#include "Framebuffer.h" +#include "bootboot.h" + +static u8* g_fb_ptr = nullptr; +static u32 g_fb_size; +static u32 g_fb_width; +static u32 g_fb_height; +static u32 g_fb_scanline; + +extern BOOTBOOT bootboot; +extern u8 fb[1]; + +namespace Framebuffer +{ + void init() + { + update(fb, bootboot.fb_size, bootboot.fb_width, bootboot.fb_height, bootboot.fb_scanline); + } + + void update(u8* ptr, u32 size, u32 width, u32 height, u32 scanline) + { + g_fb_ptr = ptr; + g_fb_size = size; + g_fb_width = width; + g_fb_height = height; + g_fb_scanline = scanline; + } + + void pixel(u32 x, u32 y, u32 color) + { + if (!g_fb_ptr) [[unlikely]] + return; + *(u32*)(g_fb_ptr + g_fb_scanline * y + x * 4) = color; + } + + void rect(u32 x, u32 y, u32 w, u32 h, u32 color) + { + if (!g_fb_ptr) [[unlikely]] + return; + for (u32 i = y; i < (y + h); i++) + { + u32* addr = (u32*)(g_fb_ptr + (g_fb_scanline * i) + (x * sizeof(u32))); + for (u32* it = addr; it < (addr + w); it++) *it = color; + } + } + + void rect(u32 x, u32 y, u32 w, u32 h, u32* colors) + { + if (!g_fb_ptr) [[unlikely]] + return; + u32 j = 0; + for (u32 i = y; i < (y + h); i++) + { + u32* addr = (u32*)(g_fb_ptr + (g_fb_scanline * i) + (x * sizeof(u32))); + for (u32* it = addr; it < (addr + w); it++, j++) *it = colors[j]; + } + } + + u8* ptr() + { + return g_fb_ptr; + } + + u32 size() + { + return g_fb_size; + } + + u32 width() + { + return g_fb_width; + } + + u32 height() + { + return g_fb_height; + } + + u32 scanline() + { + return g_fb_scanline; + } +} \ No newline at end of file diff --git a/kernel/src/Framebuffer.h b/kernel/src/Framebuffer.h new file mode 100644 index 00000000..9105f315 --- /dev/null +++ b/kernel/src/Framebuffer.h @@ -0,0 +1,18 @@ +#pragma once +#include + +namespace Framebuffer +{ + void init(); + void update(u8* ptr, u32 size, u32 width, u32 height, u32 scanline); + + void pixel(u32 x, u32 y, u32 color); + void rect(u32 x, u32 y, u32 w, u32 h, u32 color); + void rect(u32 x, u32 y, u32 w, u32 h, u32* colors); + + u8* ptr(); + u32 size(); + u32 width(); + u32 height(); + u32 scanline(); +} \ No newline at end of file diff --git a/kernel/src/bootboot.h b/kernel/src/bootboot.h new file mode 100644 index 00000000..8ac1207e --- /dev/null +++ b/kernel/src/bootboot.h @@ -0,0 +1,162 @@ +/* + * bootboot.h + * https://gitlab.com/bztsrc/bootboot + * + * Copyright (C) 2017 - 2021 bzt (bztsrc@gitlab) + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * This file is part of the BOOTBOOT Protocol package. + * @brief The BOOTBOOT structure + * + */ + +#ifndef _BOOTBOOT_H_ +#define _BOOTBOOT_H_ + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif +#ifndef _MSC_VER +#define _pack __attribute__((packed)) +#else +#define _pack +#pragma pack(push) +#pragma pack(1) +#endif + +#define BOOTBOOT_MAGIC "BOOT" + +/* default virtual addresses for level 0 and 1 static loaders */ +#define BOOTBOOT_MMIO 0xfffffffff8000000 /* memory mapped IO virtual address */ +#define BOOTBOOT_FB 0xfffffffffc000000 /* frame buffer virtual address */ +#define BOOTBOOT_INFO 0xffffffffffe00000 /* bootboot struct virtual address */ +#define BOOTBOOT_ENV 0xffffffffffe01000 /* environment string virtual address */ +#define BOOTBOOT_CORE 0xffffffffffe02000 /* core loadable segment start */ + +/* minimum protocol level: + * hardcoded kernel name, static kernel memory addresses */ +#define PROTOCOL_MINIMAL 0 +/* static protocol level: + * kernel name parsed from environment, static kernel memory addresses */ +#define PROTOCOL_STATIC 1 +/* dynamic protocol level: + * kernel name parsed, kernel memory addresses from ELF or PE symbols */ +#define PROTOCOL_DYNAMIC 2 +/* big-endian flag */ +#define PROTOCOL_BIGENDIAN 0x80 + +/* loader types, just informational */ +#define LOADER_BIOS (0 << 2) +#define LOADER_UEFI (1 << 2) +#define LOADER_RPI (2 << 2) +#define LOADER_COREBOOT (3 << 2) + +/* framebuffer pixel format, only 32 bits supported */ +#define FB_ARGB 0 +#define FB_RGBA 1 +#define FB_ABGR 2 +#define FB_BGRA 3 + + /* mmap entry, type is stored in least significant tetrad (half byte) of size + * this means size described in 16 byte units (not a problem, most modern + * firmware report memory in pages, 4096 byte units anyway). */ + typedef struct + { + uint64_t ptr; + uint64_t size; + } _pack MMapEnt; +#define MMapEnt_Ptr(a) ((a)->ptr) +#define MMapEnt_Size(a) ((a)->size & 0xFFFFFFFFFFFFFFF0) +#define MMapEnt_Type(a) ((a)->size & 0xF) +#define MMapEnt_IsFree(a) (((a)->size & 0xF) == 1) + +#define MMAP_USED 0 /* don't use. Reserved or unknown regions */ +#define MMAP_FREE 1 /* usable memory */ +#define MMAP_ACPI 2 /* acpi memory, volatile and non-volatile as well */ +#define MMAP_MMIO 3 /* memory mapped IO region */ + +#define INITRD_MAXSIZE 16 /* Mb */ + + typedef struct + { + /* first 64 bytes is platform independent */ + uint8_t magic[4]; /* 'BOOT' magic */ + uint32_t size; /* length of bootboot structure, minimum 128 */ + uint8_t protocol; /* 1, static addresses, see PROTOCOL_* and LOADER_* above */ + uint8_t fb_type; /* framebuffer type, see FB_* above */ + uint16_t numcores; /* number of processor cores */ + uint16_t bspid; /* Bootsrap processor ID (Local APIC Id on x86_64) */ + int16_t timezone; /* in minutes -1440..1440 */ + uint8_t datetime[8]; /* in BCD yyyymmddhhiiss UTC (independent to timezone) */ + uint64_t initrd_ptr; /* ramdisk image position and size */ + uint64_t initrd_size; + uint64_t fb_ptr; /* framebuffer pointer and dimensions */ + uint32_t fb_size; + uint32_t fb_width; + uint32_t fb_height; + uint32_t fb_scanline; + + /* the rest (64 bytes) is platform specific */ + union { + struct + { + uint64_t acpi_ptr; + uint64_t smbi_ptr; + uint64_t efi_ptr; + uint64_t mp_ptr; + uint64_t unused0; + uint64_t unused1; + uint64_t unused2; + uint64_t unused3; + } x86_64; + struct + { + uint64_t acpi_ptr; + uint64_t mmio_ptr; + uint64_t efi_ptr; + uint64_t unused0; + uint64_t unused1; + uint64_t unused2; + uint64_t unused3; + uint64_t unused4; + } aarch64; + } arch; + + /* from 128th byte, MMapEnt[], more records may follow */ + MMapEnt mmap; + /* use like this: + * MMapEnt *mmap_ent = &bootboot.mmap; mmap_ent++; + * until you reach bootboot->size, while(mmap_ent < bootboot + bootboot->size) */ + } _pack BOOTBOOT; + +#ifdef _MSC_VER +#pragma pack(pop) +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index 3b529072..b2721fe4 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -1,9 +1,14 @@ +#include "Framebuffer.h" #include "arch/Serial.h" extern "C" void _start() { Serial::println("Hello, world!"); + Framebuffer::init(); + + Framebuffer::rect(0, 0, 200, 200, 0xFF00FF00); + for (;;) ; } \ No newline at end of file