grub-fstest.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /* grub-fstest.c - debug tool for filesystem driver */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 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/misc.h>
  24. #include <grub/device.h>
  25. #include <grub/disk.h>
  26. #include <grub/file.h>
  27. #include <grub/fs.h>
  28. #include <grub/env.h>
  29. #include <grub/term.h>
  30. #include <grub/mm.h>
  31. #include <grub/lib/hexdump.h>
  32. #include <grub/crypto.h>
  33. #include <grub/command.h>
  34. #include <grub/i18n.h>
  35. #include <grub/zfs/zfs.h>
  36. #include <grub/emu/hostfile.h>
  37. #include <stdio.h>
  38. #include <errno.h>
  39. #include <string.h>
  40. #include "progname.h"
  41. #pragma GCC diagnostic ignored "-Wmissing-prototypes"
  42. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  43. #include "argp.h"
  44. #pragma GCC diagnostic error "-Wmissing-prototypes"
  45. #pragma GCC diagnostic error "-Wmissing-declarations"
  46. static grub_err_t
  47. execute_command (const char *name, int n, char **args)
  48. {
  49. grub_command_t cmd;
  50. cmd = grub_command_find (name);
  51. if (! cmd)
  52. grub_util_error (_("can't find command `%s'"), name);
  53. return (cmd->func) (cmd, n, args);
  54. }
  55. enum {
  56. CMD_LS = 1,
  57. CMD_CP,
  58. CMD_CAT,
  59. CMD_CMP,
  60. CMD_HEX,
  61. CMD_CRC,
  62. CMD_BLOCKLIST,
  63. CMD_TESTLOAD,
  64. CMD_ZFSINFO,
  65. CMD_XNU_UUID
  66. };
  67. #define BUF_SIZE 32256
  68. static grub_disk_addr_t skip, leng;
  69. static int uncompress = 0;
  70. static void
  71. read_file (char *pathname, int (*hook) (grub_off_t ofs, char *buf, int len, void *hook_arg), void *hook_arg)
  72. {
  73. static char buf[BUF_SIZE];
  74. grub_file_t file;
  75. if ((pathname[0] == '-') && (pathname[1] == 0))
  76. {
  77. grub_device_t dev;
  78. dev = grub_device_open (0);
  79. if ((! dev) || (! dev->disk))
  80. grub_util_error ("%s", grub_errmsg);
  81. grub_util_info ("total sectors : %" GRUB_HOST_PRIuLONG_LONG,
  82. (unsigned long long) dev->disk->total_sectors);
  83. if (! leng)
  84. leng = (dev->disk->total_sectors << GRUB_DISK_SECTOR_BITS) - skip;
  85. while (leng)
  86. {
  87. grub_size_t len;
  88. len = (leng > BUF_SIZE) ? BUF_SIZE : leng;
  89. if (grub_disk_read (dev->disk, 0, skip, len, buf))
  90. {
  91. char *msg = grub_xasprintf (_("disk read fails at offset %lld, length %lld"),
  92. (long long) skip, (long long) len);
  93. grub_util_error ("%s", msg);
  94. }
  95. if (hook (skip, buf, len, hook_arg))
  96. break;
  97. skip += len;
  98. leng -= len;
  99. }
  100. grub_device_close (dev);
  101. return;
  102. }
  103. if (uncompress == 0)
  104. grub_file_filter_disable_compression ();
  105. file = grub_file_open (pathname);
  106. if (!file)
  107. {
  108. grub_util_error (_("cannot open `%s': %s"), pathname,
  109. grub_errmsg);
  110. return;
  111. }
  112. grub_util_info ("file size : %" GRUB_HOST_PRIuLONG_LONG,
  113. (unsigned long long) file->size);
  114. if (skip > file->size)
  115. {
  116. char *msg = grub_xasprintf (_("invalid skip value %lld"),
  117. (unsigned long long) skip);
  118. grub_util_error ("%s", msg);
  119. return;
  120. }
  121. {
  122. grub_off_t ofs, len;
  123. ofs = skip;
  124. len = file->size - skip;
  125. if ((leng) && (leng < len))
  126. len = leng;
  127. file->offset = skip;
  128. while (len)
  129. {
  130. grub_ssize_t sz;
  131. sz = grub_file_read (file, buf, (len > BUF_SIZE) ? BUF_SIZE : len);
  132. if (sz < 0)
  133. {
  134. char *msg = grub_xasprintf (_("read error at offset %llu: %s"),
  135. (unsigned long long) ofs, grub_errmsg);
  136. grub_util_error ("%s", msg);
  137. break;
  138. }
  139. if ((sz == 0) || (hook (ofs, buf, sz, hook_arg)))
  140. break;
  141. ofs += sz;
  142. len -= sz;
  143. }
  144. }
  145. grub_file_close (file);
  146. }
  147. struct cp_hook_ctx
  148. {
  149. FILE *ff;
  150. const char *dest;
  151. };
  152. static int
  153. cp_hook (grub_off_t ofs, char *buf, int len, void *_ctx)
  154. {
  155. struct cp_hook_ctx *ctx = _ctx;
  156. (void) ofs;
  157. if ((int) fwrite (buf, 1, len, ctx->ff) != len)
  158. {
  159. grub_util_error (_("cannot write to `%s': %s"),
  160. ctx->dest, strerror (errno));
  161. return 1;
  162. }
  163. return 0;
  164. }
  165. static void
  166. cmd_cp (char *src, const char *dest)
  167. {
  168. struct cp_hook_ctx ctx =
  169. {
  170. .dest = dest
  171. };
  172. ctx.ff = grub_util_fopen (dest, "wb");
  173. if (ctx.ff == NULL)
  174. {
  175. grub_util_error (_("cannot open OS file `%s': %s"), dest,
  176. strerror (errno));
  177. return;
  178. }
  179. read_file (src, cp_hook, &ctx);
  180. fclose (ctx.ff);
  181. }
  182. static int
  183. cat_hook (grub_off_t ofs, char *buf, int len, void *_arg __attribute__ ((unused)))
  184. {
  185. (void) ofs;
  186. if ((int) fwrite (buf, 1, len, stdout) != len)
  187. {
  188. grub_util_error (_("cannot write to the stdout: %s"),
  189. strerror (errno));
  190. return 1;
  191. }
  192. return 0;
  193. }
  194. static void
  195. cmd_cat (char *src)
  196. {
  197. read_file (src, cat_hook, 0);
  198. }
  199. static int
  200. cmp_hook (grub_off_t ofs, char *buf, int len, void *ff_in)
  201. {
  202. FILE *ff = ff_in;
  203. static char buf_1[BUF_SIZE];
  204. if ((int) fread (buf_1, 1, len, ff) != len)
  205. {
  206. char *msg = grub_xasprintf (_("read error at offset %llu: %s"),
  207. (unsigned long long) ofs, grub_errmsg);
  208. grub_util_error ("%s", msg);
  209. return 1;
  210. }
  211. if (grub_memcmp (buf, buf_1, len) != 0)
  212. {
  213. int i;
  214. for (i = 0; i < len; i++, ofs++)
  215. if (buf_1[i] != buf[i])
  216. {
  217. char *msg = grub_xasprintf (_("compare fail at offset %llu"),
  218. (unsigned long long) ofs);
  219. grub_util_error ("%s", msg);
  220. return 1;
  221. }
  222. }
  223. return 0;
  224. }
  225. static void
  226. cmd_cmp (char *src, char *dest)
  227. {
  228. FILE *ff;
  229. if (grub_util_is_directory (dest))
  230. {
  231. grub_util_fd_dir_t dir = grub_util_fd_opendir (dest);
  232. grub_util_fd_dirent_t entry;
  233. if (dir == NULL)
  234. {
  235. grub_util_error (_("OS file %s open error: %s"), dest,
  236. grub_util_fd_strerror ());
  237. return;
  238. }
  239. while ((entry = grub_util_fd_readdir (dir)))
  240. {
  241. char *srcnew, *destnew;
  242. char *ptr;
  243. if (strcmp (entry->d_name, ".") == 0
  244. || strcmp (entry->d_name, "..") == 0)
  245. continue;
  246. srcnew = xmalloc (strlen (src) + sizeof ("/")
  247. + strlen (entry->d_name));
  248. destnew = xmalloc (strlen (dest) + sizeof ("/")
  249. + strlen (entry->d_name));
  250. ptr = grub_stpcpy (srcnew, src);
  251. *ptr++ = '/';
  252. strcpy (ptr, entry->d_name);
  253. ptr = grub_stpcpy (destnew, dest);
  254. *ptr++ = '/';
  255. strcpy (ptr, entry->d_name);
  256. if (grub_util_is_special_file (destnew))
  257. continue;
  258. cmd_cmp (srcnew, destnew);
  259. }
  260. grub_util_fd_closedir (dir);
  261. return;
  262. }
  263. ff = grub_util_fopen (dest, "rb");
  264. if (ff == NULL)
  265. {
  266. grub_util_error (_("OS file %s open error: %s"), dest,
  267. strerror (errno));
  268. return;
  269. }
  270. if ((skip) && (fseeko (ff, skip, SEEK_SET)))
  271. grub_util_error (_("cannot seek `%s': %s"), dest,
  272. strerror (errno));
  273. read_file (src, cmp_hook, ff);
  274. {
  275. grub_uint64_t pre;
  276. pre = ftell (ff);
  277. fseek (ff, 0, SEEK_END);
  278. if (pre != ftell (ff))
  279. grub_util_error ("%s", _("unexpected end of file"));
  280. }
  281. fclose (ff);
  282. }
  283. static int
  284. hex_hook (grub_off_t ofs, char *buf, int len, void *arg __attribute__ ((unused)))
  285. {
  286. hexdump (ofs, buf, len);
  287. return 0;
  288. }
  289. static void
  290. cmd_hex (char *pathname)
  291. {
  292. read_file (pathname, hex_hook, 0);
  293. }
  294. static int
  295. crc_hook (grub_off_t ofs, char *buf, int len, void *crc_ctx)
  296. {
  297. (void) ofs;
  298. GRUB_MD_CRC32->write(crc_ctx, buf, len);
  299. return 0;
  300. }
  301. static void
  302. cmd_crc (char *pathname)
  303. {
  304. grub_uint8_t *crc32_context = xmalloc (GRUB_MD_CRC32->contextsize);
  305. GRUB_MD_CRC32->init(crc32_context);
  306. read_file (pathname, crc_hook, crc32_context);
  307. GRUB_MD_CRC32->final(crc32_context);
  308. printf ("%08x\n",
  309. grub_be_to_cpu32 (grub_get_unaligned32 (GRUB_MD_CRC32->read (crc32_context))));
  310. free (crc32_context);
  311. }
  312. static const char *root = NULL;
  313. static int args_count = 0;
  314. static int nparm = 0;
  315. static int num_disks = 1;
  316. static char **images = NULL;
  317. static int cmd = 0;
  318. static char *debug_str = NULL;
  319. static char **args = NULL;
  320. static int mount_crypt = 0;
  321. static void
  322. fstest (int n)
  323. {
  324. char *host_file;
  325. char *loop_name;
  326. int i;
  327. for (i = 0; i < num_disks; i++)
  328. {
  329. char *argv[2];
  330. loop_name = grub_xasprintf ("loop%d", i);
  331. if (!loop_name)
  332. grub_util_error ("%s", grub_errmsg);
  333. host_file = grub_xasprintf ("(host)%s", images[i]);
  334. if (!host_file)
  335. grub_util_error ("%s", grub_errmsg);
  336. argv[0] = loop_name;
  337. argv[1] = host_file;
  338. if (execute_command ("loopback", 2, argv))
  339. grub_util_error (_("`loopback' command fails: %s"), grub_errmsg);
  340. grub_free (loop_name);
  341. grub_free (host_file);
  342. }
  343. {
  344. if (mount_crypt)
  345. {
  346. char *argv[2] = { xstrdup ("-a"), NULL};
  347. if (execute_command ("cryptomount", 1, argv))
  348. grub_util_error (_("`cryptomount' command fails: %s"),
  349. grub_errmsg);
  350. free (argv[0]);
  351. }
  352. }
  353. grub_ldm_fini ();
  354. grub_lvm_fini ();
  355. grub_mdraid09_fini ();
  356. grub_mdraid1x_fini ();
  357. grub_diskfilter_fini ();
  358. grub_diskfilter_init ();
  359. grub_mdraid09_init ();
  360. grub_mdraid1x_init ();
  361. grub_lvm_init ();
  362. grub_ldm_init ();
  363. switch (cmd)
  364. {
  365. case CMD_LS:
  366. execute_command ("ls", n, args);
  367. break;
  368. case CMD_ZFSINFO:
  369. execute_command ("zfsinfo", n, args);
  370. break;
  371. case CMD_CP:
  372. cmd_cp (args[0], args[1]);
  373. break;
  374. case CMD_CAT:
  375. cmd_cat (args[0]);
  376. break;
  377. case CMD_CMP:
  378. cmd_cmp (args[0], args[1]);
  379. break;
  380. case CMD_HEX:
  381. cmd_hex (args[0]);
  382. break;
  383. case CMD_CRC:
  384. cmd_crc (args[0]);
  385. break;
  386. case CMD_BLOCKLIST:
  387. execute_command ("blocklist", n, args);
  388. grub_printf ("\n");
  389. break;
  390. case CMD_TESTLOAD:
  391. execute_command ("testload", n, args);
  392. grub_printf ("\n");
  393. break;
  394. case CMD_XNU_UUID:
  395. {
  396. grub_device_t dev;
  397. grub_fs_t fs;
  398. char *uuid = 0;
  399. char *argv[3] = { xstrdup ("-l"), NULL, NULL};
  400. dev = grub_device_open (n ? args[0] : 0);
  401. if (!dev)
  402. grub_util_error ("%s", grub_errmsg);
  403. fs = grub_fs_probe (dev);
  404. if (!fs)
  405. grub_util_error ("%s", grub_errmsg);
  406. if (!fs->uuid)
  407. grub_util_error ("%s", _("couldn't retrieve UUID"));
  408. if (fs->uuid (dev, &uuid))
  409. grub_util_error ("%s", grub_errmsg);
  410. if (!uuid)
  411. grub_util_error ("%s", _("couldn't retrieve UUID"));
  412. argv[1] = uuid;
  413. execute_command ("xnu_uuid", 2, argv);
  414. grub_free (argv[0]);
  415. grub_free (uuid);
  416. grub_device_close (dev);
  417. }
  418. }
  419. for (i = 0; i < num_disks; i++)
  420. {
  421. char *argv[2];
  422. loop_name = grub_xasprintf ("loop%d", i);
  423. if (!loop_name)
  424. grub_util_error ("%s", grub_errmsg);
  425. argv[0] = xstrdup ("-d");
  426. argv[1] = loop_name;
  427. execute_command ("loopback", 2, argv);
  428. grub_free (loop_name);
  429. grub_free (argv[0]);
  430. }
  431. }
  432. static struct argp_option options[] = {
  433. {0, 0, 0 , OPTION_DOC, N_("Commands:"), 1},
  434. {N_("ls PATH"), 0, 0 , OPTION_DOC, N_("List files in PATH."), 1},
  435. {N_("cp FILE LOCAL"), 0, 0, OPTION_DOC, N_("Copy FILE to local file LOCAL."), 1},
  436. {N_("cat FILE"), 0, 0 , OPTION_DOC, N_("Copy FILE to standard output."), 1},
  437. {N_("cmp FILE LOCAL"), 0, 0, OPTION_DOC, N_("Compare FILE with local file LOCAL."), 1},
  438. {N_("hex FILE"), 0, 0 , OPTION_DOC, N_("Show contents of FILE in hex."), 1},
  439. {N_("crc FILE"), 0, 0 , OPTION_DOC, N_("Get crc32 checksum of FILE."), 1},
  440. {N_("blocklist FILE"), 0, 0, OPTION_DOC, N_("Display blocklist of FILE."), 1},
  441. {N_("xnu_uuid DEVICE"), 0, 0, OPTION_DOC, N_("Compute XNU UUID of the device."), 1},
  442. {"root", 'r', N_("DEVICE_NAME"), 0, N_("Set root device."), 2},
  443. {"skip", 's', N_("NUM"), 0, N_("Skip N bytes from output file."), 2},
  444. {"length", 'n', N_("NUM"), 0, N_("Handle N bytes in output file."), 2},
  445. {"diskcount", 'c', N_("NUM"), 0, N_("Specify the number of input files."), 2},
  446. {"debug", 'd', N_("STRING"), 0, N_("Set debug environment variable."), 2},
  447. {"crypto", 'C', NULL, 0, N_("Mount crypto devices."), 2},
  448. {"zfs-key", 'K',
  449. /* TRANSLATORS: "prompt" is a keyword. */
  450. N_("FILE|prompt"), 0, N_("Load zfs crypto key."), 2},
  451. {"verbose", 'v', NULL, 0, N_("print verbose messages."), 2},
  452. {"uncompress", 'u', NULL, 0, N_("Uncompress data."), 2},
  453. {0, 0, 0, 0, 0, 0}
  454. };
  455. /* Print the version information. */
  456. static void
  457. print_version (FILE *stream, struct argp_state *state)
  458. {
  459. fprintf (stream, "%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
  460. }
  461. void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
  462. static error_t
  463. argp_parser (int key, char *arg, struct argp_state *state)
  464. {
  465. char *p;
  466. switch (key)
  467. {
  468. case 'r':
  469. root = arg;
  470. return 0;
  471. case 'K':
  472. if (strcmp (arg, "prompt") == 0)
  473. {
  474. char buf[1024];
  475. grub_puts_ (N_("Enter ZFS password: "));
  476. if (grub_password_get (buf, 1023))
  477. {
  478. grub_zfs_add_key ((grub_uint8_t *) buf, grub_strlen (buf), 1);
  479. }
  480. }
  481. else
  482. {
  483. FILE *f;
  484. ssize_t real_size;
  485. grub_uint8_t buf[1024];
  486. f = grub_util_fopen (arg, "rb");
  487. if (!f)
  488. {
  489. printf (_("%s: error:"), program_name);
  490. printf (_("cannot open `%s': %s"), arg, strerror (errno));
  491. printf ("\n");
  492. return 0;
  493. }
  494. real_size = fread (buf, 1, 1024, f);
  495. fclose (f);
  496. if (real_size < 0)
  497. {
  498. printf (_("%s: error:"), program_name);
  499. printf (_("cannot read `%s': %s"), arg, strerror (errno));
  500. printf ("\n");
  501. return 0;
  502. }
  503. grub_zfs_add_key (buf, real_size, 0);
  504. }
  505. return 0;
  506. case 'C':
  507. mount_crypt = 1;
  508. return 0;
  509. case 's':
  510. skip = grub_strtoul (arg, &p, 0);
  511. if (*p == 's')
  512. skip <<= GRUB_DISK_SECTOR_BITS;
  513. return 0;
  514. case 'n':
  515. leng = grub_strtoul (arg, &p, 0);
  516. if (*p == 's')
  517. leng <<= GRUB_DISK_SECTOR_BITS;
  518. return 0;
  519. case 'c':
  520. num_disks = grub_strtoul (arg, NULL, 0);
  521. if (num_disks < 1)
  522. {
  523. fprintf (stderr, "%s", _("Invalid disk count.\n"));
  524. argp_usage (state);
  525. }
  526. if (args_count != 0)
  527. {
  528. /* TRANSLATORS: disk count is optional but if it's there it must
  529. be before disk list. So please don't imply disk count as mandatory.
  530. */
  531. fprintf (stderr, "%s", _("Disk count must precede disks list.\n"));
  532. argp_usage (state);
  533. }
  534. return 0;
  535. case 'd':
  536. debug_str = arg;
  537. return 0;
  538. case 'v':
  539. verbosity++;
  540. return 0;
  541. case 'u':
  542. uncompress = 1;
  543. return 0;
  544. case ARGP_KEY_END:
  545. if (args_count < num_disks)
  546. {
  547. fprintf (stderr, "%s", _("No command is specified.\n"));
  548. argp_usage (state);
  549. }
  550. if (args_count - 1 - num_disks < nparm)
  551. {
  552. fprintf (stderr, "%s", _("Not enough parameters to command.\n"));
  553. argp_usage (state);
  554. }
  555. return 0;
  556. case ARGP_KEY_ARG:
  557. break;
  558. default:
  559. return ARGP_ERR_UNKNOWN;
  560. }
  561. if (args_count < num_disks)
  562. {
  563. if (args_count == 0)
  564. images = xmalloc (num_disks * sizeof (images[0]));
  565. images[args_count] = grub_canonicalize_file_name (arg);
  566. args_count++;
  567. return 0;
  568. }
  569. if (args_count == num_disks)
  570. {
  571. if (!grub_strcmp (arg, "ls"))
  572. {
  573. cmd = CMD_LS;
  574. }
  575. else if (!grub_strcmp (arg, "zfsinfo"))
  576. {
  577. cmd = CMD_ZFSINFO;
  578. }
  579. else if (!grub_strcmp (arg, "cp"))
  580. {
  581. cmd = CMD_CP;
  582. nparm = 2;
  583. }
  584. else if (!grub_strcmp (arg, "cat"))
  585. {
  586. cmd = CMD_CAT;
  587. nparm = 1;
  588. }
  589. else if (!grub_strcmp (arg, "cmp"))
  590. {
  591. cmd = CMD_CMP;
  592. nparm = 2;
  593. }
  594. else if (!grub_strcmp (arg, "hex"))
  595. {
  596. cmd = CMD_HEX;
  597. nparm = 1;
  598. }
  599. else if (!grub_strcmp (arg, "crc"))
  600. {
  601. cmd = CMD_CRC;
  602. nparm = 1;
  603. }
  604. else if (!grub_strcmp (arg, "blocklist"))
  605. {
  606. cmd = CMD_BLOCKLIST;
  607. nparm = 1;
  608. }
  609. else if (!grub_strcmp (arg, "testload"))
  610. {
  611. cmd = CMD_TESTLOAD;
  612. nparm = 1;
  613. }
  614. else if (grub_strcmp (arg, "xnu_uuid") == 0)
  615. {
  616. cmd = CMD_XNU_UUID;
  617. nparm = 0;
  618. }
  619. else
  620. {
  621. fprintf (stderr, _("Invalid command %s.\n"), arg);
  622. argp_usage (state);
  623. }
  624. args_count++;
  625. return 0;
  626. }
  627. args[args_count - 1 - num_disks] = xstrdup (arg);
  628. args_count++;
  629. return 0;
  630. }
  631. struct argp argp = {
  632. options, argp_parser, N_("IMAGE_PATH COMMANDS"),
  633. N_("Debug tool for filesystem driver."),
  634. NULL, NULL, NULL
  635. };
  636. int
  637. main (int argc, char *argv[])
  638. {
  639. const char *default_root;
  640. char *alloc_root;
  641. grub_util_host_init (&argc, &argv);
  642. args = xmalloc (argc * sizeof (args[0]));
  643. argp_parse (&argp, argc, argv, 0, 0, 0);
  644. /* Initialize all modules. */
  645. grub_init_all ();
  646. grub_gcry_init_all ();
  647. if (debug_str)
  648. grub_env_set ("debug", debug_str);
  649. default_root = (num_disks == 1) ? "loop0" : "md0";
  650. alloc_root = 0;
  651. if (root)
  652. {
  653. if ((*root >= '0') && (*root <= '9'))
  654. {
  655. alloc_root = xmalloc (strlen (default_root) + strlen (root) + 2);
  656. sprintf (alloc_root, "%s,%s", default_root, root);
  657. root = alloc_root;
  658. }
  659. }
  660. else
  661. root = default_root;
  662. grub_env_set ("root", root);
  663. if (alloc_root)
  664. free (alloc_root);
  665. /* Do it. */
  666. fstest (args_count - 1 - num_disks);
  667. /* Free resources. */
  668. grub_gcry_fini_all ();
  669. grub_fini_all ();
  670. return 0;
  671. }