blob: f6ea7c934572ca971c79ea08501c8e4fa96cae1a [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" {
Evan Lojewskif1e547c2019-03-14 14:34:33 +10307#include "backend.h"
Andrew Jeffery261f61a2019-03-14 09:57:28 +10308#include "mboxd.h"
9}
10
William A. Kennington IIId5f1d402018-10-11 13:55:04 -070011#include "vpnor/pnor_partition_table.hpp"
12
Andrew Jeffery261f61a2019-03-14 09:57:28 +103013#include <cassert>
14#include <cstring>
William A. Kennington IIId5f1d402018-10-11 13:55:04 -070015#include <experimental/filesystem>
16#include <fstream>
17#include <vector>
18
Andrew Jefferyd083efd2018-02-21 21:19:00 +103019namespace openpower
20{
21namespace virtual_pnor
22{
23namespace test
24{
25
Andrew Jeffery812923d2018-02-22 11:59:52 +103026namespace fs = std::experimental::filesystem;
27
28class VpnorRoot
Andrew Jefferyd083efd2018-02-21 21:19:00 +103029{
Andrew Jeffery812923d2018-02-22 11:59:52 +103030 public:
31 template <std::size_t N>
Evan Lojewskif1e547c2019-03-14 14:34:33 +103032 VpnorRoot(struct backend* backend, const std::string (&toc)[N],
33 size_t blockSize) :
34 backend(backend)
Andrew Jefferyd083efd2018-02-21 21:19:00 +103035 {
Andrew Jeffery812923d2018-02-22 11:59:52 +103036 char tmplt[] = "/tmp/vpnor_root.XXXXXX";
37 char* tmpdir = mkdtemp(tmplt);
38 root = fs::path{tmpdir};
Andrew Jefferyd083efd2018-02-21 21:19:00 +103039
Andrew Jeffery097495c2018-02-22 11:36:13 +103040 for (const auto& attr : attributes)
41 {
42 fs::create_directory(root / attr);
43 }
44
45 fs::path tocFilePath = root / "ro" / PARTITION_TOC_FILE;
Andrew Jefferyd083efd2018-02-21 21:19:00 +103046
Andrew Jeffery812923d2018-02-22 11:59:52 +103047 for (const std::string& line : toc)
48 {
49 pnor_partition part;
Andrew Jefferyd083efd2018-02-21 21:19:00 +103050
Andrew Jeffery812923d2018-02-22 11:59:52 +103051 openpower::virtual_pnor::parseTocLine(line, blockSize, part);
52
53 /* Populate the partition in the tree */
Andrew Jeffery097495c2018-02-22 11:36:13 +103054 std::vector<char> zeroed(part.data.actual, 0);
55 fs::path partitionFilePath = root / "ro" / part.data.name;
56 std::ofstream(partitionFilePath)
57 .write(zeroed.data(), zeroed.size());
Andrew Jeffery812923d2018-02-22 11:59:52 +103058
59 /* Update the ToC if the partition file was created */
Andrew Jeffery097495c2018-02-22 11:36:13 +103060 std::ofstream(tocFilePath, std::ofstream::app) << line << "\n";
Andrew Jeffery812923d2018-02-22 11:59:52 +103061 }
Andrew Jeffery32476cb2018-02-22 15:34:17 +103062
Evan Lojewskif1e547c2019-03-14 14:34:33 +103063 vpnor_partition_paths paths{};
64
65 snprintf(paths.ro_loc, PATH_MAX - 1, "%s/ro", root.c_str());
66 paths.ro_loc[PATH_MAX - 1] = '\0';
67 snprintf(paths.rw_loc, PATH_MAX - 1, "%s/rw", root.c_str());
68 paths.rw_loc[PATH_MAX - 1] = '\0';
69 snprintf(paths.prsv_loc, PATH_MAX - 1, "%s/prsv", root.c_str());
70 paths.prsv_loc[PATH_MAX - 1] = '\0';
71 snprintf(paths.patch_loc, PATH_MAX - 1, "%s/patch", root.c_str());
72 paths.patch_loc[PATH_MAX - 1] = '\0';
73
74 if (backend_probe_vpnor(backend, &paths))
75 {
76 throw std::system_error(errno, std::system_category());
77 }
Andrew Jefferyd083efd2018-02-21 21:19:00 +103078 }
79
Andrew Jeffery812923d2018-02-22 11:59:52 +103080 VpnorRoot(const VpnorRoot&) = delete;
81 VpnorRoot& operator=(const VpnorRoot&) = delete;
82 VpnorRoot(VpnorRoot&&) = delete;
83 VpnorRoot& operator=(VpnorRoot&&) = delete;
84
85 ~VpnorRoot()
86 {
Evan Lojewskif1e547c2019-03-14 14:34:33 +103087 backend_free(backend);
Andrew Jeffery812923d2018-02-22 11:59:52 +103088 fs::remove_all(root);
89 }
Andrew Jeffery097495c2018-02-22 11:36:13 +103090 fs::path ro()
Andrew Jeffery812923d2018-02-22 11:59:52 +103091 {
Andrew Jeffery097495c2018-02-22 11:36:13 +103092 return fs::path{root} / "ro";
93 }
94 fs::path rw()
95 {
96 return fs::path{root} / "rw";
97 }
98 fs::path prsv()
99 {
100 return fs::path{root} / "prsv";
101 }
102 fs::path patch()
103 {
104 return fs::path{root} / "patch";
Andrew Jeffery812923d2018-02-22 11:59:52 +1030105 }
106 size_t write(const std::string& name, const void* data, size_t len);
Andrew Jeffery2a4bee72018-03-01 15:49:17 +1030107 size_t patch(const std::string& name, const void* data, size_t len);
Andrew Jeffery812923d2018-02-22 11:59:52 +1030108
109 private:
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030110 struct backend* backend;
Andrew Jeffery812923d2018-02-22 11:59:52 +1030111 fs::path root;
Andrew Jeffery097495c2018-02-22 11:36:13 +1030112 const std::string attributes[4] = {"ro", "rw", "prsv", "patch"};
Andrew Jeffery812923d2018-02-22 11:59:52 +1030113};
Andrew Jefferyd083efd2018-02-21 21:19:00 +1030114
Andrew Jeffery7a03b072018-09-05 17:34:49 +0930115} // namespace test
116} // namespace virtual_pnor
117} // namespace openpower