aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/kernel.c
blob: 335cfe401999a5fe675dc2f74eb2c56076af7667 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdint.h>
#include "kprintf.h"
#include "gdt.h"
#include "idt.h"
#include "acpi.h"

#include "asm_inline.h"

typedef struct _OSDATA
{
  uint32_t Magic; // magic number to check

  uint32_t FBWidth; // with of the framebuffer
  uint32_t FBHeight; // height
  uint32_t PixelSize; // size of each pixel(an rgb pixel might have a bigger size)
  void * FBAddr; // address of the linear framebuffer

  void * MEMMap; // pointer to the system memory map

  void * RAMDisk; // pointer to a ramdisk loaded from the hdd

  void* RSDP;
} OSDATA;

typedef struct _Pixel
{
  uint8_t B;
  uint8_t G;
  uint8_t R;
  uint8_t Z;
} Pixel;

void early_serial_init()
{
  outb(COM1 + 1, 0x00);
  outb(COM1 + 3, 0x80);
  outb(COM1 + 0, 0x03);
  outb(COM1 + 1, 0x00);
  outb(COM1 + 3, 0x03);
  outb(COM1 + 2, 0xC7);
  outb(COM1 + 4, 0x0B);
}

int kmain(void * osdata)
{
  early_serial_init();

  fldcw(0x37F);

  kprintf("\n\n\n");
  kprintf("=====================\n");
  kprintf("    OrnitorrincOS\n");
  kprintf("=====================\n");
  kprintf("\n\n\n");
  // kprintf("Hello from the kernel\n");
  // kprintf("This is a %d complex %x test: %X\n", 1234, 1234, 1234);

  uint32_t regs[4] = {0};
  char* str = (char*)regs;
  uint32_t rax, rbx, rcx, rdx;
  cpuid(0, 0, &rax, &rbx, &rcx, &rdx);
  regs[0] = rbx;
  regs[1] = rdx;
  regs[2] = rcx;
  kprintf("CPU: %s\n", str);

  OSDATA* data = (OSDATA*)osdata;

  Pixel* fb = data->FBAddr;

  // kprintf("FB Address: %p\n", fb);

  {
    int i = 0;
    int j = 0;

    for(i = 0; i < 256; ++i)
    {
      for(j = 0; j < 256; ++j)
      {
        Pixel p;
        p.R = 0;
        p.G = 255;
        p.B = 0;
        p.Z = 255;
        fb[j + 800*i] = p; // original: 1024 but 800 seems to work better(maybe we are not setting the display mode correctly
      }
    }
  }

  // kprintf("image finished\n");

  kprintf("\n");
  kprintf("=> Initializing GDT\n");

  cli();

  init_gdt();

  kprintf("=> GDT initialized\n");

  kprintf("\n");

  kprintf("=> Initializing IDT\n");

  init_idt();

  kprintf("=> IDT initialized\n");

  sti();

  kprintf("=> Interrupts reenabled\n");

  char* test_value = (char*)0xFFFFFFFF00000000;
  // *test_value = 'B';

  XSDP* xsdt = (XSDP*)data->RSDP;

  kprintf("\n");

  init_acpi(xsdt);

  while(1) {};

  kprintf("KERNEL FINISHED\n");

  hlt();

  return 0;
}