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 <fruit/fruit.h>
22 
23 #include "common/libs/utils/files.h"
24 #include "common/libs/utils/subprocess.h"
25 #include "host/commands/assemble_cvd/boot_config.h"
26 #include "host/commands/assemble_cvd/boot_image_utils.h"
27 #include "host/libs/config/known_paths.h"
28 
29 namespace cuttlefish {
30 
31 using APBootFlow = CuttlefishConfig::InstanceSpecific::APBootFlow;
32 
PrepareVBMetaImage(const std::string & path,bool has_boot_config)33 static bool PrepareVBMetaImage(const std::string& path, bool has_boot_config) {
34   std::unique_ptr<Avb> avbtool = GetDefaultAvb();
35   std::vector<ChainPartition> chained_partitions = {ChainPartition{
36       .name = "uboot_env",
37       .rollback_index = "1",
38       .key_path = TestPubKeyRsa4096(),
39   }};
40   if (has_boot_config) {
41     chained_partitions.emplace_back(ChainPartition{
42         .name = "bootconfig",
43         .rollback_index = "2",
44         .key_path = TestPubKeyRsa4096(),
45     });
46   }
47   Result<void> result =
48       avbtool->MakeVbMetaImage(path, chained_partitions, {}, {});
49   if (!result.ok()) {
50     LOG(ERROR) << result.error().Trace();
51     return false;
52   }
53   return true;
54 }
55 
GeneratePersistentVbmeta(const CuttlefishConfig::InstanceSpecific & instance,AutoSetup<InitBootloaderEnvPartition>::Type &,AutoSetup<GeneratePersistentBootconfig>::Type &)56 Result<void> GeneratePersistentVbmeta(
57     const CuttlefishConfig::InstanceSpecific& instance,
58     AutoSetup<InitBootloaderEnvPartition>::Type& /* dependency */,
59     AutoSetup<GeneratePersistentBootconfig>::Type& /* dependency */) {
60   if (!instance.protected_vm()) {
61     CF_EXPECT(PrepareVBMetaImage(instance.vbmeta_path(),
62                                  instance.bootconfig_supported()));
63   }
64   if (instance.ap_boot_flow() == APBootFlow::Grub) {
65     CF_EXPECT(PrepareVBMetaImage(instance.ap_vbmeta_path(), false));
66   }
67   return {};
68 }
69 
70 }  // namespace cuttlefish
71