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