130bcf84cSAndrew Jeffery // SPDX-License-Identifier: Apache-2.0
230bcf84cSAndrew Jeffery // Copyright (C) 2018 IBM Corp.
3035ad76bSAndrew Jeffery 
4d5f1d40fSWilliam A. Kennington III #include "config.h"
5d5f1d40fSWilliam A. Kennington III 
630bcf84cSAndrew Jeffery extern "C" {
7035ad76bSAndrew Jeffery #include "backend.h"
830bcf84cSAndrew Jeffery #include "test/mbox.h"
930bcf84cSAndrew Jeffery #include "test/system.h"
1030bcf84cSAndrew Jeffery }
1130bcf84cSAndrew Jeffery 
12*de08ca2dSAndrew Jeffery #include "vpnor/table.hpp"
1330bcf84cSAndrew Jeffery #include "vpnor/test/tmpd.hpp"
1430bcf84cSAndrew Jeffery 
15035ad76bSAndrew Jeffery #include <cassert>
16035ad76bSAndrew Jeffery #include <cstring>
17035ad76bSAndrew Jeffery 
1830bcf84cSAndrew Jeffery static const auto BLOCK_SIZE = 4 * 1024;
1930bcf84cSAndrew Jeffery static const auto ERASE_SIZE = BLOCK_SIZE;
2030bcf84cSAndrew Jeffery static const auto PNOR_SIZE = 64 * 1024 * 1024;
2130bcf84cSAndrew Jeffery static const auto MEM_SIZE = PNOR_SIZE;
2230bcf84cSAndrew Jeffery static const auto N_WINDOWS = 1;
2330bcf84cSAndrew Jeffery static const auto WINDOW_SIZE = BLOCK_SIZE;
2430bcf84cSAndrew Jeffery 
2530bcf84cSAndrew Jeffery const std::string toc[] = {
2630bcf84cSAndrew Jeffery     "partition01=HBB,00000000,00001000,80,ECC,PRESERVED",
2730bcf84cSAndrew Jeffery };
2830bcf84cSAndrew Jeffery constexpr auto partitionName = "HBB";
2930bcf84cSAndrew Jeffery 
3030bcf84cSAndrew Jeffery namespace test = openpower::virtual_pnor::test;
3130bcf84cSAndrew Jeffery 
main()3230bcf84cSAndrew Jeffery int main()
3330bcf84cSAndrew Jeffery {
3430bcf84cSAndrew Jeffery     struct mbox_context* ctx;
3530bcf84cSAndrew Jeffery 
3630bcf84cSAndrew Jeffery     system_set_reserved_size(MEM_SIZE);
3730bcf84cSAndrew Jeffery     system_set_mtd_sizes(PNOR_SIZE, ERASE_SIZE);
38f1e547c7SEvan Lojewski     ctx = mbox_create_frontend_context(N_WINDOWS, WINDOW_SIZE);
39f1e547c7SEvan Lojewski     test::VpnorRoot root(&ctx->backend, toc, BLOCK_SIZE);
40f1e547c7SEvan Lojewski     const openpower::virtual_pnor::partition::Table table(&ctx->backend);
4130bcf84cSAndrew Jeffery 
4230bcf84cSAndrew Jeffery     pnor_partition_table expectedTable{};
4330bcf84cSAndrew Jeffery     expectedTable.data.magic = PARTITION_HEADER_MAGIC;
4430bcf84cSAndrew Jeffery     expectedTable.data.version = PARTITION_VERSION_1;
4530bcf84cSAndrew Jeffery     expectedTable.data.size = 1;
4630bcf84cSAndrew Jeffery     expectedTable.data.entry_size = sizeof(pnor_partition);
4730bcf84cSAndrew Jeffery     expectedTable.data.entry_count = 1;
4830bcf84cSAndrew Jeffery     expectedTable.data.block_size = BLOCK_SIZE;
4930bcf84cSAndrew Jeffery     expectedTable.data.block_count =
5030bcf84cSAndrew Jeffery         (PNOR_SIZE) / expectedTable.data.block_size;
5130bcf84cSAndrew Jeffery     expectedTable.checksum =
5230bcf84cSAndrew Jeffery         openpower::virtual_pnor::details::checksum(expectedTable.data);
5330bcf84cSAndrew Jeffery 
5430bcf84cSAndrew Jeffery     pnor_partition expectedPartition{};
5530bcf84cSAndrew Jeffery     strcpy(expectedPartition.data.name, partitionName);
5630bcf84cSAndrew Jeffery     expectedPartition.data.base = 0;
5730bcf84cSAndrew Jeffery     expectedPartition.data.size = 1;
5830bcf84cSAndrew Jeffery     expectedPartition.data.actual = 0x1000;
5930bcf84cSAndrew Jeffery     expectedPartition.data.id = 1;
6030bcf84cSAndrew Jeffery     expectedPartition.data.pid = PARENT_PATITION_ID;
6130bcf84cSAndrew Jeffery     expectedPartition.data.type = PARTITION_TYPE_DATA;
6230bcf84cSAndrew Jeffery     expectedPartition.data.flags = 0;
6330bcf84cSAndrew Jeffery     expectedPartition.data.user.data[0] = PARTITION_ECC_PROTECTED;
6430bcf84cSAndrew Jeffery     expectedPartition.data.user.data[1] |= PARTITION_PRESERVED;
6530bcf84cSAndrew Jeffery     expectedPartition.data.user.data[1] |= PARTITION_VERSION_CHECK_SHA512;
6630bcf84cSAndrew Jeffery     expectedPartition.checksum =
6730bcf84cSAndrew Jeffery         openpower::virtual_pnor::details::checksum(expectedPartition.data);
6830bcf84cSAndrew Jeffery 
6930bcf84cSAndrew Jeffery     const pnor_partition_table& result = table.getNativeTable();
7030bcf84cSAndrew Jeffery 
7130bcf84cSAndrew Jeffery     auto rc = memcmp(&expectedTable, &result, sizeof(pnor_partition_table));
7230bcf84cSAndrew Jeffery     assert(rc == 0);
7330bcf84cSAndrew Jeffery 
7430bcf84cSAndrew Jeffery     rc = memcmp(&expectedPartition, &result.partitions[0],
7530bcf84cSAndrew Jeffery                 sizeof(pnor_partition));
7630bcf84cSAndrew Jeffery     assert(rc == 0);
7730bcf84cSAndrew Jeffery 
7830bcf84cSAndrew Jeffery     const pnor_partition& first = table.partition(0);
7930bcf84cSAndrew Jeffery     rc = memcmp(&first, &result.partitions[0], sizeof(pnor_partition));
8030bcf84cSAndrew Jeffery     assert(rc == 0);
8130bcf84cSAndrew Jeffery 
8230bcf84cSAndrew Jeffery     return 0;
8330bcf84cSAndrew Jeffery }
84