blob: 79e9fe07174df249f69765812458cf137e3c1021 [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>
Jae Hoon Kimade1b6c2023-11-09 05:22:3216#include <base/strings/stringprintf.h>
Saketh Pothireddy8c66ed12023-10-29 05:14:4417#include <brillo/secure_blob.h>
Saketh Pothireddy7d631002023-09-18 20:37:3518#include <brillo/udev/udev.h>
Saketh Pothireddy0a254f62023-11-09 01:26:3319#include <libcrossystem/crossystem.h>
Saketh Pothireddyecd2d402023-10-29 04:13:0720#include <minios/proto_bindings/minios.pb.h>
Saketh Pothireddy1fe90eb2024-04-03 17:34:1821#include <vpd/vpd.h>
Jae Hoon Kim0fbd6472021-04-29 19:08:3322
Jae Hoon Kim7c70ae42024-02-14 07:27:5723#include "minios/process_manager.h"
Vyshu70660892021-06-09 16:52:2624
Jae Hoon Kim0fbd6472021-04-29 19:08:3325namespace minios {
26
Saketh Pothireddy2038a782023-03-03 00:00:5727// Alert Log error categories.
28extern const char kCategoryInit[];
29extern const char kCategoryReboot[];
30extern const char kCategoryUpdate[];
31
Saketh Pothireddy7230b622023-06-23 23:55:0932extern const char kLogFilePath[];
33
Saketh Pothireddy53176292023-05-23 16:52:3634extern const base::FilePath kDefaultArchivePath;
Saketh Pothireddy0a254f62023-11-09 01:26:3335extern const int kLogStoreKeySizeBytes;
Saketh Pothireddy599c84e2024-03-19 18:26:4136extern const brillo::SecureBlob kNullKey;
Saketh Pothireddy53176292023-05-23 16:52:3637
Saketh Pothireddy1e5b22e2023-11-29 17:43:3338extern const base::FilePath kStatefulPath;
39extern const base::FilePath kUnencryptedMiniosPath;
40extern const char kLogArchiveFile[];
41
Jae Hoon Kim0fbd6472021-04-29 19:08:3342// 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.
48std::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`.
65std::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
Vyshu25e45bf2021-09-02 20:36:3871// Gets VPD region data given a key. Returns false on failure.
Saketh Pothireddy66f7e1a2023-10-28 05:05:1072bool GetCrosRegionData(std::shared_ptr<ProcessManagerInterface> process_manager,
Vyshu25e45bf2021-09-02 20:36:3873 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 Pothireddy66f7e1a2023-10-28 05:05:1078std::string GetKeyboardLayout(
79 std::shared_ptr<ProcessManagerInterface> process_manager);
Vyshu70660892021-06-09 16:52:2680
Yuanpeng Ni6e6d6cf2023-03-22 04:28:3781// Read frecon created symbolic link and return the virtual terminal path.
82base::FilePath GetLogConsole();
83
Vyshue9a22a7b2021-10-08 14:55:5384bool TriggerShutdown();
85
Saketh Pothireddy2038a782023-03-03 00:00:5786// 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 << ....;`
89inline std::string AlertLogTag(const std::string& category) {
90 return base::StringPrintf("[CoreServicesAlert<%s>] ", category.c_str());
91}
92
Saketh Pothireddya9f31832023-11-10 20:54:3193// Mount the stateful partition at `/stateful/`. Returns true if successfully
94// mounted, false otherwise.
Saketh Pothireddy66f7e1a2023-10-28 05:05:1095bool MountStatefulPartition(
96 std::shared_ptr<ProcessManagerInterface> process_manager);
Saketh Pothireddyc131f042023-05-25 18:17:0297
Saketh Pothireddya9f31832023-11-10 20:54:3198// Unmount path. Returns true if successfully unmounted, false otherwise.
99bool UnmountPath(std::shared_ptr<ProcessManagerInterface> process_manager,
100 const base::FilePath& path);
101
102// Unmount `kStatefulPath`. Returns true if successful, false otherwise.
103bool 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 Pothireddy66f7e1a2023-10-28 05:05:10108int CompressLogs(std::shared_ptr<ProcessManagerInterface> process_manager,
Saketh Pothireddy53176292023-05-23 16:52:36109 const base::FilePath& archive_path = kDefaultArchivePath);
Jae Hoon Kim0fbd6472021-04-29 19:08:33110
Saketh Pothireddyb41d8a92023-06-21 02:45:36111// Calculate kernel size.
112std::optional<uint64_t> KernelSize(
Saketh Pothireddy66f7e1a2023-10-28 05:05:10113 std::shared_ptr<ProcessManagerInterface> process_manager,
Saketh Pothireddyb41d8a92023-06-21 02:45:36114 const base::FilePath& device);
115
Saketh Pothireddy18a92342023-08-15 21:10:48116// Read the kernel cmdline and get the current version.
117std::optional<std::string> GetMiniOSVersion();
118
Saketh Pothireddy7d631002023-09-18 20:37:35119// 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.
122bool GetRemovableDevices(
123 std::vector<base::FilePath>& devices,
124 std::unique_ptr<brillo::Udev> udev = brillo::Udev::Create());
125
Saketh Pothireddyeb3aa622023-09-28 20:14:03126// Check if the given log store key is valid.
Saketh Pothireddy8c66ed12023-10-29 05:14:44127bool IsLogStoreKeyValid(const brillo::SecureBlob& key);
Saketh Pothireddyeb3aa622023-09-28 20:14:03128
Saketh Pothireddyeb3aa622023-09-28 20:14:03129// Get log encryption key from VPD. Returns `nullopt` if not found.
Saketh Pothireddy1fe90eb2024-04-03 17:34:18130std::optional<brillo::SecureBlob> GetLogStoreKey(std::shared_ptr<vpd::Vpd> vpd);
Saketh Pothireddyeb3aa622023-09-28 20:14:03131
132// Save a given log encryption key to VPD. Returns true on success, false
133// otherwise.
Saketh Pothireddy1fe90eb2024-04-03 17:34:18134bool SaveLogStoreKey(std::shared_ptr<vpd::Vpd> vpd,
Saketh Pothireddy8c66ed12023-10-29 05:14:44135 const brillo::SecureBlob& key);
Saketh Pothireddyeb3aa622023-09-28 20:14:03136
Saketh Pothireddy0a254f62023-11-09 01:26:33137// Overwrite log store key in VPD with zeros. Returns true on success, false
138// otherwise.
Saketh Pothireddy1fe90eb2024-04-03 17:34:18139bool ClearLogStoreKey(std::shared_ptr<vpd::Vpd> vpd);
Saketh Pothireddy0a254f62023-11-09 01:26:33140
Saketh Pothireddyecd2d402023-10-29 04:13:07141// Read contents of a given file into a secureblob. Returns file contents on
142// success and nullopt otherwise.
143std::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.
148bool 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 Pothireddy1e5b22e2023-11-29 17:43:33153std::optional<EncryptedLogFile> EncryptLogArchive(
Saketh Pothireddyecd2d402023-10-29 04:13:07154 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 Pothireddy1e5b22e2023-11-29 17:43:33158std::optional<brillo::SecureBlob> DecryptLogArchive(
159 const EncryptedLogFile& encrypted_archive, const brillo::SecureBlob& key);
Saketh Pothireddyecd2d402023-10-29 04:13:07160
Saketh Pothireddy0a254f62023-11-09 01:26:33161std::optional<uint64_t> GetMiniOsPriorityPartition(
162 std::shared_ptr<crossystem::Crossystem> cros_system);
163
Saketh Pothireddy1e5b22e2023-11-29 17:43:33164// Check whether currently running in MiniOS. Returns nullopt if environment
165// cannot be determined. Otherwise true if running in MiniOS, false if not.
166std::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.
170bool 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 Pothireddy53176292023-05-23 16:52:36175} // namespace minios
Saketh Pothireddyecd2d402023-10-29 04:13:07176
Jae Hoon Kim0fbd6472021-04-29 19:08:33177#endif // MINIOS_UTILS_H__