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
19 #include <elf.h>
20 #include <endian.h>
21
22 #include <algorithm>
23
24 #include <7zCrc.h>
25 #include <Xz.h>
26 #include <XzEnc.h>
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 {
BlobReaderStream__anonc5e10ba60111::BlobReaderStream35 explicit BlobReaderStream(const brillo::Blob& data) : data_(data) {
36 Read = &BlobReaderStream::ReadStatic;
37 }
38
ReadStatic__anonc5e10ba60111::BlobReaderStream39 static SRes ReadStatic(const ISeqInStream* p, void* buf, size_t* size) {
40 auto* self = static_cast<BlobReaderStream*>(const_cast<ISeqInStream*>(p));
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 {
BlobWriterStream__anonc5e10ba60111::BlobWriterStream55 explicit BlobWriterStream(brillo::Blob* data) : data_(data) {
56 Write = &BlobWriterStream::WriteStatic;
57 }
58
WriteStatic__anonc5e10ba60111::BlobWriterStream59 static size_t WriteStatic(const ISeqOutStream* p,
60 const void* buf,
61 size_t size) {
62 auto* self = static_cast<const BlobWriterStream*>(p);
63 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(buf);
64 self->data_->reserve(self->data_->size() + size);
65 self->data_->insert(self->data_->end(), buffer, buffer + size);
66 return size;
67 }
68
69 brillo::Blob* data_;
70 };
71
72 // Returns the filter id to be used to compress |data|.
73 // Only BCJ filter for x86 and ARM ELF file are supported, returns 0 otherwise.
GetFilterID(const brillo::Blob & data)74 int GetFilterID(const brillo::Blob& data) {
75 if (data.size() < sizeof(Elf32_Ehdr) ||
76 memcmp(data.data(), ELFMAG, SELFMAG) != 0)
77 return 0;
78
79 const Elf32_Ehdr* header = reinterpret_cast<const Elf32_Ehdr*>(data.data());
80
81 // Only little-endian is supported.
82 if (header->e_ident[EI_DATA] != ELFDATA2LSB)
83 return 0;
84
85 switch (le16toh(header->e_machine)) {
86 case EM_386:
87 case EM_X86_64:
88 return XZ_ID_X86;
89 case EM_ARM:
90 // Both ARM and ARM Thumb instructions could be found in the same ARM ELF
91 // file. We choose to use the ARM Thumb filter here because testing shows
92 // that it usually works better than the ARM filter.
93 return XZ_ID_ARMT;
94 #ifdef EM_AARCH64
95 case EM_AARCH64:
96 // Neither the ARM nor the ARM Thumb filter works well with AArch64.
97 return 0;
98 #endif
99 }
100 return 0;
101 }
102
103 } // namespace
104
105 namespace chromeos_update_engine {
106
XzCompressInit()107 void XzCompressInit() {
108 if (xz_initialized)
109 return;
110 xz_initialized = true;
111 // Although we don't include a CRC32 for the stream, the xz file header has
112 // a CRC32 of the header itself, which required the CRC table to be
113 // initialized.
114 CrcGenerateTable();
115 }
116
XzCompress(const brillo::Blob & in,brillo::Blob * out)117 bool XzCompress(const brillo::Blob& in, brillo::Blob* out) {
118 CHECK(xz_initialized) << "Initialize XzCompress first";
119 out->clear();
120 if (in.empty())
121 return true;
122
123 // Xz compression properties.
124 CXzProps props;
125 XzProps_Init(&props);
126 // No checksum in the xz stream. xz-embedded (used by the decompressor) only
127 // supports CRC32, but we already check the sha-1 of the whole blob during
128 // payload application.
129 props.checkId = XZ_CHECK_NO;
130
131 // LZMA2 compression properties.
132 CLzma2EncProps lzma2Props;
133 Lzma2EncProps_Init(&lzma2Props);
134 // LZMA compression "level 6" requires 9 MB of RAM to decompress in the worst
135 // case.
136 lzma2Props.lzmaProps.level = 6;
137 lzma2Props.lzmaProps.numThreads = 1;
138 // The input size data is used to reduce the dictionary size if possible.
139 lzma2Props.lzmaProps.reduceSize = in.size();
140 Lzma2EncProps_Normalize(&lzma2Props);
141 props.lzma2Props = lzma2Props;
142
143 props.filterProps.id = GetFilterID(in);
144
145 BlobWriterStream out_writer(out);
146 BlobReaderStream in_reader(in);
147 SRes res = Xz_Encode(&out_writer, &in_reader, &props, nullptr /* progress */);
148 return res == SZ_OK;
149 }
150
151 } // namespace chromeos_update_engine
152