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(boot_path.data(), 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 fsync(ramdisk_content_file->fd);
67
68 return ramdisk_content_file;
69 }
70
71 } // namespace
72
73 // From the boot image / partition, extract the ramdisk section, decompress it,
74 // and extract from the cpio archive.
ExtractRamdiskToDirectory(std::string_view boot_path)75 android::base::Result<std::unique_ptr<TemporaryDir>> ExtractRamdiskToDirectory(
76 std::string_view boot_path) {
77 auto raw_ramdisk_file = ExtractRamdiskRaw(boot_path);
78 if (!raw_ramdisk_file.ok()) return raw_ramdisk_file.error();
79
80 TemporaryFile decompressed;
81 auto decompress_res = android::Lz4DecompressLegacy((*raw_ramdisk_file)->path,
82 decompressed.path);
83 if (!decompress_res.ok()) return decompress_res.error();
84
85 return android::CpioExtract(decompressed.path);
86 }
87
88 } // namespace android
89