1 /**
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 * Description: for osal adapt.
15 *
16 * Create: 2023-04-23
17 */
18
19 #include "crypto_osal_lib.h"
20 #include "drv_trng.h"
21 #include "crypto_osal_adapt.h"
22 #include "td_base.h"
23 #include "boot_serial.h"
24
25 td_char const hex2ascii_data[] = "0123456789abcdefghijklmnopqrstuvwxyz";
26 #define hex2ascii(hex) (hex2ascii_data[hex])
27
28 #define crypto_fallthrough() __attribute__((fallthrough))
29
30 #define toupper(c) ((c) - 0x20 * (((c) >= 'a') && ((c) <= 'z')))
31
ksprintn(td_char * nbuf,td_u32 num,td_s32 base,td_s32 * lenp,td_s32 upper)32 static td_char *ksprintn(td_char *nbuf, td_u32 num, td_s32 base, td_s32 *lenp, td_s32 upper)
33 {
34 td_char *p, c;
35
36 p = nbuf;
37 *p = '\0';
38 do {
39 c = hex2ascii(num % base);
40 *++p = upper ? toupper(c) : c;
41 } while (num /= base);
42 if (lenp != TD_NULL) {
43 *lenp = p - nbuf;
44 }
45
46 return (p);
47 }
48
kvprintf(td_char const * fmt,td_void (* func)(td_char),va_list ap)49 static td_s32 kvprintf(td_char const * fmt, td_void (*func)(td_char), va_list ap)
50 {
51 #define crypto_pchar(c) do { \
52 td_char cc = (c); \
53 if (func != TD_NULL) \
54 (*func)(cc); \
55 retval++; \
56 } while (0)
57 td_char nbuf[sizeof(td_s32) * 8 + 1]; // 8: 8 bits in one byte
58 const td_char *p, *percent;
59 td_s32 ch, n;
60 td_u32 num;
61 td_s32 base, tmp, width, neg, sign, dot;
62 td_s32 dwidth, upper;
63 td_char padc;
64 td_s32 stop = 0;
65 td_s32 retval = 0;
66
67 num = 0;
68
69 if (fmt == TD_NULL) {
70 fmt = "(fmt null)n";
71 }
72
73 for (;;) {
74 padc = ' ';
75 width = 0;
76 while ((ch = (td_uchar)*fmt++) != '%' || stop) {
77 if (ch == '\0') {
78 return (retval);
79 }
80 crypto_pchar(ch);
81 }
82 percent = fmt - 1;
83 neg = 0;
84 sign = 0;
85 dot = 0;
86 dwidth = 0;
87 upper = 0;
88
89 reswitch:
90 switch (ch = (td_uchar)*fmt++) {
91 case '%':
92 crypto_pchar(ch);
93 break;
94 case '0':
95 if (!dot) {
96 padc = '0';
97 goto reswitch;
98 }
99 crypto_fallthrough();
100 case '1':
101 case '2':
102 case '3':
103 case '4':
104 case '5':
105 case '6':
106 case '7':
107 case '8':
108 case '9':
109 for (n = 0;; ++fmt) {
110 n = n * 10 + ch - '0'; /* 10: Decimal data conversion. */
111 ch = *fmt;
112 if (ch < '0' || ch > '9') {
113 break;
114 }
115 }
116 if (dot) {
117 dwidth = n;
118 } else {
119 width = n;
120 }
121 goto reswitch;
122 case 'l': // Must use l flag to use u32
123 goto reswitch;
124 case 'c':
125 crypto_pchar(va_arg(ap, td_s32));
126 break;
127 case 'd':
128 case 'i':
129 base = 10; /* 10: Decimal data. */
130 sign = 1;
131 goto handle_sign;
132 case 'u':
133 base = 10; /* 10: Decimal data. */
134 goto handle_nosign;
135 case 's':
136 p = va_arg(ap, td_char *);
137 if (p == TD_NULL) {
138 p = "(null)";
139 }
140 if (!dot) {
141 n = strlen(p);
142 } else {
143 for (n = 0; n < dwidth && p[n]; n++) {
144 continue;
145 }
146 }
147 width -= n;
148
149 if (width > 0) {
150 while (width--) {
151 crypto_pchar(padc);
152 }
153 }
154 while (n--) {
155 crypto_pchar(*p++);
156 }
157 break;
158 case 'X':
159 upper = 1;
160 crypto_fallthrough();
161 case 'x':
162 base = 16; /* 16: Hexadecimal data. */
163 goto handle_nosign;
164 handle_nosign:
165 sign = 0;
166 num = va_arg(ap, td_u32);
167 goto number;
168 handle_sign:
169 num = va_arg(ap, td_s32);
170 number:
171 if (sign && (td_s32)num < 0) {
172 neg = 1;
173 num = -(td_s32)num;
174 }
175 p = ksprintn(nbuf, num, base, &tmp, upper);
176 if (neg) {
177 tmp++;
178 }
179 if (padc != '0' && width && (width -= tmp) > 0) {
180 while (width--) {
181 crypto_pchar(padc);
182 }
183 }
184 if (neg) {
185 crypto_pchar('-');
186 }
187 if (width && (width -= tmp) > 0) {
188 while (width--) {
189 crypto_pchar(padc);
190 }
191 }
192 while (*p) {
193 crypto_pchar(*p--);
194 }
195 break;
196 default:
197 while (percent < fmt) {
198 crypto_pchar(*percent++);
199 }
200 /*
201 * Since we ignore an formatting argument it is no
202 * longer safe to obey the remaining formatting
203 * arguments as the arguments will no longer match
204 * the format specs.
205 */
206 stop = 1;
207 break;
208 }
209 }
210 #undef crypto_pchar
211 return 0;
212 }
213
debug_putc(const td_char c)214 static td_void debug_putc(const td_char c)
215 {
216 (void)serial_putc(c);
217 }
218
crypto_boot_debug_message(const char * fmt,...)219 td_void crypto_boot_debug_message(const char *fmt, ...)
220 {
221 va_list ap;
222
223 va_start(ap, fmt);
224 kvprintf(fmt, debug_putc, ap);
225 va_end(ap);
226 }
227
crypto_cache_flush(uintptr_t base_addr,td_u32 size)228 void __attribute__((weak)) crypto_cache_flush(uintptr_t base_addr, td_u32 size)
229 {
230 crypto_unused(base_addr);
231 crypto_unused(size);
232 }
233
crypto_cache_inv(uintptr_t base_addr,td_u32 size)234 void __attribute__((weak)) crypto_cache_inv(uintptr_t base_addr, td_u32 size)
235 {
236 crypto_unused(base_addr);
237 crypto_unused(size);
238 }
239
crypto_cache_all(void)240 void __attribute__((weak)) crypto_cache_all(void)
241 {
242 }
243
crypto_wait_func_is_support(void)244 td_bool crypto_wait_func_is_support(void)
245 {
246 return TD_FALSE;
247 }