xref: /openbmc/hiomapd/vpnor/test/tmpd.hpp (revision d5f1d40f2ddf9234099b331e1d90c54ece0ee88b)
1 /* SPDX-License-Identifier: Apache-2.0 */
2 /* Copyright (C) 2018 IBM Corp. */
3 
4 #include "config.h"
5 
6 #include "vpnor/pnor_partition_table.hpp"
7 
8 #include <assert.h>
9 #include <string.h>
10 
11 #include <experimental/filesystem>
12 #include <fstream>
13 #include <vector>
14 
15 #include "mboxd.h"
16 
17 namespace openpower
18 {
19 namespace virtual_pnor
20 {
21 namespace test
22 {
23 
24 namespace fs = std::experimental::filesystem;
25 
26 class VpnorRoot
27 {
28   public:
29     template <std::size_t N>
30     VpnorRoot(struct mbox_context* ctx, const std::string (&toc)[N],
31               size_t blockSize)
32     {
33         char tmplt[] = "/tmp/vpnor_root.XXXXXX";
34         char* tmpdir = mkdtemp(tmplt);
35         root = fs::path{tmpdir};
36 
37         for (const auto& attr : attributes)
38         {
39             fs::create_directory(root / attr);
40         }
41 
42         fs::path tocFilePath = root / "ro" / PARTITION_TOC_FILE;
43 
44         for (const std::string& line : toc)
45         {
46             pnor_partition part;
47 
48             openpower::virtual_pnor::parseTocLine(line, blockSize, part);
49 
50             /* Populate the partition in the tree */
51             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());
55 
56             /* Update the ToC if the partition file was created */
57             std::ofstream(tocFilePath, std::ofstream::app) << line << "\n";
58         }
59 
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';
68     }
69 
70     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     }
79     fs::path ro()
80     {
81         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";
94     }
95     size_t write(const std::string& name, const void* data, size_t len);
96     size_t patch(const std::string& name, const void* data, size_t len);
97 
98   private:
99     fs::path root;
100     const std::string attributes[4] = {"ro", "rw", "prsv", "patch"};
101 };
102 
103 } // namespace test
104 } // namespace virtual_pnor
105 } // namespace openpower
106