Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +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 | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 14 | #include <fcntl.h> |
| 15 | #include <stdint.h> |
| 16 | #include <sys/ioctl.h> |
| 17 | #include <sys/mman.h> |
| 18 | #include <sys/syslog.h> |
| 19 | #include <unistd.h> |
| 20 | |
Andrew Jeffery | 261f61a | 2019-03-14 09:57:28 +1030 | [diff] [blame] | 21 | #include <cassert> |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 22 | #include <experimental/filesystem> |
| 23 | |
Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 24 | static constexpr auto BLOCK_SIZE = 0x1000; |
| 25 | static constexpr auto PART_SIZE = BLOCK_SIZE; |
| 26 | static constexpr auto PATCH_SIZE = BLOCK_SIZE / 2; |
| 27 | static constexpr auto UPDATE_SIZE = BLOCK_SIZE; |
| 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; |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 39 | void* map; |
Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 40 | int rc; |
| 41 | int fd; |
| 42 | |
| 43 | /* Setup */ |
| 44 | memset(ctx, 0, sizeof(mbox_context)); |
| 45 | |
| 46 | mbox_vlog = &mbox_log_console; |
| 47 | verbosity = (verbose)2; |
| 48 | |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 49 | ctx->backend.flash_size = 0x2000; |
| 50 | test::VpnorRoot root(&ctx->backend, toc, BLOCK_SIZE); |
Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 51 | std::vector<uint8_t> roContent(PART_SIZE, 0xff); |
| 52 | root.write("TEST1", roContent.data(), roContent.size()); |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 53 | /* flash_write doesn't copy the file for us */ |
Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 54 | std::vector<uint8_t> patchContent(PATCH_SIZE, 0xaa); |
| 55 | root.patch("TEST1", patchContent.data(), patchContent.size()); |
| 56 | |
Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 57 | /* Test */ |
| 58 | std::vector<uint8_t> update(UPDATE_SIZE, 0x55); |
Andrew Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 59 | rc = backend_write(&ctx->backend, 0x1000, update.data(), update.size()); |
Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 60 | assert(rc == 0); |
| 61 | |
| 62 | /* Check that PATCH is modified with the new data */ |
| 63 | fs::path patch = root.patch() / "TEST1"; |
| 64 | assert(UPDATE_SIZE == fs::file_size(patch)); |
| 65 | fd = open(patch.c_str(), O_RDONLY); |
| 66 | map = mmap(NULL, UPDATE_SIZE, PROT_READ, MAP_SHARED, fd, 0); |
| 67 | assert(map != MAP_FAILED); |
| 68 | rc = memcmp(update.data(), map, update.size()); |
| 69 | assert(rc == 0); |
| 70 | munmap(map, update.size()); |
| 71 | close(fd); |
| 72 | |
Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 73 | return rc; |
| 74 | } |