blob: 924ddc9c68c394a58dfe45b3cf8b05424d9bbe5a [file] [log] [blame]
Mike Frysinger3a446f22022-09-08 07:37:141// Copyright 2021 The ChromiumOS Authors
Jae Hoon Kim0fbd6472021-04-29 19:08:332// 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 Pothireddyb41d8a92023-06-21 02:45:368#include <cstdint>
Saketh Pothireddy53176292023-05-23 16:52:369#include <memory>
Saketh Pothireddyb41d8a92023-06-21 02:45:3610#include <optional>
Jae Hoon Kim0fbd6472021-04-29 19:08:3311#include <string>
12#include <tuple>
13
14#include <base/files/file_path.h>
15
Vyshu70660892021-06-09 16:52:2616#include "minios/process_manager.h"
17
Jae Hoon Kim0fbd6472021-04-29 19:08:3318namespace minios {
19
Saketh Pothireddy2038a782023-03-03 00:00:5720// Alert Log error categories.
21extern const char kCategoryInit[];
22extern const char kCategoryReboot[];
23extern const char kCategoryUpdate[];
24
Saketh Pothireddy7230b622023-06-23 23:55:0925extern const char kLogFilePath[];
26
Saketh Pothireddy53176292023-05-23 16:52:3627extern const base::FilePath kDefaultArchivePath;
28
Jae Hoon Kim0fbd6472021-04-29 19:08:3329// 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.
35std::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`.
52std::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
Vyshu25e45bf2021-09-02 20:36:3858// Gets VPD region data given a key. Returns false on failure.
59bool 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.
65std::string GetKeyboardLayout(ProcessManagerInterface* process_manager);
Vyshu70660892021-06-09 16:52:2666
Yuanpeng Ni6e6d6cf2023-03-22 04:28:3767// Read frecon created symbolic link and return the virtual terminal path.
68base::FilePath GetLogConsole();
69
Vyshue9a22a7b2021-10-08 14:55:5370bool TriggerShutdown();
71
Saketh Pothireddy2038a782023-03-03 00:00:5772// 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 << ....;`
75inline std::string AlertLogTag(const std::string& category) {
76 return base::StringPrintf("[CoreServicesAlert<%s>] ", category.c_str());
77}
78
Saketh Pothireddy53176292023-05-23 16:52:3679// Mount the stateful partition at `/stateful/` if its not currently mounted.
80// Returns true if successfully mounted, false otherwise.
Saketh Pothireddyc131f042023-05-25 18:17:0281bool MountStatefulPartition(ProcessManagerInterface* process_manager);
82
Saketh Pothireddy53176292023-05-23 16:52:3683// Compress a pre-determined list of NBR logs and save it to the provided path.
84// Returns the result of running a `tar` command.
85int CompressLogs(std::unique_ptr<ProcessManagerInterface> process_manager,
86 const base::FilePath& archive_path = kDefaultArchivePath);
Jae Hoon Kim0fbd6472021-04-29 19:08:3387
Saketh Pothireddyb41d8a92023-06-21 02:45:3688// Calculate kernel size.
89std::optional<uint64_t> KernelSize(
90 std::unique_ptr<ProcessManagerInterface> process_manager,
91 const base::FilePath& device);
92
Saketh Pothireddy53176292023-05-23 16:52:3693} // namespace minios
Jae Hoon Kim0fbd6472021-04-29 19:08:3394#endif // MINIOS_UTILS_H__