Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: (GPL-2.0+ OR MIT) |
| 2 | /* |
| 3 | * Copyright (c) 2019 Amlogic, Inc. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | #include <config.h> |
| 7 | #include <common.h> |
| 8 | #include <asm/amlogic/arch/io.h> |
| 9 | #include <command.h> |
| 10 | #include <malloc.h> |
| 11 | #include <tee.h> |
| 12 | #include <mmc.h> |
| 13 | #include <amlogic/storage.h> |
| 14 | |
| 15 | #define TEE_ERROR_RPMB_AUTH_KEY_PROGRAMMED 0x7FFFFFFD |
| 16 | |
| 17 | #define CMD_RPMB_AUTH_KEY_INIT 0 |
| 18 | #define CMD_RPMB_AUTH_KEY_STATE 1 |
| 19 | |
| 20 | #define TA_RPMB_UUID { 0x0ab5a718, 0xee63, 0x4115, \ |
| 21 | { 0xa0, 0xad, 0xef, 0x6e, 0xe9, 0x8f, 0xcb, 0xc7 } } |
| 22 | |
| 23 | static int do_rpmb_init(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) |
| 24 | { |
| 25 | int ret = 0; |
| 26 | struct udevice *dev; |
| 27 | struct tee_open_session_arg open_arg; |
| 28 | struct tee_invoke_arg invoke_arg; |
| 29 | const struct tee_optee_ta_uuid uuid = TA_RPMB_UUID; |
| 30 | |
| 31 | dev = tee_find_device(NULL, NULL, NULL, NULL); |
| 32 | if (!dev) { |
| 33 | printf("tee_find_device() failed"); |
| 34 | return -ENODEV; |
| 35 | } |
| 36 | |
| 37 | memset(&open_arg, 0, sizeof(open_arg)); |
| 38 | tee_optee_ta_uuid_to_octets(open_arg.uuid, &uuid); |
| 39 | ret = tee_open_session(dev, &open_arg, 0, NULL); |
| 40 | if (ret) { |
| 41 | printf("tee_open_session() failed, ret = 0x%x\n", ret); |
| 42 | return ret; |
| 43 | } |
| 44 | if (open_arg.ret) { |
| 45 | printf("tee_open_session() failed, ret = 0x%x, ret_origin=0x%x \n", |
| 46 | open_arg.ret, open_arg.ret_origin); |
| 47 | return open_arg.ret; |
| 48 | } |
| 49 | |
| 50 | memset(&invoke_arg, 0, sizeof(invoke_arg)); |
| 51 | invoke_arg.session = open_arg.session; |
| 52 | invoke_arg.func = CMD_RPMB_AUTH_KEY_INIT; |
| 53 | |
| 54 | ret = tee_invoke_func(dev, &invoke_arg, 0, NULL); |
| 55 | if (ret) { |
| 56 | printf("tee_invoke_func() failed, ret = 0x%x\n", ret); |
| 57 | goto exit; |
| 58 | } |
| 59 | if (invoke_arg.ret == TEE_SUCCESS) { |
| 60 | printf("program RPMB auth key success\n"); |
| 61 | } else if (invoke_arg.ret == TEE_ERROR_RPMB_AUTH_KEY_PROGRAMMED) { |
| 62 | printf("RPMB auth key is programmed already\n"); |
| 63 | } else { |
| 64 | printf("tee_invoke_func() failed, ret = 0x%x, origin = %d\n", |
| 65 | invoke_arg.ret, invoke_arg.ret_origin); |
| 66 | ret = invoke_arg.ret; |
| 67 | goto exit; |
| 68 | } |
| 69 | |
| 70 | exit: |
| 71 | tee_close_session(dev, open_arg.session); |
| 72 | |
| 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | U_BOOT_CMD(rpmb_init, CONFIG_SYS_MAXARGS, 0, do_rpmb_init, |
| 77 | "RPMB sub-system", |
| 78 | "RPMB auth key init\n"); |
| 79 | |
| 80 | static int do_rpmb_state(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) |
| 81 | { |
| 82 | int ret = 0; |
| 83 | struct udevice *dev; |
| 84 | struct tee_open_session_arg open_arg; |
| 85 | struct tee_invoke_arg invoke_arg; |
| 86 | const struct tee_optee_ta_uuid uuid = TA_RPMB_UUID; |
| 87 | struct tee_param param = { .attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT }; |
| 88 | char *parg = NULL; |
| 89 | struct mmc *mmc = NULL; |
| 90 | |
| 91 | if (store_get_type() == BOOT_EMMC) |
| 92 | mmc = find_mmc_device(1); |
| 93 | |
| 94 | if (!mmc) |
| 95 | return -ENODEV; |
| 96 | |
| 97 | dev = tee_find_device(NULL, NULL, NULL, NULL); |
| 98 | if (!dev) { |
| 99 | printf("tee_find_device() failed"); |
| 100 | return -ENODEV; |
| 101 | } |
| 102 | |
| 103 | memset(&open_arg, 0, sizeof(open_arg)); |
| 104 | tee_optee_ta_uuid_to_octets(open_arg.uuid, &uuid); |
| 105 | ret = tee_open_session(dev, &open_arg, 0, NULL); |
| 106 | if (ret) { |
| 107 | printf("tee_open_session() failed, ret = 0x%x\n", ret); |
| 108 | return ret; |
| 109 | } |
| 110 | if (open_arg.ret) { |
| 111 | printf("tee_open_session() failed, ret = 0x%x, ret_origin=0x%x\n", |
| 112 | open_arg.ret, open_arg.ret_origin); |
| 113 | return open_arg.ret; |
| 114 | } |
| 115 | |
| 116 | memset(&invoke_arg, 0, sizeof(invoke_arg)); |
| 117 | invoke_arg.session = open_arg.session; |
| 118 | invoke_arg.func = CMD_RPMB_AUTH_KEY_STATE; |
| 119 | |
| 120 | ret = tee_invoke_func(dev, &invoke_arg, 1, ¶m); |
| 121 | if (ret) { |
| 122 | printf("tee_invoke_func() failed, ret = 0x%x\n", ret); |
| 123 | goto exit; |
| 124 | } |
| 125 | if (invoke_arg.ret) { |
| 126 | printf("tee_invoke_func() failed, ret = 0x%x, origin = %d\n", |
| 127 | invoke_arg.ret, invoke_arg.ret_origin); |
| 128 | ret = invoke_arg.ret; |
| 129 | goto exit; |
| 130 | } |
| 131 | env_set("rpmb_state", param.u.value.a ? "1" : "0"); //need this? |
| 132 | |
| 133 | parg = env_get("bootconfig"); |
hao.qi | c0d6b84 | 2024-07-03 16:19:31 +0800 | [diff] [blame] | 134 | if (!parg) |
| 135 | parg = env_get("bootargs"); |
| 136 | else |
| 137 | env_set("use_bootconfig", "true"); |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 138 | if (parg) |
| 139 | { |
| 140 | char *buff =malloc(strlen(parg) + 64); |
| 141 | if (!buff) { |
| 142 | ret = -ENOMEM; |
| 143 | goto exit; |
| 144 | } |
| 145 | |
| 146 | memset(buff + strlen(parg), 0, 64); |
| 147 | strcpy(buff, parg); |
| 148 | char *find = strstr(buff, "androidboot.rpmb_state"); |
| 149 | if (!find) |
| 150 | sprintf(buff,"%s androidboot.rpmb_state=0x%llx", parg, param.u.value.a); |
| 151 | else |
| 152 | find[23] = param.u.value.a ? '1':'0'; |
| 153 | |
| 154 | //printf("2 bootargs=%s\n",buff); |
| 155 | |
| 156 | env_set("rpmb_state", param.u.value.a?"1":"0"); //need this? |
hao.qi | c0d6b84 | 2024-07-03 16:19:31 +0800 | [diff] [blame] | 157 | char *use_bootconfig = env_get("use_bootconfig"); |
| 158 | |
| 159 | if (use_bootconfig && (strcmp(use_bootconfig, "true") == 0)) |
| 160 | env_set("bootconfig", buff); |
| 161 | else |
| 162 | env_set("bootargs", buff); |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 163 | free(buff); |
| 164 | buff = NULL; |
| 165 | } |
| 166 | |
| 167 | exit: |
| 168 | tee_close_session(dev, open_arg.session); |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | U_BOOT_CMD(rpmb_state, CONFIG_SYS_MAXARGS, 0, do_rpmb_state, |
| 174 | "RPMB sub-system", |
| 175 | "RPMB state\n"); |
| 176 | |
| 177 | |
| 178 | /****************************************************/ |