summaryrefslogtreecommitdiff
path: root/src/arch/i386/boot/multiboot.c
blob: d331193416735fa9faf9edf1bfce2672cd5489f1 (plain)
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
/*
 * support for Multiboot payloads
 *
 * Copyright (C) 2008 Robert Millan
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <cpu/x86/multiboot.h>
#include <string.h>
#include <device/resource.h>
#include <console/console.h>

static struct multiboot_mmap_entry *mb_mem;
struct multiboot_info *mbi;

static struct {
	u64 addr;
	u64 len;
} reserved_mem[2];

static void build_mb_mem_range_nooverlap(u64 addr, u64 len)
{
	int i;
	for (i = 0; i < sizeof(reserved_mem) / sizeof(reserved_mem[0]); i++) {
		/* free region fully contained in reserved region, abort */
		if (addr >= reserved_mem[i].addr && addr + len <= reserved_mem[i].addr + reserved_mem[i].len)
			return;
		/* reserved region splits free region */
		if (addr < reserved_mem[i].addr && addr + len > reserved_mem[i].addr + reserved_mem[i].len) {
			build_mb_mem_range_nooverlap(addr, reserved_mem[i].addr - addr);
			build_mb_mem_range_nooverlap(reserved_mem[i].addr + reserved_mem[i].len, (addr + len) - (reserved_mem[i].addr + reserved_mem[i].len));
			return;
		}
		/* left overlap */
		if (addr < reserved_mem[i].addr + reserved_mem[i].len && addr + len > reserved_mem[i].addr + reserved_mem[i].len) {
			len += addr;
			addr = reserved_mem[i].addr + reserved_mem[i].len;
			len -= addr;
			/* len += addr - old_addr */
			continue;
		}
		/* right overlap */
		if (addr < reserved_mem[i].addr && addr + len > reserved_mem[i].addr) {
			len = reserved_mem[i].addr - addr;
			continue;
		}
		/* none of the above, just add it */
	}

	mb_mem->addr = addr;
	mb_mem->len = len;
	mb_mem->type = 1;
	mb_mem->size = sizeof(*mb_mem) - sizeof(mb_mem->size);
	mb_mem++;
}

static void build_mb_mem_range(void *gp, struct device *dev, struct resource *res)
{
	build_mb_mem_range_nooverlap(res->base, res->size);
}

#define ROUND(_r,_a) (((_r) + (((_a) - 1))) & ~((_a) - 1))

unsigned long write_multiboot_info(
	unsigned long low_table_start, unsigned long low_table_end,
	unsigned long rom_table_start, unsigned long rom_table_end)
{
	struct multiboot_info *mbi;
	int i;

	mbi = (struct multiboot_info *)rom_table_end;
	memset(mbi, 0, sizeof(*mbi));
	rom_table_end += sizeof(*mbi);

	mbi->mmap_addr = (u32) rom_table_end;
	mb_mem = (struct multiboot_mmap_entry *)rom_table_end;

	/* reserved regions */
	reserved_mem[0].addr = low_table_start;
	reserved_mem[0].len = ROUND(low_table_end - low_table_start, 4096);
	reserved_mem[1].addr = rom_table_start;
	reserved_mem[1].len = ROUND(rom_table_end - rom_table_start, 4096);

	for (i = 0; i < sizeof(reserved_mem) / sizeof(reserved_mem[0]); i++) {
		mb_mem->addr = reserved_mem[i].addr;
		mb_mem->len = reserved_mem[i].len;
		mb_mem->type = 2;
		mb_mem->size = sizeof(*mb_mem) - sizeof(mb_mem->size);
		mb_mem++;
	}

	/* free regions */
	search_global_resources( IORESOURCE_MEM | IORESOURCE_CACHEABLE,
		IORESOURCE_MEM | IORESOURCE_CACHEABLE, build_mb_mem_range, NULL);

	mbi->mmap_length = ((u32) mb_mem) - mbi->mmap_addr;
	mbi->flags |= MB_INFO_MEM_MAP;

	printk_info("Multiboot Information structure has been written.\n");

	return (unsigned long)mb_mem;
}