payload_generation_config.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //
  2. // Copyright (C) 2015 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. #include "update_engine/payload_generator/payload_generation_config.h"
  17. #include <algorithm>
  18. #include <map>
  19. #include <utility>
  20. #include <base/logging.h>
  21. #include <base/strings/string_number_conversions.h>
  22. #include <brillo/strings/string_utils.h>
  23. #include "update_engine/common/utils.h"
  24. #include "update_engine/payload_consumer/delta_performer.h"
  25. #include "update_engine/payload_generator/boot_img_filesystem.h"
  26. #include "update_engine/payload_generator/delta_diff_generator.h"
  27. #include "update_engine/payload_generator/delta_diff_utils.h"
  28. #include "update_engine/payload_generator/ext2_filesystem.h"
  29. #include "update_engine/payload_generator/mapfile_filesystem.h"
  30. #include "update_engine/payload_generator/raw_filesystem.h"
  31. using std::string;
  32. namespace chromeos_update_engine {
  33. bool PostInstallConfig::IsEmpty() const {
  34. return !run && path.empty() && filesystem_type.empty() && !optional;
  35. }
  36. bool VerityConfig::IsEmpty() const {
  37. return hash_tree_data_extent.num_blocks() == 0 &&
  38. hash_tree_extent.num_blocks() == 0 && hash_tree_algorithm.empty() &&
  39. hash_tree_salt.empty() && fec_data_extent.num_blocks() == 0 &&
  40. fec_extent.num_blocks() == 0 && fec_roots == 0;
  41. }
  42. bool PartitionConfig::ValidateExists() const {
  43. TEST_AND_RETURN_FALSE(!path.empty());
  44. TEST_AND_RETURN_FALSE(utils::FileExists(path.c_str()));
  45. TEST_AND_RETURN_FALSE(size > 0);
  46. // The requested size is within the limits of the file.
  47. TEST_AND_RETURN_FALSE(static_cast<off_t>(size) <=
  48. utils::FileSize(path.c_str()));
  49. return true;
  50. }
  51. bool PartitionConfig::OpenFilesystem() {
  52. if (path.empty())
  53. return true;
  54. fs_interface.reset();
  55. if (diff_utils::IsExtFilesystem(path)) {
  56. fs_interface = Ext2Filesystem::CreateFromFile(path);
  57. // TODO(deymo): The delta generator algorithm doesn't support a block size
  58. // different than 4 KiB. Remove this check once that's fixed. b/26972455
  59. if (fs_interface) {
  60. TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
  61. return true;
  62. }
  63. }
  64. if (!mapfile_path.empty()) {
  65. fs_interface = MapfileFilesystem::CreateFromFile(path, mapfile_path);
  66. if (fs_interface) {
  67. TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
  68. return true;
  69. }
  70. }
  71. fs_interface = BootImgFilesystem::CreateFromFile(path);
  72. if (fs_interface) {
  73. TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
  74. return true;
  75. }
  76. // Fall back to a RAW filesystem.
  77. TEST_AND_RETURN_FALSE(size % kBlockSize == 0);
  78. fs_interface = RawFilesystem::Create(
  79. "<" + name + "-partition>", kBlockSize, size / kBlockSize);
  80. return true;
  81. }
  82. bool ImageConfig::ValidateIsEmpty() const {
  83. TEST_AND_RETURN_FALSE(ImageInfoIsEmpty());
  84. return partitions.empty();
  85. }
  86. bool ImageConfig::LoadImageSize() {
  87. for (PartitionConfig& part : partitions) {
  88. if (part.path.empty())
  89. continue;
  90. part.size = utils::FileSize(part.path);
  91. }
  92. return true;
  93. }
  94. bool ImageConfig::LoadPostInstallConfig(const brillo::KeyValueStore& store) {
  95. bool found_postinstall = false;
  96. for (PartitionConfig& part : partitions) {
  97. bool run_postinstall;
  98. if (!store.GetBoolean("RUN_POSTINSTALL_" + part.name, &run_postinstall) ||
  99. !run_postinstall)
  100. continue;
  101. found_postinstall = true;
  102. part.postinstall.run = true;
  103. store.GetString("POSTINSTALL_PATH_" + part.name, &part.postinstall.path);
  104. store.GetString("FILESYSTEM_TYPE_" + part.name,
  105. &part.postinstall.filesystem_type);
  106. store.GetBoolean("POSTINSTALL_OPTIONAL_" + part.name,
  107. &part.postinstall.optional);
  108. }
  109. if (!found_postinstall) {
  110. LOG(ERROR) << "No valid postinstall config found.";
  111. return false;
  112. }
  113. return true;
  114. }
  115. bool ImageConfig::LoadDynamicPartitionMetadata(
  116. const brillo::KeyValueStore& store) {
  117. auto metadata = std::make_unique<DynamicPartitionMetadata>();
  118. string buf;
  119. if (!store.GetString("super_partition_groups", &buf)) {
  120. LOG(ERROR) << "Dynamic partition info missing super_partition_groups.";
  121. return false;
  122. }
  123. auto group_names = brillo::string_utils::Split(buf, " ");
  124. for (const auto& group_name : group_names) {
  125. DynamicPartitionGroup* group = metadata->add_groups();
  126. group->set_name(group_name);
  127. if (!store.GetString(group_name + "_size", &buf)) {
  128. LOG(ERROR) << "Missing " << group_name + "_size.";
  129. return false;
  130. }
  131. uint64_t max_size;
  132. if (!base::StringToUint64(buf, &max_size)) {
  133. LOG(ERROR) << group_name << "_size=" << buf << " is not an integer.";
  134. return false;
  135. }
  136. group->set_size(max_size);
  137. if (store.GetString(group_name + "_partition_list", &buf)) {
  138. auto partition_names = brillo::string_utils::Split(buf, " ");
  139. for (const auto& partition_name : partition_names) {
  140. group->add_partition_names()->assign(partition_name);
  141. }
  142. }
  143. }
  144. dynamic_partition_metadata = std::move(metadata);
  145. return true;
  146. }
  147. bool ImageConfig::ValidateDynamicPartitionMetadata() const {
  148. if (dynamic_partition_metadata == nullptr) {
  149. LOG(ERROR) << "dynamic_partition_metadata is not loaded.";
  150. return false;
  151. }
  152. for (const auto& group : dynamic_partition_metadata->groups()) {
  153. uint64_t sum_size = 0;
  154. for (const auto& partition_name : group.partition_names()) {
  155. auto partition_config = std::find_if(partitions.begin(),
  156. partitions.end(),
  157. [&partition_name](const auto& e) {
  158. return e.name == partition_name;
  159. });
  160. if (partition_config == partitions.end()) {
  161. LOG(ERROR) << "Cannot find partition " << partition_name
  162. << " which is in " << group.name() << "_partition_list";
  163. return false;
  164. }
  165. sum_size += partition_config->size;
  166. }
  167. if (sum_size > group.size()) {
  168. LOG(ERROR) << "Sum of sizes in " << group.name() << "_partition_list is "
  169. << sum_size << ", which is greater than " << group.name()
  170. << "_size (" << group.size() << ")";
  171. return false;
  172. }
  173. }
  174. return true;
  175. }
  176. bool ImageConfig::ImageInfoIsEmpty() const {
  177. return image_info.board().empty() && image_info.key().empty() &&
  178. image_info.channel().empty() && image_info.version().empty() &&
  179. image_info.build_channel().empty() &&
  180. image_info.build_version().empty();
  181. }
  182. PayloadVersion::PayloadVersion(uint64_t major_version, uint32_t minor_version) {
  183. major = major_version;
  184. minor = minor_version;
  185. }
  186. bool PayloadVersion::Validate() const {
  187. TEST_AND_RETURN_FALSE(major == kChromeOSMajorPayloadVersion ||
  188. major == kBrilloMajorPayloadVersion);
  189. TEST_AND_RETURN_FALSE(minor == kFullPayloadMinorVersion ||
  190. minor == kInPlaceMinorPayloadVersion ||
  191. minor == kSourceMinorPayloadVersion ||
  192. minor == kOpSrcHashMinorPayloadVersion ||
  193. minor == kBrotliBsdiffMinorPayloadVersion ||
  194. minor == kPuffdiffMinorPayloadVersion ||
  195. minor == kVerityMinorPayloadVersion);
  196. return true;
  197. }
  198. bool PayloadVersion::OperationAllowed(InstallOperation::Type operation) const {
  199. switch (operation) {
  200. // Full operations:
  201. case InstallOperation::REPLACE:
  202. case InstallOperation::REPLACE_BZ:
  203. // These operations were included in the original payload format.
  204. return true;
  205. case InstallOperation::REPLACE_XZ:
  206. // These operations are included in the major version used in Brillo, but
  207. // can also be used with minor version 3 or newer.
  208. return major == kBrilloMajorPayloadVersion ||
  209. minor >= kOpSrcHashMinorPayloadVersion;
  210. case InstallOperation::ZERO:
  211. case InstallOperation::DISCARD:
  212. // The implementation of these operations had a bug in earlier versions
  213. // that prevents them from being used in any payload. We will enable
  214. // them for delta payloads for now.
  215. return minor >= kBrotliBsdiffMinorPayloadVersion;
  216. // Delta operations:
  217. case InstallOperation::MOVE:
  218. case InstallOperation::BSDIFF:
  219. // MOVE and BSDIFF were replaced by SOURCE_COPY and SOURCE_BSDIFF and
  220. // should not be used in newer delta versions, since the idempotent checks
  221. // were removed.
  222. return minor == kInPlaceMinorPayloadVersion;
  223. case InstallOperation::SOURCE_COPY:
  224. case InstallOperation::SOURCE_BSDIFF:
  225. return minor >= kSourceMinorPayloadVersion;
  226. case InstallOperation::BROTLI_BSDIFF:
  227. return minor >= kBrotliBsdiffMinorPayloadVersion;
  228. case InstallOperation::PUFFDIFF:
  229. return minor >= kPuffdiffMinorPayloadVersion;
  230. }
  231. return false;
  232. }
  233. bool PayloadVersion::IsDelta() const {
  234. return minor != kFullPayloadMinorVersion;
  235. }
  236. bool PayloadVersion::InplaceUpdate() const {
  237. return minor == kInPlaceMinorPayloadVersion;
  238. }
  239. bool PayloadGenerationConfig::Validate() const {
  240. TEST_AND_RETURN_FALSE(version.Validate());
  241. TEST_AND_RETURN_FALSE(version.IsDelta() == is_delta);
  242. if (is_delta) {
  243. for (const PartitionConfig& part : source.partitions) {
  244. if (!part.path.empty()) {
  245. TEST_AND_RETURN_FALSE(part.ValidateExists());
  246. TEST_AND_RETURN_FALSE(part.size % block_size == 0);
  247. }
  248. // Source partition should not have postinstall or verity config.
  249. TEST_AND_RETURN_FALSE(part.postinstall.IsEmpty());
  250. TEST_AND_RETURN_FALSE(part.verity.IsEmpty());
  251. }
  252. // If new_image_info is present, old_image_info must be present.
  253. TEST_AND_RETURN_FALSE(source.ImageInfoIsEmpty() ==
  254. target.ImageInfoIsEmpty());
  255. } else {
  256. // All the "source" image fields must be empty for full payloads.
  257. TEST_AND_RETURN_FALSE(source.ValidateIsEmpty());
  258. }
  259. // In all cases, the target image must exists.
  260. for (const PartitionConfig& part : target.partitions) {
  261. TEST_AND_RETURN_FALSE(part.ValidateExists());
  262. TEST_AND_RETURN_FALSE(part.size % block_size == 0);
  263. if (version.minor == kInPlaceMinorPayloadVersion &&
  264. part.name == kPartitionNameRoot)
  265. TEST_AND_RETURN_FALSE(rootfs_partition_size >= part.size);
  266. if (version.major == kChromeOSMajorPayloadVersion)
  267. TEST_AND_RETURN_FALSE(part.postinstall.IsEmpty());
  268. if (version.minor < kVerityMinorPayloadVersion)
  269. TEST_AND_RETURN_FALSE(part.verity.IsEmpty());
  270. }
  271. TEST_AND_RETURN_FALSE(hard_chunk_size == -1 ||
  272. hard_chunk_size % block_size == 0);
  273. TEST_AND_RETURN_FALSE(soft_chunk_size % block_size == 0);
  274. TEST_AND_RETURN_FALSE(rootfs_partition_size % block_size == 0);
  275. return true;
  276. }
  277. } // namespace chromeos_update_engine