| Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2016 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 "update_engine/payload_generator/xz.h" | 
|  | 18 |  | 
| Sen Jiang | 5288bd1 | 2018-05-03 11:39:04 -0700 | [diff] [blame] | 19 | #include <elf.h> | 
|  | 20 | #include <endian.h> | 
| Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 21 |  | 
|  | 22 | #include <algorithm> | 
|  | 23 |  | 
| Sen Jiang | 5288bd1 | 2018-05-03 11:39:04 -0700 | [diff] [blame] | 24 | #include <7zCrc.h> | 
|  | 25 | #include <Xz.h> | 
|  | 26 | #include <XzEnc.h> | 
| Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 27 | #include <base/logging.h> | 
|  | 28 |  | 
|  | 29 | namespace { | 
|  | 30 |  | 
|  | 31 | bool xz_initialized = false; | 
|  | 32 |  | 
|  | 33 | // An ISeqInStream implementation that reads all the data from the passed Blob. | 
|  | 34 | struct BlobReaderStream : public ISeqInStream { | 
|  | 35 | explicit BlobReaderStream(const brillo::Blob& data) : data_(data) { | 
|  | 36 | Read = &BlobReaderStream::ReadStatic; | 
|  | 37 | } | 
|  | 38 |  | 
| Sen Jiang | 654ce96 | 2018-05-03 13:42:06 -0700 | [diff] [blame] | 39 | static SRes ReadStatic(const ISeqInStream* p, void* buf, size_t* size) { | 
|  | 40 | auto* self = static_cast<BlobReaderStream*>(const_cast<ISeqInStream*>(p)); | 
| Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 41 | *size = std::min(*size, self->data_.size() - self->pos_); | 
|  | 42 | memcpy(buf, self->data_.data() + self->pos_, *size); | 
|  | 43 | self->pos_ += *size; | 
|  | 44 | return SZ_OK; | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | const brillo::Blob& data_; | 
|  | 48 |  | 
|  | 49 | // The current reader position. | 
|  | 50 | size_t pos_ = 0; | 
|  | 51 | }; | 
|  | 52 |  | 
|  | 53 | // An ISeqOutStream implementation that writes all the data to the passed Blob. | 
|  | 54 | struct BlobWriterStream : public ISeqOutStream { | 
|  | 55 | explicit BlobWriterStream(brillo::Blob* data) : data_(data) { | 
|  | 56 | Write = &BlobWriterStream::WriteStatic; | 
|  | 57 | } | 
|  | 58 |  | 
| Sen Jiang | 654ce96 | 2018-05-03 13:42:06 -0700 | [diff] [blame] | 59 | static size_t WriteStatic(const ISeqOutStream* p, | 
|  | 60 | const void* buf, | 
|  | 61 | size_t size) { | 
|  | 62 | auto* self = static_cast<const BlobWriterStream*>(p); | 
| Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 63 | const uint8_t* buffer = reinterpret_cast<const uint8_t*>(buf); | 
| Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 64 | self->data_->insert(self->data_->end(), buffer, buffer + size); | 
|  | 65 | return size; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | brillo::Blob* data_; | 
|  | 69 | }; | 
|  | 70 |  | 
| Sen Jiang | 5288bd1 | 2018-05-03 11:39:04 -0700 | [diff] [blame] | 71 | // Returns the filter id to be used to compress |data|. | 
|  | 72 | // Only BCJ filter for x86 and ARM ELF file are supported, returns 0 otherwise. | 
|  | 73 | int GetFilterID(const brillo::Blob& data) { | 
|  | 74 | if (data.size() < sizeof(Elf32_Ehdr) || | 
|  | 75 | memcmp(data.data(), ELFMAG, SELFMAG) != 0) | 
|  | 76 | return 0; | 
|  | 77 |  | 
|  | 78 | const Elf32_Ehdr* header = reinterpret_cast<const Elf32_Ehdr*>(data.data()); | 
|  | 79 |  | 
|  | 80 | // Only little-endian is supported. | 
|  | 81 | if (header->e_ident[EI_DATA] != ELFDATA2LSB) | 
|  | 82 | return 0; | 
|  | 83 |  | 
|  | 84 | switch (le16toh(header->e_machine)) { | 
|  | 85 | case EM_386: | 
|  | 86 | case EM_X86_64: | 
|  | 87 | return XZ_ID_X86; | 
|  | 88 | case EM_ARM: | 
|  | 89 | // Both ARM and ARM Thumb instructions could be found in the same ARM ELF | 
|  | 90 | // file. We choose to use the ARM Thumb filter here because testing shows | 
|  | 91 | // that it usually works better than the ARM filter. | 
|  | 92 | return XZ_ID_ARMT; | 
|  | 93 | #ifdef EM_AARCH64 | 
|  | 94 | case EM_AARCH64: | 
|  | 95 | // Neither the ARM nor the ARM Thumb filter works well with AArch64. | 
|  | 96 | return 0; | 
|  | 97 | #endif | 
|  | 98 | } | 
|  | 99 | return 0; | 
|  | 100 | } | 
|  | 101 |  | 
| Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 102 | }  // namespace | 
|  | 103 |  | 
|  | 104 | namespace chromeos_update_engine { | 
|  | 105 |  | 
|  | 106 | void XzCompressInit() { | 
|  | 107 | if (xz_initialized) | 
|  | 108 | return; | 
|  | 109 | xz_initialized = true; | 
|  | 110 | // Although we don't include a CRC32 for the stream, the xz file header has | 
|  | 111 | // a CRC32 of the header itself, which required the CRC table to be | 
|  | 112 | // initialized. | 
|  | 113 | CrcGenerateTable(); | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | bool XzCompress(const brillo::Blob& in, brillo::Blob* out) { | 
|  | 117 | CHECK(xz_initialized) << "Initialize XzCompress first"; | 
|  | 118 | out->clear(); | 
|  | 119 | if (in.empty()) | 
|  | 120 | return true; | 
|  | 121 |  | 
|  | 122 | // Xz compression properties. | 
|  | 123 | CXzProps props; | 
|  | 124 | XzProps_Init(&props); | 
|  | 125 | // No checksum in the xz stream. xz-embedded (used by the decompressor) only | 
|  | 126 | // supports CRC32, but we already check the sha-1 of the whole blob during | 
|  | 127 | // payload application. | 
|  | 128 | props.checkId = XZ_CHECK_NO; | 
|  | 129 |  | 
|  | 130 | // LZMA2 compression properties. | 
|  | 131 | CLzma2EncProps lzma2Props; | 
| Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 132 | Lzma2EncProps_Init(&lzma2Props); | 
|  | 133 | // LZMA compression "level 6" requires 9 MB of RAM to decompress in the worst | 
|  | 134 | // case. | 
|  | 135 | lzma2Props.lzmaProps.level = 6; | 
|  | 136 | lzma2Props.lzmaProps.numThreads = 1; | 
|  | 137 | // The input size data is used to reduce the dictionary size if possible. | 
|  | 138 | lzma2Props.lzmaProps.reduceSize = in.size(); | 
|  | 139 | Lzma2EncProps_Normalize(&lzma2Props); | 
| Sen Jiang | 654ce96 | 2018-05-03 13:42:06 -0700 | [diff] [blame] | 140 | props.lzma2Props = lzma2Props; | 
| Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 141 |  | 
| Sen Jiang | 5288bd1 | 2018-05-03 11:39:04 -0700 | [diff] [blame] | 142 | props.filterProps.id = GetFilterID(in); | 
|  | 143 |  | 
| Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 144 | BlobWriterStream out_writer(out); | 
|  | 145 | BlobReaderStream in_reader(in); | 
|  | 146 | SRes res = Xz_Encode(&out_writer, &in_reader, &props, nullptr /* progress */); | 
|  | 147 | return res == SZ_OK; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | }  // namespace chromeos_update_engine |