1
0
Fork 0
mirror of https://gitlab.com/bztsrc/bootboot.git synced 2023-02-13 20:54:32 -05:00

Mykernel C++ example

This commit is contained in:
bzt 2021-01-17 22:30:09 +01:00
parent b977f1c067
commit 1128072863
6 changed files with 232 additions and 2 deletions

View file

@ -14,7 +14,7 @@ Előre lefordított binárisok mellékelve, egyből használhatók.
3. *aarch64-rpi* ARMv8 betöltő Raspberry Pi 3-hoz, 4-hez
[bootboot.img](https://gitlab.com/bztsrc/bootboot/raw/master/dist/bootboot.img) (34k)
4. *mykernel* egy példa BOOTBOOT [kompatíbilis kernel](https://gitlab.com/bztsrc/bootboot/tree/binaries/mykernel) (forrás elérhető [C](https://gitlab.com/bztsrc/bootboot/tree/master/mykernel/c)-ben, [Pascal](https://gitlab.com/bztsrc/bootboot/tree/master/mykernel/pas)-ban, [Rust](https://gitlab.com/bztsrc/bootboot/tree/master/mykernel/rust)-ban és [Go](https://gitlab.com/bztsrc/bootboot/tree/master/mykernel/go)-ban), ami vonalakat húz meg színes dobozokat rajzol
4. *mykernel* egy példa BOOTBOOT [kompatíbilis kernel](https://gitlab.com/bztsrc/bootboot/tree/binaries/mykernel) (forrás elérhető [C](https://gitlab.com/bztsrc/bootboot/tree/master/mykernel/c)-ben, [C++](https://gitlab.com/bztsrc/bootboot/tree/master/mykernel/cpp)-ban, [Pascal](https://gitlab.com/bztsrc/bootboot/tree/master/mykernel/pas)-ban, [Rust](https://gitlab.com/bztsrc/bootboot/tree/master/mykernel/rust)-ban és [Go](https://gitlab.com/bztsrc/bootboot/tree/master/mykernel/go)-ban), ami vonalakat húz meg színes dobozokat rajzol
5. *mkbootimg* minden az egyben, multiplatform [bootolható lemezkép készítő](https://gitlab.com/bztsrc/bootboot/tree/binaries) (Windows, MacOSX, Linux).

View file

@ -14,7 +14,7 @@ I provide pre-compiled images ready for use.
3. *aarch64-rpi* ARMv8 boot loader for Raspberry Pi 3, 4
[bootboot.img](https://gitlab.com/bztsrc/bootboot/raw/master/dist/bootboot.img) (34k)
4. *mykernel* an example BOOTBOOT [compatible kernel](https://gitlab.com/bztsrc/bootboot/tree/binaries/mykernel) (source available in [C](https://gitlab.com/bztsrc/bootboot/tree/master/mykernel/c), [Pascal](https://gitlab.com/bztsrc/bootboot/tree/master/mykernel/pas), [Rust](https://gitlab.com/bztsrc/bootboot/tree/master/mykernel/rust) and [Go](https://gitlab.com/bztsrc/bootboot/tree/master/mykernel/go)) which draws lines and boxes
4. *mykernel* an example BOOTBOOT [compatible kernel](https://gitlab.com/bztsrc/bootboot/tree/binaries/mykernel) (source available in [C](https://gitlab.com/bztsrc/bootboot/tree/master/mykernel/c), [C++](https://gitlab.com/bztsrc/bootboot/tree/master/mykernel/cpp), [Pascal](https://gitlab.com/bztsrc/bootboot/tree/master/mykernel/pas), [Rust](https://gitlab.com/bztsrc/bootboot/tree/master/mykernel/rust) and [Go](https://gitlab.com/bztsrc/bootboot/tree/master/mykernel/go)) which draws lines and boxes
5. *mkbootimg* an all-in-one multiplatform [bootable disk image creator](https://gitlab.com/bztsrc/bootboot/tree/binaries/) (Windows, MacOSX, Linux).

52
mykernel/cpp/Makefile Normal file
View file

@ -0,0 +1,52 @@
#
# mykernel/cpp/Makefile
#
# 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 An example Makefile for sample kernel
#
#
CPPFLAGS = -Wall -fpic -ffreestanding -fno-stack-protector -fno-exceptions -fno-rtti -nostdinc -nostdlib -I../../dist/
LDFLAGS = -nostdlib -nostartfiles -T link.ld
STRIPFLAGS = -s -K mmio -K fb -K bootboot -K environment
all: mykernel.x86_64.elf mykernel.aarch64.elf
mykernel.x86_64.elf: kernel.cpp
x86_64-elf-g++ $(CPPFLAGS) -mno-red-zone -c kernel.cpp -o kernel.o
x86_64-elf-ld -r -b binary -o font.o font.psf
x86_64-elf-ld $(LDFLAGS) kernel.o font.o -o mykernel.x86_64.elf
x86_64-elf-strip $(STRIPFLAGS) mykernel.x86_64.elf
x86_64-elf-readelf -hls mykernel.x86_64.elf >mykernel.x86_64.txt
mykernel.aarch64.elf: kernel.cpp
aarch64-elf-g++ $(CPPFLAGS) -c kernel.cpp -o kernel.o
aarch64-elf-ld -r -b binary -o font.o font.psf
aarch64-elf-ld $(LDFLAGS) kernel.o font.o -o mykernel.aarch64.elf
aarch64-elf-strip $(STRIPFLAGS) mykernel.aarch64.elf
aarch64-elf-readelf -hls mykernel.aarch64.elf >mykernel.aarch64.txt
clean:
rm *.o *.elf *.txt

BIN
mykernel/cpp/font.psf Normal file

Binary file not shown.

122
mykernel/cpp/kernel.cpp Normal file
View file

@ -0,0 +1,122 @@
/*
* mykernel/cpp/kernel.cpp
*
* 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 A sample BOOTBOOT compatible kernel
*
*/
/* we don't assume cstdint exists */
typedef short int int16_t;
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long int uint64_t;
#include <bootboot.h>
/* imported virtual addresses, see linker script */
extern BOOTBOOT bootboot; // see bootboot.h
extern unsigned char environment[4096]; // configuration, UTF-8 text key=value pairs
extern uint8_t fb; // linear framebuffer mapped
/* font */
typedef struct {
uint32_t magic;
uint32_t version;
uint32_t headersize;
uint32_t flags;
uint32_t numglyph;
uint32_t bytesperglyph;
uint32_t height;
uint32_t width;
uint8_t glyphs;
} __attribute__((packed)) psf2_t;
extern volatile unsigned char _binary_font_psf_start;
class MyKernel {
public:
/* contructor */
MyKernel()
{
/*** NOTE: this code runs on all cores in parallel ***/
int x, y, s=bootboot.fb_scanline, w=bootboot.fb_width, h=bootboot.fb_height;
// cross-hair to see screen dimension detected correctly
for(y=0;y<h;y++) { *((uint32_t*)(&fb + s*y + (w*2)))=0x00FFFFFF; }
for(x=0;x<w;x++) { *((uint32_t*)(&fb + s*(h/2)+x*4))=0x00FFFFFF; }
// red, green, blue boxes in order
for(y=0;y<20;y++) { for(x=0;x<20;x++) { *((uint32_t*)(&fb + s*(y+20) + (x+20)*4))=0x00FF0000; } }
for(y=0;y<20;y++) { for(x=0;x<20;x++) { *((uint32_t*)(&fb + s*(y+20) + (x+50)*4))=0x0000FF00; } }
for(y=0;y<20;y++) { for(x=0;x<20;x++) { *((uint32_t*)(&fb + s*(y+20) + (x+80)*4))=0x000000FF; } }
// say hello
MyKernel::puts("Hello from a simple BOOTBOOT kernel");
// hang for now
while(1);
}
/* destructor */
~MyKernel()
{
}
private:
/**************************
* Display text on screen *
**************************/
void puts(const char *s)
{
psf2_t *font = (psf2_t*)&_binary_font_psf_start;
uint32_t x,y,kx=0,line,mask,offs;
int bpl=(font->width+7)/8;
while(*s) {
unsigned char *glyph = (unsigned char*)&_binary_font_psf_start + font->headersize +
(*s>0&&(uint32_t)(*s)<font->numglyph?*s:0)*font->bytesperglyph;
offs = (kx * (font->width+1) * 4);
for(y=0;y<font->height;y++) {
line=offs; mask=1<<(font->width-1);
for(x=0;x<font->width;x++) {
*((uint32_t*)((uint64_t)&fb+line))=((int)*glyph) & (mask)?0xFFFFFF:0;
mask>>=1; line+=4;
}
*((uint32_t*)((uint64_t)&fb+line))=0; glyph+=bpl; offs+=bootboot.fb_scanline;
}
s++; kx++;
}
}
};
/******************************************
* Entry point, called by BOOTBOOT Loader *
******************************************/
int main()
{
MyKernel();
return 0;
}

56
mykernel/cpp/link.ld Normal file
View file

@ -0,0 +1,56 @@
/*
* mykernel/cpp/link.ld
*
* 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 An example linker script for sample kernel
*
*/
ENTRY(main)
mmio = 0xfffffffff8000000; /* these are configurable for level 2 loaders */
fb = 0xfffffffffc000000;
PHDRS
{
boot PT_LOAD; /* one single loadable segment */
}
SECTIONS
{
. = 0xffffffffffe00000;
bootboot = .; . += 4096;
environment = .; . += 4096;
.text : {
KEEP(*(.text.boot)) *(.text .text.*) /* code */
*(.rodata .rodata.*) /* data */
*(.data .data.*)
} :boot
.bss (NOLOAD) : { /* bss */
. = ALIGN(16);
*(.bss .bss.*)
*(COMMON)
} :boot
/DISCARD/ : { *(.eh_frame) *(.comment) }
}