xref: /openbmc/hiomapd/vpnor/test/write_toc.cpp (revision de08ca2d)
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 }
10 
11 #include "vpnor/table.hpp"
12 #include "vpnor/test/tmpd.hpp"
13 
14 #include <sys/mman.h>
15 
16 #include <cassert>
17 #include <cstring>
18 
19 static constexpr auto BLOCK_SIZE = 4 * 1024;
20 static constexpr auto PNOR_SIZE = 2 * BLOCK_SIZE;
21 static constexpr auto MEM_SIZE = BLOCK_SIZE;
22 static constexpr auto ERASE_SIZE = BLOCK_SIZE;
23 static constexpr auto N_WINDOWS = 1;
24 static constexpr auto WINDOW_SIZE = BLOCK_SIZE;
25 static constexpr auto TOC_PART_SIZE = BLOCK_SIZE;
26 
27 const std::string toc[] = {
28     "partition00=part,00000000,00001000,80,READWRITE",
29     "partition01=ONE,00001000,00002000,80,READWRITE",
30 };
31 
32 static const uint8_t get_info[] = {0x02, 0x00, 0x02, 0x00, 0x00, 0x00,
33                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34                                    0x00, 0x00, 0x00, 0x00};
35 
36 static const uint8_t write_toc[] = {0x06, 0x01, 0x00, 0x00, 0x01, 0x00,
37                                     0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38                                     0x00, 0x00, 0x00, 0x00};
39 
40 static const uint8_t erase_toc[] = {0x0a, 0x02, 0x00, 0x00, 0x01, 0x00,
41                                     0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42                                     0x00, 0x00, 0x00, 0x00};
43 
44 static const uint8_t read_one[] = {0x04, 0x03, 0x01, 0x00, 0x01, 0x00,
45                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46                                    0x00, 0x00, 0x00, 0x00};
47 
48 static const uint8_t read_toc[] = {0x04, 0x04, 0x00, 0x00, 0x01, 0x00,
49                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50                                    0x00, 0x00, 0x00, 0x00};
51 
main()52 int main()
53 {
54     namespace test = openpower::virtual_pnor::test;
55     namespace vpnor = openpower::virtual_pnor;
56 
57     struct mbox_context* ctx;
58     int rc;
59 
60     void* erased;
61 
62     erased = malloc(BLOCK_SIZE);
63     assert(erased);
64 
65     memset(erased, 0xff, BLOCK_SIZE);
66 
67     system_set_reserved_size(MEM_SIZE);
68     system_set_mtd_sizes(PNOR_SIZE, ERASE_SIZE);
69 
70     ctx = mbox_create_frontend_context(N_WINDOWS, WINDOW_SIZE);
71     test::VpnorRoot root(&ctx->backend, toc, BLOCK_SIZE);
72     vpnor::partition::Table table(&ctx->backend);
73 
74     assert(table.capacity() == TOC_PART_SIZE);
75 
76     rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
77     assert(rc == MBOX_R_SUCCESS);
78 
79     rc = mbox_command_dispatch(ctx, write_toc, sizeof(write_toc));
80     assert(rc == MBOX_R_SUCCESS);
81 
82     rc = mbox_command_dispatch(ctx, erase_toc, sizeof(erase_toc));
83     assert(rc == MBOX_R_SUCCESS);
84 
85     rc = memcmp(ctx->mem, erased, BLOCK_SIZE);
86     assert(rc == 0);
87 
88     rc = mbox_command_dispatch(ctx, read_one, sizeof(read_one));
89     assert(rc == MBOX_R_SUCCESS);
90 
91     rc = mbox_command_dispatch(ctx, read_toc, sizeof(read_toc));
92     assert(rc == MBOX_R_SUCCESS);
93 
94     rc = memcmp(ctx->mem, erased, BLOCK_SIZE);
95     assert(rc == 0);
96 
97     free(erased);
98 
99     return 0;
100 }
101