Andrew Jeffery | 4fe996c | 2018-02-27 12:16:48 +1030 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2018 IBM Corp. |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 3 | #include "pnor_partition_table.hpp" |
| 4 | #include "config.h" |
| 5 | #include <assert.h> |
| 6 | #include <string.h> |
| 7 | #include <vector> |
| 8 | #include <fstream> |
| 9 | #include <experimental/filesystem> |
| 10 | |
Andrew Jeffery | d083efd | 2018-02-21 21:19:00 +1030 | [diff] [blame^] | 11 | #include "test/vpnor/tmpd.hpp" |
| 12 | |
Andrew Jeffery | 9ecef79 | 2018-02-21 17:13:20 +1030 | [diff] [blame] | 13 | static const auto BLOCK_SIZE = 4 * 1024; |
| 14 | static const auto PNOR_SIZE = 64 * 1024 * 1024; |
| 15 | |
| 16 | const std::string toc[] = { |
| 17 | "partition01=HBB,00000000,00000400,80,ECC,PRESERVED", |
| 18 | }; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 19 | constexpr auto partitionName = "HBB"; |
Andrew Jeffery | 9ecef79 | 2018-02-21 17:13:20 +1030 | [diff] [blame] | 20 | |
| 21 | namespace fs = std::experimental::filesystem; |
| 22 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 23 | int main() |
| 24 | { |
Andrew Jeffery | 9ecef79 | 2018-02-21 17:13:20 +1030 | [diff] [blame] | 25 | char tmplt[] = "/tmp/vpnor_partitions.XXXXXX"; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 26 | char* tmpdir = mkdtemp(tmplt); |
| 27 | assert(tmpdir != nullptr); |
Andrew Jeffery | 9ecef79 | 2018-02-21 17:13:20 +1030 | [diff] [blame] | 28 | fs::path root{tmpdir}; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 29 | |
Andrew Jeffery | d083efd | 2018-02-21 21:19:00 +1030 | [diff] [blame^] | 30 | openpower::virtual_pnor::test::createVpnorTree(root, toc, BLOCK_SIZE); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 31 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 32 | const openpower::virtual_pnor::partition::Table table( |
Andrew Jeffery | 9ecef79 | 2018-02-21 17:13:20 +1030 | [diff] [blame] | 33 | fs::path{tmpdir}, BLOCK_SIZE, PNOR_SIZE); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 34 | |
| 35 | pnor_partition_table expectedTable{}; |
| 36 | expectedTable.data.magic = PARTITION_HEADER_MAGIC; |
| 37 | expectedTable.data.version = PARTITION_VERSION_1; |
| 38 | expectedTable.data.size = 1; // 1 block |
| 39 | expectedTable.data.entry_size = sizeof(pnor_partition); |
| 40 | expectedTable.data.entry_count = 1; // 1 partition |
Andrew Jeffery | 9ecef79 | 2018-02-21 17:13:20 +1030 | [diff] [blame] | 41 | expectedTable.data.block_size = BLOCK_SIZE; |
Deepak Kodihalli | d1d7930 | 2017-07-11 23:01:39 -0500 | [diff] [blame] | 42 | expectedTable.data.block_count = |
Andrew Jeffery | 9ecef79 | 2018-02-21 17:13:20 +1030 | [diff] [blame] | 43 | (PNOR_SIZE) / expectedTable.data.block_size; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 44 | expectedTable.checksum = |
| 45 | openpower::virtual_pnor::details::checksum(expectedTable.data); |
| 46 | |
| 47 | pnor_partition expectedPartition{}; |
| 48 | strcpy(expectedPartition.data.name, partitionName); |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 49 | expectedPartition.data.base = 0; // starts at offset 0 |
| 50 | expectedPartition.data.size = 1; // 1 block |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 51 | expectedPartition.data.actual = 0x400; // 1024 bytes |
| 52 | expectedPartition.data.id = 1; |
| 53 | expectedPartition.data.pid = PARENT_PATITION_ID; |
| 54 | expectedPartition.data.type = PARTITION_TYPE_DATA; |
| 55 | expectedPartition.data.flags = 0; |
| 56 | expectedPartition.data.user.data[0] = PARTITION_ECC_PROTECTED; |
| 57 | expectedPartition.data.user.data[1] |= PARTITION_PRESERVED; |
Deepak Kodihalli | e8b0e8a | 2017-07-12 10:01:31 -0500 | [diff] [blame] | 58 | expectedPartition.data.user.data[1] |= PARTITION_VERSION_CHECK_SHA512; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 59 | expectedPartition.checksum = |
| 60 | openpower::virtual_pnor::details::checksum(expectedPartition.data); |
| 61 | |
| 62 | const pnor_partition_table& result = table.getNativeTable(); |
| 63 | |
| 64 | fs::remove_all(fs::path{tmpdir}); |
| 65 | |
| 66 | auto rc = memcmp(&expectedTable, &result, sizeof(pnor_partition_table)); |
| 67 | assert(rc == 0); |
| 68 | |
| 69 | rc = memcmp(&expectedPartition, &result.partitions[0], |
| 70 | sizeof(pnor_partition)); |
| 71 | assert(rc == 0); |
| 72 | |
Adriana Kobylak | 2ad2132 | 2017-11-28 14:59:19 -0600 | [diff] [blame] | 73 | const pnor_partition& first = table.partition(0); // Partition at offset 0 |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 74 | rc = memcmp(&first, &result.partitions[0], sizeof(pnor_partition)); |
| 75 | assert(rc == 0); |
| 76 | |
| 77 | return 0; |
| 78 | } |