probe.c 4.2 KB

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