grub-probe.c 22 KB

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