couple of bits on the x86_64 boot code
[newos.git] / boot / pc / x86_64 / multiboot.c
blob30cc92d80a532883a520eb8da710681f66f91442
1 #include <stdio.h>
2 #include "stage2_priv.h"
3 #include "multiboot.h"
5 static uint32 read32(const void *ptr, unsigned int offset)
7 return *(const uint32 *)((const char *)ptr + offset);
10 static void dump_multiboot_mmap(const void *mmap_base, uint32 len)
12 unsigned int index = 0;
13 struct multiboot_mmap *mmap = (struct multiboot_mmap *)mmap_base;
15 while (index < (len / sizeof(struct multiboot_mmap))) {
16 // dprintf("\t\tsize 0x%x\n", mmap[index].size);
17 uint64 addr = ((uint64)mmap[index].base_addr_high << 32) | mmap[index].base_addr_low;
18 dprintf("\t\taddr 0x%Lx, ", addr);
19 uint64 length = ((uint64)mmap[index].len_high << 32) | mmap[index].len_low;
20 dprintf("len 0x%Lx, ", length);
21 dprintf("type 0x%x\n", mmap[index].type);
23 index++;
27 void dump_multiboot(const void *multiboot)
29 uint32 flags = read32(multiboot, 0);
31 dprintf("multiboot struct at %p\n", multiboot);
32 dprintf("\tflags 0x%x\n", flags);
34 if (flags & (1<<0)) {
35 dprintf("\tmem_lower 0x%x\n", read32(multiboot, 4));
36 dprintf("\tmem_upper 0x%x\n", read32(multiboot, 8));
38 if (flags & (1<<1)) {
39 dprintf("\tboot_device 0x%x\n", read32(multiboot, 12));
41 if (flags & (1<<2)) {
42 dprintf("\tcmdline 0x%x\n", read32(multiboot, 16));
44 if (flags & (1<<3)) {
45 dprintf("\tmods_count 0x%x\n", read32(multiboot, 20));
46 dprintf("\tmods_addr 0x%x\n", read32(multiboot, 24));
48 if (flags & (1<<6)) {
49 dprintf("\tmmap_length 0x%x\n", read32(multiboot, 44));
50 dprintf("\tmmap_addr 0x%x\n", read32(multiboot, 48));
52 dump_multiboot_mmap((void *)(unsigned long)read32(multiboot, 48), read32(multiboot, 44));
56 void fill_ka_memranges(const void *multiboot)
58 uint32 flags;
60 flags = read32(multiboot, 0);
62 if (flags & (1<<6)) {
63 unsigned int index = 0;
64 unsigned int len = read32(multiboot, 44);
65 struct multiboot_mmap *mmap = (struct multiboot_mmap *)(addr_t)read32(multiboot, 48);
67 ka->num_phys_mem_ranges = 0;
69 while (index < (len / sizeof(struct multiboot_mmap))) {
70 if (mmap[index].type == 1) {
71 uint64 addr = ((uint64)mmap[index].base_addr_high << 32) | mmap[index].base_addr_low;
72 uint64 length = ((uint64)mmap[index].len_high << 32) | mmap[index].len_low;
74 ka->phys_mem_range[ka->num_phys_mem_ranges].start = addr;
75 ka->phys_mem_range[ka->num_phys_mem_ranges].size = length;
76 ka->num_phys_mem_ranges++;
79 index++;
81 } else {
82 panic("no multiboot mmap\n");