blob: 80777ce87ecbaa01aa516b4e7469137c22194288 [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
Ratan Guptac0ef9872017-06-06 14:31:37 +053023class Request
24{
Andrew Jefferyf34db312018-03-09 15:27:03 +103025 public:
Andrew Jefferyad343102018-02-28 00:35:50 +103026 /** @brief Construct a flash access request
27 *
28 * @param[in] ctx - The mbox context used to process the request
29 * @param[in] offset - The absolute offset into the flash device as
30 * provided by the mbox message associated with the
31 * request
32 *
33 * The class does not take ownership of the ctx pointer. The lifetime of
34 * the ctx pointer must strictly exceed the lifetime of the class
35 * instance.
36 */
37 Request(struct mbox_context* ctx, size_t offset) :
38 ctx(ctx), partition(ctx->vpnor->table->partition(offset)),
39 base(partition.data.base << ctx->block_size_shift),
40 offset(offset - base)
41 {
42 }
Andrew Jefferyf34db312018-03-09 15:27:03 +103043 Request(const Request&) = delete;
44 Request& operator=(const Request&) = delete;
45 Request(Request&&) = default;
46 Request& operator=(Request&&) = default;
47 ~Request() = default;
Ratan Guptac0ef9872017-06-06 14:31:37 +053048
Andrew Jefferyad343102018-02-28 00:35:50 +103049 ssize_t read(void* dst, size_t len)
50 {
51 return fulfil(dst, len, O_RDONLY);
52 }
Ratan Guptac0ef9872017-06-06 14:31:37 +053053
Andrew Jefferyad343102018-02-28 00:35:50 +103054 ssize_t write(void* dst, size_t len)
55 {
56 return fulfil(dst, len, O_RDWR);
57 }
58
59 private:
60 /** @brief Returns the partition file path associated with the offset.
Andrew Jefferyf34db312018-03-09 15:27:03 +103061 *
Andrew Jefferyad343102018-02-28 00:35:50 +103062 * The search strategy for the partition file depends on the value of the
63 * flags parameter.
Andrew Jefferyf34db312018-03-09 15:27:03 +103064 *
Andrew Jefferyad343102018-02-28 00:35:50 +103065 * For the O_RDONLY case:
Andrew Jefferyf34db312018-03-09 15:27:03 +103066 *
67 * 1. Depending on the partition type,tries to open the file
68 * from the associated partition(RW/PRSV/RO).
69 * 1a. if file not found in the corresponding
70 * partition(RW/PRSV/RO) then tries to read the file from
71 * the read only partition.
72 * 1b. if the file not found in the read only partition then
73 * throw exception.
74 *
Andrew Jefferyad343102018-02-28 00:35:50 +103075 * For the O_RDWR case:
Andrew Jefferyf34db312018-03-09 15:27:03 +103076 *
77 * 1. Depending on the partition type tries to open the file
78 * from the associated partition.
79 * 1a. if file not found in the corresponding partition(RW/PRSV)
80 * then copy the file from the read only partition to the (RW/PRSV)
81 * partition depending on the partition type.
82 * 1b. if the file not found in the read only partition then throw
Andrew Jefferyad343102018-02-28 00:35:50 +103083 * exception.
Andrew Jefferyf34db312018-03-09 15:27:03 +103084 *
Andrew Jefferyad343102018-02-28 00:35:50 +103085 * @param[in] flags - The flags that will be used to open the file. Must
86 * be one of O_RDONLY or O_RDWR.
87 *
88 * Post-condition: The file described by the returned path exists
89 *
90 * Throws: std::filesystem_error, std::bad_alloc
Andrew Jefferyf34db312018-03-09 15:27:03 +103091 */
Andrew Jefferyad343102018-02-28 00:35:50 +103092 std::experimental::filesystem::path getPartitionFilePath(int flags);
93
94 /** @brief Fill dst with the content of the partition relative to offset.
95 *
96 * @param[in] offset - The pnor offset(bytes).
97 * @param[out] dst - The buffer to fill with partition data
98 * @param[in] len - The length of the destination buffer
99 */
100 ssize_t fulfil(void* dst, size_t len, int flags);
101
102 struct mbox_context* ctx;
103 const pnor_partition& partition;
104 size_t base;
105 size_t offset;
Ratan Guptac0ef9872017-06-06 14:31:37 +0530106};
107
Andrew Jefferyf34db312018-03-09 15:27:03 +1030108} // namespace virtual_pnor
109} // namespace openpower