blob: 7386f2fc13e430381c2a52f11838a6d680185d01 [file] [log] [blame]
Andrew Jefferyd083efd2018-02-21 21:19:00 +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 "mboxd.h"
8}
9
William A. Kennington IIId5f1d402018-10-11 13:55:04 -070010#include "vpnor/pnor_partition_table.hpp"
11
Andrew Jeffery261f61a2019-03-14 09:57:28 +103012#include <cassert>
13#include <cstring>
William A. Kennington IIId5f1d402018-10-11 13:55:04 -070014#include <experimental/filesystem>
15#include <fstream>
16#include <vector>
17
Andrew Jefferyd083efd2018-02-21 21:19:00 +103018namespace openpower
19{
20namespace virtual_pnor
21{
22namespace test
23{
24
Andrew Jeffery812923d2018-02-22 11:59:52 +103025namespace fs = std::experimental::filesystem;
26
27class VpnorRoot
Andrew Jefferyd083efd2018-02-21 21:19:00 +103028{
Andrew Jeffery812923d2018-02-22 11:59:52 +103029 public:
30 template <std::size_t N>
Andrew Jeffery32476cb2018-02-22 15:34:17 +103031 VpnorRoot(struct mbox_context* ctx, const std::string (&toc)[N],
32 size_t blockSize)
Andrew Jefferyd083efd2018-02-21 21:19:00 +103033 {
Andrew Jeffery812923d2018-02-22 11:59:52 +103034 char tmplt[] = "/tmp/vpnor_root.XXXXXX";
35 char* tmpdir = mkdtemp(tmplt);
36 root = fs::path{tmpdir};
Andrew Jefferyd083efd2018-02-21 21:19:00 +103037
Andrew Jeffery097495c2018-02-22 11:36:13 +103038 for (const auto& attr : attributes)
39 {
40 fs::create_directory(root / attr);
41 }
42
43 fs::path tocFilePath = root / "ro" / PARTITION_TOC_FILE;
Andrew Jefferyd083efd2018-02-21 21:19:00 +103044
Andrew Jeffery812923d2018-02-22 11:59:52 +103045 for (const std::string& line : toc)
46 {
47 pnor_partition part;
Andrew Jefferyd083efd2018-02-21 21:19:00 +103048
Andrew Jeffery812923d2018-02-22 11:59:52 +103049 openpower::virtual_pnor::parseTocLine(line, blockSize, part);
50
51 /* Populate the partition in the tree */
Andrew Jeffery097495c2018-02-22 11:36:13 +103052 std::vector<char> zeroed(part.data.actual, 0);
53 fs::path partitionFilePath = root / "ro" / part.data.name;
54 std::ofstream(partitionFilePath)
55 .write(zeroed.data(), zeroed.size());
Andrew Jeffery812923d2018-02-22 11:59:52 +103056
57 /* Update the ToC if the partition file was created */
Andrew Jeffery097495c2018-02-22 11:36:13 +103058 std::ofstream(tocFilePath, std::ofstream::app) << line << "\n";
Andrew Jeffery812923d2018-02-22 11:59:52 +103059 }
Andrew Jeffery32476cb2018-02-22 15:34:17 +103060
61 strncpy(ctx->paths.ro_loc, ro().c_str(), PATH_MAX - 1);
62 ctx->paths.ro_loc[PATH_MAX - 1] = '\0';
63 strncpy(ctx->paths.rw_loc, rw().c_str(), PATH_MAX - 1);
64 ctx->paths.rw_loc[PATH_MAX - 1] = '\0';
65 strncpy(ctx->paths.prsv_loc, prsv().c_str(), PATH_MAX - 1);
66 ctx->paths.prsv_loc[PATH_MAX - 1] = '\0';
67 strncpy(ctx->paths.patch_loc, patch().c_str(), PATH_MAX - 1);
68 ctx->paths.patch_loc[PATH_MAX - 1] = '\0';
Andrew Jefferyd083efd2018-02-21 21:19:00 +103069 }
70
Andrew Jeffery812923d2018-02-22 11:59:52 +103071 VpnorRoot(const VpnorRoot&) = delete;
72 VpnorRoot& operator=(const VpnorRoot&) = delete;
73 VpnorRoot(VpnorRoot&&) = delete;
74 VpnorRoot& operator=(VpnorRoot&&) = delete;
75
76 ~VpnorRoot()
77 {
78 fs::remove_all(root);
79 }
Andrew Jeffery097495c2018-02-22 11:36:13 +103080 fs::path ro()
Andrew Jeffery812923d2018-02-22 11:59:52 +103081 {
Andrew Jeffery097495c2018-02-22 11:36:13 +103082 return fs::path{root} / "ro";
83 }
84 fs::path rw()
85 {
86 return fs::path{root} / "rw";
87 }
88 fs::path prsv()
89 {
90 return fs::path{root} / "prsv";
91 }
92 fs::path patch()
93 {
94 return fs::path{root} / "patch";
Andrew Jeffery812923d2018-02-22 11:59:52 +103095 }
96 size_t write(const std::string& name, const void* data, size_t len);
Andrew Jeffery2a4bee72018-03-01 15:49:17 +103097 size_t patch(const std::string& name, const void* data, size_t len);
Andrew Jeffery812923d2018-02-22 11:59:52 +103098
99 private:
100 fs::path root;
Andrew Jeffery097495c2018-02-22 11:36:13 +1030101 const std::string attributes[4] = {"ro", "rw", "prsv", "patch"};
Andrew Jeffery812923d2018-02-22 11:59:52 +1030102};
Andrew Jefferyd083efd2018-02-21 21:19:00 +1030103
Andrew Jeffery7a03b072018-09-05 17:34:49 +0930104} // namespace test
105} // namespace virtual_pnor
106} // namespace openpower