Merge branch 'master' of git://github.com/illumos/illumos-gate
[unleashed.git] / usr / src / grub / grub-0.97 / util / mbchk.c
blobfd71858279675eb719e3383af7333e0a2b6713ba
1 /* mbchk - a simple checker for the format of a Multiboot kernel */
2 /*
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.
20 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <getopt.h>
25 #include <multiboot.h>
27 static int quiet = 0;
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'},
34 {0}
37 static void
38 usage (int status)
40 if (status)
41 fprintf (stderr, "Try ``mbchk --help'' for more information.\n");
42 else
43 printf ("Usage: mbchk [OPTION]... [FILE]...\n"
44 "Check if the format of FILE complies with the Multiboot Specification.\n"
45 "\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"
49 "\n"
50 "Report bugs to <bug-grub@gnu.org>.\n");
52 exit (status);
55 static int
56 check_multiboot (const char *filename, FILE *fp)
58 multiboot_header_t *mbh = 0;
59 int i;
60 char buf[8192];
62 if (fread (buf, 1, 8192, fp) < 0)
64 fprintf (stderr, "%s: Read error.\n", filename);
65 return 0;
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);
75 break;
79 if (! mbh)
81 fprintf (stderr, "%s: No Multiboot header.\n", filename);
82 return 0;
85 if (! quiet)
86 printf ("%s: The Multiboot header is found at the offset %d.\n",
87 filename, i);
89 /* Check for the checksum. */
90 if (mbh->magic + mbh->flags + mbh->checksum != 0)
92 fprintf (stderr,
93 "%s: Bad checksum (0x%lx).\n",
94 filename, mbh->checksum);
95 return 0;
98 /* Reserved flags must be zero. */
99 if (mbh->flags & ~0x00010003)
101 fprintf (stderr,
102 "%s: Non-zero is found in reserved flags (0x%lx).\n",
103 filename, mbh->flags);
104 return 0;
107 if (! quiet)
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)
122 fprintf (stderr,
123 "%s: header_addr is less than "
124 "load_addr (0x%lx > 0x%lx).\n",
125 filename, mbh->header_addr, mbh->load_addr);
126 return 0;
129 if (mbh->load_end_addr && mbh->load_addr >= mbh->load_end_addr)
131 fprintf (stderr,
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);
135 return 0;
138 if (mbh->bss_end_addr && mbh->load_end_addr > mbh->bss_end_addr)
140 fprintf (stderr,
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);
144 return 0;
147 if (mbh->load_addr > mbh->entry_addr)
149 fprintf (stderr,
150 "%s: load_addr is greater than entry_addr"
151 " (0x%lx > 0x%lx).\n",
152 filename, mbh->load_addr, mbh->entry_addr);
153 return 0;
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)
160 fprintf (stderr,
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);
164 return 0;
167 /* This is a GRUB-specific limitation. */
168 if (mbh->load_addr < 0x100000)
170 fprintf (stderr,
171 "%s: Cannot be loaded at less than 1MB by GRUB"
172 " (0x%lx).\n",
173 filename, mbh->load_addr);
174 return 0;
178 if (! quiet)
179 printf ("%s: All checks passed.\n", filename);
181 return 1;
185 main (int argc, char *argv[])
187 int c;
191 c = getopt_long (argc, argv, optstring, longopts, 0);
192 switch (c)
194 case EOF:
195 break;
197 case 'h':
198 usage (0);
199 break;
201 case 'v':
202 printf ("mbchk (GNU GRUB " VERSION ")\n");
203 exit (0);
204 break;
206 case 'q':
207 quiet = 1;
208 break;
210 default:
211 usage (1);
212 break;
215 while (c != EOF);
217 if (optind < argc)
219 while (optind < argc)
221 FILE *fp;
223 fp = fopen (argv[optind], "r");
224 if (! fp)
226 fprintf (stderr, "%s: No such file.\n", argv[optind]);
227 exit (1);
230 if (! check_multiboot (argv[optind], fp))
231 exit (1);
233 fclose (fp);
234 optind++;
237 else
239 if (! check_multiboot ("<stdin>", stdin))
240 exit (1);
243 return 0;