probe.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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/gpt_partition.h>
  27. #include <grub/net.h>
  28. #include <grub/fs.h>
  29. #include <grub/file.h>
  30. #include <grub/misc.h>
  31. #include <grub/env.h>
  32. #include <grub/extcmd.h>
  33. #include <grub/i18n.h>
  34. #include <grub/i386/pc/boot.h>
  35. GRUB_MOD_LICENSE ("GPLv3+");
  36. static const struct grub_arg_option options[] =
  37. {
  38. {"set", 's', 0,
  39. N_("Set a variable to return value."), N_("VARNAME"), ARG_TYPE_STRING},
  40. /* TRANSLATORS: It's a driver that is currently in use to access
  41. the diven disk. */
  42. {"driver", 'd', 0, N_("Determine driver."), 0, 0},
  43. {"partmap", 'p', 0, N_("Determine partition map type."), 0, 0},
  44. {"fs", 'f', 0, N_("Determine filesystem type."), 0, 0},
  45. {"fs-uuid", 'u', 0, N_("Determine filesystem UUID."), 0, 0},
  46. {"label", 'l', 0, N_("Determine filesystem label."), 0, 0},
  47. {"part-uuid", 0, 0, N_("Determine partition UUID."), 0, 0},
  48. {0, 0, 0, 0, 0, 0}
  49. };
  50. static grub_err_t
  51. grub_cmd_probe (grub_extcmd_context_t ctxt, int argc, char **args)
  52. {
  53. struct grub_arg_list *state = ctxt->state;
  54. grub_device_t dev;
  55. grub_fs_t fs;
  56. char *ptr;
  57. grub_err_t err;
  58. if (argc < 1)
  59. return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
  60. ptr = args[0] + grub_strlen (args[0]) - 1;
  61. if (args[0][0] == '(' && *ptr == ')')
  62. {
  63. *ptr = 0;
  64. dev = grub_device_open (args[0] + 1);
  65. *ptr = ')';
  66. }
  67. else
  68. dev = grub_device_open (args[0]);
  69. if (! dev)
  70. return grub_errno;
  71. if (state[1].set)
  72. {
  73. const char *val = "none";
  74. if (dev->net)
  75. val = dev->net->protocol->name;
  76. if (dev->disk)
  77. val = dev->disk->dev->name;
  78. if (state[0].set)
  79. grub_env_set (state[0].arg, val);
  80. else
  81. grub_printf ("%s", val);
  82. grub_device_close (dev);
  83. return GRUB_ERR_NONE;
  84. }
  85. if (state[2].set)
  86. {
  87. const char *val = "none";
  88. if (dev->disk && dev->disk->partition)
  89. val = dev->disk->partition->partmap->name;
  90. if (state[0].set)
  91. grub_env_set (state[0].arg, val);
  92. else
  93. grub_printf ("%s", val);
  94. grub_device_close (dev);
  95. return GRUB_ERR_NONE;
  96. }
  97. if (state[6].set)
  98. {
  99. /* AAAABBBB-CCCC-DDDD-EEEE-FFFFFFFFFFFF + null terminator */
  100. char val[37] = "none";
  101. if (dev->disk && dev->disk->partition)
  102. {
  103. struct grub_partition *p = dev->disk->partition;
  104. grub_disk_t disk = grub_disk_open(dev->disk->name);
  105. if (!disk)
  106. {
  107. grub_device_close (dev);
  108. return grub_errno;
  109. }
  110. if (grub_strcmp(dev->disk->partition->partmap->name, "gpt") == 0)
  111. {
  112. struct grub_gpt_partentry entry;
  113. grub_guid_t *guid;
  114. if (grub_disk_read(disk, p->offset, p->index, sizeof(entry), &entry))
  115. {
  116. grub_error_push ();
  117. grub_disk_close (disk);
  118. grub_device_close (dev);
  119. grub_error_pop ();
  120. return grub_errno;
  121. }
  122. guid = &entry.guid;
  123. guid->data1 = grub_le_to_cpu32 (guid->data1);
  124. guid->data2 = grub_le_to_cpu16 (guid->data2);
  125. guid->data3 = grub_le_to_cpu16 (guid->data3);
  126. grub_snprintf (val, sizeof(val), "%pG", guid);
  127. }
  128. else if (grub_strcmp(dev->disk->partition->partmap->name, "msdos") == 0)
  129. {
  130. grub_uint32_t nt_disk_sig;
  131. if (grub_disk_read(disk, 0, GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
  132. sizeof(nt_disk_sig), &nt_disk_sig) == 0)
  133. grub_snprintf (val, sizeof(val), "%08x-%02x",
  134. grub_le_to_cpu32(nt_disk_sig), 1 + p->number);
  135. }
  136. grub_disk_close(disk);
  137. }
  138. if (state[0].set)
  139. grub_env_set (state[0].arg, val);
  140. else
  141. grub_printf ("%s", val);
  142. grub_device_close (dev);
  143. return GRUB_ERR_NONE;
  144. }
  145. fs = grub_fs_probe (dev);
  146. if (! fs)
  147. {
  148. grub_error_push ();
  149. grub_device_close (dev);
  150. grub_error_pop ();
  151. return grub_errno;
  152. }
  153. if (state[3].set)
  154. {
  155. if (state[0].set)
  156. grub_env_set (state[0].arg, fs->name);
  157. else
  158. grub_printf ("%s", fs->name);
  159. grub_device_close (dev);
  160. return GRUB_ERR_NONE;
  161. }
  162. if (state[4].set)
  163. {
  164. char *uuid;
  165. if (! fs->fs_uuid)
  166. {
  167. grub_device_close (dev);
  168. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  169. N_("%s does not support UUIDs"), fs->name);
  170. }
  171. err = fs->fs_uuid (dev, &uuid);
  172. if (err)
  173. {
  174. grub_device_close (dev);
  175. return err;
  176. }
  177. if (! uuid)
  178. {
  179. grub_device_close (dev);
  180. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  181. N_("%s does not support UUIDs"), fs->name);
  182. }
  183. if (state[0].set)
  184. grub_env_set (state[0].arg, uuid);
  185. else
  186. grub_printf ("%s", uuid);
  187. grub_free (uuid);
  188. grub_device_close (dev);
  189. return GRUB_ERR_NONE;
  190. }
  191. if (state[5].set)
  192. {
  193. char *label;
  194. if (! fs->fs_label)
  195. {
  196. grub_device_close (dev);
  197. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  198. N_("filesystem `%s' does not support labels"),
  199. fs->name);
  200. }
  201. err = fs->fs_label (dev, &label);
  202. if (err)
  203. {
  204. grub_device_close (dev);
  205. return err;
  206. }
  207. if (! label)
  208. {
  209. grub_device_close (dev);
  210. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  211. N_("filesystem `%s' does not support labels"),
  212. fs->name);
  213. }
  214. if (state[0].set)
  215. grub_env_set (state[0].arg, label);
  216. else
  217. grub_printf ("%s", label);
  218. grub_free (label);
  219. grub_device_close (dev);
  220. return GRUB_ERR_NONE;
  221. }
  222. grub_device_close (dev);
  223. return grub_error (GRUB_ERR_BAD_ARGUMENT, "unrecognised target");
  224. }
  225. static grub_extcmd_t cmd;
  226. GRUB_MOD_INIT (probe)
  227. {
  228. cmd = grub_register_extcmd ("probe", grub_cmd_probe, 0, N_("DEVICE"),
  229. N_("Retrieve device info."), options);
  230. }
  231. GRUB_MOD_FINI (probe)
  232. {
  233. grub_unregister_extcmd (cmd);
  234. }