blob: caa930a3f958e14f179b78fdb564de3456af38b3 [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
Andrew Jefferyad343102018-02-28 00:35:50 +10305extern "C" {
6#include "mbox.h"
7};
8
Ratan Guptac0ef9872017-06-06 14:31:37 +05309#include "mboxd_pnor_partition_table.h"
Andrew Jefferyad343102018-02-28 00:35:50 +103010#include "pnor_partition_table.hpp"
Ratan Guptac0ef9872017-06-06 14:31:37 +053011#include <fcntl.h>
12#include <string>
13#include <unistd.h>
14#include <experimental/filesystem>
15
16namespace openpower
17{
Ratan Guptac0ef9872017-06-06 14:31:37 +053018namespace virtual_pnor
19{
20
21namespace fs = std::experimental::filesystem;
22
23enum class ReturnCode : uint8_t
24{
25 FILE_NOT_FOUND = 0,
26 PARTITION_NOT_FOUND = 1,
27 PARTITION_READ_ONLY = 2,
28 FILE_OPEN_FAILURE = 3,
29 SUCCESS = 4,
30};
31
32class Request
33{
Andrew Jefferyf34db312018-03-09 15:27:03 +103034 public:
Andrew Jefferyad343102018-02-28 00:35:50 +103035 /** @brief Construct a flash access request
36 *
37 * @param[in] ctx - The mbox context used to process the request
38 * @param[in] offset - The absolute offset into the flash device as
39 * provided by the mbox message associated with the
40 * request
41 *
42 * The class does not take ownership of the ctx pointer. The lifetime of
43 * the ctx pointer must strictly exceed the lifetime of the class
44 * instance.
45 */
46 Request(struct mbox_context* ctx, size_t offset) :
47 ctx(ctx), partition(ctx->vpnor->table->partition(offset)),
48 base(partition.data.base << ctx->block_size_shift),
49 offset(offset - base)
50 {
51 }
Andrew Jefferyf34db312018-03-09 15:27:03 +103052 Request(const Request&) = delete;
53 Request& operator=(const Request&) = delete;
54 Request(Request&&) = default;
55 Request& operator=(Request&&) = default;
56 ~Request() = default;
Ratan Guptac0ef9872017-06-06 14:31:37 +053057
Andrew Jefferyad343102018-02-28 00:35:50 +103058 ssize_t read(void* dst, size_t len)
59 {
60 return fulfil(dst, len, O_RDONLY);
61 }
Ratan Guptac0ef9872017-06-06 14:31:37 +053062
Andrew Jefferyad343102018-02-28 00:35:50 +103063 ssize_t write(void* dst, size_t len)
64 {
65 return fulfil(dst, len, O_RDWR);
66 }
67
68 private:
69 /** @brief Returns the partition file path associated with the offset.
Andrew Jefferyf34db312018-03-09 15:27:03 +103070 *
Andrew Jefferyad343102018-02-28 00:35:50 +103071 * The search strategy for the partition file depends on the value of the
72 * flags parameter.
Andrew Jefferyf34db312018-03-09 15:27:03 +103073 *
Andrew Jefferyad343102018-02-28 00:35:50 +103074 * For the O_RDONLY case:
Andrew Jefferyf34db312018-03-09 15:27:03 +103075 *
76 * 1. Depending on the partition type,tries to open the file
77 * from the associated partition(RW/PRSV/RO).
78 * 1a. if file not found in the corresponding
79 * partition(RW/PRSV/RO) then tries to read the file from
80 * the read only partition.
81 * 1b. if the file not found in the read only partition then
82 * throw exception.
83 *
Andrew Jefferyad343102018-02-28 00:35:50 +103084 * For the O_RDWR case:
Andrew Jefferyf34db312018-03-09 15:27:03 +103085 *
86 * 1. Depending on the partition type tries to open the file
87 * from the associated partition.
88 * 1a. if file not found in the corresponding partition(RW/PRSV)
89 * then copy the file from the read only partition to the (RW/PRSV)
90 * partition depending on the partition type.
91 * 1b. if the file not found in the read only partition then throw
Andrew Jefferyad343102018-02-28 00:35:50 +103092 * exception.
Andrew Jefferyf34db312018-03-09 15:27:03 +103093 *
Andrew Jefferyad343102018-02-28 00:35:50 +103094 * @param[in] flags - The flags that will be used to open the file. Must
95 * be one of O_RDONLY or O_RDWR.
96 *
97 * Post-condition: The file described by the returned path exists
98 *
99 * Throws: std::filesystem_error, std::bad_alloc
Andrew Jefferyf34db312018-03-09 15:27:03 +1030100 */
Andrew Jefferyad343102018-02-28 00:35:50 +1030101 std::experimental::filesystem::path getPartitionFilePath(int flags);
102
103 /** @brief Fill dst with the content of the partition relative to offset.
104 *
105 * @param[in] offset - The pnor offset(bytes).
106 * @param[out] dst - The buffer to fill with partition data
107 * @param[in] len - The length of the destination buffer
108 */
109 ssize_t fulfil(void* dst, size_t len, int flags);
110
111 struct mbox_context* ctx;
112 const pnor_partition& partition;
113 size_t base;
114 size_t offset;
Ratan Guptac0ef9872017-06-06 14:31:37 +0530115};
116
Andrew Jefferyf34db312018-03-09 15:27:03 +1030117} // namespace virtual_pnor
118} // namespace openpower