blob: 8ff40ad0ae4de6c80491d405f80c0893ec096708 [file] [log] [blame]
Andrew Jefferyd083efd2018-02-21 21:19:00 +10301/* SPDX-License-Identifier: Apache-2.0 */
2/* Copyright (C) 2018 IBM Corp. */
3
4#include <assert.h>
5#include <string.h>
6#include <vector>
7#include <fstream>
8#include <experimental/filesystem>
9
10#include "config.h"
Andrew Jeffery32476cb2018-02-22 15:34:17 +103011#include "mbox.h"
Andrew Jefferyd083efd2018-02-21 21:19:00 +103012#include "pnor_partition_table.hpp"
13
Andrew Jefferyd083efd2018-02-21 21:19:00 +103014namespace openpower
15{
16namespace virtual_pnor
17{
18namespace test
19{
20
Andrew Jeffery812923d2018-02-22 11:59:52 +103021namespace fs = std::experimental::filesystem;
22
23class VpnorRoot
Andrew Jefferyd083efd2018-02-21 21:19:00 +103024{
Andrew Jeffery812923d2018-02-22 11:59:52 +103025 public:
26 template <std::size_t N>
Andrew Jeffery32476cb2018-02-22 15:34:17 +103027 VpnorRoot(struct mbox_context* ctx, const std::string (&toc)[N],
28 size_t blockSize)
Andrew Jefferyd083efd2018-02-21 21:19:00 +103029 {
Andrew Jeffery812923d2018-02-22 11:59:52 +103030 char tmplt[] = "/tmp/vpnor_root.XXXXXX";
31 char* tmpdir = mkdtemp(tmplt);
32 root = fs::path{tmpdir};
Andrew Jefferyd083efd2018-02-21 21:19:00 +103033
Andrew Jeffery097495c2018-02-22 11:36:13 +103034 for (const auto& attr : attributes)
35 {
36 fs::create_directory(root / attr);
37 }
38
39 fs::path tocFilePath = root / "ro" / PARTITION_TOC_FILE;
Andrew Jefferyd083efd2018-02-21 21:19:00 +103040
Andrew Jeffery812923d2018-02-22 11:59:52 +103041 for (const std::string& line : toc)
42 {
43 pnor_partition part;
Andrew Jefferyd083efd2018-02-21 21:19:00 +103044
Andrew Jeffery812923d2018-02-22 11:59:52 +103045 openpower::virtual_pnor::parseTocLine(line, blockSize, part);
46
47 /* Populate the partition in the tree */
Andrew Jeffery097495c2018-02-22 11:36:13 +103048 std::vector<char> zeroed(part.data.actual, 0);
49 fs::path partitionFilePath = root / "ro" / part.data.name;
50 std::ofstream(partitionFilePath)
51 .write(zeroed.data(), zeroed.size());
Andrew Jeffery812923d2018-02-22 11:59:52 +103052
53 /* Update the ToC if the partition file was created */
Andrew Jeffery097495c2018-02-22 11:36:13 +103054 std::ofstream(tocFilePath, std::ofstream::app) << line << "\n";
Andrew Jeffery812923d2018-02-22 11:59:52 +103055 }
Andrew Jeffery32476cb2018-02-22 15:34:17 +103056
57 strncpy(ctx->paths.ro_loc, ro().c_str(), PATH_MAX - 1);
58 ctx->paths.ro_loc[PATH_MAX - 1] = '\0';
59 strncpy(ctx->paths.rw_loc, rw().c_str(), PATH_MAX - 1);
60 ctx->paths.rw_loc[PATH_MAX - 1] = '\0';
61 strncpy(ctx->paths.prsv_loc, prsv().c_str(), PATH_MAX - 1);
62 ctx->paths.prsv_loc[PATH_MAX - 1] = '\0';
63 strncpy(ctx->paths.patch_loc, patch().c_str(), PATH_MAX - 1);
64 ctx->paths.patch_loc[PATH_MAX - 1] = '\0';
Andrew Jefferyd083efd2018-02-21 21:19:00 +103065 }
66
Andrew Jeffery812923d2018-02-22 11:59:52 +103067 VpnorRoot(const VpnorRoot&) = delete;
68 VpnorRoot& operator=(const VpnorRoot&) = delete;
69 VpnorRoot(VpnorRoot&&) = delete;
70 VpnorRoot& operator=(VpnorRoot&&) = delete;
71
72 ~VpnorRoot()
73 {
74 fs::remove_all(root);
75 }
Andrew Jeffery097495c2018-02-22 11:36:13 +103076 fs::path ro()
Andrew Jeffery812923d2018-02-22 11:59:52 +103077 {
Andrew Jeffery097495c2018-02-22 11:36:13 +103078 return fs::path{root} / "ro";
79 }
80 fs::path rw()
81 {
82 return fs::path{root} / "rw";
83 }
84 fs::path prsv()
85 {
86 return fs::path{root} / "prsv";
87 }
88 fs::path patch()
89 {
90 return fs::path{root} / "patch";
Andrew Jeffery812923d2018-02-22 11:59:52 +103091 }
92 size_t write(const std::string& name, const void* data, size_t len);
93
94 private:
95 fs::path root;
Andrew Jeffery097495c2018-02-22 11:36:13 +103096 const std::string attributes[4] = {"ro", "rw", "prsv", "patch"};
Andrew Jeffery812923d2018-02-22 11:59:52 +103097};
Andrew Jefferyd083efd2018-02-21 21:19:00 +103098
99} // test
100} // virtual_pnor
101} // openpower