blob: 6b6d46bfa4ab683996c74e7883dde50eecd5dac8 [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" {
Evan Lojewskif1e547c2019-03-14 14:34:33 +10306#include "backend.h"
Andrew Jefferyf4bc3352019-03-18 12:09:48 +10307#include "vpnor/backend.h"
Andrew Jefferyad343102018-02-28 00:35:50 +10308};
9
Andrew Jefferyde08ca22019-03-18 13:23:46 +103010#include "vpnor/table.hpp"
William A. Kennington IIId5f1d402018-10-11 13:55:04 -070011
Ratan Guptac0ef9872017-06-06 14:31:37 +053012#include <fcntl.h>
Ratan Guptac0ef9872017-06-06 14:31:37 +053013#include <unistd.h>
William A. Kennington IIId5f1d402018-10-11 13:55:04 -070014
Ratan Guptac0ef9872017-06-06 14:31:37 +053015#include <experimental/filesystem>
William A. Kennington IIId5f1d402018-10-11 13:55:04 -070016#include <string>
17
Ratan Guptac0ef9872017-06-06 14:31:37 +053018namespace openpower
19{
Ratan Guptac0ef9872017-06-06 14:31:37 +053020namespace virtual_pnor
21{
22
23namespace fs = std::experimental::filesystem;
24
Ratan Guptac0ef9872017-06-06 14:31:37 +053025class Request
26{
Andrew Jefferyf34db312018-03-09 15:27:03 +103027 public:
Andrew Jefferyad343102018-02-28 00:35:50 +103028 /** @brief Construct a flash access request
29 *
Evan Lojewskif1e547c2019-03-14 14:34:33 +103030 * @param[in] backend - The backend context used to process the request
Andrew Jefferyad343102018-02-28 00:35:50 +103031 * @param[in] offset - The absolute offset into the flash device as
32 * provided by the mbox message associated with the
33 * request
34 *
35 * The class does not take ownership of the ctx pointer. The lifetime of
36 * the ctx pointer must strictly exceed the lifetime of the class
37 * instance.
38 */
Evan Lojewskif1e547c2019-03-14 14:34:33 +103039 Request(struct backend* backend, size_t offset) :
40 backend(backend), partition(((struct vpnor_data*)backend->priv)
41 ->vpnor->table->partition(offset)),
42 base(partition.data.base << backend->block_size_shift),
Andrew Jefferyad343102018-02-28 00:35:50 +103043 offset(offset - base)
44 {
45 }
Andrew Jefferyf34db312018-03-09 15:27:03 +103046 Request(const Request&) = delete;
47 Request& operator=(const Request&) = delete;
48 Request(Request&&) = default;
49 Request& operator=(Request&&) = default;
50 ~Request() = default;
Ratan Guptac0ef9872017-06-06 14:31:37 +053051
Andrew Jefferye2744c02020-01-28 15:08:13 +103052 ssize_t read(void* dst, size_t len);
53 ssize_t write(void* dst, size_t len);
Andrew Jefferyad343102018-02-28 00:35:50 +103054
55 private:
Andrew Jeffery1a3f8432018-03-02 10:18:02 +103056 /** @brief Clamp the access length to the maximum supported by the ToC */
57 size_t clamp(size_t len);
58
Andrew Jefferyad343102018-02-28 00:35:50 +103059 /** @brief Returns the partition file path associated with the offset.
Andrew Jefferyf34db312018-03-09 15:27:03 +103060 *
Andrew Jefferyad343102018-02-28 00:35:50 +103061 * The search strategy for the partition file depends on the value of the
62 * flags parameter.
Andrew Jefferyf34db312018-03-09 15:27:03 +103063 *
Andrew Jefferyad343102018-02-28 00:35:50 +103064 * For the O_RDONLY case:
Andrew Jefferyf34db312018-03-09 15:27:03 +103065 *
66 * 1. Depending on the partition type,tries to open the file
67 * from the associated partition(RW/PRSV/RO).
68 * 1a. if file not found in the corresponding
69 * partition(RW/PRSV/RO) then tries to read the file from
70 * the read only partition.
71 * 1b. if the file not found in the read only partition then
72 * throw exception.
73 *
Andrew Jefferyad343102018-02-28 00:35:50 +103074 * For the O_RDWR 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.
78 * 1a. if file not found in the corresponding partition(RW/PRSV)
79 * then copy the file from the read only partition to the (RW/PRSV)
80 * partition depending on the partition type.
81 * 1b. if the file not found in the read only partition then throw
Andrew Jefferyad343102018-02-28 00:35:50 +103082 * exception.
Andrew Jefferyf34db312018-03-09 15:27:03 +103083 *
Andrew Jefferyad343102018-02-28 00:35:50 +103084 * @param[in] flags - The flags that will be used to open the file. Must
85 * be one of O_RDONLY or O_RDWR.
86 *
87 * Post-condition: The file described by the returned path exists
88 *
89 * Throws: std::filesystem_error, std::bad_alloc
Andrew Jefferyf34db312018-03-09 15:27:03 +103090 */
Andrew Jefferyad343102018-02-28 00:35:50 +103091 std::experimental::filesystem::path getPartitionFilePath(int flags);
92
Evan Lojewskif1e547c2019-03-14 14:34:33 +103093 struct backend* backend;
Andrew Jefferyad343102018-02-28 00:35:50 +103094 const pnor_partition& partition;
95 size_t base;
96 size_t offset;
Ratan Guptac0ef9872017-06-06 14:31:37 +053097};
98
Andrew Jefferyf34db312018-03-09 15:27:03 +103099} // namespace virtual_pnor
100} // namespace openpower