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 | |
| 4 | #include <assert.h> |
| 5 | #include <experimental/filesystem> |
| 6 | #include <fcntl.h> |
| 7 | #include <stdint.h> |
| 8 | #include <sys/ioctl.h> |
| 9 | #include <sys/mman.h> |
| 10 | #include <sys/syslog.h> |
| 11 | #include <unistd.h> |
| 12 | |
| 13 | #include "config.h" |
| 14 | #include "common.h" |
| 15 | #include "mbox.h" |
Andrew Jeffery | 71eaa73 | 2018-08-08 16:44:24 +0930 | [diff] [blame] | 16 | |
| 17 | extern "C" { |
Andrew Jeffery | eebc6bd | 2018-08-08 10:38:19 +0930 | [diff] [blame] | 18 | #include "flash.h" |
Andrew Jeffery | 71eaa73 | 2018-08-08 16:44:24 +0930 | [diff] [blame] | 19 | } |
Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 20 | |
Andrew Jeffery | 30bcf84 | 2018-03-26 12:13:20 +1030 | [diff] [blame] | 21 | #include "vpnor/test/tmpd.hpp" |
Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 22 | |
| 23 | static constexpr auto BLOCK_SIZE = 0x1000; |
| 24 | static constexpr auto PART_SIZE = BLOCK_SIZE; |
| 25 | static constexpr auto PATCH_SIZE = BLOCK_SIZE / 2; |
| 26 | static constexpr auto UPDATE_SIZE = BLOCK_SIZE; |
| 27 | |
| 28 | const std::string toc[] = { |
| 29 | "partition01=TEST1,00001000,00002000,80,ECC,READWRITE", |
| 30 | }; |
| 31 | |
| 32 | int main(void) |
| 33 | { |
| 34 | namespace fs = std::experimental::filesystem; |
| 35 | namespace test = openpower::virtual_pnor::test; |
| 36 | |
| 37 | struct mbox_context _ctx, *ctx = &_ctx; |
| 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 | std::vector<uint8_t> roContent(PART_SIZE, 0xff); |
| 50 | root.write("TEST1", roContent.data(), roContent.size()); |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 51 | /* flash_write doesn't copy the file for us */ |
Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 52 | std::vector<uint8_t> patchContent(PATCH_SIZE, 0xaa); |
| 53 | root.patch("TEST1", patchContent.data(), patchContent.size()); |
| 54 | |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 55 | init_vpnor_from_paths(ctx); |
Andrew Jeffery | e9493ad | 2018-03-28 14:04:34 +1030 | [diff] [blame] | 56 | |
| 57 | /* Test */ |
| 58 | std::vector<uint8_t> update(UPDATE_SIZE, 0x55); |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 59 | rc = flash_write(ctx, 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 | |
| 73 | destroy_vpnor(ctx); |
| 74 | free(ctx->flash_bmap); |
| 75 | |
| 76 | return rc; |
| 77 | } |