blob: 056342c632da1b15fa6a52c7bd5c98ac16638360 [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>
Saketh Pothireddy7d631002023-09-18 20:37:3513#include <vector>
Jae Hoon Kim0fbd6472021-04-29 19:08:3314
15#include <base/files/file_path.h>
Saketh Pothireddy8c66ed12023-10-29 05:14:4416#include <brillo/secure_blob.h>
Saketh Pothireddy7d631002023-09-18 20:37:3517#include <brillo/udev/udev.h>
Jae Hoon Kim0fbd6472021-04-29 19:08:3318
Vyshu70660892021-06-09 16:52:2619#include "minios/process_manager.h"
20
Jae Hoon Kim0fbd6472021-04-29 19:08:3321namespace minios {
22
Saketh Pothireddy2038a782023-03-03 00:00:5723// Alert Log error categories.
24extern const char kCategoryInit[];
25extern const char kCategoryReboot[];
26extern const char kCategoryUpdate[];
27
Saketh Pothireddy7230b622023-06-23 23:55:0928extern const char kLogFilePath[];
29
Saketh Pothireddy53176292023-05-23 16:52:3630extern const base::FilePath kDefaultArchivePath;
31
Jae Hoon Kim0fbd6472021-04-29 19:08:3332// 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.
38std::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`.
55std::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
Vyshu25e45bf2021-09-02 20:36:3861// Gets VPD region data given a key. Returns false on failure.
Saketh Pothireddy66f7e1a2023-10-28 05:05:1062bool GetCrosRegionData(std::shared_ptr<ProcessManagerInterface> process_manager,
Vyshu25e45bf2021-09-02 20:36:3863 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 Pothireddy66f7e1a2023-10-28 05:05:1068std::string GetKeyboardLayout(
69 std::shared_ptr<ProcessManagerInterface> process_manager);
Vyshu70660892021-06-09 16:52:2670
Yuanpeng Ni6e6d6cf2023-03-22 04:28:3771// Read frecon created symbolic link and return the virtual terminal path.
72base::FilePath GetLogConsole();
73
Vyshue9a22a7b2021-10-08 14:55:5374bool TriggerShutdown();
75
Saketh Pothireddy2038a782023-03-03 00:00:5776// 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 << ....;`
79inline std::string AlertLogTag(const std::string& category) {
80 return base::StringPrintf("[CoreServicesAlert<%s>] ", category.c_str());
81}
82
Saketh Pothireddy53176292023-05-23 16:52:3683// Mount the stateful partition at `/stateful/` if its not currently mounted.
84// Returns true if successfully mounted, false otherwise.
Saketh Pothireddy66f7e1a2023-10-28 05:05:1085bool MountStatefulPartition(
86 std::shared_ptr<ProcessManagerInterface> process_manager);
Saketh Pothireddyc131f042023-05-25 18:17:0287
Saketh Pothireddy53176292023-05-23 16:52:3688// 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 Pothireddy66f7e1a2023-10-28 05:05:1090int CompressLogs(std::shared_ptr<ProcessManagerInterface> process_manager,
Saketh Pothireddy53176292023-05-23 16:52:3691 const base::FilePath& archive_path = kDefaultArchivePath);
Jae Hoon Kim0fbd6472021-04-29 19:08:3392
Saketh Pothireddyb41d8a92023-06-21 02:45:3693// Calculate kernel size.
94std::optional<uint64_t> KernelSize(
Saketh Pothireddy66f7e1a2023-10-28 05:05:1095 std::shared_ptr<ProcessManagerInterface> process_manager,
Saketh Pothireddyb41d8a92023-06-21 02:45:3696 const base::FilePath& device);
97
Saketh Pothireddy18a92342023-08-15 21:10:4898// Read the kernel cmdline and get the current version.
99std::optional<std::string> GetMiniOSVersion();
100
Saketh Pothireddy7d631002023-09-18 20:37:35101// 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.
104bool GetRemovableDevices(
105 std::vector<base::FilePath>& devices,
106 std::unique_ptr<brillo::Udev> udev = brillo::Udev::Create());
107
Saketh Pothireddyeb3aa622023-09-28 20:14:03108// Check if the given log store key is valid.
Saketh Pothireddy8c66ed12023-10-29 05:14:44109bool IsLogStoreKeyValid(const brillo::SecureBlob& key);
Saketh Pothireddyeb3aa622023-09-28 20:14:03110
111// Trim the provided key for any trailing whitespace beyond
Saketh Pothireddy8c66ed12023-10-29 05:14:44112// `kLogStoreHexKeySizeBytes`.
Saketh Pothireddyeb3aa622023-09-28 20:14:03113void TrimLogStoreKey(std::string& key);
114
115// Get log encryption key from VPD. Returns `nullopt` if not found.
Saketh Pothireddy8c66ed12023-10-29 05:14:44116std::optional<brillo::SecureBlob> GetLogStoreKey(
Saketh Pothireddy66f7e1a2023-10-28 05:05:10117 std::shared_ptr<ProcessManagerInterface> process_manager);
Saketh Pothireddyeb3aa622023-09-28 20:14:03118
119// Save a given log encryption key to VPD. Returns true on success, false
120// otherwise.
Saketh Pothireddy66f7e1a2023-10-28 05:05:10121bool SaveLogStoreKey(std::shared_ptr<ProcessManagerInterface> process_manager,
Saketh Pothireddy8c66ed12023-10-29 05:14:44122 const brillo::SecureBlob& key);
Saketh Pothireddyeb3aa622023-09-28 20:14:03123
Saketh Pothireddy53176292023-05-23 16:52:36124} // namespace minios
Jae Hoon Kim0fbd6472021-04-29 19:08:33125#endif // MINIOS_UTILS_H__