blob: db852b0d794507ea6dd346cadcf91ab22de29161 [file] [log] [blame]
Bo Lv72d0e902023-01-02 14:27:34 +00001// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2/*
3 * Copyright (c) 2019 Amlogic, Inc. All rights reserved.
4 */
5
6#include <common.h>
7#include <command.h>
8#include <version.h>
9#include <linux/compiler.h>
Sam Wuc7da1752023-07-28 12:05:06 +080010#include <amlogic/cpu_id.h>
Zhongfu Luo862f0ce2024-05-16 19:22:35 +080011#include <asm/amlogic/arch/cpu_config.h>
12
13
14
15#ifdef CONFIG_BUILD_MESSAGE
16#include <asm/amlogic/arch/bl31_apis.h>
17#include <malloc.h>
18#include <linux/libfdt.h>
19#include <fdt_support.h>
20
21#define U_BOOT_VERSION_STRING U_BOOT_VERSION " (" U_BOOT_DATE " - " \
22 U_BOOT_TIME " " U_BOOT_TZ ")" CONFIG_IDENT_STRING
23
24const char __weak version_string[] = U_BOOT_VERSION_STRING;
25
26#ifdef CONFIG_BL30_VERSION_SAVE
27#include <asm/amlogic/arch/mailbox.h>
28#include <asm/amlogic/arch/secure_apb.h>
29#include <asm/amlogic/arch/cpu.h>
30#define GET_BL30_VERSION_SIZE 0x30303000
31#define GET_BL30_VERSION_INFO 0x30303001
bangzheng.liu4e1b3572024-06-28 11:37:06 +080032#define GET_BL30_VERSION_TIME 0x30303002
Zhongfu Luo862f0ce2024-05-16 19:22:35 +080033#define GET_BL30_LEN_MAX 400
34#define GET_BL30_LEN_ONCE MAILBOX_USER_DATA_SIZE
bangzheng.liu4e1b3572024-06-28 11:37:06 +080035static int bl30_version_flag, bl30_version_size;
36static char bl30_version_str[GET_BL30_LEN_MAX];
37static char bl30_version_str_once[GET_BL30_LEN_ONCE];
Zhongfu Luo862f0ce2024-05-16 19:22:35 +080038#endif
39
40#ifdef CONFIG_BL30_VERSION_RTOSSDK
41#define BUFF_LENGTH (512 + 400)
bangzheng.liu4e1b3572024-06-28 11:37:06 +080042static int bl30_version_total_size, bl30_version_size_once;
43static int bl30_version_trans_times;
Zhongfu Luo862f0ce2024-05-16 19:22:35 +080044#else
45#define BUFF_LENGTH 512
46#endif
47
48const char compile_user[] = COMPILE_USER;
49unsigned char *bootloader_build_message;
50char version_buffer[BUFF_LENGTH];
51int index;
52
53int putin_buf(const char *str)
54{
55 char *ptr = version_buffer;
56 int num = strlen(str);
57
58 if ((index + num) > BUFF_LENGTH)
59 return -1;
60
61 ptr += index;
62 memcpy(ptr, str, num);
63 index += num;
64
65 return 0;
66}
67
68void free_build_message(void)
69{
70 if (bootloader_build_message)
71 free(bootloader_build_message);
72}
73#endif
74
75int get_bootloader_build_message(void)
76{
77#ifdef CONFIG_BUILD_MESSAGE
78 static build_messages_t *build_info;
79
80 bootloader_build_message = (uint8_t *)malloc(sizeof(build_messages_t));
81 if (!bootloader_build_message) {
82 printf("version message malloc fail\n");
83 return -1;
84 }
85
86 if (!aml_get_bootloader_version(bootloader_build_message)) {
87 build_info = (build_messages_t *)(bootloader_build_message);
88 if (build_info->h.type != PARAM_MESSAGE ||
89 build_info->h.version != VERSION_2 ||
90 build_info->h.size != sizeof(build_messages_t)) {
91 printf("invalid build messages info head!\n");
92 free_build_message();
93 return -1;
94 }
95 /*
96 * snprintf(build_info->bl33_message.hash, 32, "%s", git_hash);
97 * snprintf(build_info->bl33_message.user, 32, "%s", compile_user);
98 * snprintf(build_info->bl33_message.time, 64, "%s%s", compile_time, compile_data);
99 * build_info->bl33_message.ver = INDEX_BL33_15;
100 * build_info->bl33_message.init_flag = 1;
101 */
102 putin_buf("Bootloader:{\n");
103 if (build_info->bl2_message.init_flag) {
104 putin_buf("\tBL2 :");
105 if (build_info->bl2_message.ver == INDEX_SPL)
106 putin_buf(" SPL-");
107 else if (build_info->bl2_message.ver == INDEX_CORE)
108 putin_buf(" CORE-");
109 //putin_buf(build_info->bl2_message.ver_str);
110 //putin_buf(",");
111 putin_buf(build_info->bl2_message.hash);
112 putin_buf(",");
Meng yud8c1dd02024-05-29 09:44:02 +0800113 if (build_info->bl2_message.ext_message.ddr.ddr_extern) {
114 putin_buf(" DDR-");
115 putin_buf(build_info->bl2_message.ext_message.ddr.ddr_hash);
116 putin_buf(",");
117 }
Zhongfu Luo862f0ce2024-05-16 19:22:35 +0800118 putin_buf(build_info->bl2_message.time);
119 putin_buf(",");
120 putin_buf(build_info->bl2_message.user);
121 putin_buf("\n");
122 }
123
124 if (build_info->bl2e_message.init_flag) {
125 putin_buf("\tBL2E: ");
126 //putin_buf(build_info->bl2e_message.ver_str);
127 //putin_buf(",");
128 putin_buf(build_info->bl2e_message.hash);
129 putin_buf(",");
130 putin_buf(build_info->bl2e_message.time);
131 putin_buf(",");
132 putin_buf(build_info->bl2e_message.user);
133 putin_buf("\n");
134 }
135
136 if (build_info->bl2x_message.init_flag) {
137 putin_buf("\tBL2X: ");
138 //putin_buf(build_info->bl2x_message.ver_str);
139 //putin_buf(",");
140 putin_buf(build_info->bl2x_message.hash);
141 putin_buf(",");
142 putin_buf(build_info->bl2x_message.time);
143 putin_buf(",");
144 putin_buf(build_info->bl2x_message.user);
145 putin_buf("\n");
146 }
147
148#ifdef CONFIG_BL30_VERSION_SAVE
149 bl30_version_flag = GET_BL30_VERSION_SIZE;
150 if (!scpi_send_data(AOCPU_REE_CHANNEL, CMD_GET_BL30_VERSION,
151 &bl30_version_flag, 4, &bl30_version_size, 4)) {
152 if (bl30_version_size > 0)
153 build_info->bl30_message.init_flag = 1;
154 } else {
155 printf("bl30 version message get fail\n");
156 }
157#endif
158 if (build_info->bl30_message.init_flag) {
159 putin_buf("\tBL30: ");
160 //putin_buf(build_info->bl30_message.ver_str);
161 //putin_buf(",");
162#ifdef CONFIG_BL30_VERSION_SAVE
bangzheng.liu4e1b3572024-06-28 11:37:06 +0800163#ifdef CONFIG_BL30_VERSION_RTOSSDK
164 bl30_version_flag = GET_BL30_VERSION_TIME;
165 if (!scpi_send_data(AOCPU_REE_CHANNEL, CMD_GET_BL30_VERSION,
166 &bl30_version_flag, 4, &bl30_version_str_once, GET_BL30_LEN_ONCE)) {
167 putin_buf(bl30_version_str_once);
168 memset(bl30_version_str_once, 0, GET_BL30_LEN_ONCE);
169 } else {
170 printf("bl30 version message get fail\n");
171 }
172
173 bl30_version_flag = GET_BL30_VERSION_INFO;
174 bl30_version_total_size = bl30_version_size & 0xffff;
175 bl30_version_size_once = bl30_version_size >> 16;
176 if (bl30_version_total_size % bl30_version_size_once)
177 bl30_version_trans_times =
178 bl30_version_total_size / bl30_version_size_once + 1;
179 else
180 bl30_version_trans_times =
181 bl30_version_total_size / bl30_version_size_once;
182
183 for (int i = 0; i < bl30_version_trans_times; i++) {
184 if (!scpi_send_data(AOCPU_REE_CHANNEL, CMD_GET_BL30_VERSION,
185 &bl30_version_flag, 4, &bl30_version_str_once,
186 bl30_version_size_once)) {
187 memcpy(&bl30_version_str[i * bl30_version_size_once],
188 bl30_version_str_once, bl30_version_size_once);
189 memset(bl30_version_str_once, 0, bl30_version_size_once);
190 } else {
191 printf("bl30 version message get fail\n");
192 break;
193 }
194 }
195#else
Zhongfu Luo862f0ce2024-05-16 19:22:35 +0800196 bl30_version_flag = GET_BL30_VERSION_INFO;
197 for (int i = 0; i < (bl30_version_size / GET_BL30_LEN_ONCE + 1); i++) {
198 if (!scpi_send_data(AOCPU_REE_CHANNEL, CMD_GET_BL30_VERSION,
199 &bl30_version_flag, 4, &bl30_version_str_once,
200 GET_BL30_LEN_ONCE)) {
201 memcpy(&bl30_version_str[i * GET_BL30_LEN_ONCE],
202 bl30_version_str_once, GET_BL30_LEN_ONCE);
203 memset(bl30_version_str_once, 0, GET_BL30_LEN_ONCE);
204 } else {
205 printf("bl30 version message get fail\n");
206 break;
207 }
208 }
bangzheng.liu4e1b3572024-06-28 11:37:06 +0800209#endif
Zhongfu Luo862f0ce2024-05-16 19:22:35 +0800210 putin_buf(bl30_version_str);
211#else
212 putin_buf(build_info->bl30_message.hash);
213 putin_buf(",");
214 putin_buf(build_info->bl30_message.time);
215 putin_buf(",");
216 putin_buf(build_info->bl30_message.user);
217 putin_buf("\n");
218#endif
219 }
220
221 if (build_info->bl31_message.init_flag) {
222 putin_buf("\tBL31: ");
223 putin_buf(build_info->bl31_message.ver_str);
224 putin_buf(",");
225 putin_buf(build_info->bl31_message.hash);
226 putin_buf(",");
227 putin_buf(build_info->bl31_message.time);
228 putin_buf(",");
229 putin_buf(build_info->bl31_message.user);
230 putin_buf("\n");
231 }
232
233 if (build_info->bl32_message.init_flag) {
234 putin_buf("\tBL32 :");
235 putin_buf(build_info->bl32_message.ver_str);
236 putin_buf(",");
237 putin_buf(build_info->bl32_message.hash);
238 putin_buf(",");
239 putin_buf(build_info->bl32_message.time);
240 putin_buf(",");
241 putin_buf(build_info->bl32_message.user);
242 putin_buf("\n");
243 }
244
245 putin_buf("\tBL33: ");
246 putin_buf(version_string);
247 putin_buf(",");
248 putin_buf(compile_user);
249 putin_buf("\n");
250 putin_buf("}\n\0");
251
252 free_build_message();
253 return 0;
254 }
255#endif
256 printf("no BUILD_MESSAGE\n");
257
258 return -1;
259}
260
261static int do_build_message_print(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
262{
263#ifdef CONFIG_BUILD_MESSAGE
264 if (index > 0) {
265 printf("%s", version_buffer);
266 return 0;
267 }
268#endif
269 printf("no version buffer data!\n");
270 return -1;
271}
272
273int fdt_bl_version(void *fdt)
274{
275#ifdef CONFIG_BUILD_MESSAGE
276 int nodeoffset;
277 int err;
278
279 if (index > 0) {
280 err = fdt_check_header(fdt);
281 if (err < 0) {
282 printf("fdt_chosen: %s\n", fdt_strerror(err));
283 return err;
284 }
285
286 /* find or create "/chosen" node. */
287 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
288 if (nodeoffset < 0)
289 return nodeoffset;
290
291 err = fdt_setprop(fdt, nodeoffset, "bootloader_build", version_buffer,
292 strlen(version_buffer) + 1);
293 if (err < 0) {
294 printf("WARNING: could not set bl2_build %s.\n",
295 fdt_strerror(err));
296 return err;
297 }
298 }
299#endif
300 return 0;
301}
302
303U_BOOT_CMD(build_message, 1, 1, do_build_message_print,
304 "print bl2/bl2e/bl2x/bl31/bl32/bl33 version",
305 ""
306);
Bo Lv72d0e902023-01-02 14:27:34 +0000307
308static int do_bootloader_version(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
309{
310 char s_version[64];
311 strcpy(s_version, "01.01.");
312 strcat(s_version, U_BOOT_DATE_TIME);
313 printf("s_version: %s\n", s_version);
314 env_set("bootloader_version", s_version);
315 return 0;
316}
317
318U_BOOT_CMD(
319 get_bootloaderversion, 1, 0, do_bootloader_version,
320 "print bootloader version",
321 ""
322);
Sam Wuc7da1752023-07-28 12:05:06 +0800323
324static int do_get_cpuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
325{
326 unsigned char chipid[16];
327 char chipid_str[32];
328 int i, j;
329 char buf_tmp[4];
330 char *buff = NULL;
331
332 memset(chipid, 0, 16);
333 if (get_chip_id(chipid, 16)) {
334 env_set("cpu_id", "1122334455667788"); //default
335 return 0;
336 }
337
338 memset(chipid_str, 0, 32);
339 buff = &chipid_str[0];
340 for (i = 0, j = 0; i < 12; ++i) {
341 sprintf(&buf_tmp[0], "%02x", chipid[15 - i]);
342 if (strcmp(buf_tmp, "00") != 0) {
343 sprintf(buff + j, "%02x", chipid[15 - i]);
344 j = j + 2;
345 }
346 }
347 env_set("cpu_id", chipid_str);
348 printf("buff: %s\n", buff);
349
350 return 0;
351}
352
353U_BOOT_CMD_COMPLETE(
354 get_cpuid, //get cpu id to env cpu_id
355 1, //maxargs
356 0, //repeatable
357 do_get_cpuid, //command function
358 "read cpuid to env cpu_id", //description
359 " argv: get_cpuid\n", //usage
360 var_complete
361);