Andrew Jeffery | 24f4459 | 2018-02-22 17:51:59 +1030 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2018 IBM Corp. |
| 3 | |
| 4 | #include <assert.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <sys/mman.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <unistd.h> |
| 10 | |
| 11 | #include "common.h" |
Andrew Jeffery | 26558db | 2018-08-10 00:22:38 +0930 | [diff] [blame] | 12 | #include "mboxd.h" |
Andrew Jeffery | 71eaa73 | 2018-08-08 16:44:24 +0930 | [diff] [blame] | 13 | extern "C" { |
Andrew Jeffery | eebc6bd | 2018-08-08 10:38:19 +0930 | [diff] [blame] | 14 | #include "flash.h" |
Andrew Jeffery | 71eaa73 | 2018-08-08 16:44:24 +0930 | [diff] [blame] | 15 | } |
Andrew Jeffery | 24f4459 | 2018-02-22 17:51:59 +1030 | [diff] [blame] | 16 | |
Andrew Jeffery | 30bcf84 | 2018-03-26 12:13:20 +1030 | [diff] [blame] | 17 | #include "vpnor/test/tmpd.hpp" |
Andrew Jeffery | 24f4459 | 2018-02-22 17:51:59 +1030 | [diff] [blame] | 18 | |
| 19 | static constexpr auto BLOCK_SIZE = 0x1000; |
| 20 | |
| 21 | const std::string toc[] = { |
| 22 | "partition01=TEST1,00001000,00002000,80,ECC,READWRITE", |
| 23 | }; |
| 24 | |
| 25 | int main(void) |
| 26 | { |
| 27 | namespace fs = std::experimental::filesystem; |
| 28 | namespace test = openpower::virtual_pnor::test; |
| 29 | |
| 30 | struct mbox_context _ctx, *ctx = &_ctx; |
| 31 | uint8_t src[8] = {0}; |
| 32 | void *map; |
| 33 | int rc; |
| 34 | int fd; |
| 35 | |
| 36 | /* Setup */ |
| 37 | memset(ctx, 0, sizeof(mbox_context)); |
| 38 | |
| 39 | mbox_vlog = &mbox_log_console; |
| 40 | verbosity = (verbose)2; |
| 41 | |
| 42 | test::VpnorRoot root(ctx, toc, BLOCK_SIZE); |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 43 | /* flash_write() doesn't copy the file for us */ |
Andrew Jeffery | 24f4459 | 2018-02-22 17:51:59 +1030 | [diff] [blame] | 44 | assert(fs::copy_file(root.ro() / "TEST1", root.rw() / "TEST1")); |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 45 | init_vpnor_from_paths(ctx); |
Andrew Jeffery | 24f4459 | 2018-02-22 17:51:59 +1030 | [diff] [blame] | 46 | |
| 47 | /* Test */ |
| 48 | memset(src, 0xbb, sizeof(src)); |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 49 | rc = flash_write(ctx, 0x1000, src, sizeof(src)); |
Andrew Jeffery | 24f4459 | 2018-02-22 17:51:59 +1030 | [diff] [blame] | 50 | assert(rc == 0); |
| 51 | fd = open((root.rw() / "TEST1").c_str(), O_RDONLY); |
Andrew Jeffery | 506f2f5 | 2018-03-27 12:13:25 +1030 | [diff] [blame] | 52 | map = mmap(NULL, sizeof(src), PROT_READ, MAP_SHARED, fd, 0); |
Andrew Jeffery | 24f4459 | 2018-02-22 17:51:59 +1030 | [diff] [blame] | 53 | assert(map != MAP_FAILED); |
| 54 | rc = memcmp(src, map, sizeof(src)); |
| 55 | assert(rc == 0); |
| 56 | |
| 57 | /* Ensure single byte writes function */ |
| 58 | memset(src, 0xcc, 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 | 24f4459 | 2018-02-22 17:51:59 +1030 | [diff] [blame] | 60 | assert(rc == 0); |
| 61 | rc = memcmp(src, map, sizeof(src)); |
| 62 | assert(rc == 0); |
| 63 | |
| 64 | src[0] = 0xff; |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 65 | rc = flash_write(ctx, 0x1000, src, 1); |
Andrew Jeffery | 24f4459 | 2018-02-22 17:51:59 +1030 | [diff] [blame] | 66 | assert(rc == 0); |
| 67 | rc = memcmp(src, map, sizeof(src)); |
| 68 | assert(rc == 0); |
| 69 | |
| 70 | src[1] = 0xff; |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 71 | rc = flash_write(ctx, 0x1000 + 1, &src[1], 1); |
Andrew Jeffery | 24f4459 | 2018-02-22 17:51:59 +1030 | [diff] [blame] | 72 | assert(rc == 0); |
| 73 | rc = memcmp(src, map, sizeof(src)); |
| 74 | assert(rc == 0); |
| 75 | |
| 76 | src[2] = 0xff; |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 77 | rc = flash_write(ctx, 0x1000 + 2, &src[2], 1); |
Andrew Jeffery | 24f4459 | 2018-02-22 17:51:59 +1030 | [diff] [blame] | 78 | assert(rc == 0); |
| 79 | rc = memcmp(src, map, sizeof(src)); |
| 80 | assert(rc == 0); |
| 81 | |
| 82 | /* Writes past the end of the partition should fail */ |
Andrew Jeffery | 0293f2f | 2018-08-08 16:59:45 +0930 | [diff] [blame] | 83 | rc = flash_write(ctx, 0x1000 + 0xff9, src, sizeof(src)); |
Andrew Jeffery | 24f4459 | 2018-02-22 17:51:59 +1030 | [diff] [blame] | 84 | assert(rc < 0); |
| 85 | |
| 86 | /* Check that RW file is unmodified after the bad write */ |
| 87 | fd = open((root.rw() / "TEST1").c_str(), O_RDONLY); |
| 88 | map = mmap(NULL, sizeof(src), PROT_READ, MAP_SHARED, fd, 0); |
| 89 | assert(map != MAP_FAILED); |
| 90 | rc = memcmp(src, map, sizeof(src)); |
| 91 | assert(rc == 0); |
| 92 | |
| 93 | munmap(map, sizeof(src)); |
| 94 | close(fd); |
| 95 | |
| 96 | destroy_vpnor(ctx); |
| 97 | |
| 98 | return 0; |
| 99 | } |