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 <fcntl.h>
7*30bcf84cSAndrew Jeffery #include <stdint.h>
8*30bcf84cSAndrew Jeffery #include <sys/ioctl.h>
9*30bcf84cSAndrew Jeffery #include <sys/mman.h>
10*30bcf84cSAndrew Jeffery #include <sys/syslog.h>
11*30bcf84cSAndrew Jeffery #include <unistd.h>
12*30bcf84cSAndrew Jeffery 
13*30bcf84cSAndrew Jeffery #include "config.h"
14*30bcf84cSAndrew Jeffery #include "common.h"
15*30bcf84cSAndrew Jeffery #include "mbox.h"
16*30bcf84cSAndrew Jeffery #include "mboxd_flash.h"
17*30bcf84cSAndrew Jeffery 
18*30bcf84cSAndrew Jeffery #include "vpnor/test/tmpd.hpp"
19*30bcf84cSAndrew Jeffery 
20*30bcf84cSAndrew Jeffery static constexpr auto BLOCK_SIZE = 0x1000;
21*30bcf84cSAndrew Jeffery static constexpr auto PART_SIZE = BLOCK_SIZE;
22*30bcf84cSAndrew Jeffery static constexpr auto PATCH_SIZE = BLOCK_SIZE / 2;
23*30bcf84cSAndrew Jeffery static constexpr auto UPDATE_SIZE = BLOCK_SIZE;
24*30bcf84cSAndrew Jeffery 
25*30bcf84cSAndrew Jeffery const std::string toc[] = {
26*30bcf84cSAndrew Jeffery     "partition01=TEST1,00001000,00002000,80,ECC,READWRITE",
27*30bcf84cSAndrew Jeffery };
28*30bcf84cSAndrew Jeffery 
main(void)29*30bcf84cSAndrew Jeffery int main(void)
30*30bcf84cSAndrew Jeffery {
31*30bcf84cSAndrew Jeffery     namespace fs = std::experimental::filesystem;
32*30bcf84cSAndrew Jeffery     namespace test = openpower::virtual_pnor::test;
33*30bcf84cSAndrew Jeffery 
34*30bcf84cSAndrew Jeffery     struct mbox_context _ctx, *ctx = &_ctx;
35*30bcf84cSAndrew Jeffery     void *map;
36*30bcf84cSAndrew Jeffery     int rc;
37*30bcf84cSAndrew Jeffery     int fd;
38*30bcf84cSAndrew Jeffery 
39*30bcf84cSAndrew Jeffery     /* Setup */
40*30bcf84cSAndrew Jeffery     memset(ctx, 0, sizeof(mbox_context));
41*30bcf84cSAndrew Jeffery 
42*30bcf84cSAndrew Jeffery     mbox_vlog = &mbox_log_console;
43*30bcf84cSAndrew Jeffery     verbosity = (verbose)2;
44*30bcf84cSAndrew Jeffery 
45*30bcf84cSAndrew Jeffery     test::VpnorRoot root(ctx, toc, BLOCK_SIZE);
46*30bcf84cSAndrew Jeffery     std::vector<uint8_t> roContent(PART_SIZE, 0xff);
47*30bcf84cSAndrew Jeffery     root.write("TEST1", roContent.data(), roContent.size());
48*30bcf84cSAndrew Jeffery     /* write_flash doesn't copy the file for us */
49*30bcf84cSAndrew Jeffery     std::vector<uint8_t> patchContent(PATCH_SIZE, 0xaa);
50*30bcf84cSAndrew Jeffery     root.patch("TEST1", patchContent.data(), patchContent.size());
51*30bcf84cSAndrew Jeffery 
52*30bcf84cSAndrew Jeffery     init_vpnor_from_paths(ctx);
53*30bcf84cSAndrew Jeffery 
54*30bcf84cSAndrew Jeffery     /* Test */
55*30bcf84cSAndrew Jeffery     std::vector<uint8_t> update(UPDATE_SIZE, 0x55);
56*30bcf84cSAndrew Jeffery     rc = write_flash(ctx, 0x1000, update.data(), update.size());
57*30bcf84cSAndrew Jeffery     assert(rc == 0);
58*30bcf84cSAndrew Jeffery 
59*30bcf84cSAndrew Jeffery     /* Check that PATCH is modified with the new data */
60*30bcf84cSAndrew Jeffery     fs::path patch = root.patch() / "TEST1";
61*30bcf84cSAndrew Jeffery     assert(UPDATE_SIZE == fs::file_size(patch));
62*30bcf84cSAndrew Jeffery     fd = open(patch.c_str(), O_RDONLY);
63*30bcf84cSAndrew Jeffery     map = mmap(NULL, UPDATE_SIZE, PROT_READ, MAP_SHARED, fd, 0);
64*30bcf84cSAndrew Jeffery     assert(map != MAP_FAILED);
65*30bcf84cSAndrew Jeffery     rc = memcmp(update.data(), map, update.size());
66*30bcf84cSAndrew Jeffery     assert(rc == 0);
67*30bcf84cSAndrew Jeffery     munmap(map, update.size());
68*30bcf84cSAndrew Jeffery     close(fd);
69*30bcf84cSAndrew Jeffery 
70*30bcf84cSAndrew Jeffery     destroy_vpnor(ctx);
71*30bcf84cSAndrew Jeffery     free(ctx->flash_bmap);
72*30bcf84cSAndrew Jeffery 
73*30bcf84cSAndrew Jeffery     return rc;
74*30bcf84cSAndrew Jeffery }
75