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