Yong Hong | 42dd1f8 | 2019-04-18 14:49:22 +0800 | [diff] [blame] | 1 | /* Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | */ |
| 5 | |
| 6 | #include <sysexits.h> |
| 7 | |
| 8 | #include <cstdlib> |
| 9 | #include <iostream> |
Wei-Han Chen | d21ec07 | 2020-01-14 20:01:42 +0800 | [diff] [blame] | 10 | #include <memory> |
Yong Hong | 42dd1f8 | 2019-04-18 14:49:22 +0800 | [diff] [blame] | 11 | |
Kevin Lin | 7d809c9 | 2020-02-19 11:54:52 +0800 | [diff] [blame] | 12 | #include <base/at_exit.h> |
Yong Hong | 42dd1f8 | 2019-04-18 14:49:22 +0800 | [diff] [blame] | 13 | #include <base/logging.h> |
| 14 | #include <brillo/flag_helper.h> |
| 15 | #include <brillo/syslog_logging.h> |
| 16 | |
| 17 | #include "hardware_verifier/cli.h" |
Wei-Han Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame] | 18 | #include "hardware_verifier/observer.h" |
Yong Hong | 42dd1f8 | 2019-04-18 14:49:22 +0800 | [diff] [blame] | 19 | |
| 20 | namespace { |
| 21 | |
| 22 | enum ExitStatus { |
| 23 | kSuccess = EXIT_SUCCESS, // 0 |
| 24 | |
| 25 | // The verification report shows the device is not complicant. |
| 26 | kVerifiedFail = 1, |
| 27 | |
| 28 | kUnknownError = 10, |
| 29 | |
| 30 | // Some of the argument is invalid. |
| 31 | kInvalidArgument = 11, |
| 32 | }; |
| 33 | |
| 34 | // Translate the error code from |hardware_verifier::CLI::Run()| to |
| 35 | // the corresponding return code. |
| 36 | ExitStatus ConvertCLIVerificationResultToExitStatus( |
| 37 | const hardware_verifier::CLIVerificationResult& verification_result) { |
| 38 | using hardware_verifier::CLIVerificationResult; |
| 39 | switch (verification_result) { |
| 40 | case CLIVerificationResult::kPass: |
| 41 | return ExitStatus::kSuccess; |
Clark Chung | 1000c38 | 2020-03-13 11:44:27 +0800 | [diff] [blame] | 42 | case CLIVerificationResult::kFail: |
| 43 | return ExitStatus::kVerifiedFail; |
Yong Hong | 42dd1f8 | 2019-04-18 14:49:22 +0800 | [diff] [blame] | 44 | case CLIVerificationResult::kInvalidHwVerificationSpecFile: |
| 45 | case CLIVerificationResult::kInvalidProbeResultFile: |
| 46 | return ExitStatus::kInvalidArgument; |
| 47 | default: |
| 48 | return ExitStatus::kUnknownError; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | int SafeConvertVerbosityFlagToLogLevel(int verbosity_flag) { |
| 53 | if (verbosity_flag < 0 || 5 < verbosity_flag) { |
| 54 | LOG(ERROR) << "The verbosity value (" << verbosity_flag |
| 55 | << ") is out of range."; |
| 56 | exit(EX_USAGE); |
| 57 | } |
| 58 | // We would like to print |VLOG(K)| messages if the verbosity level is `K`. |
| 59 | // Since the corresponding log level is `-K`, we can resolve the correct |
| 60 | // log level by simply taking a negative sign. |
| 61 | return -verbosity_flag; |
| 62 | } |
| 63 | |
| 64 | hardware_verifier::CLIOutputFormat SafeConvertOutputFormatFlagToEnum( |
| 65 | const std::string& output_format_flag) { |
| 66 | if (output_format_flag == "proto") { |
| 67 | return hardware_verifier::CLIOutputFormat::kProtoBin; |
| 68 | } |
| 69 | if (output_format_flag == "text") { |
Yong Hong | 85a4dff | 2019-05-25 04:22:47 +0800 | [diff] [blame] | 70 | return hardware_verifier::CLIOutputFormat::kText; |
Yong Hong | 42dd1f8 | 2019-04-18 14:49:22 +0800 | [diff] [blame] | 71 | } |
| 72 | LOG(ERROR) << "The output format (" << output_format_flag << ") is invalid."; |
| 73 | exit(EX_USAGE); |
| 74 | } |
| 75 | |
| 76 | } // namespace |
| 77 | |
| 78 | int main(int argc, char* argv[]) { |
Kevin Lin | 7d809c9 | 2020-02-19 11:54:52 +0800 | [diff] [blame] | 79 | // Required by dbus in libchrome. |
| 80 | base::AtExitManager at_exit_manager; |
| 81 | |
Yong Hong | 42dd1f8 | 2019-04-18 14:49:22 +0800 | [diff] [blame] | 82 | DEFINE_int32(verbosity, 0, |
| 83 | "Verbosity level, range from 0 to 5. The greater number is " |
| 84 | "set, the more detail messages will be printed."); |
| 85 | DEFINE_string(probe_result_file, "", |
| 86 | "File path to the probe result in prototxt format, empty to " |
| 87 | "get directly from |runtime_probe| D-Bus service."); |
| 88 | DEFINE_string(hw_verification_spec_file, "", |
| 89 | "File path to the hardware verification spec in prototxt " |
| 90 | "format, empty to use the default one."); |
| 91 | DEFINE_string(output_format, "proto", |
| 92 | "Format of the output verification report, can be either " |
| 93 | "\"proto\" for protobuf binary format or \"text\" for human " |
| 94 | "readable text format."); |
Wei-Han Chen | d21ec07 | 2020-01-14 20:01:42 +0800 | [diff] [blame] | 95 | DEFINE_bool(send_to_uma, false, "Send data to UMA."); |
Kevin Lin | ef321a5 | 2020-09-01 18:31:52 +0800 | [diff] [blame] | 96 | DEFINE_bool(pii, false, |
| 97 | "Output result including PII data like UUID and generic device " |
| 98 | "info."); |
Yong Hong | 42dd1f8 | 2019-04-18 14:49:22 +0800 | [diff] [blame] | 99 | brillo::FlagHelper::Init(argc, argv, "ChromeOS Hardware Verifier Tool"); |
| 100 | |
| 101 | brillo::InitLog(brillo::kLogToSyslog | brillo::kLogToStderr); |
| 102 | |
| 103 | // Validate the non-trivial flags and convert them to the proper value types. |
| 104 | const auto log_level = SafeConvertVerbosityFlagToLogLevel(FLAGS_verbosity); |
| 105 | const auto output_format = |
| 106 | SafeConvertOutputFormatFlagToEnum(FLAGS_output_format); |
| 107 | |
| 108 | logging::SetMinLogLevel(log_level); |
| 109 | |
Wei-Han Chen | 918b3ba | 2020-03-20 18:26:37 +0800 | [diff] [blame] | 110 | auto observer = hardware_verifier::Observer::GetInstance(); |
Wei-Han Chen | d21ec07 | 2020-01-14 20:01:42 +0800 | [diff] [blame] | 111 | if (FLAGS_send_to_uma) { |
Wei-Han Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame] | 112 | observer->SetMetricsLibrary(std::make_unique<MetricsLibrary>()); |
Wei-Han Chen | d21ec07 | 2020-01-14 20:01:42 +0800 | [diff] [blame] | 113 | } |
Wei-Han Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame] | 114 | observer->StartTimer(hardware_verifier::kMetricTimeToFinish); |
Yong Hong | 42dd1f8 | 2019-04-18 14:49:22 +0800 | [diff] [blame] | 115 | // TODO(yhong): Add the D-Bus service mode. |
| 116 | |
| 117 | hardware_verifier::CLI cli; |
Kevin Lin | ef321a5 | 2020-09-01 18:31:52 +0800 | [diff] [blame] | 118 | const auto cli_result = |
| 119 | cli.Run(FLAGS_probe_result_file, FLAGS_hw_verification_spec_file, |
| 120 | output_format, FLAGS_pii); |
Wei-Han Chen | d21ec07 | 2020-01-14 20:01:42 +0800 | [diff] [blame] | 121 | |
| 122 | const auto exit_status = ConvertCLIVerificationResultToExitStatus(cli_result); |
| 123 | |
Wei-Han Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame] | 124 | observer->StopTimer(hardware_verifier::kMetricTimeToFinish); |
Wei-Han Chen | d21ec07 | 2020-01-14 20:01:42 +0800 | [diff] [blame] | 125 | return exit_status; |
Yong Hong | 42dd1f8 | 2019-04-18 14:49:22 +0800 | [diff] [blame] | 126 | } |