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> |
| 13 | |
| 14 | #include <base/files/file_path.h> |
| 15 | |
Vyshu | 7066089 | 2021-06-09 16:52:26 | [diff] [blame] | 16 | #include "minios/process_manager.h" |
| 17 | |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 18 | namespace minios { |
| 19 | |
Saketh Pothireddy | 2038a78 | 2023-03-03 00:00:57 | [diff] [blame] | 20 | // Alert Log error categories. |
| 21 | extern const char kCategoryInit[]; |
| 22 | extern const char kCategoryReboot[]; |
| 23 | extern const char kCategoryUpdate[]; |
| 24 | |
Saketh Pothireddy | 7230b62 | 2023-06-23 23:55:09 | [diff] [blame] | 25 | extern const char kLogFilePath[]; |
| 26 | |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 27 | extern const base::FilePath kDefaultArchivePath; |
| 28 | |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 29 | // Reads the content of `file_path` from `start_offset` to `end_offset` with |
| 30 | // maximum characters per line being `max_columns` at max. If the file ends |
| 31 | // before reading all bytes between `start_offset` and `end_offset` it will |
| 32 | // return true. |
| 33 | // - bool: Success or failure. |
| 34 | // - std::string: The content read. |
| 35 | std::tuple<bool, std::string> ReadFileContentWithinRange( |
| 36 | const base::FilePath& file_path, |
| 37 | int64_t start_offset, |
| 38 | int64_t end_offset, |
| 39 | int num_cols); |
| 40 | |
| 41 | // Reads the content of `file_path` from `offset`. |
| 42 | // The `num_lines` and `num_cols` is the maximum amount of lines and characters |
| 43 | // per line that will be read. |
| 44 | // The return will include: |
| 45 | // - bool: Success or failure. |
| 46 | // - std::string: The content read. |
| 47 | // - int64_t: The number of bytes read. |
| 48 | // Note: The number of bytes read can differ than the length of the content |
| 49 | // output in the second tuple element because the content read is formatted to |
| 50 | // number of lines and columns format to fit onto the requested area of |
| 51 | // `num_lines` * `num_cols`. |
| 52 | std::tuple<bool, std::string, int64_t> ReadFileContent( |
| 53 | const base::FilePath& file_path, |
| 54 | int64_t offset, |
| 55 | int num_lines, |
| 56 | int num_cols); |
| 57 | |
Vyshu | 25e45bf | 2021-09-02 20:36:38 | [diff] [blame] | 58 | // Gets VPD region data given a key. Returns false on failure. |
| 59 | bool GetCrosRegionData(ProcessManagerInterface* process_manager, |
| 60 | std::string key, |
| 61 | std::string* value); |
| 62 | |
| 63 | // Gets XKB keyboard data and extracts country code from it. Defaults to "us" on |
| 64 | // failure. |
| 65 | std::string GetKeyboardLayout(ProcessManagerInterface* process_manager); |
Vyshu | 7066089 | 2021-06-09 16:52:26 | [diff] [blame] | 66 | |
Yuanpeng Ni | 6e6d6cf | 2023-03-22 04:28:37 | [diff] [blame] | 67 | // Read frecon created symbolic link and return the virtual terminal path. |
| 68 | base::FilePath GetLogConsole(); |
| 69 | |
Vyshu | e9a22a7b | 2021-10-08 14:55:53 | [diff] [blame] | 70 | bool TriggerShutdown(); |
| 71 | |
Saketh Pothireddy | 2038a78 | 2023-03-03 00:00:57 | [diff] [blame] | 72 | // Create a tag that can be added to an Error log message to allow easier |
| 73 | // filtering from listnr logs. Expected to be used as the first field of a log |
| 74 | // message. e.g.: `LOG(ERROR) << AlertLogTag(kCategoryName) << err_msg << ....;` |
| 75 | inline std::string AlertLogTag(const std::string& category) { |
| 76 | return base::StringPrintf("[CoreServicesAlert<%s>] ", category.c_str()); |
| 77 | } |
| 78 | |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 79 | // Mount the stateful partition at `/stateful/` if its not currently mounted. |
| 80 | // Returns true if successfully mounted, false otherwise. |
Saketh Pothireddy | c131f04 | 2023-05-25 18:17:02 | [diff] [blame] | 81 | bool MountStatefulPartition(ProcessManagerInterface* process_manager); |
| 82 | |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 83 | // Compress a pre-determined list of NBR logs and save it to the provided path. |
| 84 | // Returns the result of running a `tar` command. |
| 85 | int CompressLogs(std::unique_ptr<ProcessManagerInterface> process_manager, |
| 86 | const base::FilePath& archive_path = kDefaultArchivePath); |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 87 | |
Saketh Pothireddy | b41d8a9 | 2023-06-21 02:45:36 | [diff] [blame^] | 88 | // Calculate kernel size. |
| 89 | std::optional<uint64_t> KernelSize( |
| 90 | std::unique_ptr<ProcessManagerInterface> process_manager, |
| 91 | const base::FilePath& device); |
| 92 | |
Saketh Pothireddy | 5317629 | 2023-05-23 16:52:36 | [diff] [blame] | 93 | } // namespace minios |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 94 | #endif // MINIOS_UTILS_H__ |