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 <endian.h>
8 #include <sys/mman.h>
9 
10 #include <cassert>
11 #include <cstring>
12 
13 extern "C" {
14 #include "test/mbox.h"
15 #include "test/system.h"
16 #include "vpnor/pnor_partition_defs.h"
17 }
18 
19 #include "vpnor/test/tmpd.hpp"
20 
21 static constexpr auto BLOCK_SIZE = 4 * 1024;
22 static constexpr auto PNOR_SIZE = BLOCK_SIZE;
23 static constexpr auto MEM_SIZE = BLOCK_SIZE;
24 static constexpr auto ERASE_SIZE = BLOCK_SIZE;
25 static constexpr auto N_WINDOWS = 1;
26 static constexpr auto WINDOW_SIZE = BLOCK_SIZE;
27 static constexpr auto TOC_PART_SIZE = BLOCK_SIZE;
28 
29 const std::string toc[] = {
30     "partition00=part,00000000,00001000,80,READWRITE",
31     "partition01=ONE,00001000,00002000,80,READWRITE",
32 };
33 
34 static const uint8_t get_info[] = {0x02, 0x00, 0x02, 0x00, 0x00, 0x00,
35                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
36                                    0x00, 0x00, 0x00, 0x00};
37 
38 static const uint8_t read_toc[] = {0x04, 0x04, 0x00, 0x00, 0x01, 0x00,
39                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
40                                    0x00, 0x00, 0x00, 0x00};
41 
42 int main()
43 {
44     namespace test = openpower::virtual_pnor::test;
45     namespace vpnor = openpower::virtual_pnor;
46 
47     struct pnor_partition_table* htable;
48     struct mbox_context* ctx;
49     uint32_t perms;
50     int rc;
51 
52     system_set_reserved_size(MEM_SIZE);
53     system_set_mtd_sizes(PNOR_SIZE, ERASE_SIZE);
54 
55     ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
56     test::VpnorRoot root(ctx, toc, BLOCK_SIZE);
57     vpnor::partition::Table table(ctx);
58 
59     assert(table.capacity() == TOC_PART_SIZE);
60 
61     init_vpnor_from_paths(ctx);
62 
63     rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
64     assert(rc == MBOX_R_SUCCESS);
65 
66     rc = mbox_command_dispatch(ctx, read_toc, sizeof(read_toc));
67     assert(rc == MBOX_R_SUCCESS);
68 
69     htable = reinterpret_cast<struct pnor_partition_table*>(ctx->mem);
70     perms = be32toh(htable->partitions[0].data.user.data[1]);
71     assert(perms & PARTITION_READONLY);
72 
73     htable = reinterpret_cast<struct pnor_partition_table*>(ctx->mem);
74     perms = be32toh(htable->partitions[1].data.user.data[1]);
75     assert(!(perms & PARTITION_READONLY));
76 
77     return 0;
78 }
79