probe.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_gpt_part_guid_t *guid;
  114. if (grub_disk_read(disk, p->offset, p->index, sizeof(entry), &entry))
  115. return grub_errno;
  116. guid = &entry.guid;
  117. grub_snprintf (val, sizeof(val),
  118. "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
  119. grub_le_to_cpu32 (guid->data1),
  120. grub_le_to_cpu16 (guid->data2),
  121. grub_le_to_cpu16 (guid->data3),
  122. guid->data4[0], guid->data4[1], guid->data4[2],
  123. guid->data4[3], guid->data4[4], guid->data4[5],
  124. guid->data4[6], guid->data4[7]);
  125. }
  126. else if (grub_strcmp(dev->disk->partition->partmap->name, "msdos") == 0)
  127. {
  128. grub_uint32_t nt_disk_sig;
  129. if (grub_disk_read(disk, 0, GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
  130. sizeof(nt_disk_sig), &nt_disk_sig) == 0)
  131. grub_snprintf (val, sizeof(val), "%08x-%02x",
  132. grub_le_to_cpu32(nt_disk_sig), 1 + p->number);
  133. }
  134. grub_disk_close(disk);
  135. }
  136. if (state[0].set)
  137. grub_env_set (state[0].arg, val);
  138. else
  139. grub_printf ("%s", val);
  140. grub_device_close (dev);
  141. return GRUB_ERR_NONE;
  142. }
  143. fs = grub_fs_probe (dev);
  144. if (! fs)
  145. return grub_errno;
  146. if (state[3].set)
  147. {
  148. if (state[0].set)
  149. grub_env_set (state[0].arg, fs->name);
  150. else
  151. grub_printf ("%s", fs->name);
  152. grub_device_close (dev);
  153. return GRUB_ERR_NONE;
  154. }
  155. if (state[4].set)
  156. {
  157. char *uuid;
  158. if (! fs->fs_uuid)
  159. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  160. N_("%s does not support UUIDs"), fs->name);
  161. err = fs->fs_uuid (dev, &uuid);
  162. if (err)
  163. return err;
  164. if (! uuid)
  165. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  166. N_("%s does not support UUIDs"), fs->name);
  167. if (state[0].set)
  168. grub_env_set (state[0].arg, uuid);
  169. else
  170. grub_printf ("%s", uuid);
  171. grub_free (uuid);
  172. grub_device_close (dev);
  173. return GRUB_ERR_NONE;
  174. }
  175. if (state[5].set)
  176. {
  177. char *label;
  178. if (! fs->fs_label)
  179. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  180. N_("filesystem `%s' does not support labels"),
  181. fs->name);
  182. err = fs->fs_label (dev, &label);
  183. if (err)
  184. return err;
  185. if (! label)
  186. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  187. N_("filesystem `%s' does not support labels"),
  188. fs->name);
  189. if (state[0].set)
  190. grub_env_set (state[0].arg, label);
  191. else
  192. grub_printf ("%s", label);
  193. grub_free (label);
  194. grub_device_close (dev);
  195. return GRUB_ERR_NONE;
  196. }
  197. grub_device_close (dev);
  198. return grub_error (GRUB_ERR_BAD_ARGUMENT, "unrecognised target");
  199. }
  200. static grub_extcmd_t cmd;
  201. GRUB_MOD_INIT (probe)
  202. {
  203. cmd = grub_register_extcmd ("probe", grub_cmd_probe, 0, N_("DEVICE"),
  204. N_("Retrieve device info."), options);
  205. }
  206. GRUB_MOD_FINI (probe)
  207. {
  208. grub_unregister_extcmd (cmd);
  209. }