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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/* mbchk - a simple checker for the format of a Multiboot kernel */
/*
* Copyright (C) 1999,2001,2002 Free Software Foundation, Inc.
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <multiboot.h>
static int quiet = 0;
static char *optstring = "hvq";
static struct option longopts[] =
{
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
{"quiet", no_argument, 0, 'q'},
{0}
};
static void
usage (int status)
{
if (status)
fprintf (stderr, "Try ``mbchk --help'' for more information.\n");
else
printf ("Usage: mbchk [OPTION]... [FILE]...\n"
"Check if the format of FILE complies with the Multiboot Specification.\n"
"\n"
"-q, --quiet suppress all normal output\n"
"-h, --help display this help and exit\n"
"-v, --version output version information and exit.\n"
"\n"
"Report bugs to <bug-grub@gnu.org>.\n");
exit (status);
}
static int
check_multiboot (const char *filename, FILE *fp)
{
multiboot_header_t *mbh = 0;
int i;
char buf[8192];
if (fread (buf, 1, 8192, fp) < 0)
{
fprintf (stderr, "%s: Read error.\n", filename);
return 0;
}
for (i = 0; i < 8192 - sizeof (multiboot_header_t); i++)
{
unsigned long magic = *((unsigned long *) (buf + i));
if (magic == MULTIBOOT_HEADER_MAGIC)
{
mbh = (multiboot_header_t *) (buf + i);
break;
}
}
if (! mbh)
{
fprintf (stderr, "%s: No Multiboot header.\n", filename);
return 0;
}
if (! quiet)
printf ("%s: The Multiboot header is found at the offset %d.\n",
filename, i);
/* Check for the checksum. */
if (mbh->magic + mbh->flags + mbh->checksum != 0)
{
fprintf (stderr,
"%s: Bad checksum (0x%lx).\n",
filename, mbh->checksum);
return 0;
}
/* Reserved flags must be zero. */
if (mbh->flags & ~0x00010003)
{
fprintf (stderr,
"%s: Non-zero is found in reserved flags (0x%lx).\n",
filename, mbh->flags);
return 0;
}
if (! quiet)
{
printf ("%s: Page alignment is turned %s.\n",
filename, (mbh->flags & 0x1)? "on" : "off");
printf ("%s: Memory information is turned %s.\n",
filename, (mbh->flags & 0x2)? "on" : "off");
printf ("%s: Address fields is turned %s.\n",
filename, (mbh->flags & 0x10000)? "on" : "off");
}
/* Check for the address fields. */
if (mbh->flags & 0x10000)
{
if (mbh->header_addr < mbh->load_addr)
{
fprintf (stderr,
"%s: header_addr is less than "
"load_addr (0x%lx > 0x%lx).\n",
filename, mbh->header_addr, mbh->load_addr);
return 0;
}
if (mbh->load_end_addr && mbh->load_addr >= mbh->load_end_addr)
{
fprintf (stderr,
"%s: load_addr is not less than load_end_addr"
" (0x%lx >= 0x%lx).\n",
filename, mbh->load_addr, mbh->load_end_addr);
return 0;
}
if (mbh->bss_end_addr && mbh->load_end_addr > mbh->bss_end_addr)
{
fprintf (stderr,
"%s: load_end_addr is greater than bss_end_addr"
" (0x%lx > 0x%lx).\n",
filename, mbh->load_end_addr, mbh->bss_end_addr);
return 0;
}
if (mbh->load_addr > mbh->entry_addr)
{
fprintf (stderr,
"%s: load_addr is greater than entry_addr"
" (0x%lx > 0x%lx).\n",
filename, mbh->load_addr, mbh->entry_addr);
return 0;
}
/* FIXME: It is better to check if the entry address is within the
file, especially when the load end address is zero. */
if (mbh->load_end_addr && mbh->load_end_addr <= mbh->entry_addr)
{
fprintf (stderr,
"%s: load_end_addr is not greater than entry_addr"
" (0x%lx <= 0x%lx).\n",
filename, mbh->load_end_addr, mbh->entry_addr);
return 0;
}
/* This is a GRUB-specific limitation. */
if (mbh->load_addr < 0x100000)
{
fprintf (stderr,
"%s: Cannot be loaded at less than 1MB by GRUB"
" (0x%lx).\n",
filename, mbh->load_addr);
return 0;
}
}
if (! quiet)
printf ("%s: All checks passed.\n", filename);
return 1;
}
int
main (int argc, char *argv[])
{
int c;
do
{
c = getopt_long (argc, argv, optstring, longopts, 0);
switch (c)
{
case EOF:
break;
case 'h':
usage (0);
break;
case 'v':
printf ("mbchk (GNU GRUB " VERSION ")\n");
exit (0);
break;
case 'q':
quiet = 1;
break;
default:
usage (1);
break;
}
}
while (c != EOF);
if (optind < argc)
{
while (optind < argc)
{
FILE *fp;
fp = fopen (argv[optind], "r");
if (! fp)
{
fprintf (stderr, "%s: No such file.\n", argv[optind]);
exit (1);
}
if (! check_multiboot (argv[optind], fp))
exit (1);
fclose (fp);
optind++;
}
}
else
{
if (! check_multiboot ("<stdin>", stdin))
exit (1);
}
return 0;
}
|