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 | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 6 | #include <assert.h> |
Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 7 | #include <fcntl.h> |
| 8 | #include <stdint.h> |
| 9 | #include <sys/ioctl.h> |
| 10 | #include <sys/mman.h> |
| 11 | #include <sys/syslog.h> |
| 12 | #include <unistd.h> |
| 13 | |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 14 | #include <experimental/filesystem> |
| 15 | |
Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 16 | #include "common.h" |
Andrew Jeffery | 26558db | 2018-08-10 00:22:38 +0930 | [diff] [blame] | 17 | #include "mboxd.h" |
Andrew Jeffery | 71eaa73 | 2018-08-08 16:44:24 +0930 | [diff] [blame] | 18 | |
| 19 | extern "C" { |
Andrew Jeffery | eebc6bd | 2018-08-08 10:38:19 +0930 | [diff] [blame] | 20 | #include "flash.h" |
Andrew Jeffery | 71eaa73 | 2018-08-08 16:44:24 +0930 | [diff] [blame] | 21 | } |
Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 22 | |
Andrew Jeffery | 30bcf84 | 2018-03-26 12:13:20 +1030 | [diff] [blame] | 23 | #include "vpnor/test/tmpd.hpp" |
Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 24 | |
| 25 | static constexpr auto BLOCK_SIZE = 0x1000; |
| 26 | static constexpr auto PART_SIZE = BLOCK_SIZE; |
| 27 | static constexpr auto PATCH_SIZE = BLOCK_SIZE / 2; |
| 28 | static constexpr auto UPDATE_SIZE = BLOCK_SIZE; |
| 29 | |
| 30 | const std::string toc[] = { |
| 31 | "partition01=TEST1,00001000,00002000,80,ECC,READWRITE", |
| 32 | }; |
| 33 | |
| 34 | int main(void) |
| 35 | { |
| 36 | namespace fs = std::experimental::filesystem; |
| 37 | namespace test = openpower::virtual_pnor::test; |
| 38 | |
| 39 | struct mbox_context _ctx, *ctx = &_ctx; |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 40 | void* map; |
Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +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 | |
| 50 | test::VpnorRoot root(ctx, toc, BLOCK_SIZE); |
| 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 | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 57 | init_vpnor_from_paths(ctx); |
Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 58 | |
| 59 | /* Test */ |
| 60 | std::vector<uint8_t> update(UPDATE_SIZE, 0x55); |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 61 | rc = flash_write(ctx, 0x1000, update.data(), update.size()); |
Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 62 | assert(rc == 0); |
| 63 | |
| 64 | /* Check that PATCH is modified with the new data */ |
| 65 | fs::path patch = root.patch() / "TEST1"; |
| 66 | assert(UPDATE_SIZE == fs::file_size(patch)); |
| 67 | fd = open(patch.c_str(), O_RDONLY); |
| 68 | map = mmap(NULL, UPDATE_SIZE, PROT_READ, MAP_SHARED, fd, 0); |
| 69 | assert(map != MAP_FAILED); |
| 70 | rc = memcmp(update.data(), map, update.size()); |
| 71 | assert(rc == 0); |
| 72 | munmap(map, update.size()); |
| 73 | close(fd); |
| 74 | |
| 75 | destroy_vpnor(ctx); |
| 76 | free(ctx->flash_bmap); |
| 77 | |
| 78 | return rc; |
| 79 | } |