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 
22 #include "cpio.h"
23 #include "lz4_legacy.h"
24 
25 using android::base::ErrnoError;
26 using android::base::Error;
27 using android::base::ReadFullyAtOffset;
28 using android::base::WriteStringToFd;
29 
30 namespace android {
31 
32 namespace {
33 // Extract ramdisk from boot image / partition at |boot_path|.
34 // Return a temporary file storing the ramdisk section.
ExtractRamdiskRaw(std::string_view boot_path)35 android::base::Result<std::unique_ptr<TemporaryFile>> ExtractRamdiskRaw(
36     std::string_view boot_path) {
37   android::base::unique_fd bootimg(
38       TEMP_FAILURE_RETRY(open(boot_path.data(), O_RDONLY)));
39   if (!bootimg.ok()) return ErrnoError() << "open(" << boot_path << ")";
40   boot_img_hdr_v3 hdr;
41   if (!ReadFullyAtOffset(bootimg.get(), &hdr, sizeof(hdr), 0))
42     return ErrnoError() << "read header";
43   if (0 != memcmp(hdr.magic, BOOT_MAGIC, BOOT_MAGIC_SIZE))
44     return Error() << "Boot magic mismatch";
45 
46   if (hdr.header_version < 3)
47     return Error() << "Unsupported header version V" << hdr.header_version;
48 
49   // See bootimg.h
50   auto kernel_size_bytes = (hdr.kernel_size + 4096 - 1) / 4096 * 4096;
51   auto ramdisk_offset = 4096 + kernel_size_bytes;
52 
53   std::string ramdisk_content(hdr.ramdisk_size, '\0');
54   if (!ReadFullyAtOffset(bootimg.get(), ramdisk_content.data(),
55                          hdr.ramdisk_size, ramdisk_offset))
56     return ErrnoError() << "read ramdisk section";
57 
58   auto ramdisk_content_file = std::make_unique<TemporaryFile>();
59   if (!WriteStringToFd(ramdisk_content, ramdisk_content_file->fd))
60     return ErrnoError() << "write ramdisk section to file";
61 
62   return ramdisk_content_file;
63 }
64 
65 }  // namespace
66 
67 // From the boot image / partition, extract the ramdisk section, decompress it,
68 // and extract from the cpio archive.
ExtractRamdiskToDirectory(std::string_view boot_path)69 android::base::Result<std::unique_ptr<TemporaryDir>> ExtractRamdiskToDirectory(
70     std::string_view boot_path) {
71   auto raw_ramdisk_file = ExtractRamdiskRaw(boot_path);
72   if (!raw_ramdisk_file.ok()) return raw_ramdisk_file.error();
73 
74   TemporaryFile decompressed;
75   auto decompress_res = android::Lz4DecompressLegacy((*raw_ramdisk_file)->path,
76                                                      decompressed.path);
77   if (!decompress_res.ok()) return decompress_res.error();
78 
79   return android::CpioExtract(decompressed.path);
80 }
81 
82 }  // namespace android
83