| Amin Hassani | 7982100 | 2019-05-06 17:40:49 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright 2019 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/payload_properties.h" | 
|  | 18 |  | 
|  | 19 | #include <algorithm> | 
|  | 20 | #include <string> | 
|  | 21 | #include <utility> | 
|  | 22 | #include <vector> | 
|  | 23 |  | 
|  | 24 | #include <base/json/json_writer.h> | 
|  | 25 | #include <base/strings/string_util.h> | 
|  | 26 | #include <base/values.h> | 
|  | 27 | #include <brillo/data_encoding.h> | 
|  | 28 |  | 
|  | 29 | #include "update_engine/common/constants.h" | 
|  | 30 | #include "update_engine/common/hash_calculator.h" | 
|  | 31 | #include "update_engine/common/utils.h" | 
|  | 32 | #include "update_engine/payload_consumer/payload_metadata.h" | 
|  | 33 | #include "update_engine/update_metadata.pb.h" | 
|  | 34 |  | 
|  | 35 | using std::string; | 
|  | 36 | using std::vector; | 
|  | 37 |  | 
|  | 38 | namespace chromeos_update_engine { | 
|  | 39 |  | 
|  | 40 | namespace { | 
|  | 41 | // These ones are needed by the GoldenEye. | 
|  | 42 | const char kPayloadPropertyJsonVersion[] = "version"; | 
|  | 43 | const char kPayloadPropertyJsonPayloadHash[] = "sha256_hex"; | 
|  | 44 | const char kPayloadPropertyJsonMetadataSize[] = "metadata_size"; | 
|  | 45 | const char kPayloadPropertyJsonMetadataSignature[] = "metadata_signature"; | 
|  | 46 |  | 
|  | 47 | // These are needed by the Nebraska and devserver. | 
|  | 48 | const char kPayloadPropertyJsonPayloadSize[] = "size"; | 
|  | 49 | const char kPayloadPropertyJsonIsDelta[] = "is_delta"; | 
|  | 50 | const char kPayloadPropertyJsonTargetVersion[] = "target_version"; | 
|  | 51 | const char kPayloadPropertyJsonSourceVersion[] = "source_version"; | 
|  | 52 | }  // namespace | 
|  | 53 |  | 
|  | 54 | PayloadProperties::PayloadProperties(const string& payload_path) | 
|  | 55 | : payload_path_(payload_path) {} | 
|  | 56 |  | 
|  | 57 | bool PayloadProperties::GetPropertiesAsJson(string* json_str) { | 
|  | 58 | TEST_AND_RETURN_FALSE(LoadFromPayload()); | 
|  | 59 |  | 
|  | 60 | base::DictionaryValue properties; | 
|  | 61 | properties.SetInteger(kPayloadPropertyJsonVersion, version_); | 
|  | 62 | properties.SetInteger(kPayloadPropertyJsonMetadataSize, metadata_size_); | 
|  | 63 | properties.SetString(kPayloadPropertyJsonMetadataSignature, | 
|  | 64 | metadata_signatures_); | 
|  | 65 | properties.SetInteger(kPayloadPropertyJsonPayloadSize, payload_size_); | 
|  | 66 | properties.SetString(kPayloadPropertyJsonPayloadHash, payload_hash_); | 
|  | 67 | properties.SetBoolean(kPayloadPropertyJsonIsDelta, is_delta_); | 
|  | 68 | properties.SetString(kPayloadPropertyJsonTargetVersion, target_version_); | 
|  | 69 | if (is_delta_) { | 
|  | 70 | properties.SetString(kPayloadPropertyJsonSourceVersion, source_version_); | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | return base::JSONWriter::Write(properties, json_str); | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | bool PayloadProperties::GetPropertiesAsKeyValue(string* key_value_str) { | 
|  | 77 | TEST_AND_RETURN_FALSE(LoadFromPayload()); | 
|  | 78 |  | 
|  | 79 | brillo::KeyValueStore properties; | 
|  | 80 | properties.SetString(kPayloadPropertyFileSize, std::to_string(payload_size_)); | 
|  | 81 | properties.SetString(kPayloadPropertyMetadataSize, | 
|  | 82 | std::to_string(metadata_size_)); | 
|  | 83 | properties.SetString(kPayloadPropertyFileHash, payload_hash_); | 
|  | 84 | properties.SetString(kPayloadPropertyMetadataHash, metadata_hash_); | 
|  | 85 |  | 
|  | 86 | *key_value_str = properties.SaveToString(); | 
|  | 87 | return true; | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | bool PayloadProperties::LoadFromPayload() { | 
|  | 91 | PayloadMetadata payload_metadata; | 
|  | 92 | DeltaArchiveManifest manifest; | 
|  | 93 | Signatures metadata_signatures; | 
|  | 94 | TEST_AND_RETURN_FALSE(payload_metadata.ParsePayloadFile( | 
|  | 95 | payload_path_, &manifest, &metadata_signatures)); | 
|  | 96 |  | 
|  | 97 | metadata_size_ = payload_metadata.GetMetadataSize(); | 
|  | 98 | payload_size_ = utils::FileSize(payload_path_); | 
|  | 99 |  | 
|  | 100 | brillo::Blob metadata_hash; | 
|  | 101 | TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile( | 
|  | 102 | payload_path_, metadata_size_, &metadata_hash) == | 
|  | 103 | static_cast<off_t>(metadata_size_)); | 
|  | 104 | metadata_hash_ = brillo::data_encoding::Base64Encode(metadata_hash); | 
|  | 105 |  | 
|  | 106 | brillo::Blob payload_hash; | 
|  | 107 | TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile( | 
|  | 108 | payload_path_, payload_size_, &payload_hash) == | 
|  | 109 | static_cast<off_t>(payload_size_)); | 
|  | 110 | payload_hash_ = brillo::data_encoding::Base64Encode(payload_hash); | 
|  | 111 |  | 
|  | 112 | if (payload_metadata.GetMetadataSignatureSize() > 0) { | 
|  | 113 | TEST_AND_RETURN_FALSE(metadata_signatures.signatures_size() > 0); | 
|  | 114 | vector<string> base64_signatures; | 
|  | 115 | for (const auto& sig : metadata_signatures.signatures()) { | 
|  | 116 | base64_signatures.push_back( | 
|  | 117 | brillo::data_encoding::Base64Encode(sig.data())); | 
|  | 118 | } | 
|  | 119 | metadata_signatures_ = base::JoinString(base64_signatures, ":"); | 
|  | 120 | } | 
|  | 121 |  | 
| Amin Hassani | 55c7541 | 2019-10-07 11:20:39 -0700 | [diff] [blame] | 122 | is_delta_ = manifest.has_old_image_info() || | 
| Amin Hassani | 7982100 | 2019-05-06 17:40:49 -0700 | [diff] [blame] | 123 | std::any_of(manifest.partitions().begin(), | 
|  | 124 | manifest.partitions().end(), | 
|  | 125 | [](const PartitionUpdate& part) { | 
|  | 126 | return part.has_old_partition_info(); | 
|  | 127 | }); | 
|  | 128 |  | 
|  | 129 | if (manifest.has_new_image_info()) { | 
|  | 130 | target_version_ = manifest.new_image_info().version(); | 
|  | 131 | } else { | 
|  | 132 | target_version_ = "99999.0.0"; | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | // No need to set the source version if it was not a delta payload. | 
|  | 136 | if (is_delta_ && manifest.has_old_image_info()) { | 
|  | 137 | source_version_ = manifest.old_image_info().version(); | 
|  | 138 | } | 
|  | 139 | return true; | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | }  // namespace chromeos_update_engine |