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> |
Amin Hassani | 7982100 | 2019-05-06 17:40:49 -0700 | [diff] [blame] | 25 | #include <base/values.h> |
| 26 | #include <brillo/data_encoding.h> |
| 27 | |
| 28 | #include "update_engine/common/constants.h" |
| 29 | #include "update_engine/common/hash_calculator.h" |
| 30 | #include "update_engine/common/utils.h" |
| 31 | #include "update_engine/payload_consumer/payload_metadata.h" |
| 32 | #include "update_engine/update_metadata.pb.h" |
| 33 | |
| 34 | using std::string; |
| 35 | using std::vector; |
| 36 | |
| 37 | namespace chromeos_update_engine { |
| 38 | |
| 39 | namespace { |
| 40 | // These ones are needed by the GoldenEye. |
| 41 | const char kPayloadPropertyJsonVersion[] = "version"; |
| 42 | const char kPayloadPropertyJsonPayloadHash[] = "sha256_hex"; |
| 43 | const char kPayloadPropertyJsonMetadataSize[] = "metadata_size"; |
| 44 | const char kPayloadPropertyJsonMetadataSignature[] = "metadata_signature"; |
| 45 | |
| 46 | // These are needed by the Nebraska and devserver. |
| 47 | const char kPayloadPropertyJsonPayloadSize[] = "size"; |
| 48 | const char kPayloadPropertyJsonIsDelta[] = "is_delta"; |
Jae Hoon Kim | 1481c0b | 2023-12-01 03:24:59 +0000 | [diff] [blame] | 49 | |
| 50 | // These are JSON specific properties to handle 64-bit sizes (> 53-bits). |
| 51 | const char kPayloadPropertyJsonMetadataSizeStr[] = "metadata_size_str"; |
| 52 | const char kPayloadPropertyJsonPayloadSizeStr[] = "size_str"; |
Amin Hassani | 7982100 | 2019-05-06 17:40:49 -0700 | [diff] [blame] | 53 | } // namespace |
| 54 | |
| 55 | PayloadProperties::PayloadProperties(const string& payload_path) |
| 56 | : payload_path_(payload_path) {} |
| 57 | |
| 58 | bool PayloadProperties::GetPropertiesAsJson(string* json_str) { |
| 59 | TEST_AND_RETURN_FALSE(LoadFromPayload()); |
| 60 | |
| 61 | base::DictionaryValue properties; |
| 62 | properties.SetInteger(kPayloadPropertyJsonVersion, version_); |
| 63 | properties.SetInteger(kPayloadPropertyJsonMetadataSize, metadata_size_); |
Jae Hoon Kim | 1481c0b | 2023-12-01 03:24:59 +0000 | [diff] [blame] | 64 | properties.SetString(kPayloadPropertyJsonMetadataSizeStr, |
| 65 | std::to_string(metadata_size_)); |
Amin Hassani | 7982100 | 2019-05-06 17:40:49 -0700 | [diff] [blame] | 66 | properties.SetString(kPayloadPropertyJsonMetadataSignature, |
| 67 | metadata_signatures_); |
| 68 | properties.SetInteger(kPayloadPropertyJsonPayloadSize, payload_size_); |
Jae Hoon Kim | 1481c0b | 2023-12-01 03:24:59 +0000 | [diff] [blame] | 69 | properties.SetString(kPayloadPropertyJsonPayloadSizeStr, |
| 70 | std::to_string(payload_size_)); |
Amin Hassani | 7982100 | 2019-05-06 17:40:49 -0700 | [diff] [blame] | 71 | properties.SetString(kPayloadPropertyJsonPayloadHash, payload_hash_); |
| 72 | properties.SetBoolean(kPayloadPropertyJsonIsDelta, is_delta_); |
Amin Hassani | 7982100 | 2019-05-06 17:40:49 -0700 | [diff] [blame] | 73 | |
| 74 | return base::JSONWriter::Write(properties, json_str); |
| 75 | } |
| 76 | |
| 77 | bool PayloadProperties::GetPropertiesAsKeyValue(string* key_value_str) { |
| 78 | TEST_AND_RETURN_FALSE(LoadFromPayload()); |
| 79 | |
| 80 | brillo::KeyValueStore properties; |
| 81 | properties.SetString(kPayloadPropertyFileSize, std::to_string(payload_size_)); |
| 82 | properties.SetString(kPayloadPropertyMetadataSize, |
| 83 | std::to_string(metadata_size_)); |
| 84 | properties.SetString(kPayloadPropertyFileHash, payload_hash_); |
| 85 | properties.SetString(kPayloadPropertyMetadataHash, metadata_hash_); |
| 86 | |
| 87 | *key_value_str = properties.SaveToString(); |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | bool PayloadProperties::LoadFromPayload() { |
| 92 | PayloadMetadata payload_metadata; |
| 93 | DeltaArchiveManifest manifest; |
| 94 | Signatures metadata_signatures; |
| 95 | TEST_AND_RETURN_FALSE(payload_metadata.ParsePayloadFile( |
| 96 | payload_path_, &manifest, &metadata_signatures)); |
| 97 | |
| 98 | metadata_size_ = payload_metadata.GetMetadataSize(); |
| 99 | payload_size_ = utils::FileSize(payload_path_); |
| 100 | |
| 101 | brillo::Blob metadata_hash; |
| 102 | TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile( |
| 103 | payload_path_, metadata_size_, &metadata_hash) == |
| 104 | static_cast<off_t>(metadata_size_)); |
| 105 | metadata_hash_ = brillo::data_encoding::Base64Encode(metadata_hash); |
| 106 | |
| 107 | brillo::Blob payload_hash; |
| 108 | TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile( |
| 109 | payload_path_, payload_size_, &payload_hash) == |
| 110 | static_cast<off_t>(payload_size_)); |
| 111 | payload_hash_ = brillo::data_encoding::Base64Encode(payload_hash); |
| 112 | |
| 113 | if (payload_metadata.GetMetadataSignatureSize() > 0) { |
| 114 | TEST_AND_RETURN_FALSE(metadata_signatures.signatures_size() > 0); |
| 115 | vector<string> base64_signatures; |
| 116 | for (const auto& sig : metadata_signatures.signatures()) { |
| 117 | base64_signatures.push_back( |
| 118 | brillo::data_encoding::Base64Encode(sig.data())); |
| 119 | } |
Kelvin Zhang | 0c18424 | 2024-10-25 11:19:27 -0700 | [diff] [blame^] | 120 | metadata_signatures_ = android::base::Join(base64_signatures, ":"); |
Amin Hassani | 7982100 | 2019-05-06 17:40:49 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Vyshu | a81598b | 2020-09-17 21:37:21 +0000 | [diff] [blame] | 123 | is_delta_ = std::any_of(manifest.partitions().begin(), |
Amin Hassani | 7982100 | 2019-05-06 17:40:49 -0700 | [diff] [blame] | 124 | manifest.partitions().end(), |
| 125 | [](const PartitionUpdate& part) { |
| 126 | return part.has_old_partition_info(); |
| 127 | }); |
Amin Hassani | 7982100 | 2019-05-06 17:40:49 -0700 | [diff] [blame] | 128 | return true; |
| 129 | } |
| 130 | |
| 131 | } // namespace chromeos_update_engine |