pxecfg.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * BURG - Brand-new Universal loadeR from GRUB
  3. * Copyright 2009 Bean Lee - All Rights Reserved
  4. *
  5. * BURG 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. * BURG 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 BURG. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/dl.h>
  19. #include <grub/mm.h>
  20. #include <grub/env.h>
  21. #include <grub/misc.h>
  22. #include <grub/command.h>
  23. #include <grub/uitree.h>
  24. #include <grub/parser.h>
  25. #include <grub/machine/pxe.h>
  26. static int
  27. match_string (grub_uitree_t node, const char *str)
  28. {
  29. grub_uitree_t child;
  30. char *cmd;
  31. child = node->child;
  32. while (child)
  33. {
  34. int len;
  35. len = grub_strlen (child->name);
  36. if (! grub_memcmp (child->name, str, len))
  37. {
  38. const char *p;
  39. p = str + len;
  40. if ((*p == '.') || (*p == '-'))
  41. p++;
  42. if (match_string (child, p))
  43. return 1;
  44. }
  45. child = child->next;
  46. }
  47. cmd = grub_uitree_get_prop (node, "command");
  48. if (cmd)
  49. {
  50. grub_parser_execute (cmd);
  51. return 1;
  52. }
  53. return 0;
  54. }
  55. static void
  56. pxe_find (grub_uitree_t node)
  57. {
  58. const char *mac, *ip;
  59. mac = grub_env_get ("net_pxe_mac");
  60. ip = grub_env_get ("net_pxe_ip");
  61. node = node->child;
  62. while (node)
  63. {
  64. if ((! grub_strcmp (node->name, "mac")) && (mac))
  65. match_string (node, mac);
  66. else if ((! grub_strcmp (node->name, "ip")) && (ip))
  67. match_string (node, ip);
  68. node = node->next;
  69. }
  70. }
  71. static grub_err_t
  72. grub_cmd_pxecfg (grub_command_t cmd __attribute__ ((unused)),
  73. int argc, char **args)
  74. {
  75. struct grub_uitree root;
  76. if (! grub_pxe_pxenv)
  77. return grub_error (GRUB_ERR_FILE_NOT_FOUND, "no pxe environment");
  78. grub_memset (&root, 0, sizeof (root));
  79. while (argc)
  80. {
  81. grub_uitree_load_file (&root, *args, GRUB_UITREE_LOAD_METHOD_MERGE);
  82. argc--;
  83. args++;
  84. }
  85. pxe_find (&root);
  86. return grub_errno;
  87. }
  88. static grub_command_t cmd;
  89. GRUB_MOD_INIT(pxecfg)
  90. {
  91. cmd = grub_register_command ("pxecfg", grub_cmd_pxecfg,
  92. "pxecfg FILES", "Load PXE config file");
  93. }
  94. GRUB_MOD_FINI(pxecfg)
  95. {
  96. grub_unregister_command (cmd);
  97. }