diff --git a/base/cvd/cuttlefish/host/commands/assemble_cvd/android_build/BUILD.bazel b/base/cvd/cuttlefish/host/commands/assemble_cvd/android_build/BUILD.bazel index 48f8e593080..02322755100 100644 --- a/base/cvd/cuttlefish/host/commands/assemble_cvd/android_build/BUILD.bazel +++ b/base/cvd/cuttlefish/host/commands/assemble_cvd/android_build/BUILD.bazel @@ -129,6 +129,16 @@ cf_cc_library( ], ) +cf_cc_library( + name = "physical_partitions", + srcs = ["physical_partitions.cc"], + hdrs = ["physical_partitions.h"], + deps = [ + "//cuttlefish/host/commands/assemble_cvd/android_build", + "//cuttlefish/result", + ], +) + cf_cc_library( name = "super_image", srcs = ["super_image.cc"], diff --git a/base/cvd/cuttlefish/host/commands/assemble_cvd/android_build/physical_partitions.cc b/base/cvd/cuttlefish/host/commands/assemble_cvd/android_build/physical_partitions.cc new file mode 100644 index 00000000000..809b4a98b69 --- /dev/null +++ b/base/cvd/cuttlefish/host/commands/assemble_cvd/android_build/physical_partitions.cc @@ -0,0 +1,70 @@ +// +// Copyright (C) 2025 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/commands/assemble_cvd/android_build/physical_partitions.h" + +#include +#include +#include +#include + +#include "cuttlefish/host/commands/assemble_cvd/android_build/android_build.h" +#include "cuttlefish/result/result.h" + +namespace cuttlefish { +namespace { + +class PhysicalPartitionsImpl : public AndroidBuild { + public: + PhysicalPartitionsImpl(AndroidBuild& build) : build_(build) {} + + Result>> PhysicalPartitions() override { + if (auto res = build_.PhysicalPartitions(); res.ok()) { + return *res; + } + + std::set> partitions = + CF_EXPECT(build_.Images()); + for (std::string logical : CF_EXPECT(build_.LogicalPartitions())) { + partitions.erase(logical); + } + + if (partitions.count("super_empty")) { + partitions.erase("super_empty"); + partitions.insert("super"); + } + + return partitions; + } + + private: + std::ostream& Format(std::ostream& out) const override { + return out << "PhysicalPartitions"; + } + + AndroidBuild& build_; +}; + +} // namespace + +Result> PhysicalPartitions(AndroidBuild& build) { + auto partitions = std::make_unique(build); + + CF_EXPECT(partitions->PhysicalPartitions()); + + return partitions; +} + +} // namespace cuttlefish diff --git a/base/cvd/cuttlefish/host/commands/assemble_cvd/android_build/physical_partitions.h b/base/cvd/cuttlefish/host/commands/assemble_cvd/android_build/physical_partitions.h new file mode 100644 index 00000000000..73fa1b87a27 --- /dev/null +++ b/base/cvd/cuttlefish/host/commands/assemble_cvd/android_build/physical_partitions.h @@ -0,0 +1,37 @@ +// +// Copyright (C) 2025 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 "cuttlefish/host/commands/assemble_cvd/android_build/android_build.h" +#include "cuttlefish/result/result.h" + +namespace cuttlefish { + +/** + * Wrap an `AndroidBuild` with fallback physical partition detection logic. + * + * If the `AndroidBuild` does not already provide GPT entry information, this + * makes a best guess based on known logical partitions and image files. + * + * One use case is the android product directory or `m` case, where the build + * system produces a collection of `.img` files including a `super.img`, but no + * explicit list of physical partitions. + */ +Result> PhysicalPartitions(AndroidBuild&); + +} // namespace cuttlefish