• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
17 
18 #include <stdint.h>
19 
20 #include <memory>
21 #include <string>
22 
23 #include "common/libs/fs/shared_fd.h"
24 
25 namespace cvd {
26 
27 // Unpacks the boot image and extracts kernel, ramdisk and kernel arguments
28 class BootImageUnpacker {
29  public:
30   // Reads header section of boot image at path and returns a BootImageUnpacker
31   // preloaded with all the metadata.
32   static std::unique_ptr<BootImageUnpacker> FromImage(const std::string& path);
33 
34   ~BootImageUnpacker() = default;
35 
36   std::string kernel_cmdline() const;
37 
HasKernelImage()38   bool HasKernelImage() const { return kernel_image_size_ > 0; }
HasRamdiskImage()39   bool HasRamdiskImage() const { return ramdisk_image_size_ > 0; }
40 
41   // Extracts the kernel image to the given path
42   bool ExtractKernelImage(const std::string& path) const;
43   // Extracts the ramdisk image to the given path. It may return false if the
44   // boot image does not contain a ramdisk, which is the case when having system
45   // as root.
46   bool ExtractRamdiskImage(const std::string& path) const;
47 
48   bool Unpack(const std::string& ramdisk_image_path,
49               const std::string& kernel_image_path);
50 
51  private:
BootImageUnpacker(SharedFD boot_image,const std::string & cmdline,uint32_t kernel_image_size,uint32_t kernel_image_offset,uint32_t ramdisk_image_size,uint32_t ramdisk_image_offset)52   BootImageUnpacker(SharedFD boot_image, const std::string& cmdline,
53                     uint32_t kernel_image_size, uint32_t kernel_image_offset,
54                     uint32_t ramdisk_image_size, uint32_t ramdisk_image_offset)
55       : boot_image_(boot_image),
56         kernel_cmdline_(cmdline),
57         kernel_image_size_(kernel_image_size),
58         kernel_image_offset_(kernel_image_offset),
59         ramdisk_image_size_(ramdisk_image_size),
60         ramdisk_image_offset_(ramdisk_image_offset) {}
61 
62   // Mutable because we only read from the fd, but do not modify its contents
63   mutable SharedFD boot_image_;
64   std::string kernel_cmdline_;
65   // When buidling the boot image a particular page size is assumed, which may
66   // not match the actual page size of the system.
67   uint32_t kernel_image_size_;
68   uint32_t kernel_image_offset_;
69   uint32_t ramdisk_image_size_;
70   uint32_t ramdisk_image_offset_;
71 };
72 
73 }  // namespace cvd
74