130bcf84cSAndrew Jeffery /* SPDX-License-Identifier: Apache-2.0 */ 230bcf84cSAndrew Jeffery /* Copyright (C) 2018 IBM Corp. */ 330bcf84cSAndrew Jeffery 430bcf84cSAndrew Jeffery #include <assert.h> 530bcf84cSAndrew Jeffery #include <string.h> 630bcf84cSAndrew Jeffery #include <vector> 730bcf84cSAndrew Jeffery #include <fstream> 830bcf84cSAndrew Jeffery #include <experimental/filesystem> 930bcf84cSAndrew Jeffery 1030bcf84cSAndrew Jeffery #include "config.h" 1130bcf84cSAndrew Jeffery #include "mbox.h" 1230bcf84cSAndrew Jeffery #include "vpnor/pnor_partition_table.hpp" 1330bcf84cSAndrew Jeffery 1430bcf84cSAndrew Jeffery namespace openpower 1530bcf84cSAndrew Jeffery { 1630bcf84cSAndrew Jeffery namespace virtual_pnor 1730bcf84cSAndrew Jeffery { 1830bcf84cSAndrew Jeffery namespace test 1930bcf84cSAndrew Jeffery { 2030bcf84cSAndrew Jeffery 2130bcf84cSAndrew Jeffery namespace fs = std::experimental::filesystem; 2230bcf84cSAndrew Jeffery 2330bcf84cSAndrew Jeffery class VpnorRoot 2430bcf84cSAndrew Jeffery { 2530bcf84cSAndrew Jeffery public: 2630bcf84cSAndrew Jeffery template <std::size_t N> 2730bcf84cSAndrew Jeffery VpnorRoot(struct mbox_context* ctx, const std::string (&toc)[N], 2830bcf84cSAndrew Jeffery size_t blockSize) 2930bcf84cSAndrew Jeffery { 3030bcf84cSAndrew Jeffery char tmplt[] = "/tmp/vpnor_root.XXXXXX"; 3130bcf84cSAndrew Jeffery char* tmpdir = mkdtemp(tmplt); 3230bcf84cSAndrew Jeffery root = fs::path{tmpdir}; 3330bcf84cSAndrew Jeffery 3430bcf84cSAndrew Jeffery for (const auto& attr : attributes) 3530bcf84cSAndrew Jeffery { 3630bcf84cSAndrew Jeffery fs::create_directory(root / attr); 3730bcf84cSAndrew Jeffery } 3830bcf84cSAndrew Jeffery 3930bcf84cSAndrew Jeffery fs::path tocFilePath = root / "ro" / PARTITION_TOC_FILE; 4030bcf84cSAndrew Jeffery 4130bcf84cSAndrew Jeffery for (const std::string& line : toc) 4230bcf84cSAndrew Jeffery { 4330bcf84cSAndrew Jeffery pnor_partition part; 4430bcf84cSAndrew Jeffery 4530bcf84cSAndrew Jeffery openpower::virtual_pnor::parseTocLine(line, blockSize, part); 4630bcf84cSAndrew Jeffery 4730bcf84cSAndrew Jeffery /* Populate the partition in the tree */ 4830bcf84cSAndrew Jeffery std::vector<char> zeroed(part.data.actual, 0); 4930bcf84cSAndrew Jeffery fs::path partitionFilePath = root / "ro" / part.data.name; 5030bcf84cSAndrew Jeffery std::ofstream(partitionFilePath) 5130bcf84cSAndrew Jeffery .write(zeroed.data(), zeroed.size()); 5230bcf84cSAndrew Jeffery 5330bcf84cSAndrew Jeffery /* Update the ToC if the partition file was created */ 5430bcf84cSAndrew Jeffery std::ofstream(tocFilePath, std::ofstream::app) << line << "\n"; 5530bcf84cSAndrew Jeffery } 5630bcf84cSAndrew Jeffery 5730bcf84cSAndrew Jeffery strncpy(ctx->paths.ro_loc, ro().c_str(), PATH_MAX - 1); 5830bcf84cSAndrew Jeffery ctx->paths.ro_loc[PATH_MAX - 1] = '\0'; 5930bcf84cSAndrew Jeffery strncpy(ctx->paths.rw_loc, rw().c_str(), PATH_MAX - 1); 6030bcf84cSAndrew Jeffery ctx->paths.rw_loc[PATH_MAX - 1] = '\0'; 6130bcf84cSAndrew Jeffery strncpy(ctx->paths.prsv_loc, prsv().c_str(), PATH_MAX - 1); 6230bcf84cSAndrew Jeffery ctx->paths.prsv_loc[PATH_MAX - 1] = '\0'; 6330bcf84cSAndrew Jeffery strncpy(ctx->paths.patch_loc, patch().c_str(), PATH_MAX - 1); 6430bcf84cSAndrew Jeffery ctx->paths.patch_loc[PATH_MAX - 1] = '\0'; 6530bcf84cSAndrew Jeffery } 6630bcf84cSAndrew Jeffery 6730bcf84cSAndrew Jeffery VpnorRoot(const VpnorRoot&) = delete; 6830bcf84cSAndrew Jeffery VpnorRoot& operator=(const VpnorRoot&) = delete; 6930bcf84cSAndrew Jeffery VpnorRoot(VpnorRoot&&) = delete; 7030bcf84cSAndrew Jeffery VpnorRoot& operator=(VpnorRoot&&) = delete; 7130bcf84cSAndrew Jeffery 7230bcf84cSAndrew Jeffery ~VpnorRoot() 7330bcf84cSAndrew Jeffery { 7430bcf84cSAndrew Jeffery fs::remove_all(root); 7530bcf84cSAndrew Jeffery } 7630bcf84cSAndrew Jeffery fs::path ro() 7730bcf84cSAndrew Jeffery { 7830bcf84cSAndrew Jeffery return fs::path{root} / "ro"; 7930bcf84cSAndrew Jeffery } 8030bcf84cSAndrew Jeffery fs::path rw() 8130bcf84cSAndrew Jeffery { 8230bcf84cSAndrew Jeffery return fs::path{root} / "rw"; 8330bcf84cSAndrew Jeffery } 8430bcf84cSAndrew Jeffery fs::path prsv() 8530bcf84cSAndrew Jeffery { 8630bcf84cSAndrew Jeffery return fs::path{root} / "prsv"; 8730bcf84cSAndrew Jeffery } 8830bcf84cSAndrew Jeffery fs::path patch() 8930bcf84cSAndrew Jeffery { 9030bcf84cSAndrew Jeffery return fs::path{root} / "patch"; 9130bcf84cSAndrew Jeffery } 9230bcf84cSAndrew Jeffery size_t write(const std::string& name, const void* data, size_t len); 9330bcf84cSAndrew Jeffery size_t patch(const std::string& name, const void* data, size_t len); 9430bcf84cSAndrew Jeffery 9530bcf84cSAndrew Jeffery private: 9630bcf84cSAndrew Jeffery fs::path root; 9730bcf84cSAndrew Jeffery const std::string attributes[4] = {"ro", "rw", "prsv", "patch"}; 9830bcf84cSAndrew Jeffery }; 9930bcf84cSAndrew Jeffery 100*7a03b074SAndrew Jeffery } // namespace test 101*7a03b074SAndrew Jeffery } // namespace virtual_pnor 102*7a03b074SAndrew Jeffery } // namespace openpower 103