xref: /rk3399_rockchip-uboot/lib/avb/libavb/avb_cmdline.c (revision d5f538dc02e53c7267fcd4a914104071fca889b5)
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #include <android_avb/avb_cmdline.h>
26 #include <android_avb/avb_sha.h>
27 #include <android_avb/avb_util.h>
28 #include <android_avb/avb_version.h>
29 
30 #define NUM_GUIDS 3
31 
32 /* Substitutes all variables (e.g. $(ANDROID_SYSTEM_PARTUUID)) with
33  * values. Returns NULL on OOM, otherwise the cmdline with values
34  * replaced.
35  */
36 char* avb_sub_cmdline(AvbOps* ops, const char* cmdline, const char* ab_suffix,
37                       bool using_boot_for_vbmeta) {
38   const char* part_name_str[NUM_GUIDS] = {"system", "boot", "vbmeta"};
39   const char* replace_str[NUM_GUIDS] = {"$(ANDROID_SYSTEM_PARTUUID)",
40                                         "$(ANDROID_BOOT_PARTUUID)",
41                                         "$(ANDROID_VBMETA_PARTUUID)"};
42   char* ret = NULL;
43   AvbIOResult io_ret;
44   size_t n;
45 
46   /* Special-case for when the top-level vbmeta struct is in the boot
47    * partition.
48    */
49   if (using_boot_for_vbmeta) {
50     part_name_str[2] = "boot";
51   }
52 
53   /* Replace unique partition GUIDs */
54   for (n = 0; n < NUM_GUIDS; n++) {
55     char part_name[AVB_PART_NAME_MAX_SIZE];
56     char guid_buf[37];
57 
58     if (!avb_str_concat(part_name,
59                         sizeof part_name,
60                         part_name_str[n],
61                         avb_strlen(part_name_str[n]),
62                         ab_suffix,
63                         avb_strlen(ab_suffix))) {
64       avb_error("Partition name and suffix does not fit.\n");
65       goto fail;
66     }
67 
68     io_ret = ops->get_unique_guid_for_partition(
69         ops, part_name, guid_buf, sizeof guid_buf);
70     if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
71       return NULL;
72     } else if (io_ret != AVB_IO_RESULT_OK) {
73       avb_error("Error getting unique GUID for partition.\n");
74       goto fail;
75     }
76 
77     if (ret == NULL) {
78       ret = avb_replace(cmdline, replace_str[n], guid_buf);
79     } else {
80       char* new_ret = avb_replace(ret, replace_str[n], guid_buf);
81       avb_free(ret);
82       ret = new_ret;
83     }
84     if (ret == NULL) {
85       goto fail;
86     }
87   }
88 
89   return ret;
90 
91 fail:
92   if (ret != NULL) {
93     avb_free(ret);
94   }
95   return NULL;
96 }
97 
98 static int cmdline_append_option(AvbSlotVerifyData* slot_data,
99                                  const char* key,
100                                  const char* value) {
101   size_t offset, key_len, value_len;
102   char* new_cmdline;
103 
104   key_len = avb_strlen(key);
105   value_len = avb_strlen(value);
106 
107   offset = 0;
108   if (slot_data->cmdline != NULL) {
109     offset = avb_strlen(slot_data->cmdline);
110     if (offset > 0) {
111       offset += 1;
112     }
113   }
114 
115   new_cmdline = avb_calloc(offset + key_len + value_len + 2);
116   if (new_cmdline == NULL) {
117     return 0;
118   }
119   if (offset > 0) {
120     avb_memcpy(new_cmdline, slot_data->cmdline, offset - 1);
121     new_cmdline[offset - 1] = ' ';
122   }
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);
128   }
129   slot_data->cmdline = new_cmdline;
130 
131   return 1;
132 }
133 
134 #define AVB_MAX_DIGITS_UINT64 32
135 
136 /* Writes |value| to |digits| in base 10 followed by a NUL byte.
137  * Returns number of characters written excluding the NUL byte.
138  */
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;
143 
144   for (num_digits = 0; num_digits < AVB_MAX_DIGITS_UINT64 - 1;) {
145     rev_digits[num_digits++] = avb_div_by_10(&value) + '0';
146     if (value == 0) {
147       break;
148     }
149   }
150 
151   for (n = 0; n < num_digits; n++) {
152     digits[n] = rev_digits[num_digits - 1 - n];
153   }
154   digits[n] = '\0';
155   return n;
156 }
157 
158 static int cmdline_append_version(AvbSlotVerifyData* slot_data,
159                                   const char* key,
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;
166 
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';
173 
174   return cmdline_append_option(slot_data, key, combined);
175 }
176 
177 static int cmdline_append_uint64_base10(AvbSlotVerifyData* slot_data,
178                                         const char* key,
179                                         uint64_t value) {
180   char digits[AVB_MAX_DIGITS_UINT64];
181   uint64_to_base10(value, digits);
182   return cmdline_append_option(slot_data, key, digits);
183 }
184 
185 static int cmdline_append_hex(AvbSlotVerifyData* slot_data,
186                               const char* key,
187                               const uint8_t* data,
188                               size_t data_len) {
189   char hex_digits[17] = "0123456789abcdef";
190   char* hex_data;
191   int ret;
192   size_t n;
193 
194   hex_data = avb_malloc(data_len * 2 + 1);
195   if (hex_data == NULL) {
196     return 0;
197   }
198 
199   for (n = 0; n < data_len; n++) {
200     hex_data[n * 2] = hex_digits[data[n] >> 4];
201     hex_data[n * 2 + 1] = hex_digits[data[n] & 0x0f];
202   }
203   hex_data[n * 2] = '\0';
204 
205   ret = cmdline_append_option(slot_data, key, hex_data);
206   avb_free(hex_data);
207   return ret;
208 }
209 
210 AvbSlotVerifyResult avb_append_options(
211     AvbOps* ops,
212     AvbSlotVerifyData* slot_data,
213     AvbVBMetaImageHeader* toplevel_vbmeta,
214     AvbAlgorithmType algorithm_type,
215     AvbHashtreeErrorMode hashtree_error_mode) {
216   AvbSlotVerifyResult ret;
217   const char* verity_mode = NULL;
218   bool is_device_unlocked;
219   AvbIOResult io_ret;
220 
221   /* Add androidboot.vbmeta.device option. */
222   if (!cmdline_append_option(slot_data,
223                              "androidboot.vbmeta.device",
224                              "PARTUUID=$(ANDROID_VBMETA_PARTUUID)")) {
225     ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
226     goto out;
227   }
228 
229   /* Add androidboot.vbmeta.avb_version option. */
230   if (!cmdline_append_version(slot_data,
231                               "androidboot.vbmeta.avb_version",
232                               AVB_VERSION_MAJOR,
233                               AVB_VERSION_MINOR)) {
234     ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
235     goto out;
236   }
237 
238   /* Set androidboot.avb.device_state to "locked" or "unlocked". */
239   io_ret = ops->read_is_device_unlocked(ops, &is_device_unlocked);
240   if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
241     ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
242     goto out;
243   } else if (io_ret != AVB_IO_RESULT_OK) {
244     avb_error("Error getting device state.\n");
245     ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
246     goto out;
247   }
248   if (!cmdline_append_option(slot_data,
249                              "androidboot.vbmeta.device_state",
250                              is_device_unlocked ? "unlocked" : "locked")) {
251     ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
252     goto out;
253   }
254 
255   /* Set androidboot.vbmeta.{hash_alg, size, digest} - use same hash
256    * function as is used to sign vbmeta.
257    */
258   switch (algorithm_type) {
259     /* Explicit fallthrough. */
260     case AVB_ALGORITHM_TYPE_NONE:
261     case AVB_ALGORITHM_TYPE_SHA256_RSA2048:
262     case AVB_ALGORITHM_TYPE_SHA256_RSA4096:
263     case AVB_ALGORITHM_TYPE_SHA256_RSA8192: {
264       AvbSHA256Ctx ctx;
265       size_t n, total_size = 0;
266       avb_sha256_init(&ctx);
267       for (n = 0; n < slot_data->num_vbmeta_images; n++) {
268         avb_sha256_update(&ctx,
269                           slot_data->vbmeta_images[n].vbmeta_data,
270                           slot_data->vbmeta_images[n].vbmeta_size);
271         total_size += slot_data->vbmeta_images[n].vbmeta_size;
272       }
273       if (!cmdline_append_option(
274               slot_data, "androidboot.vbmeta.hash_alg", "sha256") ||
275           !cmdline_append_uint64_base10(
276               slot_data, "androidboot.vbmeta.size", total_size) ||
277           !cmdline_append_hex(slot_data,
278                               "androidboot.vbmeta.digest",
279                               avb_sha256_final(&ctx),
280                               AVB_SHA256_DIGEST_SIZE)) {
281         ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
282         goto out;
283       }
284     } break;
285     /* Explicit fallthrough. */
286     case AVB_ALGORITHM_TYPE_SHA512_RSA2048:
287     case AVB_ALGORITHM_TYPE_SHA512_RSA4096:
288     case AVB_ALGORITHM_TYPE_SHA512_RSA8192: {
289       AvbSHA512Ctx ctx;
290       size_t n, total_size = 0;
291       avb_sha512_init(&ctx);
292       for (n = 0; n < slot_data->num_vbmeta_images; n++) {
293         avb_sha512_update(&ctx,
294                           slot_data->vbmeta_images[n].vbmeta_data,
295                           slot_data->vbmeta_images[n].vbmeta_size);
296         total_size += slot_data->vbmeta_images[n].vbmeta_size;
297       }
298       if (!cmdline_append_option(
299               slot_data, "androidboot.vbmeta.hash_alg", "sha512") ||
300           !cmdline_append_uint64_base10(
301               slot_data, "androidboot.vbmeta.size", total_size) ||
302           !cmdline_append_hex(slot_data,
303                               "androidboot.vbmeta.digest",
304                               avb_sha512_final(&ctx),
305                               AVB_SHA512_DIGEST_SIZE)) {
306         ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
307         goto out;
308       }
309     } break;
310     case _AVB_ALGORITHM_NUM_TYPES:
311       avb_assert_not_reached();
312       break;
313   }
314 
315   /* Set androidboot.veritymode and androidboot.vbmeta.invalidate_on_error */
316   if (toplevel_vbmeta->flags & AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED) {
317     verity_mode = "disabled";
318   } else {
319     const char* dm_verity_mode = NULL;
320     char* new_ret;
321 
322     switch (hashtree_error_mode) {
323       case AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE:
324         if (!cmdline_append_option(
325                 slot_data, "androidboot.vbmeta.invalidate_on_error", "yes")) {
326           ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
327           goto out;
328         }
329         verity_mode = "enforcing";
330         dm_verity_mode = "restart_on_corruption";
331         break;
332       case AVB_HASHTREE_ERROR_MODE_RESTART:
333         verity_mode = "enforcing";
334         dm_verity_mode = "restart_on_corruption";
335         break;
336       case AVB_HASHTREE_ERROR_MODE_EIO:
337         verity_mode = "eio";
338         /* For now there's no option to specify the EIO mode. So
339          * just use 'ignore_zero_blocks' since that's already set
340          * and dm-verity-target.c supports specifying this multiple
341          * times.
342          */
343         dm_verity_mode = "ignore_zero_blocks";
344         break;
345       case AVB_HASHTREE_ERROR_MODE_LOGGING:
346         verity_mode = "logging";
347         dm_verity_mode = "ignore_corruption";
348         break;
349     }
350     new_ret = avb_replace(
351         slot_data->cmdline, "$(ANDROID_VERITY_MODE)", dm_verity_mode);
352     avb_free(slot_data->cmdline);
353     slot_data->cmdline = new_ret;
354     if (slot_data->cmdline == NULL) {
355       ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
356       goto out;
357     }
358   }
359   if (!cmdline_append_option(
360           slot_data, "androidboot.veritymode", verity_mode)) {
361     ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
362     goto out;
363   }
364 
365   ret = AVB_SLOT_VERIFY_RESULT_OK;
366 
367 out:
368 
369   return ret;
370 }
371 
372