blob: 5d3980218398766a63e741f017ea9a8e13d789e8 [file] [log] [blame]
Andrew Jefferye9493ad2018-03-28 14:04:34 +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 <stdint.h>
8#include <sys/ioctl.h>
9#include <sys/mman.h>
10#include <sys/syslog.h>
11#include <unistd.h>
12
13#include "config.h"
14#include "common.h"
15#include "mbox.h"
Andrew Jefferyeebc6bd2018-08-08 10:38:19 +093016#include "flash.h"
Andrew Jefferye9493ad2018-03-28 14:04:34 +103017
Andrew Jeffery30bcf842018-03-26 12:13:20 +103018#include "vpnor/test/tmpd.hpp"
Andrew Jefferye9493ad2018-03-28 14:04:34 +103019
20static constexpr auto BLOCK_SIZE = 0x1000;
21static constexpr auto PART_SIZE = BLOCK_SIZE;
22static constexpr auto PATCH_SIZE = BLOCK_SIZE / 2;
23static constexpr auto UPDATE_SIZE = BLOCK_SIZE;
24
25const std::string toc[] = {
26 "partition01=TEST1,00001000,00002000,80,ECC,READWRITE",
27};
28
29int main(void)
30{
31 namespace fs = std::experimental::filesystem;
32 namespace test = openpower::virtual_pnor::test;
33
34 struct mbox_context _ctx, *ctx = &_ctx;
35 void *map;
36 int rc;
37 int fd;
38
39 /* Setup */
40 memset(ctx, 0, sizeof(mbox_context));
41
42 mbox_vlog = &mbox_log_console;
43 verbosity = (verbose)2;
44
45 test::VpnorRoot root(ctx, toc, BLOCK_SIZE);
46 std::vector<uint8_t> roContent(PART_SIZE, 0xff);
47 root.write("TEST1", roContent.data(), roContent.size());
48 /* write_flash doesn't copy the file for us */
49 std::vector<uint8_t> patchContent(PATCH_SIZE, 0xaa);
50 root.patch("TEST1", patchContent.data(), patchContent.size());
51
Andrew Jeffery742a1f62018-03-02 09:26:03 +103052 init_vpnor_from_paths(ctx);
Andrew Jefferye9493ad2018-03-28 14:04:34 +103053
54 /* Test */
55 std::vector<uint8_t> update(UPDATE_SIZE, 0x55);
56 rc = write_flash(ctx, 0x1000, update.data(), update.size());
57 assert(rc == 0);
58
59 /* Check that PATCH is modified with the new data */
60 fs::path patch = root.patch() / "TEST1";
61 assert(UPDATE_SIZE == fs::file_size(patch));
62 fd = open(patch.c_str(), O_RDONLY);
63 map = mmap(NULL, UPDATE_SIZE, PROT_READ, MAP_SHARED, fd, 0);
64 assert(map != MAP_FAILED);
65 rc = memcmp(update.data(), map, update.size());
66 assert(rc == 0);
67 munmap(map, update.size());
68 close(fd);
69
70 destroy_vpnor(ctx);
71 free(ctx->flash_bmap);
72
73 return rc;
74}