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 | |
| 4 | #include <assert.h> |
| 5 | #include <string.h> |
| 6 | #include <vector> |
| 7 | #include <fstream> |
| 8 | #include <experimental/filesystem> |
| 9 | |
| 10 | #include "config.h" |
Andrew Jeffery | 32476cb | 2018-02-22 15:34:17 +1030 | [diff] [blame] | 11 | #include "mbox.h" |
Andrew Jeffery | 53c21aa | 2018-03-26 11:56:16 +1030 | [diff] [blame] | 12 | #include "vpnor/pnor_partition_table.hpp" |
Andrew Jeffery | d083efd | 2018-02-21 21:19:00 +1030 | [diff] [blame] | 13 | |
Andrew Jeffery | d083efd | 2018-02-21 21:19:00 +1030 | [diff] [blame] | 14 | namespace openpower |
| 15 | { |
| 16 | namespace virtual_pnor |
| 17 | { |
| 18 | namespace test |
| 19 | { |
| 20 | |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 21 | namespace fs = std::experimental::filesystem; |
| 22 | |
| 23 | class VpnorRoot |
Andrew Jeffery | d083efd | 2018-02-21 21:19:00 +1030 | [diff] [blame] | 24 | { |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 25 | public: |
| 26 | template <std::size_t N> |
Andrew Jeffery | 32476cb | 2018-02-22 15:34:17 +1030 | [diff] [blame] | 27 | VpnorRoot(struct mbox_context* ctx, const std::string (&toc)[N], |
| 28 | size_t blockSize) |
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 | char tmplt[] = "/tmp/vpnor_root.XXXXXX"; |
| 31 | char* tmpdir = mkdtemp(tmplt); |
| 32 | root = fs::path{tmpdir}; |
Andrew Jeffery | d083efd | 2018-02-21 21:19:00 +1030 | [diff] [blame] | 33 | |
Andrew Jeffery | 097495c | 2018-02-22 11:36:13 +1030 | [diff] [blame] | 34 | for (const auto& attr : attributes) |
| 35 | { |
| 36 | fs::create_directory(root / attr); |
| 37 | } |
| 38 | |
| 39 | fs::path tocFilePath = root / "ro" / PARTITION_TOC_FILE; |
Andrew Jeffery | d083efd | 2018-02-21 21:19:00 +1030 | [diff] [blame] | 40 | |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 41 | for (const std::string& line : toc) |
| 42 | { |
| 43 | pnor_partition part; |
Andrew Jeffery | d083efd | 2018-02-21 21:19:00 +1030 | [diff] [blame] | 44 | |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 45 | openpower::virtual_pnor::parseTocLine(line, blockSize, part); |
| 46 | |
| 47 | /* Populate the partition in the tree */ |
Andrew Jeffery | 097495c | 2018-02-22 11:36:13 +1030 | [diff] [blame] | 48 | 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 Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 52 | |
| 53 | /* Update the ToC if the partition file was created */ |
Andrew Jeffery | 097495c | 2018-02-22 11:36:13 +1030 | [diff] [blame] | 54 | std::ofstream(tocFilePath, std::ofstream::app) << line << "\n"; |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 55 | } |
Andrew Jeffery | 32476cb | 2018-02-22 15:34:17 +1030 | [diff] [blame] | 56 | |
| 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 Jeffery | d083efd | 2018-02-21 21:19:00 +1030 | [diff] [blame] | 65 | } |
| 66 | |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 67 | 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 Jeffery | 097495c | 2018-02-22 11:36:13 +1030 | [diff] [blame] | 76 | fs::path ro() |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 77 | { |
Andrew Jeffery | 097495c | 2018-02-22 11:36:13 +1030 | [diff] [blame] | 78 | 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 Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 91 | } |
| 92 | 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] | 93 | 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] | 94 | |
| 95 | private: |
| 96 | fs::path root; |
Andrew Jeffery | 097495c | 2018-02-22 11:36:13 +1030 | [diff] [blame] | 97 | const std::string attributes[4] = {"ro", "rw", "prsv", "patch"}; |
Andrew Jeffery | 812923d | 2018-02-22 11:59:52 +1030 | [diff] [blame] | 98 | }; |
Andrew Jeffery | d083efd | 2018-02-21 21:19:00 +1030 | [diff] [blame] | 99 | |
| 100 | } // test |
| 101 | } // virtual_pnor |
| 102 | } // openpower |