grub-mkmod.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* grub-mkmod.c - tool to generate mod file from object files. */
  2. /*
  3. * BURG - Brand-new Universal loadeR from GRUB
  4. * Copyright 2009 Bean Lee - All Rights Reserved
  5. *
  6. * BURG is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * BURG is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with BURG. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <grub/types.h>
  21. #include <grub/util/obj.h>
  22. #include <grub/util/misc.h>
  23. #include <grub/i18n.h>
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <getopt.h>
  29. #include "progname.h"
  30. int
  31. grub_strcmp (const char *s1, const char *s2)
  32. {
  33. return strcmp (s1, s2);
  34. }
  35. static struct option options[] = {
  36. {"help", no_argument, 0, 'h'},
  37. {"version", no_argument, 0, 'V'},
  38. {"verbose", no_argument, 0, 'v'},
  39. {"output", required_argument, 0, 'o'},
  40. {"name", required_argument, 0, 'n'},
  41. {0, 0, 0, 0}
  42. };
  43. static void
  44. usage (int status)
  45. {
  46. if (status)
  47. fprintf (stderr, _("Try `%s --help' for more information.\n"), program_name);
  48. else
  49. printf (_("\
  50. Usage: %s [OPTIONS] [OBJECT_FILES].\n\
  51. \n\
  52. Tool to generate mod file from object files.\n\
  53. \nOptions:\n\
  54. -h, --help display this message and exit\n\
  55. -V, --version print version information and exit\n\
  56. -v, --verbose print verbose messages\n\
  57. -o, --output=FILE output a generated image to FILE [default=stdout]\n\
  58. -n, --name=NAME set module name\n\
  59. \n\
  60. Report bugs to <%s>.\n"), program_name, PACKAGE_BUGREPORT);
  61. exit (status);
  62. }
  63. extern int sss;
  64. static void
  65. mkmod (char *objs[], char *name, FILE *fp)
  66. {
  67. struct grub_util_obj *obj;
  68. int merge = GRUB_OBJ_MERGE_SAME;
  69. obj = xmalloc_zero (sizeof (*obj));
  70. while (*objs)
  71. {
  72. char *image;
  73. int size;
  74. image = grub_util_read_image (*objs);
  75. size = grub_util_get_image_size (*objs);
  76. grub_obj_import (obj, image, size);
  77. free (image);
  78. objs++;
  79. }
  80. grub_obj_csym_done (obj);
  81. grub_obj_got_done (obj);
  82. grub_obj_reverse (obj);
  83. grub_obj_sort_segments (obj);
  84. grub_obj_merge_segments (obj, 0, merge);
  85. grub_obj_reloc_symbols (obj, merge);
  86. grub_obj_filter_symbols (obj);
  87. grub_obj_save (obj, name, fp);
  88. grub_obj_free (obj);
  89. }
  90. int
  91. main (int argc, char *argv[])
  92. {
  93. FILE *fp = stdout;
  94. char *output = NULL;
  95. char *name = NULL;
  96. set_program_name (argv[0]);
  97. grub_util_init_nls ();
  98. /* Check for options. */
  99. while (1)
  100. {
  101. int c = getopt_long (argc, argv, "hVvo:n:", options, 0);
  102. if (c == -1)
  103. break;
  104. else
  105. switch (c)
  106. {
  107. case 'h':
  108. usage (0);
  109. break;
  110. case 'V':
  111. printf ("%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
  112. return 0;
  113. case 'v':
  114. verbosity++;
  115. break;
  116. case 'o':
  117. if (output)
  118. free (output);
  119. output = xstrdup (optarg);
  120. break;
  121. case 'n':
  122. if (name)
  123. free (name);
  124. name = xstrdup (optarg);
  125. break;
  126. default:
  127. usage (1);
  128. break;
  129. }
  130. }
  131. if (! name)
  132. {
  133. if (! output)
  134. grub_util_error ("no module name");
  135. name = grub_util_get_module_name (output);
  136. }
  137. if (output)
  138. {
  139. fp = fopen (output, "wb");
  140. if (! fp)
  141. grub_util_error ("cannot open %s", output);
  142. free (output);
  143. }
  144. mkmod (argv + optind, name, fp);
  145. fclose (fp);
  146. free (name);
  147. return 0;
  148. }