1 /* 2 * Copyright 2023, Rockchip Electronics Co., Ltd 3 * hisping lin, <hisping.lin@rock-chips.com> 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 #include <stdlib.h> 8 #include <boot_rkimg.h> 9 #include <command.h> 10 #include <common.h> 11 #include <mmc.h> 12 #include <optee_include/OpteeClientLoadTa.h> 13 14 int is_uuid_equal(TEEC_UUID uuid1, TEEC_UUID uuid2) 15 { 16 bool a, b, c; 17 18 a = (uuid1.timeLow == uuid2.timeLow); 19 b = (uuid1.timeMid == uuid2.timeMid); 20 c = (uuid1.timeHiAndVersion == uuid2.timeHiAndVersion); 21 if ((a & b & c) == 0) { 22 return 0; 23 } else { 24 if (memcmp(uuid1.clockSeqAndNode, 25 uuid2.clockSeqAndNode, 8) == 0) { 26 return 1; 27 } else { 28 return 0; 29 } 30 } 31 } 32 33 void tee_uuid_from_octets(TEEC_UUID *d, const uint8_t *s) 34 { 35 d->timeLow = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; 36 d->timeMid = (s[4] << 8) | s[5]; 37 d->timeHiAndVersion = (s[6] << 8) | s[7]; 38 memcpy(d->clockSeqAndNode, s + 8, sizeof(d->clockSeqAndNode)); 39 } 40 41 static struct blk_desc *dev_desc; 42 static disk_partition_t part_info; 43 int search_ta(void *uuid_octets, void *ta, size_t *ta_size) 44 { 45 char fname[255]; 46 unsigned long ret = 0; 47 TEEC_UUID uuid; 48 TEEC_UUID ta_uuid; 49 uint8_t *userta; 50 struct userta_header *header; 51 struct userta_item *item; 52 int res; 53 54 if (!uuid_octets || !ta_size) { 55 printf("TEEC: wrong parameter to search_ta\n"); 56 return -1; 57 } 58 59 if (!dev_desc) { 60 dev_desc = rockchip_get_bootdev(); 61 if (!dev_desc) { 62 printf("TEEC: %s: Could not find device\n", __func__); 63 return -1; 64 } 65 66 if (part_get_info_by_name(dev_desc, 67 "userta", &part_info) < 0) { 68 dev_desc = NULL; 69 printf("TEEC: Could not find userta partition\n"); 70 return -1; 71 } 72 } 73 tee_uuid_from_octets(&uuid, uuid_octets); 74 snprintf(fname, 255, 75 "%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x.ta", 76 uuid.timeLow, 77 uuid.timeMid, 78 uuid.timeHiAndVersion, 79 uuid.clockSeqAndNode[0], 80 uuid.clockSeqAndNode[1], 81 uuid.clockSeqAndNode[2], 82 uuid.clockSeqAndNode[3], 83 uuid.clockSeqAndNode[4], 84 uuid.clockSeqAndNode[5], 85 uuid.clockSeqAndNode[6], 86 uuid.clockSeqAndNode[7]); 87 88 printf("Attempt to load %s \n", fname); 89 90 userta = (uint8_t *)memalign(CONFIG_SYS_CACHELINE_SIZE, part_info.size * part_info.blksz); 91 if (!userta) { 92 printf("TEEC: Malloc failed!\n"); 93 res = -1; 94 goto exit; 95 } 96 97 ret = blk_dread(dev_desc, part_info.start, part_info.size, userta); 98 if (ret != part_info.size) { 99 printf("TEEC: blk_dread fail\n"); 100 res = -1; 101 goto exit; 102 } 103 104 header = (struct userta_header *)userta; 105 if (header->magic != 0x524B5441 || header->img_ver != 1) { 106 printf("TEEC: userta_header format error! \n"); 107 res = -1; 108 goto exit; 109 } 110 111 item = (struct userta_item *)(header + 1); 112 113 for (int i = 0; i < header->ta_num; i++) { 114 tee_uuid_from_octets(&ta_uuid, item->ta_uuid); 115 snprintf(fname, 255, 116 "%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x.ta", 117 ta_uuid.timeLow, 118 ta_uuid.timeMid, 119 ta_uuid.timeHiAndVersion, 120 ta_uuid.clockSeqAndNode[0], 121 ta_uuid.clockSeqAndNode[1], 122 ta_uuid.clockSeqAndNode[2], 123 ta_uuid.clockSeqAndNode[3], 124 ta_uuid.clockSeqAndNode[4], 125 ta_uuid.clockSeqAndNode[5], 126 ta_uuid.clockSeqAndNode[6], 127 ta_uuid.clockSeqAndNode[7]); 128 debug("search TA %s \n", fname); 129 debug("item->ta_offset=0x%x item->ta_len=0x%x *ta_size=%zu\n", 130 item->ta_offset, item->ta_len, *ta_size); 131 132 if (is_uuid_equal(ta_uuid, uuid) && item->ta_ver == 2) { 133 if (item->ta_len <= *ta_size && ta) 134 memcpy(ta, userta + item->ta_offset, item->ta_len); 135 *ta_size = item->ta_len; 136 res = TA_BINARY_FOUND; 137 goto exit; 138 } else { 139 item++; 140 } 141 } 142 res = TA_BINARY_NOT_FOUND; 143 144 exit: 145 if (userta) 146 free(userta); 147 return res; 148 } 149