poke-devel
[Top][All Lists]
Advanced

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

[PATCH 2/3] pickles: improve MBR pickle


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

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

        * pickles/mbr.pk (MBR_SectorSize): New unit.
        (MBR_PartitionType): Change it to integral struct.
        (MBR_PTE): Change `lba' and `sector_count' to little-endian offset
        type.  Add `lba_end' method.
        (MBR): Mark `signature' and `magic' as little-endian.
        * testsuite/poke.pickles/mbr-test.pk (SAMPLE_MBR): New sample data.
        ("Sample MBR verification"): New test to verify sample data.
---
 ChangeLog                          |  10 ++
 pickles/mbr.pk                     |  16 ++--
 testsuite/poke.pickles/mbr-test.pk | 144 +++++++++++++++++++++++++++++
 3 files changed, 163 insertions(+), 7 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 0b6ec7a0..15398781 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2023-09-10  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
+
+       * pickles/mbr.pk (MBR_SectorSize): New unit.
+       (MBR_PartitionType): Change it to integral struct.
+       (MBR_PTE): Change `lba' and `sector_count' to little-endian offset
+       type.  Add `lba_end' method.
+       (MBR): Mark `signature' and `magic' as little-endian.
+       * testsuite/poke.pickles/mbr-test.pk (SAMPLE_MBR): New sample data.
+       ("Sample MBR verification"): New test to verify sample data.
+
 2023-09-09  Denis Maryin  <des.maryin@googlemail.com>
 
        * pickles/Makefile.am (dist_pickles_DATA): Add new pickles.
diff --git a/pickles/mbr.pk b/pickles/mbr.pk
index f602ff6a..d00c549a 100644
--- a/pickles/mbr.pk
+++ b/pickles/mbr.pk
@@ -16,8 +16,6 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* set_endian(ENDIAN_LITTLE); */
-
 type MBR_CHS =
   struct
   {
@@ -81,7 +79,7 @@ var MBR_PT_Empty           = 0x00UB,
     MBR_PT_IBM_OS2         = 0xFBUB;
 
 type MBR_PartitionType =
-  struct
+  struct uint<8>
   {
     uint<8> p_type;
     method _print = void:
@@ -124,6 +122,7 @@ type MBR_PartitionType =
       }
   };
 
+unit MBR_SectorSize = 512 * 8;  /* 512 bytes (by convention).  */
 
 /* MBR Partition Table Entry (PTE)
  *
@@ -154,8 +153,11 @@ type MBR_PTE =
     MBR_CHS start_chs;
     MBR_PartitionType part_type;
     MBR_CHS end_chs;
-    uint<32> lba;
-    uint<32> sector_count;
+    little offset<uint<32>,MBR_SectorSize> lba;
+    little offset<uint<32>,MBR_SectorSize> sector_count;
+
+    method lba_end = offset<uint<32>,MBR_SectorSize>:
+      { return lba + sector_count; }
   };
 
 /*
@@ -176,8 +178,8 @@ type MBR =
   struct
   {
     uint<8>[440#B] bootstrap;
-    uint<32> signature;
+    little uint<32> signature;
     uint<8>[2] reserved;
     MBR_PTE[4] pte;
-    uint<16> magic == 0xaa55UH;
+    little uint<16> magic == 0xaa55UH;
   };
diff --git a/testsuite/poke.pickles/mbr-test.pk 
b/testsuite/poke.pickles/mbr-test.pk
index a8be8008..d243f856 100644
--- a/testsuite/poke.pickles/mbr-test.pk
+++ b/testsuite/poke.pickles/mbr-test.pk
@@ -23,6 +23,88 @@ set_endian(ENDIAN_LITTLE);
 
 var data = open ("*data*");
 
+/* 4 primary partition.
+   mbr_fdisk
+     :disk_identifier 0x23bdaa60
+     :partitions [
+       part :start 2048#4096 :end 22527#4096 :sectors 20480
+            :size 10#MiB :id 83 :typ "Linux";
+       // 1#MiB gap.
+       part :start 24576#4096 :end 40959#4096 :sectors 16384
+            :size 8#MiB :id 83 :typ "Linux";
+       part :start 40960#4096 :end 81919#4096 :sectors 40960
+            :size 20#MiB :id 83 :typ "Linux";
+       part :start 81920#4096 :end 198655#4096 :sectors 116736
+            :size 57#MiB :id 83 :typ "Linux";
+     ];
+*/
+var SAMPLE_MBR = [
+  0xfaUB, 0xb8UB, 0x00UB, 0x10UB, 0x8eUB, 0xd0UB, 0xbcUB, 0x00UB,
+  0xb0UB, 0xb8UB, 0x00UB, 0x00UB, 0x8eUB, 0xd8UB, 0x8eUB, 0xc0UB,
+  0xfbUB, 0xbeUB, 0x00UB, 0x7cUB, 0xbfUB, 0x00UB, 0x06UB, 0xb9UB,
+  0x00UB, 0x02UB, 0xf3UB, 0xa4UB, 0xeaUB, 0x21UB, 0x06UB, 0x00UB,
+  0x00UB, 0xbeUB, 0xbeUB, 0x07UB, 0x38UB, 0x04UB, 0x75UB, 0x0bUB,
+  0x83UB, 0xc6UB, 0x10UB, 0x81UB, 0xfeUB, 0xfeUB, 0x07UB, 0x75UB,
+  0xf3UB, 0xebUB, 0x16UB, 0xb4UB, 0x02UB, 0xb0UB, 0x01UB, 0xbbUB,
+  0x00UB, 0x7cUB, 0xb2UB, 0x80UB, 0x8aUB, 0x74UB, 0x01UB, 0x8bUB,
+  0x4cUB, 0x02UB, 0xcdUB, 0x13UB, 0xeaUB, 0x00UB, 0x7cUB, 0x00UB,
+  0x00UB, 0xebUB, 0xfeUB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x60UB, 0xaaUB, 0xbdUB, 0x23UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x01UB, 0x10UB, 0x83UB, 0x03UB, 0x20UB, 0xafUB, 0x00UB, 0x08UB,
+  0x00UB, 0x00UB, 0x00UB, 0x50UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x01UB, 0xc0UB, 0x83UB, 0x03UB, 0x60UB, 0x3fUB, 0x00UB, 0x60UB,
+  0x00UB, 0x00UB, 0x00UB, 0x40UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x41UB, 0x40UB, 0x83UB, 0x03UB, 0xa0UB, 0x7fUB, 0x00UB, 0xa0UB,
+  0x00UB, 0x00UB, 0x00UB, 0xa0UB, 0x00UB, 0x00UB, 0x00UB, 0x00UB,
+  0x81UB, 0x80UB, 0x83UB, 0x03UB, 0xe0UB, 0xffUB, 0x00UB, 0x40UB,
+  0x01UB, 0x00UB, 0x00UB, 0xc8UB, 0x01UB, 0x00UB, 0x55UB, 0xaaUB,
+];
+
 var tests = [
   PkTest {
     name = "magic number",
@@ -108,6 +190,68 @@ var tests = [
                [0xa5UB, 0x7fUB, 0x81UB]);
       },
   },
+  PkTest {
+    name = "Sample MBR verification",
+    func = lambda (string name) void:
+      {
+        for (end in [ENDIAN_BIG, ENDIAN_LITTLE])
+          with_temp_ios
+            :endian end
+            :do lambda void:
+              {
+                uint<8>[512#B] @ 0#B = SAMPLE_MBR;
+
+                var mbr = MBR @ 0#B;
+
+                assert (mbr.signature == 0x23bdaa60);
+                assert (mbr.reserved == [0UB, 0UB]);
+
+                /* TODO mbr.pte[0].start_chs  */
+                /* TODO mbr.pte[0].end_chs  */
+                assert (mbr.pte[0].part_type == 0x83 as MBR_PartitionType);
+                assert (mbr.pte[0].part_type
+                        == MBR_PT_LinuxNative as MBR_PartitionType);
+                assert (mbr.pte[0].lba == 2048#MBR_SectorSize);
+                assert (mbr.pte[0].sector_count == 20480#MBR_SectorSize);
+                assert (mbr.pte[0].sector_count == 10#MiB);
+                assert (mbr.pte[0].lba == 1#MiB);
+                assert (mbr.pte[0].lba_end == 11#MiB);
+
+                /* TODO mbr.pte[1].start_chs  */
+                /* TODO mbr.pte[1].end_chs  */
+                assert (mbr.pte[1].part_type == 0x83 as MBR_PartitionType);
+                assert (mbr.pte[1].part_type
+                        == MBR_PT_LinuxNative as MBR_PartitionType);
+                assert (mbr.pte[1].lba == 24576#MBR_SectorSize);
+                assert (mbr.pte[1].sector_count == 16384#MBR_SectorSize);
+                assert (mbr.pte[1].sector_count == 8#MiB);
+                assert (mbr.pte[1].lba == 11#MiB + /*gap*/ 1#MiB);
+                assert (mbr.pte[1].lba_end == 20#MiB);
+
+                /* TODO mbr.pte[2].start_chs  */
+                /* TODO mbr.pte[2].end_chs  */
+                assert (mbr.pte[2].part_type == 0x83 as MBR_PartitionType);
+                assert (mbr.pte[2].part_type
+                        == MBR_PT_LinuxNative as MBR_PartitionType);
+                assert (mbr.pte[2].lba == 40960#MBR_SectorSize);
+                assert (mbr.pte[2].sector_count == 40960#MBR_SectorSize);
+                assert (mbr.pte[2].sector_count == 20#MiB);
+                assert (mbr.pte[2].lba == 20#MiB);
+                assert (mbr.pte[2].lba_end == 40#MiB);
+
+                /* TODO mbr.pte[3].start_chs  */
+                /* TODO mbr.pte[3].end_chs  */
+                assert (mbr.pte[3].part_type == 0x83 as MBR_PartitionType);
+                assert (mbr.pte[3].part_type
+                        == MBR_PT_LinuxNative as MBR_PartitionType);
+                assert (mbr.pte[3].lba == 81920#MBR_SectorSize);
+                assert (mbr.pte[3].sector_count == 116736#MBR_SectorSize);
+                assert (mbr.pte[3].sector_count == 57#MiB);
+                assert (mbr.pte[3].lba == 40#MiB);
+                assert (mbr.pte[3].lba_end == 97#MiB); // 97.66 MiB
+              };
+      },
+  },
 ];
 
 var ec = pktest_run (tests) ? 0 : 1;
-- 
2.42.0




reply via email to

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