kernel.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * mykernel/c/kernel.c
  3. *
  4. * Copyright (C) 2017 - 2021 bzt (bztsrc@gitlab)
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * This file is part of the BOOTBOOT Protocol package.
  27. * @brief A sample BOOTBOOT compatible kernel
  28. *
  29. */
  30. /* function to display a string, see below */
  31. void puts(char *s);
  32. /* we don't assume stdint.h exists */
  33. typedef short int int16_t;
  34. typedef unsigned char uint8_t;
  35. typedef unsigned short int uint16_t;
  36. typedef unsigned int uint32_t;
  37. typedef unsigned long int uint64_t;
  38. #include <bootboot.h>
  39. /* imported virtual addresses, see linker script */
  40. extern BOOTBOOT bootboot; // see bootboot.h
  41. extern unsigned char environment[4096]; // configuration, UTF-8 text key=value pairs
  42. extern uint8_t fb; // linear framebuffer mapped
  43. /******************************************
  44. * Entry point, called by BOOTBOOT Loader *
  45. ******************************************/
  46. void _start()
  47. {
  48. /*** NOTE: this code runs on all cores in parallel ***/
  49. int x, y, s=bootboot.fb_scanline, w=bootboot.fb_width, h=bootboot.fb_height;
  50. if(s) {
  51. // cross-hair to see screen dimension detected correctly
  52. for(y=0;y<h;y++) { *((uint32_t*)(&fb + s*y + (w*2)))=0x00FFFFFF; }
  53. for(x=0;x<w;x++) { *((uint32_t*)(&fb + s*(h/2)+x*4))=0x00FFFFFF; }
  54. // red, green, blue boxes in order
  55. for(y=0;y<20;y++) { for(x=0;x<20;x++) { *((uint32_t*)(&fb + s*(y+20) + (x+20)*4))=0x00FF0000; } }
  56. for(y=0;y<20;y++) { for(x=0;x<20;x++) { *((uint32_t*)(&fb + s*(y+20) + (x+50)*4))=0x0000FF00; } }
  57. for(y=0;y<20;y++) { for(x=0;x<20;x++) { *((uint32_t*)(&fb + s*(y+20) + (x+80)*4))=0x000000FF; } }
  58. // say hello
  59. puts("Hello from a simple BOOTBOOT kernel");
  60. }
  61. // hang for now
  62. while(1);
  63. }
  64. /**************************
  65. * Display text on screen *
  66. **************************/
  67. typedef struct {
  68. uint32_t magic;
  69. uint32_t version;
  70. uint32_t headersize;
  71. uint32_t flags;
  72. uint32_t numglyph;
  73. uint32_t bytesperglyph;
  74. uint32_t height;
  75. uint32_t width;
  76. uint8_t glyphs;
  77. } __attribute__((packed)) psf2_t;
  78. extern volatile unsigned char _binary_font_psf_start;
  79. void puts(char *s)
  80. {
  81. psf2_t *font = (psf2_t*)&_binary_font_psf_start;
  82. int x,y,kx=0,line,mask,offs;
  83. int bpl=(font->width+7)/8;
  84. while(*s) {
  85. unsigned char *glyph = (unsigned char*)&_binary_font_psf_start + font->headersize +
  86. (*s>0&&*s<font->numglyph?*s:0)*font->bytesperglyph;
  87. offs = (kx * (font->width+1) * 4);
  88. for(y=0;y<font->height;y++) {
  89. line=offs; mask=1<<(font->width-1);
  90. for(x=0;x<font->width;x++) {
  91. *((uint32_t*)((uint64_t)&fb+line))=((int)*glyph) & (mask)?0xFFFFFF:0;
  92. mask>>=1; line+=4;
  93. }
  94. *((uint32_t*)((uint64_t)&fb+line))=0; glyph+=bpl; offs+=bootboot.fb_scanline;
  95. }
  96. s++; kx++;
  97. }
  98. }