1 /*
2 * Copyright (C) 2021 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 #include "ramdisk_utils.h"
17
18 #include <android-base/file.h>
19 #include <android-base/result.h>
20 #include <bootimg.h>
21 #include <iostream>
22
23 #include "cpio.h"
24 #include "lz4_legacy.h"
25
26 using android::base::ErrnoError;
27 using android::base::Error;
28 using android::base::ReadFullyAtOffset;
29 using android::base::WriteStringToFd;
30
31 namespace android {
32
33 namespace {
34 // Extract ramdisk from boot image / partition at |boot_path|.
35 // Return a temporary file storing the ramdisk section.
ExtractRamdiskRaw(std::string_view boot_path)36 android::base::Result<std::unique_ptr<TemporaryFile>> ExtractRamdiskRaw(
37 std::string_view boot_path) {
38 android::base::unique_fd bootimg(
39 TEMP_FAILURE_RETRY(open(std::string(boot_path).c_str(), O_RDONLY)));
40 if (!bootimg.ok()) return ErrnoError() << "open(" << boot_path << ")";
41 boot_img_hdr_v3 hdr{};
42 if (!ReadFullyAtOffset(bootimg.get(), &hdr, sizeof(hdr), 0))
43 return ErrnoError() << "read header";
44 if (0 != memcmp(hdr.magic, BOOT_MAGIC, BOOT_MAGIC_SIZE))
45 return Error() << "Boot magic mismatch";
46
47 if (hdr.header_version < 3)
48 return Error() << "Unsupported header version V" << hdr.header_version;
49 if (hdr.ramdisk_size <= 0) {
50 return Error() << boot_path
51 << " contains a valid bootimg header but no ramdisk";
52 }
53
54 // See bootimg.h
55 auto kernel_size_bytes = (hdr.kernel_size + 4096 - 1) / 4096 * 4096;
56 auto ramdisk_offset = 4096 + kernel_size_bytes;
57
58 std::string ramdisk_content(hdr.ramdisk_size, '\0');
59 if (!ReadFullyAtOffset(bootimg.get(), ramdisk_content.data(),
60 hdr.ramdisk_size, ramdisk_offset))
61 return ErrnoError() << "read ramdisk section";
62
63 auto ramdisk_content_file = std::make_unique<TemporaryFile>();
64 if (!WriteStringToFd(ramdisk_content, ramdisk_content_file->fd))
65 return ErrnoError() << "write ramdisk section to file";
66 if (fsync(ramdisk_content_file->fd) != 0)
67 return ErrnoError() << "fsync ramdisk section file";
68
69 return ramdisk_content_file;
70 }
71
ExtractVendorRamdiskRaw(const std::string & vendor_boot_path)72 android::base::Result<std::unique_ptr<TemporaryFile>> ExtractVendorRamdiskRaw(
73 const std::string &vendor_boot_path) {
74 android::base::unique_fd bootimg(
75 TEMP_FAILURE_RETRY(open(vendor_boot_path.c_str(), O_RDONLY)));
76 if (!bootimg.ok()) return ErrnoError() << "open(" << vendor_boot_path << ")";
77 vendor_boot_img_hdr_v3 hdr{};
78 if (!ReadFullyAtOffset(bootimg.get(), &hdr, sizeof(hdr), 0))
79 return ErrnoError() << "read header";
80 if (0 != memcmp(hdr.magic, VENDOR_BOOT_MAGIC, VENDOR_BOOT_MAGIC_SIZE))
81 return Error() << "Boot magic mismatch";
82
83 if (hdr.header_version < 3)
84 return Error() << "Unsupported header version V" << hdr.header_version;
85
86 // See bootimg.h
87 const auto num_boot_header_pages =
88 (hdr.header_size + hdr.page_size - 1) / hdr.page_size;
89 const auto ramdisk_offset_base = hdr.page_size * num_boot_header_pages;
90
91 // Ignore the vendor ramdisk table and load the entire vendor ramdisk section.
92 // This has the same effect as does loading all of the vendor ramdisk
93 // fragments in the vendor_boot partition.
94 // https://source.android.com/docs/core/architecture/partitions/vendor-boot-partitions#vendor-boot-header
95 std::string vendor_ramdisk_content(hdr.vendor_ramdisk_size, '\0');
96 auto vendor_ramdisk_content_file = std::make_unique<TemporaryFile>();
97
98 if (!ReadFullyAtOffset(bootimg.get(), vendor_ramdisk_content.data(),
99 hdr.vendor_ramdisk_size, ramdisk_offset_base))
100 return ErrnoError() << "read ramdisk section";
101 if (!WriteStringToFd(vendor_ramdisk_content, vendor_ramdisk_content_file->fd))
102 return ErrnoError() << "write ramdisk section to file";
103 if (fsync(vendor_ramdisk_content_file->fd) != 0)
104 return ErrnoError() << "fsync ramdisk section file";
105 return vendor_ramdisk_content_file;
106 }
107
108 } // namespace
109
110 // From the boot image / partition, extract the ramdisk section, decompress it,
111 // and extract from the cpio archive.
ExtractRamdiskToDirectory(std::string_view boot_path)112 android::base::Result<std::unique_ptr<TemporaryDir>> ExtractRamdiskToDirectory(
113 std::string_view boot_path) {
114 const auto raw_ramdisk_file = ExtractRamdiskRaw(boot_path);
115 if (!raw_ramdisk_file.ok()) return raw_ramdisk_file.error();
116
117 TemporaryFile decompressed;
118 auto decompress_res = android::Lz4DecompressLegacy((*raw_ramdisk_file)->path,
119 decompressed.path);
120 if (!decompress_res.ok()) return decompress_res.error();
121
122 return android::CpioExtract(decompressed.path);
123 }
124
125 // From the vendor_boot image / partition, extract the vendor_ramdisk section,
126 // decompress it, and extract from the cpio archive.
127 android::base::Result<std::unique_ptr<TemporaryDir>>
ExtractVendorRamdiskToDirectory(const std::string & vendor_boot_path)128 ExtractVendorRamdiskToDirectory(const std::string &vendor_boot_path) {
129 const auto vendor_raw_ramdisk_file =
130 ExtractVendorRamdiskRaw(vendor_boot_path);
131 if (!vendor_raw_ramdisk_file.ok()) return vendor_raw_ramdisk_file.error();
132
133 TemporaryFile decompressed;
134 // TODO: b/374932907 -- Verify if this assumption is correct,
135 // if not add logic to support Gzip, or uncompressed ramdisks.
136 auto decompress_res = android::Lz4DecompressLegacy(
137 (*vendor_raw_ramdisk_file)->path, decompressed.path);
138 if (!decompress_res.ok()) return decompress_res.error();
139
140 return android::CpioExtract(decompressed.path);
141 }
142 } // namespace android
143