ARM: renesas: Drop unused mmc.h
[pandora-u-boot.git] / common / avb_verify.c
index f8c6ae5..cff9117 100644 (file)
@@ -1,15 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2018, Linaro Limited
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <avb_verify.h>
 #include <blk.h>
-#include <fastboot.h>
+#include <cpu_func.h>
 #include <image.h>
 #include <malloc.h>
 #include <part.h>
+#include <tee.h>
+#include <tee/optee_ta_avb.h>
 
 static const unsigned char avb_root_pub[1032] = {
        0x0, 0x0, 0x10, 0x0, 0x55, 0xd9, 0x4, 0xad, 0xd8, 0x4,
@@ -118,6 +119,55 @@ static const unsigned char avb_root_pub[1032] = {
        0xd8, 0x7e,
 };
 
+const char *str_avb_io_error(AvbIOResult res)
+{
+       switch (res) {
+       case AVB_IO_RESULT_OK:
+               return "Requested operation was successful";
+       case AVB_IO_RESULT_ERROR_IO:
+               return "Underlying hardware encountered an I/O error";
+       case AVB_IO_RESULT_ERROR_OOM:
+               return "Unable to allocate memory";
+       case AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION:
+               return "Requested partition does not exist";
+       case AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION:
+               return "Bytes requested is outside the range of partition";
+       case AVB_IO_RESULT_ERROR_NO_SUCH_VALUE:
+               return "Named persistent value does not exist";
+       case AVB_IO_RESULT_ERROR_INVALID_VALUE_SIZE:
+               return "Named persistent value size is not supported";
+       case AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE:
+               return "Buffer is too small for the requested operation";
+       default:
+               return "Unknown AVB error";
+       }
+}
+
+const char *str_avb_slot_error(AvbSlotVerifyResult res)
+{
+       switch (res) {
+       case AVB_SLOT_VERIFY_RESULT_OK:
+               return "Verification passed successfully";
+       case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
+               return "Allocation of memory failed";
+       case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
+               return "I/O error occurred while trying to load data";
+       case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
+               return "Digest didn't match or signature checks failed";
+       case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
+               return "Rollback index is less than its stored value";
+       case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
+               return "Public keys are not accepted";
+       case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
+               return "Metadata is invalid or inconsistent";
+       case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
+               return "Metadata requires a newer version of libavb";
+       case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT:
+               return "Invalid arguments are used";
+       default:
+               return "Unknown AVB slot verification error";
+       }
+}
 /**
  * ============================================================================
  * Boot states support (GREEN, YELLOW, ORANGE, RED) and dm_verity
@@ -278,9 +328,9 @@ static unsigned long mmc_read_and_flush(struct mmc_part *part,
         * Reading fails on unaligned buffers, so we have to
         * use aligned temporary buffer and then copy to destination
         */
-
        if (unaligned) {
-               printf("Handling unaligned read buffer..\n");
+               debug("%s: handling unaligned read buffer, addr = 0x%p\n",
+                     __func__, buffer);
                tmp_buf = get_sector_buf();
                buf_size = get_sector_buf_size();
                if (sectors > buf_size / part->info.blksz)
@@ -319,7 +369,8 @@ static unsigned long mmc_write(struct mmc_part *part, lbaint_t start,
        if (unaligned) {
                tmp_buf = get_sector_buf();
                buf_size = get_sector_buf_size();
-               printf("Handling unaligned wrire buffer..\n");
+               debug("%s: handling unaligned read buffer, addr = 0x%p\n",
+                     __func__, buffer);
                if (sectors > buf_size / part->info.blksz)
                        sectors = buf_size / part->info.blksz;
 
@@ -347,28 +398,35 @@ static struct mmc_part *get_partition(AvbOps *ops, const char *partition)
        dev_num = get_boot_device(ops);
        part->mmc = find_mmc_device(dev_num);
        if (!part->mmc) {
-               printf("No MMC device at slot %x\n", dev_num);
+               printf("%s: no MMC device at slot %x\n", __func__, dev_num);
                goto err;
        }
 
-       if (mmc_init(part->mmc)) {
-               printf("MMC initialization failed\n");
+       ret = mmc_init(part->mmc);
+       if (ret) {
+               printf("%s: MMC initialization failed, err = %d\n",
+                      __func__, ret);
                goto err;
        }
 
-       ret = mmc_switch_part(part->mmc, part_num);
-       if (ret)
-               goto err;
+       if (IS_MMC(part->mmc)) {
+               ret = mmc_switch_part(part->mmc, part_num);
+               if (ret) {
+                       printf("%s: MMC part switch failed, err = %d\n",
+                              __func__, ret);
+                       goto err;
+               }
+       }
 
        mmc_blk = mmc_get_blk_desc(part->mmc);
        if (!mmc_blk) {
-               printf("Error - failed to obtain block descriptor\n");
+               printf("%s: failed to obtain block descriptor\n", __func__);
                goto err;
        }
 
        ret = part_get_info_by_name(mmc_blk, partition, &part->info);
-       if (!ret) {
-               printf("Can't find partition '%s'\n", partition);
+       if (ret < 0) {
+               printf("%s: can't find partition '%s'\n", __func__, partition);
                goto err;
        }
 
@@ -600,6 +658,70 @@ static AvbIOResult validate_vbmeta_public_key(AvbOps *ops,
        return AVB_IO_RESULT_OK;
 }
 
+#ifdef CONFIG_OPTEE_TA_AVB
+static int get_open_session(struct AvbOpsData *ops_data)
+{
+       struct udevice *tee = NULL;
+
+       while (!ops_data->tee) {
+               const struct tee_optee_ta_uuid uuid = TA_AVB_UUID;
+               struct tee_open_session_arg arg;
+               int rc;
+
+               tee = tee_find_device(tee, NULL, NULL, NULL);
+               if (!tee)
+                       return -ENODEV;
+
+               memset(&arg, 0, sizeof(arg));
+               tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
+               rc = tee_open_session(tee, &arg, 0, NULL);
+               if (rc || arg.ret)
+                       continue;
+
+               ops_data->tee = tee;
+               ops_data->session = arg.session;
+       }
+
+       return 0;
+}
+
+static AvbIOResult invoke_func(struct AvbOpsData *ops_data, u32 func,
+                              ulong num_param, struct tee_param *param)
+{
+       struct tee_invoke_arg arg;
+
+       if (get_open_session(ops_data))
+               return AVB_IO_RESULT_ERROR_IO;
+
+       memset(&arg, 0, sizeof(arg));
+       arg.func = func;
+       arg.session = ops_data->session;
+
+       if (tee_invoke_func(ops_data->tee, &arg, num_param, param))
+               return AVB_IO_RESULT_ERROR_IO;
+       switch (arg.ret) {
+       case TEE_SUCCESS:
+               return AVB_IO_RESULT_OK;
+       case TEE_ERROR_OUT_OF_MEMORY:
+               return AVB_IO_RESULT_ERROR_OOM;
+       case TEE_ERROR_STORAGE_NO_SPACE:
+               return AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE;
+       case TEE_ERROR_ITEM_NOT_FOUND:
+               return AVB_IO_RESULT_ERROR_NO_SUCH_VALUE;
+       case TEE_ERROR_TARGET_DEAD:
+               /*
+                * The TA has paniced, close the session to reload the TA
+                * for the next request.
+                */
+               tee_close_session(ops_data->tee, ops_data->session);
+               ops_data->tee = NULL;
+               return AVB_IO_RESULT_ERROR_IO;
+       default:
+               return AVB_IO_RESULT_ERROR_IO;
+       }
+}
+#endif
+
 /**
  * read_rollback_index() - gets the rollback index corresponding to the
  * location of given by @out_rollback_index.
@@ -615,13 +737,35 @@ static AvbIOResult read_rollback_index(AvbOps *ops,
                                       size_t rollback_index_slot,
                                       u64 *out_rollback_index)
 {
+#ifndef CONFIG_OPTEE_TA_AVB
        /* For now we always return 0 as the stored rollback index. */
-       printf("%s not supported yet\n", __func__);
+       debug("%s: rollback protection is not implemented\n", __func__);
 
        if (out_rollback_index)
                *out_rollback_index = 0;
 
        return AVB_IO_RESULT_OK;
+#else
+       AvbIOResult rc;
+       struct tee_param param[2];
+
+       if (rollback_index_slot >= TA_AVB_MAX_ROLLBACK_LOCATIONS)
+               return AVB_IO_RESULT_ERROR_NO_SUCH_VALUE;
+
+       memset(param, 0, sizeof(param));
+       param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
+       param[0].u.value.a = rollback_index_slot;
+       param[1].attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT;
+
+       rc = invoke_func(ops->user_data, TA_AVB_CMD_READ_ROLLBACK_INDEX,
+                        ARRAY_SIZE(param), param);
+       if (rc)
+               return rc;
+
+       *out_rollback_index = (u64)param[1].u.value.a << 32 |
+                             (u32)param[1].u.value.b;
+       return AVB_IO_RESULT_OK;
+#endif
 }
 
 /**
@@ -639,10 +783,27 @@ static AvbIOResult write_rollback_index(AvbOps *ops,
                                        size_t rollback_index_slot,
                                        u64 rollback_index)
 {
+#ifndef CONFIG_OPTEE_TA_AVB
        /* For now this is a no-op. */
-       printf("%s not supported yet\n", __func__);
+       debug("%s: rollback protection is not implemented\n", __func__);
 
        return AVB_IO_RESULT_OK;
+#else
+       struct tee_param param[2];
+
+       if (rollback_index_slot >= TA_AVB_MAX_ROLLBACK_LOCATIONS)
+               return AVB_IO_RESULT_ERROR_NO_SUCH_VALUE;
+
+       memset(param, 0, sizeof(param));
+       param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
+       param[0].u.value.a = rollback_index_slot;
+       param[1].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
+       param[1].u.value.a = (u32)(rollback_index >> 32);
+       param[1].u.value.b = (u32)rollback_index;
+
+       return invoke_func(ops->user_data, TA_AVB_CMD_WRITE_ROLLBACK_INDEX,
+                          ARRAY_SIZE(param), param);
+#endif
 }
 
 /**
@@ -658,13 +819,23 @@ static AvbIOResult write_rollback_index(AvbOps *ops,
  */
 static AvbIOResult read_is_device_unlocked(AvbOps *ops, bool *out_is_unlocked)
 {
+#ifndef CONFIG_OPTEE_TA_AVB
        /* For now we always return that the device is unlocked. */
-
-       printf("%s not supported yet\n", __func__);
+       debug("%s: device locking is not implemented\n", __func__);
 
        *out_is_unlocked = true;
 
        return AVB_IO_RESULT_OK;
+#else
+       AvbIOResult rc;
+       struct tee_param param = { .attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT };
+
+       rc = invoke_func(ops->user_data, TA_AVB_CMD_READ_LOCK_STATE, 1, &param);
+       if (rc)
+               return rc;
+       *out_is_unlocked = !param.u.value.a;
+       return AVB_IO_RESULT_OK;
+#endif
 }
 
 /**
@@ -736,6 +907,126 @@ static AvbIOResult get_size_of_partition(AvbOps *ops,
        return AVB_IO_RESULT_OK;
 }
 
+#ifdef CONFIG_OPTEE_TA_AVB
+static AvbIOResult read_persistent_value(AvbOps *ops,
+                                        const char *name,
+                                        size_t buffer_size,
+                                        u8 *out_buffer,
+                                        size_t *out_num_bytes_read)
+{
+       AvbIOResult rc;
+       struct tee_shm *shm_name;
+       struct tee_shm *shm_buf;
+       struct tee_param param[2];
+       struct udevice *tee;
+       size_t name_size = strlen(name) + 1;
+
+       if (get_open_session(ops->user_data))
+               return AVB_IO_RESULT_ERROR_IO;
+
+       tee = ((struct AvbOpsData *)ops->user_data)->tee;
+
+       rc = tee_shm_alloc(tee, name_size,
+                          TEE_SHM_ALLOC, &shm_name);
+       if (rc)
+               return AVB_IO_RESULT_ERROR_OOM;
+
+       rc = tee_shm_alloc(tee, buffer_size,
+                          TEE_SHM_ALLOC, &shm_buf);
+       if (rc) {
+               rc = AVB_IO_RESULT_ERROR_OOM;
+               goto free_name;
+       }
+
+       memcpy(shm_name->addr, name, name_size);
+
+       memset(param, 0, sizeof(param));
+       param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
+       param[0].u.memref.shm = shm_name;
+       param[0].u.memref.size = name_size;
+       param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT;
+       param[1].u.memref.shm = shm_buf;
+       param[1].u.memref.size = buffer_size;
+
+       rc = invoke_func(ops->user_data, TA_AVB_CMD_READ_PERSIST_VALUE,
+                        2, param);
+       if (rc)
+               goto out;
+
+       if (param[1].u.memref.size > buffer_size) {
+               rc = AVB_IO_RESULT_ERROR_NO_SUCH_VALUE;
+               goto out;
+       }
+
+       *out_num_bytes_read = param[1].u.memref.size;
+
+       memcpy(out_buffer, shm_buf->addr, *out_num_bytes_read);
+
+out:
+       tee_shm_free(shm_buf);
+free_name:
+       tee_shm_free(shm_name);
+
+       return rc;
+}
+
+static AvbIOResult write_persistent_value(AvbOps *ops,
+                                         const char *name,
+                                         size_t value_size,
+                                         const u8 *value)
+{
+       AvbIOResult rc;
+       struct tee_shm *shm_name;
+       struct tee_shm *shm_buf;
+       struct tee_param param[2];
+       struct udevice *tee;
+       size_t name_size = strlen(name) + 1;
+
+       if (get_open_session(ops->user_data))
+               return AVB_IO_RESULT_ERROR_IO;
+
+       tee = ((struct AvbOpsData *)ops->user_data)->tee;
+
+       if (!value_size)
+               return AVB_IO_RESULT_ERROR_NO_SUCH_VALUE;
+
+       rc = tee_shm_alloc(tee, name_size,
+                          TEE_SHM_ALLOC, &shm_name);
+       if (rc)
+               return AVB_IO_RESULT_ERROR_OOM;
+
+       rc = tee_shm_alloc(tee, value_size,
+                          TEE_SHM_ALLOC, &shm_buf);
+       if (rc) {
+               rc = AVB_IO_RESULT_ERROR_OOM;
+               goto free_name;
+       }
+
+       memcpy(shm_name->addr, name, name_size);
+       memcpy(shm_buf->addr, value, value_size);
+
+       memset(param, 0, sizeof(param));
+       param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
+       param[0].u.memref.shm = shm_name;
+       param[0].u.memref.size = name_size;
+       param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
+       param[1].u.memref.shm = shm_buf;
+       param[1].u.memref.size = value_size;
+
+       rc = invoke_func(ops->user_data, TA_AVB_CMD_WRITE_PERSIST_VALUE,
+                        2, param);
+       if (rc)
+               goto out;
+
+out:
+       tee_shm_free(shm_buf);
+free_name:
+       tee_shm_free(shm_name);
+
+       return rc;
+}
+#endif
+
 /**
  * ============================================================================
  * AVB2.0 AvbOps alloc/initialisation/free
@@ -759,6 +1050,10 @@ AvbOps *avb_ops_alloc(int boot_device)
        ops_data->ops.read_is_device_unlocked = read_is_device_unlocked;
        ops_data->ops.get_unique_guid_for_partition =
                get_unique_guid_for_partition;
+#ifdef CONFIG_OPTEE_TA_AVB
+       ops_data->ops.write_persistent_value = write_persistent_value;
+       ops_data->ops.read_persistent_value = read_persistent_value;
+#endif
        ops_data->ops.get_size_of_partition = get_size_of_partition;
        ops_data->mmc_dev = boot_device;
 
@@ -774,6 +1069,11 @@ void avb_ops_free(AvbOps *ops)
 
        ops_data = ops->user_data;
 
-       if (ops_data)
+       if (ops_data) {
+#ifdef CONFIG_OPTEE_TA_AVB
+               if (ops_data->tee)
+                       tee_close_session(ops_data->tee, ops_data->session);
+#endif
                avb_free(ops_data);
+       }
 }