diff --git a/OLVASSEL.md b/OLVASSEL.md index 43d3ba8..3e743e2 100644 --- a/OLVASSEL.md +++ b/OLVASSEL.md @@ -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). diff --git a/README.md b/README.md index 88a7c98..21c1041 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/mykernel/cpp/Makefile b/mykernel/cpp/Makefile new file mode 100644 index 0000000..539b185 --- /dev/null +++ b/mykernel/cpp/Makefile @@ -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 diff --git a/mykernel/cpp/font.psf b/mykernel/cpp/font.psf new file mode 100644 index 0000000..3e67693 Binary files /dev/null and b/mykernel/cpp/font.psf differ diff --git a/mykernel/cpp/kernel.cpp b/mykernel/cpp/kernel.cpp new file mode 100644 index 0000000..ba12610 --- /dev/null +++ b/mykernel/cpp/kernel.cpp @@ -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 + +/* 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;ywidth+7)/8; + while(*s) { + unsigned char *glyph = (unsigned char*)&_binary_font_psf_start + font->headersize + + (*s>0&&(uint32_t)(*s)numglyph?*s:0)*font->bytesperglyph; + offs = (kx * (font->width+1) * 4); + for(y=0;yheight;y++) { + line=offs; mask=1<<(font->width-1); + for(x=0;xwidth;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; +} diff --git a/mykernel/cpp/link.ld b/mykernel/cpp/link.ld new file mode 100644 index 0000000..d24cc1b --- /dev/null +++ b/mykernel/cpp/link.ld @@ -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) } +}