blob: cf9bf7b26847c08c32056e71be463bd4e0f0f59e [file] [log] [blame]
Andrew Jeffery24f44592018-02-22 17:51:59 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Andrew Jeffery261f61a2019-03-14 09:57:28 +10303#include "config.h"
Andrew Jeffery24f44592018-02-22 17:51:59 +10304
Andrew Jeffery261f61a2019-03-14 09:57:28 +10305extern "C" {
Evan Lojewskif1e547c2019-03-14 14:34:33 +10306#include "backend.h"
Andrew Jeffery261f61a2019-03-14 09:57:28 +10307#include "common.h"
Andrew Jeffery261f61a2019-03-14 09:57:28 +10308#include "mboxd.h"
9}
10
11#include "vpnor/test/tmpd.hpp"
12
Andrew Jeffery24f44592018-02-22 17:51:59 +103013#include <fcntl.h>
14#include <sys/mman.h>
15#include <sys/stat.h>
16#include <sys/types.h>
17#include <unistd.h>
18
Andrew Jeffery261f61a2019-03-14 09:57:28 +103019#include <cassert>
Andrew Jeffery24f44592018-02-22 17:51:59 +103020
21static constexpr auto BLOCK_SIZE = 0x1000;
22
23const std::string toc[] = {
24 "partition01=TEST1,00001000,00002000,80,ECC,READWRITE",
25};
26
27int main(void)
28{
29 namespace fs = std::experimental::filesystem;
30 namespace test = openpower::virtual_pnor::test;
31
32 struct mbox_context _ctx, *ctx = &_ctx;
33 uint8_t src[8] = {0};
William A. Kennington IIId5f1d402018-10-11 13:55:04 -070034 void* map;
Andrew Jeffery24f44592018-02-22 17:51:59 +103035 int rc;
36 int fd;
37
38 /* Setup */
39 memset(ctx, 0, sizeof(mbox_context));
40
41 mbox_vlog = &mbox_log_console;
42 verbosity = (verbose)2;
43
Evan Lojewskif1e547c2019-03-14 14:34:33 +103044 ctx->backend.flash_size = 0x2000;
45 test::VpnorRoot root(&ctx->backend, toc, BLOCK_SIZE);
Andrew Jeffery0293f2f2018-08-08 16:59:45 +093046 /* flash_write() doesn't copy the file for us */
Andrew Jeffery24f44592018-02-22 17:51:59 +103047 assert(fs::copy_file(root.ro() / "TEST1", root.rw() / "TEST1"));
Andrew Jeffery24f44592018-02-22 17:51:59 +103048
49 /* Test */
50 memset(src, 0xbb, sizeof(src));
Andrew Jeffery0297e5b2019-03-14 16:36:27 +103051 rc = backend_write(&ctx->backend, 0x1000, src, sizeof(src));
Andrew Jeffery24f44592018-02-22 17:51:59 +103052 assert(rc == 0);
53 fd = open((root.rw() / "TEST1").c_str(), O_RDONLY);
Andrew Jeffery506f2f52018-03-27 12:13:25 +103054 map = mmap(NULL, sizeof(src), PROT_READ, MAP_SHARED, fd, 0);
Andrew Jeffery24f44592018-02-22 17:51:59 +103055 assert(map != MAP_FAILED);
56 rc = memcmp(src, map, sizeof(src));
57 assert(rc == 0);
58
59 /* Ensure single byte writes function */
60 memset(src, 0xcc, sizeof(src));
Andrew Jeffery0297e5b2019-03-14 16:36:27 +103061 rc = backend_write(&ctx->backend, 0x1000, src, sizeof(src));
Andrew Jeffery24f44592018-02-22 17:51:59 +103062 assert(rc == 0);
63 rc = memcmp(src, map, sizeof(src));
64 assert(rc == 0);
65
66 src[0] = 0xff;
Andrew Jeffery0297e5b2019-03-14 16:36:27 +103067 rc = backend_write(&ctx->backend, 0x1000, src, 1);
Andrew Jeffery24f44592018-02-22 17:51:59 +103068 assert(rc == 0);
69 rc = memcmp(src, map, sizeof(src));
70 assert(rc == 0);
71
72 src[1] = 0xff;
Andrew Jeffery0297e5b2019-03-14 16:36:27 +103073 rc = backend_write(&ctx->backend, 0x1000 + 1, &src[1], 1);
Andrew Jeffery24f44592018-02-22 17:51:59 +103074 assert(rc == 0);
75 rc = memcmp(src, map, sizeof(src));
76 assert(rc == 0);
77
78 src[2] = 0xff;
Andrew Jeffery0297e5b2019-03-14 16:36:27 +103079 rc = backend_write(&ctx->backend, 0x1000 + 2, &src[2], 1);
Andrew Jeffery24f44592018-02-22 17:51:59 +103080 assert(rc == 0);
81 rc = memcmp(src, map, sizeof(src));
82 assert(rc == 0);
83
84 /* Writes past the end of the partition should fail */
Andrew Jeffery0297e5b2019-03-14 16:36:27 +103085 rc = backend_write(&ctx->backend, 0x1000 + 0xff9, src, sizeof(src));
Andrew Jeffery24f44592018-02-22 17:51:59 +103086 assert(rc < 0);
87
88 /* Check that RW file is unmodified after the bad write */
89 fd = open((root.rw() / "TEST1").c_str(), O_RDONLY);
90 map = mmap(NULL, sizeof(src), PROT_READ, MAP_SHARED, fd, 0);
91 assert(map != MAP_FAILED);
92 rc = memcmp(src, map, sizeof(src));
93 assert(rc == 0);
94
95 munmap(map, sizeof(src));
96 close(fd);
97
Andrew Jeffery24f44592018-02-22 17:51:59 +103098 return 0;
99}