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