grub-syslinux2cfg.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010,2012,2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB 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 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config.h>
  19. #include <grub/util/misc.h>
  20. #include <grub/i18n.h>
  21. #include <grub/term.h>
  22. #include <grub/font.h>
  23. #include <grub/emu/hostdisk.h>
  24. #define _GNU_SOURCE 1
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include <grub/err.h>
  31. #include <grub/types.h>
  32. #include <grub/dl.h>
  33. #include <grub/misc.h>
  34. #include <grub/mm.h>
  35. #include <grub/syslinux_parse.h>
  36. #pragma GCC diagnostic ignored "-Wmissing-prototypes"
  37. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  38. #include <argp.h>
  39. #pragma GCC diagnostic error "-Wmissing-prototypes"
  40. #pragma GCC diagnostic error "-Wmissing-declarations"
  41. #include "progname.h"
  42. struct arguments
  43. {
  44. char *input;
  45. char *root;
  46. char *target_root;
  47. char *cwd;
  48. char *target_cwd;
  49. char *output;
  50. int verbosity;
  51. grub_syslinux_flavour_t flav;
  52. };
  53. static struct argp_option options[] = {
  54. {"target-root", 't', N_("DIR"), 0,
  55. N_("root directory as it will be seen on runtime [default=/]."), 0},
  56. {"root", 'r', N_("DIR"), 0,
  57. N_("root directory of the syslinux disk [default=/]."), 0},
  58. {"target-cwd", 'T', N_("DIR"), 0,
  59. N_(
  60. "current directory of syslinux as it will be seen on runtime [default is parent directory of input file]."
  61. ), 0},
  62. {"cwd", 'c', N_("DIR"), 0,
  63. N_("current directory of syslinux [default is parent directory of input file]."), 0},
  64. {"output", 'o', N_("FILE"), 0, N_("write output to FILE [default=stdout]."), 0},
  65. {"isolinux", 'i', 0, 0, N_("assume input is an isolinux configuration file."), 0},
  66. {"pxelinux", 'p', 0, 0, N_("assume input is a pxelinux configuration file."), 0},
  67. {"syslinux", 's', 0, 0, N_("assume input is a syslinux configuration file."), 0},
  68. {"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
  69. { 0, 0, 0, 0, 0, 0 }
  70. };
  71. static error_t
  72. argp_parser (int key, char *arg, struct argp_state *state)
  73. {
  74. /* Get the input argument from argp_parse, which we
  75. know is a pointer to our arguments structure. */
  76. struct arguments *arguments = state->input;
  77. switch (key)
  78. {
  79. case 't':
  80. free (arguments->target_root);
  81. arguments->target_root = xstrdup (arg);
  82. break;
  83. case 'T':
  84. free (arguments->target_cwd);
  85. arguments->target_cwd = xstrdup (arg);
  86. break;
  87. case 'c':
  88. free (arguments->cwd);
  89. arguments->cwd = xstrdup (arg);
  90. break;
  91. case 'o':
  92. free (arguments->output);
  93. arguments->output = xstrdup (arg);
  94. break;
  95. case ARGP_KEY_ARG:
  96. if (!arguments->input)
  97. {
  98. arguments->input = xstrdup (arg);
  99. return 0;
  100. }
  101. return ARGP_ERR_UNKNOWN;
  102. case 'r':
  103. free (arguments->root);
  104. arguments->root = xstrdup (arg);
  105. return 0;
  106. case 'i':
  107. arguments->flav = GRUB_SYSLINUX_ISOLINUX;
  108. break;
  109. case 's':
  110. arguments->flav = GRUB_SYSLINUX_SYSLINUX;
  111. break;
  112. case 'p':
  113. arguments->flav = GRUB_SYSLINUX_PXELINUX;
  114. break;
  115. case 'v':
  116. arguments->verbosity++;
  117. break;
  118. default:
  119. return ARGP_ERR_UNKNOWN;
  120. }
  121. return 0;
  122. }
  123. static struct argp argp = {
  124. options, argp_parser, N_("FILE"),
  125. N_("Transform syslinux config into GRUB one."),
  126. NULL, NULL, NULL
  127. };
  128. int
  129. main (int argc, char *argv[])
  130. {
  131. struct arguments arguments;
  132. grub_util_host_init (&argc, &argv);
  133. /* Check for options. */
  134. memset (&arguments, 0, sizeof (struct arguments));
  135. if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
  136. {
  137. fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
  138. exit(1);
  139. }
  140. if (!arguments.input)
  141. {
  142. fprintf (stderr, "%s", _("Missing arguments\n"));
  143. exit(1);
  144. }
  145. grub_init_all ();
  146. grub_hostfs_init ();
  147. grub_host_init ();
  148. char *t, *inpfull, *rootfull, *res;
  149. t = grub_canonicalize_file_name (arguments.input);
  150. if (!t)
  151. {
  152. grub_util_error (_("cannot open `%s': %s"), arguments.input,
  153. strerror (errno));
  154. }
  155. inpfull = xasprintf ("(host)/%s", t);
  156. free (t);
  157. t = grub_canonicalize_file_name (arguments.root ? : "/");
  158. if (!t)
  159. {
  160. grub_util_error (_("cannot open `%s': %s"), arguments.root,
  161. strerror (errno));
  162. }
  163. rootfull = xasprintf ("(host)/%s", t);
  164. free (t);
  165. char *cwd = xstrdup (arguments.input);
  166. char *p = strrchr (cwd, '/');
  167. char *cwdfull;
  168. if (p)
  169. *p = '\0';
  170. else
  171. {
  172. free (cwd);
  173. cwd = xstrdup (".");
  174. }
  175. t = grub_canonicalize_file_name (arguments.cwd ? : cwd);
  176. if (!t)
  177. {
  178. grub_util_error (_("cannot open `%s': %s"), arguments.root,
  179. strerror (errno));
  180. }
  181. cwdfull = xasprintf ("(host)/%s", t);
  182. free (t);
  183. res = grub_syslinux_config_file (rootfull, arguments.target_root ? : "/",
  184. cwdfull, arguments.target_cwd ? : cwd,
  185. inpfull, arguments.flav);
  186. if (!res)
  187. grub_util_error ("%s", grub_errmsg);
  188. if (arguments.output)
  189. {
  190. FILE *f = grub_util_fopen (arguments.output, "wb");
  191. if (!f)
  192. grub_util_error (_("cannot open `%s': %s"), arguments.output,
  193. strerror (errno));
  194. fwrite (res, 1, strlen (res), f);
  195. fclose (f);
  196. }
  197. else
  198. printf ("%s\n", res);
  199. free (res);
  200. free (rootfull);
  201. free (inpfull);
  202. free (arguments.root);
  203. free (arguments.output);
  204. free (arguments.target_root);
  205. free (arguments.input);
  206. free (cwdfull);
  207. free (cwd);
  208. return 0;
  209. }