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 | |
Deepak Kodihalli | d1d7930 | 2017-07-11 23:01:39 -0500 | [diff] [blame] | 33 | const openpower::virtual_pnor::partition::Table |
| 34 | table(fs::path{tmpdir}, 4 * 1024, 64 * 1024 * 1024); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 35 | |
| 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 Kodihalli | d1d7930 | 2017-07-11 23:01:39 -0500 | [diff] [blame] | 43 | expectedTable.data.block_count = |
| 44 | (64 * 1024 * 1024) / expectedTable.data.block_size; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 45 | expectedTable.checksum = |
| 46 | openpower::virtual_pnor::details::checksum(expectedTable.data); |
| 47 | |
| 48 | pnor_partition expectedPartition{}; |
| 49 | strcpy(expectedPartition.data.name, partitionName); |
| 50 | expectedPartition.data.base = 1; // starts after 1 block |
| 51 | 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; |
| 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 | |
| 73 | const pnor_partition& first = table.partition(4096); |
| 74 | rc = memcmp(&first, &result.partitions[0], sizeof(pnor_partition)); |
| 75 | assert(rc == 0); |
| 76 | |
| 77 | return 0; |
| 78 | } |