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