aboutsummaryrefslogtreecommitdiff
path: root/host/commands/assemble_cvd/misc_info.cc
blob: 834cf7389147e55ac7b9c1f1593c6940344781f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//
// Copyright (C) 2020 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "misc_info.h"

#include <array>
#include <memory>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>

#include <android-base/logging.h>
#include <android-base/parseint.h>
#include <android-base/strings.h>
#include <fmt/format.h>

#include "common/libs/fs/shared_buf.h"
#include "common/libs/fs/shared_fd.h"
#include "common/libs/utils/contains.h"
#include "common/libs/utils/result.h"

namespace cuttlefish {
namespace {

constexpr char kDynamicPartitions[] = "dynamic_partition_list";
constexpr char kGoogleDynamicPartitions[] = "google_dynamic_partitions";
constexpr char kRollbackIndexSuffix[] = "_rollback_index_location";
constexpr char kSuperBlockDevices[] = "super_block_devices";
constexpr char kSuperPartitionGroups[] = "super_partition_groups";
constexpr char kUseDynamicPartitions[] = "use_dynamic_partitions";
constexpr std::array kNonPartitionKeysToMerge = {
    "ab_update", "default_system_dev_certificate"};

Result<std::string> GetExpected(const MiscInfo& misc_info,
                                const std::string& key) {
  auto lookup = misc_info.find(key);
  CF_EXPECTF(lookup != misc_info.end(),
             "Unable to retrieve expected value from key: {}", key);
  return lookup->second;
}

std::string GetOrDefault(const MiscInfo& misc_info, const std::string& key,
                         const std::string& default_value) {
  auto result = GetExpected(misc_info, key);
  return result.ok() ? *result : default_value;
}

std::string MergePartitionLists(const std::string& vendor,
                                const std::string& system) {
  const std::string full_string = fmt::format("{} {}", vendor, system);
  auto full_list = android::base::Tokenize(full_string, " ");
  // std::set removes duplicates and orders the elements, which we want
  std::set<std::string> full_set(full_list.begin(), full_list.end());
  return android::base::Join(full_set, " ");
}

std::string GetPartitionList(const MiscInfo& vendor_info,
                             const MiscInfo& system_info,
                             const std::string& key) {
  std::string vendor_list = GetOrDefault(vendor_info, key, "");
  std::string system_list = GetOrDefault(system_info, key, "");
  return MergePartitionLists(vendor_list, system_list);
}

std::vector<std::string> GeneratePartitionKeys(const std::string& name) {
  std::vector<std::string> result;
  result.emplace_back("avb_" + name);
  result.emplace_back("avb_" + name + "_algorithm");
  result.emplace_back("avb_" + name + "_key_path");
  result.emplace_back("avb_" + name + kRollbackIndexSuffix);
  result.emplace_back("avb_" + name + "_hashtree_enable");
  result.emplace_back("avb_" + name + "_add_hashtree_footer_args");
  result.emplace_back(name + "_disable_sparse");
  result.emplace_back("building_" + name + "_image");
  auto fs_type_key = name + "_fs_type";
  if (name == "system") {
    fs_type_key = "fs_type";
  }
  result.emplace_back(fs_type_key);
  return result;
}

Result<int> ResolveRollbackIndexConflicts(
    const std::string& index_string,
    const std::unordered_set<int> used_indices) {
  int index;
  CF_EXPECTF(android::base::ParseInt(index_string, &index),
             "Unable to parse value {} to string.  Maybe a wrong or bad value "
             "read for the rollback index?",
             index_string);
  while (Contains(used_indices, index)) {
    ++index;
  }
  return index;
}

}  // namespace

Result<MiscInfo> ParseMiscInfo(const std::string& misc_info_contents) {
  auto lines = android::base::Split(misc_info_contents, "\n");
  MiscInfo misc_info;
  for (auto& line : lines) {
    line = android::base::Trim(line);
    if (line.size() == 0) {
      continue;
    }
    auto eq_pos = line.find('=');
    if (eq_pos == std::string::npos) {
      LOG(WARNING) << "Line in unknown format: \"" << line << "\"";
      continue;
    }
    // Not using android::base::Split here to only capture the first =
    const auto key = android::base::Trim(line.substr(0, eq_pos));
    const auto value = android::base::Trim(line.substr(eq_pos + 1));
    const bool duplicate = Contains(misc_info, key) && misc_info[key] != value;
    CF_EXPECTF(!duplicate,
               "Duplicate key with different value. key:\"{}\", previous "
               "value:\"{}\", this value:\"{}\"",
               key, misc_info[key], value);
    misc_info[key] = value;
  }
  return misc_info;
}

Result<void> WriteMiscInfo(const MiscInfo& misc_info,
                           const std::string& output_path) {
  std::stringstream file_content;
  for (const auto& entry : misc_info) {
    file_content << entry.first << "=" << entry.second << "\n";
  }

  SharedFD output_file = SharedFD::Creat(output_path.c_str(), 0644);
  CF_EXPECT(output_file->IsOpen(),
            "Failed to open output misc file: " << output_file->StrError());

  CF_EXPECT(
      WriteAll(output_file, file_content.str()) >= 0,
      "Failed to write output misc file contents: " << output_file->StrError());
  return {};
}

// based on build/make/tools/releasetools/merge/merge_target_files.py
Result<MiscInfo> GetCombinedDynamicPartitions(const MiscInfo& vendor_info,
                                              const MiscInfo& system_info) {
  CF_EXPECTF(CF_EXPECT(GetExpected(vendor_info, kDynamicPartitions)) == "true",
             "Vendor build must have {}=true", kUseDynamicPartitions);
  CF_EXPECTF(CF_EXPECT(GetExpected(system_info, kDynamicPartitions)) == "true",
             "System build must have {}=true", kUseDynamicPartitions);
  MiscInfo result;
  // copy where both files are equal
  for (const auto& key_val : vendor_info) {
    const auto value_result = GetExpected(system_info, key_val.first);
    if (value_result.ok() && *value_result == key_val.second) {
      result[key_val.first] = key_val.second;
    }
  }

  result[kDynamicPartitions] =
      GetPartitionList(vendor_info, system_info, kDynamicPartitions);

  const auto block_devices_result =
      GetExpected(vendor_info, kSuperBlockDevices);
  if (block_devices_result.ok()) {
    result[kSuperBlockDevices] = *block_devices_result;
    for (const auto& block_device :
         android::base::Tokenize(result[kSuperBlockDevices], " ")) {
      const auto key = "super_" + block_device + "_device_size";
      result[key] = CF_EXPECT(GetExpected(vendor_info, key));
    }
  }

  result[kSuperPartitionGroups] =
      CF_EXPECT(GetExpected(vendor_info, kSuperPartitionGroups));
  for (const auto& group :
       android::base::Tokenize(result[kSuperPartitionGroups], " ")) {
    const auto group_size_key = "super_" + group + "_group_size";
    result[group_size_key] =
        CF_EXPECT(GetExpected(vendor_info, group_size_key));

    const auto partition_list_key = "super_" + group + "_partition_list";
    result[partition_list_key] =
        GetPartitionList(vendor_info, system_info, partition_list_key);
  }

  // TODO(chadreynolds): add vabc_cow_version logic if we need to support older
  // builds
  for (const auto& key :
       {"virtual_ab", "virtual_ab_retrofit", "lpmake", "super_metadata_device",
        "super_partition_error_limit", "super_partition_size"}) {
    const auto value_result = GetExpected(vendor_info, key);
    if (value_result.ok()) {
      result[key] = *value_result;
    }
  }
  return std::move(result);
}

Result<MiscInfo> MergeMiscInfos(
    const MiscInfo& vendor_info, const MiscInfo& system_info,
    const MiscInfo& combined_dp_info,
    const std::vector<std::string>& system_partitions) {
  // the combined misc info uses the vendor values as defaults
  MiscInfo result = vendor_info;
  std::unordered_set<int> used_indices;
  for (const auto& partition : system_partitions) {
    for (const auto& key : GeneratePartitionKeys(partition)) {
      if (!Contains(system_info, key)) {
        continue;
      }
      auto system_value = system_info.find(key)->second;
      // avb_<partition>_rollback_index_location values can conflict across
      // different builds
      if (android::base::EndsWith(key, kRollbackIndexSuffix)) {
        const auto index = CF_EXPECT(
            ResolveRollbackIndexConflicts(system_value, used_indices));
        used_indices.insert(index);
        system_value = std::to_string(index);
      }
      result[key] = system_value;
    }
  }
  for (const auto& key : kNonPartitionKeysToMerge) {
    if (Contains(system_info, key)) {
      result[key] = system_info.find(key)->second;
    }
  }
  for (const auto& key_val : combined_dp_info) {
    result[key_val.first] = key_val.second;
  }
  return std::move(result);
}

} // namespace cuttlefish