1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright (C) 2018 IBM Corp.
3 #include "config.h"
4 
5 extern "C" {
6 #include "backend.h"
7 #include "test/mbox.h"
8 #include "test/system.h"
9 #include "vpnor/ffs.h"
10 }
11 
12 #include "vpnor/table.hpp"
13 #include "vpnor/test/tmpd.hpp"
14 
15 #include <endian.h>
16 #include <sys/mman.h>
17 
18 #include <cassert>
19 #include <cstring>
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_frontend_context(N_WINDOWS, WINDOW_SIZE);
56     test::VpnorRoot root(&ctx->backend, toc, BLOCK_SIZE);
57     vpnor::partition::Table table(&ctx->backend);
58 
59     assert(table.capacity() == TOC_PART_SIZE);
60 
61     rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
62     assert(rc == MBOX_R_SUCCESS);
63 
64     rc = mbox_command_dispatch(ctx, read_toc, sizeof(read_toc));
65     assert(rc == MBOX_R_SUCCESS);
66 
67     htable = reinterpret_cast<struct pnor_partition_table*>(ctx->mem);
68     perms = be32toh(htable->partitions[0].data.user.data[1]);
69     assert(perms & PARTITION_READONLY);
70 
71     htable = reinterpret_cast<struct pnor_partition_table*>(ctx->mem);
72     perms = be32toh(htable->partitions[1].data.user.data[1]);
73     assert(!(perms & PARTITION_READONLY));
74 
75     return 0;
76 }
77