blob: 60c682a671eb5f5478bfa0f403c7b1e89a94f10f [file] [log] [blame]
Amin Hassani79821002019-05-06 17:40:49 -07001//
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 Hassani79821002019-05-06 17:40:49 -070025#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
34using std::string;
35using std::vector;
36
37namespace chromeos_update_engine {
38
39namespace {
40// These ones are needed by the GoldenEye.
41const char kPayloadPropertyJsonVersion[] = "version";
42const char kPayloadPropertyJsonPayloadHash[] = "sha256_hex";
43const char kPayloadPropertyJsonMetadataSize[] = "metadata_size";
44const char kPayloadPropertyJsonMetadataSignature[] = "metadata_signature";
45
46// These are needed by the Nebraska and devserver.
47const char kPayloadPropertyJsonPayloadSize[] = "size";
48const char kPayloadPropertyJsonIsDelta[] = "is_delta";
Jae Hoon Kim1481c0b2023-12-01 03:24:59 +000049
50// These are JSON specific properties to handle 64-bit sizes (> 53-bits).
51const char kPayloadPropertyJsonMetadataSizeStr[] = "metadata_size_str";
52const char kPayloadPropertyJsonPayloadSizeStr[] = "size_str";
Amin Hassani79821002019-05-06 17:40:49 -070053} // namespace
54
55PayloadProperties::PayloadProperties(const string& payload_path)
56 : payload_path_(payload_path) {}
57
58bool 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 Kim1481c0b2023-12-01 03:24:59 +000064 properties.SetString(kPayloadPropertyJsonMetadataSizeStr,
65 std::to_string(metadata_size_));
Amin Hassani79821002019-05-06 17:40:49 -070066 properties.SetString(kPayloadPropertyJsonMetadataSignature,
67 metadata_signatures_);
68 properties.SetInteger(kPayloadPropertyJsonPayloadSize, payload_size_);
Jae Hoon Kim1481c0b2023-12-01 03:24:59 +000069 properties.SetString(kPayloadPropertyJsonPayloadSizeStr,
70 std::to_string(payload_size_));
Amin Hassani79821002019-05-06 17:40:49 -070071 properties.SetString(kPayloadPropertyJsonPayloadHash, payload_hash_);
72 properties.SetBoolean(kPayloadPropertyJsonIsDelta, is_delta_);
Amin Hassani79821002019-05-06 17:40:49 -070073
74 return base::JSONWriter::Write(properties, json_str);
75}
76
77bool 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
91bool 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 Zhang0c184242024-10-25 11:19:27 -0700120 metadata_signatures_ = android::base::Join(base64_signatures, ":");
Amin Hassani79821002019-05-06 17:40:49 -0700121 }
122
Vyshua81598b2020-09-17 21:37:21 +0000123 is_delta_ = std::any_of(manifest.partitions().begin(),
Amin Hassani79821002019-05-06 17:40:49 -0700124 manifest.partitions().end(),
125 [](const PartitionUpdate& part) {
126 return part.has_old_partition_info();
127 });
Amin Hassani79821002019-05-06 17:40:49 -0700128 return true;
129}
130
131} // namespace chromeos_update_engine