xref: /illumos-gate/usr/src/grub/grub-0.97/util/mbchk.c (revision 1b8adde7)
17c478bd9Sstevel@tonic-gate /* mbchk - a simple checker for the format of a Multiboot kernel */
27c478bd9Sstevel@tonic-gate /*
37c478bd9Sstevel@tonic-gate  *  Copyright (C) 1999,2001,2002  Free Software Foundation, Inc.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  *  This program is free software; you can redistribute it and/or modify
67c478bd9Sstevel@tonic-gate  *  it under the terms of the GNU General Public License as published by
77c478bd9Sstevel@tonic-gate  *  the Free Software Foundation; either version 2 of the License, or
87c478bd9Sstevel@tonic-gate  *  (at your option) any later version.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  *  This program is distributed in the hope that it will be useful,
117c478bd9Sstevel@tonic-gate  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
127c478bd9Sstevel@tonic-gate  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
137c478bd9Sstevel@tonic-gate  *  GNU General Public License for more details.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  *  You should have received a copy of the GNU General Public License
167c478bd9Sstevel@tonic-gate  *  along with this program; if not, write to the Free Software
177c478bd9Sstevel@tonic-gate  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
187c478bd9Sstevel@tonic-gate  */
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include <config.h>
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include <stdio.h>
237c478bd9Sstevel@tonic-gate #include <stdlib.h>
247c478bd9Sstevel@tonic-gate #include <getopt.h>
257c478bd9Sstevel@tonic-gate #include <multiboot.h>
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate static int quiet = 0;
287c478bd9Sstevel@tonic-gate static char *optstring = "hvq";
297c478bd9Sstevel@tonic-gate static struct option longopts[] =
307c478bd9Sstevel@tonic-gate {
317c478bd9Sstevel@tonic-gate   {"help", no_argument, 0, 'h'},
327c478bd9Sstevel@tonic-gate   {"version", no_argument, 0, 'v'},
337c478bd9Sstevel@tonic-gate   {"quiet", no_argument, 0, 'q'},
347c478bd9Sstevel@tonic-gate   {0}
357c478bd9Sstevel@tonic-gate };
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate static void
usage(int status)387c478bd9Sstevel@tonic-gate usage (int status)
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate   if (status)
417c478bd9Sstevel@tonic-gate     fprintf (stderr, "Try ``mbchk --help'' for more information.\n");
427c478bd9Sstevel@tonic-gate   else
437c478bd9Sstevel@tonic-gate     printf ("Usage: mbchk [OPTION]... [FILE]...\n"
447c478bd9Sstevel@tonic-gate 	    "Check if the format of FILE complies with the Multiboot Specification.\n"
457c478bd9Sstevel@tonic-gate 	    "\n"
467c478bd9Sstevel@tonic-gate 	    "-q, --quiet                suppress all normal output\n"
477c478bd9Sstevel@tonic-gate 	    "-h, --help                 display this help and exit\n"
487c478bd9Sstevel@tonic-gate 	    "-v, --version              output version information and exit.\n"
497c478bd9Sstevel@tonic-gate 	    "\n"
507c478bd9Sstevel@tonic-gate 	    "Report bugs to <bug-grub@gnu.org>.\n");
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate   exit (status);
537c478bd9Sstevel@tonic-gate }
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static int
check_multiboot(const char * filename,FILE * fp)567c478bd9Sstevel@tonic-gate check_multiboot (const char *filename, FILE *fp)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate   multiboot_header_t *mbh = 0;
597c478bd9Sstevel@tonic-gate   int i;
607c478bd9Sstevel@tonic-gate   char buf[8192];
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate   if (fread (buf, 1, 8192, fp) < 0)
637c478bd9Sstevel@tonic-gate     {
647c478bd9Sstevel@tonic-gate       fprintf (stderr, "%s: Read error.\n", filename);
657c478bd9Sstevel@tonic-gate       return 0;
667c478bd9Sstevel@tonic-gate     }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate   for (i = 0; i < 8192 - sizeof (multiboot_header_t); i++)
697c478bd9Sstevel@tonic-gate     {
707c478bd9Sstevel@tonic-gate       unsigned long magic = *((unsigned long *) (buf + i));
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate       if (magic == MULTIBOOT_HEADER_MAGIC)
737c478bd9Sstevel@tonic-gate 	{
747c478bd9Sstevel@tonic-gate 	  mbh = (multiboot_header_t *) (buf + i);
757c478bd9Sstevel@tonic-gate 	  break;
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate     }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate   if (! mbh)
807c478bd9Sstevel@tonic-gate     {
817c478bd9Sstevel@tonic-gate       fprintf (stderr, "%s: No Multiboot header.\n", filename);
827c478bd9Sstevel@tonic-gate       return 0;
837c478bd9Sstevel@tonic-gate     }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate   if (! quiet)
867c478bd9Sstevel@tonic-gate     printf ("%s: The Multiboot header is found at the offset %d.\n",
877c478bd9Sstevel@tonic-gate 	    filename, i);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate   /* Check for the checksum.  */
907c478bd9Sstevel@tonic-gate   if (mbh->magic + mbh->flags + mbh->checksum != 0)
917c478bd9Sstevel@tonic-gate     {
927c478bd9Sstevel@tonic-gate       fprintf (stderr,
937c478bd9Sstevel@tonic-gate 	       "%s: Bad checksum (0x%lx).\n",
947c478bd9Sstevel@tonic-gate 	       filename, mbh->checksum);
957c478bd9Sstevel@tonic-gate       return 0;
967c478bd9Sstevel@tonic-gate     }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate   /* Reserved flags must be zero.  */
997c478bd9Sstevel@tonic-gate   if (mbh->flags & ~0x00010003)
1007c478bd9Sstevel@tonic-gate     {
1017c478bd9Sstevel@tonic-gate       fprintf (stderr,
1027c478bd9Sstevel@tonic-gate 	       "%s: Non-zero is found in reserved flags (0x%lx).\n",
1037c478bd9Sstevel@tonic-gate 	       filename, mbh->flags);
1047c478bd9Sstevel@tonic-gate       return 0;
1057c478bd9Sstevel@tonic-gate     }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate   if (! quiet)
1087c478bd9Sstevel@tonic-gate     {
1097c478bd9Sstevel@tonic-gate       printf ("%s: Page alignment is turned %s.\n",
1107c478bd9Sstevel@tonic-gate 	      filename, (mbh->flags & 0x1)? "on" : "off");
1117c478bd9Sstevel@tonic-gate       printf ("%s: Memory information is turned %s.\n",
1127c478bd9Sstevel@tonic-gate 	      filename, (mbh->flags & 0x2)? "on" : "off");
1137c478bd9Sstevel@tonic-gate       printf ("%s: Address fields is turned %s.\n",
1147c478bd9Sstevel@tonic-gate 	      filename, (mbh->flags & 0x10000)? "on" : "off");
1157c478bd9Sstevel@tonic-gate     }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate   /* Check for the address fields.  */
1187c478bd9Sstevel@tonic-gate   if (mbh->flags & 0x10000)
1197c478bd9Sstevel@tonic-gate     {
1207c478bd9Sstevel@tonic-gate       if (mbh->header_addr < mbh->load_addr)
1217c478bd9Sstevel@tonic-gate 	{
1227c478bd9Sstevel@tonic-gate 	  fprintf (stderr,
1237c478bd9Sstevel@tonic-gate 		   "%s: header_addr is less than "
1247c478bd9Sstevel@tonic-gate 		   "load_addr (0x%lx > 0x%lx).\n",
1257c478bd9Sstevel@tonic-gate 		   filename, mbh->header_addr, mbh->load_addr);
1267c478bd9Sstevel@tonic-gate 	  return 0;
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate       if (mbh->load_end_addr && mbh->load_addr >= mbh->load_end_addr)
1307c478bd9Sstevel@tonic-gate 	{
1317c478bd9Sstevel@tonic-gate 	  fprintf (stderr,
1327c478bd9Sstevel@tonic-gate 		   "%s: load_addr is not less than load_end_addr"
1337c478bd9Sstevel@tonic-gate 		   " (0x%lx >= 0x%lx).\n",
1347c478bd9Sstevel@tonic-gate 		   filename, mbh->load_addr, mbh->load_end_addr);
1357c478bd9Sstevel@tonic-gate 	  return 0;
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate       if (mbh->bss_end_addr && mbh->load_end_addr > mbh->bss_end_addr)
1397c478bd9Sstevel@tonic-gate 	{
1407c478bd9Sstevel@tonic-gate 	  fprintf (stderr,
1417c478bd9Sstevel@tonic-gate 		   "%s: load_end_addr is greater than bss_end_addr"
1427c478bd9Sstevel@tonic-gate 		   " (0x%lx > 0x%lx).\n",
1437c478bd9Sstevel@tonic-gate 		   filename, mbh->load_end_addr, mbh->bss_end_addr);
1447c478bd9Sstevel@tonic-gate 	  return 0;
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate       if (mbh->load_addr > mbh->entry_addr)
1487c478bd9Sstevel@tonic-gate 	{
1497c478bd9Sstevel@tonic-gate 	  fprintf (stderr,
1507c478bd9Sstevel@tonic-gate 		   "%s: load_addr is greater than entry_addr"
1517c478bd9Sstevel@tonic-gate 		   " (0x%lx > 0x%lx).\n",
1527c478bd9Sstevel@tonic-gate 		   filename, mbh->load_addr, mbh->entry_addr);
1537c478bd9Sstevel@tonic-gate 	  return 0;
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate       /* FIXME: It is better to check if the entry address is within the
1577c478bd9Sstevel@tonic-gate 	 file, especially when the load end address is zero.  */
1587c478bd9Sstevel@tonic-gate       if (mbh->load_end_addr && mbh->load_end_addr <= mbh->entry_addr)
1597c478bd9Sstevel@tonic-gate 	{
1607c478bd9Sstevel@tonic-gate 	  fprintf (stderr,
1617c478bd9Sstevel@tonic-gate 		   "%s: load_end_addr is not greater than entry_addr"
1627c478bd9Sstevel@tonic-gate 		   " (0x%lx <= 0x%lx).\n",
1637c478bd9Sstevel@tonic-gate 		   filename, mbh->load_end_addr, mbh->entry_addr);
1647c478bd9Sstevel@tonic-gate 	  return 0;
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate       /* This is a GRUB-specific limitation.  */
1687c478bd9Sstevel@tonic-gate       if (mbh->load_addr < 0x100000)
1697c478bd9Sstevel@tonic-gate 	{
1707c478bd9Sstevel@tonic-gate 	  fprintf (stderr,
1717c478bd9Sstevel@tonic-gate 		   "%s: Cannot be loaded at less than 1MB by GRUB"
1727c478bd9Sstevel@tonic-gate 		   " (0x%lx).\n",
1737c478bd9Sstevel@tonic-gate 		   filename, mbh->load_addr);
1747c478bd9Sstevel@tonic-gate 	  return 0;
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate     }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate   if (! quiet)
1797c478bd9Sstevel@tonic-gate     printf ("%s: All checks passed.\n", filename);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate   return 1;
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])1857c478bd9Sstevel@tonic-gate main (int argc, char *argv[])
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate   int c;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate   do
1907c478bd9Sstevel@tonic-gate     {
1917c478bd9Sstevel@tonic-gate       c = getopt_long (argc, argv, optstring, longopts, 0);
1927c478bd9Sstevel@tonic-gate       switch (c)
1937c478bd9Sstevel@tonic-gate 	{
1947c478bd9Sstevel@tonic-gate 	case EOF:
1957c478bd9Sstevel@tonic-gate 	  break;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	case 'h':
1987c478bd9Sstevel@tonic-gate 	  usage (0);
1997c478bd9Sstevel@tonic-gate 	  break;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	case 'v':
2027c478bd9Sstevel@tonic-gate 	  printf ("mbchk (GNU GRUB " VERSION ")\n");
2037c478bd9Sstevel@tonic-gate 	  exit (0);
2047c478bd9Sstevel@tonic-gate 	  break;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	case 'q':
2077c478bd9Sstevel@tonic-gate 	  quiet = 1;
2087c478bd9Sstevel@tonic-gate 	  break;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	default:
2117c478bd9Sstevel@tonic-gate 	  usage (1);
2127c478bd9Sstevel@tonic-gate 	  break;
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate     }
2157c478bd9Sstevel@tonic-gate   while (c != EOF);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate   if (optind < argc)
2187c478bd9Sstevel@tonic-gate     {
2197c478bd9Sstevel@tonic-gate       while (optind < argc)
2207c478bd9Sstevel@tonic-gate 	{
2217c478bd9Sstevel@tonic-gate 	  FILE *fp;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	  fp = fopen (argv[optind], "r");
2247c478bd9Sstevel@tonic-gate 	  if (! fp)
2257c478bd9Sstevel@tonic-gate 	    {
2267c478bd9Sstevel@tonic-gate 	      fprintf (stderr, "%s: No such file.\n", argv[optind]);
2277c478bd9Sstevel@tonic-gate 	      exit (1);
2287c478bd9Sstevel@tonic-gate 	    }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	  if (! check_multiboot (argv[optind], fp))
2317c478bd9Sstevel@tonic-gate 	    exit (1);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	  fclose (fp);
2347c478bd9Sstevel@tonic-gate 	  optind++;
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate     }
2377c478bd9Sstevel@tonic-gate   else
2387c478bd9Sstevel@tonic-gate     {
2397c478bd9Sstevel@tonic-gate       if (! check_multiboot ("<stdin>", stdin))
2407c478bd9Sstevel@tonic-gate 	exit (1);
2417c478bd9Sstevel@tonic-gate     }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate   return 0;
2447c478bd9Sstevel@tonic-gate }
245