Andrew Jeffery | 4fe996c | 2018-02-27 12:16:48 +1030 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 */ |
| 2 | /* Copyright (C) 2018 IBM Corp. */ |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 3 | #pragma once |
| 4 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 5 | extern "C" { |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 6 | #include "backend.h" |
Andrew Jeffery | f4bc335 | 2019-03-18 12:09:48 +1030 | [diff] [blame] | 7 | #include "vpnor/backend.h" |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 8 | }; |
| 9 | |
Andrew Jeffery | de08ca2 | 2019-03-18 13:23:46 +1030 | [diff] [blame] | 10 | #include "vpnor/table.hpp" |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 11 | |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 12 | #include <fcntl.h> |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 13 | #include <unistd.h> |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 14 | |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 15 | #include <experimental/filesystem> |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 16 | #include <string> |
| 17 | |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 18 | namespace openpower |
| 19 | { |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 20 | namespace virtual_pnor |
| 21 | { |
| 22 | |
| 23 | namespace fs = std::experimental::filesystem; |
| 24 | |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 25 | class Request |
| 26 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 27 | public: |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 28 | /** @brief Construct a flash access request |
| 29 | * |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 30 | * @param[in] backend - The backend context used to process the request |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 31 | * @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 Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 39 | 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 Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 43 | offset(offset - base) |
| 44 | { |
| 45 | } |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 46 | Request(const Request&) = delete; |
| 47 | Request& operator=(const Request&) = delete; |
| 48 | Request(Request&&) = default; |
| 49 | Request& operator=(Request&&) = default; |
| 50 | ~Request() = default; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 51 | |
Andrew Jeffery | e2744c0 | 2020-01-28 15:08:13 +1030 | [diff] [blame] | 52 | ssize_t read(void* dst, size_t len); |
| 53 | ssize_t write(void* dst, size_t len); |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 54 | |
| 55 | private: |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 56 | /** @brief Clamp the access length to the maximum supported by the ToC */ |
| 57 | size_t clamp(size_t len); |
| 58 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 59 | /** @brief Returns the partition file path associated with the offset. |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 60 | * |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 61 | * The search strategy for the partition file depends on the value of the |
| 62 | * flags parameter. |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 63 | * |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 64 | * For the O_RDONLY case: |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 65 | * |
| 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 Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 74 | * For the O_RDWR case: |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 75 | * |
| 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 Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 82 | * exception. |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 83 | * |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 84 | * @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 Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 90 | */ |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 91 | std::experimental::filesystem::path getPartitionFilePath(int flags); |
| 92 | |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 93 | struct backend* backend; |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 94 | const pnor_partition& partition; |
| 95 | size_t base; |
| 96 | size_t offset; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 97 | }; |
| 98 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 99 | } // namespace virtual_pnor |
| 100 | } // namespace openpower |