mbchk.c revision 1
1N/A/* mbchk - a simple checker for the format of a Multiboot kernel */
1N/A/*
1N/A * Copyright (C) 1999,2001,2002 Free Software Foundation, Inc.
1N/A *
1N/A * This program is free software; you can redistribute it and/or modify
1N/A * it under the terms of the GNU General Public License as published by
1N/A * the Free Software Foundation; either version 2 of the License, or
1N/A * (at your option) any later version.
1N/A *
1N/A * This program is distributed in the hope that it will be useful,
1N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A * GNU General Public License for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program; if not, write to the Free Software
1N/A * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1N/A */
1N/A
1N/A#include <config.h>
1N/A
1N/A#include <stdio.h>
1N/A#include <stdlib.h>
1N/A#include <getopt.h>
1N/A#include <multiboot.h>
1N/A
1N/Astatic int quiet = 0;
1N/Astatic char *optstring = "hvq";
1N/Astatic struct option longopts[] =
1N/A{
1N/A {"help", no_argument, 0, 'h'},
1N/A {"version", no_argument, 0, 'v'},
1N/A {"quiet", no_argument, 0, 'q'},
1N/A {0}
1N/A};
1N/A
1N/Astatic void
1N/Ausage (int status)
1N/A{
1N/A if (status)
1N/A fprintf (stderr, "Try ``mbchk --help'' for more information.\n");
1N/A else
1N/A printf ("Usage: mbchk [OPTION]... [FILE]...\n"
1N/A "Check if the format of FILE complies with the Multiboot Specification.\n"
1N/A "\n"
1N/A "-q, --quiet suppress all normal output\n"
1N/A "-h, --help display this help and exit\n"
1N/A "-v, --version output version information and exit.\n"
1N/A "\n"
1N/A "Report bugs to <bug-grub@gnu.org>.\n");
1N/A
1N/A exit (status);
1N/A}
1N/A
1N/Astatic int
1N/Acheck_multiboot (const char *filename, FILE *fp)
1N/A{
1N/A multiboot_header_t *mbh = 0;
1N/A int i;
1N/A char buf[8192];
1N/A
1N/A if (fread (buf, 1, 8192, fp) < 0)
1N/A {
1N/A fprintf (stderr, "%s: Read error.\n", filename);
1N/A return 0;
1N/A }
1N/A
1N/A for (i = 0; i < 8192 - sizeof (multiboot_header_t); i++)
1N/A {
1N/A unsigned long magic = *((unsigned long *) (buf + i));
1N/A
1N/A if (magic == MULTIBOOT_HEADER_MAGIC)
1N/A {
1N/A mbh = (multiboot_header_t *) (buf + i);
1N/A break;
1N/A }
1N/A }
1N/A
1N/A if (! mbh)
1N/A {
1N/A fprintf (stderr, "%s: No Multiboot header.\n", filename);
1N/A return 0;
1N/A }
1N/A
1N/A if (! quiet)
1N/A printf ("%s: The Multiboot header is found at the offset %d.\n",
1N/A filename, i);
1N/A
1N/A /* Check for the checksum. */
1N/A if (mbh->magic + mbh->flags + mbh->checksum != 0)
1N/A {
1N/A fprintf (stderr,
1N/A "%s: Bad checksum (0x%lx).\n",
1N/A filename, mbh->checksum);
1N/A return 0;
1N/A }
1N/A
1N/A /* Reserved flags must be zero. */
1N/A if (mbh->flags & ~0x00010003)
1N/A {
1N/A fprintf (stderr,
1N/A "%s: Non-zero is found in reserved flags (0x%lx).\n",
1N/A filename, mbh->flags);
1N/A return 0;
1N/A }
1N/A
1N/A if (! quiet)
1N/A {
1N/A printf ("%s: Page alignment is turned %s.\n",
1N/A filename, (mbh->flags & 0x1)? "on" : "off");
1N/A printf ("%s: Memory information is turned %s.\n",
1N/A filename, (mbh->flags & 0x2)? "on" : "off");
1N/A printf ("%s: Address fields is turned %s.\n",
1N/A filename, (mbh->flags & 0x10000)? "on" : "off");
1N/A }
1N/A
1N/A /* Check for the address fields. */
1N/A if (mbh->flags & 0x10000)
1N/A {
1N/A if (mbh->header_addr < mbh->load_addr)
1N/A {
1N/A fprintf (stderr,
1N/A "%s: header_addr is less than "
1N/A "load_addr (0x%lx > 0x%lx).\n",
1N/A filename, mbh->header_addr, mbh->load_addr);
1N/A return 0;
1N/A }
1N/A
1N/A if (mbh->load_end_addr && mbh->load_addr >= mbh->load_end_addr)
1N/A {
1N/A fprintf (stderr,
1N/A "%s: load_addr is not less than load_end_addr"
1N/A " (0x%lx >= 0x%lx).\n",
1N/A filename, mbh->load_addr, mbh->load_end_addr);
1N/A return 0;
1N/A }
1N/A
1N/A if (mbh->bss_end_addr && mbh->load_end_addr > mbh->bss_end_addr)
1N/A {
1N/A fprintf (stderr,
1N/A "%s: load_end_addr is greater than bss_end_addr"
1N/A " (0x%lx > 0x%lx).\n",
1N/A filename, mbh->load_end_addr, mbh->bss_end_addr);
1N/A return 0;
1N/A }
1N/A
1N/A if (mbh->load_addr > mbh->entry_addr)
1N/A {
1N/A fprintf (stderr,
1N/A "%s: load_addr is greater than entry_addr"
1N/A " (0x%lx > 0x%lx).\n",
1N/A filename, mbh->load_addr, mbh->entry_addr);
1N/A return 0;
1N/A }
1N/A
1N/A /* FIXME: It is better to check if the entry address is within the
1N/A file, especially when the load end address is zero. */
1N/A if (mbh->load_end_addr && mbh->load_end_addr <= mbh->entry_addr)
1N/A {
1N/A fprintf (stderr,
1N/A "%s: load_end_addr is not greater than entry_addr"
1N/A " (0x%lx <= 0x%lx).\n",
1N/A filename, mbh->load_end_addr, mbh->entry_addr);
1N/A return 0;
1N/A }
1N/A
1N/A /* This is a GRUB-specific limitation. */
1N/A if (mbh->load_addr < 0x100000)
1N/A {
1N/A fprintf (stderr,
1N/A "%s: Cannot be loaded at less than 1MB by GRUB"
1N/A " (0x%lx).\n",
1N/A filename, mbh->load_addr);
1N/A return 0;
1N/A }
1N/A }
1N/A
1N/A if (! quiet)
1N/A printf ("%s: All checks passed.\n", filename);
1N/A
1N/A return 1;
1N/A}
1N/A
1N/Aint
1N/Amain (int argc, char *argv[])
1N/A{
1N/A int c;
1N/A
1N/A do
1N/A {
1N/A c = getopt_long (argc, argv, optstring, longopts, 0);
1N/A switch (c)
1N/A {
1N/A case EOF:
1N/A break;
1N/A
1N/A case 'h':
1N/A usage (0);
1N/A break;
1N/A
1N/A case 'v':
1N/A printf ("mbchk (GNU GRUB " VERSION ")\n");
1N/A exit (0);
1N/A break;
1N/A
1N/A case 'q':
1N/A quiet = 1;
1N/A break;
1N/A
1N/A default:
1N/A usage (1);
1N/A break;
1N/A }
1N/A }
1N/A while (c != EOF);
1N/A
1N/A if (optind < argc)
1N/A {
1N/A while (optind < argc)
1N/A {
1N/A FILE *fp;
1N/A
1N/A fp = fopen (argv[optind], "r");
1N/A if (! fp)
1N/A {
1N/A fprintf (stderr, "%s: No such file.\n", argv[optind]);
1N/A exit (1);
1N/A }
1N/A
1N/A if (! check_multiboot (argv[optind], fp))
1N/A exit (1);
1N/A
1N/A fclose (fp);
1N/A optind++;
1N/A }
1N/A }
1N/A else
1N/A {
1N/A if (! check_multiboot ("<stdin>", stdin))
1N/A exit (1);
1N/A }
1N/A
1N/A return 0;
1N/A}
1N/A