1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "host/commands/assemble_cvd/disk/disk.h"
18 
19 #include <string>
20 
21 #include "common/libs/utils/files.h"
22 #include "common/libs/utils/subprocess.h"
23 #include "host/commands/assemble_cvd/boot_config.h"
24 #include "host/commands/assemble_cvd/boot_image_utils.h"
25 #include "host/libs/config/known_paths.h"
26 
27 namespace cuttlefish {
28 
29 using APBootFlow = CuttlefishConfig::InstanceSpecific::APBootFlow;
30 
PrepareVBMetaImage(const std::string & path,bool has_boot_config)31 static bool PrepareVBMetaImage(const std::string& path, bool has_boot_config) {
32   std::unique_ptr<Avb> avbtool = GetDefaultAvb();
33   std::vector<ChainPartition> chained_partitions = {ChainPartition{
34       .name = "uboot_env",
35       .rollback_index = "1",
36       .key_path = TestPubKeyRsa4096(),
37   }};
38   if (has_boot_config) {
39     chained_partitions.emplace_back(ChainPartition{
40         .name = "bootconfig",
41         .rollback_index = "2",
42         .key_path = TestPubKeyRsa4096(),
43     });
44   }
45   Result<void> result =
46       avbtool->MakeVbMetaImage(path, chained_partitions, {}, {});
47   if (!result.ok()) {
48     LOG(ERROR) << result.error().Trace();
49     return false;
50   }
51   return true;
52 }
53 
GeneratePersistentVbmeta(const CuttlefishConfig::InstanceSpecific & instance,AutoSetup<InitBootloaderEnvPartition>::Type &,AutoSetup<GeneratePersistentBootconfig>::Type &)54 Result<void> GeneratePersistentVbmeta(
55     const CuttlefishConfig::InstanceSpecific& instance,
56     AutoSetup<InitBootloaderEnvPartition>::Type& /* dependency */,
57     AutoSetup<GeneratePersistentBootconfig>::Type& /* dependency */) {
58   if (!instance.protected_vm()) {
59     CF_EXPECT(PrepareVBMetaImage(instance.vbmeta_path(),
60                                  instance.bootconfig_supported()));
61   }
62   if (instance.ap_boot_flow() == APBootFlow::Grub) {
63     CF_EXPECT(PrepareVBMetaImage(instance.ap_vbmeta_path(), false));
64   }
65   return {};
66 }
67 
68 }  // namespace cuttlefish
69