blob: d47c0599dcf4a8c1b2acb3265ddafd1ffd5d796d [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>
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
35using std::string;
36using std::vector;
37
38namespace chromeos_update_engine {
39
40namespace {
41// These ones are needed by the GoldenEye.
42const char kPayloadPropertyJsonVersion[] = "version";
43const char kPayloadPropertyJsonPayloadHash[] = "sha256_hex";
44const char kPayloadPropertyJsonMetadataSize[] = "metadata_size";
45const char kPayloadPropertyJsonMetadataSignature[] = "metadata_signature";
46
47// These are needed by the Nebraska and devserver.
48const char kPayloadPropertyJsonPayloadSize[] = "size";
49const char kPayloadPropertyJsonIsDelta[] = "is_delta";
Jae Hoon Kim1481c0b2023-12-01 03:24:59 +000050
51// These are JSON specific properties to handle 64-bit sizes (> 53-bits).
52const char kPayloadPropertyJsonMetadataSizeStr[] = "metadata_size_str";
53const char kPayloadPropertyJsonPayloadSizeStr[] = "size_str";
Amin Hassani79821002019-05-06 17:40:49 -070054} // namespace
55
56PayloadProperties::PayloadProperties(const string& payload_path)
57 : payload_path_(payload_path) {}
58
59bool PayloadProperties::GetPropertiesAsJson(string* json_str) {
60 TEST_AND_RETURN_FALSE(LoadFromPayload());
61
62 base::DictionaryValue properties;
63 properties.SetInteger(kPayloadPropertyJsonVersion, version_);
64 properties.SetInteger(kPayloadPropertyJsonMetadataSize, metadata_size_);
Jae Hoon Kim1481c0b2023-12-01 03:24:59 +000065 properties.SetString(kPayloadPropertyJsonMetadataSizeStr,
66 std::to_string(metadata_size_));
Amin Hassani79821002019-05-06 17:40:49 -070067 properties.SetString(kPayloadPropertyJsonMetadataSignature,
68 metadata_signatures_);
69 properties.SetInteger(kPayloadPropertyJsonPayloadSize, payload_size_);
Jae Hoon Kim1481c0b2023-12-01 03:24:59 +000070 properties.SetString(kPayloadPropertyJsonPayloadSizeStr,
71 std::to_string(payload_size_));
Amin Hassani79821002019-05-06 17:40:49 -070072 properties.SetString(kPayloadPropertyJsonPayloadHash, payload_hash_);
73 properties.SetBoolean(kPayloadPropertyJsonIsDelta, is_delta_);
Amin Hassani79821002019-05-06 17:40:49 -070074
75 return base::JSONWriter::Write(properties, json_str);
76}
77
78bool PayloadProperties::GetPropertiesAsKeyValue(string* key_value_str) {
79 TEST_AND_RETURN_FALSE(LoadFromPayload());
80
81 brillo::KeyValueStore properties;
82 properties.SetString(kPayloadPropertyFileSize, std::to_string(payload_size_));
83 properties.SetString(kPayloadPropertyMetadataSize,
84 std::to_string(metadata_size_));
85 properties.SetString(kPayloadPropertyFileHash, payload_hash_);
86 properties.SetString(kPayloadPropertyMetadataHash, metadata_hash_);
87
88 *key_value_str = properties.SaveToString();
89 return true;
90}
91
92bool PayloadProperties::LoadFromPayload() {
93 PayloadMetadata payload_metadata;
94 DeltaArchiveManifest manifest;
95 Signatures metadata_signatures;
96 TEST_AND_RETURN_FALSE(payload_metadata.ParsePayloadFile(
97 payload_path_, &manifest, &metadata_signatures));
98
99 metadata_size_ = payload_metadata.GetMetadataSize();
100 payload_size_ = utils::FileSize(payload_path_);
101
102 brillo::Blob metadata_hash;
103 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile(
104 payload_path_, metadata_size_, &metadata_hash) ==
105 static_cast<off_t>(metadata_size_));
106 metadata_hash_ = brillo::data_encoding::Base64Encode(metadata_hash);
107
108 brillo::Blob payload_hash;
109 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile(
110 payload_path_, payload_size_, &payload_hash) ==
111 static_cast<off_t>(payload_size_));
112 payload_hash_ = brillo::data_encoding::Base64Encode(payload_hash);
113
114 if (payload_metadata.GetMetadataSignatureSize() > 0) {
115 TEST_AND_RETURN_FALSE(metadata_signatures.signatures_size() > 0);
116 vector<string> base64_signatures;
117 for (const auto& sig : metadata_signatures.signatures()) {
118 base64_signatures.push_back(
119 brillo::data_encoding::Base64Encode(sig.data()));
120 }
121 metadata_signatures_ = base::JoinString(base64_signatures, ":");
122 }
123
Vyshua81598b2020-09-17 21:37:21 +0000124 is_delta_ = std::any_of(manifest.partitions().begin(),
Amin Hassani79821002019-05-06 17:40:49 -0700125 manifest.partitions().end(),
126 [](const PartitionUpdate& part) {
127 return part.has_old_partition_info();
128 });
Amin Hassani79821002019-05-06 17:40:49 -0700129 return true;
130}
131
132} // namespace chromeos_update_engine