Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 1 | #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 | |
| 9 | constexpr auto line = "partition01=HBB,00000000,00000400,ECC,PRESERVED"; |
| 10 | constexpr auto partitionName = "HBB"; |
| 11 | char tmplt[] = "/tmp/vpnor_partitions.XXXXXX"; |
| 12 | |
| 13 | int 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 | |
| 33 | const openpower::virtual_pnor::partition::Table table(fs::path{tmpdir}); |
| 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 |
| 41 | expectedTable.data.block_size = 4096; |
| 42 | expectedTable.data.block_count = 2; // 1 table block and 1 partition block |
| 43 | expectedTable.checksum = |
| 44 | openpower::virtual_pnor::details::checksum(expectedTable.data); |
| 45 | |
| 46 | pnor_partition expectedPartition{}; |
| 47 | strcpy(expectedPartition.data.name, partitionName); |
| 48 | expectedPartition.data.base = 1; // starts after 1 block |
| 49 | expectedPartition.data.size = 1; // 1 block |
| 50 | expectedPartition.data.actual = 0x400; // 1024 bytes |
| 51 | expectedPartition.data.id = 1; |
| 52 | expectedPartition.data.pid = PARENT_PATITION_ID; |
| 53 | expectedPartition.data.type = PARTITION_TYPE_DATA; |
| 54 | expectedPartition.data.flags = 0; |
| 55 | expectedPartition.data.user.data[0] = PARTITION_ECC_PROTECTED; |
| 56 | expectedPartition.data.user.data[1] |= PARTITION_PRESERVED; |
| 57 | expectedPartition.checksum = |
| 58 | openpower::virtual_pnor::details::checksum(expectedPartition.data); |
| 59 | |
| 60 | const pnor_partition_table& result = table.getNativeTable(); |
| 61 | |
| 62 | fs::remove_all(fs::path{tmpdir}); |
| 63 | |
| 64 | auto rc = memcmp(&expectedTable, &result, sizeof(pnor_partition_table)); |
| 65 | assert(rc == 0); |
| 66 | |
| 67 | rc = memcmp(&expectedPartition, &result.partitions[0], |
| 68 | sizeof(pnor_partition)); |
| 69 | assert(rc == 0); |
| 70 | |
| 71 | const pnor_partition& first = table.partition(4096); |
| 72 | rc = memcmp(&first, &result.partitions[0], sizeof(pnor_partition)); |
| 73 | assert(rc == 0); |
| 74 | |
| 75 | return 0; |
| 76 | } |