-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrub.cpp
173 lines (122 loc) · 4.42 KB
/
grub.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/***************************************************************************
* grub.cpp
*
* Copyright 2008 Matthias v.d. Vlies
* matthias@mserver.nl
****************************************************************************/
/*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*! \file grub.cpp
* \brief GrubChecker
*
* This file defines the GrubChecker class. This class checks if the Multiboot info header provided by GRUB is correct.
* It also parses the memory size of the system.
*
*\see http://www.gnu.org/software/grub/manual/multiboot/html_node/kernel_002ec.html#kernel_002ec
*/
#include <grub/grub.h>
#include <grub/multiboot.h>
#include <core/console.h>
#include <errors.h>
Grub::GrubChecker::GrubChecker(unsigned long magic, unsigned long address) {
this->_address = address;
this->_magic = magic;
// auto register
Core::ResourceManager::getInstance()->registerResource(this);
}
/*! Macro for checking if a flag-bit is set in a grub multibootheader
*
*\param flags The flag to check
*\param bit The bit number of the bit to check */
#define CHECK_FLAG(flags,bit) ((flags) & (1 << (bit)))
unsigned long Grub::GrubChecker::startResource() {
bool valid = true;
bool warning = false;
Core::Console* console = Core::Console::getInstance();
// Am I booted by a Multiboot-compliant boot loader?
if (this->_magic != MULTIBOOT_BOOTLOADER_MAGIC) {
console->write("\nInvalid bootloader magic!!!");
valid = false;
}
multiboot_info_t* multibootInfo = reinterpret_cast<multiboot_info_t*>(this->_address);
// Are mem_* valid?
if (!CHECK_FLAG (multibootInfo->flags, 0)) {
console->write("\nInvalid memory area");
valid = false;
}
else {
this->_memorySize = multibootInfo->mem_lower + multibootInfo->mem_upper;
}
// valid boot device?
if (!CHECK_FLAG (multibootInfo->flags, 1)) {
console->write("\nInvalid boot device");
valid = false;
}
// Is the command line passed?
if (!CHECK_FLAG (multibootInfo->flags, 2)) {
console->write("\nNo command line passed");
valid = false;
}
#ifdef DEBUG
// Are mods_* valid?
if (!CHECK_FLAG (multibootInfo->flags, 3)) {
// just warn
console->write("\nInvalid modules");
warning = true;
}
#endif
// Bits 4 and 5 are mutually exclusive!
if (CHECK_FLAG (multibootInfo->flags, 4) && CHECK_FLAG (multibootInfo->flags, 5)) {
console->write("\nNo mutual exclusion on bit 4 and 6");
valid = false;
}
#ifdef DEBUG
// Is the symbol table of a.out valid?
if (!CHECK_FLAG (multibootInfo->flags, 4)) {
// just warn
console->write("\nInvalid a.out symble table");
warning = true;
}
// Is the section header table of ELF valid?
if (!CHECK_FLAG (multibootInfo->flags, 5)) {
// just warn
console->write("\nInvalid ELF section header");
warning = true;
}
#endif
// Are mmap_* valid?
if (!CHECK_FLAG (multibootInfo->flags, 6)) {
console->write("\nInvalid mmap");
valid = false;
}
#ifdef DEBUG
if(warning && valid) {
console->write("\n");
return E_WARNING;
}
#endif
if(valid)
return E_SUCCESS;
console->write("\n");
// something really wrong!
return E_PANIC;
}
const char* Grub::GrubChecker::getResourceName() {
return "GRUB multiboot header";
}
unsigned long Grub::GrubChecker::getMemorySize() {
return this->_memorySize;
}