poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 3/3] pickles: improve GPT pickle


From: Mohammad-Reza Nabipoor
Subject: [PATCH 3/3] pickles: improve GPT pickle
Date: Sun, 10 Sep 2023 02:27:16 +0330

2023-09-10  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>

        * pickles/gpt.pk (GPT_SECTOR_SIZE): Remove.
        (GPT_SectorSize): New unit.
        (GPT_LBA): New type.
        (GPT_Header): Change type of fields to offset when appropriate.
        (GPT_Partition_Entry): Likewise.
        (GPT): Update.
        * testsuite/poke.pickles/gpt-test.pk (tests): Likewise.
---
 ChangeLog                          | 10 ++++++++++
 pickles/gpt.pk                     | 32 +++++++++++++++++-------------
 testsuite/poke.pickles/gpt-test.pk |  6 +++---
 3 files changed, 31 insertions(+), 17 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 15398781..cb06b9b2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2023-09-10  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
+
+       * pickles/gpt.pk (GPT_SECTOR_SIZE): Remove.
+       (GPT_SectorSize): New unit.
+       (GPT_LBA): New type.
+       (GPT_Header): Change type of fields to offset when appropriate.
+       (GPT_Partition_Entry): Likewise.
+       (GPT): Update.
+       * testsuite/poke.pickles/gpt-test.pk (tests): Likewise.
+
 2023-09-10  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
 
        * pickles/mbr.pk (MBR_SectorSize): New unit.
diff --git a/pickles/gpt.pk b/pickles/gpt.pk
index ebbf182b..57b53b1d 100644
--- a/pickles/gpt.pk
+++ b/pickles/gpt.pk
@@ -24,29 +24,33 @@ load "guid.pk";
 load "gpt-partition-types.pk";
 load "gpt-partition-attrs.pk";
 
-var GPT_SECTOR_SIZE = 512#B;
+unit GPT_SectorSize = 512 * 8;  /* 512 bytes (by convention).  */
+
+assert (#GPT_SectorSize == #MBR_SectorSize);
+
+type GPT_LBA = offset<uint<64>,GPT_SectorSize>;
 
 type GPT_Header =
   struct
   {
     uint<8>[8] signature == ['E', 'F', 'I', ' ', 'P', 'A', 'R', 'T'];
     uint<8>[4] revision_magic;
-    little uint<32> header_size == 92;
+    little offset<uint<32>,B> header_size == 92#B;
     little uint<32> header_crc;
     little uint<32> reserved_zero1 == 0;
-    little uint<64> current_lba;
-    little uint<64> backup_lba;
-    little uint<64> first_usable_lba;
-    little uint<64> last_usable_lba;
+    little GPT_LBA current_lba;
+    little GPT_LBA backup_lba;
+    little GPT_LBA first_usable_lba;
+    little GPT_LBA last_usable_lba;
     GUID disk_guid;
-    little uint<64> partition_entries_start_lba;
+    little GPT_LBA partition_entries_start_lba;
     little uint<32> partition_entries_count;
-    little uint<32> partition_entry_size;
+    little offset<uint<32>,B> partition_entry_size == 128#B;
     little uint<32> partition_entries_crc;
-    uint<8>[GPT_SECTOR_SIZE - 92#B] padding : OFFSET == 92#B;
+    uint<8>[1#GPT_SectorSize - 92#B] padding : OFFSET == 92#B;
   };
 
-assert ((GPT_Header {})'size == GPT_SECTOR_SIZE);
+assert (#GPT_Header == #GPT_SectorSize);
 
 type GPT_Partition_Name =
   struct
@@ -72,8 +76,8 @@ type GPT_Partition_Entry =
   {
     GPT_PartitionType_GUID pt_guid;
     GUID this_partition_guid;
-    little uint<64> first_lba;
-    little uint<64> last_lba;
+    little GPT_LBA first_lba;
+    little GPT_LBA last_lba;
     GPT_Partition_Attributes attribute_flags;
     GPT_Partition_Name partition_name : OFFSET == 56#B;
   };
@@ -84,7 +88,7 @@ type GPT =
   struct
   {
     MBR protective_mbr;
-    GPT_Header header : OFFSET == GPT_SECTOR_SIZE;
+    GPT_Header header;
     GPT_Partition_Entry[header.partition_entries_count] partitions
-        @ GPT_SECTOR_SIZE * header.partition_entries_start_lba;
+        @ header.partition_entries_start_lba;
   };
diff --git a/testsuite/poke.pickles/gpt-test.pk 
b/testsuite/poke.pickles/gpt-test.pk
index ce0b3998..d91b1a79 100644
--- a/testsuite/poke.pickles/gpt-test.pk
+++ b/testsuite/poke.pickles/gpt-test.pk
@@ -55,13 +55,13 @@ var tests = [
               uint<32> @ (512 + 12)#B = 92U;
               /* Fix GPT header reserved-must-be-zero.  */
               uint<32> @ (512 + 20)#B = 0U;
+              /* Fix GPT header `partition_entry_size'.  */
+              offset<uint<32>,B> @ (512 + 84)#B = 128#B;
 
               /* Must not fail now.  */
               var g = GPT @ 0#B;
 
-              /* Check for random field zero.  */
-              assert(g.header.partition_entry_size == 0,
-                     "Must be zero for uninitialized GPT");
+              assert(g.header.partition_entry_size == 128#B);
           };
       },
   },
-- 
2.42.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]