blob: 2239f9ca70a28cfeceb5bac4f85fbdc470d672d3 [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
6#include "vpnor/pnor_partition_table.hpp"
7
Andrew Jefferyd083efd2018-02-21 21:19:00 +10308#include <assert.h>
9#include <string.h>
Andrew Jefferyd083efd2018-02-21 21:19:00 +103010
William A. Kennington IIId5f1d402018-10-11 13:55:04 -070011#include <experimental/filesystem>
12#include <fstream>
13#include <vector>
14
Andrew Jeffery26558db2018-08-10 00:22:38 +093015#include "mboxd.h"
Andrew Jefferyd083efd2018-02-21 21:19:00 +103016
Andrew Jefferyd083efd2018-02-21 21:19:00 +103017namespace openpower
18{
19namespace virtual_pnor
20{
21namespace test
22{
23
Andrew Jeffery812923d2018-02-22 11:59:52 +103024namespace fs = std::experimental::filesystem;
25
26class VpnorRoot
Andrew Jefferyd083efd2018-02-21 21:19:00 +103027{
Andrew Jeffery812923d2018-02-22 11:59:52 +103028 public:
29 template <std::size_t N>
Andrew Jeffery32476cb2018-02-22 15:34:17 +103030 VpnorRoot(struct mbox_context* ctx, const std::string (&toc)[N],
31 size_t blockSize)
Andrew Jefferyd083efd2018-02-21 21:19:00 +103032 {
Andrew Jeffery812923d2018-02-22 11:59:52 +103033 char tmplt[] = "/tmp/vpnor_root.XXXXXX";
34 char* tmpdir = mkdtemp(tmplt);
35 root = fs::path{tmpdir};
Andrew Jefferyd083efd2018-02-21 21:19:00 +103036
Andrew Jeffery097495c2018-02-22 11:36:13 +103037 for (const auto& attr : attributes)
38 {
39 fs::create_directory(root / attr);
40 }
41
42 fs::path tocFilePath = root / "ro" / PARTITION_TOC_FILE;
Andrew Jefferyd083efd2018-02-21 21:19:00 +103043
Andrew Jeffery812923d2018-02-22 11:59:52 +103044 for (const std::string& line : toc)
45 {
46 pnor_partition part;
Andrew Jefferyd083efd2018-02-21 21:19:00 +103047
Andrew Jeffery812923d2018-02-22 11:59:52 +103048 openpower::virtual_pnor::parseTocLine(line, blockSize, part);
49
50 /* Populate the partition in the tree */
Andrew Jeffery097495c2018-02-22 11:36:13 +103051 std::vector<char> zeroed(part.data.actual, 0);
52 fs::path partitionFilePath = root / "ro" / part.data.name;
53 std::ofstream(partitionFilePath)
54 .write(zeroed.data(), zeroed.size());
Andrew Jeffery812923d2018-02-22 11:59:52 +103055
56 /* Update the ToC if the partition file was created */
Andrew Jeffery097495c2018-02-22 11:36:13 +103057 std::ofstream(tocFilePath, std::ofstream::app) << line << "\n";
Andrew Jeffery812923d2018-02-22 11:59:52 +103058 }
Andrew Jeffery32476cb2018-02-22 15:34:17 +103059
60 strncpy(ctx->paths.ro_loc, ro().c_str(), PATH_MAX - 1);
61 ctx->paths.ro_loc[PATH_MAX - 1] = '\0';
62 strncpy(ctx->paths.rw_loc, rw().c_str(), PATH_MAX - 1);
63 ctx->paths.rw_loc[PATH_MAX - 1] = '\0';
64 strncpy(ctx->paths.prsv_loc, prsv().c_str(), PATH_MAX - 1);
65 ctx->paths.prsv_loc[PATH_MAX - 1] = '\0';
66 strncpy(ctx->paths.patch_loc, patch().c_str(), PATH_MAX - 1);
67 ctx->paths.patch_loc[PATH_MAX - 1] = '\0';
Andrew Jefferyd083efd2018-02-21 21:19:00 +103068 }
69
Andrew Jeffery812923d2018-02-22 11:59:52 +103070 VpnorRoot(const VpnorRoot&) = delete;
71 VpnorRoot& operator=(const VpnorRoot&) = delete;
72 VpnorRoot(VpnorRoot&&) = delete;
73 VpnorRoot& operator=(VpnorRoot&&) = delete;
74
75 ~VpnorRoot()
76 {
77 fs::remove_all(root);
78 }
Andrew Jeffery097495c2018-02-22 11:36:13 +103079 fs::path ro()
Andrew Jeffery812923d2018-02-22 11:59:52 +103080 {
Andrew Jeffery097495c2018-02-22 11:36:13 +103081 return fs::path{root} / "ro";
82 }
83 fs::path rw()
84 {
85 return fs::path{root} / "rw";
86 }
87 fs::path prsv()
88 {
89 return fs::path{root} / "prsv";
90 }
91 fs::path patch()
92 {
93 return fs::path{root} / "patch";
Andrew Jeffery812923d2018-02-22 11:59:52 +103094 }
95 size_t write(const std::string& name, const void* data, size_t len);
Andrew Jeffery2a4bee72018-03-01 15:49:17 +103096 size_t patch(const std::string& name, const void* data, size_t len);
Andrew Jeffery812923d2018-02-22 11:59:52 +103097
98 private:
99 fs::path root;
Andrew Jeffery097495c2018-02-22 11:36:13 +1030100 const std::string attributes[4] = {"ro", "rw", "prsv", "patch"};
Andrew Jeffery812923d2018-02-22 11:59:52 +1030101};
Andrew Jefferyd083efd2018-02-21 21:19:00 +1030102
Andrew Jeffery7a03b072018-09-05 17:34:49 +0930103} // namespace test
104} // namespace virtual_pnor
105} // namespace openpower