probe.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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."), N_("VARNAME"), ARG_TYPE_STRING},
  38. /* TRANSLATORS: It's a driver that is currently in use to access
  39. the diven disk. */
  40. {"driver", 'd', 0, N_("Determine driver."), 0, 0},
  41. {"partmap", 'p', 0, N_("Determine partition map type."), 0, 0},
  42. {"fs", 'f', 0, N_("Determine filesystem type."), 0, 0},
  43. {"fs-uuid", 'u', 0, N_("Determine filesystem UUID."), 0, 0},
  44. {"label", 'l', 0, N_("Determine filesystem label."), 0, 0},
  45. {0, 0, 0, 0, 0, 0}
  46. };
  47. static grub_err_t
  48. grub_cmd_probe (grub_extcmd_context_t ctxt, int argc, char **args)
  49. {
  50. struct grub_arg_list *state = ctxt->state;
  51. grub_device_t dev;
  52. grub_fs_t fs;
  53. char *ptr;
  54. grub_err_t err;
  55. if (argc < 1)
  56. return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
  57. ptr = args[0] + grub_strlen (args[0]) - 1;
  58. if (args[0][0] == '(' && *ptr == ')')
  59. {
  60. *ptr = 0;
  61. dev = grub_device_open (args[0] + 1);
  62. *ptr = ')';
  63. }
  64. else
  65. dev = grub_device_open (args[0]);
  66. if (! dev)
  67. return grub_errno;
  68. if (state[1].set)
  69. {
  70. const char *val = "none";
  71. if (dev->net)
  72. val = dev->net->protocol->name;
  73. if (dev->disk)
  74. val = dev->disk->dev->name;
  75. if (state[0].set)
  76. grub_env_set (state[0].arg, val);
  77. else
  78. grub_printf ("%s", val);
  79. grub_device_close (dev);
  80. return GRUB_ERR_NONE;
  81. }
  82. if (state[2].set)
  83. {
  84. const char *val = "none";
  85. if (dev->disk && dev->disk->partition)
  86. val = dev->disk->partition->partmap->name;
  87. if (state[0].set)
  88. grub_env_set (state[0].arg, val);
  89. else
  90. grub_printf ("%s", val);
  91. grub_device_close (dev);
  92. return GRUB_ERR_NONE;
  93. }
  94. fs = grub_fs_probe (dev);
  95. if (! fs)
  96. return grub_errno;
  97. if (state[3].set)
  98. {
  99. if (state[0].set)
  100. grub_env_set (state[0].arg, fs->name);
  101. else
  102. grub_printf ("%s", fs->name);
  103. grub_device_close (dev);
  104. return GRUB_ERR_NONE;
  105. }
  106. if (state[4].set)
  107. {
  108. char *uuid;
  109. if (! fs->uuid)
  110. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  111. N_("%s does not support UUIDs"), fs->name);
  112. err = fs->uuid (dev, &uuid);
  113. if (err)
  114. return err;
  115. if (! uuid)
  116. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  117. N_("%s does not support UUIDs"), fs->name);
  118. if (state[0].set)
  119. grub_env_set (state[0].arg, uuid);
  120. else
  121. grub_printf ("%s", uuid);
  122. grub_free (uuid);
  123. grub_device_close (dev);
  124. return GRUB_ERR_NONE;
  125. }
  126. if (state[5].set)
  127. {
  128. char *label;
  129. if (! fs->label)
  130. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  131. N_("filesystem `%s' does not support labels"),
  132. fs->name);
  133. err = fs->label (dev, &label);
  134. if (err)
  135. return err;
  136. if (! label)
  137. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  138. N_("filesystem `%s' does not support labels"),
  139. fs->name);
  140. if (state[0].set)
  141. grub_env_set (state[0].arg, label);
  142. else
  143. grub_printf ("%s", label);
  144. grub_free (label);
  145. grub_device_close (dev);
  146. return GRUB_ERR_NONE;
  147. }
  148. grub_device_close (dev);
  149. return grub_error (GRUB_ERR_BAD_ARGUMENT, "unrecognised target");
  150. }
  151. static grub_extcmd_t cmd;
  152. GRUB_MOD_INIT (probe)
  153. {
  154. cmd = grub_register_extcmd ("probe", grub_cmd_probe, 0, N_("DEVICE"),
  155. N_("Retrieve device info."), options);
  156. }
  157. GRUB_MOD_FINI (probe)
  158. {
  159. grub_unregister_extcmd (cmd);
  160. }