mbchk.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* mbchk - a simple checker for the format of a Multiboot kernel */
  2. /*
  3. * Copyright (C) 1999,2001,2002 Free Software Foundation, Inc.
  4. *
  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.
  9. *
  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.
  14. *
  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.
  18. */
  19. #include <config.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <getopt.h>
  23. #include <multiboot.h>
  24. static int quiet = 0;
  25. static char *optstring = "hvq";
  26. static struct option longopts[] =
  27. {
  28. {"help", no_argument, 0, 'h'},
  29. {"version", no_argument, 0, 'v'},
  30. {"quiet", no_argument, 0, 'q'},
  31. {0}
  32. };
  33. static void
  34. usage (int status)
  35. {
  36. if (status)
  37. fprintf (stderr, "Try ``mbchk --help'' for more information.\n");
  38. else
  39. printf ("Usage: mbchk [OPTION]... [FILE]...\n"
  40. "Check if the format of FILE complies with the Multiboot Specification.\n"
  41. "\n"
  42. "-q, --quiet suppress all normal output\n"
  43. "-h, --help display this help and exit\n"
  44. "-v, --version output version information and exit.\n"
  45. "\n"
  46. "Report bugs to <bug-grub@gnu.org>.\n");
  47. exit (status);
  48. }
  49. static int
  50. check_multiboot (const char *filename, FILE *fp)
  51. {
  52. struct multiboot_header *mbh = 0;
  53. int i;
  54. char buf[8192];
  55. if (fread (buf, 1, 8192, fp) < 0)
  56. {
  57. fprintf (stderr, "%s: Read error.\n", filename);
  58. return 0;
  59. }
  60. for (i = 0; i < 8192 - sizeof (struct multiboot_header); i++)
  61. {
  62. mbh = (struct multiboot_header *) (buf + i);
  63. if (mbh->magic == MULTIBOOT_HEADER_MAGIC)
  64. break;
  65. }
  66. if (i == 8192 - sizeof (struct multiboot_header))
  67. {
  68. fprintf (stderr, "%s: No Multiboot header.\n", filename);
  69. return 0;
  70. }
  71. if (! quiet)
  72. printf ("%s: The Multiboot header is found at the offset %d.\n",
  73. filename, i);
  74. /* Check for the checksum. */
  75. if (mbh->magic + mbh->flags + mbh->checksum != 0)
  76. {
  77. fprintf (stderr,
  78. "%s: Bad checksum (0x%lx).\n",
  79. filename, mbh->checksum);
  80. return 0;
  81. }
  82. /* Reserved flags must be zero. */
  83. if (mbh->flags & ~0x00010003)
  84. {
  85. fprintf (stderr,
  86. "%s: Non-zero is found in reserved flags (0x%lx).\n",
  87. filename, mbh->flags);
  88. return 0;
  89. }
  90. if (! quiet)
  91. {
  92. printf ("%s: Page alignment is turned %s.\n",
  93. filename, (mbh->flags & 0x1)? "on" : "off");
  94. printf ("%s: Memory information is turned %s.\n",
  95. filename, (mbh->flags & 0x2)? "on" : "off");
  96. printf ("%s: Address fields is turned %s.\n",
  97. filename, (mbh->flags & 0x10000)? "on" : "off");
  98. }
  99. /* Check for the address fields. */
  100. if (mbh->flags & 0x10000)
  101. {
  102. if (mbh->header_addr < mbh->load_addr)
  103. {
  104. fprintf (stderr,
  105. "%s: header_addr is less than "
  106. "load_addr (0x%lx > 0x%lx).\n",
  107. filename, mbh->header_addr, mbh->load_addr);
  108. return 0;
  109. }
  110. if (mbh->load_end_addr && mbh->load_addr >= mbh->load_end_addr)
  111. {
  112. fprintf (stderr,
  113. "%s: load_addr is not less than load_end_addr"
  114. " (0x%lx >= 0x%lx).\n",
  115. filename, mbh->load_addr, mbh->load_end_addr);
  116. return 0;
  117. }
  118. if (mbh->bss_end_addr && mbh->load_end_addr > mbh->bss_end_addr)
  119. {
  120. fprintf (stderr,
  121. "%s: load_end_addr is greater than bss_end_addr"
  122. " (0x%lx > 0x%lx).\n",
  123. filename, mbh->load_end_addr, mbh->bss_end_addr);
  124. return 0;
  125. }
  126. if (mbh->load_addr > mbh->entry_addr)
  127. {
  128. fprintf (stderr,
  129. "%s: load_addr is greater than entry_addr"
  130. " (0x%lx > 0x%lx).\n",
  131. filename, mbh->load_addr, mbh->entry_addr);
  132. return 0;
  133. }
  134. /* FIXME: It is better to check if the entry address is within the
  135. file, especially when the load end address is zero. */
  136. if (mbh->load_end_addr && mbh->load_end_addr <= mbh->entry_addr)
  137. {
  138. fprintf (stderr,
  139. "%s: load_end_addr is not greater than entry_addr"
  140. " (0x%lx <= 0x%lx).\n",
  141. filename, mbh->load_end_addr, mbh->entry_addr);
  142. return 0;
  143. }
  144. }
  145. if (! quiet)
  146. printf ("%s: All checks passed.\n", filename);
  147. return 1;
  148. }
  149. int
  150. main (int argc, char *argv[])
  151. {
  152. int c;
  153. do
  154. {
  155. c = getopt_long (argc, argv, optstring, longopts, 0);
  156. switch (c)
  157. {
  158. case EOF:
  159. break;
  160. case 'h':
  161. usage (0);
  162. break;
  163. case 'v':
  164. printf ("mbchk (GNU GRUB " VERSION ")\n");
  165. exit (0);
  166. break;
  167. case 'q':
  168. quiet = 1;
  169. break;
  170. default:
  171. usage (1);
  172. break;
  173. }
  174. }
  175. while (c != EOF);
  176. if (optind < argc)
  177. {
  178. while (optind < argc)
  179. {
  180. FILE *fp;
  181. fp = fopen (argv[optind], "r");
  182. if (! fp)
  183. {
  184. fprintf (stderr, "%s: No such file.\n", argv[optind]);
  185. exit (1);
  186. }
  187. if (! check_multiboot (argv[optind], fp))
  188. exit (1);
  189. fclose (fp);
  190. optind++;
  191. }
  192. }
  193. else
  194. {
  195. if (! check_multiboot ("<stdin>", stdin))
  196. exit (1);
  197. }
  198. return 0;
  199. }