1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #ifndef __USER__
10 #include "mmc_emmc.h"
11 #endif
12 #include "emmc_if.h"
13 #ifdef __USER__
14 #include "hdf_io_service_if.h"
15 #endif
16 #include "hdf_log.h"
17 #include "securec.h"
18
19 #define HDF_LOG_TAG emmc_if_c
20
21 #ifdef __USER__
EmmcGetCidReadReplyData(struct HdfSBuf * reply,uint8_t * cid,uint32_t size)22 static int32_t EmmcGetCidReadReplyData(struct HdfSBuf *reply, uint8_t *cid, uint32_t size)
23 {
24 uint32_t rLen;
25 const void *rBuf = NULL;
26
27 if (HdfSbufReadBuffer(reply, &rBuf, &rLen) == false) {
28 HDF_LOGE("EmmcGetCidReadReplyData: read rBuf fail!");
29 return HDF_ERR_IO;
30 }
31 if (size != rLen) {
32 HDF_LOGE("EmmcGetCidReadReplyData: err len:%u, rLen:%u", size, rLen);
33 if (rLen > size) {
34 rLen = size;
35 }
36 }
37
38 if (memcpy_s(cid, size, rBuf, rLen) != EOK) {
39 HDF_LOGE("EmmcGetCidReadReplyData: memcpy rBuf fail!");
40 return HDF_ERR_IO;
41 }
42 return HDF_SUCCESS;
43 }
44
EmmcServiceGetCid(struct HdfIoService * service,uint8_t * cid,uint32_t size)45 int32_t EmmcServiceGetCid(struct HdfIoService *service, uint8_t *cid, uint32_t size)
46 {
47 int32_t ret;
48 struct HdfSBuf *reply = NULL;
49
50 reply = HdfSbufObtainDefaultSize();
51 if (reply == NULL) {
52 HDF_LOGE("EmmcServiceGetCid: failed to obtain reply!");
53 ret = HDF_ERR_MALLOC_FAIL;
54 goto __EXIT;
55 }
56
57 if (service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
58 HDF_LOGE("EmmcServiceGetCid: dispatcher or Dispatch is NULL!");
59 ret = HDF_ERR_NOT_SUPPORT;
60 goto __EXIT;
61 }
62
63 ret = service->dispatcher->Dispatch(&service->object, EMMC_CMD_GET_CID, NULL, reply);
64 if (ret != HDF_SUCCESS) {
65 HDF_LOGE("EmmcServiceGetCid: failed to send service call:%d", ret);
66 goto __EXIT;
67 }
68
69 ret = EmmcGetCidReadReplyData(reply, cid, size);
70 if (ret != HDF_SUCCESS) {
71 goto __EXIT;
72 }
73 HDF_LOGD("EmmcServiceGetCid: success");
74
75 __EXIT:
76 if (reply != NULL) {
77 HdfSbufRecycle(reply);
78 }
79 return ret;
80 }
81
82 #else
EmmcDeviceGetFromHandle(DevHandle handle,struct EmmcDevice ** emmc)83 static int32_t EmmcDeviceGetFromHandle(DevHandle handle, struct EmmcDevice **emmc)
84 {
85 struct MmcDevice *mmc = NULL;
86
87 if (handle == NULL) {
88 return HDF_ERR_INVALID_OBJECT;
89 }
90
91 if (emmc == NULL) {
92 return HDF_ERR_INVALID_PARAM;
93 }
94
95 mmc = MmcCntlrGetDevice((struct MmcCntlr *)handle);
96 if (mmc == NULL) {
97 return HDF_PLT_ERR_NO_DEV;
98 }
99 if (mmc->type != MMC_DEV_EMMC) {
100 MmcDevicePut(mmc);
101 return HDF_PLT_ERR_DEV_TYPE;
102 }
103
104 *emmc = (struct EmmcDevice *)mmc;
105 return HDF_SUCCESS;
106 }
107
108 #endif
109
EmmcGetCid(DevHandle handle,uint8_t * cid,uint32_t size)110 int32_t EmmcGetCid(DevHandle handle, uint8_t *cid, uint32_t size)
111 {
112 #ifndef __USER__
113 struct EmmcDevice *emmc = NULL;
114 int32_t ret;
115 #endif
116 if (handle == NULL) {
117 HDF_LOGE("EmmcGetCid: handle is NULL!");
118 return HDF_ERR_INVALID_OBJECT;
119 }
120
121 if (cid == NULL || size < EMMC_CID_LEN) {
122 HDF_LOGE("EmmcGetCid: error parms!");
123 return HDF_ERR_INVALID_PARAM;
124 }
125 #ifdef __USER__
126 return EmmcServiceGetCid((struct HdfIoService *)handle, cid, size);
127 #else
128 if (EmmcDeviceGetFromHandle(handle, &emmc) != HDF_SUCCESS) {
129 return HDF_ERR_INVALID_OBJECT;
130 }
131 ret = EmmcDeviceGetCid(emmc, cid, size);
132 MmcDevicePut((struct MmcDevice *)emmc);
133 return ret;
134 #endif
135 }
136
EmmcGetHuid(uint8_t * cid,uint32_t size)137 void EmmcGetHuid(uint8_t *cid, uint32_t size)
138 {
139 DevHandle handle = NULL;
140 if (cid == NULL || size == 0) {
141 HDF_LOGE("EmmcGetUdid: error parms!");
142 return;
143 }
144 if (memset_s(cid, sizeof(uint8_t) * size, 0, sizeof(uint8_t) * size) != EOK) {
145 HDF_LOGE("EmmcGetUdid: memset_s fail!");
146 return;
147 }
148 handle = EmmcOpen(0);
149 if (handle == NULL) {
150 HDF_LOGD("EmmcGetUdid: open fail, use default value!");
151 return;
152 }
153 if (EmmcGetCid(handle, cid, size) != HDF_SUCCESS) {
154 HDF_LOGD("EmmcGetUdid: get fail, use default value!");
155 }
156 EmmcClose(handle);
157 }
158