Commits

Milko Krachounov  committed 431ab62

Implement raw printing of the already written partition table on a hard disk, with a command line argument. Doesn't work on anything but msdos partition tables and can't be used for printing the current partition table, at the moment. (fdisk@sv.gnu.org/fdisk--main--0--patch-54)
fdisk@sv.gnu.org/fdisk--main--0--patch-54
Keywords:

  • Participants
  • Parent commits 7f84713

Comments (0)

Files changed (3)

 .B -l, --list
 lists the partition table on the specified device and exits. If there is no device specified, lists the partition tables on all detected devices.
 .TP
+.B -r, --raw-list
+displays a hex dump of the partition table of the disk, similar to the way Linux fdisk displays the raw data in the partition table.
+.TP
 .B -u, --sector-units
 use sectors, instead of cylinders for a default unit.
 .TP

File doc/fdisk.texi

 there  is  no  device  specified, lists the partition tables on all
 detected devices.
 
+@item @b{@minus{}r, @minus{}@minus{}raw-list}
+Displays a hex dump of the partition table of the disk, similar to the way Linux fdisk displays the raw data in the partition table, using the "print the raw data in the partition table" from the expert menu.
+
 @item @b{@minus{}u, @minus{}@minus{}unit-sector}
 Use sectors, instead of cylinders for a default unit.
 
 	/* name, has-arg, string-return-val, char-return-val */
 	{"help",	0, NULL, 'h'},
 	{"list",        0, NULL, 'l'},
+	{"raw-print",	0, NULL, 'r'},
 	{"size",	1, NULL, 's'},
 	{"linux-fdisk", 0, NULL, 'L'},
 	{"gnu-fdisk",   0, NULL, 'G'},
 static char*	options_help [][2] = {
 	{"help",	N_("displays this help message")},
 	{"list",        N_("list partition table(s)")},
+	{"raw-print",	N_("show the raw data in the partition table(s)")},
 	{"size",	N_("show partition size")},
 	{"linux-fdisk", N_("enable Linux fdisk compatibility mode")},
 	{"gnu-fdisk",   N_("disable Linux fdisk compatibility mode")},
 int fdisk_compatibility_mode = 0;
 int fdisk_opt_script_mode;
 int fdisk_list_table = 0;
+int fdisk_print_raw = 0;
+
 /* Used for -s option, NULL when there was no -s option */
 const char *fdisk_part_size = NULL;
 
 	return 1;
 }
 
+/* This prints a sector, in the style Linux fdisk prints raw partition
+   tables, in hex, with 16 bytes per line */
+static void
+print_sector (PedDevice *dev, PedSector sector)
+{
+	char *buf;
+	int size = dev->sector_size, i, pos = 0;
+	buf = malloc(size);
+	if (ped_device_read(dev, buf, sector, 1)) {
+		for (i = 0; i < size; i++) {
+			/* We print the line prefix */
+			if (pos == 0)
+				printf("0x%03X:", i);
+
+			printf(" %02hhX", buf[i]);
+
+			/* We print the line suffix */
+			if (pos == 15) {
+				printf("\n");
+				pos = 0;
+			}
+			else
+				pos++;
+		}
+		if (pos != 0)
+			printf("\n");
+		printf("\n");
+	}
+	free(buf);
+}
+
+static int
+do_raw (PedDisk **disk) 
+{
+	/* TODO: Make this for other disklabels, too */
+	if (strcmp((*disk)->type->name, "msdos")) {
+		fprintf(stderr,
+		        _("Raw printing on %s not supported\n"),
+		        (*disk)->type->name);
+		return 0;
+	}
+
+	printf(_("Device: %s\n"), (*disk)->dev->path);
+
+	/* This is for DOS partition table */
+	PedPartition *part;
+	int i;
+
+	/* First we print the MBR */
+	print_sector((*disk)->dev,0);
+	
+	/* Then we print the partition table sectors for the logical partitions */
+	for (part = ped_disk_get_partition(*disk, i = 5); part;
+	     part = ped_disk_get_partition(*disk, ++i))
+	{
+		print_sector((*disk)->dev, part->prev->geom.start);
+	} 
+
+}
 
 static void
 print_partition_size(PedDisk *disk)
                 ped_device_probe_all ();
     
                 while ((dev = ped_device_get_next (dev))) {
-                         if (!ped_device_open(dev))
-	                         break;
+                        if (!ped_device_open(dev))
+	                        break;
      
-                         PedDisk *disk = ped_disk_new(dev);
-                         do_print (&disk);
-                         ped_disk_destroy(disk);
+                        PedDisk *disk = ped_disk_new(dev);
+
+			if (fdisk_print_raw)
+				do_raw (&disk);
+			else
+                        	do_print (&disk);
+
+                        ped_disk_destroy(disk);
                 }
-        } else
-                do_print(&disk);
+        } else {
+		if (fdisk_print_raw)
+			do_raw(&disk);
+		else
+                	do_print(&disk);
+	}
         exit(0);
 }
 
 	{
 	/* NOTE: b, C, H, S are ignored, for Linux fdisk compatibility */
 #ifdef HAVE_GETOPT_H
-		opt = getopt_long (*argc_ptr, *argv_ptr, "hlipvLGs:utb:C:H:S:",
+		opt = getopt_long (*argc_ptr, *argv_ptr, "hlipvLGs:utb:C:H:S:r",
 				   options, NULL);
 #else
-		opt = getopt (*argc_ptr, *argv_ptr, "hlipvLGs:utb:C:H:S:");
+		opt = getopt (*argc_ptr, *argv_ptr, "hlipvLGs:utb:C:H:S:r");
 #endif
 		if (opt == -1)
 			break;
 		switch (opt) {
 			case 'h': fdisk_help_msg (); break;
 			case 'l': fdisk_list_table = 1; break;
+
+			case 'r': fdisk_list_table = 1;
+			          fdisk_print_raw = 1;
+			          break;
+
 			case 'i': fdisk_opt_script_mode = 0; break;
 			case 'p': fdisk_opt_script_mode = 1; break;
 			case 'G': fdisk_compatibility_mode = 0; break;