130bcf84cSAndrew Jeffery // SPDX-License-Identifier: Apache-2.0
230bcf84cSAndrew Jeffery // Copyright (C) 2018 IBM Corp.
330bcf84cSAndrew Jeffery 
4d5f1d40fSWilliam A. Kennington III #include "config.h"
5d5f1d40fSWilliam A. Kennington III 
630bcf84cSAndrew Jeffery extern "C" {
730bcf84cSAndrew Jeffery #include "test/mbox.h"
830bcf84cSAndrew Jeffery #include "test/system.h"
930bcf84cSAndrew Jeffery }
1030bcf84cSAndrew Jeffery 
1130bcf84cSAndrew Jeffery #include "vpnor/test/tmpd.hpp"
1230bcf84cSAndrew Jeffery 
13261f61a1SAndrew Jeffery #include <cassert>
14261f61a1SAndrew Jeffery #include <cstring>
15*150be912SPatrick Williams #include <filesystem>
16261f61a1SAndrew Jeffery #include <fstream>
17261f61a1SAndrew Jeffery #include <vector>
18261f61a1SAndrew Jeffery 
19f4bc335bSAndrew Jeffery #include "vpnor/backend.h"
20261f61a1SAndrew Jeffery 
2130bcf84cSAndrew Jeffery // A read window assumes that the toc is located at offset 0,
2230bcf84cSAndrew Jeffery // so create dummy partition at arbitrary offset 0x1000.
2330bcf84cSAndrew Jeffery const std::string toc[] = {
2430bcf84cSAndrew Jeffery     "partition01=HBB,00001000,00002000,80,ECC,READONLY",
2530bcf84cSAndrew Jeffery };
2630bcf84cSAndrew Jeffery 
2730bcf84cSAndrew Jeffery static const uint8_t data[8] = {0xaa, 0x55, 0xaa, 0x66, 0x77, 0x88, 0x99, 0xab};
2830bcf84cSAndrew Jeffery 
2930bcf84cSAndrew Jeffery static const auto BLOCK_SIZE = 4096;
3030bcf84cSAndrew Jeffery static const auto MEM_SIZE = BLOCK_SIZE * 2;
3130bcf84cSAndrew Jeffery static const auto ERASE_SIZE = BLOCK_SIZE;
3230bcf84cSAndrew Jeffery static const auto N_WINDOWS = 1;
3330bcf84cSAndrew Jeffery static const auto WINDOW_SIZE = BLOCK_SIZE;
3430bcf84cSAndrew Jeffery 
3530bcf84cSAndrew Jeffery static const uint8_t get_info[] = {0x02, 0x00, 0x02, 0x00, 0x00, 0x00,
3630bcf84cSAndrew Jeffery                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3730bcf84cSAndrew Jeffery                                    0x00, 0x00, 0x00, 0x00};
3830bcf84cSAndrew Jeffery 
3930bcf84cSAndrew Jeffery // offset 0x100 and size 6
4030bcf84cSAndrew Jeffery static const uint8_t create_read_window[] = {0x04, 0x01, 0x01, 0x00, 0x01, 0x00,
4130bcf84cSAndrew Jeffery                                              0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4230bcf84cSAndrew Jeffery                                              0x00, 0x00, 0x00, 0x00};
4330bcf84cSAndrew Jeffery 
4430bcf84cSAndrew Jeffery static const uint8_t response[] = {0x04, 0x01, 0xfe, 0xff, 0x01, 0x00, 0x01,
4530bcf84cSAndrew Jeffery                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
4630bcf84cSAndrew Jeffery 
4730bcf84cSAndrew Jeffery namespace test = openpower::virtual_pnor::test;
4830bcf84cSAndrew Jeffery 
main()4930bcf84cSAndrew Jeffery int main()
5030bcf84cSAndrew Jeffery {
5130bcf84cSAndrew Jeffery     struct mbox_context* ctx;
5230bcf84cSAndrew Jeffery 
5330bcf84cSAndrew Jeffery     system_set_reserved_size(MEM_SIZE);
5430bcf84cSAndrew Jeffery     system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE);
5530bcf84cSAndrew Jeffery 
56f1e547c7SEvan Lojewski     ctx = mbox_create_frontend_context(N_WINDOWS, WINDOW_SIZE);
5730bcf84cSAndrew Jeffery 
58f1e547c7SEvan Lojewski     test::VpnorRoot root(&ctx->backend, toc, BLOCK_SIZE);
5930bcf84cSAndrew Jeffery     root.write("HBB", data, sizeof(data));
6030bcf84cSAndrew Jeffery 
6130bcf84cSAndrew Jeffery     int rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
6230bcf84cSAndrew Jeffery     assert(rc == 1);
6330bcf84cSAndrew Jeffery 
6430bcf84cSAndrew Jeffery     // send the request for partition1
6530bcf84cSAndrew Jeffery     rc = mbox_command_dispatch(ctx, create_read_window,
6630bcf84cSAndrew Jeffery                                sizeof(create_read_window));
6730bcf84cSAndrew Jeffery     assert(rc == 1);
6830bcf84cSAndrew Jeffery 
6930bcf84cSAndrew Jeffery     rc = mbox_cmp(ctx, response, sizeof(response));
7030bcf84cSAndrew Jeffery     assert(rc == 0);
7130bcf84cSAndrew Jeffery 
7230bcf84cSAndrew Jeffery     // Compare the reserved memory to the pnor
7330bcf84cSAndrew Jeffery     rc = memcmp(ctx->mem, data, 6);
7430bcf84cSAndrew Jeffery     assert(rc == 0);
7530bcf84cSAndrew Jeffery 
7630bcf84cSAndrew Jeffery     return rc;
7730bcf84cSAndrew Jeffery }
78