130bcf84cSAndrew Jeffery // SPDX-License-Identifier: Apache-2.0
230bcf84cSAndrew Jeffery // Copyright (C) 2018 IBM Corp.
330bcf84cSAndrew Jeffery
430bcf84cSAndrew Jeffery #include <assert.h>
530bcf84cSAndrew Jeffery #include <fcntl.h>
630bcf84cSAndrew Jeffery #include <sys/mman.h>
730bcf84cSAndrew Jeffery #include <sys/stat.h>
830bcf84cSAndrew Jeffery #include <sys/types.h>
930bcf84cSAndrew Jeffery #include <unistd.h>
1030bcf84cSAndrew Jeffery
1130bcf84cSAndrew Jeffery #include "common.h"
1230bcf84cSAndrew Jeffery #include "mbox.h"
1330bcf84cSAndrew Jeffery #include "mboxd_flash.h"
1430bcf84cSAndrew Jeffery
1530bcf84cSAndrew Jeffery #include "vpnor/test/tmpd.hpp"
1630bcf84cSAndrew Jeffery
1730bcf84cSAndrew Jeffery static constexpr auto BLOCK_SIZE = 0x1000;
1830bcf84cSAndrew Jeffery
1930bcf84cSAndrew Jeffery const std::string toc[] = {
2030bcf84cSAndrew Jeffery "partition01=TEST1,00001000,00002000,80,ECC,READWRITE",
2130bcf84cSAndrew Jeffery };
2230bcf84cSAndrew Jeffery
main(void)2330bcf84cSAndrew Jeffery int main(void)
2430bcf84cSAndrew Jeffery {
2530bcf84cSAndrew Jeffery namespace fs = std::experimental::filesystem;
2630bcf84cSAndrew Jeffery namespace test = openpower::virtual_pnor::test;
2730bcf84cSAndrew Jeffery
2830bcf84cSAndrew Jeffery struct mbox_context _ctx, *ctx = &_ctx;
2930bcf84cSAndrew Jeffery uint8_t src[8] = {0};
3030bcf84cSAndrew Jeffery void *map;
3130bcf84cSAndrew Jeffery int rc;
3230bcf84cSAndrew Jeffery int fd;
3330bcf84cSAndrew Jeffery
3430bcf84cSAndrew Jeffery /* Setup */
3530bcf84cSAndrew Jeffery memset(ctx, 0, sizeof(mbox_context));
3630bcf84cSAndrew Jeffery
3730bcf84cSAndrew Jeffery mbox_vlog = &mbox_log_console;
3830bcf84cSAndrew Jeffery verbosity = (verbose)2;
3930bcf84cSAndrew Jeffery
4030bcf84cSAndrew Jeffery test::VpnorRoot root(ctx, toc, BLOCK_SIZE);
4130bcf84cSAndrew Jeffery /* write_flash() doesn't copy the file for us */
4230bcf84cSAndrew Jeffery assert(fs::copy_file(root.ro() / "TEST1", root.rw() / "TEST1"));
4330bcf84cSAndrew Jeffery init_vpnor_from_paths(ctx);
4430bcf84cSAndrew Jeffery
4530bcf84cSAndrew Jeffery /* Test */
4630bcf84cSAndrew Jeffery memset(src, 0xbb, sizeof(src));
4730bcf84cSAndrew Jeffery rc = write_flash(ctx, 0x1000, src, sizeof(src));
4830bcf84cSAndrew Jeffery assert(rc == 0);
4930bcf84cSAndrew Jeffery fd = open((root.rw() / "TEST1").c_str(), O_RDONLY);
50*506f2f55SAndrew Jeffery map = mmap(NULL, sizeof(src), PROT_READ, MAP_SHARED, fd, 0);
5130bcf84cSAndrew Jeffery assert(map != MAP_FAILED);
5230bcf84cSAndrew Jeffery rc = memcmp(src, map, sizeof(src));
5330bcf84cSAndrew Jeffery assert(rc == 0);
5430bcf84cSAndrew Jeffery
5530bcf84cSAndrew Jeffery /* Ensure single byte writes function */
5630bcf84cSAndrew Jeffery memset(src, 0xcc, sizeof(src));
5730bcf84cSAndrew Jeffery rc = write_flash(ctx, 0x1000, src, sizeof(src));
5830bcf84cSAndrew Jeffery assert(rc == 0);
5930bcf84cSAndrew Jeffery rc = memcmp(src, map, sizeof(src));
6030bcf84cSAndrew Jeffery assert(rc == 0);
6130bcf84cSAndrew Jeffery
6230bcf84cSAndrew Jeffery src[0] = 0xff;
6330bcf84cSAndrew Jeffery rc = write_flash(ctx, 0x1000, src, 1);
6430bcf84cSAndrew Jeffery assert(rc == 0);
6530bcf84cSAndrew Jeffery rc = memcmp(src, map, sizeof(src));
6630bcf84cSAndrew Jeffery assert(rc == 0);
6730bcf84cSAndrew Jeffery
6830bcf84cSAndrew Jeffery src[1] = 0xff;
6930bcf84cSAndrew Jeffery rc = write_flash(ctx, 0x1000 + 1, &src[1], 1);
7030bcf84cSAndrew Jeffery assert(rc == 0);
7130bcf84cSAndrew Jeffery rc = memcmp(src, map, sizeof(src));
7230bcf84cSAndrew Jeffery assert(rc == 0);
7330bcf84cSAndrew Jeffery
7430bcf84cSAndrew Jeffery src[2] = 0xff;
7530bcf84cSAndrew Jeffery rc = write_flash(ctx, 0x1000 + 2, &src[2], 1);
7630bcf84cSAndrew Jeffery assert(rc == 0);
7730bcf84cSAndrew Jeffery rc = memcmp(src, map, sizeof(src));
7830bcf84cSAndrew Jeffery assert(rc == 0);
7930bcf84cSAndrew Jeffery
8030bcf84cSAndrew Jeffery /* Writes past the end of the partition should fail */
8130bcf84cSAndrew Jeffery rc = write_flash(ctx, 0x1000 + 0xff9, src, sizeof(src));
8230bcf84cSAndrew Jeffery assert(rc < 0);
8330bcf84cSAndrew Jeffery
8430bcf84cSAndrew Jeffery /* Check that RW file is unmodified after the bad write */
8530bcf84cSAndrew Jeffery fd = open((root.rw() / "TEST1").c_str(), O_RDONLY);
8630bcf84cSAndrew Jeffery map = mmap(NULL, sizeof(src), PROT_READ, MAP_SHARED, fd, 0);
8730bcf84cSAndrew Jeffery assert(map != MAP_FAILED);
8830bcf84cSAndrew Jeffery rc = memcmp(src, map, sizeof(src));
8930bcf84cSAndrew Jeffery assert(rc == 0);
9030bcf84cSAndrew Jeffery
9130bcf84cSAndrew Jeffery munmap(map, sizeof(src));
9230bcf84cSAndrew Jeffery close(fd);
9330bcf84cSAndrew Jeffery
9430bcf84cSAndrew Jeffery destroy_vpnor(ctx);
9530bcf84cSAndrew Jeffery
9630bcf84cSAndrew Jeffery return 0;
9730bcf84cSAndrew Jeffery }
98