grub-mkimg.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* grub-mkrawmod.c - tool to generate raw image 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. {"base", required_argument, 0, 'b'},
  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 img 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. -b, --base=ADDR set base address\n\
  59. \n\
  60. Report bugs to <%s>.\n"), program_name, PACKAGE_BUGREPORT);
  61. exit (status);
  62. }
  63. static void
  64. save_image (struct grub_util_obj *obj, grub_uint32_t base, FILE *fp)
  65. {
  66. struct grub_util_obj_segment *seg;
  67. seg = obj->segments;
  68. while (seg)
  69. {
  70. if (seg->segment.type == GRUB_OBJ_SEG_BSS)
  71. break;
  72. grub_util_write_image_at (seg->data, seg->segment.size,
  73. seg->segment.offset - base, fp);
  74. seg = seg->next;
  75. }
  76. }
  77. static void
  78. mkrawimage (char *objs[], grub_uint32_t base, FILE *fp)
  79. {
  80. struct grub_util_obj *obj;
  81. obj = xmalloc_zero (sizeof (*obj));
  82. while (*objs)
  83. {
  84. char *image;
  85. int size;
  86. image = grub_util_read_image (*objs);
  87. size = grub_util_get_image_size (*objs);
  88. grub_obj_import (obj, image, size);
  89. free (image);
  90. objs++;
  91. }
  92. grub_obj_reverse (obj);
  93. grub_obj_sort_segments (obj);
  94. grub_obj_merge_segments (obj, 0, GRUB_OBJ_MERGE_ALL);
  95. grub_obj_reloc_symbols (obj, GRUB_OBJ_MERGE_ALL);
  96. grub_obj_link (obj, base);
  97. save_image (obj, base, fp);
  98. grub_obj_free (obj);
  99. }
  100. int
  101. main (int argc, char *argv[])
  102. {
  103. FILE *fp = stdout;
  104. char *output = NULL;
  105. grub_uint32_t base = 0;
  106. set_program_name (argv[0]);
  107. grub_util_init_nls ();
  108. /* Check for options. */
  109. while (1)
  110. {
  111. int c = getopt_long (argc, argv, "hVvo:b:", options, 0);
  112. if (c == -1)
  113. break;
  114. else
  115. switch (c)
  116. {
  117. case 'h':
  118. usage (0);
  119. break;
  120. case 'V':
  121. printf ("%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
  122. return 0;
  123. case 'v':
  124. verbosity++;
  125. break;
  126. case 'o':
  127. if (output)
  128. free (output);
  129. output = xstrdup (optarg);
  130. break;
  131. case 'b':
  132. base = strtoul (optarg, 0, 0);
  133. break;
  134. default:
  135. usage (1);
  136. break;
  137. }
  138. }
  139. if (output)
  140. {
  141. fp = fopen (output, "wb");
  142. if (! fp)
  143. grub_util_error ("cannot open %s", output);
  144. free (output);
  145. }
  146. mkrawimage (argv + optind, base, fp);
  147. fclose (fp);
  148. return 0;
  149. }