aboutsummaryrefslogtreecommitdiff
path: root/host
diff options
context:
space:
mode:
authorChad Reynolds <chadreynolds@google.com>2024-04-11 15:14:57 -0700
committerChad Reynolds <chadreynolds@google.com>2024-04-18 17:44:09 -0700
commit910e4c88b7bf699988b23e21ee6588387154a8ae (patch)
treeeea013bbfcfda3661c3f9d5bd54b96628d215a88 /host
parent222e3167670344c9d188a9f38e84bcffd9d4ae27 (diff)
downloadcuttlefish-910e4c88b7bf699988b23e21ee6588387154a8ae.tar.gz
Move `info_image` logic into `Avb`
Continuing to encapsulate all of the `avbtool` calls into this helper. Bug: 322887496 Test: launch_cvd --resume=false --daemon Test: # the `info_image` is written to a temporary `boot_params` file Test: # if it did not work then we would get an error message Test: # https://cs.android.com/android/platform/superproject/main/+/3d19fbcc09b1b44928639b06cd0b88f735cd988d:device/google/cuttlefish/host/commands/assemble_cvd/boot_image_utils.cc;l=488;drc=3d19fbcc09b1b44928639b06cd0b88f735cd988d Change-Id: I6fdb0333842621edd965d7b49d03152708a3e3ee
Diffstat (limited to 'host')
-rw-r--r--host/commands/assemble_cvd/boot_image_utils.cc34
-rw-r--r--host/libs/avb/avb.cpp24
-rw-r--r--host/libs/avb/avb.h6
3 files changed, 41 insertions, 23 deletions
diff --git a/host/commands/assemble_cvd/boot_image_utils.cc b/host/commands/assemble_cvd/boot_image_utils.cc
index 10f1ab0f0..1fbeef543 100644
--- a/host/commands/assemble_cvd/boot_image_utils.cc
+++ b/host/commands/assemble_cvd/boot_image_utils.cc
@@ -15,13 +15,12 @@
*/
#include "host/commands/assemble_cvd/boot_image_utils.h"
-#include "common/libs/fs/shared_fd.h"
-#include "host/libs/config/cuttlefish_config.h"
#include <string.h>
#include <unistd.h>
#include <fstream>
+#include <memory>
#include <regex>
#include <sstream>
#include <string>
@@ -29,10 +28,12 @@
#include <android-base/logging.h>
#include <android-base/strings.h>
+#include "common/libs/fs/shared_fd.h"
#include "common/libs/utils/files.h"
#include "common/libs/utils/result.h"
#include "common/libs/utils/subprocess.h"
#include "host/libs/avb/avb.cpp"
+#include "host/libs/config/cuttlefish_config.h"
#include "host/libs/config/known_paths.h"
const char TMP_EXTENSION[] = ".tmp";
@@ -161,26 +162,13 @@ void UnpackRamdisk(const std::string& original_ramdisk_path,
<< success;
}
-bool GetAvbMetadatFromBootImage(const std::string& boot_image_path,
- const std::string& unpack_dir) {
- auto avbtool_path = HostBinaryPath("avbtool");
- Command avb_cmd(avbtool_path);
- avb_cmd.AddParameter("info_image");
- avb_cmd.AddParameter("--image");
- avb_cmd.AddParameter(boot_image_path);
-
- auto output_file = SharedFD::Creat(unpack_dir + "/boot_params", 0666);
- if (!output_file->IsOpen()) {
- LOG(ERROR) << "Unable to create intermediate boot params file: "
- << output_file->StrError();
- return false;
- }
- avb_cmd.RedirectStdIO(Subprocess::StdIOChannel::kStdOut, output_file);
-
- int success = avb_cmd.Start().Wait();
-
- if (success != 0) {
- LOG(ERROR) << "Unable to run avb command. Exited with status " << success;
+bool GetAvbMetadataFromBootImage(const std::string& boot_image_path,
+ const std::string& unpack_dir) {
+ std::unique_ptr<Avb> avbtool = GetDefaultAvb();
+ Result<void> result =
+ avbtool->WriteInfoImage(boot_image_path, unpack_dir + "/boot_params");
+ if (!result.ok()) {
+ LOG(ERROR) << result.error().Trace();
return false;
}
return true;
@@ -465,7 +453,7 @@ Result<std::string> ReadAndroidVersionFromBootImage(
if (!unpack_dir) {
return CF_ERR("boot image unpack dir could not be created");
}
- bool unpack_status = GetAvbMetadatFromBootImage(boot_image_path, unpack_dir);
+ bool unpack_status = GetAvbMetadataFromBootImage(boot_image_path, unpack_dir);
if (!unpack_status) {
RecursivelyRemoveDirectory(unpack_dir);
return CF_ERR("\"" + boot_image_path + "\" boot image unpack into \"" +
diff --git a/host/libs/avb/avb.cpp b/host/libs/avb/avb.cpp
index 956f814bb..76c96d131 100644
--- a/host/libs/avb/avb.cpp
+++ b/host/libs/avb/avb.cpp
@@ -22,6 +22,7 @@
#include <fruit/fruit.h>
+#include "common/libs/fs/shared_fd.h"
#include "common/libs/utils/result.h"
#include "common/libs/utils/subprocess.h"
#include "host/libs/config/cuttlefish_config.h"
@@ -32,6 +33,7 @@ namespace {
constexpr char kAddHashFooter[] = "add_hash_footer";
constexpr char kDefaultAlgorithm[] = "SHA256_RSA4096";
+constexpr char kInfoImage[] = "info_image";
} // namespace
@@ -75,6 +77,28 @@ Result<void> Avb::AddHashFooter(const std::string& image_path,
return {};
}
+Command Avb::GenerateInfoImage(const std::string& image_path,
+ const SharedFD& output_file) const {
+ Command command(avbtool_path_);
+ command.AddParameter(kInfoImage);
+ command.AddParameter("--image");
+ command.AddParameter(image_path);
+ command.RedirectStdIO(Subprocess::StdIOChannel::kStdOut, output_file);
+ return command;
+}
+
+Result<void> Avb::WriteInfoImage(const std::string& image_path,
+ const std::string& output_path) const {
+ auto output_file = SharedFD::Creat(output_path, 0666);
+ CF_EXPECTF(output_file->IsOpen(), "Unable to create {} with error - {}",
+ output_path, output_file->StrError());
+ auto command = GenerateInfoImage(image_path, output_file);
+ int exit_code = command.Start().Wait();
+ CF_EXPECTF(exit_code == 0, "Failure running {} {}. Exited with status {}",
+ command.Executable(), kInfoImage, exit_code);
+ return {};
+}
+
std::unique_ptr<Avb> GetDefaultAvb() {
return std::unique_ptr<Avb>(
new Avb(AvbToolBinary(), kDefaultAlgorithm, TestKeyRsa4096()));
diff --git a/host/libs/avb/avb.h b/host/libs/avb/avb.h
index e7767e883..6a1a257a7 100644
--- a/host/libs/avb/avb.h
+++ b/host/libs/avb/avb.h
@@ -25,6 +25,7 @@
#include <fruit/fruit.h>
+#include "common/libs/fs/shared_fd.h"
#include "common/libs/utils/subprocess.h"
namespace cuttlefish {
@@ -48,11 +49,16 @@ class Avb {
Result<void> AddHashFooter(const std::string& image_path,
const std::string& partition_name,
const off_t partition_size_bytes) const;
+ Result<void> WriteInfoImage(const std::string& image_path,
+ const std::string& output_path) const;
private:
Command GenerateAddHashFooter(const std::string& image_path,
const std::string& partition_name,
const off_t partition_size_bytes) const;
+ Command GenerateInfoImage(const std::string& image_path,
+ const SharedFD& output_path) const;
+
std::string avbtool_path_;
std::string algorithm_;
std::string key_;