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 | |
Andrew Jeffery | 261f61a | 2019-03-14 09:57:28 +1030 | [diff] [blame] | 6 | extern "C" { |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 7 | #include "backend.h" |
Andrew Jeffery | 261f61a | 2019-03-14 09:57:28 +1030 | [diff] [blame] | 8 | #include "mboxd.h" |
| 9 | } |
| 10 | |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 11 | #include "vpnor/pnor_partition_table.hpp" |
| 12 | |
Andrew Jeffery | 261f61a | 2019-03-14 09:57:28 +1030 | [diff] [blame] | 13 | #include <cassert> |
| 14 | #include <cstring> |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 15 | #include <experimental/filesystem> |
| 16 | #include <fstream> |
| 17 | #include <vector> |
| 18 | |
Andrew Jeffery | d083efd | 2018-02-21 21:19:00 +1030 | [diff] [blame] | 19 | namespace openpower |
| 20 | { |
| 21 | namespace virtual_pnor |
| 22 | { |
| 23 | namespace test |
| 24 | { |
| 25 | |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 26 | namespace fs = std::experimental::filesystem; |
| 27 | |
| 28 | class VpnorRoot |
Andrew Jeffery | d083efd | 2018-02-21 21:19:00 +1030 | [diff] [blame] | 29 | { |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 30 | public: |
| 31 | template <std::size_t N> |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 32 | VpnorRoot(struct backend* backend, const std::string (&toc)[N], |
| 33 | size_t blockSize) : |
| 34 | backend(backend) |
Andrew Jeffery | d083efd | 2018-02-21 21:19:00 +1030 | [diff] [blame] | 35 | { |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 36 | char tmplt[] = "/tmp/vpnor_root.XXXXXX"; |
| 37 | char* tmpdir = mkdtemp(tmplt); |
| 38 | root = fs::path{tmpdir}; |
Andrew Jeffery | d083efd | 2018-02-21 21:19:00 +1030 | [diff] [blame] | 39 | |
Andrew Jeffery | 097495c | 2018-02-22 11:36:13 +1030 | [diff] [blame] | 40 | for (const auto& attr : attributes) |
| 41 | { |
| 42 | fs::create_directory(root / attr); |
| 43 | } |
| 44 | |
| 45 | fs::path tocFilePath = root / "ro" / PARTITION_TOC_FILE; |
Andrew Jeffery | d083efd | 2018-02-21 21:19:00 +1030 | [diff] [blame] | 46 | |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 47 | for (const std::string& line : toc) |
| 48 | { |
| 49 | pnor_partition part; |
Andrew Jeffery | d083efd | 2018-02-21 21:19:00 +1030 | [diff] [blame] | 50 | |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 51 | openpower::virtual_pnor::parseTocLine(line, blockSize, part); |
| 52 | |
| 53 | /* Populate the partition in the tree */ |
Andrew Jeffery | 097495c | 2018-02-22 11:36:13 +1030 | [diff] [blame] | 54 | std::vector<char> zeroed(part.data.actual, 0); |
| 55 | fs::path partitionFilePath = root / "ro" / part.data.name; |
| 56 | std::ofstream(partitionFilePath) |
| 57 | .write(zeroed.data(), zeroed.size()); |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 58 | |
| 59 | /* Update the ToC if the partition file was created */ |
Andrew Jeffery | 097495c | 2018-02-22 11:36:13 +1030 | [diff] [blame] | 60 | std::ofstream(tocFilePath, std::ofstream::app) << line << "\n"; |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 61 | } |
Andrew Jeffery | 32476cb | 2018-02-22 15:34:17 +1030 | [diff] [blame] | 62 | |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 63 | vpnor_partition_paths paths{}; |
| 64 | |
| 65 | snprintf(paths.ro_loc, PATH_MAX - 1, "%s/ro", root.c_str()); |
| 66 | paths.ro_loc[PATH_MAX - 1] = '\0'; |
| 67 | snprintf(paths.rw_loc, PATH_MAX - 1, "%s/rw", root.c_str()); |
| 68 | paths.rw_loc[PATH_MAX - 1] = '\0'; |
| 69 | snprintf(paths.prsv_loc, PATH_MAX - 1, "%s/prsv", root.c_str()); |
| 70 | paths.prsv_loc[PATH_MAX - 1] = '\0'; |
| 71 | snprintf(paths.patch_loc, PATH_MAX - 1, "%s/patch", root.c_str()); |
| 72 | paths.patch_loc[PATH_MAX - 1] = '\0'; |
| 73 | |
| 74 | if (backend_probe_vpnor(backend, &paths)) |
| 75 | { |
| 76 | throw std::system_error(errno, std::system_category()); |
| 77 | } |
Andrew Jeffery | d083efd | 2018-02-21 21:19:00 +1030 | [diff] [blame] | 78 | } |
| 79 | |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 80 | VpnorRoot(const VpnorRoot&) = delete; |
| 81 | VpnorRoot& operator=(const VpnorRoot&) = delete; |
| 82 | VpnorRoot(VpnorRoot&&) = delete; |
| 83 | VpnorRoot& operator=(VpnorRoot&&) = delete; |
| 84 | |
| 85 | ~VpnorRoot() |
| 86 | { |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 87 | backend_free(backend); |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 88 | fs::remove_all(root); |
| 89 | } |
Andrew Jeffery | 097495c | 2018-02-22 11:36:13 +1030 | [diff] [blame] | 90 | fs::path ro() |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 91 | { |
Andrew Jeffery | 097495c | 2018-02-22 11:36:13 +1030 | [diff] [blame] | 92 | return fs::path{root} / "ro"; |
| 93 | } |
| 94 | fs::path rw() |
| 95 | { |
| 96 | return fs::path{root} / "rw"; |
| 97 | } |
| 98 | fs::path prsv() |
| 99 | { |
| 100 | return fs::path{root} / "prsv"; |
| 101 | } |
| 102 | fs::path patch() |
| 103 | { |
| 104 | return fs::path{root} / "patch"; |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 105 | } |
| 106 | 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] | 107 | 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] | 108 | |
| 109 | private: |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 110 | struct backend* backend; |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 111 | fs::path root; |
Andrew Jeffery | 097495c | 2018-02-22 11:36:13 +1030 | [diff] [blame] | 112 | const std::string attributes[4] = {"ro", "rw", "prsv", "patch"}; |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 113 | }; |
Andrew Jeffery | d083efd | 2018-02-21 21:19:00 +1030 | [diff] [blame] | 114 | |
Andrew Jeffery | 7a03b07 | 2018-09-05 17:34:49 +0930 | [diff] [blame] | 115 | } // namespace test |
| 116 | } // namespace virtual_pnor |
| 117 | } // namespace openpower |