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