Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "mboxd_pnor_partition_table.h" |
| 4 | #include <fcntl.h> |
| 5 | #include <string> |
| 6 | #include <unistd.h> |
| 7 | #include <experimental/filesystem> |
| 8 | |
| 9 | namespace openpower |
| 10 | { |
| 11 | namespace file |
| 12 | { |
| 13 | |
| 14 | class Descriptor |
| 15 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 16 | private: |
| 17 | /** default value */ |
| 18 | int fd = -1; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 19 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 20 | public: |
| 21 | Descriptor() = default; |
| 22 | Descriptor(const Descriptor&) = delete; |
| 23 | Descriptor& operator=(const Descriptor&) = delete; |
| 24 | Descriptor(Descriptor&&) = delete; |
| 25 | Descriptor& operator=(Descriptor&&) = delete; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 26 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 27 | Descriptor(int fd) : fd(fd) |
| 28 | { |
| 29 | } |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 30 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 31 | ~Descriptor() |
| 32 | { |
| 33 | if (fd >= 0) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 34 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 35 | close(fd); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 36 | } |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 37 | } |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 38 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 39 | int operator()() const |
| 40 | { |
| 41 | return fd; |
| 42 | } |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 43 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 44 | void set(int descriptor) |
| 45 | { |
| 46 | fd = descriptor; |
| 47 | } |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 48 | }; |
| 49 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 50 | } // namespace file |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 51 | |
| 52 | namespace virtual_pnor |
| 53 | { |
| 54 | |
| 55 | namespace fs = std::experimental::filesystem; |
| 56 | |
| 57 | enum class ReturnCode : uint8_t |
| 58 | { |
| 59 | FILE_NOT_FOUND = 0, |
| 60 | PARTITION_NOT_FOUND = 1, |
| 61 | PARTITION_READ_ONLY = 2, |
| 62 | FILE_OPEN_FAILURE = 3, |
| 63 | SUCCESS = 4, |
| 64 | }; |
| 65 | |
| 66 | class Request |
| 67 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 68 | public: |
| 69 | Request() = default; |
| 70 | Request(const Request&) = delete; |
| 71 | Request& operator=(const Request&) = delete; |
| 72 | Request(Request&&) = default; |
| 73 | Request& operator=(Request&&) = default; |
| 74 | ~Request() = default; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 75 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 76 | openpower::file::Descriptor fd; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 77 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 78 | protected: |
| 79 | /** @brief opens the partition file |
| 80 | * |
| 81 | * @param[in] filePath - Absolute file path. |
| 82 | * @param[in] mode - File open mode. |
| 83 | */ |
| 84 | ReturnCode open(const std::string& filePath, int mode); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 85 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 86 | /** @brief returns the partition file path associated with the offset. |
| 87 | * |
| 88 | * @param[in] context - The mbox context pointer. |
| 89 | * @param[in] offset - The pnor offset(bytes). |
| 90 | */ |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 91 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 92 | std::string getPartitionFilePath(struct mbox_context* context, |
| 93 | uint32_t offset); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 94 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 95 | const pnor_partition* partition = nullptr; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | /** @class RORequest |
| 99 | * @brief Represent the read request of the partition. |
| 100 | * Stores the partition meta data. |
| 101 | */ |
| 102 | class RORequest : public Request |
| 103 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 104 | public: |
| 105 | RORequest() = default; |
| 106 | RORequest(const RORequest&) = delete; |
| 107 | RORequest& operator=(const RORequest&) = delete; |
| 108 | RORequest(RORequest&&) = default; |
| 109 | RORequest& operator=(RORequest&&) = default; |
| 110 | ~RORequest(){}; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 111 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 112 | /** @brief opens the partition file associated with the offset |
| 113 | * in read only mode and gets the partition details. |
| 114 | * |
| 115 | * 1. Depending on the partition type,tries to open the file |
| 116 | * from the associated partition(RW/PRSV/RO). |
| 117 | * 1a. if file not found in the corresponding |
| 118 | * partition(RW/PRSV/RO) then tries to read the file from |
| 119 | * the read only partition. |
| 120 | * 1b. if the file not found in the read only partition then |
| 121 | * throw exception. |
| 122 | * |
| 123 | * @param[in] context - The mbox context pointer. |
| 124 | * @param[in] offset - The pnor offset(bytes). |
| 125 | */ |
| 126 | const pnor_partition* getPartitionInfo(struct mbox_context* context, |
| 127 | uint32_t offset); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | /** @class RWRequest |
| 131 | * @brief Represent the write request of the partition. |
| 132 | * Stores the partition meta data. |
| 133 | */ |
| 134 | class RWRequest : public Request |
| 135 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 136 | public: |
| 137 | RWRequest() = default; |
| 138 | RWRequest(const RWRequest&) = delete; |
| 139 | RWRequest& operator=(const RWRequest&) = delete; |
| 140 | RWRequest(RWRequest&&) = default; |
| 141 | RWRequest& operator=(RWRequest&&) = default; |
| 142 | ~RWRequest(){}; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 143 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 144 | /** @brief opens the partition file associated with the offset |
| 145 | * in write mode and gets the parttition details. |
| 146 | * |
| 147 | * 1. Depending on the partition type tries to open the file |
| 148 | * from the associated partition. |
| 149 | * 1a. if file not found in the corresponding partition(RW/PRSV) |
| 150 | * then copy the file from the read only partition to the (RW/PRSV) |
| 151 | * partition depending on the partition type. |
| 152 | * 1b. if the file not found in the read only partition then throw |
| 153 | * exception. |
| 154 | * |
| 155 | * @param[in] context - The mbox context pointer. |
| 156 | * @param[in] offset - The pnor offset(bytes). |
| 157 | */ |
| 158 | const pnor_partition* getPartitionInfo(struct mbox_context* context, |
| 159 | uint32_t offset); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 160 | }; |
| 161 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 162 | } // namespace virtual_pnor |
| 163 | } // namespace openpower |