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" 12 #include "mboxd.h" 13 extern "C" { 14 #include "flash.h" 15 } 16 17 #include "vpnor/test/tmpd.hpp" 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); 43 /* flash_write() doesn't copy the file for us */ 44 assert(fs::copy_file(root.ro() / "TEST1", root.rw() / "TEST1")); 45 init_vpnor_from_paths(ctx); 46 47 /* Test */ 48 memset(src, 0xbb, sizeof(src)); 49 rc = flash_write(ctx, 0x1000, src, sizeof(src)); 50 assert(rc == 0); 51 fd = open((root.rw() / "TEST1").c_str(), O_RDONLY); 52 map = mmap(NULL, sizeof(src), PROT_READ, MAP_SHARED, fd, 0); 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)); 59 rc = flash_write(ctx, 0x1000, src, sizeof(src)); 60 assert(rc == 0); 61 rc = memcmp(src, map, sizeof(src)); 62 assert(rc == 0); 63 64 src[0] = 0xff; 65 rc = flash_write(ctx, 0x1000, src, 1); 66 assert(rc == 0); 67 rc = memcmp(src, map, sizeof(src)); 68 assert(rc == 0); 69 70 src[1] = 0xff; 71 rc = flash_write(ctx, 0x1000 + 1, &src[1], 1); 72 assert(rc == 0); 73 rc = memcmp(src, map, sizeof(src)); 74 assert(rc == 0); 75 76 src[2] = 0xff; 77 rc = flash_write(ctx, 0x1000 + 2, &src[2], 1); 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 */ 83 rc = flash_write(ctx, 0x1000 + 0xff9, src, sizeof(src)); 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 } 100