Mike Frysinger | 3a446f2 | 2022-09-08 07:37:14 | [diff] [blame] | 1 | // Copyright 2021 The ChromiumOS Authors |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef MINIOS_UTILS_H_ |
| 6 | #define MINIOS_UTILS_H_ |
| 7 | |
Saketh Pothireddy | b41d8a9 | 2023-06-21 02:45:36 | [diff] [blame] | 8 | #include <cstdint> |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 9 | #include <memory> |
Saketh Pothireddy | b41d8a9 | 2023-06-21 02:45:36 | [diff] [blame] | 10 | #include <optional> |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <tuple> |
Saketh Pothireddy | 7d63100 | 2023-09-18 20:37:35 | [diff] [blame] | 13 | #include <vector> |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 14 | |
| 15 | #include <base/files/file_path.h> |
Jae Hoon Kim | ade1b6c | 2023-11-09 05:22:32 | [diff] [blame] | 16 | #include <base/strings/stringprintf.h> |
Saketh Pothireddy | 8c66ed1 | 2023-10-29 05:14:44 | [diff] [blame] | 17 | #include <brillo/secure_blob.h> |
Saketh Pothireddy | 7d63100 | 2023-09-18 20:37:35 | [diff] [blame] | 18 | #include <brillo/udev/udev.h> |
Saketh Pothireddy | 0a254f6 | 2023-11-09 01:26:33 | [diff] [blame] | 19 | #include <libcrossystem/crossystem.h> |
Saketh Pothireddy | ecd2d40 | 2023-10-29 04:13:07 | [diff] [blame] | 20 | #include <minios/proto_bindings/minios.pb.h> |
Saketh Pothireddy | 1fe90eb | 2024-04-03 17:34:18 | [diff] [blame] | 21 | #include <vpd/vpd.h> |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 22 | |
Jae Hoon Kim | 7c70ae4 | 2024-02-14 07:27:57 | [diff] [blame] | 23 | #include "minios/process_manager.h" |
Vyshu | 7066089 | 2021-06-09 16:52:26 | [diff] [blame] | 24 | |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 25 | namespace minios { |
| 26 | |
Saketh Pothireddy | 2038a78 | 2023-03-03 00:00:57 | [diff] [blame] | 27 | // Alert Log error categories. |
| 28 | extern const char kCategoryInit[]; |
| 29 | extern const char kCategoryReboot[]; |
| 30 | extern const char kCategoryUpdate[]; |
| 31 | |
Saketh Pothireddy | 7230b62 | 2023-06-23 23:55:09 | [diff] [blame] | 32 | extern const char kLogFilePath[]; |
| 33 | |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 34 | extern const base::FilePath kDefaultArchivePath; |
Saketh Pothireddy | 0a254f6 | 2023-11-09 01:26:33 | [diff] [blame] | 35 | extern const int kLogStoreKeySizeBytes; |
Saketh Pothireddy | 599c84e | 2024-03-19 18:26:41 | [diff] [blame] | 36 | extern const brillo::SecureBlob kNullKey; |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 37 | |
Saketh Pothireddy | 1e5b22e | 2023-11-29 17:43:33 | [diff] [blame] | 38 | extern const base::FilePath kStatefulPath; |
| 39 | extern const base::FilePath kUnencryptedMiniosPath; |
| 40 | extern const char kLogArchiveFile[]; |
| 41 | |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 42 | // Reads the content of `file_path` from `start_offset` to `end_offset` with |
| 43 | // maximum characters per line being `max_columns` at max. If the file ends |
| 44 | // before reading all bytes between `start_offset` and `end_offset` it will |
| 45 | // return true. |
| 46 | // - bool: Success or failure. |
| 47 | // - std::string: The content read. |
| 48 | std::tuple<bool, std::string> ReadFileContentWithinRange( |
| 49 | const base::FilePath& file_path, |
| 50 | int64_t start_offset, |
| 51 | int64_t end_offset, |
| 52 | int num_cols); |
| 53 | |
| 54 | // Reads the content of `file_path` from `offset`. |
| 55 | // The `num_lines` and `num_cols` is the maximum amount of lines and characters |
| 56 | // per line that will be read. |
| 57 | // The return will include: |
| 58 | // - bool: Success or failure. |
| 59 | // - std::string: The content read. |
| 60 | // - int64_t: The number of bytes read. |
| 61 | // Note: The number of bytes read can differ than the length of the content |
| 62 | // output in the second tuple element because the content read is formatted to |
| 63 | // number of lines and columns format to fit onto the requested area of |
| 64 | // `num_lines` * `num_cols`. |
| 65 | std::tuple<bool, std::string, int64_t> ReadFileContent( |
| 66 | const base::FilePath& file_path, |
| 67 | int64_t offset, |
| 68 | int num_lines, |
| 69 | int num_cols); |
| 70 | |
Vyshu | 25e45bf | 2021-09-02 20:36:38 | [diff] [blame] | 71 | // Gets VPD region data given a key. Returns false on failure. |
Saketh Pothireddy | 66f7e1a | 2023-10-28 05:05:10 | [diff] [blame] | 72 | bool GetCrosRegionData(std::shared_ptr<ProcessManagerInterface> process_manager, |
Vyshu | 25e45bf | 2021-09-02 20:36:38 | [diff] [blame] | 73 | std::string key, |
| 74 | std::string* value); |
| 75 | |
| 76 | // Gets XKB keyboard data and extracts country code from it. Defaults to "us" on |
| 77 | // failure. |
Saketh Pothireddy | 66f7e1a | 2023-10-28 05:05:10 | [diff] [blame] | 78 | std::string GetKeyboardLayout( |
| 79 | std::shared_ptr<ProcessManagerInterface> process_manager); |
Vyshu | 7066089 | 2021-06-09 16:52:26 | [diff] [blame] | 80 | |
Yuanpeng Ni | 6e6d6cf | 2023-03-22 04:28:37 | [diff] [blame] | 81 | // Read frecon created symbolic link and return the virtual terminal path. |
| 82 | base::FilePath GetLogConsole(); |
| 83 | |
Vyshu | e9a22a7b | 2021-10-08 14:55:53 | [diff] [blame] | 84 | bool TriggerShutdown(); |
| 85 | |
Saketh Pothireddy | 2038a78 | 2023-03-03 00:00:57 | [diff] [blame] | 86 | // Create a tag that can be added to an Error log message to allow easier |
| 87 | // filtering from listnr logs. Expected to be used as the first field of a log |
| 88 | // message. e.g.: `LOG(ERROR) << AlertLogTag(kCategoryName) << err_msg << ....;` |
| 89 | inline std::string AlertLogTag(const std::string& category) { |
| 90 | return base::StringPrintf("[CoreServicesAlert<%s>] ", category.c_str()); |
| 91 | } |
| 92 | |
Saketh Pothireddy | a9f3183 | 2023-11-10 20:54:31 | [diff] [blame] | 93 | // Mount the stateful partition at `/stateful/`. Returns true if successfully |
| 94 | // mounted, false otherwise. |
Saketh Pothireddy | 66f7e1a | 2023-10-28 05:05:10 | [diff] [blame] | 95 | bool MountStatefulPartition( |
| 96 | std::shared_ptr<ProcessManagerInterface> process_manager); |
Saketh Pothireddy | c131f04 | 2023-05-25 18:17:02 | [diff] [blame] | 97 | |
Saketh Pothireddy | a9f3183 | 2023-11-10 20:54:31 | [diff] [blame] | 98 | // Unmount path. Returns true if successfully unmounted, false otherwise. |
| 99 | bool UnmountPath(std::shared_ptr<ProcessManagerInterface> process_manager, |
| 100 | const base::FilePath& path); |
| 101 | |
| 102 | // Unmount `kStatefulPath`. Returns true if successful, false otherwise. |
| 103 | bool UnmountStatefulPartition( |
| 104 | std::shared_ptr<ProcessManagerInterface> process_manager); |
| 105 | |
| 106 | // Compress a pre-determined list of NBR logs and save it to the provided |
| 107 | // path. Returns the result of running a `tar` command. |
Saketh Pothireddy | 66f7e1a | 2023-10-28 05:05:10 | [diff] [blame] | 108 | int CompressLogs(std::shared_ptr<ProcessManagerInterface> process_manager, |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 109 | const base::FilePath& archive_path = kDefaultArchivePath); |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 110 | |
Saketh Pothireddy | b41d8a9 | 2023-06-21 02:45:36 | [diff] [blame] | 111 | // Calculate kernel size. |
| 112 | std::optional<uint64_t> KernelSize( |
Saketh Pothireddy | 66f7e1a | 2023-10-28 05:05:10 | [diff] [blame] | 113 | std::shared_ptr<ProcessManagerInterface> process_manager, |
Saketh Pothireddy | b41d8a9 | 2023-06-21 02:45:36 | [diff] [blame] | 114 | const base::FilePath& device); |
| 115 | |
Saketh Pothireddy | 18a9234 | 2023-08-15 21:10:48 | [diff] [blame] | 116 | // Read the kernel cmdline and get the current version. |
| 117 | std::optional<std::string> GetMiniOSVersion(); |
| 118 | |
Saketh Pothireddy | 7d63100 | 2023-09-18 20:37:35 | [diff] [blame] | 119 | // Enumerate udev devices and query for removable storage devices. Returns true |
| 120 | // on success and devices will be added to the passed in vector. Vector will be |
| 121 | // cleared before any devices are possibly added to it. |
| 122 | bool GetRemovableDevices( |
| 123 | std::vector<base::FilePath>& devices, |
| 124 | std::unique_ptr<brillo::Udev> udev = brillo::Udev::Create()); |
| 125 | |
Saketh Pothireddy | eb3aa62 | 2023-09-28 20:14:03 | [diff] [blame] | 126 | // Check if the given log store key is valid. |
Saketh Pothireddy | 8c66ed1 | 2023-10-29 05:14:44 | [diff] [blame] | 127 | bool IsLogStoreKeyValid(const brillo::SecureBlob& key); |
Saketh Pothireddy | eb3aa62 | 2023-09-28 20:14:03 | [diff] [blame] | 128 | |
Saketh Pothireddy | eb3aa62 | 2023-09-28 20:14:03 | [diff] [blame] | 129 | // Get log encryption key from VPD. Returns `nullopt` if not found. |
Saketh Pothireddy | 1fe90eb | 2024-04-03 17:34:18 | [diff] [blame] | 130 | std::optional<brillo::SecureBlob> GetLogStoreKey(std::shared_ptr<vpd::Vpd> vpd); |
Saketh Pothireddy | eb3aa62 | 2023-09-28 20:14:03 | [diff] [blame] | 131 | |
| 132 | // Save a given log encryption key to VPD. Returns true on success, false |
| 133 | // otherwise. |
Saketh Pothireddy | 1fe90eb | 2024-04-03 17:34:18 | [diff] [blame] | 134 | bool SaveLogStoreKey(std::shared_ptr<vpd::Vpd> vpd, |
Saketh Pothireddy | 8c66ed1 | 2023-10-29 05:14:44 | [diff] [blame] | 135 | const brillo::SecureBlob& key); |
Saketh Pothireddy | eb3aa62 | 2023-09-28 20:14:03 | [diff] [blame] | 136 | |
Saketh Pothireddy | 0a254f6 | 2023-11-09 01:26:33 | [diff] [blame] | 137 | // Overwrite log store key in VPD with zeros. Returns true on success, false |
| 138 | // otherwise. |
Saketh Pothireddy | 1fe90eb | 2024-04-03 17:34:18 | [diff] [blame] | 139 | bool ClearLogStoreKey(std::shared_ptr<vpd::Vpd> vpd); |
Saketh Pothireddy | 0a254f6 | 2023-11-09 01:26:33 | [diff] [blame] | 140 | |
Saketh Pothireddy | ecd2d40 | 2023-10-29 04:13:07 | [diff] [blame] | 141 | // Read contents of a given file into a secureblob. Returns file contents on |
| 142 | // success and nullopt otherwise. |
| 143 | std::optional<brillo::SecureBlob> ReadFileToSecureBlob( |
| 144 | const base::FilePath& log_archive_path); |
| 145 | |
| 146 | // Read contents of a secureblob into a given file. Returns true on success, |
| 147 | // false otherwise. |
| 148 | bool WriteSecureBlobToFile(const base::FilePath& log_archive_path, |
| 149 | const brillo::SecureBlob& data); |
| 150 | |
| 151 | // Encrypt data with the given key. Returns encrypted contents, iv and |
| 152 | // tag on success, nullopt otherwise. |
Saketh Pothireddy | 1e5b22e | 2023-11-29 17:43:33 | [diff] [blame] | 153 | std::optional<EncryptedLogFile> EncryptLogArchive( |
Saketh Pothireddy | ecd2d40 | 2023-10-29 04:13:07 | [diff] [blame] | 154 | const brillo::SecureBlob& plain_data, const brillo::SecureBlob& key); |
| 155 | |
| 156 | // Decrypt encrypted contents (along with iv and tag) with given key. Returns |
| 157 | // plain text data on success, nullopt otherwise. |
Saketh Pothireddy | 1e5b22e | 2023-11-29 17:43:33 | [diff] [blame] | 158 | std::optional<brillo::SecureBlob> DecryptLogArchive( |
| 159 | const EncryptedLogFile& encrypted_archive, const brillo::SecureBlob& key); |
Saketh Pothireddy | ecd2d40 | 2023-10-29 04:13:07 | [diff] [blame] | 160 | |
Saketh Pothireddy | 0a254f6 | 2023-11-09 01:26:33 | [diff] [blame] | 161 | std::optional<uint64_t> GetMiniOsPriorityPartition( |
| 162 | std::shared_ptr<crossystem::Crossystem> cros_system); |
| 163 | |
Saketh Pothireddy | 1e5b22e | 2023-11-29 17:43:33 | [diff] [blame] | 164 | // Check whether currently running in MiniOS. Returns nullopt if environment |
| 165 | // cannot be determined. Otherwise true if running in MiniOS, false if not. |
| 166 | std::optional<bool> IsRunningFromMiniOs(); |
| 167 | |
| 168 | // Uncompress the specified log archive to `dest_path`. Specify any additional |
| 169 | // `tar` options in args. Returns true on success, false otherwise. |
| 170 | bool ExtractArchive(std::shared_ptr<ProcessManagerInterface> process_manager, |
| 171 | const base::FilePath& archive_path, |
| 172 | const base::FilePath& dest_path, |
| 173 | const std::vector<std::string>& args); |
| 174 | |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 175 | } // namespace minios |
Saketh Pothireddy | ecd2d40 | 2023-10-29 04:13:07 | [diff] [blame] | 176 | |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 177 | #endif // MINIOS_UTILS_H__ |