probe.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2009 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/mm.h>
  21. #include <grub/err.h>
  22. #include <grub/dl.h>
  23. #include <grub/device.h>
  24. #include <grub/disk.h>
  25. #include <grub/partition.h>
  26. #include <grub/net.h>
  27. #include <grub/fs.h>
  28. #include <grub/file.h>
  29. #include <grub/misc.h>
  30. #include <grub/env.h>
  31. #include <grub/extcmd.h>
  32. #include <grub/i18n.h>
  33. static const struct grub_arg_option options[] =
  34. {
  35. {"set", 's', GRUB_ARG_OPTION_OPTIONAL,
  36. N_("Set a variable to return value."), "VAR", ARG_TYPE_STRING},
  37. {"driver", 'd', 0, N_("Determine driver."), 0, 0},
  38. {"partmap", 'p', 0, N_("Determine partition map type."), 0, 0},
  39. {"fs", 'f', 0, N_("Determine filesystem type."), 0, 0},
  40. {"fs-uuid", 'u', 0, N_("Determine filesystem UUID."), 0, 0},
  41. {"label", 'l', 0, N_("Determine filesystem label."), 0, 0},
  42. {0, 0, 0, 0, 0, 0}
  43. };
  44. static grub_err_t
  45. grub_cmd_probe (grub_extcmd_t cmd, int argc, char **args)
  46. {
  47. struct grub_arg_list *state = cmd->state;
  48. grub_device_t dev;
  49. grub_fs_t fs;
  50. char *ptr;
  51. grub_err_t err;
  52. if (argc < 1)
  53. return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
  54. ptr = args[0] + grub_strlen (args[0]) - 1;
  55. if (args[0][0] == '(' && *ptr == ')')
  56. {
  57. *ptr = 0;
  58. dev = grub_device_open (args[0] + 1);
  59. *ptr = ')';
  60. }
  61. else
  62. dev = grub_device_open (args[0]);
  63. if (! dev)
  64. return grub_error (GRUB_ERR_BAD_DEVICE, "couldn't open device");
  65. if (state[1].set)
  66. {
  67. const char *val = "none";
  68. if (dev->net)
  69. val = dev->net->dev->name;
  70. if (dev->disk)
  71. val = dev->disk->dev->name;
  72. if (state[0].set)
  73. grub_env_set (state[0].arg, val);
  74. else
  75. grub_printf ("%s", val);
  76. return GRUB_ERR_NONE;
  77. }
  78. if (state[2].set)
  79. {
  80. const char *val = "none";
  81. if (dev->disk && dev->disk->partition)
  82. val = dev->disk->partition->partmap->name;
  83. if (state[0].set)
  84. grub_env_set (state[0].arg, val);
  85. else
  86. grub_printf ("%s", val);
  87. return GRUB_ERR_NONE;
  88. }
  89. fs = grub_fs_probe (dev);
  90. if (! fs)
  91. return grub_error (GRUB_ERR_UNKNOWN_FS, "unrecognised fs");
  92. if (state[3].set)
  93. {
  94. if (state[0].set)
  95. grub_env_set (state[0].arg, fs->name);
  96. else
  97. grub_printf ("%s", fs->name);
  98. return GRUB_ERR_NONE;
  99. }
  100. if (state[4].set)
  101. {
  102. char *uuid;
  103. if (! fs->uuid)
  104. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  105. "uuid for this FS isn't supported yet");
  106. err = fs->uuid (dev, &uuid);
  107. if (err)
  108. return err;
  109. if (! uuid)
  110. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  111. "uuid for this FS isn't supported yet");
  112. if (state[0].set)
  113. grub_env_set (state[0].arg, uuid);
  114. else
  115. grub_printf ("%s", uuid);
  116. grub_free (uuid);
  117. return GRUB_ERR_NONE;
  118. }
  119. if (state[5].set)
  120. {
  121. char *label;
  122. if (! fs->label)
  123. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  124. "label for this FS isn't supported yet");
  125. err = fs->label (dev, &label);
  126. if (err)
  127. return err;
  128. if (! label)
  129. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  130. "uuid for this FS isn't supported yet");
  131. if (state[0].set)
  132. grub_env_set (state[0].arg, label);
  133. else
  134. grub_printf ("%s", label);
  135. grub_free (label);
  136. return GRUB_ERR_NONE;
  137. }
  138. return grub_error (GRUB_ERR_BAD_ARGUMENT, "unrecognised target");
  139. }
  140. static grub_extcmd_t cmd;
  141. GRUB_MOD_INIT (probe)
  142. {
  143. cmd = grub_register_extcmd ("probe", grub_cmd_probe, GRUB_COMMAND_FLAG_BOTH,
  144. N_("[DEVICE]"),
  145. N_("Retrieve device info."), options);
  146. }
  147. GRUB_MOD_FINI (probe)
  148. {
  149. grub_unregister_extcmd (cmd);
  150. }