Blame SOURCES/gdisk-1.0.3-byteswap.patch

cb466f
From 904ce2993978f6cfbec986e051e55e34ad5d7292 Mon Sep 17 00:00:00 2001
cb466f
From: =?UTF-8?q?Nikola=20Forr=C3=B3?= <nforro@redhat.com>
cb466f
Date: Wed, 29 Sep 2021 15:33:33 +0200
cb466f
Subject: [PATCH] Fix incorrect byte order of partition names on big-endian
cb466f
 systems
cb466f
cb466f
---
cb466f
 gdisk.8    |  8 ++++++++
cb466f
 gptcl.cc   | 11 +++++++++++
cb466f
 gptpart.cc | 13 +++++++------
cb466f
 gptpart.h  |  1 +
cb466f
 gpttext.cc | 20 ++++++++++++++++++++
cb466f
 gpttext.h  |  1 +
cb466f
 sgdisk.8   |  8 ++++++++
cb466f
 7 files changed, 56 insertions(+), 6 deletions(-)
cb466f
cb466f
diff --git a/gdisk.8 b/gdisk.8
cb466f
index c2cf83d..071756c 100644
cb466f
--- a/gdisk.8
cb466f
+++ b/gdisk.8
cb466f
@@ -416,6 +416,14 @@ set features for each partition. \fBgdisk\fR supports four attributes:
cb466f
 aren't translated into anything useful. In practice, most OSes seem to
cb466f
 ignore these attributes.
cb466f
 
cb466f
+.TP 
cb466f
+.B b
cb466f
+Swap the byte order for the name of the specified partition. Some
cb466f
+partitioning tools, including GPT fdisk 1.0.7 and earlier, can write the
cb466f
+partition name in the wrong byte order on big-endian computers, such as the
cb466f
+IBM s390 mainframes and PowerPC-based Macs. This feature corrects this
cb466f
+problem.
cb466f
+
cb466f
 .TP 
cb466f
 .B c
cb466f
 Change partition GUID. You can enter a custom unique GUID for a partition
cb466f
diff --git a/gptcl.cc b/gptcl.cc
cb466f
index 6c36738..58afc8a 100644
cb466f
--- a/gptcl.cc
cb466f
+++ b/gptcl.cc
cb466f
@@ -64,6 +64,7 @@ int GPTDataCL::DoOptions(int argc, char* argv[]) {
cb466f
    GPTData secondDevice;
cb466f
    int opt, numOptions = 0, saveData = 0, neverSaveData = 0;
cb466f
    int partNum = 0, newPartNum = -1, saveNonGPT = 1, retval = 0, pretend = 0;
cb466f
+   int byteSwapPartNum = 0;
cb466f
    uint64_t low, high, startSector, endSector, sSize, mainTableLBA;
cb466f
    uint64_t temp; // temporary variable; free to use in any case
cb466f
    char *device;
cb466f
@@ -76,6 +77,7 @@ int GPTDataCL::DoOptions(int argc, char* argv[]) {
cb466f
           "list|[partnum:show|or|nand|xor|=|set|clear|toggle|get[:bitnum|hexbitmask]]"},
cb466f
       {"set-alignment", 'a', POPT_ARG_INT, &alignment, 'a', "set sector alignment", "value"},
cb466f
       {"backup", 'b', POPT_ARG_STRING, &backupFile, 'b', "backup GPT to file", "file"},
cb466f
+      {"byte-swap-name", 'B',  POPT_ARG_INT, &byteSwapPartNum, 'B', "byte-swap partition's name", "partnum"},
cb466f
       {"change-name", 'c', POPT_ARG_STRING, &partName, 'c', "change partition's name", "partnum:name"},
cb466f
       {"recompute-chs", 'C', POPT_ARG_NONE, NULL, 'C', "recompute CHS values in protective/hybrid MBR", ""},
cb466f
       {"delete", 'd', POPT_ARG_INT, &deletePartNum, 'd', "delete a partition", "partnum"},
cb466f
@@ -191,6 +193,15 @@ int GPTDataCL::DoOptions(int argc, char* argv[]) {
cb466f
                case 'a':
cb466f
                   SetAlignment(alignment);
cb466f
                   break;
cb466f
+               case 'B':
cb466f
+                  if (IsUsedPartNum(byteSwapPartNum - 1)) {
cb466f
+                     partitions[byteSwapPartNum - 1].ReverseNameBytes();
cb466f
+                     cout << "Changed partition " << byteSwapPartNum << "'s name to "
cb466f
+                          << partitions[byteSwapPartNum - 1].GetDescription() << "\n";
cb466f
+                     JustLooking(0);
cb466f
+                     saveData = 1;
cb466f
+                  }
cb466f
+                  break;
cb466f
                case 'b':
cb466f
                   SaveGPTBackup(backupFile);
cb466f
                   free(backupFile);
cb466f
diff --git a/gptpart.cc b/gptpart.cc
cb466f
index 17d6f15..c2b6500 100644
cb466f
--- a/gptpart.cc
cb466f
+++ b/gptpart.cc
cb466f
@@ -234,7 +234,6 @@ void GPTPart::SetName(const string & theName) {
cb466f
       // then to utf16le
cb466f
       if ( uni < 0x10000 ) {
cb466f
          name[ pos ] = (uint16_t) uni ;
cb466f
-         if ( ! IsLittleEndian() ) ReverseBytes( name + pos , 2 ) ;
cb466f
          pos ++ ;
cb466f
       } // if
cb466f
       else {
cb466f
@@ -244,10 +243,8 @@ void GPTPart::SetName(const string & theName) {
cb466f
          } // if
cb466f
          uni -= 0x10000 ;
cb466f
          name[ pos ] = (uint16_t)( uni >> 10 ) | 0xd800 ;
cb466f
-         if ( ! IsLittleEndian() ) ReverseBytes( name + pos , 2 ) ;
cb466f
          pos ++ ;
cb466f
          name[ pos ] = (uint16_t)( uni & 0x3ff ) | 0xdc00 ;
cb466f
-         if ( ! IsLittleEndian() ) ReverseBytes( name + pos , 2 ) ;
cb466f
          pos ++ ;
cb466f
       }
cb466f
    } // for
cb466f
@@ -407,14 +404,18 @@ int GPTPart::DoTheyOverlap(const GPTPart & other) {
cb466f
 // Reverse the bytes of integral data types and of the UTF-16LE name;
cb466f
 // used on big-endian systems.
cb466f
 void GPTPart::ReversePartBytes(void) {
cb466f
-   int i;
cb466f
-
cb466f
    ReverseBytes(&firstLBA, 8);
cb466f
    ReverseBytes(&lastLBA, 8);
cb466f
    ReverseBytes(&attributes, 8);
cb466f
+   ReverseNameBytes();
cb466f
+} // GPTPart::ReversePartBytes()
cb466f
+
cb466f
+void GPTPart::ReverseNameBytes(void) {
cb466f
+   int i;
cb466f
+
cb466f
    for (i = 0; i < NAME_SIZE; i ++ )
cb466f
       ReverseBytes(name + i, 2);
cb466f
-} // GPTPart::ReverseBytes()
cb466f
+} // GPTPart::ReverseNameBytes()
cb466f
 
cb466f
 /****************************************
cb466f
  * Functions requiring user interaction *
cb466f
diff --git a/gptpart.h b/gptpart.h
cb466f
index 657b3f9..ac8a725 100644
cb466f
--- a/gptpart.h
cb466f
+++ b/gptpart.h
cb466f
@@ -93,6 +93,7 @@ class GPTPart {
cb466f
       void BlankPartition(void); // empty partition of data
cb466f
       int DoTheyOverlap(const GPTPart & other); // returns 1 if there's overlap
cb466f
       void ReversePartBytes(void); // reverse byte order of all integer fields
cb466f
+      void ReverseNameBytes(void); // reverse byte order of partition's name field
cb466f
 
cb466f
       // Functions requiring user interaction
cb466f
       void ChangeType(void); // Change the type code
cb466f
diff --git a/gpttext.cc b/gpttext.cc
cb466f
index 732d861..6de7121 100644
cb466f
--- a/gpttext.cc
cb466f
+++ b/gpttext.cc
cb466f
@@ -341,6 +341,22 @@ int GPTDataTextUI::SetName(uint32_t partNum) {
cb466f
    return retval;
cb466f
 } // GPTDataTextUI::SetName()
cb466f
 
cb466f
+// Enable the user to byte-swap the name of the partition. Used to correct
cb466f
+// partition names damaged by incorrect byte order, as could be created by
cb466f
+// GPT fdisk 1.0.7 and earlier on big-endian systems, and perhaps other tools.
cb466f
+void GPTDataTextUI::ReverseName(uint32_t partNum) {
cb466f
+   int swapBytes;
cb466f
+
cb466f
+   cout << "Current name is: " << partitions[partNum].GetDescription() << "\n";
cb466f
+   partitions[partNum].ReverseNameBytes();
cb466f
+   cout << "Byte-swapped name is: " << partitions[partNum].GetDescription() << "\n";
cb466f
+   cout << "Do you want to byte-swap the name? ";
cb466f
+   swapBytes = (GetYN() == 'Y');
cb466f
+   // Already swapped for display, so undo if necessary....
cb466f
+   if (!swapBytes)
cb466f
+      partitions[partNum].ReverseNameBytes();
cb466f
+} // GPTDataTextUI::ReverseName()
cb466f
+
cb466f
 // Ask user for two partition numbers and swap them in the table. Note that
cb466f
 // this just reorders table entries; it doesn't adjust partition layout on
cb466f
 // the disk.
cb466f
@@ -799,6 +815,9 @@ void GPTDataTextUI::ExpertsMenu(string filename) {
cb466f
             else
cb466f
                cout << "No partitions\n";
cb466f
             break;
cb466f
+         case 'b': case 'B':
cb466f
+            ReverseName(GetPartNum());
cb466f
+            break;
cb466f
          case 'c': case 'C':
cb466f
             ChangeUniqueGuid();
cb466f
             break;
cb466f
@@ -896,6 +915,7 @@ void GPTDataTextUI::ExpertsMenu(string filename) {
cb466f
 
cb466f
 void GPTDataTextUI::ShowExpertCommands(void) {
cb466f
    cout << "a\tset attributes\n";
cb466f
+   cout << "b\tbyte-swap a partition's name\n";
cb466f
    cout << "c\tchange partition GUID\n";
cb466f
    cout << "d\tdisplay the sector alignment value\n";
cb466f
    cout << "e\trelocate backup data structures to the end of the disk\n";
cb466f
diff --git a/gpttext.h b/gpttext.h
cb466f
index 98e59af..db27246 100644
cb466f
--- a/gpttext.h
cb466f
+++ b/gpttext.h
cb466f
@@ -49,6 +49,7 @@ class GPTDataTextUI : public GPTData {
cb466f
       void ChangeUniqueGuid(void);
cb466f
       void SetAttributes(uint32_t partNum);
cb466f
       int SetName(uint32_t partNum);
cb466f
+      void ReverseName(uint32_t partNum);
cb466f
       int SwapPartitions(void);
cb466f
       int DestroyGPTwPrompt(void); // Returns 1 if user proceeds
cb466f
       void ShowDetails(void);
cb466f
diff --git a/sgdisk.8 b/sgdisk.8
cb466f
index 2cb18b9..3bc51f2 100644
cb466f
--- a/sgdisk.8
cb466f
+++ b/sgdisk.8
cb466f
@@ -182,6 +182,14 @@ backup will reflect your changes. If the GPT data structures are damaged,
cb466f
 the backup may not accurately reflect the damaged state; instead, they
cb466f
 will reflect GPT fdisk's first\-pass interpretation of the GPT.
cb466f
 
cb466f
+.TP 
cb466f
+.B \-B, \-\-byte\-swap\-name=partnum
cb466f
+Swap the byte order for the name of the specified partition. Some
cb466f
+partitioning tools, including GPT fdisk 1.0.7 and earlier, can write the
cb466f
+partition name in the wrong byte order on big-endian computers, such as the
cb466f
+IBM s390 mainframes and PowerPC-based Macs. This feature corrects this
cb466f
+problem.
cb466f
+
cb466f
 .TP 
cb466f
 .B \-c, \-\-change\-name=partnum:name
cb466f
 Change the GPT name of a partition. This name is encoded as a UTF\-16
cb466f
-- 
cb466f
2.32.0
cb466f