summaryrefslogtreecommitdiffstats
path: root/vpnor/test/write_patch_resize.cpp
blob: 8d676833ac3d7498af9b8be2cf70ca7618d32ca4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2018 IBM Corp.

#include <assert.h>
#include <experimental/filesystem>
#include <fcntl.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/syslog.h>
#include <unistd.h>

#include "config.h"
#include "common.h"
#include "mbox.h"
#include "mboxd_flash.h"

#include "vpnor/test/tmpd.hpp"

static constexpr auto BLOCK_SIZE = 0x1000;
static constexpr auto PART_SIZE = BLOCK_SIZE;
static constexpr auto PATCH_SIZE = BLOCK_SIZE / 2;
static constexpr auto UPDATE_SIZE = BLOCK_SIZE;

const std::string toc[] = {
    "partition01=TEST1,00001000,00002000,80,ECC,READWRITE",
};

int main(void)
{
    namespace fs = std::experimental::filesystem;
    namespace test = openpower::virtual_pnor::test;

    struct mbox_context _ctx, *ctx = &_ctx;
    void *map;
    int rc;
    int fd;

    /* Setup */
    memset(ctx, 0, sizeof(mbox_context));

    mbox_vlog = &mbox_log_console;
    verbosity = (verbose)2;

    test::VpnorRoot root(ctx, toc, BLOCK_SIZE);
    std::vector<uint8_t> roContent(PART_SIZE, 0xff);
    root.write("TEST1", roContent.data(), roContent.size());
    /* write_flash doesn't copy the file for us */
    std::vector<uint8_t> patchContent(PATCH_SIZE, 0xaa);
    root.patch("TEST1", patchContent.data(), patchContent.size());

    init_vpnor_from_paths(ctx);

    /* Test */
    std::vector<uint8_t> update(UPDATE_SIZE, 0x55);
    rc = write_flash(ctx, 0x1000, update.data(), update.size());
    assert(rc == 0);

    /* Check that PATCH is modified with the new data */
    fs::path patch = root.patch() / "TEST1";
    assert(UPDATE_SIZE == fs::file_size(patch));
    fd = open(patch.c_str(), O_RDONLY);
    map = mmap(NULL, UPDATE_SIZE, PROT_READ, MAP_SHARED, fd, 0);
    assert(map != MAP_FAILED);
    rc = memcmp(update.data(), map, update.size());
    assert(rc == 0);
    munmap(map, update.size());
    close(fd);

    destroy_vpnor(ctx);
    free(ctx->flash_bmap);

    return rc;
}
OpenPOWER on IntegriCloud