1*30bcf84cSAndrew Jeffery // SPDX-License-Identifier: Apache-2.0
2*30bcf84cSAndrew Jeffery // Copyright (C) 2018 IBM Corp.
3*30bcf84cSAndrew Jeffery 
4*30bcf84cSAndrew Jeffery #include <assert.h>
5*30bcf84cSAndrew Jeffery #include <experimental/filesystem>
6*30bcf84cSAndrew Jeffery #include <fstream>
7*30bcf84cSAndrew Jeffery #include <string.h>
8*30bcf84cSAndrew Jeffery #include <vector>
9*30bcf84cSAndrew Jeffery 
10*30bcf84cSAndrew Jeffery #include "config.h"
11*30bcf84cSAndrew Jeffery #include "vpnor/mboxd_pnor_partition_table.h"
12*30bcf84cSAndrew Jeffery 
13*30bcf84cSAndrew Jeffery extern "C" {
14*30bcf84cSAndrew Jeffery #include "test/mbox.h"
15*30bcf84cSAndrew Jeffery #include "test/system.h"
16*30bcf84cSAndrew Jeffery }
17*30bcf84cSAndrew Jeffery 
18*30bcf84cSAndrew Jeffery #include "vpnor/test/tmpd.hpp"
19*30bcf84cSAndrew Jeffery 
20*30bcf84cSAndrew Jeffery // A read window assumes that the toc is located at offset 0,
21*30bcf84cSAndrew Jeffery // so create dummy partition at arbitrary offset 0x1000.
22*30bcf84cSAndrew Jeffery const std::string toc[] = {
23*30bcf84cSAndrew Jeffery     "partition01=HBB,00001000,00002000,80,ECC,READONLY",
24*30bcf84cSAndrew Jeffery };
25*30bcf84cSAndrew Jeffery 
26*30bcf84cSAndrew Jeffery static const uint8_t data[8] = {0xaa, 0x55, 0xaa, 0x66, 0x77, 0x88, 0x99, 0xab};
27*30bcf84cSAndrew Jeffery 
28*30bcf84cSAndrew Jeffery static const auto BLOCK_SIZE = 4096;
29*30bcf84cSAndrew Jeffery static const auto MEM_SIZE = BLOCK_SIZE * 2;
30*30bcf84cSAndrew Jeffery static const auto ERASE_SIZE = BLOCK_SIZE;
31*30bcf84cSAndrew Jeffery static const auto N_WINDOWS = 1;
32*30bcf84cSAndrew Jeffery static const auto WINDOW_SIZE = BLOCK_SIZE;
33*30bcf84cSAndrew Jeffery 
34*30bcf84cSAndrew Jeffery static const uint8_t get_info[] = {0x02, 0x00, 0x02, 0x00, 0x00, 0x00,
35*30bcf84cSAndrew Jeffery                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
36*30bcf84cSAndrew Jeffery                                    0x00, 0x00, 0x00, 0x00};
37*30bcf84cSAndrew Jeffery 
38*30bcf84cSAndrew Jeffery // offset 0x100 and size 6
39*30bcf84cSAndrew Jeffery static const uint8_t create_read_window[] = {0x04, 0x01, 0x01, 0x00, 0x01, 0x00,
40*30bcf84cSAndrew Jeffery                                              0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41*30bcf84cSAndrew Jeffery                                              0x00, 0x00, 0x00, 0x00};
42*30bcf84cSAndrew Jeffery 
43*30bcf84cSAndrew Jeffery static const uint8_t response[] = {0x04, 0x01, 0xfe, 0xff, 0x01, 0x00, 0x01,
44*30bcf84cSAndrew Jeffery                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
45*30bcf84cSAndrew Jeffery 
46*30bcf84cSAndrew Jeffery namespace test = openpower::virtual_pnor::test;
47*30bcf84cSAndrew Jeffery 
main()48*30bcf84cSAndrew Jeffery int main()
49*30bcf84cSAndrew Jeffery {
50*30bcf84cSAndrew Jeffery     struct mbox_context *ctx;
51*30bcf84cSAndrew Jeffery 
52*30bcf84cSAndrew Jeffery     system_set_reserved_size(MEM_SIZE);
53*30bcf84cSAndrew Jeffery     system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE);
54*30bcf84cSAndrew Jeffery 
55*30bcf84cSAndrew Jeffery     ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
56*30bcf84cSAndrew Jeffery 
57*30bcf84cSAndrew Jeffery     test::VpnorRoot root(ctx, toc, BLOCK_SIZE);
58*30bcf84cSAndrew Jeffery     root.write("HBB", data, sizeof(data));
59*30bcf84cSAndrew Jeffery 
60*30bcf84cSAndrew Jeffery     init_vpnor_from_paths(ctx);
61*30bcf84cSAndrew Jeffery 
62*30bcf84cSAndrew Jeffery     int rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
63*30bcf84cSAndrew Jeffery     assert(rc == 1);
64*30bcf84cSAndrew Jeffery 
65*30bcf84cSAndrew Jeffery     // send the request for partition1
66*30bcf84cSAndrew Jeffery     rc = mbox_command_dispatch(ctx, create_read_window,
67*30bcf84cSAndrew Jeffery                                sizeof(create_read_window));
68*30bcf84cSAndrew Jeffery     assert(rc == 1);
69*30bcf84cSAndrew Jeffery 
70*30bcf84cSAndrew Jeffery     rc = mbox_cmp(ctx, response, sizeof(response));
71*30bcf84cSAndrew Jeffery     assert(rc == 0);
72*30bcf84cSAndrew Jeffery 
73*30bcf84cSAndrew Jeffery     // Compare the reserved memory to the pnor
74*30bcf84cSAndrew Jeffery     rc = memcmp(ctx->mem, data, 6);
75*30bcf84cSAndrew Jeffery     assert(rc == 0);
76*30bcf84cSAndrew Jeffery 
77*30bcf84cSAndrew Jeffery     return rc;
78*30bcf84cSAndrew Jeffery }
79