1 // SPDX-License-Identifier: MIT
3 * Copyright (C) 2016 The Android Open Source Project
6 #include "avb_cmdline.h"
9 #include "avb_version.h"
13 /* Substitutes all variables (e.g. $(ANDROID_SYSTEM_PARTUUID)) with
14 * values. Returns NULL on OOM, otherwise the cmdline with values
17 char* avb_sub_cmdline(AvbOps* ops,
19 const char* ab_suffix,
20 bool using_boot_for_vbmeta,
21 const AvbCmdlineSubstList* additional_substitutions) {
22 const char* part_name_str[NUM_GUIDS] = {"system", "boot", "vbmeta"};
23 const char* replace_str[NUM_GUIDS] = {"$(ANDROID_SYSTEM_PARTUUID)",
24 "$(ANDROID_BOOT_PARTUUID)",
25 "$(ANDROID_VBMETA_PARTUUID)"};
30 /* Special-case for when the top-level vbmeta struct is in the boot
33 if (using_boot_for_vbmeta) {
34 part_name_str[2] = "boot";
37 /* Replace unique partition GUIDs */
38 for (n = 0; n < NUM_GUIDS; n++) {
39 char part_name[AVB_PART_NAME_MAX_SIZE];
42 /* Don't attempt to query the partition guid unless its search string is
43 * present in the command line. Note: the original cmdline is used here,
44 * not the replaced one. See b/116010959.
46 if (avb_strstr(cmdline, replace_str[n]) == NULL) {
50 if (!avb_str_concat(part_name,
53 avb_strlen(part_name_str[n]),
55 avb_strlen(ab_suffix))) {
56 avb_error("Partition name and suffix does not fit.\n");
60 io_ret = ops->get_unique_guid_for_partition(
61 ops, part_name, guid_buf, sizeof guid_buf);
62 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
64 } else if (io_ret != AVB_IO_RESULT_OK) {
65 avb_error("Error getting unique GUID for partition.\n");
70 ret = avb_replace(cmdline, replace_str[n], guid_buf);
72 char* new_ret = avb_replace(ret, replace_str[n], guid_buf);
81 /* It's possible there is no _PARTUUID for replacement above.
82 * Duplicate cmdline to ret for additional substitutions below.
85 ret = avb_strdup(cmdline);
91 /* Replace any additional substitutions. */
92 if (additional_substitutions != NULL) {
93 for (n = 0; n < additional_substitutions->size; ++n) {
94 char* new_ret = avb_replace(ret,
95 additional_substitutions->tokens[n],
96 additional_substitutions->values[n]);
114 static int cmdline_append_option(AvbSlotVerifyData* slot_data,
117 size_t offset, key_len, value_len;
120 key_len = avb_strlen(key);
121 value_len = avb_strlen(value);
124 if (slot_data->cmdline != NULL) {
125 offset = avb_strlen(slot_data->cmdline);
131 new_cmdline = avb_calloc(offset + key_len + value_len + 2);
132 if (new_cmdline == NULL) {
136 avb_memcpy(new_cmdline, slot_data->cmdline, offset - 1);
137 new_cmdline[offset - 1] = ' ';
139 avb_memcpy(new_cmdline + offset, key, key_len);
140 new_cmdline[offset + key_len] = '=';
141 avb_memcpy(new_cmdline + offset + key_len + 1, value, value_len);
142 if (slot_data->cmdline != NULL) {
143 avb_free(slot_data->cmdline);
145 slot_data->cmdline = new_cmdline;
150 #define AVB_MAX_DIGITS_UINT64 32
152 /* Writes |value| to |digits| in base 10 followed by a NUL byte.
153 * Returns number of characters written excluding the NUL byte.
155 static size_t uint64_to_base10(uint64_t value,
156 char digits[AVB_MAX_DIGITS_UINT64]) {
157 char rev_digits[AVB_MAX_DIGITS_UINT64];
158 size_t n, num_digits;
160 for (num_digits = 0; num_digits < AVB_MAX_DIGITS_UINT64 - 1;) {
161 rev_digits[num_digits++] = avb_div_by_10(&value) + '0';
167 for (n = 0; n < num_digits; n++) {
168 digits[n] = rev_digits[num_digits - 1 - n];
174 static int cmdline_append_version(AvbSlotVerifyData* slot_data,
176 uint64_t major_version,
177 uint64_t minor_version) {
178 char major_digits[AVB_MAX_DIGITS_UINT64];
179 char minor_digits[AVB_MAX_DIGITS_UINT64];
180 char combined[AVB_MAX_DIGITS_UINT64 * 2 + 1];
181 size_t num_major_digits, num_minor_digits;
183 num_major_digits = uint64_to_base10(major_version, major_digits);
184 num_minor_digits = uint64_to_base10(minor_version, minor_digits);
185 avb_memcpy(combined, major_digits, num_major_digits);
186 combined[num_major_digits] = '.';
187 avb_memcpy(combined + num_major_digits + 1, minor_digits, num_minor_digits);
188 combined[num_major_digits + 1 + num_minor_digits] = '\0';
190 return cmdline_append_option(slot_data, key, combined);
193 static int cmdline_append_uint64_base10(AvbSlotVerifyData* slot_data,
196 char digits[AVB_MAX_DIGITS_UINT64];
197 uint64_to_base10(value, digits);
198 return cmdline_append_option(slot_data, key, digits);
201 static int cmdline_append_hex(AvbSlotVerifyData* slot_data,
206 char* hex_data = avb_bin2hex(data, data_len);
207 if (hex_data == NULL) {
210 ret = cmdline_append_option(slot_data, key, hex_data);
215 AvbSlotVerifyResult avb_append_options(
217 AvbSlotVerifyFlags flags,
218 AvbSlotVerifyData* slot_data,
219 AvbVBMetaImageHeader* toplevel_vbmeta,
220 AvbAlgorithmType algorithm_type,
221 AvbHashtreeErrorMode hashtree_error_mode,
222 AvbHashtreeErrorMode resolved_hashtree_error_mode) {
223 AvbSlotVerifyResult ret;
224 const char* verity_mode;
225 bool is_device_unlocked;
228 /* Add androidboot.vbmeta.device option... except if not using a vbmeta
229 * partition since it doesn't make sense in that case.
231 if (!(flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION)) {
232 if (!cmdline_append_option(slot_data,
233 "androidboot.vbmeta.device",
234 "PARTUUID=$(ANDROID_VBMETA_PARTUUID)")) {
235 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
240 /* Add androidboot.vbmeta.avb_version option. */
241 if (!cmdline_append_version(slot_data,
242 "androidboot.vbmeta.avb_version",
244 AVB_VERSION_MINOR)) {
245 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
249 /* Set androidboot.avb.device_state to "locked" or "unlocked". */
250 io_ret = ops->read_is_device_unlocked(ops, &is_device_unlocked);
251 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
252 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
254 } else if (io_ret != AVB_IO_RESULT_OK) {
255 avb_error("Error getting device state.\n");
256 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
259 if (!cmdline_append_option(slot_data,
260 "androidboot.vbmeta.device_state",
261 is_device_unlocked ? "unlocked" : "locked")) {
262 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
266 /* Set androidboot.vbmeta.{hash_alg, size, digest} - use same hash
267 * function as is used to sign vbmeta.
269 switch (algorithm_type) {
270 /* Explicit fallthrough. */
271 case AVB_ALGORITHM_TYPE_NONE:
272 case AVB_ALGORITHM_TYPE_SHA256_RSA2048:
273 case AVB_ALGORITHM_TYPE_SHA256_RSA4096:
274 case AVB_ALGORITHM_TYPE_SHA256_RSA8192: {
275 size_t n, total_size = 0;
276 uint8_t vbmeta_digest[AVB_SHA256_DIGEST_SIZE];
277 avb_slot_verify_data_calculate_vbmeta_digest(
278 slot_data, AVB_DIGEST_TYPE_SHA256, vbmeta_digest);
279 for (n = 0; n < slot_data->num_vbmeta_images; n++) {
280 total_size += slot_data->vbmeta_images[n].vbmeta_size;
282 if (!cmdline_append_option(
283 slot_data, "androidboot.vbmeta.hash_alg", "sha256") ||
284 !cmdline_append_uint64_base10(
285 slot_data, "androidboot.vbmeta.size", total_size) ||
286 !cmdline_append_hex(slot_data,
287 "androidboot.vbmeta.digest",
289 AVB_SHA256_DIGEST_SIZE)) {
290 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
294 /* Explicit fallthrough. */
295 case AVB_ALGORITHM_TYPE_SHA512_RSA2048:
296 case AVB_ALGORITHM_TYPE_SHA512_RSA4096:
297 case AVB_ALGORITHM_TYPE_SHA512_RSA8192: {
298 size_t n, total_size = 0;
299 uint8_t vbmeta_digest[AVB_SHA512_DIGEST_SIZE];
300 avb_slot_verify_data_calculate_vbmeta_digest(
301 slot_data, AVB_DIGEST_TYPE_SHA512, vbmeta_digest);
302 for (n = 0; n < slot_data->num_vbmeta_images; n++) {
303 total_size += slot_data->vbmeta_images[n].vbmeta_size;
305 if (!cmdline_append_option(
306 slot_data, "androidboot.vbmeta.hash_alg", "sha512") ||
307 !cmdline_append_uint64_base10(
308 slot_data, "androidboot.vbmeta.size", total_size) ||
309 !cmdline_append_hex(slot_data,
310 "androidboot.vbmeta.digest",
312 AVB_SHA512_DIGEST_SIZE)) {
313 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
317 case _AVB_ALGORITHM_NUM_TYPES:
318 avb_assert_not_reached();
322 /* Set androidboot.veritymode and androidboot.vbmeta.invalidate_on_error */
323 if (toplevel_vbmeta->flags & AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED) {
324 verity_mode = "disabled";
326 const char* dm_verity_mode;
329 switch (resolved_hashtree_error_mode) {
330 case AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE:
331 if (!cmdline_append_option(
332 slot_data, "androidboot.vbmeta.invalidate_on_error", "yes")) {
333 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
336 verity_mode = "enforcing";
337 dm_verity_mode = "restart_on_corruption";
339 case AVB_HASHTREE_ERROR_MODE_RESTART:
340 verity_mode = "enforcing";
341 dm_verity_mode = "restart_on_corruption";
343 case AVB_HASHTREE_ERROR_MODE_EIO:
345 /* For now there's no option to specify the EIO mode. So
346 * just use 'ignore_zero_blocks' since that's already set
347 * and dm-verity-target.c supports specifying this multiple
350 dm_verity_mode = "ignore_zero_blocks";
352 case AVB_HASHTREE_ERROR_MODE_LOGGING:
353 verity_mode = "logging";
354 dm_verity_mode = "ignore_corruption";
356 case AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO:
357 // Should never get here because MANAGED_RESTART_AND_EIO is
358 // remapped by avb_manage_hashtree_error_mode().
359 avb_assert_not_reached();
360 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
363 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
366 new_ret = avb_replace(
367 slot_data->cmdline, "$(ANDROID_VERITY_MODE)", dm_verity_mode);
368 avb_free(slot_data->cmdline);
369 slot_data->cmdline = new_ret;
370 if (slot_data->cmdline == NULL) {
371 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
375 if (!cmdline_append_option(
376 slot_data, "androidboot.veritymode", verity_mode)) {
377 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
380 if (hashtree_error_mode == AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO) {
381 if (!cmdline_append_option(
382 slot_data, "androidboot.veritymode.managed", "yes")) {
383 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
388 ret = AVB_SLOT_VERIFY_RESULT_OK;
395 AvbCmdlineSubstList* avb_new_cmdline_subst_list() {
396 return (AvbCmdlineSubstList*)avb_calloc(sizeof(AvbCmdlineSubstList));
399 void avb_free_cmdline_subst_list(AvbCmdlineSubstList* cmdline_subst) {
401 for (i = 0; i < cmdline_subst->size; ++i) {
402 avb_free(cmdline_subst->tokens[i]);
403 avb_free(cmdline_subst->values[i]);
405 cmdline_subst->size = 0;
406 avb_free(cmdline_subst);
409 AvbSlotVerifyResult avb_add_root_digest_substitution(
410 const char* part_name,
411 const uint8_t* digest,
413 AvbCmdlineSubstList* out_cmdline_subst) {
414 const char* kDigestSubPrefix = "$(AVB_";
415 const char* kDigestSubSuffix = "_ROOT_DIGEST)";
416 size_t part_name_len = avb_strlen(part_name);
417 size_t list_index = out_cmdline_subst->size;
419 avb_assert(part_name_len < AVB_PART_NAME_MAX_SIZE);
420 avb_assert(digest_size <= AVB_SHA512_DIGEST_SIZE);
421 if (part_name_len >= AVB_PART_NAME_MAX_SIZE ||
422 digest_size > AVB_SHA512_DIGEST_SIZE) {
423 return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
426 if (out_cmdline_subst->size >= AVB_MAX_NUM_CMDLINE_SUBST) {
427 /* The list is full. Currently dynamic growth of this list is not supported.
429 return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
432 /* Construct the token to replace in the command line based on the partition
433 * name. For partition 'foo', this will be '$(AVB_FOO_ROOT_DIGEST)'.
435 out_cmdline_subst->tokens[list_index] =
436 avb_strdupv(kDigestSubPrefix, part_name, kDigestSubSuffix, NULL);
437 if (out_cmdline_subst->tokens[list_index] == NULL) {
440 avb_uppercase(out_cmdline_subst->tokens[list_index]);
442 /* The digest value is hex encoded when inserted in the command line. */
443 out_cmdline_subst->values[list_index] = avb_bin2hex(digest, digest_size);
444 if (out_cmdline_subst->values[list_index] == NULL) {
448 out_cmdline_subst->size++;
449 return AVB_SLOT_VERIFY_RESULT_OK;
452 if (out_cmdline_subst->tokens[list_index]) {
453 avb_free(out_cmdline_subst->tokens[list_index]);
455 if (out_cmdline_subst->values[list_index]) {
456 avb_free(out_cmdline_subst->values[list_index]);
458 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;