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