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> |
Saketh Pothireddy | 8c66ed1 | 2023-10-29 05:14:44 | [diff] [blame^] | 16 | #include <brillo/secure_blob.h> |
Saketh Pothireddy | 7d63100 | 2023-09-18 20:37:35 | [diff] [blame] | 17 | #include <brillo/udev/udev.h> |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 18 | |
Vyshu | 7066089 | 2021-06-09 16:52:26 | [diff] [blame] | 19 | #include "minios/process_manager.h" |
| 20 | |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 21 | namespace minios { |
| 22 | |
Saketh Pothireddy | 2038a78 | 2023-03-03 00:00:57 | [diff] [blame] | 23 | // Alert Log error categories. |
| 24 | extern const char kCategoryInit[]; |
| 25 | extern const char kCategoryReboot[]; |
| 26 | extern const char kCategoryUpdate[]; |
| 27 | |
Saketh Pothireddy | 7230b62 | 2023-06-23 23:55:09 | [diff] [blame] | 28 | extern const char kLogFilePath[]; |
| 29 | |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 30 | extern const base::FilePath kDefaultArchivePath; |
| 31 | |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 32 | // Reads the content of `file_path` from `start_offset` to `end_offset` with |
| 33 | // maximum characters per line being `max_columns` at max. If the file ends |
| 34 | // before reading all bytes between `start_offset` and `end_offset` it will |
| 35 | // return true. |
| 36 | // - bool: Success or failure. |
| 37 | // - std::string: The content read. |
| 38 | std::tuple<bool, std::string> ReadFileContentWithinRange( |
| 39 | const base::FilePath& file_path, |
| 40 | int64_t start_offset, |
| 41 | int64_t end_offset, |
| 42 | int num_cols); |
| 43 | |
| 44 | // Reads the content of `file_path` from `offset`. |
| 45 | // The `num_lines` and `num_cols` is the maximum amount of lines and characters |
| 46 | // per line that will be read. |
| 47 | // The return will include: |
| 48 | // - bool: Success or failure. |
| 49 | // - std::string: The content read. |
| 50 | // - int64_t: The number of bytes read. |
| 51 | // Note: The number of bytes read can differ than the length of the content |
| 52 | // output in the second tuple element because the content read is formatted to |
| 53 | // number of lines and columns format to fit onto the requested area of |
| 54 | // `num_lines` * `num_cols`. |
| 55 | std::tuple<bool, std::string, int64_t> ReadFileContent( |
| 56 | const base::FilePath& file_path, |
| 57 | int64_t offset, |
| 58 | int num_lines, |
| 59 | int num_cols); |
| 60 | |
Vyshu | 25e45bf | 2021-09-02 20:36:38 | [diff] [blame] | 61 | // Gets VPD region data given a key. Returns false on failure. |
Saketh Pothireddy | 66f7e1a | 2023-10-28 05:05:10 | [diff] [blame] | 62 | bool GetCrosRegionData(std::shared_ptr<ProcessManagerInterface> process_manager, |
Vyshu | 25e45bf | 2021-09-02 20:36:38 | [diff] [blame] | 63 | std::string key, |
| 64 | std::string* value); |
| 65 | |
| 66 | // Gets XKB keyboard data and extracts country code from it. Defaults to "us" on |
| 67 | // failure. |
Saketh Pothireddy | 66f7e1a | 2023-10-28 05:05:10 | [diff] [blame] | 68 | std::string GetKeyboardLayout( |
| 69 | std::shared_ptr<ProcessManagerInterface> process_manager); |
Vyshu | 7066089 | 2021-06-09 16:52:26 | [diff] [blame] | 70 | |
Yuanpeng Ni | 6e6d6cf | 2023-03-22 04:28:37 | [diff] [blame] | 71 | // Read frecon created symbolic link and return the virtual terminal path. |
| 72 | base::FilePath GetLogConsole(); |
| 73 | |
Vyshu | e9a22a7b | 2021-10-08 14:55:53 | [diff] [blame] | 74 | bool TriggerShutdown(); |
| 75 | |
Saketh Pothireddy | 2038a78 | 2023-03-03 00:00:57 | [diff] [blame] | 76 | // Create a tag that can be added to an Error log message to allow easier |
| 77 | // filtering from listnr logs. Expected to be used as the first field of a log |
| 78 | // message. e.g.: `LOG(ERROR) << AlertLogTag(kCategoryName) << err_msg << ....;` |
| 79 | inline std::string AlertLogTag(const std::string& category) { |
| 80 | return base::StringPrintf("[CoreServicesAlert<%s>] ", category.c_str()); |
| 81 | } |
| 82 | |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 83 | // Mount the stateful partition at `/stateful/` if its not currently mounted. |
| 84 | // Returns true if successfully mounted, false otherwise. |
Saketh Pothireddy | 66f7e1a | 2023-10-28 05:05:10 | [diff] [blame] | 85 | bool MountStatefulPartition( |
| 86 | std::shared_ptr<ProcessManagerInterface> process_manager); |
Saketh Pothireddy | c131f04 | 2023-05-25 18:17:02 | [diff] [blame] | 87 | |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 88 | // Compress a pre-determined list of NBR logs and save it to the provided path. |
| 89 | // Returns the result of running a `tar` command. |
Saketh Pothireddy | 66f7e1a | 2023-10-28 05:05:10 | [diff] [blame] | 90 | int CompressLogs(std::shared_ptr<ProcessManagerInterface> process_manager, |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 91 | const base::FilePath& archive_path = kDefaultArchivePath); |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 92 | |
Saketh Pothireddy | b41d8a9 | 2023-06-21 02:45:36 | [diff] [blame] | 93 | // Calculate kernel size. |
| 94 | std::optional<uint64_t> KernelSize( |
Saketh Pothireddy | 66f7e1a | 2023-10-28 05:05:10 | [diff] [blame] | 95 | std::shared_ptr<ProcessManagerInterface> process_manager, |
Saketh Pothireddy | b41d8a9 | 2023-06-21 02:45:36 | [diff] [blame] | 96 | const base::FilePath& device); |
| 97 | |
Saketh Pothireddy | 18a9234 | 2023-08-15 21:10:48 | [diff] [blame] | 98 | // Read the kernel cmdline and get the current version. |
| 99 | std::optional<std::string> GetMiniOSVersion(); |
| 100 | |
Saketh Pothireddy | 7d63100 | 2023-09-18 20:37:35 | [diff] [blame] | 101 | // Enumerate udev devices and query for removable storage devices. Returns true |
| 102 | // on success and devices will be added to the passed in vector. Vector will be |
| 103 | // cleared before any devices are possibly added to it. |
| 104 | bool GetRemovableDevices( |
| 105 | std::vector<base::FilePath>& devices, |
| 106 | std::unique_ptr<brillo::Udev> udev = brillo::Udev::Create()); |
| 107 | |
Saketh Pothireddy | eb3aa62 | 2023-09-28 20:14:03 | [diff] [blame] | 108 | // Check if the given log store key is valid. |
Saketh Pothireddy | 8c66ed1 | 2023-10-29 05:14:44 | [diff] [blame^] | 109 | bool IsLogStoreKeyValid(const brillo::SecureBlob& key); |
Saketh Pothireddy | eb3aa62 | 2023-09-28 20:14:03 | [diff] [blame] | 110 | |
| 111 | // Trim the provided key for any trailing whitespace beyond |
Saketh Pothireddy | 8c66ed1 | 2023-10-29 05:14:44 | [diff] [blame^] | 112 | // `kLogStoreHexKeySizeBytes`. |
Saketh Pothireddy | eb3aa62 | 2023-09-28 20:14:03 | [diff] [blame] | 113 | void TrimLogStoreKey(std::string& key); |
| 114 | |
| 115 | // Get log encryption key from VPD. Returns `nullopt` if not found. |
Saketh Pothireddy | 8c66ed1 | 2023-10-29 05:14:44 | [diff] [blame^] | 116 | std::optional<brillo::SecureBlob> GetLogStoreKey( |
Saketh Pothireddy | 66f7e1a | 2023-10-28 05:05:10 | [diff] [blame] | 117 | std::shared_ptr<ProcessManagerInterface> process_manager); |
Saketh Pothireddy | eb3aa62 | 2023-09-28 20:14:03 | [diff] [blame] | 118 | |
| 119 | // Save a given log encryption key to VPD. Returns true on success, false |
| 120 | // otherwise. |
Saketh Pothireddy | 66f7e1a | 2023-10-28 05:05:10 | [diff] [blame] | 121 | bool SaveLogStoreKey(std::shared_ptr<ProcessManagerInterface> process_manager, |
Saketh Pothireddy | 8c66ed1 | 2023-10-29 05:14:44 | [diff] [blame^] | 122 | const brillo::SecureBlob& key); |
Saketh Pothireddy | eb3aa62 | 2023-09-28 20:14:03 | [diff] [blame] | 123 | |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 124 | } // namespace minios |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 125 | #endif // MINIOS_UTILS_H__ |