xref: /aosp_15_r20/external/avb/libavb/avb_cmdline.c (revision d289c2ba6de359471b23d594623b906876bc48a0)
1*d289c2baSAndroid Build Coastguard Worker /*
2*d289c2baSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*d289c2baSAndroid Build Coastguard Worker  *
4*d289c2baSAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person
5*d289c2baSAndroid Build Coastguard Worker  * obtaining a copy of this software and associated documentation
6*d289c2baSAndroid Build Coastguard Worker  * files (the "Software"), to deal in the Software without
7*d289c2baSAndroid Build Coastguard Worker  * restriction, including without limitation the rights to use, copy,
8*d289c2baSAndroid Build Coastguard Worker  * modify, merge, publish, distribute, sublicense, and/or sell copies
9*d289c2baSAndroid Build Coastguard Worker  * of the Software, and to permit persons to whom the Software is
10*d289c2baSAndroid Build Coastguard Worker  * furnished to do so, subject to the following conditions:
11*d289c2baSAndroid Build Coastguard Worker  *
12*d289c2baSAndroid Build Coastguard Worker  * The above copyright notice and this permission notice shall be
13*d289c2baSAndroid Build Coastguard Worker  * included in all copies or substantial portions of the Software.
14*d289c2baSAndroid Build Coastguard Worker  *
15*d289c2baSAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*d289c2baSAndroid Build Coastguard Worker  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*d289c2baSAndroid Build Coastguard Worker  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*d289c2baSAndroid Build Coastguard Worker  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*d289c2baSAndroid Build Coastguard Worker  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*d289c2baSAndroid Build Coastguard Worker  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*d289c2baSAndroid Build Coastguard Worker  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*d289c2baSAndroid Build Coastguard Worker  * SOFTWARE.
23*d289c2baSAndroid Build Coastguard Worker  */
24*d289c2baSAndroid Build Coastguard Worker 
25*d289c2baSAndroid Build Coastguard Worker #include "avb_cmdline.h"
26*d289c2baSAndroid Build Coastguard Worker #include "avb_sha.h"
27*d289c2baSAndroid Build Coastguard Worker #include "avb_util.h"
28*d289c2baSAndroid Build Coastguard Worker #include "avb_version.h"
29*d289c2baSAndroid Build Coastguard Worker 
30*d289c2baSAndroid Build Coastguard Worker #define NUM_GUIDS 3
31*d289c2baSAndroid Build Coastguard Worker 
32*d289c2baSAndroid Build Coastguard Worker /* Substitutes all variables (e.g. $(ANDROID_SYSTEM_PARTUUID)) with
33*d289c2baSAndroid Build Coastguard Worker  * values. Returns NULL on OOM, otherwise the cmdline with values
34*d289c2baSAndroid Build Coastguard Worker  * replaced.
35*d289c2baSAndroid Build Coastguard Worker  */
avb_sub_cmdline(AvbOps * ops,const char * cmdline,const char * ab_suffix,bool using_boot_for_vbmeta,const AvbCmdlineSubstList * additional_substitutions)36*d289c2baSAndroid Build Coastguard Worker char* avb_sub_cmdline(AvbOps* ops,
37*d289c2baSAndroid Build Coastguard Worker                       const char* cmdline,
38*d289c2baSAndroid Build Coastguard Worker                       const char* ab_suffix,
39*d289c2baSAndroid Build Coastguard Worker                       bool using_boot_for_vbmeta,
40*d289c2baSAndroid Build Coastguard Worker                       const AvbCmdlineSubstList* additional_substitutions) {
41*d289c2baSAndroid Build Coastguard Worker   const char* part_name_str[NUM_GUIDS] = {"system", "boot", "vbmeta"};
42*d289c2baSAndroid Build Coastguard Worker   const char* replace_str[NUM_GUIDS] = {"$(ANDROID_SYSTEM_PARTUUID)",
43*d289c2baSAndroid Build Coastguard Worker                                         "$(ANDROID_BOOT_PARTUUID)",
44*d289c2baSAndroid Build Coastguard Worker                                         "$(ANDROID_VBMETA_PARTUUID)"};
45*d289c2baSAndroid Build Coastguard Worker   char* ret = NULL;
46*d289c2baSAndroid Build Coastguard Worker   AvbIOResult io_ret;
47*d289c2baSAndroid Build Coastguard Worker   size_t n;
48*d289c2baSAndroid Build Coastguard Worker 
49*d289c2baSAndroid Build Coastguard Worker   /* Special-case for when the top-level vbmeta struct is in the boot
50*d289c2baSAndroid Build Coastguard Worker    * partition.
51*d289c2baSAndroid Build Coastguard Worker    */
52*d289c2baSAndroid Build Coastguard Worker   if (using_boot_for_vbmeta) {
53*d289c2baSAndroid Build Coastguard Worker     part_name_str[2] = "boot";
54*d289c2baSAndroid Build Coastguard Worker   }
55*d289c2baSAndroid Build Coastguard Worker 
56*d289c2baSAndroid Build Coastguard Worker   /* Replace unique partition GUIDs */
57*d289c2baSAndroid Build Coastguard Worker   for (n = 0; n < NUM_GUIDS; n++) {
58*d289c2baSAndroid Build Coastguard Worker     char part_name[AVB_PART_NAME_MAX_SIZE];
59*d289c2baSAndroid Build Coastguard Worker     char guid_buf[37];
60*d289c2baSAndroid Build Coastguard Worker 
61*d289c2baSAndroid Build Coastguard Worker     /* Don't attempt to query the partition guid unless its search string is
62*d289c2baSAndroid Build Coastguard Worker      * present in the command line. Note: the original cmdline is used here,
63*d289c2baSAndroid Build Coastguard Worker      * not the replaced one. See b/116010959.
64*d289c2baSAndroid Build Coastguard Worker      */
65*d289c2baSAndroid Build Coastguard Worker     if (avb_strstr(cmdline, replace_str[n]) == NULL) {
66*d289c2baSAndroid Build Coastguard Worker       continue;
67*d289c2baSAndroid Build Coastguard Worker     }
68*d289c2baSAndroid Build Coastguard Worker 
69*d289c2baSAndroid Build Coastguard Worker     if (!avb_str_concat(part_name,
70*d289c2baSAndroid Build Coastguard Worker                         sizeof part_name,
71*d289c2baSAndroid Build Coastguard Worker                         part_name_str[n],
72*d289c2baSAndroid Build Coastguard Worker                         avb_strlen(part_name_str[n]),
73*d289c2baSAndroid Build Coastguard Worker                         ab_suffix,
74*d289c2baSAndroid Build Coastguard Worker                         avb_strlen(ab_suffix))) {
75*d289c2baSAndroid Build Coastguard Worker       avb_error("Partition name and suffix does not fit.\n");
76*d289c2baSAndroid Build Coastguard Worker       goto fail;
77*d289c2baSAndroid Build Coastguard Worker     }
78*d289c2baSAndroid Build Coastguard Worker 
79*d289c2baSAndroid Build Coastguard Worker     io_ret = ops->get_unique_guid_for_partition(
80*d289c2baSAndroid Build Coastguard Worker         ops, part_name, guid_buf, sizeof guid_buf);
81*d289c2baSAndroid Build Coastguard Worker     if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
82*d289c2baSAndroid Build Coastguard Worker       goto fail;
83*d289c2baSAndroid Build Coastguard Worker     } else if (io_ret != AVB_IO_RESULT_OK) {
84*d289c2baSAndroid Build Coastguard Worker       avb_error("Error getting unique GUID for partition.\n");
85*d289c2baSAndroid Build Coastguard Worker       goto fail;
86*d289c2baSAndroid Build Coastguard Worker     }
87*d289c2baSAndroid Build Coastguard Worker 
88*d289c2baSAndroid Build Coastguard Worker     if (ret == NULL) {
89*d289c2baSAndroid Build Coastguard Worker       ret = avb_replace(cmdline, replace_str[n], guid_buf);
90*d289c2baSAndroid Build Coastguard Worker     } else {
91*d289c2baSAndroid Build Coastguard Worker       char* new_ret = avb_replace(ret, replace_str[n], guid_buf);
92*d289c2baSAndroid Build Coastguard Worker       avb_free(ret);
93*d289c2baSAndroid Build Coastguard Worker       ret = new_ret;
94*d289c2baSAndroid Build Coastguard Worker     }
95*d289c2baSAndroid Build Coastguard Worker     if (ret == NULL) {
96*d289c2baSAndroid Build Coastguard Worker       goto fail;
97*d289c2baSAndroid Build Coastguard Worker     }
98*d289c2baSAndroid Build Coastguard Worker   }
99*d289c2baSAndroid Build Coastguard Worker 
100*d289c2baSAndroid Build Coastguard Worker   /* It's possible there is no _PARTUUID for replacement above.
101*d289c2baSAndroid Build Coastguard Worker    * Duplicate cmdline to ret for additional substitutions below.
102*d289c2baSAndroid Build Coastguard Worker    */
103*d289c2baSAndroid Build Coastguard Worker   if (ret == NULL) {
104*d289c2baSAndroid Build Coastguard Worker     ret = avb_strdup(cmdline);
105*d289c2baSAndroid Build Coastguard Worker     if (ret == NULL) {
106*d289c2baSAndroid Build Coastguard Worker       goto fail;
107*d289c2baSAndroid Build Coastguard Worker     }
108*d289c2baSAndroid Build Coastguard Worker   }
109*d289c2baSAndroid Build Coastguard Worker 
110*d289c2baSAndroid Build Coastguard Worker   /* Replace any additional substitutions. */
111*d289c2baSAndroid Build Coastguard Worker   if (additional_substitutions != NULL) {
112*d289c2baSAndroid Build Coastguard Worker     for (n = 0; n < additional_substitutions->size; ++n) {
113*d289c2baSAndroid Build Coastguard Worker       char* new_ret = avb_replace(ret,
114*d289c2baSAndroid Build Coastguard Worker                                   additional_substitutions->tokens[n],
115*d289c2baSAndroid Build Coastguard Worker                                   additional_substitutions->values[n]);
116*d289c2baSAndroid Build Coastguard Worker       avb_free(ret);
117*d289c2baSAndroid Build Coastguard Worker       ret = new_ret;
118*d289c2baSAndroid Build Coastguard Worker       if (ret == NULL) {
119*d289c2baSAndroid Build Coastguard Worker         goto fail;
120*d289c2baSAndroid Build Coastguard Worker       }
121*d289c2baSAndroid Build Coastguard Worker     }
122*d289c2baSAndroid Build Coastguard Worker   }
123*d289c2baSAndroid Build Coastguard Worker 
124*d289c2baSAndroid Build Coastguard Worker   return ret;
125*d289c2baSAndroid Build Coastguard Worker 
126*d289c2baSAndroid Build Coastguard Worker fail:
127*d289c2baSAndroid Build Coastguard Worker   if (ret != NULL) {
128*d289c2baSAndroid Build Coastguard Worker     avb_free(ret);
129*d289c2baSAndroid Build Coastguard Worker   }
130*d289c2baSAndroid Build Coastguard Worker   return NULL;
131*d289c2baSAndroid Build Coastguard Worker }
132*d289c2baSAndroid Build Coastguard Worker 
cmdline_append_option(AvbSlotVerifyData * slot_data,const char * key,const char * value)133*d289c2baSAndroid Build Coastguard Worker static int cmdline_append_option(AvbSlotVerifyData* slot_data,
134*d289c2baSAndroid Build Coastguard Worker                                  const char* key,
135*d289c2baSAndroid Build Coastguard Worker                                  const char* value) {
136*d289c2baSAndroid Build Coastguard Worker   size_t offset, key_len, value_len;
137*d289c2baSAndroid Build Coastguard Worker   char* new_cmdline;
138*d289c2baSAndroid Build Coastguard Worker 
139*d289c2baSAndroid Build Coastguard Worker   key_len = avb_strlen(key);
140*d289c2baSAndroid Build Coastguard Worker   value_len = avb_strlen(value);
141*d289c2baSAndroid Build Coastguard Worker 
142*d289c2baSAndroid Build Coastguard Worker   offset = 0;
143*d289c2baSAndroid Build Coastguard Worker   if (slot_data->cmdline != NULL) {
144*d289c2baSAndroid Build Coastguard Worker     offset = avb_strlen(slot_data->cmdline);
145*d289c2baSAndroid Build Coastguard Worker     if (offset > 0) {
146*d289c2baSAndroid Build Coastguard Worker       offset += 1;
147*d289c2baSAndroid Build Coastguard Worker     }
148*d289c2baSAndroid Build Coastguard Worker   }
149*d289c2baSAndroid Build Coastguard Worker 
150*d289c2baSAndroid Build Coastguard Worker   new_cmdline = avb_calloc(offset + key_len + value_len + 2);
151*d289c2baSAndroid Build Coastguard Worker   if (new_cmdline == NULL) {
152*d289c2baSAndroid Build Coastguard Worker     return 0;
153*d289c2baSAndroid Build Coastguard Worker   }
154*d289c2baSAndroid Build Coastguard Worker   if (offset > 0) {
155*d289c2baSAndroid Build Coastguard Worker     avb_memcpy(new_cmdline, slot_data->cmdline, offset - 1);
156*d289c2baSAndroid Build Coastguard Worker     new_cmdline[offset - 1] = ' ';
157*d289c2baSAndroid Build Coastguard Worker   }
158*d289c2baSAndroid Build Coastguard Worker   avb_memcpy(new_cmdline + offset, key, key_len);
159*d289c2baSAndroid Build Coastguard Worker   new_cmdline[offset + key_len] = '=';
160*d289c2baSAndroid Build Coastguard Worker   avb_memcpy(new_cmdline + offset + key_len + 1, value, value_len);
161*d289c2baSAndroid Build Coastguard Worker   if (slot_data->cmdline != NULL) {
162*d289c2baSAndroid Build Coastguard Worker     avb_free(slot_data->cmdline);
163*d289c2baSAndroid Build Coastguard Worker   }
164*d289c2baSAndroid Build Coastguard Worker   slot_data->cmdline = new_cmdline;
165*d289c2baSAndroid Build Coastguard Worker 
166*d289c2baSAndroid Build Coastguard Worker   return 1;
167*d289c2baSAndroid Build Coastguard Worker }
168*d289c2baSAndroid Build Coastguard Worker 
cmdline_append_version(AvbSlotVerifyData * slot_data,const char * key,uint64_t major_version,uint64_t minor_version)169*d289c2baSAndroid Build Coastguard Worker static int cmdline_append_version(AvbSlotVerifyData* slot_data,
170*d289c2baSAndroid Build Coastguard Worker                                   const char* key,
171*d289c2baSAndroid Build Coastguard Worker                                   uint64_t major_version,
172*d289c2baSAndroid Build Coastguard Worker                                   uint64_t minor_version) {
173*d289c2baSAndroid Build Coastguard Worker   char major_digits[AVB_MAX_DIGITS_UINT64];
174*d289c2baSAndroid Build Coastguard Worker   char minor_digits[AVB_MAX_DIGITS_UINT64];
175*d289c2baSAndroid Build Coastguard Worker   char combined[AVB_MAX_DIGITS_UINT64 * 2 + 1];
176*d289c2baSAndroid Build Coastguard Worker   size_t num_major_digits, num_minor_digits;
177*d289c2baSAndroid Build Coastguard Worker 
178*d289c2baSAndroid Build Coastguard Worker   num_major_digits = avb_uint64_to_base10(major_version, major_digits);
179*d289c2baSAndroid Build Coastguard Worker   num_minor_digits = avb_uint64_to_base10(minor_version, minor_digits);
180*d289c2baSAndroid Build Coastguard Worker   avb_memcpy(combined, major_digits, num_major_digits);
181*d289c2baSAndroid Build Coastguard Worker   combined[num_major_digits] = '.';
182*d289c2baSAndroid Build Coastguard Worker   avb_memcpy(combined + num_major_digits + 1, minor_digits, num_minor_digits);
183*d289c2baSAndroid Build Coastguard Worker   combined[num_major_digits + 1 + num_minor_digits] = '\0';
184*d289c2baSAndroid Build Coastguard Worker 
185*d289c2baSAndroid Build Coastguard Worker   return cmdline_append_option(slot_data, key, combined);
186*d289c2baSAndroid Build Coastguard Worker }
187*d289c2baSAndroid Build Coastguard Worker 
cmdline_append_uint64_base10(AvbSlotVerifyData * slot_data,const char * key,uint64_t value)188*d289c2baSAndroid Build Coastguard Worker static int cmdline_append_uint64_base10(AvbSlotVerifyData* slot_data,
189*d289c2baSAndroid Build Coastguard Worker                                         const char* key,
190*d289c2baSAndroid Build Coastguard Worker                                         uint64_t value) {
191*d289c2baSAndroid Build Coastguard Worker   char digits[AVB_MAX_DIGITS_UINT64];
192*d289c2baSAndroid Build Coastguard Worker   avb_uint64_to_base10(value, digits);
193*d289c2baSAndroid Build Coastguard Worker   return cmdline_append_option(slot_data, key, digits);
194*d289c2baSAndroid Build Coastguard Worker }
195*d289c2baSAndroid Build Coastguard Worker 
cmdline_append_hex(AvbSlotVerifyData * slot_data,const char * key,const uint8_t * data,size_t data_len)196*d289c2baSAndroid Build Coastguard Worker static int cmdline_append_hex(AvbSlotVerifyData* slot_data,
197*d289c2baSAndroid Build Coastguard Worker                               const char* key,
198*d289c2baSAndroid Build Coastguard Worker                               const uint8_t* data,
199*d289c2baSAndroid Build Coastguard Worker                               size_t data_len) {
200*d289c2baSAndroid Build Coastguard Worker   int ret;
201*d289c2baSAndroid Build Coastguard Worker   char* hex_data = avb_bin2hex(data, data_len);
202*d289c2baSAndroid Build Coastguard Worker   if (hex_data == NULL) {
203*d289c2baSAndroid Build Coastguard Worker     return 0;
204*d289c2baSAndroid Build Coastguard Worker   }
205*d289c2baSAndroid Build Coastguard Worker   ret = cmdline_append_option(slot_data, key, hex_data);
206*d289c2baSAndroid Build Coastguard Worker   avb_free(hex_data);
207*d289c2baSAndroid Build Coastguard Worker   return ret;
208*d289c2baSAndroid Build Coastguard Worker }
209*d289c2baSAndroid Build Coastguard Worker 
avb_append_options(AvbOps * ops,AvbSlotVerifyFlags flags,AvbSlotVerifyData * slot_data,AvbVBMetaImageHeader * toplevel_vbmeta,AvbAlgorithmType algorithm_type,AvbHashtreeErrorMode hashtree_error_mode,AvbHashtreeErrorMode resolved_hashtree_error_mode)210*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyResult avb_append_options(
211*d289c2baSAndroid Build Coastguard Worker     AvbOps* ops,
212*d289c2baSAndroid Build Coastguard Worker     AvbSlotVerifyFlags flags,
213*d289c2baSAndroid Build Coastguard Worker     AvbSlotVerifyData* slot_data,
214*d289c2baSAndroid Build Coastguard Worker     AvbVBMetaImageHeader* toplevel_vbmeta,
215*d289c2baSAndroid Build Coastguard Worker     AvbAlgorithmType algorithm_type,
216*d289c2baSAndroid Build Coastguard Worker     AvbHashtreeErrorMode hashtree_error_mode,
217*d289c2baSAndroid Build Coastguard Worker     AvbHashtreeErrorMode resolved_hashtree_error_mode) {
218*d289c2baSAndroid Build Coastguard Worker   AvbSlotVerifyResult ret;
219*d289c2baSAndroid Build Coastguard Worker   const char* verity_mode;
220*d289c2baSAndroid Build Coastguard Worker   bool is_device_unlocked;
221*d289c2baSAndroid Build Coastguard Worker   AvbIOResult io_ret;
222*d289c2baSAndroid Build Coastguard Worker 
223*d289c2baSAndroid Build Coastguard Worker   /* Add androidboot.vbmeta.device option... except if not using a vbmeta
224*d289c2baSAndroid Build Coastguard Worker    * partition since it doesn't make sense in that case.
225*d289c2baSAndroid Build Coastguard Worker    */
226*d289c2baSAndroid Build Coastguard Worker   if (!(flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION)) {
227*d289c2baSAndroid Build Coastguard Worker     if (!cmdline_append_option(slot_data,
228*d289c2baSAndroid Build Coastguard Worker                                "androidboot.vbmeta.device",
229*d289c2baSAndroid Build Coastguard Worker                                "PARTUUID=$(ANDROID_VBMETA_PARTUUID)")) {
230*d289c2baSAndroid Build Coastguard Worker       ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
231*d289c2baSAndroid Build Coastguard Worker       goto out;
232*d289c2baSAndroid Build Coastguard Worker     }
233*d289c2baSAndroid Build Coastguard Worker   }
234*d289c2baSAndroid Build Coastguard Worker 
235*d289c2baSAndroid Build Coastguard Worker   /* Add androidboot.vbmeta.avb_version option. */
236*d289c2baSAndroid Build Coastguard Worker   if (!cmdline_append_version(slot_data,
237*d289c2baSAndroid Build Coastguard Worker                               "androidboot.vbmeta.avb_version",
238*d289c2baSAndroid Build Coastguard Worker                               AVB_VERSION_MAJOR,
239*d289c2baSAndroid Build Coastguard Worker                               AVB_VERSION_MINOR)) {
240*d289c2baSAndroid Build Coastguard Worker     ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
241*d289c2baSAndroid Build Coastguard Worker     goto out;
242*d289c2baSAndroid Build Coastguard Worker   }
243*d289c2baSAndroid Build Coastguard Worker 
244*d289c2baSAndroid Build Coastguard Worker   /* Set androidboot.avb.device_state to "locked" or "unlocked". */
245*d289c2baSAndroid Build Coastguard Worker   io_ret = ops->read_is_device_unlocked(ops, &is_device_unlocked);
246*d289c2baSAndroid Build Coastguard Worker   if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
247*d289c2baSAndroid Build Coastguard Worker     ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
248*d289c2baSAndroid Build Coastguard Worker     goto out;
249*d289c2baSAndroid Build Coastguard Worker   } else if (io_ret != AVB_IO_RESULT_OK) {
250*d289c2baSAndroid Build Coastguard Worker     avb_error("Error getting device state.\n");
251*d289c2baSAndroid Build Coastguard Worker     ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
252*d289c2baSAndroid Build Coastguard Worker     goto out;
253*d289c2baSAndroid Build Coastguard Worker   }
254*d289c2baSAndroid Build Coastguard Worker   if (!cmdline_append_option(slot_data,
255*d289c2baSAndroid Build Coastguard Worker                              "androidboot.vbmeta.device_state",
256*d289c2baSAndroid Build Coastguard Worker                              is_device_unlocked ? "unlocked" : "locked")) {
257*d289c2baSAndroid Build Coastguard Worker     ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
258*d289c2baSAndroid Build Coastguard Worker     goto out;
259*d289c2baSAndroid Build Coastguard Worker   }
260*d289c2baSAndroid Build Coastguard Worker 
261*d289c2baSAndroid Build Coastguard Worker   /* Set androidboot.vbmeta.{hash_alg, size, digest} - use same hash
262*d289c2baSAndroid Build Coastguard Worker    * function as is used to sign vbmeta.
263*d289c2baSAndroid Build Coastguard Worker    */
264*d289c2baSAndroid Build Coastguard Worker   switch (algorithm_type) {
265*d289c2baSAndroid Build Coastguard Worker     /* Explicit fallthrough. */
266*d289c2baSAndroid Build Coastguard Worker     case AVB_ALGORITHM_TYPE_NONE:
267*d289c2baSAndroid Build Coastguard Worker     case AVB_ALGORITHM_TYPE_SHA256_RSA2048:
268*d289c2baSAndroid Build Coastguard Worker     case AVB_ALGORITHM_TYPE_SHA256_RSA4096:
269*d289c2baSAndroid Build Coastguard Worker     case AVB_ALGORITHM_TYPE_SHA256_RSA8192: {
270*d289c2baSAndroid Build Coastguard Worker       size_t n, total_size = 0;
271*d289c2baSAndroid Build Coastguard Worker       uint8_t vbmeta_digest[AVB_SHA256_DIGEST_SIZE];
272*d289c2baSAndroid Build Coastguard Worker       avb_slot_verify_data_calculate_vbmeta_digest(
273*d289c2baSAndroid Build Coastguard Worker           slot_data, AVB_DIGEST_TYPE_SHA256, vbmeta_digest);
274*d289c2baSAndroid Build Coastguard Worker       for (n = 0; n < slot_data->num_vbmeta_images; n++) {
275*d289c2baSAndroid Build Coastguard Worker         total_size += slot_data->vbmeta_images[n].vbmeta_size;
276*d289c2baSAndroid Build Coastguard Worker       }
277*d289c2baSAndroid Build Coastguard Worker       if (!cmdline_append_option(
278*d289c2baSAndroid Build Coastguard Worker               slot_data, "androidboot.vbmeta.hash_alg", "sha256") ||
279*d289c2baSAndroid Build Coastguard Worker           !cmdline_append_uint64_base10(
280*d289c2baSAndroid Build Coastguard Worker               slot_data, "androidboot.vbmeta.size", total_size) ||
281*d289c2baSAndroid Build Coastguard Worker           !cmdline_append_hex(slot_data,
282*d289c2baSAndroid Build Coastguard Worker                               "androidboot.vbmeta.digest",
283*d289c2baSAndroid Build Coastguard Worker                               vbmeta_digest,
284*d289c2baSAndroid Build Coastguard Worker                               AVB_SHA256_DIGEST_SIZE)) {
285*d289c2baSAndroid Build Coastguard Worker         ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
286*d289c2baSAndroid Build Coastguard Worker         goto out;
287*d289c2baSAndroid Build Coastguard Worker       }
288*d289c2baSAndroid Build Coastguard Worker     } break;
289*d289c2baSAndroid Build Coastguard Worker     /* Explicit fallthrough. */
290*d289c2baSAndroid Build Coastguard Worker     case AVB_ALGORITHM_TYPE_SHA512_RSA2048:
291*d289c2baSAndroid Build Coastguard Worker     case AVB_ALGORITHM_TYPE_SHA512_RSA4096:
292*d289c2baSAndroid Build Coastguard Worker     case AVB_ALGORITHM_TYPE_SHA512_RSA8192: {
293*d289c2baSAndroid Build Coastguard Worker       size_t n, total_size = 0;
294*d289c2baSAndroid Build Coastguard Worker       uint8_t vbmeta_digest[AVB_SHA512_DIGEST_SIZE];
295*d289c2baSAndroid Build Coastguard Worker       avb_slot_verify_data_calculate_vbmeta_digest(
296*d289c2baSAndroid Build Coastguard Worker           slot_data, AVB_DIGEST_TYPE_SHA512, vbmeta_digest);
297*d289c2baSAndroid Build Coastguard Worker       for (n = 0; n < slot_data->num_vbmeta_images; n++) {
298*d289c2baSAndroid Build Coastguard Worker         total_size += slot_data->vbmeta_images[n].vbmeta_size;
299*d289c2baSAndroid Build Coastguard Worker       }
300*d289c2baSAndroid Build Coastguard Worker       if (!cmdline_append_option(
301*d289c2baSAndroid Build Coastguard Worker               slot_data, "androidboot.vbmeta.hash_alg", "sha512") ||
302*d289c2baSAndroid Build Coastguard Worker           !cmdline_append_uint64_base10(
303*d289c2baSAndroid Build Coastguard Worker               slot_data, "androidboot.vbmeta.size", total_size) ||
304*d289c2baSAndroid Build Coastguard Worker           !cmdline_append_hex(slot_data,
305*d289c2baSAndroid Build Coastguard Worker                               "androidboot.vbmeta.digest",
306*d289c2baSAndroid Build Coastguard Worker                               vbmeta_digest,
307*d289c2baSAndroid Build Coastguard Worker                               AVB_SHA512_DIGEST_SIZE)) {
308*d289c2baSAndroid Build Coastguard Worker         ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
309*d289c2baSAndroid Build Coastguard Worker         goto out;
310*d289c2baSAndroid Build Coastguard Worker       }
311*d289c2baSAndroid Build Coastguard Worker     } break;
312*d289c2baSAndroid Build Coastguard Worker     case _AVB_ALGORITHM_NUM_TYPES:
313*d289c2baSAndroid Build Coastguard Worker       avb_assert_not_reached();
314*d289c2baSAndroid Build Coastguard Worker       break;
315*d289c2baSAndroid Build Coastguard Worker   }
316*d289c2baSAndroid Build Coastguard Worker 
317*d289c2baSAndroid Build Coastguard Worker   /* Set androidboot.veritymode and androidboot.vbmeta.invalidate_on_error */
318*d289c2baSAndroid Build Coastguard Worker   if (toplevel_vbmeta->flags & AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED) {
319*d289c2baSAndroid Build Coastguard Worker     verity_mode = "disabled";
320*d289c2baSAndroid Build Coastguard Worker   } else {
321*d289c2baSAndroid Build Coastguard Worker     const char* dm_verity_mode;
322*d289c2baSAndroid Build Coastguard Worker     char* new_ret;
323*d289c2baSAndroid Build Coastguard Worker 
324*d289c2baSAndroid Build Coastguard Worker     switch (resolved_hashtree_error_mode) {
325*d289c2baSAndroid Build Coastguard Worker       case AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE:
326*d289c2baSAndroid Build Coastguard Worker         if (!cmdline_append_option(
327*d289c2baSAndroid Build Coastguard Worker                 slot_data, "androidboot.vbmeta.invalidate_on_error", "yes")) {
328*d289c2baSAndroid Build Coastguard Worker           ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
329*d289c2baSAndroid Build Coastguard Worker           goto out;
330*d289c2baSAndroid Build Coastguard Worker         }
331*d289c2baSAndroid Build Coastguard Worker         verity_mode = "enforcing";
332*d289c2baSAndroid Build Coastguard Worker         dm_verity_mode = "restart_on_corruption";
333*d289c2baSAndroid Build Coastguard Worker         break;
334*d289c2baSAndroid Build Coastguard Worker       case AVB_HASHTREE_ERROR_MODE_RESTART:
335*d289c2baSAndroid Build Coastguard Worker         verity_mode = "enforcing";
336*d289c2baSAndroid Build Coastguard Worker         dm_verity_mode = "restart_on_corruption";
337*d289c2baSAndroid Build Coastguard Worker         break;
338*d289c2baSAndroid Build Coastguard Worker       case AVB_HASHTREE_ERROR_MODE_EIO:
339*d289c2baSAndroid Build Coastguard Worker         verity_mode = "eio";
340*d289c2baSAndroid Build Coastguard Worker         /* For now there's no option to specify the EIO mode. So
341*d289c2baSAndroid Build Coastguard Worker          * just use 'ignore_zero_blocks' since that's already set
342*d289c2baSAndroid Build Coastguard Worker          * and dm-verity-target.c supports specifying this multiple
343*d289c2baSAndroid Build Coastguard Worker          * times.
344*d289c2baSAndroid Build Coastguard Worker          */
345*d289c2baSAndroid Build Coastguard Worker         dm_verity_mode = "ignore_zero_blocks";
346*d289c2baSAndroid Build Coastguard Worker         break;
347*d289c2baSAndroid Build Coastguard Worker       case AVB_HASHTREE_ERROR_MODE_LOGGING:
348*d289c2baSAndroid Build Coastguard Worker         verity_mode = "logging";
349*d289c2baSAndroid Build Coastguard Worker         dm_verity_mode = "ignore_corruption";
350*d289c2baSAndroid Build Coastguard Worker         break;
351*d289c2baSAndroid Build Coastguard Worker       case AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO:
352*d289c2baSAndroid Build Coastguard Worker         // Should never get here because MANAGED_RESTART_AND_EIO is
353*d289c2baSAndroid Build Coastguard Worker         // remapped by avb_manage_hashtree_error_mode().
354*d289c2baSAndroid Build Coastguard Worker         avb_assert_not_reached();
355*d289c2baSAndroid Build Coastguard Worker         break;
356*d289c2baSAndroid Build Coastguard Worker       case AVB_HASHTREE_ERROR_MODE_PANIC:
357*d289c2baSAndroid Build Coastguard Worker         verity_mode = "panicking";
358*d289c2baSAndroid Build Coastguard Worker         dm_verity_mode = "panic_on_corruption";
359*d289c2baSAndroid Build Coastguard Worker         break;
360*d289c2baSAndroid Build Coastguard Worker     }
361*d289c2baSAndroid Build Coastguard Worker     new_ret = avb_replace(
362*d289c2baSAndroid Build Coastguard Worker         slot_data->cmdline, "$(ANDROID_VERITY_MODE)", dm_verity_mode);
363*d289c2baSAndroid Build Coastguard Worker     avb_free(slot_data->cmdline);
364*d289c2baSAndroid Build Coastguard Worker     slot_data->cmdline = new_ret;
365*d289c2baSAndroid Build Coastguard Worker     if (slot_data->cmdline == NULL) {
366*d289c2baSAndroid Build Coastguard Worker       ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
367*d289c2baSAndroid Build Coastguard Worker       goto out;
368*d289c2baSAndroid Build Coastguard Worker     }
369*d289c2baSAndroid Build Coastguard Worker   }
370*d289c2baSAndroid Build Coastguard Worker   if (!cmdline_append_option(
371*d289c2baSAndroid Build Coastguard Worker           slot_data, "androidboot.veritymode", verity_mode)) {
372*d289c2baSAndroid Build Coastguard Worker     ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
373*d289c2baSAndroid Build Coastguard Worker     goto out;
374*d289c2baSAndroid Build Coastguard Worker   }
375*d289c2baSAndroid Build Coastguard Worker   if (hashtree_error_mode == AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO) {
376*d289c2baSAndroid Build Coastguard Worker     if (!cmdline_append_option(
377*d289c2baSAndroid Build Coastguard Worker             slot_data, "androidboot.veritymode.managed", "yes")) {
378*d289c2baSAndroid Build Coastguard Worker       ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
379*d289c2baSAndroid Build Coastguard Worker       goto out;
380*d289c2baSAndroid Build Coastguard Worker     }
381*d289c2baSAndroid Build Coastguard Worker   }
382*d289c2baSAndroid Build Coastguard Worker 
383*d289c2baSAndroid Build Coastguard Worker   ret = AVB_SLOT_VERIFY_RESULT_OK;
384*d289c2baSAndroid Build Coastguard Worker 
385*d289c2baSAndroid Build Coastguard Worker out:
386*d289c2baSAndroid Build Coastguard Worker 
387*d289c2baSAndroid Build Coastguard Worker   return ret;
388*d289c2baSAndroid Build Coastguard Worker }
389*d289c2baSAndroid Build Coastguard Worker 
avb_new_cmdline_subst_list()390*d289c2baSAndroid Build Coastguard Worker AvbCmdlineSubstList* avb_new_cmdline_subst_list() {
391*d289c2baSAndroid Build Coastguard Worker   return (AvbCmdlineSubstList*)avb_calloc(sizeof(AvbCmdlineSubstList));
392*d289c2baSAndroid Build Coastguard Worker }
393*d289c2baSAndroid Build Coastguard Worker 
avb_free_cmdline_subst_list(AvbCmdlineSubstList * cmdline_subst)394*d289c2baSAndroid Build Coastguard Worker void avb_free_cmdline_subst_list(AvbCmdlineSubstList* cmdline_subst) {
395*d289c2baSAndroid Build Coastguard Worker   size_t i;
396*d289c2baSAndroid Build Coastguard Worker   for (i = 0; i < cmdline_subst->size; ++i) {
397*d289c2baSAndroid Build Coastguard Worker     avb_free(cmdline_subst->tokens[i]);
398*d289c2baSAndroid Build Coastguard Worker     avb_free(cmdline_subst->values[i]);
399*d289c2baSAndroid Build Coastguard Worker   }
400*d289c2baSAndroid Build Coastguard Worker   cmdline_subst->size = 0;
401*d289c2baSAndroid Build Coastguard Worker   avb_free(cmdline_subst);
402*d289c2baSAndroid Build Coastguard Worker }
403*d289c2baSAndroid Build Coastguard Worker 
avb_add_root_digest_substitution(const char * part_name,const uint8_t * digest,size_t digest_size,AvbCmdlineSubstList * out_cmdline_subst)404*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyResult avb_add_root_digest_substitution(
405*d289c2baSAndroid Build Coastguard Worker     const char* part_name,
406*d289c2baSAndroid Build Coastguard Worker     const uint8_t* digest,
407*d289c2baSAndroid Build Coastguard Worker     size_t digest_size,
408*d289c2baSAndroid Build Coastguard Worker     AvbCmdlineSubstList* out_cmdline_subst) {
409*d289c2baSAndroid Build Coastguard Worker   const char* kDigestSubPrefix = "$(AVB_";
410*d289c2baSAndroid Build Coastguard Worker   const char* kDigestSubSuffix = "_ROOT_DIGEST)";
411*d289c2baSAndroid Build Coastguard Worker   size_t part_name_len = avb_strlen(part_name);
412*d289c2baSAndroid Build Coastguard Worker   size_t list_index = out_cmdline_subst->size;
413*d289c2baSAndroid Build Coastguard Worker 
414*d289c2baSAndroid Build Coastguard Worker   avb_assert(part_name_len < AVB_PART_NAME_MAX_SIZE);
415*d289c2baSAndroid Build Coastguard Worker   avb_assert(digest_size <= AVB_SHA512_DIGEST_SIZE);
416*d289c2baSAndroid Build Coastguard Worker   if (part_name_len >= AVB_PART_NAME_MAX_SIZE ||
417*d289c2baSAndroid Build Coastguard Worker       digest_size > AVB_SHA512_DIGEST_SIZE) {
418*d289c2baSAndroid Build Coastguard Worker     return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
419*d289c2baSAndroid Build Coastguard Worker   }
420*d289c2baSAndroid Build Coastguard Worker 
421*d289c2baSAndroid Build Coastguard Worker   if (out_cmdline_subst->size >= AVB_MAX_NUM_CMDLINE_SUBST) {
422*d289c2baSAndroid Build Coastguard Worker     /* The list is full. Currently dynamic growth of this list is not supported.
423*d289c2baSAndroid Build Coastguard Worker      */
424*d289c2baSAndroid Build Coastguard Worker     return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
425*d289c2baSAndroid Build Coastguard Worker   }
426*d289c2baSAndroid Build Coastguard Worker 
427*d289c2baSAndroid Build Coastguard Worker   /* Construct the token to replace in the command line based on the partition
428*d289c2baSAndroid Build Coastguard Worker    * name. For partition 'foo', this will be '$(AVB_FOO_ROOT_DIGEST)'.
429*d289c2baSAndroid Build Coastguard Worker    */
430*d289c2baSAndroid Build Coastguard Worker   out_cmdline_subst->tokens[list_index] =
431*d289c2baSAndroid Build Coastguard Worker       avb_strdupv(kDigestSubPrefix, part_name, kDigestSubSuffix, NULL);
432*d289c2baSAndroid Build Coastguard Worker   if (out_cmdline_subst->tokens[list_index] == NULL) {
433*d289c2baSAndroid Build Coastguard Worker     goto fail;
434*d289c2baSAndroid Build Coastguard Worker   }
435*d289c2baSAndroid Build Coastguard Worker   avb_uppercase(out_cmdline_subst->tokens[list_index]);
436*d289c2baSAndroid Build Coastguard Worker 
437*d289c2baSAndroid Build Coastguard Worker   /* The digest value is hex encoded when inserted in the command line. */
438*d289c2baSAndroid Build Coastguard Worker   out_cmdline_subst->values[list_index] = avb_bin2hex(digest, digest_size);
439*d289c2baSAndroid Build Coastguard Worker   if (out_cmdline_subst->values[list_index] == NULL) {
440*d289c2baSAndroid Build Coastguard Worker     goto fail;
441*d289c2baSAndroid Build Coastguard Worker   }
442*d289c2baSAndroid Build Coastguard Worker 
443*d289c2baSAndroid Build Coastguard Worker   out_cmdline_subst->size++;
444*d289c2baSAndroid Build Coastguard Worker   return AVB_SLOT_VERIFY_RESULT_OK;
445*d289c2baSAndroid Build Coastguard Worker 
446*d289c2baSAndroid Build Coastguard Worker fail:
447*d289c2baSAndroid Build Coastguard Worker   if (out_cmdline_subst->tokens[list_index]) {
448*d289c2baSAndroid Build Coastguard Worker     avb_free(out_cmdline_subst->tokens[list_index]);
449*d289c2baSAndroid Build Coastguard Worker   }
450*d289c2baSAndroid Build Coastguard Worker   if (out_cmdline_subst->values[list_index]) {
451*d289c2baSAndroid Build Coastguard Worker     avb_free(out_cmdline_subst->values[list_index]);
452*d289c2baSAndroid Build Coastguard Worker   }
453*d289c2baSAndroid Build Coastguard Worker   return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
454*d289c2baSAndroid Build Coastguard Worker }
455