diff options
author | Rod Smith <rodsmith@rodsbooks.com> | 2021-06-08 15:23:02 -0400 |
---|---|---|
committer | Rod Smith <rodsmith@rodsbooks.com> | 2021-06-08 15:23:02 -0400 |
commit | 331ad9c795e4db7d09e8141979f809e4f5815319 (patch) | |
tree | f5175e41f8e0124fc10b9d190d5ea8d43bbe48ef | |
parent | 08be8933a9a55d4e919875bf4df4a3ed0a4b514e (diff) | |
download | gptfdisk-331ad9c795e4db7d09e8141979f809e4f5815319.tar.gz |
Add ability to reverse byte order of partition names to gdisk and sgdisk
-rw-r--r-- | NEWS | 11 | ||||
-rw-r--r-- | gdisk.8 | 8 | ||||
-rw-r--r-- | gptcl.cc | 11 | ||||
-rw-r--r-- | gptpart.cc | 10 | ||||
-rw-r--r-- | gptpart.h | 1 | ||||
-rw-r--r-- | gpttext.cc | 20 | ||||
-rw-r--r-- | gpttext.h | 1 | ||||
-rw-r--r-- | sgdisk.8 | 8 |
8 files changed, 66 insertions, 4 deletions
@@ -1,11 +1,20 @@ 1.0.8 (?/??/2021): ------------------ -- Fixed double byte swap operation on writes of partition label data on +- Fixed double byte swap operation on writes of partition name data on big-endian systems; this is in addition to the double byte swap fix on reading partition label data fixed in 1.0.7. (Thanks to Erik Larsson for both fixes.) +- Added feature to gdisk and sgdisk to enable swapping the byte order of + partition names, so as to correct disks already affected by the preceding + bug. This option is 'b' on the experts' menu in gdisk and + -b/--byte-swap-name in sgdisk. This seems advanced/obscure enough that I + don't want to clutter cgdisk's menu with this option, so I haven't added + it there. + +- Trivial code cleanup. + 1.0.7 (3/10/2021): ------------------ @@ -420,6 +420,14 @@ aren't translated into anything useful. In practice, most OSes seem to ignore these attributes. .TP +.B b +Swap the byte order for the name of the specified partition. Some +partitioning tools, including GPT fdisk 1.0.7 and earlier, can write the +partition name in the wrong byte order on big-endian computers, such as the +IBM s390 mainframes and PowerPC-based Macs. This feature corrects this +problem. + +.TP .B c Change partition GUID. You can enter a custom unique GUID for a partition using this option. (Note this refers to the GUID that uniquely identifies a @@ -64,6 +64,7 @@ int GPTDataCL::DoOptions(int argc, char* argv[]) { GPTData secondDevice; int opt, numOptions = 0, saveData = 0, neverSaveData = 0; int partNum = 0, newPartNum = -1, saveNonGPT = 1, retval = 0, pretend = 0; + int byteSwapPartNum = 0; uint64_t low, high, startSector, endSector, sSize, mainTableLBA; uint64_t temp; // temporary variable; free to use in any case char *device; @@ -76,6 +77,7 @@ int GPTDataCL::DoOptions(int argc, char* argv[]) { "list|[partnum:show|or|nand|xor|=|set|clear|toggle|get[:bitnum|hexbitmask]]"}, {"set-alignment", 'a', POPT_ARG_INT, &alignment, 'a', "set sector alignment", "value"}, {"backup", 'b', POPT_ARG_STRING, &backupFile, 'b', "backup GPT to file", "file"}, + {"byte-swap-name", 'B', POPT_ARG_INT, &byteSwapPartNum, 'B', "byte-swap partition's name", "partnum"}, {"change-name", 'c', POPT_ARG_STRING, &partName, 'c', "change partition's name", "partnum:name"}, {"recompute-chs", 'C', POPT_ARG_NONE, NULL, 'C', "recompute CHS values in protective/hybrid MBR", ""}, {"delete", 'd', POPT_ARG_INT, &deletePartNum, 'd', "delete a partition", "partnum"}, @@ -191,6 +193,15 @@ int GPTDataCL::DoOptions(int argc, char* argv[]) { case 'a': SetAlignment(alignment); break; + case 'B': + if (IsUsedPartNum(byteSwapPartNum - 1)) { + partitions[byteSwapPartNum - 1].ReverseNameBytes(); + cout << "Changed partition " << byteSwapPartNum << "'s name to " + << partitions[byteSwapPartNum - 1].GetDescription() << "\n"; + JustLooking(0); + saveData = 1; + } + break; case 'b': SaveGPTBackup(backupFile); free(backupFile); @@ -412,14 +412,18 @@ int GPTPart::DoTheyOverlap(const GPTPart & other) { // Reverse the bytes of integral data types and of the UTF-16LE name; // used on big-endian systems. void GPTPart::ReversePartBytes(void) { - int i; - ReverseBytes(&firstLBA, 8); ReverseBytes(&lastLBA, 8); ReverseBytes(&attributes, 8); + ReverseNameBytes(); +} // GPTPart::ReversePartBytes() + +void GPTPart::ReverseNameBytes(void) { + int i; + for (i = 0; i < NAME_SIZE; i ++ ) ReverseBytes(name + i, 2); -} // GPTPart::ReversePartBytes() +} // GPTPart::ReverseNameBytes() /**************************************** * Functions requiring user interaction * @@ -94,6 +94,7 @@ class GPTPart { void BlankPartition(void); // empty partition of data int DoTheyOverlap(const GPTPart & other); // returns 1 if there's overlap void ReversePartBytes(void); // reverse byte order of all integer fields + void ReverseNameBytes(void); // reverse byte order of partition's name field // Functions requiring user interaction void ChangeType(void); // Change the type code @@ -341,6 +341,22 @@ int GPTDataTextUI::SetName(uint32_t partNum) { return retval; } // GPTDataTextUI::SetName() +// Enable the user to byte-swap the name of the partition. Used to correct +// partition names damaged by incorrect byte order, as could be created by +// GPT fdisk 1.0.7 and earlier on big-endian systems, and perhaps other tools. +void GPTDataTextUI::ReverseName(uint32_t partNum) { + int swapBytes; + + cout << "Current name is: " << partitions[partNum].GetDescription() << "\n"; + partitions[partNum].ReverseNameBytes(); + cout << "Byte-swapped name is: " << partitions[partNum].GetDescription() << "\n"; + cout << "Do you want to byte-swap the name? "; + swapBytes = (GetYN() == 'Y'); + // Already swapped for display, so undo if necessary.... + if (!swapBytes) + partitions[partNum].ReverseNameBytes(); +} // GPTDataTextUI::ReverseName() + // Ask user for two partition numbers and swap them in the table. Note that // this just reorders table entries; it doesn't adjust partition layout on // the disk. @@ -799,6 +815,9 @@ void GPTDataTextUI::ExpertsMenu(string filename) { else cout << "No partitions\n"; break; + case 'b': case 'B': + ReverseName(GetPartNum()); + break; case 'c': case 'C': ChangeUniqueGuid(); break; @@ -896,6 +915,7 @@ void GPTDataTextUI::ExpertsMenu(string filename) { void GPTDataTextUI::ShowExpertCommands(void) { cout << "a\tset attributes\n"; + cout << "b\tbyte-swap a partition's name\n"; cout << "c\tchange partition GUID\n"; cout << "d\tdisplay the sector alignment value\n"; cout << "e\trelocate backup data structures to the end of the disk\n"; @@ -49,6 +49,7 @@ class GPTDataTextUI : public GPTData { void ChangeUniqueGuid(void); void SetAttributes(uint32_t partNum); int SetName(uint32_t partNum); + void ReverseName(uint32_t partNum); int SwapPartitions(void); int DestroyGPTwPrompt(void); // Returns 1 if user proceeds void ShowDetails(void); @@ -183,6 +183,14 @@ the backup may not accurately reflect the damaged state; instead, they will reflect GPT fdisk's first\-pass interpretation of the GPT. .TP +.B \-B, \-\-byte\-swap\-name=partnum +Swap the byte order for the name of the specified partition. Some +partitioning tools, including GPT fdisk 1.0.7 and earlier, can write the +partition name in the wrong byte order on big-endian computers, such as the +IBM s390 mainframes and PowerPC-based Macs. This feature corrects this +problem. + +.TP .B \-c, \-\-change\-name=partnum:name Change the GPT name of a partition. This name is encoded as a UTF\-16 string, but proper entry and display of anything beyond basic ASCII values |