1*30bcf84cSAndrew Jeffery // SPDX-License-Identifier: Apache-2.0
2*30bcf84cSAndrew Jeffery // Copyright (C) 2018 IBM Corp.
3*30bcf84cSAndrew Jeffery #include <assert.h>
4*30bcf84cSAndrew Jeffery #include <sys/mman.h>
5*30bcf84cSAndrew Jeffery #include <string.h>
6*30bcf84cSAndrew Jeffery
7*30bcf84cSAndrew Jeffery #include "config.h"
8*30bcf84cSAndrew Jeffery #include "vpnor/pnor_partition_table.hpp"
9*30bcf84cSAndrew Jeffery
10*30bcf84cSAndrew Jeffery extern "C" {
11*30bcf84cSAndrew Jeffery #include "test/mbox.h"
12*30bcf84cSAndrew Jeffery #include "test/system.h"
13*30bcf84cSAndrew Jeffery }
14*30bcf84cSAndrew Jeffery
15*30bcf84cSAndrew Jeffery #include "vpnor/test/tmpd.hpp"
16*30bcf84cSAndrew Jeffery
17*30bcf84cSAndrew Jeffery static constexpr auto BLOCK_SIZE = 4 * 1024;
18*30bcf84cSAndrew Jeffery static constexpr auto PNOR_SIZE = 64 * 1024 * 1024;
19*30bcf84cSAndrew Jeffery static constexpr auto MEM_SIZE = BLOCK_SIZE * 2;
20*30bcf84cSAndrew Jeffery static constexpr auto ERASE_SIZE = BLOCK_SIZE;
21*30bcf84cSAndrew Jeffery static constexpr auto N_WINDOWS = 1;
22*30bcf84cSAndrew Jeffery static constexpr auto WINDOW_SIZE = BLOCK_SIZE;
23*30bcf84cSAndrew Jeffery static constexpr auto TOC_PART_SIZE = BLOCK_SIZE;
24*30bcf84cSAndrew Jeffery
25*30bcf84cSAndrew Jeffery const std::string toc[] = {
26*30bcf84cSAndrew Jeffery "partition00=part,00000000,00001000,80,READONLY",
27*30bcf84cSAndrew Jeffery "partition01=ONE,00001000,00002000,80,READWRITE",
28*30bcf84cSAndrew Jeffery };
29*30bcf84cSAndrew Jeffery
30*30bcf84cSAndrew Jeffery static const uint8_t get_info[] = {0x02, 0x00, 0x02, 0x00, 0x00, 0x00,
31*30bcf84cSAndrew Jeffery 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32*30bcf84cSAndrew Jeffery 0x00, 0x00, 0x00, 0x00};
33*30bcf84cSAndrew Jeffery
34*30bcf84cSAndrew Jeffery /* Request access to the ToC base for one block */
35*30bcf84cSAndrew Jeffery static const uint8_t create_read_window[] = {0x04, 0x01, 0x00, 0x00, 0x01, 0x00,
36*30bcf84cSAndrew Jeffery 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
37*30bcf84cSAndrew Jeffery 0x00, 0x00, 0x00, 0x00};
38*30bcf84cSAndrew Jeffery
39*30bcf84cSAndrew Jeffery /* Expect a response containing the ToC in one block */
40*30bcf84cSAndrew Jeffery static const uint8_t response[] = {
41*30bcf84cSAndrew Jeffery 0x04, 0x01, 0xfe, 0xff, 0x01, 0x00, 0x00,
42*30bcf84cSAndrew Jeffery 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
43*30bcf84cSAndrew Jeffery };
44*30bcf84cSAndrew Jeffery
main()45*30bcf84cSAndrew Jeffery int main()
46*30bcf84cSAndrew Jeffery {
47*30bcf84cSAndrew Jeffery namespace test = openpower::virtual_pnor::test;
48*30bcf84cSAndrew Jeffery namespace vpnor = openpower::virtual_pnor;
49*30bcf84cSAndrew Jeffery
50*30bcf84cSAndrew Jeffery struct mbox_context* ctx;
51*30bcf84cSAndrew Jeffery int rc;
52*30bcf84cSAndrew Jeffery
53*30bcf84cSAndrew Jeffery system_set_reserved_size(MEM_SIZE);
54*30bcf84cSAndrew Jeffery system_set_mtd_sizes(PNOR_SIZE, ERASE_SIZE);
55*30bcf84cSAndrew Jeffery
56*30bcf84cSAndrew Jeffery ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
57*30bcf84cSAndrew Jeffery test::VpnorRoot root(ctx, toc, BLOCK_SIZE);
58*30bcf84cSAndrew Jeffery vpnor::partition::Table table(ctx);
59*30bcf84cSAndrew Jeffery
60*30bcf84cSAndrew Jeffery /* Make sure the ToC exactly fits in the space allocated for it */
61*30bcf84cSAndrew Jeffery assert(table.capacity() == TOC_PART_SIZE);
62*30bcf84cSAndrew Jeffery
63*30bcf84cSAndrew Jeffery init_vpnor_from_paths(ctx);
64*30bcf84cSAndrew Jeffery
65*30bcf84cSAndrew Jeffery rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
66*30bcf84cSAndrew Jeffery assert(rc == 1);
67*30bcf84cSAndrew Jeffery
68*30bcf84cSAndrew Jeffery rc = mbox_command_dispatch(ctx, create_read_window,
69*30bcf84cSAndrew Jeffery sizeof(create_read_window));
70*30bcf84cSAndrew Jeffery assert(rc == 1);
71*30bcf84cSAndrew Jeffery
72*30bcf84cSAndrew Jeffery rc = mbox_cmp(ctx, response, sizeof(response));
73*30bcf84cSAndrew Jeffery assert(rc == 0);
74*30bcf84cSAndrew Jeffery
75*30bcf84cSAndrew Jeffery /* Ensure our partition table is present and as expected */
76*30bcf84cSAndrew Jeffery const pnor_partition_table& toc = table.getHostTable();
77*30bcf84cSAndrew Jeffery rc = memcmp(ctx->mem, &toc, table.size());
78*30bcf84cSAndrew Jeffery assert(rc == 0);
79*30bcf84cSAndrew Jeffery
80*30bcf84cSAndrew Jeffery return 0;
81*30bcf84cSAndrew Jeffery }
82