pnor_partition_table: Rework semantics of Table::size()

Table::size() now returns the exact table size in bytes,
Table::capacity() returns the block-aligned size in bytes (capacity in
terms of how much the table could grow before expanding to another
block), and Table::blocks() returns the size in blocks.

This helps out with code clarity around the codebase and enables the
introduction of ToC-related integration tests.

The one wrinkle is vpnor_get_partition_table_size(), which is modified
to call Table::blocks() but retains 'size' in its name. This is largely
unimportant as the function will go away shortly.

Change-Id: I3becf47f2201df5fe0bed86fcb92d7b94d06ab11
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/mboxd_pnor_partition_table.cpp b/mboxd_pnor_partition_table.cpp
index 40d2ea6..39a4529 100644
--- a/mboxd_pnor_partition_table.cpp
+++ b/mboxd_pnor_partition_table.cpp
@@ -64,7 +64,7 @@
 
 size_t vpnor_get_partition_table_size(const struct mbox_context *context)
 {
-    return context && context->vpnor ? context->vpnor->table->size() : 0;
+    return context && context->vpnor ? context->vpnor->table->blocks() : 0;
 }
 
 const struct pnor_partition_table *
@@ -118,11 +118,11 @@
         memcpy(&local.paths, &context->paths, sizeof(local.paths));
 
         size_t tocOffset = 0;
-        uint32_t tocSize = blTable.size() * eraseSize;
 
         // Copy TOC
         copy_flash(&local, tocOffset,
-                   static_cast<uint8_t *>(context->mem) + tocStart, tocSize);
+                   static_cast<uint8_t *>(context->mem) + tocStart,
+                   blTable.capacity());
         const pnor_partition &partition = blTable.partition(blPartitionName);
         size_t hbbOffset = partition.data.base * eraseSize;
         uint32_t hbbSize = partition.data.actual;
diff --git a/pnor_partition_table.cpp b/pnor_partition_table.cpp
index 54bb347..eb956d6 100644
--- a/pnor_partition_table.cpp
+++ b/pnor_partition_table.cpp
@@ -21,8 +21,8 @@
 {
 
 Table::Table(fs::path&& directory, size_t blockSize, size_t pnorSize) :
-    szBlocks(0), directory(std::move(directory)), numParts(0),
-    blockSize(blockSize), pnorSize(pnorSize)
+    szBytes(sizeof(pnor_partition_table)), directory(std::move(directory)),
+    numParts(0), blockSize(blockSize), pnorSize(pnorSize)
 {
     preparePartitions();
     prepareHeader();
@@ -34,7 +34,7 @@
     decltype(auto) table = getNativeTable();
     table.data.magic = PARTITION_HEADER_MAGIC;
     table.data.version = PARTITION_VERSION_1;
-    table.data.size = szBlocks;
+    table.data.size = blocks();
     table.data.entry_size = sizeof(pnor_partition);
     table.data.entry_count = numParts;
     table.data.block_size = blockSize;
@@ -62,11 +62,8 @@
         }
     }
 
-    size_t totalSizeBytes =
-        sizeof(pnor_partition_table) + (num * sizeof(pnor_partition));
-    size_t totalSizeAligned = align_up(totalSizeBytes, blockSize);
-    szBlocks = totalSizeAligned / blockSize;
-    tbl.resize(totalSizeAligned);
+    szBytes = sizeof(pnor_partition_table) + (num * sizeof(pnor_partition));
+    tbl.resize(capacity());
 }
 
 void Table::preparePartitions()
diff --git a/pnor_partition_table.hpp b/pnor_partition_table.hpp
index fae03a9..d19ff4b 100644
--- a/pnor_partition_table.hpp
+++ b/pnor_partition_table.hpp
@@ -4,6 +4,7 @@
 #include <memory>
 #include <numeric>
 #include <experimental/filesystem>
+#include "common.h"
 #include "pnor_partition_defs.h"
 
 namespace openpower
@@ -102,13 +103,34 @@
     Table& operator=(Table&&) = delete;
     ~Table() = default;
 
-    /** @brief Return size of partition table
+    /** @brief Return the exact size of partition table in bytes
      *
-     *  @returns size_t - size of partition table in blocks
+     *  @returns size_t - size of partition table in bytes
      */
     size_t size() const
     {
-        return szBlocks;
+        return szBytes;
+    }
+
+    /** @brief Return aligned size of partition table in bytes
+     *
+     *  The value returned will be greater-than or equal to size(), and
+     *  aligned to blockSize.
+     *
+     *  @returns size_t - capacity of partition table in bytes
+     */
+    size_t capacity() const
+    {
+        return align_up(szBytes, blockSize);
+    }
+
+    /** @brief Return the size of partition table in blocks
+     *
+     *  @returns size_t - size of partition table in blocks
+     */
+    size_t blocks() const
+    {
+        return capacity() / blockSize;
     }
 
     /** @brief Return a partition table having byte-ordering
@@ -181,9 +203,8 @@
     /** @brief Size of the PNOR partition table -
      *         sizeof(pnor_partition_table) +
      *         (no. of partitions * sizeof(pnor_partition)),
-     *         measured in erase-blocks.
      */
-    size_t szBlocks;
+    size_t szBytes;
 
     /** @brief Partition table */
     PartitionTable tbl;