1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <getopt.h>
16 #include <string.h>
17 #include <sysexits.h>
18 #include <unistd.h>
19
20 #include <iostream>
21 #include <optional>
22
23 #include <android-base/file.h>
24 #include <android-base/logging.h>
25 #include <android-base/unique_fd.h>
26 #include <liblp/builder.h>
27 #include <sparse/sparse.h>
28
29 using android::base::borrowed_fd;
30 using android::base::unique_fd;
31 using android::fs_mgr::LpMetadata;
32 using android::fs_mgr::MetadataBuilder;
33 using android::fs_mgr::ReadMetadata;
34 using android::fs_mgr::UpdatePartitionTable;
35 using SparsePtr = std::unique_ptr<sparse_file, decltype(&sparse_file_destroy)>;
36
37 std::optional<TemporaryDir> gTempDir;
38
usage(const char * program)39 static int usage(const char* program) {
40 std::cerr << program << " - command-line tool for adding partitions to a super.img\n";
41 std::cerr << "\n";
42 std::cerr << "Usage:\n";
43 std::cerr << " " << program << " [options] SUPER PARTNAME PARTGROUP [IMAGE]\n";
44 std::cerr << "\n";
45 std::cerr << " SUPER Path to the super image. It can be sparsed or\n"
46 << " unsparsed. If sparsed, it will be unsparsed\n"
47 << " temporarily and re-sparsed over the original\n"
48 << " file. This will consume extra space during the\n"
49 << " execution of " << program << ".\n";
50 std::cerr << " PARTNAME Name of the partition to add.\n";
51 std::cerr << " PARTGROUP Name of the partition group to use. If the\n"
52 << " partition can be updated over OTA, the group\n"
53 << " should match its updatable group.\n";
54 std::cerr << " IMAGE If specified, the contents of the given image\n"
55 << " will be added to the super image. If the image\n"
56 << " is sparsed, it will be temporarily unsparsed.\n"
57 << " If no image is specified, the partition will\n"
58 << " be zero-sized.\n";
59 std::cerr << "\n";
60 std::cerr << "Extra options:\n";
61 std::cerr << " --readonly The partition should be mapped read-only.\n";
62 std::cerr << " --replace The partition contents should be replaced with\n"
63 << " the input image.\n";
64 std::cerr << "\n";
65 return EX_USAGE;
66 }
67
68 enum class OptionCode : int {
69 kReadonly = 1,
70 kReplace = 2,
71
72 // Special options.
73 kHelp = (int)'h',
74 };
75
GetTemporaryDir()76 static std::string GetTemporaryDir() {
77 if (!gTempDir) {
78 gTempDir.emplace();
79 int saved_errno = errno;
80 if (access(gTempDir->path, F_OK) != 0) {
81 std::cerr << "Could not create temporary dir: " << gTempDir->path << ": "
82 << strerror(saved_errno) << std::endl;
83 abort();
84 }
85 }
86 return gTempDir->path;
87 }
88
89 class LocalSuperOpener final : public android::fs_mgr::PartitionOpener {
90 public:
LocalSuperOpener(const std::string & path,borrowed_fd fd)91 LocalSuperOpener(const std::string& path, borrowed_fd fd)
92 : local_super_(path), local_super_fd_(fd) {}
93
Open(const std::string & partition_name,int flags) const94 unique_fd Open(const std::string& partition_name, int flags) const override {
95 if (partition_name == local_super_) {
96 return unique_fd{dup(local_super_fd_.get())};
97 }
98 return PartitionOpener::Open(partition_name, flags);
99 }
100
101 private:
102 std::string local_super_;
103 borrowed_fd local_super_fd_;
104 };
105
106 class SuperHelper final {
107 public:
SuperHelper(const std::string & super_path)108 explicit SuperHelper(const std::string& super_path) : super_path_(super_path) {}
109
110 bool Open();
111 bool AddPartition(const std::string& partition_name, const std::string& group_name,
112 uint32_t attributes, const std::string& image_path, bool replace);
113 bool Finalize();
114
115 private:
116 bool OpenSuperFile();
117 bool UpdateSuper();
118 bool WritePartition(borrowed_fd fd, uint64_t file_size, const std::string& partition_name);
119 bool WriteExtent(borrowed_fd fd, uint64_t file_size, const LpMetadataExtent& extent);
120
121 // Returns true if |fd| does not contain a sparsed file. If |fd| does
122 // contain a sparsed file, |temp_file| will contain the unsparsed output.
123 // If |fd| cannot be read or failed to unsparse, false is returned.
124 bool MaybeUnsparse(const std::string& file, borrowed_fd fd,
125 std::optional<TemporaryFile>* temp_file, uint32_t* block_size = nullptr);
126
127 std::string super_path_;
128 std::string abs_super_path_;
129 bool was_empty_ = false;
130 // fd for the super file, sparsed or temporarily unsparsed.
131 int super_fd_;
132 // fd for the super file if unsparsed.
133 unique_fd output_fd_;
134 // If the super file is sparse, this holds the temp unsparsed file.
135 std::optional<TemporaryFile> temp_super_;
136 uint32_t sparse_block_size_ = 0;
137 std::unique_ptr<LpMetadata> metadata_;
138 std::unique_ptr<MetadataBuilder> builder_;
139 };
140
Open()141 bool SuperHelper::Open() {
142 if (!OpenSuperFile()) {
143 return false;
144 }
145
146 was_empty_ = android::fs_mgr::IsEmptySuperImage(abs_super_path_);
147 if (was_empty_) {
148 metadata_ = android::fs_mgr::ReadFromImageFile(abs_super_path_);
149 } else {
150 metadata_ = android::fs_mgr::ReadMetadata(abs_super_path_, 0);
151 }
152 if (!metadata_) {
153 std::cerr << "Could not read super partition metadata for " << super_path_ << "\n";
154 return false;
155 }
156 builder_ = MetadataBuilder::New(*metadata_.get());
157 if (!builder_) {
158 std::cerr << "Could not create MetadataBuilder for " << super_path_ << "\n";
159 return false;
160 }
161 return true;
162 }
163
AddPartition(const std::string & partition_name,const std::string & group_name,uint32_t attributes,const std::string & image_path,bool replace)164 bool SuperHelper::AddPartition(const std::string& partition_name, const std::string& group_name,
165 uint32_t attributes, const std::string& image_path, bool replace) {
166 if (!image_path.empty() && was_empty_) {
167 std::cerr << "Cannot add a partition image to an empty super file.\n";
168 return false;
169 }
170
171 if (replace) {
172 auto partition = builder_->FindPartition(partition_name);
173 if (!partition) {
174 std::cerr << "Could not find partition to replace: " << partition_name << "\n";
175 return false;
176 }
177 builder_->RemovePartition(partition_name);
178 }
179
180 auto partition = builder_->AddPartition(partition_name, group_name, attributes);
181 if (!partition) {
182 std::cerr << "Could not add partition: " << partition_name << "\n";
183 return false;
184 }
185
186 // Open the source image and get its file size so we can resize the
187 // partition.
188 int source_fd = -1;
189 uint64_t file_size;
190 unique_fd raw_image_fd;
191 std::optional<TemporaryFile> temp_image;
192 if (!image_path.empty()) {
193 raw_image_fd.reset(open(image_path.c_str(), O_RDONLY | O_CLOEXEC));
194 if (raw_image_fd < 0) {
195 std::cerr << "open failed: " << image_path << ": " << strerror(errno) << "\n";
196 return false;
197 }
198 if (!MaybeUnsparse(image_path, raw_image_fd, &temp_image)) {
199 return false;
200 }
201 source_fd = temp_image ? temp_image->fd : raw_image_fd.get();
202
203 auto size = lseek(source_fd, 0, SEEK_END);
204 if (size < 0 || lseek(source_fd, 0, SEEK_SET) < 0) {
205 std::cerr << "lseek failed: " << image_path << ": " << strerror(errno) << "\n";
206 return false;
207 }
208 if (!builder_->ResizePartition(partition, size)) {
209 std::cerr << "Failed to set partition " << partition_name << " size to " << size
210 << "bytes.\n";
211 return false;
212 }
213 file_size = (uint64_t)size;
214 }
215
216 // Write the new metadata out. We do this by re-using the on-device flashing
217 // logic, and using the local file instead of a block device.
218 if (!UpdateSuper()) {
219 return false;
220 }
221
222 // If no partition contents were specified, early return. Otherwise, we
223 // require a full super image to continue writing.
224 if (source_fd >= 0 && !WritePartition(source_fd, file_size, partition_name)) {
225 return false;
226 }
227 return true;
228 }
229
OpenSuperFile()230 bool SuperHelper::OpenSuperFile() {
231 auto actual_path = super_path_;
232
233 output_fd_.reset(open(actual_path.c_str(), O_RDWR | O_CLOEXEC));
234 if (output_fd_ < 0) {
235 std::cerr << "open failed: " << actual_path << ": " << strerror(errno) << "\n";
236 return false;
237 }
238 super_fd_ = output_fd_.get();
239
240 if (!MaybeUnsparse(super_path_, super_fd_, &temp_super_, &sparse_block_size_)) {
241 return false;
242 }
243 if (temp_super_) {
244 actual_path = temp_super_->path;
245 super_fd_ = temp_super_->fd;
246 }
247
248 // PartitionOpener will decorate relative paths with /dev/block/by-name
249 // so get an absolute path here.
250 if (!android::base::Realpath(actual_path, &abs_super_path_)) {
251 std::cerr << "realpath failed: " << actual_path << ": " << strerror(errno) << "\n";
252 return false;
253 }
254 return true;
255 }
256
MaybeUnsparse(const std::string & file,borrowed_fd fd,std::optional<TemporaryFile> * temp_file,uint32_t * block_size)257 bool SuperHelper::MaybeUnsparse(const std::string& file, borrowed_fd fd,
258 std::optional<TemporaryFile>* temp_file,
259 uint32_t* block_size) {
260 SparsePtr sf(sparse_file_import(fd.get(), false, false), sparse_file_destroy);
261 if (!sf) {
262 return true;
263 }
264
265 temp_file->emplace(GetTemporaryDir());
266 if ((*temp_file)->fd < 0) {
267 std::cerr << "mkstemp failed: " << strerror(errno) << "\n";
268 return false;
269 }
270
271 std::cout << "Unsparsing " << file << "... " << std::endl;
272
273 if (sparse_file_write(sf.get(), (*temp_file)->fd, false, false, false) != 0) {
274 std::cerr << "Could not write unsparsed file.\n";
275 return false;
276 }
277 if (block_size) {
278 *block_size = sparse_file_block_size(sf.get());
279 }
280 return true;
281 }
282
UpdateSuper()283 bool SuperHelper::UpdateSuper() {
284 metadata_ = builder_->Export();
285 if (!metadata_) {
286 std::cerr << "Failed to export new metadata.\n";
287 return false;
288 }
289
290 // Empty images get written at the very end.
291 if (was_empty_) {
292 return true;
293 }
294
295 // Note: A/B devices have an extra metadata slot that is unused, so we cap
296 // the writes to the first two slots.
297 LocalSuperOpener opener(abs_super_path_, super_fd_);
298 uint32_t slots = std::min(metadata_->geometry.metadata_slot_count, (uint32_t)2);
299 for (uint32_t i = 0; i < slots; i++) {
300 if (!UpdatePartitionTable(opener, abs_super_path_, *metadata_.get(), i)) {
301 std::cerr << "Could not write new super partition metadata.\n";
302 return false;
303 }
304 }
305 return true;
306 }
307
WritePartition(borrowed_fd fd,uint64_t file_size,const std::string & partition_name)308 bool SuperHelper::WritePartition(borrowed_fd fd, uint64_t file_size,
309 const std::string& partition_name) {
310 auto partition = android::fs_mgr::FindPartition(*metadata_.get(), partition_name);
311 if (!partition) {
312 std::cerr << "Could not find partition in metadata: " << partition_name << "\n";
313 return false;
314 }
315
316 std::cout << "Writing data for partition " << partition_name << "..." << std::endl;
317 for (uint32_t i = 0; i < partition->num_extents; i++) {
318 auto extent_index = partition->first_extent_index + i;
319 const auto& extent = metadata_->extents[extent_index];
320 if (!WriteExtent(fd, file_size, extent)) {
321 return false;
322 }
323 }
324
325 // Assert that the full file was written.
326 [[maybe_unused]] auto pos = lseek(fd.get(), 0, SEEK_CUR);
327 CHECK(pos >= 0 && (uint64_t)pos == file_size);
328 return true;
329 }
330
WriteExtent(borrowed_fd fd,uint64_t file_size,const LpMetadataExtent & extent)331 bool SuperHelper::WriteExtent(borrowed_fd fd, uint64_t file_size, const LpMetadataExtent& extent) {
332 // Must be a linear extent, and there must only be one block device.
333 CHECK(extent.target_type == LP_TARGET_TYPE_LINEAR);
334 CHECK(extent.target_source == 0);
335
336 auto pos = lseek(fd.get(), 0, SEEK_CUR);
337 if (pos < 0) {
338 std::cerr << "lseek failed: " << strerror(errno) << "\n";
339 return false;
340 }
341
342 // Clamp the number of bytes to either remaining data in the file, or the
343 // size of this extent.
344 CHECK((uint64_t)pos <= file_size);
345 uint64_t bytes_remaining =
346 std::min(file_size - (uint64_t)pos, extent.num_sectors * LP_SECTOR_SIZE);
347
348 // Reposition to the appropriate offset in super.
349 if (lseek(super_fd_, extent.target_data * LP_SECTOR_SIZE, SEEK_SET) < 0) {
350 std::cerr << "lseek failed: " << strerror(errno) << "\n";
351 return false;
352 }
353
354 uint8_t buffer[4096];
355 while (bytes_remaining > 0) {
356 uint64_t bytes = std::min((uint64_t)sizeof(buffer), bytes_remaining);
357 if (!android::base::ReadFully(fd.get(), buffer, bytes)) {
358 std::cerr << "read failed: " << strerror(errno) << "\n";
359 return false;
360 }
361 if (!android::base::WriteFully(super_fd_, buffer, bytes)) {
362 std::cerr << "write failed: " << strerror(errno) << "\n";
363 return false;
364 }
365 bytes_remaining -= bytes;
366 }
367 return true;
368 }
369
Truncate(borrowed_fd fd)370 static bool Truncate(borrowed_fd fd) {
371 if (ftruncate(fd.get(), 0) < 0) {
372 std::cerr << "truncate failed: " << strerror(errno) << "\n";
373 return false;
374 }
375 if (lseek(fd.get(), 0, SEEK_SET) < 0) {
376 std::cerr << "lseek failed: " << strerror(errno) << "\n";
377 return false;
378 }
379 return true;
380 }
381
Finalize()382 bool SuperHelper::Finalize() {
383 if (was_empty_) {
384 if (!Truncate(super_fd_)) {
385 return false;
386 }
387 if (!android::fs_mgr::WriteToImageFile(super_fd_, *metadata_.get())) {
388 std::cerr << "Could not write image file.\n";
389 return false;
390 }
391 }
392
393 // If the super image wasn't original sparsed, we don't have to do anything
394 // else.
395 if (!temp_super_) {
396 return true;
397 }
398
399 // Otherwise, we have to sparse the temporary file. Find its length.
400 auto len = lseek(super_fd_, 0, SEEK_END);
401 if (len < 0 || lseek(super_fd_, 0, SEEK_SET < 0)) {
402 std::cerr << "lseek failed: " << strerror(errno) << "\n";
403 return false;
404 }
405
406 SparsePtr sf(sparse_file_new(sparse_block_size_, len), sparse_file_destroy);
407 if (!sf) {
408 std::cerr << "Could not allocate sparse file.\n";
409 return false;
410 }
411 sparse_file_verbose(sf.get());
412
413 std::cout << "Writing sparse super image... " << std::endl;
414 if (sparse_file_read(sf.get(), super_fd_, SPARSE_READ_MODE_NORMAL, false) != 0) {
415 std::cerr << "Could not import super partition for sparsing.\n";
416 return false;
417 }
418 if (!Truncate(output_fd_)) {
419 return false;
420 }
421 if (sparse_file_write(sf.get(), output_fd_, false, true, false)) {
422 return false;
423 }
424 return true;
425 }
426
ErrorLogger(android::base::LogId,android::base::LogSeverity severity,const char *,const char *,unsigned int,const char * msg)427 static void ErrorLogger(android::base::LogId, android::base::LogSeverity severity, const char*,
428 const char*, unsigned int, const char* msg) {
429 if (severity < android::base::WARNING) {
430 return;
431 }
432 std::cerr << msg << std::endl;
433 }
434
main(int argc,char * argv[])435 int main(int argc, char* argv[]) {
436 struct option options[] = {
437 {"readonly", no_argument, nullptr, (int)OptionCode::kReadonly},
438 {"replace", no_argument, nullptr, (int)OptionCode::kReplace},
439 {nullptr, 0, nullptr, 0},
440 };
441
442 bool readonly = false;
443 bool replace = false;
444
445 int rv, index;
446 while ((rv = getopt_long(argc, argv, "h", options, &index)) != -1) {
447 switch ((OptionCode)rv) {
448 case OptionCode::kHelp:
449 usage(argv[0]);
450 return EX_OK;
451 case OptionCode::kReadonly:
452 readonly = true;
453 break;
454 case OptionCode::kReplace:
455 replace = true;
456 break;
457 default:
458 return usage(argv[0]);
459 }
460 }
461
462 if (optind + 3 > argc) {
463 std::cerr << "Missing required arguments.\n\n";
464 return usage(argv[0]);
465 }
466
467 std::string super_path = argv[optind++];
468 std::string partition_name = argv[optind++];
469 std::string group_name = argv[optind++];
470 std::string image_path;
471
472 if (optind < argc) {
473 image_path = argv[optind++];
474 }
475 if (optind != argc) {
476 std::cerr << "Unexpected arguments.\n\n";
477 return usage(argv[0]);
478 }
479
480 // Suppress log spam from liblp.
481 android::base::SetLogger(ErrorLogger);
482
483 SuperHelper super(super_path);
484 if (!super.Open()) {
485 return EX_SOFTWARE;
486 }
487
488 uint32_t attributes = LP_PARTITION_ATTR_NONE;
489 if (readonly) {
490 attributes |= LP_PARTITION_ATTR_READONLY;
491 }
492 if (!super.AddPartition(partition_name, group_name, attributes, image_path, replace)) {
493 return EX_SOFTWARE;
494 }
495 if (!super.Finalize()) {
496 return EX_SOFTWARE;
497 }
498
499 std::cout << "Done.\n";
500 return EX_OK;
501 }
502