1
/* mbchk - a simple checker for the format of a Multiboot kernel */
3
* Copyright (C) 1999,2001,2002 Free Software Foundation, Inc.
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
#include <multiboot.h>
28
static char *optstring = "hvq";
29
static struct option longopts[] =
31
{"help", no_argument, 0, 'h'},
32
{"version", no_argument, 0, 'v'},
33
{"quiet", no_argument, 0, 'q'},
41
fprintf (stderr, "Try ``mbchk --help'' for more information.\n");
43
printf ("Usage: mbchk [OPTION]... [FILE]...\n"
44
"Check if the format of FILE complies with the Multiboot Specification.\n"
46
"-q, --quiet suppress all normal output\n"
47
"-h, --help display this help and exit\n"
48
"-v, --version output version information and exit.\n"
50
"Report bugs to <bug-grub@gnu.org>.\n");
56
check_multiboot (const char *filename, FILE *fp)
58
multiboot_header_t *mbh = 0;
62
if (fread (buf, 1, 8192, fp) < 0)
64
fprintf (stderr, "%s: Read error.\n", filename);
68
for (i = 0; i < 8192 - sizeof (multiboot_header_t); i++)
70
unsigned long magic = *((unsigned long *) (buf + i));
72
if (magic == MULTIBOOT_HEADER_MAGIC)
74
mbh = (multiboot_header_t *) (buf + i);
81
fprintf (stderr, "%s: No Multiboot header.\n", filename);
86
printf ("%s: The Multiboot header is found at the offset %d.\n",
89
/* Check for the checksum. */
90
if (mbh->magic + mbh->flags + mbh->checksum != 0)
93
"%s: Bad checksum (0x%lx).\n",
94
filename, mbh->checksum);
98
/* Reserved flags must be zero. */
99
if (mbh->flags & ~0x00010003)
102
"%s: Non-zero is found in reserved flags (0x%lx).\n",
103
filename, mbh->flags);
109
printf ("%s: Page alignment is turned %s.\n",
110
filename, (mbh->flags & 0x1)? "on" : "off");
111
printf ("%s: Memory information is turned %s.\n",
112
filename, (mbh->flags & 0x2)? "on" : "off");
113
printf ("%s: Address fields is turned %s.\n",
114
filename, (mbh->flags & 0x10000)? "on" : "off");
117
/* Check for the address fields. */
118
if (mbh->flags & 0x10000)
120
if (mbh->header_addr < mbh->load_addr)
123
"%s: header_addr is less than "
124
"load_addr (0x%lx > 0x%lx).\n",
125
filename, mbh->header_addr, mbh->load_addr);
129
if (mbh->load_end_addr && mbh->load_addr >= mbh->load_end_addr)
132
"%s: load_addr is not less than load_end_addr"
133
" (0x%lx >= 0x%lx).\n",
134
filename, mbh->load_addr, mbh->load_end_addr);
138
if (mbh->bss_end_addr && mbh->load_end_addr > mbh->bss_end_addr)
141
"%s: load_end_addr is greater than bss_end_addr"
142
" (0x%lx > 0x%lx).\n",
143
filename, mbh->load_end_addr, mbh->bss_end_addr);
147
if (mbh->load_addr > mbh->entry_addr)
150
"%s: load_addr is greater than entry_addr"
151
" (0x%lx > 0x%lx).\n",
152
filename, mbh->load_addr, mbh->entry_addr);
156
/* FIXME: It is better to check if the entry address is within the
157
file, especially when the load end address is zero. */
158
if (mbh->load_end_addr && mbh->load_end_addr <= mbh->entry_addr)
161
"%s: load_end_addr is not greater than entry_addr"
162
" (0x%lx <= 0x%lx).\n",
163
filename, mbh->load_end_addr, mbh->entry_addr);
167
/* This is a GRUB-specific limitation. */
168
if (mbh->load_addr < 0x100000)
171
"%s: Cannot be loaded at less than 1MB by GRUB"
173
filename, mbh->load_addr);
179
printf ("%s: All checks passed.\n", filename);
185
main (int argc, char *argv[])
191
c = getopt_long (argc, argv, optstring, longopts, 0);
202
printf ("mbchk (GNU GRUB " VERSION ")\n");
219
while (optind < argc)
223
fp = fopen (argv[optind], "r");
226
fprintf (stderr, "%s: No such file.\n", argv[optind]);
230
if (! check_multiboot (argv[optind], fp))
239
if (! check_multiboot ("<stdin>", stdin))