blob: 9a6979f3d4f967bb86cccbb325c5046c641945d0 [file] [log] [blame]
Andrew Jefferye9493ad2018-03-28 14:04:34 +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 Jeffery261f61a2019-03-14 09:57:28 +10306extern "C" {
7#include "common.h"
8#include "flash.h"
9#include "mboxd.h"
10}
11
12#include "vpnor/test/tmpd.hpp"
13
Andrew Jefferye9493ad2018-03-28 14:04:34 +103014#include <fcntl.h>
15#include <stdint.h>
16#include <sys/ioctl.h>
17#include <sys/mman.h>
18#include <sys/syslog.h>
19#include <unistd.h>
20
Andrew Jeffery261f61a2019-03-14 09:57:28 +103021#include <cassert>
William A. Kennington IIId5f1d402018-10-11 13:55:04 -070022#include <experimental/filesystem>
23
Andrew Jefferye9493ad2018-03-28 14:04:34 +103024static constexpr auto BLOCK_SIZE = 0x1000;
25static constexpr auto PART_SIZE = BLOCK_SIZE;
26static constexpr auto PATCH_SIZE = BLOCK_SIZE / 2;
27static constexpr auto UPDATE_SIZE = BLOCK_SIZE;
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;
William A. Kennington IIId5f1d402018-10-11 13:55:04 -070039 void* map;
Andrew Jefferye9493ad2018-03-28 14:04:34 +103040 int rc;
41 int fd;
42
43 /* Setup */
44 memset(ctx, 0, sizeof(mbox_context));
45
46 mbox_vlog = &mbox_log_console;
47 verbosity = (verbose)2;
48
49 test::VpnorRoot root(ctx, toc, BLOCK_SIZE);
50 std::vector<uint8_t> roContent(PART_SIZE, 0xff);
51 root.write("TEST1", roContent.data(), roContent.size());
Andrew Jeffery0293f2f2018-08-08 16:59:45 +093052 /* flash_write doesn't copy the file for us */
Andrew Jefferye9493ad2018-03-28 14:04:34 +103053 std::vector<uint8_t> patchContent(PATCH_SIZE, 0xaa);
54 root.patch("TEST1", patchContent.data(), patchContent.size());
55
Andrew Jeffery742a1f62018-03-02 09:26:03 +103056 init_vpnor_from_paths(ctx);
Andrew Jefferye9493ad2018-03-28 14:04:34 +103057
58 /* Test */
59 std::vector<uint8_t> update(UPDATE_SIZE, 0x55);
Andrew Jeffery0293f2f2018-08-08 16:59:45 +093060 rc = flash_write(ctx, 0x1000, update.data(), update.size());
Andrew Jefferye9493ad2018-03-28 14:04:34 +103061 assert(rc == 0);
62
63 /* Check that PATCH is modified with the new data */
64 fs::path patch = root.patch() / "TEST1";
65 assert(UPDATE_SIZE == fs::file_size(patch));
66 fd = open(patch.c_str(), O_RDONLY);
67 map = mmap(NULL, UPDATE_SIZE, PROT_READ, MAP_SHARED, fd, 0);
68 assert(map != MAP_FAILED);
69 rc = memcmp(update.data(), map, update.size());
70 assert(rc == 0);
71 munmap(map, update.size());
72 close(fd);
73
74 destroy_vpnor(ctx);
75 free(ctx->flash_bmap);
76
77 return rc;
78}