syslinuxcfg.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 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 <grub/types.h>
  19. #include <grub/misc.h>
  20. #include <grub/extcmd.h>
  21. #include <grub/mm.h>
  22. #include <grub/err.h>
  23. #include <grub/dl.h>
  24. #include <grub/file.h>
  25. #include <grub/normal.h>
  26. #include <grub/script_sh.h>
  27. #include <grub/i18n.h>
  28. #include <grub/term.h>
  29. #include <grub/syslinux_parse.h>
  30. #include <grub/crypto.h>
  31. #include <grub/auth.h>
  32. #include <grub/disk.h>
  33. #include <grub/partition.h>
  34. GRUB_MOD_LICENSE ("GPLv3+");
  35. /* Helper for syslinux_file. */
  36. static grub_err_t
  37. syslinux_file_getline (char **line, int cont __attribute__ ((unused)),
  38. void *data __attribute__ ((unused)))
  39. {
  40. *line = 0;
  41. return GRUB_ERR_NONE;
  42. }
  43. static const struct grub_arg_option options[] =
  44. {
  45. {"root", 'r', 0,
  46. N_("root directory of the syslinux disk [default=/]."),
  47. N_("DIR"), ARG_TYPE_STRING},
  48. {"cwd", 'c', 0,
  49. N_("current directory of syslinux [default is parent directory of input file]."),
  50. N_("DIR"), ARG_TYPE_STRING},
  51. {"isolinux", 'i', 0, N_("assume input is an isolinux configuration file."), 0, 0},
  52. {"pxelinux", 'p', 0, N_("assume input is a pxelinux configuration file."), 0, 0},
  53. {"syslinux", 's', 0, N_("assume input is a syslinux configuration file."), 0, 0},
  54. {0, 0, 0, 0, 0, 0}
  55. };
  56. enum
  57. {
  58. OPTION_ROOT,
  59. OPTION_CWD,
  60. OPTION_ISOLINUX,
  61. OPTION_PXELINUX,
  62. OPTION_SYSLINUX
  63. };
  64. static grub_err_t
  65. syslinux_file (grub_extcmd_context_t ctxt, const char *filename)
  66. {
  67. char *result;
  68. const char *root = ctxt->state[OPTION_ROOT].set ? ctxt->state[OPTION_ROOT].arg : "/";
  69. const char *cwd = ctxt->state[OPTION_CWD].set ? ctxt->state[OPTION_CWD].arg : NULL;
  70. grub_syslinux_flavour_t flav = GRUB_SYSLINUX_UNKNOWN;
  71. char *cwdf = NULL;
  72. grub_menu_t menu;
  73. if (ctxt->state[OPTION_ISOLINUX].set)
  74. flav = GRUB_SYSLINUX_ISOLINUX;
  75. if (ctxt->state[OPTION_PXELINUX].set)
  76. flav = GRUB_SYSLINUX_PXELINUX;
  77. if (ctxt->state[OPTION_SYSLINUX].set)
  78. flav = GRUB_SYSLINUX_SYSLINUX;
  79. if (!cwd)
  80. {
  81. char *p;
  82. cwdf = grub_strdup (filename);
  83. if (!cwdf)
  84. return grub_errno;
  85. p = grub_strrchr (cwdf, '/');
  86. if (!p)
  87. {
  88. grub_free (cwdf);
  89. cwd = "/";
  90. cwdf = NULL;
  91. }
  92. else
  93. {
  94. *p = '\0';
  95. cwd = cwdf;
  96. }
  97. }
  98. grub_dprintf ("syslinux",
  99. "transforming syslinux config %s, root = %s, cwd = %s\n",
  100. filename, root, cwd);
  101. result = grub_syslinux_config_file (root, root, cwd, cwd, filename, flav);
  102. if (!result)
  103. return grub_errno;
  104. grub_dprintf ("syslinux", "syslinux config transformed\n");
  105. menu = grub_env_get_menu ();
  106. if (! menu)
  107. {
  108. menu = grub_zalloc (sizeof (*menu));
  109. if (! menu)
  110. {
  111. grub_free (result);
  112. return grub_errno;
  113. }
  114. grub_env_set_menu (menu);
  115. }
  116. grub_normal_parse_line (result, syslinux_file_getline, NULL);
  117. grub_print_error ();
  118. grub_free (result);
  119. grub_free (cwdf);
  120. return GRUB_ERR_NONE;
  121. }
  122. static grub_err_t
  123. grub_cmd_syslinux_source (grub_extcmd_context_t ctxt,
  124. int argc, char **args)
  125. {
  126. int new_env, extractor;
  127. grub_err_t ret;
  128. if (argc != 1)
  129. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  130. extractor = (ctxt->extcmd->cmd->name[0] == 'e');
  131. new_env = (ctxt->extcmd->cmd->name[extractor ? (sizeof ("extract_syslinux_entries_") - 1)
  132. : (sizeof ("syslinux_") - 1)] == 'c');
  133. if (new_env)
  134. grub_cls ();
  135. if (new_env && !extractor)
  136. grub_env_context_open ();
  137. if (extractor)
  138. grub_env_extractor_open (!new_env);
  139. ret = syslinux_file (ctxt, args[0]);
  140. if (new_env)
  141. {
  142. grub_menu_t menu;
  143. menu = grub_env_get_menu ();
  144. if (menu && menu->size)
  145. grub_show_menu (menu, 1, 0);
  146. if (!extractor)
  147. grub_env_context_close ();
  148. }
  149. if (extractor)
  150. grub_env_extractor_close (!new_env);
  151. return ret;
  152. }
  153. static grub_extcmd_t cmd_source, cmd_configfile;
  154. static grub_extcmd_t cmd_source_extract, cmd_configfile_extract;
  155. GRUB_MOD_INIT(syslinuxcfg)
  156. {
  157. cmd_source
  158. = grub_register_extcmd ("syslinux_source",
  159. grub_cmd_syslinux_source, 0,
  160. N_("FILE"),
  161. /* TRANSLATORS: "syslinux config" means
  162. "config as used by syslinux". */
  163. N_("Execute syslinux config in same context"),
  164. options);
  165. cmd_configfile
  166. = grub_register_extcmd ("syslinux_configfile",
  167. grub_cmd_syslinux_source, 0,
  168. N_("FILE"),
  169. N_("Execute syslinux config in new context"),
  170. options);
  171. cmd_source_extract
  172. = grub_register_extcmd ("extract_syslinux_entries_source",
  173. grub_cmd_syslinux_source, 0,
  174. N_("FILE"),
  175. N_("Execute syslinux config in same context taking only menu entries"),
  176. options);
  177. cmd_configfile_extract
  178. = grub_register_extcmd ("extract_syslinux_entries_configfile",
  179. grub_cmd_syslinux_source, 0,
  180. N_("FILE"),
  181. N_("Execute syslinux config in new context taking only menu entries"),
  182. options);
  183. }
  184. GRUB_MOD_FINI(syslinuxcfg)
  185. {
  186. grub_unregister_extcmd (cmd_source);
  187. grub_unregister_extcmd (cmd_configfile);
  188. grub_unregister_extcmd (cmd_source_extract);
  189. grub_unregister_extcmd (cmd_configfile_extract);
  190. }