1*30bcf84cSAndrew Jeffery // SPDX-License-Identifier: Apache-2.0
2*30bcf84cSAndrew Jeffery // Copyright (C) 2018 IBM Corp.
3*30bcf84cSAndrew Jeffery #include <assert.h>
4*30bcf84cSAndrew Jeffery #include <string.h>
5*30bcf84cSAndrew Jeffery 
6*30bcf84cSAndrew Jeffery #include "config.h"
7*30bcf84cSAndrew Jeffery #include "vpnor/pnor_partition_table.hpp"
8*30bcf84cSAndrew Jeffery 
9*30bcf84cSAndrew Jeffery extern "C" {
10*30bcf84cSAndrew Jeffery #include "test/mbox.h"
11*30bcf84cSAndrew Jeffery #include "test/system.h"
12*30bcf84cSAndrew Jeffery }
13*30bcf84cSAndrew Jeffery 
14*30bcf84cSAndrew Jeffery #include "vpnor/test/tmpd.hpp"
15*30bcf84cSAndrew Jeffery 
16*30bcf84cSAndrew Jeffery static const auto BLOCK_SIZE = 4 * 1024;
17*30bcf84cSAndrew Jeffery static const auto ERASE_SIZE = BLOCK_SIZE;
18*30bcf84cSAndrew Jeffery static const auto PNOR_SIZE = 64 * 1024 * 1024;
19*30bcf84cSAndrew Jeffery static const auto MEM_SIZE = PNOR_SIZE;
20*30bcf84cSAndrew Jeffery static const auto N_WINDOWS = 1;
21*30bcf84cSAndrew Jeffery static const auto WINDOW_SIZE = BLOCK_SIZE;
22*30bcf84cSAndrew Jeffery 
23*30bcf84cSAndrew Jeffery const std::string toc[] = {
24*30bcf84cSAndrew Jeffery     "partition01=HBB,00000000,00001000,80,ECC,PRESERVED",
25*30bcf84cSAndrew Jeffery };
26*30bcf84cSAndrew Jeffery constexpr auto partitionName = "HBB";
27*30bcf84cSAndrew Jeffery 
28*30bcf84cSAndrew Jeffery namespace test = openpower::virtual_pnor::test;
29*30bcf84cSAndrew Jeffery 
main()30*30bcf84cSAndrew Jeffery int main()
31*30bcf84cSAndrew Jeffery {
32*30bcf84cSAndrew Jeffery     struct mbox_context* ctx;
33*30bcf84cSAndrew Jeffery 
34*30bcf84cSAndrew Jeffery     system_set_reserved_size(MEM_SIZE);
35*30bcf84cSAndrew Jeffery     system_set_mtd_sizes(PNOR_SIZE, ERASE_SIZE);
36*30bcf84cSAndrew Jeffery     ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
37*30bcf84cSAndrew Jeffery     test::VpnorRoot root(ctx, toc, BLOCK_SIZE);
38*30bcf84cSAndrew Jeffery     const openpower::virtual_pnor::partition::Table table(ctx);
39*30bcf84cSAndrew Jeffery 
40*30bcf84cSAndrew Jeffery     pnor_partition_table expectedTable{};
41*30bcf84cSAndrew Jeffery     expectedTable.data.magic = PARTITION_HEADER_MAGIC;
42*30bcf84cSAndrew Jeffery     expectedTable.data.version = PARTITION_VERSION_1;
43*30bcf84cSAndrew Jeffery     expectedTable.data.size = 1;
44*30bcf84cSAndrew Jeffery     expectedTable.data.entry_size = sizeof(pnor_partition);
45*30bcf84cSAndrew Jeffery     expectedTable.data.entry_count = 1;
46*30bcf84cSAndrew Jeffery     expectedTable.data.block_size = BLOCK_SIZE;
47*30bcf84cSAndrew Jeffery     expectedTable.data.block_count =
48*30bcf84cSAndrew Jeffery         (PNOR_SIZE) / expectedTable.data.block_size;
49*30bcf84cSAndrew Jeffery     expectedTable.checksum =
50*30bcf84cSAndrew Jeffery         openpower::virtual_pnor::details::checksum(expectedTable.data);
51*30bcf84cSAndrew Jeffery 
52*30bcf84cSAndrew Jeffery     pnor_partition expectedPartition{};
53*30bcf84cSAndrew Jeffery     strcpy(expectedPartition.data.name, partitionName);
54*30bcf84cSAndrew Jeffery     expectedPartition.data.base = 0;
55*30bcf84cSAndrew Jeffery     expectedPartition.data.size = 1;
56*30bcf84cSAndrew Jeffery     expectedPartition.data.actual = 0x1000;
57*30bcf84cSAndrew Jeffery     expectedPartition.data.id = 1;
58*30bcf84cSAndrew Jeffery     expectedPartition.data.pid = PARENT_PATITION_ID;
59*30bcf84cSAndrew Jeffery     expectedPartition.data.type = PARTITION_TYPE_DATA;
60*30bcf84cSAndrew Jeffery     expectedPartition.data.flags = 0;
61*30bcf84cSAndrew Jeffery     expectedPartition.data.user.data[0] = PARTITION_ECC_PROTECTED;
62*30bcf84cSAndrew Jeffery     expectedPartition.data.user.data[1] |= PARTITION_PRESERVED;
63*30bcf84cSAndrew Jeffery     expectedPartition.data.user.data[1] |= PARTITION_VERSION_CHECK_SHA512;
64*30bcf84cSAndrew Jeffery     expectedPartition.checksum =
65*30bcf84cSAndrew Jeffery         openpower::virtual_pnor::details::checksum(expectedPartition.data);
66*30bcf84cSAndrew Jeffery 
67*30bcf84cSAndrew Jeffery     const pnor_partition_table& result = table.getNativeTable();
68*30bcf84cSAndrew Jeffery 
69*30bcf84cSAndrew Jeffery     auto rc = memcmp(&expectedTable, &result, sizeof(pnor_partition_table));
70*30bcf84cSAndrew Jeffery     assert(rc == 0);
71*30bcf84cSAndrew Jeffery 
72*30bcf84cSAndrew Jeffery     rc = memcmp(&expectedPartition, &result.partitions[0],
73*30bcf84cSAndrew Jeffery                 sizeof(pnor_partition));
74*30bcf84cSAndrew Jeffery     assert(rc == 0);
75*30bcf84cSAndrew Jeffery 
76*30bcf84cSAndrew Jeffery     const pnor_partition& first = table.partition(0);
77*30bcf84cSAndrew Jeffery     rc = memcmp(&first, &result.partitions[0], sizeof(pnor_partition));
78*30bcf84cSAndrew Jeffery     assert(rc == 0);
79*30bcf84cSAndrew Jeffery 
80*30bcf84cSAndrew Jeffery     return 0;
81*30bcf84cSAndrew Jeffery }
82