blob: 6f6b101109b2d001dce1f3ad10511323c63e3cfe [file] [log] [blame]
Andrew Jeffery89985752018-11-13 15:17:30 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
3#include "config.h"
4
5#include "vpnor/pnor_partition_table.hpp"
6
Andrew Jeffery89985752018-11-13 15:17:30 +10307#include <sys/mman.h>
8
Andrew Jeffery261f61a2019-03-14 09:57:28 +10309#include <cassert>
10#include <cstring>
11
Andrew Jeffery89985752018-11-13 15:17:30 +103012extern "C" {
13#include "test/mbox.h"
14#include "test/system.h"
15}
16
17#include "vpnor/test/tmpd.hpp"
18
19static constexpr auto BLOCK_SIZE = 4 * 1024;
20static constexpr auto PNOR_SIZE = 2 * BLOCK_SIZE;
21static constexpr auto MEM_SIZE = BLOCK_SIZE;
22static constexpr auto ERASE_SIZE = BLOCK_SIZE;
23static constexpr auto N_WINDOWS = 1;
24static constexpr auto WINDOW_SIZE = BLOCK_SIZE;
25static constexpr auto TOC_PART_SIZE = BLOCK_SIZE;
26
27const std::string toc[] = {
28 "partition00=part,00000000,00001000,80,READWRITE",
29 "partition01=ONE,00001000,00002000,80,READWRITE",
30};
31
32static const uint8_t get_info[] = {0x02, 0x00, 0x02, 0x00, 0x00, 0x00,
33 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34 0x00, 0x00, 0x00, 0x00};
35
36static const uint8_t write_toc[] = {0x06, 0x01, 0x00, 0x00, 0x01, 0x00,
37 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38 0x00, 0x00, 0x00, 0x00};
39
40static const uint8_t erase_toc[] = {0x0a, 0x02, 0x00, 0x00, 0x01, 0x00,
41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42 0x00, 0x00, 0x00, 0x00};
43
44static const uint8_t read_one[] = {0x04, 0x03, 0x01, 0x00, 0x01, 0x00,
45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 0x00, 0x00, 0x00, 0x00};
47
48static const uint8_t read_toc[] = {0x04, 0x04, 0x00, 0x00, 0x01, 0x00,
49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50 0x00, 0x00, 0x00, 0x00};
51
52int 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_test_context(N_WINDOWS, WINDOW_SIZE);
71 test::VpnorRoot root(ctx, toc, BLOCK_SIZE);
72 vpnor::partition::Table table(ctx);
73
74 assert(table.capacity() == TOC_PART_SIZE);
75
76 init_vpnor_from_paths(ctx);
77
78 rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
79 assert(rc == MBOX_R_SUCCESS);
80
81 rc = mbox_command_dispatch(ctx, write_toc, sizeof(write_toc));
82 assert(rc == MBOX_R_SUCCESS);
83
84 rc = mbox_command_dispatch(ctx, erase_toc, sizeof(erase_toc));
85 assert(rc == MBOX_R_SUCCESS);
86
87 rc = memcmp(ctx->mem, erased, BLOCK_SIZE);
88 assert(rc == 0);
89
90 rc = mbox_command_dispatch(ctx, read_one, sizeof(read_one));
91 assert(rc == MBOX_R_SUCCESS);
92
93 rc = mbox_command_dispatch(ctx, read_toc, sizeof(read_toc));
94 assert(rc == MBOX_R_SUCCESS);
95
96 rc = memcmp(ctx->mem, erased, BLOCK_SIZE);
97 assert(rc == 0);
98
99 free(erased);
100
101 return 0;
102}