42,45c42,53 < #define PRINT_FS 0 < #define PRINT_DRIVE 1 < #define PRINT_DEVICE 2 < #define PRINT_PARTMAP 3 --- > #define GRUB_PRINT_DEFAULT 0x00 > #define GRUB_PRINT_DRIVE 0x01 > #define GRUB_PRINT_DEVICE 0x02 > #define GRUB_PRINT_PARTMAP 0x04 > #define GRUB_PRINT_FS 0x08 > #define GRUB_PRINT_INVAL 0x10 > #define GRUB_PRINT_SHVAR 0x20 > #define GRUB_PRINT_PREFIX 0x40 > > static int grub_parse_target (const char *arg, int old_fl); > static void usage (int status); > static void dump_target_flags (int fl); 47c55,58 < int print = PRINT_FS; --- > static char *output_prefix = NULL; > static void print_target_value ( int target_fl, int target, const char *value); > > static void probe (const char *path, int target_fl); 72,138d82 < static void < probe (const char *path) < { < char *device_name; < char *drive_name = NULL; < grub_device_t dev; < grub_fs_t fs; < < device_name = grub_guess_root_device (path); < if (! device_name) < grub_util_error ("cannot find a device for %s.\n", path); < < if (print == PRINT_DEVICE) < { < printf ("%s\n", device_name); < goto end; < } < < drive_name = grub_util_get_grub_dev (device_name); < if (! drive_name) < grub_util_error ("cannot find a GRUB drive for %s.\n", device_name); < < if (print == PRINT_DRIVE) < { < printf ("(%s)\n", drive_name); < goto end; < } < < grub_util_info ("opening %s", drive_name); < dev = grub_device_open (drive_name); < if (! dev) < grub_util_error ("%s", grub_errmsg); < < if (print == PRINT_PARTMAP) < { < if (dev->disk->partition == NULL) < grub_util_error ("Cannot detect partition map for %s", drive_name); < < if (strcmp (dev->disk->partition->partmap->name, "amiga_partition_map") == 0) < printf ("amiga\n"); < else if (strcmp (dev->disk->partition->partmap->name, "apple_partition_map") == 0) < printf ("apple\n"); < else if (strcmp (dev->disk->partition->partmap->name, "gpt_partition_map") == 0) < printf ("gpt\n"); < else if (strcmp (dev->disk->partition->partmap->name, "pc_partition_map") == 0) < printf ("pc\n"); < else if (strcmp (dev->disk->partition->partmap->name, "sun_partition_map") == 0) < printf ("sun\n"); < else < grub_util_error ("Unknown partition map %s", dev->disk->partition->partmap->name); < goto end; < } < < fs = grub_fs_probe (dev); < if (! fs) < grub_util_error ("%s", grub_errmsg); < < printf ("%s\n", fs->name); < < grub_device_close (dev); < < end: < < free (device_name); < free (drive_name); < } < 145a90,91 > {"prefix-prefix", required_argument, 0, 'P'}, > {"prefix-shvar", required_argument, 0, 'S'}, 149,174d94 < static void < usage (int status) < { < if (status) < fprintf (stderr, < "Try ``grub-probe --help'' for more information.\n"); < else < printf ("\ < Usage: grub-probe [OPTION]... PATH\n\ < \n\ < Probe device information for a given path.\n\ < \n\ < -m, --device-map=FILE use FILE as the device map [default=%s]\n\ < -t, --target=(fs|drive|device|partmap)\n\ < print filesystem module, GRUB drive, system device or partition map module [default=fs]\n\ < -h, --help display this message and exit\n\ < -V, --version print version information and exit\n\ < -v, --verbose print verbose messages\n\ < \n\ < Report bugs to <%s>.\n\ < ", < DEFAULT_DEVICE_MAP, PACKAGE_BUGREPORT); < < exit (status); < } < 179a100 > int target_fl = GRUB_PRINT_DEFAULT; 186c107 < int c = getopt_long (argc, argv, "m:t:hVv", options, 0); --- > int c = getopt_long (argc, argv, "m:t:hVvP:S:A", options, 0); 201,210c122 < if (!strcmp (optarg, "fs")) < print = PRINT_FS; < else if (!strcmp (optarg, "drive")) < print = PRINT_DRIVE; < else if (!strcmp (optarg, "device")) < print = PRINT_DEVICE; < else if (!strcmp (optarg, "partmap")) < print = PRINT_PARTMAP; < else < usage (1); --- > target_fl = grub_parse_target (optarg, target_fl); 212a125,139 > case 'S': > output_prefix = optarg; > target_fl |= GRUB_PRINT_SHVAR; > break; > > case 'P': > output_prefix = optarg; > target_fl |= GRUB_PRINT_PREFIX; > break; > > case 'A': > target_fl |= GRUB_PRINT_FS|GRUB_PRINT_PARTMAP > |GRUB_PRINT_DEVICE|GRUB_PRINT_DRIVE; > break; > 245a173,185 > if (target_fl & GRUB_PRINT_INVAL) > { > if ((target_fl & ~GRUB_PRINT_INVAL) == GRUB_PRINT_DEFAULT) > { > fputs ("No targets provided are valid.\n", stderr); > usage(1); > } > target_fl = target_fl & ~GRUB_PRINT_INVAL; > } > > if ((target_fl & GRUB_PRINT_PREFIX) && (target_fl & (GRUB_PRINT_SHVAR))) > grub_util_error ("targets `prefix' and `shvar' should not be used together!"); > 253c193 < probe (path); --- > probe (path, target_fl); 261a202,452 > } > > > static void > probe (const char *path, int target_fl) > { > char *device_name = NULL; > char *drive_name = NULL; > grub_device_t dev; > grub_fs_t fs; > > if ((target_fl & ~(GRUB_PRINT_PREFIX|GRUB_PRINT_SHVAR)) == GRUB_PRINT_DEFAULT) > target_fl |= GRUB_PRINT_FS; > > //dump_target_flags (target_fl); > device_name = grub_guess_root_device (path); > if (! device_name) > grub_util_error ("cannot find a device for %s.\n", path); > > grub_util_info ("Using device `%s' for `%s'", device_name, path); > if (target_fl & GRUB_PRINT_DEVICE) > print_target_value (target_fl, GRUB_PRINT_DEVICE, device_name); > > if (target_fl & (GRUB_PRINT_FS|GRUB_PRINT_PARTMAP|GRUB_PRINT_DRIVE)) > { > drive_name = grub_util_get_grub_dev (device_name); > if (! drive_name) > grub_util_error ("cannot find a GRUB drive for %s.\n", device_name); > > if (target_fl & GRUB_PRINT_DRIVE) > print_target_value (target_fl, GRUB_PRINT_DRIVE, drive_name); > > if (target_fl & (GRUB_PRINT_FS|GRUB_PRINT_PARTMAP)) > { > dev = grub_device_open (drive_name); > if (! dev) > grub_util_error ("%s", grub_errmsg); > > if (target_fl & GRUB_PRINT_PARTMAP) > { > char *value; > if (dev->disk->partition == NULL) > grub_util_error ("Cannot detect partition map for %s", drive_name); > > if (strcmp (dev->disk->partition->partmap->name, "amiga_partition_map") == 0) > value = "amiga"; > else if (strcmp (dev->disk->partition->partmap->name, "apple_partition_map") == 0) > value = "apple"; > else if (strcmp (dev->disk->partition->partmap->name, "gpt_partition_map") == 0) > value = "gpt"; > else if (strcmp (dev->disk->partition->partmap->name, "pc_partition_map") == 0) > value = "pc"; > else if (strcmp (dev->disk->partition->partmap->name, "sun_partition_map") == 0) > value = "sun"; > else > { > if (!(target_fl & (GRUB_PRINT_PREFIX|GRUB_PRINT_SHVAR))) > grub_util_error ("Unknown partition map %s", dev->disk->partition->partmap->name); > else > value = ""; > } > print_target_value (target_fl, GRUB_PRINT_PARTMAP, value); > } > if (target_fl & GRUB_PRINT_FS) > { > fs = grub_fs_probe (dev); > if (! fs) > grub_util_error ("%s", grub_errmsg); > > print_target_value (target_fl, GRUB_PRINT_FS, fs->name); > } > grub_device_close (dev); > } > } > if (device_name) > free (device_name); > if (drive_name) > free (drive_name); > } > > static void > print_target_value ( int target_fl, int target, const char *value) > { > char *target_str = NULL; > > if (!value) > value = ""; > > if (target == GRUB_PRINT_FS) > target_str = "fs"; > else if (target == GRUB_PRINT_DRIVE) > target_str = "drive"; > else if (target == GRUB_PRINT_DEVICE) > target_str = "device"; > else if (target == GRUB_PRINT_PARTMAP) > target_str = "partmap"; > else > { > fprintf (stderr, "INTERNAL ERROR: invalid target int print_target_value()\n"); > exit(1); > } > > if (target_fl & (GRUB_PRINT_SHVAR|GRUB_PRINT_PREFIX)) > { > if (target_fl & GRUB_PRINT_SHVAR) > { > if (output_prefix) > fprintf (stdout, "%s%s=\"%s\"\n", output_prefix, target_str, value); > else > fprintf (stdout, "%s=\"%s\"\n", target_str, value); > } > if (target_fl & GRUB_PRINT_PREFIX) > { > if (output_prefix) > fprintf (stdout, "%s%s: %s\n", output_prefix, target_str, value); > else > fprintf (stdout, "%s: %s\n", target_str, value); > } > } > else > { > puts (value); > } > } > > static int > grub_parse_target (const char *arg, int old_fl) > { > char *argv[16]; > int i; > int fl = GRUB_PRINT_DEFAULT; > > if ( arg ) > { > char *next = (char*) arg; > for ( i = 0; i < 16 && next && *next ; ++i ) > { > arg = (const char*)next; > next = strchr( next, ','); > if ( next ) > { > *next = 0; > ++next; > } > argv[i] = (char*)arg; > } > while (i > 0) > { > --i; > if (!strcmp ("all", argv[i])) > fl |= GRUB_PRINT_FS|GRUB_PRINT_DRIVE|GRUB_PRINT_DEVICE|GRUB_PRINT_PARTMAP; > else if (!strcmp ("fs", argv[i])) > fl |= GRUB_PRINT_FS; > else if (!strcmp ("drive", argv[i])) > fl |= GRUB_PRINT_DRIVE; > else if (!strcmp ("device", argv[i])) > fl |= GRUB_PRINT_DEVICE; > else if (!strcmp ("partmap", argv[i])) > fl |= GRUB_PRINT_PARTMAP; > else if (!strcmp ("prefix", argv[i])) > fl |= GRUB_PRINT_PREFIX; > else if (!strcmp ("shvar", argv[i])) > fl |= GRUB_PRINT_SHVAR; > else > { > fl |= GRUB_PRINT_INVAL; > grub_util_info ("grub-probe: warning: Invalid target `%s'\n", argv[i]); > } > } > } > return fl | old_fl; > } > > > static void > usage (int status) > { > if (status) > fprintf (stderr, > "Try ``grub-probe --help'' for more information.\n"); > else > printf ("\ > Usage: grub-probe [OPTION]... PATH\n\ > \n\ > Probe device information for a given path.\n\ > \n\ > -m, --device-map=FILE use FILE as the device map [default=%s]\n\ > -t, --target=(all,fs,drive,device,partmap,prefix,shvar)\n\ > print filesystem module, GRUB drive, system\n\ > device and/or partition map module [default=fs].\n\ > It is possible to add multiple targets by separating\n\ > them using commas and/or adding multiple -t on\n\ > the command line. `all' can be used to set\n\ > `fs', `drive', `device' and `partmap' targets.\n\n\ > If no -t parameters are specified, grub-probe\n\ > will print the default target.\n\n\ > On the other hand, if targets are specified and none\n\ > contain valid targets, the application terminates\n\ > after emitting an error message. If some of the targets\n\ > are valid, the application will warn the user about\n\ > invalid targets and print the values for the valid\n\ > ones.\n\n\ > There is also two pseudo-targets which cannot co-exist:\n\n\ > \t`prefix' will print the target name before the\n\ > \t\t value, separating them with the sequence\n\ > \t\t semi-colon_space (: ).\n\ > \t\tex:\ttarget: value\\n\n\n\ > \t`shvar'\t prints values in this format:\n\ > \t\tex:\ttarget=\"value\"\\n\n\n\ > If `shvar' is used, value can be imported into Bourne\n\ > Shell scripts using:\n\ > \t\teval `grub-probe -t shvar `\n\ > -P, --prefix-prefix=\"prefix\"\n\ > Prefixes the output variable name with \"prefix\" in \n\ > `prefix' output mode and activate it.\n\ > -S, --prefix-shvar=\"prefix\"\n\ > Prefix outputs variable name with \"prefix\" in \n\ > `shvar' output mode and activate it.\n\ > -A, --all Same as `-t all'\n\ > -h, --help display this message and exit\n\ > -V, --version print version information and exit\n\ > -v, --verbose print verbose messages\n\ > \n\ > Report bugs to <%s>.\n\ > ", > DEFAULT_DEVICE_MAP, PACKAGE_BUGREPORT); > > exit (status); > } > > > > static void > dump_target_flags (int fl) > { > fputs ("grub-probe: DUMP: target flags = ", stderr); > if ( fl & GRUB_PRINT_INVAL ) > fputs ("GRUB_PRINT_INVAL ", stderr); > if ( fl & GRUB_PRINT_DRIVE ) > fputs ("GRUB_PRINT_DRIVE ", stderr); > if ( fl & GRUB_PRINT_DEVICE ) > fputs ("GRUB_PRINT_DEVICE ", stderr); > if ( fl & GRUB_PRINT_PARTMAP ) > fputs ("GRUB_PRINT_PARTMAP ", stderr); > if ( fl & GRUB_PRINT_FS ) > fputs ("GRUB_PRINT_FS ", stderr); > if ( fl & GRUB_PRINT_SHVAR ) > fputs ("GRUB_PRINT_SHVAR ", stderr); > if ( fl & GRUB_PRINT_PREFIX ) > fputs ("GRUB_PRINT_PREFIX ", stderr); > fputs ("\n", stderr);