blob: 5d93fc99c3e77b099f2edd1f5980ae60762d55d5 [file] [log] [blame]
Andrew Jeffery733cbe62018-02-22 22:10:16 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
3
4#include <assert.h>
5#include <experimental/filesystem>
6#include <fcntl.h>
7#include <sys/ioctl.h>
8#include <sys/mman.h>
9#include <sys/syslog.h>
10#include <unistd.h>
11
12#include "config.h"
13#include "common.h"
14#include "mbox.h"
Andrew Jeffery71eaa732018-08-08 16:44:24 +093015extern "C" {
Andrew Jefferyeebc6bd2018-08-08 10:38:19 +093016#include "flash.h"
Andrew Jeffery71eaa732018-08-08 16:44:24 +093017}
Andrew Jeffery733cbe62018-02-22 22:10:16 +103018
Andrew Jeffery30bcf842018-03-26 12:13:20 +103019#include "vpnor/test/tmpd.hpp"
Andrew Jeffery733cbe62018-02-22 22:10:16 +103020
21static constexpr auto BLOCK_SIZE = 0x1000;
22static constexpr auto DATA_SIZE = 8;
23
24const uint8_t data[DATA_SIZE] = {0xa0, 0xa1, 0xa2, 0xa3,
25 0xa4, 0xa5, 0xa6, 0xa7};
26
27const std::string toc[] = {
28 "partition01=TEST1,00001000,00002000,80,ECC,READWRITE",
29};
30
31int main(void)
32{
33 namespace fs = std::experimental::filesystem;
34 namespace test = openpower::virtual_pnor::test;
35
36 struct mbox_context _ctx, *ctx = &_ctx;
37 char src[DATA_SIZE]{0};
38 void *map;
39 int rc;
40 int fd;
41
42 /* Setup */
43 memset(ctx, 0, sizeof(mbox_context));
44
45 mbox_vlog = &mbox_log_console;
46 verbosity = (verbose)2;
47
48 test::VpnorRoot root(ctx, toc, BLOCK_SIZE);
49 root.write("TEST1", data, sizeof(data));
Andrew Jeffery0293f2f2018-08-08 16:59:45 +093050 /* flash_write doesn't copy the file for us */
Andrew Jeffery733cbe62018-02-22 22:10:16 +103051 assert(fs::copy_file(root.ro() / "TEST1", root.rw() / "TEST1"));
52 fs::path patch = root.patch() / "TEST1";
53 assert(fs::copy_file(root.ro() / "TEST1", patch));
54
Andrew Jeffery742a1f62018-03-02 09:26:03 +103055 init_vpnor_from_paths(ctx);
Andrew Jeffery733cbe62018-02-22 22:10:16 +103056
57 /* Test */
58 memset(src, 0x33, sizeof(src));
Andrew Jeffery0293f2f2018-08-08 16:59:45 +093059 rc = flash_write(ctx, 0x1000, src, sizeof(src));
Andrew Jeffery733cbe62018-02-22 22:10:16 +103060 assert(rc == 0);
61
62 /* Check that RW file is unmodified after the patch write */
63 fd = open((root.rw() / "TEST1").c_str(), O_RDONLY);
64 map = mmap(NULL, sizeof(src), PROT_READ, MAP_SHARED, fd, 0);
65 assert(map != MAP_FAILED);
66 rc = memcmp(data, map, sizeof(src));
67 assert(rc == 0);
68 munmap(map, sizeof(src));
69 close(fd);
70
71 /* Check that PATCH is modified with the new data */
72 fd = open(patch.c_str(), O_RDONLY);
73 map = mmap(NULL, sizeof(src), PROT_READ, MAP_SHARED, fd, 0);
74 assert(map != MAP_FAILED);
75 rc = memcmp(src, map, sizeof(src));
76 assert(rc == 0);
77 munmap(map, sizeof(src));
78 close(fd);
79
80 destroy_vpnor(ctx);
81 free(ctx->flash_bmap);
82
83 return rc;
84}