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 //
16 
17 #include "update_engine/payload_generator/delta_diff_generator.h"
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <inttypes.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 
25 #include <algorithm>
26 #include <memory>
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 #include <base/logging.h>
32 
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"
36 #include "update_engine/payload_generator/ab_generator.h"
37 #include "update_engine/payload_generator/blob_file_writer.h"
38 #include "update_engine/payload_generator/delta_diff_utils.h"
39 #include "update_engine/payload_generator/full_update_generator.h"
40 #include "update_engine/payload_generator/inplace_generator.h"
41 #include "update_engine/payload_generator/payload_file.h"
42 
43 using std::string;
44 using std::unique_ptr;
45 using std::vector;
46 
47 namespace chromeos_update_engine {
48 
49 // bytes
50 const size_t kRootFSPartitionSize = static_cast<size_t>(2) * 1024 * 1024 * 1024;
51 const size_t kBlockSize = 4096;  // bytes
52 
GenerateUpdatePayloadFile(const PayloadGenerationConfig & config,const string & output_path,const string & private_key_path,uint64_t * metadata_size)53 bool GenerateUpdatePayloadFile(
54     const PayloadGenerationConfig& config,
55     const string& output_path,
56     const string& private_key_path,
57     uint64_t* metadata_size) {
58   if (!config.version.Validate()) {
59     LOG(ERROR) << "Unsupported major.minor version: " << config.version.major
60                << "." << config.version.minor;
61     return false;
62   }
63 
64   // Create empty payload file object.
65   PayloadFile payload;
66   TEST_AND_RETURN_FALSE(payload.Init(config));
67 
68   const string kTempFileTemplate("CrAU_temp_data.XXXXXX");
69   string temp_file_path;
70   int data_file_fd;
71   TEST_AND_RETURN_FALSE(
72       utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &data_file_fd));
73   ScopedPathUnlinker temp_file_unlinker(temp_file_path);
74   TEST_AND_RETURN_FALSE(data_file_fd >= 0);
75 
76   {
77     off_t data_file_size = 0;
78     ScopedFdCloser data_file_fd_closer(&data_file_fd);
79     BlobFileWriter blob_file(data_file_fd, &data_file_size);
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() &&
98           !utils::IsSquashfsFilesystem(new_part.path)) {
99         // Delta update.
100         if (utils::IsExtFilesystem(new_part.path)) {
101           LOG_IF(WARNING, old_part.size != new_part.size)
102               << "Old and new filesystems have different size.";
103           // TODO(deymo): Our tools only support growing the filesystem size
104           // during an update. Remove this check when that's fixed.
105           // crbug.com/192136
106           LOG_IF(FATAL, old_part.size > new_part.size)
107               << "Shirking the filesystem size is not supported at the moment.";
108         }
109         if (config.version.minor == kInPlaceMinorPayloadVersion) {
110           LOG(INFO) << "Using generator InplaceGenerator().";
111           strategy.reset(new InplaceGenerator());
112         } else {
113           LOG(INFO) << "Using generator ABGenerator().";
114           strategy.reset(new ABGenerator());
115         }
116       } else {
117         LOG(INFO) << "Using generator FullUpdateGenerator().";
118         strategy.reset(new FullUpdateGenerator());
119       }
120 
121       vector<AnnotatedOperation> aops;
122       // Generate the operations using the strategy we selected above.
123       TEST_AND_RETURN_FALSE(strategy->GenerateOperations(config,
124                                                          old_part,
125                                                          new_part,
126                                                          &blob_file,
127                                                          &aops));
128 
129       // Filter the no-operations. OperationsGenerators should not output this
130       // kind of operations normally, but this is an extra step to fix that if
131       // happened.
132       diff_utils::FilterNoopOperations(&aops);
133 
134       TEST_AND_RETURN_FALSE(payload.AddPartition(old_part, new_part, aops));
135     }
136   }
137 
138   LOG(INFO) << "Writing payload file...";
139   // Write payload file to disk.
140   TEST_AND_RETURN_FALSE(payload.WritePayload(output_path, temp_file_path,
141                                              private_key_path, metadata_size));
142 
143   LOG(INFO) << "All done. Successfully created delta file with "
144             << "metadata size = " << *metadata_size;
145   return true;
146 }
147 
148 };  // namespace chromeos_update_engine
149