From 18bdd771f015b3b3516e09cb3f08de5883e1c339 Mon Sep 17 00:00:00 2001 From: "A. Cody Schuffelen" Date: Wed, 18 Mar 2026 19:12:04 +0000 Subject: [PATCH] Split EspBuilder into its own file under config/esp #gemini Bug: b/492644615 Test: bazel test //... --- .../cuttlefish/host/libs/config/BUILD.bazel | 3 +- base/cvd/cuttlefish/host/libs/config/esp.cpp | 110 +-------------- .../host/libs/config/esp/BUILD.bazel | 13 ++ .../host/libs/config/esp/esp_builder.cc | 129 ++++++++++++++++++ .../host/libs/config/esp/esp_builder.h | 47 +++++++ 5 files changed, 191 insertions(+), 111 deletions(-) create mode 100644 base/cvd/cuttlefish/host/libs/config/esp/esp_builder.cc create mode 100644 base/cvd/cuttlefish/host/libs/config/esp/esp_builder.h diff --git a/base/cvd/cuttlefish/host/libs/config/BUILD.bazel b/base/cvd/cuttlefish/host/libs/config/BUILD.bazel index a4cb4535150..cf7204f7260 100644 --- a/base/cvd/cuttlefish/host/libs/config/BUILD.bazel +++ b/base/cvd/cuttlefish/host/libs/config/BUILD.bazel @@ -202,8 +202,7 @@ cf_cc_library( "//cuttlefish/common/libs/utils:files", "//cuttlefish/common/libs/utils:host_info", "//cuttlefish/common/libs/utils:subprocess", - "//cuttlefish/host/libs/config:known_paths", - "//cuttlefish/host/libs/config/esp:make_fat_image", + "//cuttlefish/host/libs/config/esp:esp_builder", "//libbase", "@abseil-cpp//absl/log", "@abseil-cpp//absl/strings", diff --git a/base/cvd/cuttlefish/host/libs/config/esp.cpp b/base/cvd/cuttlefish/host/libs/config/esp.cpp index e5e7bc2a546..c63a514f4b7 100644 --- a/base/cvd/cuttlefish/host/libs/config/esp.cpp +++ b/base/cvd/cuttlefish/host/libs/config/esp.cpp @@ -33,8 +33,7 @@ #include "cuttlefish/common/libs/utils/files.h" #include "cuttlefish/common/libs/utils/host_info.h" #include "cuttlefish/common/libs/utils/subprocess.h" -#include "cuttlefish/host/libs/config/esp/make_fat_image.h" -#include "cuttlefish/host/libs/config/known_paths.h" +#include "cuttlefish/host/libs/config/esp/esp_builder.h" namespace cuttlefish { @@ -108,28 +107,6 @@ bool CanGenerateGrubEsp(Arch arch) { } } -static bool MsdosMakeDirectories(const std::string& image_path, - const std::vector& directories) { - std::vector command {MmdBinary(), "-i", image_path}; - command.insert(command.end(), directories.begin(), directories.end()); - - const auto success = Execute(command); - if (success != 0) { - return false; - } - return true; -} - -static bool CopyToMsdos(const std::string& image, const std::string& path, - const std::string& destination) { - const auto success = - Execute({McopyBinary(), "-o", "-i", image, "-s", path, destination}); - if (success != 0) { - return false; - } - return true; -} - template static bool GrubMakeImage(const std::string& prefix, const std::string& format, const std::string& directory, @@ -143,91 +120,6 @@ static bool GrubMakeImage(const std::string& prefix, const std::string& format, return success == 0; } -class EspBuilder final { - public: - EspBuilder() {} - EspBuilder(std::string image_path): image_path_(std::move(image_path)) {} - - EspBuilder& File(std::string from, std::string to, bool required) & { - files_.push_back(FileToAdd {std::move(from), std::move(to), required}); - return *this; - } - - EspBuilder& File(std::string from, std::string to) & { - return File(std::move(from), std::move(to), false); - } - - EspBuilder& Directory(std::string path) & { - directories_.push_back(std::move(path)); - return *this; - } - - EspBuilder& Merge(EspBuilder builder) & { - std::move(builder.directories_.begin(), builder.directories_.end(), - std::back_inserter(directories_)); - std::move(builder.files_.begin(), builder.files_.end(), - std::back_inserter(files_)); - return *this; - } - - bool Build() { - if (image_path_.empty()) { - LOG(ERROR) << "Image path is required to build ESP. Empty constructor is intended to " - << "be used only for the merge functionality"; - return false; - } - - // newfs_msdos won't make a partition smaller than 257 mb - // this should be enough for anybody.. - const auto tmp_esp_image = image_path_ + ".tmp"; - if (!MakeFatImage(tmp_esp_image, 257 /* mb */, 0 /* mb (offset) */).ok()) { - LOG(ERROR) << "Failed to create filesystem for " << tmp_esp_image; - return false; - } - - if (!MsdosMakeDirectories(tmp_esp_image, directories_)) { - LOG(ERROR) << "Failed to create directories in " << tmp_esp_image; - return false; - } - - for (const FileToAdd& file : files_) { - if (!FileExists(file.from)) { - if (file.required) { - LOG(ERROR) << "Failed to copy " << file.from << " to " << tmp_esp_image - << ": File does not exist"; - return false; - } - continue; - } - - if (!CopyToMsdos(tmp_esp_image, file.from, "::" + file.to)) { - LOG(ERROR) << "Failed to copy " << file.from << " to " << tmp_esp_image - << ": mcopy execution failed"; - return false; - } - } - - if (!RenameFile(tmp_esp_image, image_path_).ok()) { - LOG(ERROR) << "Renaming " << tmp_esp_image << " to " - << image_path_ << " failed"; - return false; - } - - return true; - } - - private: - const std::string image_path_; - - struct FileToAdd { - std::string from; - std::string to; - bool required; - }; - std::vector directories_; - std::vector files_; -}; - EspBuilder PrepareGrubESP(const std::string& image_path, Arch arch) { auto builder = EspBuilder(image_path); builder.Directory("EFI").Directory("EFI/BOOT").Directory("EFI/modules"); diff --git a/base/cvd/cuttlefish/host/libs/config/esp/BUILD.bazel b/base/cvd/cuttlefish/host/libs/config/esp/BUILD.bazel index 5fe2909a01c..2e24f9aa66a 100644 --- a/base/cvd/cuttlefish/host/libs/config/esp/BUILD.bazel +++ b/base/cvd/cuttlefish/host/libs/config/esp/BUILD.bazel @@ -19,3 +19,16 @@ cf_cc_library( "//cuttlefish/result:result_type", ], ) + +cf_cc_library( + name = "esp_builder", + srcs = ["esp_builder.cc"], + hdrs = ["esp_builder.h"], + deps = [ + "//cuttlefish/common/libs/utils:files", + "//cuttlefish/common/libs/utils:subprocess", + "//cuttlefish/host/libs/config:known_paths", + "//cuttlefish/host/libs/config/esp:make_fat_image", + "@abseil-cpp//absl/log", + ], +) diff --git a/base/cvd/cuttlefish/host/libs/config/esp/esp_builder.cc b/base/cvd/cuttlefish/host/libs/config/esp/esp_builder.cc new file mode 100644 index 00000000000..0db706bf027 --- /dev/null +++ b/base/cvd/cuttlefish/host/libs/config/esp/esp_builder.cc @@ -0,0 +1,129 @@ +// +// Copyright (C) 2022 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "cuttlefish/host/libs/config/esp/esp_builder.h" + +#include +#include +#include +#include +#include + +#include "absl/log/log.h" + +#include "cuttlefish/common/libs/utils/files.h" +#include "cuttlefish/common/libs/utils/subprocess.h" +#include "cuttlefish/host/libs/config/esp/make_fat_image.h" +#include "cuttlefish/host/libs/config/known_paths.h" + +namespace cuttlefish { + +static bool MsdosMakeDirectories(const std::string& image_path, + const std::vector& directories) { + std::vector command{MmdBinary(), "-i", image_path}; + command.insert(command.end(), directories.begin(), directories.end()); + + const auto success = Execute(command); + if (success != 0) { + return false; + } + return true; +} + +static bool CopyToMsdos(const std::string& image, const std::string& path, + const std::string& destination) { + const auto success = + Execute({McopyBinary(), "-o", "-i", image, "-s", path, destination}); + if (success != 0) { + return false; + } + return true; +} + +EspBuilder::EspBuilder() {} +EspBuilder::EspBuilder(std::string image_path) + : image_path_(std::move(image_path)) {} + +EspBuilder& EspBuilder::File(std::string from, std::string to, + bool required) & { + files_.push_back(FileToAdd{std::move(from), std::move(to), required}); + return *this; +} + +EspBuilder& EspBuilder::File(std::string from, std::string to) & { + return File(std::move(from), std::move(to), false); +} + +EspBuilder& EspBuilder::Directory(std::string path) & { + directories_.push_back(std::move(path)); + return *this; +} + +EspBuilder& EspBuilder::Merge(EspBuilder builder) & { + std::move(builder.directories_.begin(), builder.directories_.end(), + std::back_inserter(directories_)); + std::move(builder.files_.begin(), builder.files_.end(), + std::back_inserter(files_)); + return *this; +} + +bool EspBuilder::Build() { + if (image_path_.empty()) { + LOG(ERROR) << "Image path is required to build ESP. Empty constructor is " + "intended to " + << "be used only for the merge functionality"; + return false; + } + + // newfs_msdos won't make a partition smaller than 257 mb + // this should be enough for anybody.. + const auto tmp_esp_image = image_path_ + ".tmp"; + if (!MakeFatImage(tmp_esp_image, 257 /* mb */, 0 /* mb (offset) */).ok()) { + LOG(ERROR) << "Failed to create filesystem for " << tmp_esp_image; + return false; + } + + if (!MsdosMakeDirectories(tmp_esp_image, directories_)) { + LOG(ERROR) << "Failed to create directories in " << tmp_esp_image; + return false; + } + + for (const FileToAdd& file : files_) { + if (!FileExists(file.from)) { + if (file.required) { + LOG(ERROR) << "Failed to copy " << file.from << " to " << tmp_esp_image + << ": File does not exist"; + return false; + } + continue; + } + + if (!CopyToMsdos(tmp_esp_image, file.from, "::" + file.to)) { + LOG(ERROR) << "Failed to copy " << file.from << " to " << tmp_esp_image + << ": mcopy execution failed"; + return false; + } + } + + if (!RenameFile(tmp_esp_image, image_path_).ok()) { + LOG(ERROR) << "Renaming " << tmp_esp_image << " to " << image_path_ + << " failed"; + return false; + } + + return true; +} + +} // namespace cuttlefish diff --git a/base/cvd/cuttlefish/host/libs/config/esp/esp_builder.h b/base/cvd/cuttlefish/host/libs/config/esp/esp_builder.h new file mode 100644 index 00000000000..49bf6f650bb --- /dev/null +++ b/base/cvd/cuttlefish/host/libs/config/esp/esp_builder.h @@ -0,0 +1,47 @@ +// +// Copyright (C) 2022 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include +#include + +namespace cuttlefish { + +class EspBuilder final { + public: + EspBuilder(); + EspBuilder(std::string image_path); + + EspBuilder& File(std::string from, std::string to, bool required) &; + EspBuilder& File(std::string from, std::string to) &; + EspBuilder& Directory(std::string path) &; + EspBuilder& Merge(EspBuilder builder) &; + + bool Build(); + + private: + const std::string image_path_; + + struct FileToAdd { + std::string from; + std::string to; + bool required; + }; + std::vector directories_; + std::vector files_; +}; + +} // namespace cuttlefish