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