poke-devel
[Top][All Lists]
Advanced

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

[PATCH] pickles: Add mbr.pk for MBR partition table


From: Mohammad-Reza Nabipoor
Subject: [PATCH] pickles: Add mbr.pk for MBR partition table
Date: Tue, 26 Jan 2021 09:15:00 +0330

2021-01-26  Mohammad-Reza Nabipoor  <m.nabipoor@yahoo.com>

        * pickles/mbr.pk: New pickle.
        * pickles/Makefile.am (dist_pickles_DATA): Add `mbr.pk`.
---

Hi, Jose!

A new pickle!
Everything is OK (from my PoV) except a weird constraint violation.

There's a problem in constraint of `magic` field of `MBR` struct.

  struct {
    /* ... */
    uint16 magic = 0xaa55UH;
  };

This gives constraint violation!
I even tried the following alternatives:
  - `uint16 magic : magic == 0xaa55UH;`
  - `byte[2] magic = [0x55UB, 0xaaUB];` and
  - `byte[2] magic : magic == [0x55UB, 0xaaUB];`



Regards,
Mohammad-Reza


 ChangeLog           |  5 +++
 pickles/Makefile.am |  2 +-
 pickles/mbr.pk      | 94 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 100 insertions(+), 1 deletion(-)
 create mode 100644 pickles/mbr.pk

diff --git a/ChangeLog b/ChangeLog
index 900a41d3..28356c29 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2021-01-26  Mohammad-Reza Nabipoor  <m.nabipoor@yahoo.com>
+
+       * pickles/mbr.pk: New pickle.
+       * pickles/Makefile.am (dist_pickles_DATA): Add `mbr.pk`.
+
 2021-01-25  Mohammad-Reza Nabipoor  <m.nabipoor@yahoo.com>
 
        * libpoke/pkl-rt.pk (exception_code): New function.
diff --git a/pickles/Makefile.am b/pickles/Makefile.am
index d5452e52..7dada3dd 100644
--- a/pickles/Makefile.am
+++ b/pickles/Makefile.am
@@ -2,4 +2,4 @@ picklesdir = $(pkgdatadir)/pickles
 dist_pickles_DATA = elf.pk ctf.pk leb128.pk bpf.pk btf.pk bmp.pk \
                     color.pk rgb24.pk id3v1.pk \
                     dwarf.pk dwarf-common.pk dwarf-frame.pk dwarf-pubnames.pk \
-                    dwarf-types.pk time.pk argp.pk pktest.pk
+                    dwarf-types.pk time.pk argp.pk pktest.pk mbr.pk
diff --git a/pickles/mbr.pk b/pickles/mbr.pk
new file mode 100644
index 00000000..f729096f
--- /dev/null
+++ b/pickles/mbr.pk
@@ -0,0 +1,94 @@
+/* mbr.pk - MBR (Master Boot Record) partition table.  */
+
+/* Copyright (C) 2021 The poke authors */
+
+/* This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* set_endian(ENDIAN_LITTLE); */
+
+type MBR_CHS =
+  struct
+  {
+    byte[3] chs;
+
+    method get_head = uint<8>:
+      {
+        return chs[0];
+      }
+    method get_sector = uint<6>:
+      {
+        return chs[1] & 0x3f;
+      }
+    method get_cylinder = uint<10>:
+      {
+        return (chs[1] & 0xc0) <<. 2 | chs[2];
+      }
+    method _print = void:
+      {
+        printf ("#<head=%u8d, sector=%u6d, cylinder=%u10d>",
+                get_head, get_sector, get_cylinder);
+      }
+  };
+
+/* MBR Partition Table Entry (PTE)
+ *
+ *  Offset  Size (bytes)  Description
+ *
+ *  0x00    1             Drive attributes (bit 7 set = active or bootable)
+ *  0x01    3             CHS Address of partition start
+ *  0x04    1             Partition type
+ *  0x05    3             CHS address of last partition sector
+ *  0x08    4             LBA of partition start
+ *  0x0C    4             Number of sectors in partition
+ *
+ * ref: https://wiki.osdev.org/MBR_(x86)
+ */
+type MBR_PTE =
+  struct
+  {
+    struct byte
+    {
+      uint<1> active;  /* bootable */
+      uint<7>;
+    } attr;
+    MBR_CHS start_chs;
+    byte typ;          /* type*/
+    MBR_CHS end_chs;
+    uint32 lba;
+    uint32 sector_count;
+  };
+
+/*
+ *  Offset  Size (bytes)  Description
+ *
+ *  0x000   440           MBR Bootstrap (flat binary executable code)
+ *  0x1B8   4             Optional "Unique Disk ID / Signature"
+ *  0x1BC   2             Optional, reserved 0x0000
+ *  0x1BE   16            First partition table entry
+ *  0x1CE   16            Second partition table entry
+ *  0x1DE   16            Third partition table entry
+ *  0x1EE   16            Fourth partition table entry
+ *  0x1FE   2             (0x55, 0xAA) "Valid bootsector" signature bytes
+ *
+ * ref: https://wiki.osdev.org/MBR_(x86)
+ */
+type MBR = struct
+  {
+    byte[440#B] bootstrap;
+    uint32 signature;
+    byte[2];  /* reserved */
+    MBR_PTE[4] pte;
+    uint16 magic; /* FIXME = 0xaa55UH */
+  };
-- 
2.30.0



reply via email to

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