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 <memory>
20 #include <string>
21 #include <unordered_set>
22 
23 #include "common/libs/fs/shared_buf.h"
24 #include "common/libs/fs/shared_fd.h"
25 #include "common/libs/utils/files.h"
26 #include "common/libs/utils/result.h"
27 #include "common/libs/utils/size_utils.h"
28 #include "host/commands/assemble_cvd/bootconfig_args.h"
29 #include "host/libs/avb/avb.h"
30 #include "host/libs/config/cuttlefish_config.h"
31 #include "host/libs/config/data_image.h"
32 #include "host/libs/config/feature.h"
33 #include "host/libs/config/known_paths.h"
34 #include "host/libs/vm_manager/gem5_manager.h"
35 
36 namespace cuttlefish {
37 
GeneratePersistentBootconfig(const CuttlefishConfig & config,const CuttlefishConfig::InstanceSpecific & instance)38 Result<void> GeneratePersistentBootconfig(
39     const CuttlefishConfig& config,
40     const CuttlefishConfig::InstanceSpecific& instance) {
41   if (instance.protected_vm()) {
42     return {};
43   }
44   //  Cuttlefish for the time being won't be able to support OTA from a
45   //  non-bootconfig kernel to a bootconfig-kernel (or vice versa) IF the
46   //  device is stopped (via stop_cvd). This is rarely an issue since OTA
47   //  testing run on cuttlefish is done within one launch cycle of the device.
48   //  If this ever becomes an issue, this code will have to be rewritten.
49   if (!instance.bootconfig_supported()) {
50     return {};
51   }
52   const auto bootconfig_path = instance.persistent_bootconfig_path();
53   if (!FileExists(bootconfig_path)) {
54     CF_EXPECT(CreateBlankImage(bootconfig_path, 1 /* mb */, "none"),
55               "Failed to create image at " << bootconfig_path);
56   }
57 
58   auto bootconfig_fd = SharedFD::Open(bootconfig_path, O_RDWR);
59   CF_EXPECT(bootconfig_fd->IsOpen(),
60             "Unable to open bootconfig file: " << bootconfig_fd->StrError());
61 
62   const auto bootconfig_args =
63       CF_EXPECT(BootconfigArgsFromConfig(config, instance));
64   const auto bootconfig =
65       CF_EXPECT(BootconfigArgsString(bootconfig_args, "\n")) + "\n";
66 
67   LOG(DEBUG) << "bootconfig size is " << bootconfig.size();
68   ssize_t bytesWritten = WriteAll(bootconfig_fd, bootconfig);
69   CF_EXPECT(WriteAll(bootconfig_fd, bootconfig) == bootconfig.size(),
70             "Failed to write bootconfig to \"" << bootconfig_path << "\"");
71   LOG(DEBUG) << "Bootconfig parameters from vendor boot image and config are "
72              << ReadFile(bootconfig_path);
73 
74   CF_EXPECT(bootconfig_fd->Truncate(bootconfig.size()) == 0,
75             "`truncate --size=" << bootconfig.size() << " bytes "
76                                 << bootconfig_path
77                                 << "` failed:" << bootconfig_fd->StrError());
78 
79   if (config.vm_manager() == VmmMode::kGem5) {
80     const off_t bootconfig_size_bytes_gem5 =
81         AlignToPowerOf2(bytesWritten, PARTITION_SIZE_SHIFT);
82     CF_EXPECT(bootconfig_fd->Truncate(bootconfig_size_bytes_gem5) == 0);
83     bootconfig_fd->Close();
84   } else {
85     bootconfig_fd->Close();
86     const off_t bootconfig_size_bytes = AlignToPowerOf2(
87         kMaxAvbMetadataSize + bootconfig.size(), PARTITION_SIZE_SHIFT);
88 
89     std::unique_ptr<Avb> avbtool = GetDefaultAvb();
90     CF_EXPECT(avbtool->AddHashFooter(bootconfig_path, "bootconfig",
91                                      bootconfig_size_bytes));
92   }
93   return {};
94 }
95 
96 }  // namespace cuttlefish
97