Andrew Jeffery | 733cbe6 | 2018-02-22 22:10:16 +1030 | [diff] [blame] | 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" |
Andrew Jeffery | 26558db | 2018-08-10 00:22:38 +0930 | [diff] [blame] | 14 | #include "mboxd.h" |
Andrew Jeffery | 71eaa73 | 2018-08-08 16:44:24 +0930 | [diff] [blame] | 15 | extern "C" { |
Andrew Jeffery | eebc6bd | 2018-08-08 10:38:19 +0930 | [diff] [blame] | 16 | #include "flash.h" |
Andrew Jeffery | 71eaa73 | 2018-08-08 16:44:24 +0930 | [diff] [blame] | 17 | } |
Andrew Jeffery | 733cbe6 | 2018-02-22 22:10:16 +1030 | [diff] [blame] | 18 | |
Andrew Jeffery | 30bcf84 | 2018-03-26 12:13:20 +1030 | [diff] [blame] | 19 | #include "vpnor/test/tmpd.hpp" |
Andrew Jeffery | 733cbe6 | 2018-02-22 22:10:16 +1030 | [diff] [blame] | 20 | |
| 21 | static constexpr auto BLOCK_SIZE = 0x1000; |
| 22 | static constexpr auto DATA_SIZE = 8; |
| 23 | |
| 24 | const uint8_t data[DATA_SIZE] = {0xa0, 0xa1, 0xa2, 0xa3, |
| 25 | 0xa4, 0xa5, 0xa6, 0xa7}; |
| 26 | |
| 27 | const std::string toc[] = { |
| 28 | "partition01=TEST1,00001000,00002000,80,ECC,READWRITE", |
| 29 | }; |
| 30 | |
| 31 | int main(void) |
| 32 | { |
| 33 | namespace fs = std::experimental::filesystem; |
| 34 | namespace test = openpower::virtual_pnor::test; |
| 35 | |
| 36 | struct mbox_context _ctx, *ctx = &_ctx; |
| 37 | char src[DATA_SIZE]{0}; |
| 38 | void *map; |
| 39 | int rc; |
| 40 | int fd; |
| 41 | |
| 42 | /* Setup */ |
| 43 | memset(ctx, 0, sizeof(mbox_context)); |
| 44 | |
| 45 | mbox_vlog = &mbox_log_console; |
| 46 | verbosity = (verbose)2; |
| 47 | |
| 48 | test::VpnorRoot root(ctx, toc, BLOCK_SIZE); |
| 49 | root.write("TEST1", data, sizeof(data)); |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 50 | /* flash_write doesn't copy the file for us */ |
Andrew Jeffery | 733cbe6 | 2018-02-22 22:10:16 +1030 | [diff] [blame] | 51 | assert(fs::copy_file(root.ro() / "TEST1", root.rw() / "TEST1")); |
| 52 | fs::path patch = root.patch() / "TEST1"; |
| 53 | assert(fs::copy_file(root.ro() / "TEST1", patch)); |
| 54 | |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 55 | init_vpnor_from_paths(ctx); |
Andrew Jeffery | 733cbe6 | 2018-02-22 22:10:16 +1030 | [diff] [blame] | 56 | |
| 57 | /* Test */ |
| 58 | memset(src, 0x33, sizeof(src)); |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 59 | rc = flash_write(ctx, 0x1000, src, sizeof(src)); |
Andrew Jeffery | 733cbe6 | 2018-02-22 22:10:16 +1030 | [diff] [blame] | 60 | assert(rc == 0); |
| 61 | |
| 62 | /* Check that RW file is unmodified after the patch write */ |
| 63 | fd = open((root.rw() / "TEST1").c_str(), O_RDONLY); |
| 64 | map = mmap(NULL, sizeof(src), PROT_READ, MAP_SHARED, fd, 0); |
| 65 | assert(map != MAP_FAILED); |
| 66 | rc = memcmp(data, map, sizeof(src)); |
| 67 | assert(rc == 0); |
| 68 | munmap(map, sizeof(src)); |
| 69 | close(fd); |
| 70 | |
| 71 | /* Check that PATCH is modified with the new data */ |
| 72 | fd = open(patch.c_str(), O_RDONLY); |
| 73 | map = mmap(NULL, sizeof(src), PROT_READ, MAP_SHARED, fd, 0); |
| 74 | assert(map != MAP_FAILED); |
| 75 | rc = memcmp(src, map, sizeof(src)); |
| 76 | assert(rc == 0); |
| 77 | munmap(map, sizeof(src)); |
| 78 | close(fd); |
| 79 | |
| 80 | destroy_vpnor(ctx); |
| 81 | free(ctx->flash_bmap); |
| 82 | |
| 83 | return rc; |
| 84 | } |