blob: c40af6a40098c664270d8cdfa7f010b98142d270 [file] [log] [blame]
Andrew Jeffery24f44592018-02-22 17:51:59 +10301// 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 Jeffery26558db2018-08-10 00:22:38 +093012#include "mboxd.h"
Andrew Jeffery71eaa732018-08-08 16:44:24 +093013extern "C" {
Andrew Jefferyeebc6bd2018-08-08 10:38:19 +093014#include "flash.h"
Andrew Jeffery71eaa732018-08-08 16:44:24 +093015}
Andrew Jeffery24f44592018-02-22 17:51:59 +103016
Andrew Jeffery30bcf842018-03-26 12:13:20 +103017#include "vpnor/test/tmpd.hpp"
Andrew Jeffery24f44592018-02-22 17:51:59 +103018
19static constexpr auto BLOCK_SIZE = 0x1000;
20
21const std::string toc[] = {
22 "partition01=TEST1,00001000,00002000,80,ECC,READWRITE",
23};
24
25int 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 Jeffery0293f2f2018-08-08 16:59:45 +093043 /* flash_write() doesn't copy the file for us */
Andrew Jeffery24f44592018-02-22 17:51:59 +103044 assert(fs::copy_file(root.ro() / "TEST1", root.rw() / "TEST1"));
Andrew Jeffery742a1f62018-03-02 09:26:03 +103045 init_vpnor_from_paths(ctx);
Andrew Jeffery24f44592018-02-22 17:51:59 +103046
47 /* Test */
48 memset(src, 0xbb, sizeof(src));
Andrew Jeffery0293f2f2018-08-08 16:59:45 +093049 rc = flash_write(ctx, 0x1000, src, sizeof(src));
Andrew Jeffery24f44592018-02-22 17:51:59 +103050 assert(rc == 0);
51 fd = open((root.rw() / "TEST1").c_str(), O_RDONLY);
Andrew Jeffery506f2f52018-03-27 12:13:25 +103052 map = mmap(NULL, sizeof(src), PROT_READ, MAP_SHARED, fd, 0);
Andrew Jeffery24f44592018-02-22 17:51:59 +103053 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 Jeffery0293f2f2018-08-08 16:59:45 +093059 rc = flash_write(ctx, 0x1000, src, sizeof(src));
Andrew Jeffery24f44592018-02-22 17:51:59 +103060 assert(rc == 0);
61 rc = memcmp(src, map, sizeof(src));
62 assert(rc == 0);
63
64 src[0] = 0xff;
Andrew Jeffery0293f2f2018-08-08 16:59:45 +093065 rc = flash_write(ctx, 0x1000, src, 1);
Andrew Jeffery24f44592018-02-22 17:51:59 +103066 assert(rc == 0);
67 rc = memcmp(src, map, sizeof(src));
68 assert(rc == 0);
69
70 src[1] = 0xff;
Andrew Jeffery0293f2f2018-08-08 16:59:45 +093071 rc = flash_write(ctx, 0x1000 + 1, &src[1], 1);
Andrew Jeffery24f44592018-02-22 17:51:59 +103072 assert(rc == 0);
73 rc = memcmp(src, map, sizeof(src));
74 assert(rc == 0);
75
76 src[2] = 0xff;
Andrew Jeffery0293f2f2018-08-08 16:59:45 +093077 rc = flash_write(ctx, 0x1000 + 2, &src[2], 1);
Andrew Jeffery24f44592018-02-22 17:51:59 +103078 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 Jeffery0293f2f2018-08-08 16:59:45 +093083 rc = flash_write(ctx, 0x1000 + 0xff9, src, sizeof(src));
Andrew Jeffery24f44592018-02-22 17:51:59 +103084 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}