blob: 76b1525a8eb6635357125121976167c153dc32ef [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"
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);
43 /* write_flash() doesn't copy the file for us */
44 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));
49 rc = write_flash(ctx, 0x1000, src, sizeof(src));
50 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));
59 rc = write_flash(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 = write_flash(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 = write_flash(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 = write_flash(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 = write_flash(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}