blob: 76ac9591d1cf05624241e17346410fa7a192e32e [file] [log] [blame]
Tom Joseph0c6d22c2019-06-26 09:58:41 +05301#pragma once
2
George Liuc453e162022-12-21 17:16:23 +08003#include <libpldm/pldm_types.h>
Tom Joseph0c6d22c2019-06-26 09:58:41 +05304
Tom Joseph0c6d22c2019-06-26 09:58:41 +05305#include <nlohmann/json.hpp>
Tom Joseph0c6d22c2019-06-26 09:58:41 +05306
Pavithra Barithayab3b84b42024-08-23 11:43:57 +05307#include <cstdint>
George Liu6492f522020-06-16 10:34:05 +08008#include <filesystem>
9#include <vector>
Tom Joseph0c6d22c2019-06-26 09:58:41 +053010
11namespace pldm
12{
13
14namespace filetable
15{
16
17namespace fs = std::filesystem;
18using Handle = uint32_t;
19using Json = nlohmann::json;
20using Table = std::vector<uint8_t>;
21
22/** @struct FileEntry
23 *
24 * Data structure for storing information regarding the files supported by
25 * PLDM File I/O. The file handle is used to uniquely identify the file. The
26 * traits provide information whether the file is Read only, Read/Write and
27 * preserved across firmware upgrades.
28 */
29struct FileEntry
30{
31 Handle handle; //!< File handle
32 fs::path fsPath; //!< File path
33 bitfield32_t traits; //!< File traits
34};
35
36/** @class FileTable
37 *
38 * FileTable class encapsulates the data related to files supported by PLDM
39 * File I/O and provides interfaces to lookup files information based on the
40 * file handle and extract the file attribute table. The file attribute table
41 * comprises of metadata for files. Metadata includes the file handle, file
42 * name, current file size and file traits.
43 */
44class FileTable
45{
46 public:
47 /** @brief The file table is initialised by parsing the config file
48 * containing information about the files.
49 *
50 * @param[in] fileTableConfigPath - path to the json file containing
51 * information
52 */
53 FileTable(const std::string& fileTableConfigPath);
54 FileTable() = default;
55 ~FileTable() = default;
56 FileTable(const FileTable&) = default;
57 FileTable& operator=(const FileTable&) = default;
58 FileTable(FileTable&&) = default;
59 FileTable& operator=(FileTable&&) = default;
60
61 /** @brief Get the file attribute table
62 *
63 * @return Table- contents of the file attribute table
64 */
65 Table operator()() const;
66
67 /** @brief Get the FileEntry at the file handle
68 *
69 * @param[in] handle - file handle
70 *
71 * @return FileEntry - file entry at the handle
72 */
73 FileEntry at(Handle handle) const
74 {
75 return tableEntries.at(handle);
76 }
77
78 /** @brief Check is file attribute table is empty
79 *
80 * @return bool - true if file attribute table is empty, false otherwise.
81 */
82 bool isEmpty() const
83 {
84 return fileTable.empty();
85 }
86
87 /** @brief Clear the file table contents
88 *
89 */
90 void clear()
91 {
92 tableEntries.clear();
93 fileTable.clear();
94 padCount = 0;
95 checkSum = 0;
96 }
97
98 private:
99 /** @brief handle to FileEntry mappings for lookups based on file handle */
100 std::unordered_map<Handle, FileEntry> tableEntries;
101
102 /** @brief file attribute table including the pad bytes, except the checksum
103 */
104 std::vector<uint8_t> fileTable;
105
106 /** @brief the pad count of the file attribute table, the number of pad
107 * bytes is between 0 and 3 */
108 uint8_t padCount = 0;
109
110 /** @brief the checksum of the file attribute table */
111 uint32_t checkSum = 0;
112};
113
114/** @brief Build the file attribute table if not already built using the
115 * file table config.
116 *
117 * @param[in] fileTablePath - path of the file table config
118 *
119 * @return FileTable& - Reference to instance of file table
120 */
121
122FileTable& buildFileTable(const std::string& fileTablePath);
123
124} // namespace filetable
125} // namespace pldm