Andrew Jeffery | acee683 | 2018-02-21 22:17:08 +1030 | [diff] [blame^] | 1 | /* |
| 2 | * Mailbox Daemon Implementation |
| 3 | * |
| 4 | * Copyright 2018 IBM |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | */ |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 19 | #pragma once |
| 20 | |
| 21 | #include "mboxd_pnor_partition_table.h" |
| 22 | #include <fcntl.h> |
| 23 | #include <string> |
| 24 | #include <unistd.h> |
| 25 | #include <experimental/filesystem> |
| 26 | |
| 27 | namespace openpower |
| 28 | { |
| 29 | namespace file |
| 30 | { |
| 31 | |
| 32 | class Descriptor |
| 33 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 34 | private: |
| 35 | /** default value */ |
| 36 | int fd = -1; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 37 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 38 | public: |
| 39 | Descriptor() = default; |
| 40 | Descriptor(const Descriptor&) = delete; |
| 41 | Descriptor& operator=(const Descriptor&) = delete; |
| 42 | Descriptor(Descriptor&&) = delete; |
| 43 | Descriptor& operator=(Descriptor&&) = delete; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 44 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 45 | Descriptor(int fd) : fd(fd) |
| 46 | { |
| 47 | } |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 48 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 49 | ~Descriptor() |
| 50 | { |
| 51 | if (fd >= 0) |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 52 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 53 | close(fd); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 54 | } |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 55 | } |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 56 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 57 | int operator()() const |
| 58 | { |
| 59 | return fd; |
| 60 | } |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 61 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 62 | void set(int descriptor) |
| 63 | { |
| 64 | fd = descriptor; |
| 65 | } |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 66 | }; |
| 67 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 68 | } // namespace file |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 69 | |
| 70 | namespace virtual_pnor |
| 71 | { |
| 72 | |
| 73 | namespace fs = std::experimental::filesystem; |
| 74 | |
| 75 | enum class ReturnCode : uint8_t |
| 76 | { |
| 77 | FILE_NOT_FOUND = 0, |
| 78 | PARTITION_NOT_FOUND = 1, |
| 79 | PARTITION_READ_ONLY = 2, |
| 80 | FILE_OPEN_FAILURE = 3, |
| 81 | SUCCESS = 4, |
| 82 | }; |
| 83 | |
| 84 | class Request |
| 85 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 86 | public: |
| 87 | Request() = default; |
| 88 | Request(const Request&) = delete; |
| 89 | Request& operator=(const Request&) = delete; |
| 90 | Request(Request&&) = default; |
| 91 | Request& operator=(Request&&) = default; |
| 92 | ~Request() = default; |
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 | openpower::file::Descriptor fd; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 95 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 96 | protected: |
| 97 | /** @brief opens the partition file |
| 98 | * |
| 99 | * @param[in] filePath - Absolute file path. |
| 100 | * @param[in] mode - File open mode. |
| 101 | */ |
| 102 | ReturnCode open(const std::string& filePath, int mode); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 103 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 104 | /** @brief returns the partition file path associated with the offset. |
| 105 | * |
| 106 | * @param[in] context - The mbox context pointer. |
| 107 | * @param[in] offset - The pnor offset(bytes). |
| 108 | */ |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 109 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 110 | std::string getPartitionFilePath(struct mbox_context* context, |
| 111 | uint32_t offset); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 112 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 113 | const pnor_partition* partition = nullptr; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /** @class RORequest |
| 117 | * @brief Represent the read request of the partition. |
| 118 | * Stores the partition meta data. |
| 119 | */ |
| 120 | class RORequest : public Request |
| 121 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 122 | public: |
| 123 | RORequest() = default; |
| 124 | RORequest(const RORequest&) = delete; |
| 125 | RORequest& operator=(const RORequest&) = delete; |
| 126 | RORequest(RORequest&&) = default; |
| 127 | RORequest& operator=(RORequest&&) = default; |
| 128 | ~RORequest(){}; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 129 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 130 | /** @brief opens the partition file associated with the offset |
| 131 | * in read only mode and gets the partition details. |
| 132 | * |
| 133 | * 1. Depending on the partition type,tries to open the file |
| 134 | * from the associated partition(RW/PRSV/RO). |
| 135 | * 1a. if file not found in the corresponding |
| 136 | * partition(RW/PRSV/RO) then tries to read the file from |
| 137 | * the read only partition. |
| 138 | * 1b. if the file not found in the read only partition then |
| 139 | * throw exception. |
| 140 | * |
| 141 | * @param[in] context - The mbox context pointer. |
| 142 | * @param[in] offset - The pnor offset(bytes). |
| 143 | */ |
| 144 | const pnor_partition* getPartitionInfo(struct mbox_context* context, |
| 145 | uint32_t offset); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | /** @class RWRequest |
| 149 | * @brief Represent the write request of the partition. |
| 150 | * Stores the partition meta data. |
| 151 | */ |
| 152 | class RWRequest : public Request |
| 153 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 154 | public: |
| 155 | RWRequest() = default; |
| 156 | RWRequest(const RWRequest&) = delete; |
| 157 | RWRequest& operator=(const RWRequest&) = delete; |
| 158 | RWRequest(RWRequest&&) = default; |
| 159 | RWRequest& operator=(RWRequest&&) = default; |
| 160 | ~RWRequest(){}; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 161 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 162 | /** @brief opens the partition file associated with the offset |
| 163 | * in write mode and gets the parttition details. |
| 164 | * |
| 165 | * 1. Depending on the partition type tries to open the file |
| 166 | * from the associated partition. |
| 167 | * 1a. if file not found in the corresponding partition(RW/PRSV) |
| 168 | * then copy the file from the read only partition to the (RW/PRSV) |
| 169 | * partition depending on the partition type. |
| 170 | * 1b. if the file not found in the read only partition then throw |
| 171 | * exception. |
| 172 | * |
| 173 | * @param[in] context - The mbox context pointer. |
| 174 | * @param[in] offset - The pnor offset(bytes). |
| 175 | */ |
| 176 | const pnor_partition* getPartitionInfo(struct mbox_context* context, |
| 177 | uint32_t offset); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 178 | }; |
| 179 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 180 | } // namespace virtual_pnor |
| 181 | } // namespace openpower |