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" { |
| 6 | #include "mbox.h" |
| 7 | }; |
| 8 | |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 9 | #include "mboxd_pnor_partition_table.h" |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 10 | #include "pnor_partition_table.hpp" |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 11 | #include <fcntl.h> |
| 12 | #include <string> |
| 13 | #include <unistd.h> |
| 14 | #include <experimental/filesystem> |
| 15 | |
| 16 | namespace openpower |
| 17 | { |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 18 | namespace virtual_pnor |
| 19 | { |
| 20 | |
| 21 | namespace fs = std::experimental::filesystem; |
| 22 | |
| 23 | enum 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 | |
| 32 | class Request |
| 33 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 34 | public: |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 35 | /** @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 Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 52 | Request(const Request&) = delete; |
| 53 | Request& operator=(const Request&) = delete; |
| 54 | Request(Request&&) = default; |
| 55 | Request& operator=(Request&&) = default; |
| 56 | ~Request() = default; |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 57 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 58 | ssize_t read(void* dst, size_t len) |
| 59 | { |
| 60 | return fulfil(dst, len, O_RDONLY); |
| 61 | } |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 62 | |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 63 | 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 Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 70 | * |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 71 | * The search strategy for the partition file depends on the value of the |
| 72 | * flags parameter. |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 73 | * |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 74 | * For the O_RDONLY 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(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 Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 84 | * For the O_RDWR case: |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 85 | * |
| 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 Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 92 | * exception. |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 93 | * |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 94 | * @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 Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 100 | */ |
Andrew Jeffery | ad34310 | 2018-02-28 00:35:50 +1030 | [diff] [blame] | 101 | 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 Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 115 | }; |
| 116 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 117 | } // namespace virtual_pnor |
| 118 | } // namespace openpower |