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 <android-base/file.h>
20
21 #include "common/libs/utils/files.h"
22 #include "host/commands/assemble_cvd/boot_image_utils.h"
23 #include "host/libs/config/feature.h"
24
25 namespace cuttlefish {
26
Gem5ImageUnpacker(const CuttlefishConfig & config,AutoSetup<RepackKernelRamdisk>::Type &)27 Result<void> Gem5ImageUnpacker(
28 const CuttlefishConfig& config,
29 AutoSetup<RepackKernelRamdisk>::Type& /* dependency */) {
30 if (config.vm_manager() != VmmMode::kGem5) {
31 return {};
32 }
33 // TODO: b/281130788 - This should accept InstanceSpecific as an argument
34 const CuttlefishConfig::InstanceSpecific& instance_ =
35 config.ForDefaultInstance();
36
37 /* Unpack the original or repacked boot and vendor boot ramdisks, so that
38 * we have access to the baked bootconfig and raw compressed ramdisks.
39 * This allows us to emulate what a bootloader would normally do, which
40 * Gem5 can't support itself. This code also copies the kernel again
41 * (because Gem5 only supports raw vmlinux) and handles the bootloader
42 * binaries specially. This code is just part of the solution; it only
43 * does the parts which are instance agnostic.
44 */
45
46 CF_EXPECT(FileHasContent(instance_.boot_image()), instance_.boot_image());
47
48 const std::string unpack_dir = config.assembly_dir();
49 // The init_boot partition is be optional for testing boot.img
50 // with the ramdisk inside.
51 if (!FileHasContent(instance_.init_boot_image())) {
52 LOG(WARNING) << "File not found: " << instance_.init_boot_image();
53 } else {
54 CF_EXPECT(UnpackBootImage(instance_.init_boot_image(), unpack_dir),
55 "Failed to extract the init boot image");
56 }
57
58 CF_EXPECT(FileHasContent(instance_.vendor_boot_image()),
59 instance_.vendor_boot_image());
60
61 CF_EXPECT(UnpackVendorBootImageIfNotUnpacked(instance_.vendor_boot_image(),
62 unpack_dir),
63 "Failed to extract the vendor boot image");
64
65 // Assume the user specified a kernel manually which is a vmlinux
66 CF_EXPECT(cuttlefish::Copy(instance_.kernel_path(), unpack_dir + "/kernel"));
67
68 // Gem5 needs the bootloader binary to be a specific directory structure
69 // to find it. Create a 'binaries' directory and copy it into there
70 const std::string binaries_dir = unpack_dir + "/binaries";
71 CF_EXPECT(
72 mkdir(binaries_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0 ||
73 errno == EEXIST,
74 "\"" << binaries_dir << "\": " << strerror(errno));
75 CF_EXPECT(Copy(
76 instance_.bootloader(),
77 binaries_dir + "/" + android::base::Basename(instance_.bootloader())));
78
79 // Gem5 also needs the ARM version of the bootloader, even though it
80 // doesn't use it. It'll even open it to check it's a valid ELF file.
81 // Work around this by copying such a named file from the same directory
82 CF_EXPECT(Copy(android::base::Dirname(instance_.bootloader()) + "/boot.arm",
83 binaries_dir + "/boot.arm"));
84
85 return {};
86 }
87
88 } // namespace cuttlefish
89