blob: d542299c632ebc2dbf25fe6ee801d63d68b77db8 [file] [log] [blame]
Deepak Kodihalli393821d2017-04-28 04:44:38 -05001#include "pnor_partition_table.hpp"
2#include "config.h"
3#include <assert.h>
4#include <string.h>
5#include <vector>
6#include <fstream>
7#include <experimental/filesystem>
8
Deepak Kodihallie8b0e8a2017-07-12 10:01:31 -05009constexpr auto line = "partition01=HBB,00000000,00000400,80,ECC,PRESERVED";
Deepak Kodihalli393821d2017-04-28 04:44:38 -050010constexpr auto partitionName = "HBB";
11char tmplt[] = "/tmp/vpnor_partitions.XXXXXX";
12
13int main()
14{
15 namespace fs = std::experimental::filesystem;
16
17 char* tmpdir = mkdtemp(tmplt);
18 assert(tmpdir != nullptr);
19
20 fs::path tocFilePath{tmpdir};
21 tocFilePath /= PARTITION_TOC_FILE;
22 std::ofstream tocFile(tocFilePath.c_str());
23 tocFile.write(line, strlen(line));
24 tocFile.close();
25
26 fs::path partitionFilePath{tmpdir};
27 partitionFilePath /= partitionName;
28 std::ofstream partitionFile(partitionFilePath.c_str());
29 std::vector<char> empty(1, 0); // 1 byte file
30 partitionFile.write(empty.data(), empty.size());
31 partitionFile.close();
32
Deepak Kodihallid1d79302017-07-11 23:01:39 -050033 const openpower::virtual_pnor::partition::Table
34 table(fs::path{tmpdir}, 4 * 1024, 64 * 1024 * 1024);
Deepak Kodihalli393821d2017-04-28 04:44:38 -050035
36 pnor_partition_table expectedTable{};
37 expectedTable.data.magic = PARTITION_HEADER_MAGIC;
38 expectedTable.data.version = PARTITION_VERSION_1;
39 expectedTable.data.size = 1; // 1 block
40 expectedTable.data.entry_size = sizeof(pnor_partition);
41 expectedTable.data.entry_count = 1; // 1 partition
42 expectedTable.data.block_size = 4096;
Deepak Kodihallid1d79302017-07-11 23:01:39 -050043 expectedTable.data.block_count =
44 (64 * 1024 * 1024) / expectedTable.data.block_size;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050045 expectedTable.checksum =
46 openpower::virtual_pnor::details::checksum(expectedTable.data);
47
48 pnor_partition expectedPartition{};
49 strcpy(expectedPartition.data.name, partitionName);
Adriana Kobylak2ad21322017-11-28 14:59:19 -060050 expectedPartition.data.base = 0; // starts at offset 0
Deepak Kodihalli393821d2017-04-28 04:44:38 -050051 expectedPartition.data.size = 1; // 1 block
52 expectedPartition.data.actual = 0x400; // 1024 bytes
53 expectedPartition.data.id = 1;
54 expectedPartition.data.pid = PARENT_PATITION_ID;
55 expectedPartition.data.type = PARTITION_TYPE_DATA;
56 expectedPartition.data.flags = 0;
57 expectedPartition.data.user.data[0] = PARTITION_ECC_PROTECTED;
58 expectedPartition.data.user.data[1] |= PARTITION_PRESERVED;
Deepak Kodihallie8b0e8a2017-07-12 10:01:31 -050059 expectedPartition.data.user.data[1] |= PARTITION_VERSION_CHECK_SHA512;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050060 expectedPartition.checksum =
61 openpower::virtual_pnor::details::checksum(expectedPartition.data);
62
63 const pnor_partition_table& result = table.getNativeTable();
64
65 fs::remove_all(fs::path{tmpdir});
66
67 auto rc = memcmp(&expectedTable, &result, sizeof(pnor_partition_table));
68 assert(rc == 0);
69
70 rc = memcmp(&expectedPartition, &result.partitions[0],
71 sizeof(pnor_partition));
72 assert(rc == 0);
73
Adriana Kobylak2ad21322017-11-28 14:59:19 -060074 const pnor_partition& first = table.partition(0); // Partition at offset 0
Deepak Kodihalli393821d2017-04-28 04:44:38 -050075 rc = memcmp(&first, &result.partitions[0], sizeof(pnor_partition));
76 assert(rc == 0);
77
78 return 0;
79}