blob: a11a921c75d696f58848842d316939b9eb6800a6 [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301/* SPDX-License-Identifier: Apache-2.0 */
2/* Copyright (C) 2018 IBM Corp. */
Ratan Guptac0ef9872017-06-06 14:31:37 +05303#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
11namespace openpower
12{
13namespace file
14{
15
16class Descriptor
17{
Andrew Jefferyf34db312018-03-09 15:27:03 +103018 private:
19 /** default value */
20 int fd = -1;
Ratan Guptac0ef9872017-06-06 14:31:37 +053021
Andrew Jefferyf34db312018-03-09 15:27:03 +103022 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 Guptac0ef9872017-06-06 14:31:37 +053028
Andrew Jefferyf34db312018-03-09 15:27:03 +103029 Descriptor(int fd) : fd(fd)
30 {
31 }
Ratan Guptac0ef9872017-06-06 14:31:37 +053032
Andrew Jefferyf34db312018-03-09 15:27:03 +103033 ~Descriptor()
34 {
35 if (fd >= 0)
Ratan Guptac0ef9872017-06-06 14:31:37 +053036 {
Andrew Jefferyf34db312018-03-09 15:27:03 +103037 close(fd);
Ratan Guptac0ef9872017-06-06 14:31:37 +053038 }
Andrew Jefferyf34db312018-03-09 15:27:03 +103039 }
Ratan Guptac0ef9872017-06-06 14:31:37 +053040
Andrew Jefferyf34db312018-03-09 15:27:03 +103041 int operator()() const
42 {
43 return fd;
44 }
Ratan Guptac0ef9872017-06-06 14:31:37 +053045
Andrew Jefferyf34db312018-03-09 15:27:03 +103046 void set(int descriptor)
47 {
48 fd = descriptor;
49 }
Ratan Guptac0ef9872017-06-06 14:31:37 +053050};
51
Andrew Jefferyf34db312018-03-09 15:27:03 +103052} // namespace file
Ratan Guptac0ef9872017-06-06 14:31:37 +053053
54namespace virtual_pnor
55{
56
57namespace fs = std::experimental::filesystem;
58
59enum 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
68class Request
69{
Andrew Jefferyf34db312018-03-09 15:27:03 +103070 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 Guptac0ef9872017-06-06 14:31:37 +053077
Andrew Jefferyf34db312018-03-09 15:27:03 +103078 openpower::file::Descriptor fd;
Ratan Guptac0ef9872017-06-06 14:31:37 +053079
Andrew Jefferyf34db312018-03-09 15:27:03 +103080 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 Guptac0ef9872017-06-06 14:31:37 +053087
Andrew Jefferyf34db312018-03-09 15:27:03 +103088 /** @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 Guptac0ef9872017-06-06 14:31:37 +053093
Andrew Jefferyf34db312018-03-09 15:27:03 +103094 std::string getPartitionFilePath(struct mbox_context* context,
95 uint32_t offset);
Ratan Guptac0ef9872017-06-06 14:31:37 +053096
Andrew Jefferyf34db312018-03-09 15:27:03 +103097 const pnor_partition* partition = nullptr;
Ratan Guptac0ef9872017-06-06 14:31:37 +053098};
99
100/** @class RORequest
101 * @brief Represent the read request of the partition.
102 * Stores the partition meta data.
103 */
104class RORequest : public Request
105{
Andrew Jefferyf34db312018-03-09 15:27:03 +1030106 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 Guptac0ef9872017-06-06 14:31:37 +0530113
Andrew Jefferyf34db312018-03-09 15:27:03 +1030114 /** @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 Guptac0ef9872017-06-06 14:31:37 +0530130};
131
132/** @class RWRequest
133 * @brief Represent the write request of the partition.
134 * Stores the partition meta data.
135 */
136class RWRequest : public Request
137{
Andrew Jefferyf34db312018-03-09 15:27:03 +1030138 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 Guptac0ef9872017-06-06 14:31:37 +0530145
Andrew Jefferyf34db312018-03-09 15:27:03 +1030146 /** @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 Guptac0ef9872017-06-06 14:31:37 +0530162};
163
Andrew Jefferyf34db312018-03-09 15:27:03 +1030164} // namespace virtual_pnor
165} // namespace openpower