grub-probe.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. /* grub-probe.c - probe device information for a given path */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <grub/types.h>
  21. #include <grub/emu/misc.h>
  22. #include <grub/util/misc.h>
  23. #include <grub/device.h>
  24. #include <grub/disk.h>
  25. #include <grub/file.h>
  26. #include <grub/fs.h>
  27. #include <grub/partition.h>
  28. #include <grub/msdos_partition.h>
  29. #include <grub/gpt_partition.h>
  30. #include <grub/i386/pc/boot.h>
  31. #include <grub/emu/hostdisk.h>
  32. #include <grub/emu/getroot.h>
  33. #include <grub/term.h>
  34. #include <grub/env.h>
  35. #include <grub/diskfilter.h>
  36. #include <grub/i18n.h>
  37. #include <grub/emu/misc.h>
  38. #include <grub/util/ofpath.h>
  39. #include <grub/crypto.h>
  40. #include <grub/cryptodisk.h>
  41. #include <stdio.h>
  42. #include <unistd.h>
  43. #include <string.h>
  44. #include <stdlib.h>
  45. #include <assert.h>
  46. #define _GNU_SOURCE 1
  47. #pragma GCC diagnostic ignored "-Wmissing-prototypes"
  48. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  49. #include <argp.h>
  50. #pragma GCC diagnostic error "-Wmissing-prototypes"
  51. #pragma GCC diagnostic error "-Wmissing-declarations"
  52. #include "progname.h"
  53. enum {
  54. PRINT_FS,
  55. PRINT_FS_UUID,
  56. PRINT_FS_LABEL,
  57. PRINT_DRIVE,
  58. PRINT_DEVICE,
  59. PRINT_PARTMAP,
  60. PRINT_PARTUUID,
  61. PRINT_ABSTRACTION,
  62. PRINT_CRYPTODISK_UUID,
  63. PRINT_HINT_STR,
  64. PRINT_BIOS_HINT,
  65. PRINT_IEEE1275_HINT,
  66. PRINT_BAREMETAL_HINT,
  67. PRINT_EFI_HINT,
  68. PRINT_ARC_HINT,
  69. PRINT_COMPATIBILITY_HINT,
  70. PRINT_MSDOS_PARTTYPE,
  71. PRINT_GPT_PARTTYPE,
  72. PRINT_ZERO_CHECK,
  73. PRINT_DISK
  74. };
  75. static const char *targets[] =
  76. {
  77. [PRINT_FS] = "fs",
  78. [PRINT_FS_UUID] = "fs_uuid",
  79. [PRINT_FS_LABEL] = "fs_label",
  80. [PRINT_DRIVE] = "drive",
  81. [PRINT_DEVICE] = "device",
  82. [PRINT_PARTMAP] = "partmap",
  83. [PRINT_PARTUUID] = "partuuid",
  84. [PRINT_ABSTRACTION] = "abstraction",
  85. [PRINT_CRYPTODISK_UUID] = "cryptodisk_uuid",
  86. [PRINT_HINT_STR] = "hints_string",
  87. [PRINT_BIOS_HINT] = "bios_hints",
  88. [PRINT_IEEE1275_HINT] = "ieee1275_hints",
  89. [PRINT_BAREMETAL_HINT] = "baremetal_hints",
  90. [PRINT_EFI_HINT] = "efi_hints",
  91. [PRINT_ARC_HINT] = "arc_hints",
  92. [PRINT_COMPATIBILITY_HINT] = "compatibility_hint",
  93. [PRINT_MSDOS_PARTTYPE] = "msdos_parttype",
  94. [PRINT_GPT_PARTTYPE] = "gpt_parttype",
  95. [PRINT_ZERO_CHECK] = "zero_check",
  96. [PRINT_DISK] = "disk",
  97. };
  98. static int print = PRINT_FS;
  99. static unsigned int argument_is_device = 0;
  100. static char *
  101. get_targets_string (void)
  102. {
  103. char **arr = xmalloc (sizeof (targets));
  104. int len = 0;
  105. char *str;
  106. char *ptr;
  107. unsigned i;
  108. memcpy (arr, targets, sizeof (targets));
  109. qsort (arr, ARRAY_SIZE (targets), sizeof (char *), grub_qsort_strcmp);
  110. for (i = 0; i < ARRAY_SIZE (targets); i++)
  111. len += grub_strlen (targets[i]) + 2;
  112. ptr = str = xmalloc (len);
  113. for (i = 0; i < ARRAY_SIZE (targets); i++)
  114. {
  115. ptr = grub_stpcpy (ptr, arr[i]);
  116. *ptr++ = ',';
  117. *ptr++ = ' ';
  118. }
  119. ptr[-2] = '\0';
  120. free (arr);
  121. return str;
  122. }
  123. static int
  124. print_gpt_guid (grub_guid_t guid)
  125. {
  126. guid.data1 = grub_le_to_cpu32 (guid.data1);
  127. guid.data2 = grub_le_to_cpu16 (guid.data2);
  128. guid.data3 = grub_le_to_cpu16 (guid.data3);
  129. return grub_printf ("%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
  130. guid.data1, guid.data2, guid.data3, guid.data4[0],
  131. guid.data4[1], guid.data4[2], guid.data4[3],
  132. guid.data4[4], guid.data4[5], guid.data4[6],
  133. guid.data4[7]);
  134. }
  135. static void
  136. do_print (const char *x, void *data)
  137. {
  138. char delim = *(const char *) data;
  139. grub_printf ("%s%c", x, delim);
  140. }
  141. static void
  142. probe_partmap (grub_disk_t disk, char delim)
  143. {
  144. grub_partition_t part;
  145. grub_disk_memberlist_t list = NULL, tmp;
  146. if (disk->partition == NULL)
  147. {
  148. grub_util_info ("no partition map found for %s", disk->name);
  149. }
  150. for (part = disk->partition; part; part = part->parent)
  151. printf ("%s%c", part->partmap->name, delim);
  152. if (disk->dev->id == GRUB_DISK_DEVICE_DISKFILTER_ID)
  153. grub_diskfilter_get_partmap (disk, do_print, &delim);
  154. /* In case of LVM/RAID, check the member devices as well. */
  155. if (disk->dev->disk_memberlist)
  156. {
  157. list = disk->dev->disk_memberlist (disk);
  158. }
  159. while (list)
  160. {
  161. probe_partmap (list->disk, delim);
  162. tmp = list->next;
  163. free (list);
  164. list = tmp;
  165. }
  166. }
  167. static void
  168. probe_partuuid (grub_disk_t disk, char delim)
  169. {
  170. grub_partition_t p = disk->partition;
  171. /*
  172. * Nested partitions not supported for now.
  173. * Non-nested partitions must have disk->partition->parent == NULL
  174. */
  175. if (p && p->parent == NULL)
  176. {
  177. disk->partition = p->parent;
  178. if (strcmp(p->partmap->name, "msdos") == 0)
  179. {
  180. /*
  181. * The partition GUID for MSDOS is the partition number (starting
  182. * with 1) prepended with the NT disk signature.
  183. */
  184. grub_uint32_t nt_disk_sig;
  185. if (grub_disk_read (disk, 0, GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
  186. sizeof(nt_disk_sig), &nt_disk_sig) == 0)
  187. grub_printf ("%08x-%02x",
  188. grub_le_to_cpu32(nt_disk_sig), 1 + p->number);
  189. }
  190. else if (strcmp(p->partmap->name, "gpt") == 0)
  191. {
  192. struct grub_gpt_partentry gptdata;
  193. if (grub_disk_read (disk, p->offset, p->index,
  194. sizeof(gptdata), &gptdata) == 0)
  195. print_gpt_guid(gptdata.guid);
  196. }
  197. disk->partition = p;
  198. }
  199. }
  200. static void
  201. probe_cryptodisk_uuid (grub_disk_t disk, char delim)
  202. {
  203. grub_disk_memberlist_t list = NULL, tmp;
  204. /* In case of LVM/RAID, check the member devices as well. */
  205. if (disk->dev->disk_memberlist)
  206. {
  207. list = disk->dev->disk_memberlist (disk);
  208. }
  209. while (list)
  210. {
  211. probe_cryptodisk_uuid (list->disk, delim);
  212. tmp = list->next;
  213. free (list);
  214. list = tmp;
  215. }
  216. if (disk->dev->id == GRUB_DISK_DEVICE_CRYPTODISK_ID)
  217. {
  218. const char *uu = grub_util_cryptodisk_get_uuid (disk);
  219. grub_printf ("%s%c", uu, delim);
  220. }
  221. }
  222. static int
  223. probe_raid_level (grub_disk_t disk)
  224. {
  225. /* disk might be NULL in the case of a LVM physical volume with no LVM
  226. signature. Ignore such cases here. */
  227. if (!disk)
  228. return -1;
  229. if (disk->dev->id != GRUB_DISK_DEVICE_DISKFILTER_ID)
  230. return -1;
  231. if (disk->name[0] != 'm' || disk->name[1] != 'd')
  232. return -1;
  233. if (!((struct grub_diskfilter_lv *) disk->data)->segments)
  234. return -1;
  235. return ((struct grub_diskfilter_lv *) disk->data)->segments->type;
  236. }
  237. static void
  238. probe_abstraction (grub_disk_t disk, char delim)
  239. {
  240. grub_disk_memberlist_t list = NULL, tmp;
  241. int raid_level;
  242. if (disk->dev->disk_memberlist)
  243. list = disk->dev->disk_memberlist (disk);
  244. while (list)
  245. {
  246. probe_abstraction (list->disk, delim);
  247. tmp = list->next;
  248. free (list);
  249. list = tmp;
  250. }
  251. if (disk->dev->id == GRUB_DISK_DEVICE_DISKFILTER_ID
  252. && (grub_memcmp (disk->name, "lvm/", sizeof ("lvm/") - 1) == 0 ||
  253. grub_memcmp (disk->name, "lvmid/", sizeof ("lvmid/") - 1) == 0))
  254. printf ("lvm%c", delim);
  255. if (disk->dev->id == GRUB_DISK_DEVICE_DISKFILTER_ID
  256. && grub_memcmp (disk->name, "ldm/", sizeof ("ldm/") - 1) == 0)
  257. printf ("ldm%c", delim);
  258. if (disk->dev->id == GRUB_DISK_DEVICE_CRYPTODISK_ID)
  259. grub_util_cryptodisk_get_abstraction (disk, do_print, &delim);
  260. raid_level = probe_raid_level (disk);
  261. if (raid_level >= 0)
  262. {
  263. printf ("diskfilter%c", delim);
  264. if (disk->dev->disk_raidname)
  265. printf ("%s%c", disk->dev->disk_raidname (disk), delim);
  266. }
  267. if (raid_level == 5)
  268. printf ("raid5rec%c", delim);
  269. if (raid_level == 6)
  270. printf ("raid6rec%c", delim);
  271. }
  272. static void
  273. probe (const char *path, char **device_names, char delim)
  274. {
  275. char **drives_names = NULL;
  276. char **curdev, **curdrive;
  277. char *grub_path = NULL;
  278. int ndev = 0;
  279. if (path != NULL)
  280. {
  281. grub_path = grub_canonicalize_file_name (path);
  282. if (! grub_path)
  283. grub_util_error (_("failed to get canonical path of `%s'"), path);
  284. device_names = grub_guess_root_devices (grub_path);
  285. free (grub_path);
  286. }
  287. if (! device_names)
  288. grub_util_error (_("cannot find a device for %s (is /dev mounted?)"), path);
  289. if (print == PRINT_DEVICE)
  290. {
  291. for (curdev = device_names; *curdev; curdev++)
  292. {
  293. printf ("%s", *curdev);
  294. putchar (delim);
  295. }
  296. goto free_device_names;
  297. }
  298. if (print == PRINT_DISK)
  299. {
  300. for (curdev = device_names; *curdev; curdev++)
  301. {
  302. char *disk;
  303. disk = grub_util_get_os_disk (*curdev);
  304. if (!disk)
  305. {
  306. grub_print_error ();
  307. continue;
  308. }
  309. printf ("%s", disk);
  310. putchar (delim);
  311. free (disk);
  312. }
  313. goto free_device_names;
  314. }
  315. for (curdev = device_names; *curdev; curdev++)
  316. {
  317. grub_util_pull_device (*curdev);
  318. ndev++;
  319. }
  320. drives_names = xcalloc (ndev + 1, sizeof (drives_names[0]));
  321. for (curdev = device_names, curdrive = drives_names; *curdev; curdev++,
  322. curdrive++)
  323. {
  324. *curdrive = grub_util_get_grub_dev (*curdev);
  325. if (! *curdrive)
  326. grub_util_error (_("cannot find a GRUB drive for %s. Check your device.map"),
  327. *curdev);
  328. }
  329. *curdrive = 0;
  330. if (print == PRINT_DRIVE)
  331. {
  332. for (curdrive = drives_names; *curdrive; curdrive++)
  333. {
  334. printf ("(%s)", *curdrive);
  335. putchar (delim);
  336. }
  337. goto end;
  338. }
  339. if (print == PRINT_ZERO_CHECK)
  340. {
  341. for (curdev = drives_names; *curdev; curdev++)
  342. {
  343. grub_device_t dev = NULL;
  344. grub_uint32_t buffer[32768];
  345. grub_disk_addr_t addr;
  346. grub_disk_addr_t dsize;
  347. grub_util_info ("opening %s", *curdev);
  348. dev = grub_device_open (*curdev);
  349. if (! dev || !dev->disk)
  350. grub_util_error ("%s", grub_errmsg);
  351. dsize = grub_disk_native_sectors (dev->disk);
  352. for (addr = 0; addr < dsize;
  353. addr += sizeof (buffer) / GRUB_DISK_SECTOR_SIZE)
  354. {
  355. grub_size_t sz = sizeof (buffer);
  356. grub_uint32_t *ptr;
  357. if (sizeof (buffer) / GRUB_DISK_SECTOR_SIZE > dsize - addr)
  358. sz = (dsize - addr) * GRUB_DISK_SECTOR_SIZE;
  359. grub_disk_read (dev->disk, addr, 0, sz, buffer);
  360. for (ptr = buffer; ptr < buffer + sz / sizeof (*buffer); ptr++)
  361. if (*ptr)
  362. {
  363. grub_printf ("false\n");
  364. grub_device_close (dev);
  365. goto end;
  366. }
  367. }
  368. grub_device_close (dev);
  369. }
  370. grub_printf ("true\n");
  371. }
  372. if (print == PRINT_FS || print == PRINT_FS_UUID
  373. || print == PRINT_FS_LABEL)
  374. {
  375. grub_device_t dev = NULL;
  376. grub_fs_t fs;
  377. grub_util_info ("opening %s", drives_names[0]);
  378. dev = grub_device_open (drives_names[0]);
  379. if (! dev)
  380. grub_util_error ("%s", grub_errmsg);
  381. fs = grub_fs_probe (dev);
  382. if (! fs)
  383. grub_util_error ("%s", grub_errmsg);
  384. if (print == PRINT_FS)
  385. {
  386. printf ("%s", fs->name);
  387. putchar (delim);
  388. }
  389. else if (print == PRINT_FS_UUID)
  390. {
  391. char *uuid;
  392. if (! fs->fs_uuid)
  393. grub_util_error (_("%s does not support UUIDs"), fs->name);
  394. if (fs->fs_uuid (dev, &uuid) != GRUB_ERR_NONE)
  395. grub_util_error ("%s", grub_errmsg);
  396. printf ("%s", uuid);
  397. putchar (delim);
  398. }
  399. else if (print == PRINT_FS_LABEL)
  400. {
  401. char *label;
  402. if (! fs->fs_label)
  403. grub_util_error (_("filesystem `%s' does not support labels"),
  404. fs->name);
  405. if (fs->fs_label (dev, &label) != GRUB_ERR_NONE)
  406. grub_util_error ("%s", grub_errmsg);
  407. printf ("%s", label);
  408. putchar (delim);
  409. }
  410. grub_device_close (dev);
  411. goto end;
  412. }
  413. for (curdrive = drives_names, curdev = device_names; *curdrive;
  414. curdrive++, curdev++)
  415. {
  416. grub_device_t dev = NULL;
  417. grub_util_info ("opening %s", *curdrive);
  418. dev = grub_device_open (*curdrive);
  419. if (! dev)
  420. grub_util_error ("%s", grub_errmsg);
  421. if (print == PRINT_HINT_STR)
  422. {
  423. const char *osdev = grub_util_biosdisk_get_osdev (dev->disk);
  424. char *ofpath = osdev ? grub_util_devname_to_ofpath (osdev) : 0;
  425. char *biosname, *bare, *efi;
  426. const char *map;
  427. if (ofpath)
  428. {
  429. char *tmp = xmalloc (strlen (ofpath) + sizeof ("ieee1275/"));
  430. char *p;
  431. p = grub_stpcpy (tmp, "ieee1275/");
  432. strcpy (p, ofpath);
  433. printf ("--hint-ieee1275='");
  434. grub_util_fprint_full_disk_name (stdout, tmp, dev);
  435. printf ("' ");
  436. free (tmp);
  437. free (ofpath);
  438. }
  439. biosname = grub_util_guess_bios_drive (*curdev);
  440. if (biosname)
  441. {
  442. printf ("--hint-bios=");
  443. grub_util_fprint_full_disk_name (stdout, biosname, dev);
  444. printf (" ");
  445. }
  446. free (biosname);
  447. efi = grub_util_guess_efi_drive (*curdev);
  448. if (efi)
  449. {
  450. printf ("--hint-efi=");
  451. grub_util_fprint_full_disk_name (stdout, efi, dev);
  452. printf (" ");
  453. }
  454. free (efi);
  455. bare = grub_util_guess_baremetal_drive (*curdev);
  456. if (bare)
  457. {
  458. printf ("--hint-baremetal=");
  459. grub_util_fprint_full_disk_name (stdout, bare, dev);
  460. printf (" ");
  461. }
  462. free (bare);
  463. /* FIXME: Add ARC hint. */
  464. map = grub_util_biosdisk_get_compatibility_hint (dev->disk);
  465. if (map)
  466. {
  467. printf ("--hint='");
  468. grub_util_fprint_full_disk_name (stdout, map, dev);
  469. printf ("' ");
  470. }
  471. if (curdrive[1])
  472. printf (" ");
  473. else
  474. printf ("\n");
  475. }
  476. else if ((print == PRINT_COMPATIBILITY_HINT || print == PRINT_BIOS_HINT
  477. || print == PRINT_IEEE1275_HINT || print == PRINT_BAREMETAL_HINT
  478. || print == PRINT_EFI_HINT || print == PRINT_ARC_HINT)
  479. && dev->disk->dev->id != GRUB_DISK_DEVICE_HOSTDISK_ID)
  480. {
  481. grub_util_fprint_full_disk_name (stdout, dev->disk->name, dev);
  482. putchar (delim);
  483. }
  484. else if (print == PRINT_COMPATIBILITY_HINT)
  485. {
  486. const char *map;
  487. char *biosname;
  488. map = grub_util_biosdisk_get_compatibility_hint (dev->disk);
  489. if (map)
  490. {
  491. grub_util_fprint_full_disk_name (stdout, map, dev);
  492. putchar (delim);
  493. grub_device_close (dev);
  494. /* Compatibility hint is one device only. */
  495. break;
  496. }
  497. biosname = grub_util_guess_bios_drive (*curdev);
  498. if (biosname)
  499. {
  500. grub_util_fprint_full_disk_name (stdout, biosname, dev);
  501. putchar (delim);
  502. free (biosname);
  503. /* Compatibility hint is one device only. */
  504. grub_device_close (dev);
  505. break;
  506. }
  507. }
  508. else if (print == PRINT_BIOS_HINT)
  509. {
  510. char *biosname;
  511. biosname = grub_util_guess_bios_drive (*curdev);
  512. if (biosname)
  513. {
  514. grub_util_fprint_full_disk_name (stdout, biosname, dev);
  515. putchar (delim);
  516. free (biosname);
  517. }
  518. }
  519. else if (print == PRINT_IEEE1275_HINT)
  520. {
  521. const char *osdev = grub_util_biosdisk_get_osdev (dev->disk);
  522. char *ofpath = grub_util_devname_to_ofpath (osdev);
  523. const char *map;
  524. map = grub_util_biosdisk_get_compatibility_hint (dev->disk);
  525. if (map)
  526. {
  527. grub_util_fprint_full_disk_name (stdout, map, dev);
  528. putchar (delim);
  529. }
  530. if (ofpath)
  531. {
  532. char *tmp = xmalloc (strlen (ofpath) + sizeof ("ieee1275/"));
  533. char *p;
  534. p = grub_stpcpy (tmp, "ieee1275/");
  535. strcpy (p, ofpath);
  536. grub_util_fprint_full_disk_name (stdout, tmp, dev);
  537. free (tmp);
  538. free (ofpath);
  539. putchar (delim);
  540. }
  541. }
  542. else if (print == PRINT_EFI_HINT)
  543. {
  544. char *biosname;
  545. const char *map;
  546. biosname = grub_util_guess_efi_drive (*curdev);
  547. map = grub_util_biosdisk_get_compatibility_hint (dev->disk);
  548. if (map)
  549. {
  550. grub_util_fprint_full_disk_name (stdout, map, dev);
  551. putchar (delim);
  552. }
  553. if (biosname)
  554. {
  555. grub_util_fprint_full_disk_name (stdout, biosname, dev);
  556. putchar (delim);
  557. free (biosname);
  558. }
  559. }
  560. else if (print == PRINT_BAREMETAL_HINT)
  561. {
  562. char *biosname;
  563. const char *map;
  564. biosname = grub_util_guess_baremetal_drive (*curdev);
  565. map = grub_util_biosdisk_get_compatibility_hint (dev->disk);
  566. if (map)
  567. {
  568. grub_util_fprint_full_disk_name (stdout, map, dev);
  569. putchar (delim);
  570. }
  571. if (biosname)
  572. {
  573. grub_util_fprint_full_disk_name (stdout, biosname, dev);
  574. putchar (delim);
  575. free (biosname);
  576. }
  577. }
  578. else if (print == PRINT_ARC_HINT)
  579. {
  580. const char *map;
  581. map = grub_util_biosdisk_get_compatibility_hint (dev->disk);
  582. if (map)
  583. {
  584. grub_util_fprint_full_disk_name (stdout, map, dev);
  585. putchar (delim);
  586. }
  587. }
  588. else if (print == PRINT_ABSTRACTION)
  589. probe_abstraction (dev->disk, delim);
  590. else if (print == PRINT_CRYPTODISK_UUID)
  591. probe_cryptodisk_uuid (dev->disk, delim);
  592. else if (print == PRINT_PARTMAP)
  593. /* Check if dev->disk itself is contained in a partmap. */
  594. probe_partmap (dev->disk, delim);
  595. else if (print == PRINT_PARTUUID)
  596. {
  597. probe_partuuid (dev->disk, delim);
  598. putchar (delim);
  599. }
  600. else if (print == PRINT_MSDOS_PARTTYPE)
  601. {
  602. if (dev->disk->partition
  603. && strcmp(dev->disk->partition->partmap->name, "msdos") == 0)
  604. printf ("%02x", dev->disk->partition->msdostype);
  605. putchar (delim);
  606. }
  607. else if (print == PRINT_GPT_PARTTYPE)
  608. {
  609. if (dev->disk->partition
  610. && strcmp (dev->disk->partition->partmap->name, "gpt") == 0)
  611. {
  612. struct grub_gpt_partentry gptdata;
  613. grub_partition_t p = dev->disk->partition;
  614. dev->disk->partition = dev->disk->partition->parent;
  615. if (grub_disk_read (dev->disk, p->offset, p->index,
  616. sizeof (gptdata), &gptdata) == 0)
  617. print_gpt_guid(gptdata.type);
  618. dev->disk->partition = p;
  619. }
  620. putchar (delim);
  621. }
  622. grub_device_close (dev);
  623. }
  624. end:
  625. for (curdrive = drives_names; *curdrive; curdrive++)
  626. free (*curdrive);
  627. free (drives_names);
  628. free_device_names:
  629. if (path != NULL)
  630. {
  631. for (curdev = device_names; *curdev; curdev++)
  632. free (*curdev);
  633. free (device_names);
  634. }
  635. }
  636. static struct argp_option options[] = {
  637. {"device", 'd', 0, 0,
  638. N_("given argument is a system device, not a path"), 0},
  639. {"device-map", 'm', N_("FILE"), 0,
  640. N_("use FILE as the device map [default=%s]"), 0},
  641. {"target", 't', N_("TARGET"), 0, 0, 0},
  642. {"verbose", 'v', 0, 0,
  643. N_("print verbose messages (pass twice to enable debug printing)."), 0},
  644. {0, '0', 0, 0, N_("separate items in output using ASCII NUL characters"), 0},
  645. { 0, 0, 0, 0, 0, 0 }
  646. };
  647. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  648. static char *
  649. help_filter (int key, const char *text, void *input __attribute__ ((unused)))
  650. {
  651. switch (key)
  652. {
  653. case 'm':
  654. return xasprintf (text, DEFAULT_DEVICE_MAP);
  655. case 't':
  656. {
  657. char *ret, *t = get_targets_string (), *def;
  658. def = xasprintf (_("[default=%s]"), targets[print]);
  659. ret = xasprintf ("%s\n%s %s %s", _("print TARGET"),
  660. _("available targets:"), t, def);
  661. free (t);
  662. free (def);
  663. return ret;
  664. }
  665. default:
  666. return (char *) text;
  667. }
  668. }
  669. #pragma GCC diagnostic error "-Wformat-nonliteral"
  670. struct arguments
  671. {
  672. char **devices;
  673. size_t device_max;
  674. size_t ndevices;
  675. char *dev_map;
  676. int zero_delim;
  677. };
  678. static error_t
  679. argp_parser (int key, char *arg, struct argp_state *state)
  680. {
  681. /* Get the input argument from argp_parse, which we
  682. know is a pointer to our arguments structure. */
  683. struct arguments *arguments = state->input;
  684. switch (key)
  685. {
  686. case 'd':
  687. argument_is_device = 1;
  688. break;
  689. case 'm':
  690. if (arguments->dev_map)
  691. free (arguments->dev_map);
  692. arguments->dev_map = xstrdup (arg);
  693. break;
  694. case 't':
  695. {
  696. int i;
  697. for (i = PRINT_FS; i < ARRAY_SIZE (targets); i++)
  698. if (strcmp (arg, targets[i]) == 0)
  699. {
  700. print = i;
  701. break;
  702. }
  703. if (i == ARRAY_SIZE (targets))
  704. argp_usage (state);
  705. }
  706. break;
  707. case '0':
  708. arguments->zero_delim = 1;
  709. break;
  710. case 'v':
  711. verbosity++;
  712. break;
  713. case ARGP_KEY_NO_ARGS:
  714. fprintf (stderr, "%s", _("No path or device is specified.\n"));
  715. argp_usage (state);
  716. break;
  717. case ARGP_KEY_ARG:
  718. assert (arguments->ndevices < arguments->device_max);
  719. arguments->devices[arguments->ndevices++] = xstrdup(arg);
  720. break;
  721. default:
  722. return ARGP_ERR_UNKNOWN;
  723. }
  724. return 0;
  725. }
  726. static struct argp argp = {
  727. options, argp_parser, N_("[OPTION]... [PATH|DEVICE]"),
  728. N_("\
  729. Probe device information for a given path (or device, if the -d option is given)."),
  730. NULL, help_filter, NULL
  731. };
  732. int
  733. main (int argc, char *argv[])
  734. {
  735. char delim;
  736. struct arguments arguments;
  737. grub_util_host_init (&argc, &argv);
  738. memset (&arguments, 0, sizeof (struct arguments));
  739. arguments.device_max = argc + 1;
  740. arguments.devices = xmalloc ((arguments.device_max + 1)
  741. * sizeof (arguments.devices[0]));
  742. memset (arguments.devices, 0, (arguments.device_max + 1)
  743. * sizeof (arguments.devices[0]));
  744. /* Parse our arguments */
  745. if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
  746. {
  747. fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
  748. exit(1);
  749. }
  750. if (verbosity > 1)
  751. grub_env_set ("debug", "all");
  752. /* Obtain ARGUMENT. */
  753. if (arguments.ndevices != 1 && !argument_is_device)
  754. {
  755. char *program = xstrdup(program_name);
  756. fprintf (stderr, _("Unknown extra argument `%s'."), arguments.devices[1]);
  757. fprintf (stderr, "\n");
  758. argp_help (&argp, stderr, ARGP_HELP_STD_USAGE, program);
  759. free (program);
  760. exit(1);
  761. }
  762. /* Initialize the emulated biosdisk driver. */
  763. grub_util_biosdisk_init (arguments.dev_map ? : DEFAULT_DEVICE_MAP);
  764. /* Initialize all modules. */
  765. grub_init_all ();
  766. grub_gcry_init_all ();
  767. grub_lvm_fini ();
  768. grub_mdraid09_fini ();
  769. grub_mdraid1x_fini ();
  770. grub_diskfilter_fini ();
  771. grub_diskfilter_init ();
  772. grub_mdraid09_init ();
  773. grub_mdraid1x_init ();
  774. grub_lvm_init ();
  775. if (print == PRINT_BIOS_HINT
  776. || print == PRINT_IEEE1275_HINT || print == PRINT_BAREMETAL_HINT
  777. || print == PRINT_EFI_HINT || print == PRINT_ARC_HINT)
  778. delim = ' ';
  779. else
  780. delim = '\n';
  781. if (arguments.zero_delim)
  782. delim = '\0';
  783. /* Do it. */
  784. if (argument_is_device)
  785. probe (NULL, arguments.devices, delim);
  786. else
  787. probe (arguments.devices[0], NULL, delim);
  788. if (delim == ' ')
  789. putchar ('\n');
  790. /* Free resources. */
  791. grub_gcry_fini_all ();
  792. grub_fini_all ();
  793. grub_util_biosdisk_fini ();
  794. {
  795. size_t i;
  796. for (i = 0; i < arguments.ndevices; i++)
  797. free (arguments.devices[i]);
  798. }
  799. free (arguments.devices);
  800. free (arguments.dev_map);
  801. return 0;
  802. }