xref: /openbmc/hiomapd/vpnor/test/write_patch.cpp (revision 30bcf84c)
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 <sys/ioctl.h>
8 #include <sys/mman.h>
9 #include <sys/syslog.h>
10 #include <unistd.h>
11 
12 #include "config.h"
13 #include "common.h"
14 #include "mbox.h"
15 #include "mboxd_flash.h"
16 
17 #include "vpnor/test/tmpd.hpp"
18 
19 static constexpr auto BLOCK_SIZE = 0x1000;
20 static constexpr auto DATA_SIZE = 8;
21 
22 const uint8_t data[DATA_SIZE] = {0xa0, 0xa1, 0xa2, 0xa3,
23                                  0xa4, 0xa5, 0xa6, 0xa7};
24 
25 const std::string toc[] = {
26     "partition01=TEST1,00001000,00002000,80,ECC,READWRITE",
27 };
28 
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     char src[DATA_SIZE]{0};
36     void *map;
37     int rc;
38     int fd;
39 
40     /* Setup */
41     memset(ctx, 0, sizeof(mbox_context));
42 
43     mbox_vlog = &mbox_log_console;
44     verbosity = (verbose)2;
45 
46     test::VpnorRoot root(ctx, toc, BLOCK_SIZE);
47     root.write("TEST1", data, sizeof(data));
48     /* write_flash doesn't copy the file for us */
49     assert(fs::copy_file(root.ro() / "TEST1", root.rw() / "TEST1"));
50     fs::path patch = root.patch() / "TEST1";
51     assert(fs::copy_file(root.ro() / "TEST1", patch));
52 
53     init_vpnor_from_paths(ctx);
54 
55     /* Test */
56     memset(src, 0x33, sizeof(src));
57     rc = write_flash(ctx, 0x1000, src, sizeof(src));
58     assert(rc == 0);
59 
60     /* Check that RW file is unmodified after the patch write */
61     fd = open((root.rw() / "TEST1").c_str(), O_RDONLY);
62     map = mmap(NULL, sizeof(src), PROT_READ, MAP_SHARED, fd, 0);
63     assert(map != MAP_FAILED);
64     rc = memcmp(data, map, sizeof(src));
65     assert(rc == 0);
66     munmap(map, sizeof(src));
67     close(fd);
68 
69     /* Check that PATCH is modified with the new data */
70     fd = open(patch.c_str(), O_RDONLY);
71     map = mmap(NULL, sizeof(src), PROT_READ, MAP_SHARED, fd, 0);
72     assert(map != MAP_FAILED);
73     rc = memcmp(src, map, sizeof(src));
74     assert(rc == 0);
75     munmap(map, sizeof(src));
76     close(fd);
77 
78     destroy_vpnor(ctx);
79     free(ctx->flash_bmap);
80 
81     return rc;
82 }
83