Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 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 | // |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 | |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 17 | #include "update_engine/payload_generator/delta_diff_generator.h" |
Darin Petkov | 880335c | 2010-10-01 15:52:53 -0700 | [diff] [blame] | 18 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
Andrew de los Reyes | 27f7d37 | 2010-10-07 11:26:07 -0700 | [diff] [blame] | 21 | #include <inttypes.h> |
Darin Petkov | 880335c | 2010-10-01 15:52:53 -0700 | [diff] [blame] | 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 25 | #include <algorithm> |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 26 | #include <memory> |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 27 | #include <string> |
| 28 | #include <utility> |
| 29 | #include <vector> |
Darin Petkov | 880335c | 2010-10-01 15:52:53 -0700 | [diff] [blame] | 30 | |
| 31 | #include <base/logging.h> |
Darin Petkov | 880335c | 2010-10-01 15:52:53 -0700 | [diff] [blame] | 32 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 33 | #include "update_engine/common/utils.h" |
| 34 | #include "update_engine/payload_consumer/delta_performer.h" |
| 35 | #include "update_engine/payload_consumer/payload_constants.h" |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 36 | #include "update_engine/payload_generator/ab_generator.h" |
Sen Jiang | 8cc502d | 2015-08-10 10:04:54 -0700 | [diff] [blame] | 37 | #include "update_engine/payload_generator/blob_file_writer.h" |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 38 | #include "update_engine/payload_generator/delta_diff_utils.h" |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 39 | #include "update_engine/payload_generator/full_update_generator.h" |
Allie Wood | cd514b5 | 2015-02-19 13:56:07 -0800 | [diff] [blame] | 40 | #include "update_engine/payload_generator/inplace_generator.h" |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 41 | #include "update_engine/payload_generator/payload_file.h" |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 42 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 43 | using std::string; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 44 | using std::unique_ptr; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 45 | using std::vector; |
| 46 | |
| 47 | namespace chromeos_update_engine { |
| 48 | |
Chris Sosa | f586b01 | 2013-05-21 13:33:42 -0700 | [diff] [blame] | 49 | // bytes |
| 50 | const size_t kRootFSPartitionSize = static_cast<size_t>(2) * 1024 * 1024 * 1024; |
Allie Wood | cd514b5 | 2015-02-19 13:56:07 -0800 | [diff] [blame] | 51 | const size_t kBlockSize = 4096; // bytes |
Alex Deymo | 9b244df | 2015-03-11 21:51:18 -0700 | [diff] [blame] | 52 | |
Alex Deymo | 477aec2 | 2015-03-24 23:40:48 -0700 | [diff] [blame] | 53 | bool GenerateUpdatePayloadFile( |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 54 | const PayloadGenerationConfig& config, |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 55 | const string& output_path, |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 56 | const string& private_key_path, |
| 57 | uint64_t* metadata_size) { |
Alex Deymo | a4073ef | 2016-03-22 23:40:53 -0700 | [diff] [blame] | 58 | if (!config.version.Validate()) { |
| 59 | LOG(ERROR) << "Unsupported major.minor version: " << config.version.major |
| 60 | << "." << config.version.minor; |
| 61 | return false; |
| 62 | } |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 63 | |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 64 | // Create empty payload file object. |
| 65 | PayloadFile payload; |
| 66 | TEST_AND_RETURN_FALSE(payload.Init(config)); |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 67 | |
Sen Jiang | 981eb11 | 2015-08-25 17:03:18 -0700 | [diff] [blame] | 68 | const string kTempFileTemplate("CrAU_temp_data.XXXXXX"); |
| 69 | string temp_file_path; |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 70 | int data_file_fd; |
| 71 | TEST_AND_RETURN_FALSE( |
| 72 | utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &data_file_fd)); |
Sen Jiang | 981eb11 | 2015-08-25 17:03:18 -0700 | [diff] [blame] | 73 | ScopedPathUnlinker temp_file_unlinker(temp_file_path); |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 74 | TEST_AND_RETURN_FALSE(data_file_fd >= 0); |
| 75 | |
| 76 | { |
Sen Jiang | 981eb11 | 2015-08-25 17:03:18 -0700 | [diff] [blame] | 77 | off_t data_file_size = 0; |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 78 | ScopedFdCloser data_file_fd_closer(&data_file_fd); |
Sen Jiang | 8cc502d | 2015-08-10 10:04:54 -0700 | [diff] [blame] | 79 | BlobFileWriter blob_file(data_file_fd, &data_file_size); |
Sen Jiang | 981eb11 | 2015-08-25 17:03:18 -0700 | [diff] [blame] | 80 | if (config.is_delta) { |
| 81 | TEST_AND_RETURN_FALSE(config.source.partitions.size() == |
| 82 | config.target.partitions.size()); |
| 83 | } |
| 84 | PartitionConfig empty_part(""); |
| 85 | for (size_t i = 0; i < config.target.partitions.size(); i++) { |
| 86 | const PartitionConfig& old_part = |
| 87 | config.is_delta ? config.source.partitions[i] : empty_part; |
| 88 | const PartitionConfig& new_part = config.target.partitions[i]; |
| 89 | LOG(INFO) << "Partition name: " << new_part.name; |
| 90 | LOG(INFO) << "Partition size: " << new_part.size; |
| 91 | LOG(INFO) << "Block count: " << new_part.size / config.block_size; |
| 92 | |
| 93 | // Select payload generation strategy based on the config. |
| 94 | unique_ptr<OperationsGenerator> strategy; |
| 95 | // We don't efficiently support deltas on squashfs. For now, we will |
| 96 | // produce full operations in that case. |
| 97 | if (!old_part.path.empty() && |
Sen Jiang | dcbc0ae | 2016-03-18 15:33:19 -0700 | [diff] [blame] | 98 | !diff_utils::IsSquashfs4Filesystem(new_part.path)) { |
Sen Jiang | 981eb11 | 2015-08-25 17:03:18 -0700 | [diff] [blame] | 99 | // Delta update. |
Alex Deymo | a4073ef | 2016-03-22 23:40:53 -0700 | [diff] [blame] | 100 | if (config.version.minor == kInPlaceMinorPayloadVersion) { |
Sen Jiang | 981eb11 | 2015-08-25 17:03:18 -0700 | [diff] [blame] | 101 | LOG(INFO) << "Using generator InplaceGenerator()."; |
| 102 | strategy.reset(new InplaceGenerator()); |
Alex Deymo | a4073ef | 2016-03-22 23:40:53 -0700 | [diff] [blame] | 103 | } else { |
Sen Jiang | 981eb11 | 2015-08-25 17:03:18 -0700 | [diff] [blame] | 104 | LOG(INFO) << "Using generator ABGenerator()."; |
| 105 | strategy.reset(new ABGenerator()); |
Sen Jiang | 981eb11 | 2015-08-25 17:03:18 -0700 | [diff] [blame] | 106 | } |
| 107 | } else { |
| 108 | LOG(INFO) << "Using generator FullUpdateGenerator()."; |
| 109 | strategy.reset(new FullUpdateGenerator()); |
| 110 | } |
| 111 | |
| 112 | vector<AnnotatedOperation> aops; |
| 113 | // Generate the operations using the strategy we selected above. |
| 114 | TEST_AND_RETURN_FALSE(strategy->GenerateOperations(config, |
| 115 | old_part, |
| 116 | new_part, |
| 117 | &blob_file, |
| 118 | &aops)); |
| 119 | |
| 120 | // Filter the no-operations. OperationsGenerators should not output this |
| 121 | // kind of operations normally, but this is an extra step to fix that if |
| 122 | // happened. |
| 123 | diff_utils::FilterNoopOperations(&aops); |
| 124 | |
| 125 | TEST_AND_RETURN_FALSE(payload.AddPartition(old_part, new_part, aops)); |
| 126 | } |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 127 | } |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 128 | |
Sen Jiang | 981eb11 | 2015-08-25 17:03:18 -0700 | [diff] [blame] | 129 | LOG(INFO) << "Writing payload file..."; |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 130 | // Write payload file to disk. |
Sen Jiang | 70a6ab0 | 2015-08-28 13:23:27 -0700 | [diff] [blame] | 131 | TEST_AND_RETURN_FALSE(payload.WritePayload(output_path, temp_file_path, |
| 132 | private_key_path, metadata_size)); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 133 | |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 134 | LOG(INFO) << "All done. Successfully created delta file with " |
| 135 | << "metadata size = " << *metadata_size; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | |
| 139 | }; // namespace chromeos_update_engine |