summaryrefslogtreecommitdiff
path: root/src/kernel/main.c
blob: 43778d460acf8fff584d9adcdc461e98a3ecdc13 (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
#include <stdint.h>
#include "paging.h"
#include "serial.h"
#include "window.h"
#include "panic.h"
#include "boot.h"
#include "cmos.h"
#include "pmap.h"
#include "task.h"
#include "util.h"
#include "elf.h"
#include "fat.h"
#include "ide.h"
#include "idt.h"
#include "kbd.h"
#include "log.h"
#include "pci.h"
#include "vbe.h"

void _start_user_mode() __attribute__ ((noreturn));

__attribute__ ((noreturn))
void main() {
  init_pagemap();
  init_paging();
  init_tasks();
  init_serial();

  pci_init();

  init_fat();
  //other fs drivers

  init_drives();

  init_ide();
  //other drive drivers

  init_log();

  init_kbd();
  init_idt();

  init_win();

  logf(LOG_INFO, "Kernel initialization done.");
  logf(LOG_INFO, "Available kernel memory: %dk", kernel_pages_left * 4);
  logf(LOG_INFO, "Available user memory: %dk", user_pages_left * 4);

  logf(LOG_INFO, "");

  logf(LOG_INFO, "PCI devices:");
  for (uint16_t i = 0; i < n_pci_devices; ++i) {
    const struct pci_device *dev = nth_pci_device(i);
    logf(LOG_INFO, "  %hw:%hw (%hb:%hb)", dev->id_vendor, dev->id_device, dev->class, dev->subclass);
  }

  logf(LOG_INFO, "");

  logf(LOG_INFO, "Drives:");
  for (uint8_t i = 0; i < n_drives; ++i) {
    const struct drive *d = &drives[i];
    const uint32_t free = d->get_free_sectors(d);
    logf(LOG_INFO, "  %s: %d%sk, %s (%d%sk free)", d->drive_type, d->n_sectors / 2, d->n_sectors % 2 ? ".5" : "", d->fs_type, free / 2, free % 2 ? ".5" : "");
  }

  logf(LOG_INFO, "");

  logf(LOG_INFO, "VBE info:");
  logf(LOG_INFO, "  Implemention: %s", RM_PTR(char, VBE_INFO->oem_name));
  logf(LOG_INFO, "  Video memory: %dk", VBE_INFO->total_memory * 64);
  logf(LOG_INFO, "  Standard:     %d.%d.%d", VBE_INFO->major_version, VBE_INFO->minor_version, VBE_INFO->version_rev);
  logf(LOG_INFO, "  Vendor:  %s", RM_PTR(char, VBE_INFO->vendor_name));
  logf(LOG_INFO, "  Product: %s", RM_PTR(char, VBE_INFO->product_name));
  logf(LOG_INFO, "  Version: %s", RM_PTR(char, VBE_INFO->product_rev_name));

  logf(LOG_INFO, "");

  #define MASK(offset, length) ((~((1 << offset) - 1)) - (offset + length == 32 ? 0 : (~((1 << (offset + length)) - 1))))
  logf(LOG_INFO, "Active video mode:");
  logf(LOG_INFO, "  Resolution: %dx%dx%d", VBE_MODE_INFO->width, VBE_MODE_INFO->height, VBE_MODE_INFO->bpp);
  logf(LOG_INFO, "  Red mask:   0x%h", MASK(VBE_MODE_INFO->red_off,   VBE_MODE_INFO->red_len));
  logf(LOG_INFO, "  Green mask: 0x%h", MASK(VBE_MODE_INFO->green_off, VBE_MODE_INFO->green_len));
  logf(LOG_INFO, "  Blue mask:  0x%h", MASK(VBE_MODE_INFO->blue_off,  VBE_MODE_INFO->blue_len));
  logf(LOG_INFO, "  Alpha mask: 0x%h", MASK(VBE_MODE_INFO->alpha_off, VBE_MODE_INFO->alpha_len));
  logf(LOG_INFO, "  Framebuffer address: 0x%h", VBE_MODE_INFO->frame_buf);

  logf(LOG_INFO, "");

  logf(LOG_INFO, "Loading init program.");

  if (!try_elf_run(drives, "bin/init", "", 0))
    PANIC("Failed to load init program.");

  logf(LOG_INFO, "Switching to init task.");

  _start_user_mode();
}