1 /*
2  * Copyright (C) 2018 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/boot_image_unpacker.h"
18 
19 #include <string.h>
20 #include <unistd.h>
21 
22 #include <sstream>
23 
24 #include <bootimg.h>
25 #include <glog/logging.h>
26 
27 #include "common/libs/utils/subprocess.h"
28 
29 namespace cvd {
30 
31 namespace {
32 
33 // Extracts size bytes from file, starting at offset bytes from the beginning to
34 // path.
ExtractFile(SharedFD source,off_t offset,size_t size,const std::string & path)35 bool ExtractFile(SharedFD source, off_t offset, size_t size,
36                  const std::string& path) {
37   auto dest = SharedFD::Open(path.c_str(), O_CREAT | O_RDWR, 0755);
38   if (!dest->IsOpen()) {
39     LOG(ERROR) << "Unable to open " << path;
40     return false;
41   }
42   auto off = source->LSeek(offset, SEEK_SET);
43   if (off != offset) {
44     LOG(ERROR) << "Failed to lseek: " << source->StrError();
45     return false;
46   }
47   return dest->CopyFrom(*source, size);
48 }
49 }  // namespace
50 
FromImages(const std::string & boot_image_path,const std::string & vendor_boot_image_path)51 std::unique_ptr<BootImageUnpacker> BootImageUnpacker::FromImages(
52     const std::string& boot_image_path,
53     const std::string& vendor_boot_image_path) {
54   auto boot_img = SharedFD::Open(boot_image_path.c_str(), O_RDONLY);
55   if (!boot_img->IsOpen()) {
56     LOG(ERROR) << "Unable to open boot image (" << boot_image_path
57                << "): " << boot_img->StrError();
58     return nullptr;
59   }
60   boot_img_hdr_v3 header;
61   auto bytes_read = boot_img->Read(&header, sizeof(header));
62   if (bytes_read != sizeof(header)) {
63     LOG(ERROR) << "Error reading boot image header";
64     return nullptr;
65   }
66 
67   auto vendor_boot_img = SharedFD::Open(vendor_boot_image_path.c_str(),
68                                         O_RDONLY);
69   if (!vendor_boot_img->IsOpen()) {
70     LOG(ERROR) << "Unable to open vendor boot image (" << vendor_boot_image_path
71                << "): " << vendor_boot_img->StrError();
72     return nullptr;
73   }
74   vendor_boot_img_hdr_v3 vboot_header;
75   bytes_read = vendor_boot_img->Read(&vboot_header, sizeof(vboot_header));
76   if (bytes_read != sizeof(vboot_header)) {
77     LOG(ERROR) << "Error reading vendor boot image header";
78     return nullptr;
79   }
80 
81   std::ostringstream cmdline;
82   cmdline << reinterpret_cast<char*>(&header.cmdline[0]);
83   if (vboot_header.cmdline[0] != '\0') {
84     cmdline << " ";
85     cmdline << reinterpret_cast<char*>(&vboot_header.cmdline[0]);
86   }
87 
88   uint32_t page_size = 4096;
89   // See system/tools/mkbootimg/include/bootimg/bootimg.h for the origin of
90   // these offset calculations
91   uint32_t kernel_offset = page_size;
92   uint32_t ramdisk_offset =
93       kernel_offset +
94       ((header.kernel_size + page_size - 1) / page_size) * page_size;
95   uint32_t vendor_ramdisk_offset =
96       ((vboot_header.header_size + vboot_header.page_size - 1) / vboot_header.page_size) *
97       vboot_header.page_size;
98 
99   std::unique_ptr<BootImageUnpacker> ret(new BootImageUnpacker(
100       boot_img, cmdline.str(), header.kernel_size, kernel_offset,
101       header.ramdisk_size, ramdisk_offset, vendor_boot_img,
102       vboot_header.vendor_ramdisk_size, vendor_ramdisk_offset));
103 
104   return ret;
105 }
106 
kernel_cmdline() const107 std::string BootImageUnpacker::kernel_cmdline() const {
108   return kernel_cmdline_;
109 }
110 
ExtractKernelImage(const std::string & path) const111 bool BootImageUnpacker::ExtractKernelImage(const std::string& path) const {
112   if (kernel_image_size_ == 0) return false;
113   return ExtractFile(boot_image_, kernel_image_offset_, kernel_image_size_,
114                      path);
115 }
ExtractRamdiskImage(const std::string & path) const116 bool BootImageUnpacker::ExtractRamdiskImage(const std::string& path) const {
117   if (ramdisk_image_size_ == 0) return false;
118   return ExtractFile(boot_image_, ramdisk_image_offset_, ramdisk_image_size_,
119                      path);
120 }
ExtractVendorRamdiskImage(const std::string & path) const121 bool BootImageUnpacker::ExtractVendorRamdiskImage(const std::string& path) const {
122   if (vendor_ramdisk_image_size_ == 0) return false;
123   return ExtractFile(vendor_boot_image_, vendor_ramdisk_image_offset_,
124                      vendor_ramdisk_image_size_, path);
125 }
126 
Unpack(const std::string & ramdisk_image_path,const std::string & vendor_ramdisk_image_path,const std::string & kernel_image_path)127 bool BootImageUnpacker::Unpack(const std::string& ramdisk_image_path,
128                                const std::string& vendor_ramdisk_image_path,
129                                const std::string& kernel_image_path) {
130   if (HasRamdiskImage()) {
131     if (!ExtractRamdiskImage(ramdisk_image_path)) {
132       LOG(ERROR) << "Error extracting ramdisk from boot image";
133       return false;
134     }
135   }
136   if (HasVendorRamdiskImage()) {
137     if (!ExtractVendorRamdiskImage(vendor_ramdisk_image_path)) {
138       LOG(ERROR) << "Error extracting vendor ramdisk from venodr boot image";
139       return false;
140     }
141   }
142   if (!kernel_image_path.empty()) {
143     if (HasKernelImage()) {
144       if (!ExtractKernelImage(kernel_image_path)) {
145         LOG(ERROR) << "Error extracting kernel from boot image";
146         return false;
147       }
148     } else {
149       LOG(ERROR) << "No kernel found on boot image";
150       return false;
151     }
152   }
153   return true;
154 }
155 
156 }  // namespace cvd
157