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 if (!avb_str_concat(part_name,
45 avb_strlen(part_name_str[n]),
47 avb_strlen(ab_suffix))) {
48 avb_error("Partition name and suffix does not fit.\n");
52 io_ret = ops->get_unique_guid_for_partition(
53 ops, part_name, guid_buf, sizeof guid_buf);
54 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
56 } else if (io_ret != AVB_IO_RESULT_OK) {
57 avb_error("Error getting unique GUID for partition.\n");
62 ret = avb_replace(cmdline, replace_str[n], guid_buf);
64 char* new_ret = avb_replace(ret, replace_str[n], guid_buf);
73 avb_assert(ret != NULL);
75 /* Replace any additional substitutions. */
76 if (additional_substitutions != NULL) {
77 for (n = 0; n < additional_substitutions->size; ++n) {
78 char* new_ret = avb_replace(ret,
79 additional_substitutions->tokens[n],
80 additional_substitutions->values[n]);
98 static int cmdline_append_option(AvbSlotVerifyData* slot_data,
101 size_t offset, key_len, value_len;
104 key_len = avb_strlen(key);
105 value_len = avb_strlen(value);
108 if (slot_data->cmdline != NULL) {
109 offset = avb_strlen(slot_data->cmdline);
115 new_cmdline = avb_calloc(offset + key_len + value_len + 2);
116 if (new_cmdline == NULL) {
120 avb_memcpy(new_cmdline, slot_data->cmdline, offset - 1);
121 new_cmdline[offset - 1] = ' ';
123 avb_memcpy(new_cmdline + offset, key, key_len);
124 new_cmdline[offset + key_len] = '=';
125 avb_memcpy(new_cmdline + offset + key_len + 1, value, value_len);
126 if (slot_data->cmdline != NULL) {
127 avb_free(slot_data->cmdline);
129 slot_data->cmdline = new_cmdline;
134 #define AVB_MAX_DIGITS_UINT64 32
136 /* Writes |value| to |digits| in base 10 followed by a NUL byte.
137 * Returns number of characters written excluding the NUL byte.
139 static size_t uint64_to_base10(uint64_t value,
140 char digits[AVB_MAX_DIGITS_UINT64]) {
141 char rev_digits[AVB_MAX_DIGITS_UINT64];
142 size_t n, num_digits;
144 for (num_digits = 0; num_digits < AVB_MAX_DIGITS_UINT64 - 1;) {
145 rev_digits[num_digits++] = avb_div_by_10(&value) + '0';
151 for (n = 0; n < num_digits; n++) {
152 digits[n] = rev_digits[num_digits - 1 - n];
158 static int cmdline_append_version(AvbSlotVerifyData* slot_data,
160 uint64_t major_version,
161 uint64_t minor_version) {
162 char major_digits[AVB_MAX_DIGITS_UINT64];
163 char minor_digits[AVB_MAX_DIGITS_UINT64];
164 char combined[AVB_MAX_DIGITS_UINT64 * 2 + 1];
165 size_t num_major_digits, num_minor_digits;
167 num_major_digits = uint64_to_base10(major_version, major_digits);
168 num_minor_digits = uint64_to_base10(minor_version, minor_digits);
169 avb_memcpy(combined, major_digits, num_major_digits);
170 combined[num_major_digits] = '.';
171 avb_memcpy(combined + num_major_digits + 1, minor_digits, num_minor_digits);
172 combined[num_major_digits + 1 + num_minor_digits] = '\0';
174 return cmdline_append_option(slot_data, key, combined);
177 static int cmdline_append_uint64_base10(AvbSlotVerifyData* slot_data,
180 char digits[AVB_MAX_DIGITS_UINT64];
181 uint64_to_base10(value, digits);
182 return cmdline_append_option(slot_data, key, digits);
185 static int cmdline_append_hex(AvbSlotVerifyData* slot_data,
190 char* hex_data = avb_bin2hex(data, data_len);
191 if (hex_data == NULL) {
194 ret = cmdline_append_option(slot_data, key, hex_data);
199 AvbSlotVerifyResult avb_append_options(
201 AvbSlotVerifyData* slot_data,
202 AvbVBMetaImageHeader* toplevel_vbmeta,
203 AvbAlgorithmType algorithm_type,
204 AvbHashtreeErrorMode hashtree_error_mode) {
205 AvbSlotVerifyResult ret;
206 const char* verity_mode;
207 bool is_device_unlocked;
210 /* Add androidboot.vbmeta.device option. */
211 if (!cmdline_append_option(slot_data,
212 "androidboot.vbmeta.device",
213 "PARTUUID=$(ANDROID_VBMETA_PARTUUID)")) {
214 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
218 /* Add androidboot.vbmeta.avb_version option. */
219 if (!cmdline_append_version(slot_data,
220 "androidboot.vbmeta.avb_version",
222 AVB_VERSION_MINOR)) {
223 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
227 /* Set androidboot.avb.device_state to "locked" or "unlocked". */
228 io_ret = ops->read_is_device_unlocked(ops, &is_device_unlocked);
229 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
230 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
232 } else if (io_ret != AVB_IO_RESULT_OK) {
233 avb_error("Error getting device state.\n");
234 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
237 if (!cmdline_append_option(slot_data,
238 "androidboot.vbmeta.device_state",
239 is_device_unlocked ? "unlocked" : "locked")) {
240 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
244 /* Set androidboot.vbmeta.{hash_alg, size, digest} - use same hash
245 * function as is used to sign vbmeta.
247 switch (algorithm_type) {
248 /* Explicit fallthrough. */
249 case AVB_ALGORITHM_TYPE_NONE:
250 case AVB_ALGORITHM_TYPE_SHA256_RSA2048:
251 case AVB_ALGORITHM_TYPE_SHA256_RSA4096:
252 case AVB_ALGORITHM_TYPE_SHA256_RSA8192: {
253 size_t n, total_size = 0;
254 uint8_t vbmeta_digest[AVB_SHA256_DIGEST_SIZE];
255 avb_slot_verify_data_calculate_vbmeta_digest(
256 slot_data, AVB_DIGEST_TYPE_SHA256, vbmeta_digest);
257 for (n = 0; n < slot_data->num_vbmeta_images; n++) {
258 total_size += slot_data->vbmeta_images[n].vbmeta_size;
260 if (!cmdline_append_option(
261 slot_data, "androidboot.vbmeta.hash_alg", "sha256") ||
262 !cmdline_append_uint64_base10(
263 slot_data, "androidboot.vbmeta.size", total_size) ||
264 !cmdline_append_hex(slot_data,
265 "androidboot.vbmeta.digest",
267 AVB_SHA256_DIGEST_SIZE)) {
268 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
272 /* Explicit fallthrough. */
273 case AVB_ALGORITHM_TYPE_SHA512_RSA2048:
274 case AVB_ALGORITHM_TYPE_SHA512_RSA4096:
275 case AVB_ALGORITHM_TYPE_SHA512_RSA8192: {
276 size_t n, total_size = 0;
277 uint8_t vbmeta_digest[AVB_SHA512_DIGEST_SIZE];
278 avb_slot_verify_data_calculate_vbmeta_digest(
279 slot_data, AVB_DIGEST_TYPE_SHA512, vbmeta_digest);
280 for (n = 0; n < slot_data->num_vbmeta_images; n++) {
281 total_size += slot_data->vbmeta_images[n].vbmeta_size;
283 if (!cmdline_append_option(
284 slot_data, "androidboot.vbmeta.hash_alg", "sha512") ||
285 !cmdline_append_uint64_base10(
286 slot_data, "androidboot.vbmeta.size", total_size) ||
287 !cmdline_append_hex(slot_data,
288 "androidboot.vbmeta.digest",
290 AVB_SHA512_DIGEST_SIZE)) {
291 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
295 case _AVB_ALGORITHM_NUM_TYPES:
296 avb_assert_not_reached();
300 /* Set androidboot.veritymode and androidboot.vbmeta.invalidate_on_error */
301 if (toplevel_vbmeta->flags & AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED) {
302 verity_mode = "disabled";
304 const char* dm_verity_mode;
307 switch (hashtree_error_mode) {
308 case AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE:
309 if (!cmdline_append_option(
310 slot_data, "androidboot.vbmeta.invalidate_on_error", "yes")) {
311 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
314 verity_mode = "enforcing";
315 dm_verity_mode = "restart_on_corruption";
317 case AVB_HASHTREE_ERROR_MODE_RESTART:
318 verity_mode = "enforcing";
319 dm_verity_mode = "restart_on_corruption";
321 case AVB_HASHTREE_ERROR_MODE_EIO:
323 /* For now there's no option to specify the EIO mode. So
324 * just use 'ignore_zero_blocks' since that's already set
325 * and dm-verity-target.c supports specifying this multiple
328 dm_verity_mode = "ignore_zero_blocks";
330 case AVB_HASHTREE_ERROR_MODE_LOGGING:
331 verity_mode = "logging";
332 dm_verity_mode = "ignore_corruption";
335 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
338 new_ret = avb_replace(
339 slot_data->cmdline, "$(ANDROID_VERITY_MODE)", dm_verity_mode);
340 avb_free(slot_data->cmdline);
341 slot_data->cmdline = new_ret;
342 if (slot_data->cmdline == NULL) {
343 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
347 if (!cmdline_append_option(
348 slot_data, "androidboot.veritymode", verity_mode)) {
349 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
353 ret = AVB_SLOT_VERIFY_RESULT_OK;
360 AvbCmdlineSubstList* avb_new_cmdline_subst_list() {
361 return (AvbCmdlineSubstList*)avb_calloc(sizeof(AvbCmdlineSubstList));
364 void avb_free_cmdline_subst_list(AvbCmdlineSubstList* cmdline_subst) {
366 for (i = 0; i < cmdline_subst->size; ++i) {
367 avb_free(cmdline_subst->tokens[i]);
368 avb_free(cmdline_subst->values[i]);
370 cmdline_subst->size = 0;
371 avb_free(cmdline_subst);
374 AvbSlotVerifyResult avb_add_root_digest_substitution(
375 const char* part_name,
376 const uint8_t* digest,
378 AvbCmdlineSubstList* out_cmdline_subst) {
379 const char* kDigestSubPrefix = "$(AVB_";
380 const char* kDigestSubSuffix = "_ROOT_DIGEST)";
381 size_t part_name_len = avb_strlen(part_name);
382 size_t list_index = out_cmdline_subst->size;
384 avb_assert(part_name_len < AVB_PART_NAME_MAX_SIZE);
385 avb_assert(digest_size <= AVB_SHA512_DIGEST_SIZE);
386 if (part_name_len >= AVB_PART_NAME_MAX_SIZE ||
387 digest_size > AVB_SHA512_DIGEST_SIZE) {
388 return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
391 if (out_cmdline_subst->size >= AVB_MAX_NUM_CMDLINE_SUBST) {
392 /* The list is full. Currently dynamic growth of this list is not supported.
394 return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
397 /* Construct the token to replace in the command line based on the partition
398 * name. For partition 'foo', this will be '$(AVB_FOO_ROOT_DIGEST)'.
400 out_cmdline_subst->tokens[list_index] =
401 avb_strdupv(kDigestSubPrefix, part_name, kDigestSubSuffix, NULL);
402 if (out_cmdline_subst->tokens[list_index] == NULL) {
405 avb_uppercase(out_cmdline_subst->tokens[list_index]);
407 /* The digest value is hex encoded when inserted in the command line. */
408 out_cmdline_subst->values[list_index] = avb_bin2hex(digest, digest_size);
409 if (out_cmdline_subst->values[list_index] == NULL) {
413 out_cmdline_subst->size++;
414 return AVB_SLOT_VERIFY_RESULT_OK;
417 if (out_cmdline_subst->tokens[list_index]) {
418 avb_free(out_cmdline_subst->tokens[list_index]);
420 if (out_cmdline_subst->values[list_index]) {
421 avb_free(out_cmdline_subst->values[list_index]);
423 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;