blob: 1c4bd38daee95b6b91ddf07f8df38098d3bb8a04 [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"
12#include "mbox.h"
13#include "mboxd_flash.h"
14
Andrew Jeffery30bcf842018-03-26 12:13:20 +103015#include "vpnor/test/tmpd.hpp"
Andrew Jeffery24f44592018-02-22 17:51:59 +103016
17static constexpr auto BLOCK_SIZE = 0x1000;
18
19const std::string toc[] = {
20 "partition01=TEST1,00001000,00002000,80,ECC,READWRITE",
21};
22
23int main(void)
24{
25 namespace fs = std::experimental::filesystem;
26 namespace test = openpower::virtual_pnor::test;
27
28 struct mbox_context _ctx, *ctx = &_ctx;
29 uint8_t src[8] = {0};
30 void *map;
31 int rc;
32 int fd;
33
34 /* Setup */
35 memset(ctx, 0, sizeof(mbox_context));
36
37 mbox_vlog = &mbox_log_console;
38 verbosity = (verbose)2;
39
40 test::VpnorRoot root(ctx, toc, BLOCK_SIZE);
41 /* write_flash() doesn't copy the file for us */
42 assert(fs::copy_file(root.ro() / "TEST1", root.rw() / "TEST1"));
Andrew Jeffery742a1f62018-03-02 09:26:03 +103043 init_vpnor_from_paths(ctx);
Andrew Jeffery24f44592018-02-22 17:51:59 +103044
45 /* Test */
46 memset(src, 0xbb, sizeof(src));
47 rc = write_flash(ctx, 0x1000, src, sizeof(src));
48 assert(rc == 0);
49 fd = open((root.rw() / "TEST1").c_str(), O_RDONLY);
Andrew Jeffery506f2f52018-03-27 12:13:25 +103050 map = mmap(NULL, sizeof(src), PROT_READ, MAP_SHARED, fd, 0);
Andrew Jeffery24f44592018-02-22 17:51:59 +103051 assert(map != MAP_FAILED);
52 rc = memcmp(src, map, sizeof(src));
53 assert(rc == 0);
54
55 /* Ensure single byte writes function */
56 memset(src, 0xcc, sizeof(src));
57 rc = write_flash(ctx, 0x1000, src, sizeof(src));
58 assert(rc == 0);
59 rc = memcmp(src, map, sizeof(src));
60 assert(rc == 0);
61
62 src[0] = 0xff;
63 rc = write_flash(ctx, 0x1000, src, 1);
64 assert(rc == 0);
65 rc = memcmp(src, map, sizeof(src));
66 assert(rc == 0);
67
68 src[1] = 0xff;
69 rc = write_flash(ctx, 0x1000 + 1, &src[1], 1);
70 assert(rc == 0);
71 rc = memcmp(src, map, sizeof(src));
72 assert(rc == 0);
73
74 src[2] = 0xff;
75 rc = write_flash(ctx, 0x1000 + 2, &src[2], 1);
76 assert(rc == 0);
77 rc = memcmp(src, map, sizeof(src));
78 assert(rc == 0);
79
80 /* Writes past the end of the partition should fail */
81 rc = write_flash(ctx, 0x1000 + 0xff9, src, sizeof(src));
82 assert(rc < 0);
83
84 /* Check that RW file is unmodified after the bad write */
85 fd = open((root.rw() / "TEST1").c_str(), O_RDONLY);
86 map = mmap(NULL, sizeof(src), PROT_READ, MAP_SHARED, fd, 0);
87 assert(map != MAP_FAILED);
88 rc = memcmp(src, map, sizeof(src));
89 assert(rc == 0);
90
91 munmap(map, sizeof(src));
92 close(fd);
93
94 destroy_vpnor(ctx);
95
96 return 0;
97}