Andrew Jeffery | 8998575 | 2018-11-13 15:17:30 +1030 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2018 IBM Corp. |
| 3 | #include "config.h" |
| 4 | |
| 5 | #include "vpnor/pnor_partition_table.hpp" |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <string.h> |
| 9 | #include <sys/mman.h> |
| 10 | |
| 11 | extern "C" { |
| 12 | #include "test/mbox.h" |
| 13 | #include "test/system.h" |
| 14 | } |
| 15 | |
| 16 | #include "vpnor/test/tmpd.hpp" |
| 17 | |
| 18 | static constexpr auto BLOCK_SIZE = 4 * 1024; |
| 19 | static constexpr auto PNOR_SIZE = 2 * BLOCK_SIZE; |
| 20 | static constexpr auto MEM_SIZE = BLOCK_SIZE; |
| 21 | static constexpr auto ERASE_SIZE = BLOCK_SIZE; |
| 22 | static constexpr auto N_WINDOWS = 1; |
| 23 | static constexpr auto WINDOW_SIZE = BLOCK_SIZE; |
| 24 | static constexpr auto TOC_PART_SIZE = BLOCK_SIZE; |
| 25 | |
| 26 | const std::string toc[] = { |
| 27 | "partition00=part,00000000,00001000,80,READWRITE", |
| 28 | "partition01=ONE,00001000,00002000,80,READWRITE", |
| 29 | }; |
| 30 | |
| 31 | static const uint8_t get_info[] = {0x02, 0x00, 0x02, 0x00, 0x00, 0x00, |
| 32 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 33 | 0x00, 0x00, 0x00, 0x00}; |
| 34 | |
| 35 | static const uint8_t write_toc[] = {0x06, 0x01, 0x00, 0x00, 0x01, 0x00, |
| 36 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 37 | 0x00, 0x00, 0x00, 0x00}; |
| 38 | |
| 39 | static const uint8_t erase_toc[] = {0x0a, 0x02, 0x00, 0x00, 0x01, 0x00, |
| 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 41 | 0x00, 0x00, 0x00, 0x00}; |
| 42 | |
| 43 | static const uint8_t read_one[] = {0x04, 0x03, 0x01, 0x00, 0x01, 0x00, |
| 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 45 | 0x00, 0x00, 0x00, 0x00}; |
| 46 | |
| 47 | static const uint8_t read_toc[] = {0x04, 0x04, 0x00, 0x00, 0x01, 0x00, |
| 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 49 | 0x00, 0x00, 0x00, 0x00}; |
| 50 | |
| 51 | int main() |
| 52 | { |
| 53 | namespace test = openpower::virtual_pnor::test; |
| 54 | namespace vpnor = openpower::virtual_pnor; |
| 55 | |
| 56 | struct mbox_context* ctx; |
| 57 | int rc; |
| 58 | |
| 59 | void* erased; |
| 60 | |
| 61 | erased = malloc(BLOCK_SIZE); |
| 62 | assert(erased); |
| 63 | |
| 64 | memset(erased, 0xff, BLOCK_SIZE); |
| 65 | |
| 66 | system_set_reserved_size(MEM_SIZE); |
| 67 | system_set_mtd_sizes(PNOR_SIZE, ERASE_SIZE); |
| 68 | |
| 69 | ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE); |
| 70 | test::VpnorRoot root(ctx, toc, BLOCK_SIZE); |
| 71 | vpnor::partition::Table table(ctx); |
| 72 | |
| 73 | assert(table.capacity() == TOC_PART_SIZE); |
| 74 | |
| 75 | init_vpnor_from_paths(ctx); |
| 76 | |
| 77 | rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info)); |
| 78 | assert(rc == MBOX_R_SUCCESS); |
| 79 | |
| 80 | rc = mbox_command_dispatch(ctx, write_toc, sizeof(write_toc)); |
| 81 | assert(rc == MBOX_R_SUCCESS); |
| 82 | |
| 83 | rc = mbox_command_dispatch(ctx, erase_toc, sizeof(erase_toc)); |
| 84 | assert(rc == MBOX_R_SUCCESS); |
| 85 | |
| 86 | rc = memcmp(ctx->mem, erased, BLOCK_SIZE); |
| 87 | assert(rc == 0); |
| 88 | |
| 89 | rc = mbox_command_dispatch(ctx, read_one, sizeof(read_one)); |
| 90 | assert(rc == MBOX_R_SUCCESS); |
| 91 | |
| 92 | rc = mbox_command_dispatch(ctx, read_toc, sizeof(read_toc)); |
| 93 | assert(rc == MBOX_R_SUCCESS); |
| 94 | |
| 95 | rc = memcmp(ctx->mem, erased, BLOCK_SIZE); |
| 96 | assert(rc == 0); |
| 97 | |
| 98 | free(erased); |
| 99 | |
| 100 | return 0; |
| 101 | } |