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