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 <common.h> |
| 7 | #include <command.h> |
| 8 | #include <version.h> |
| 9 | #include <linux/compiler.h> |
Sam Wu | c7da175 | 2023-07-28 12:05:06 +0800 | [diff] [blame] | 10 | #include <amlogic/cpu_id.h> |
Zhongfu Luo | 862f0ce | 2024-05-16 19:22:35 +0800 | [diff] [blame] | 11 | #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 | |
| 24 | const 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 |
| 32 | #define GET_BL30_LEN_MAX 400 |
| 33 | #define GET_BL30_LEN_ONCE MAILBOX_USER_DATA_SIZE |
| 34 | int bl30_version_flag, bl30_version_size; |
| 35 | char bl30_version_str[GET_BL30_LEN_MAX]; |
| 36 | char bl30_version_str_once[GET_BL30_LEN_ONCE]; |
| 37 | #endif |
| 38 | |
| 39 | #ifdef CONFIG_BL30_VERSION_RTOSSDK |
| 40 | #define BUFF_LENGTH (512 + 400) |
| 41 | #else |
| 42 | #define BUFF_LENGTH 512 |
| 43 | #endif |
| 44 | |
| 45 | const char compile_user[] = COMPILE_USER; |
| 46 | unsigned char *bootloader_build_message; |
| 47 | char version_buffer[BUFF_LENGTH]; |
| 48 | int index; |
| 49 | |
| 50 | int putin_buf(const char *str) |
| 51 | { |
| 52 | char *ptr = version_buffer; |
| 53 | int num = strlen(str); |
| 54 | |
| 55 | if ((index + num) > BUFF_LENGTH) |
| 56 | return -1; |
| 57 | |
| 58 | ptr += index; |
| 59 | memcpy(ptr, str, num); |
| 60 | index += num; |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | void free_build_message(void) |
| 66 | { |
| 67 | if (bootloader_build_message) |
| 68 | free(bootloader_build_message); |
| 69 | } |
| 70 | #endif |
| 71 | |
| 72 | int get_bootloader_build_message(void) |
| 73 | { |
| 74 | #ifdef CONFIG_BUILD_MESSAGE |
| 75 | static build_messages_t *build_info; |
| 76 | |
| 77 | bootloader_build_message = (uint8_t *)malloc(sizeof(build_messages_t)); |
| 78 | if (!bootloader_build_message) { |
| 79 | printf("version message malloc fail\n"); |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | if (!aml_get_bootloader_version(bootloader_build_message)) { |
| 84 | build_info = (build_messages_t *)(bootloader_build_message); |
| 85 | if (build_info->h.type != PARAM_MESSAGE || |
| 86 | build_info->h.version != VERSION_2 || |
| 87 | build_info->h.size != sizeof(build_messages_t)) { |
| 88 | printf("invalid build messages info head!\n"); |
| 89 | free_build_message(); |
| 90 | return -1; |
| 91 | } |
| 92 | /* |
| 93 | * snprintf(build_info->bl33_message.hash, 32, "%s", git_hash); |
| 94 | * snprintf(build_info->bl33_message.user, 32, "%s", compile_user); |
| 95 | * snprintf(build_info->bl33_message.time, 64, "%s%s", compile_time, compile_data); |
| 96 | * build_info->bl33_message.ver = INDEX_BL33_15; |
| 97 | * build_info->bl33_message.init_flag = 1; |
| 98 | */ |
| 99 | putin_buf("Bootloader:{\n"); |
| 100 | if (build_info->bl2_message.init_flag) { |
| 101 | putin_buf("\tBL2 :"); |
| 102 | if (build_info->bl2_message.ver == INDEX_SPL) |
| 103 | putin_buf(" SPL-"); |
| 104 | else if (build_info->bl2_message.ver == INDEX_CORE) |
| 105 | putin_buf(" CORE-"); |
| 106 | //putin_buf(build_info->bl2_message.ver_str); |
| 107 | //putin_buf(","); |
| 108 | putin_buf(build_info->bl2_message.hash); |
| 109 | putin_buf(","); |
Meng yu | d8c1dd0 | 2024-05-29 09:44:02 +0800 | [diff] [blame^] | 110 | if (build_info->bl2_message.ext_message.ddr.ddr_extern) { |
| 111 | putin_buf(" DDR-"); |
| 112 | putin_buf(build_info->bl2_message.ext_message.ddr.ddr_hash); |
| 113 | putin_buf(","); |
| 114 | } |
Zhongfu Luo | 862f0ce | 2024-05-16 19:22:35 +0800 | [diff] [blame] | 115 | putin_buf(build_info->bl2_message.time); |
| 116 | putin_buf(","); |
| 117 | putin_buf(build_info->bl2_message.user); |
| 118 | putin_buf("\n"); |
| 119 | } |
| 120 | |
| 121 | if (build_info->bl2e_message.init_flag) { |
| 122 | putin_buf("\tBL2E: "); |
| 123 | //putin_buf(build_info->bl2e_message.ver_str); |
| 124 | //putin_buf(","); |
| 125 | putin_buf(build_info->bl2e_message.hash); |
| 126 | putin_buf(","); |
| 127 | putin_buf(build_info->bl2e_message.time); |
| 128 | putin_buf(","); |
| 129 | putin_buf(build_info->bl2e_message.user); |
| 130 | putin_buf("\n"); |
| 131 | } |
| 132 | |
| 133 | if (build_info->bl2x_message.init_flag) { |
| 134 | putin_buf("\tBL2X: "); |
| 135 | //putin_buf(build_info->bl2x_message.ver_str); |
| 136 | //putin_buf(","); |
| 137 | putin_buf(build_info->bl2x_message.hash); |
| 138 | putin_buf(","); |
| 139 | putin_buf(build_info->bl2x_message.time); |
| 140 | putin_buf(","); |
| 141 | putin_buf(build_info->bl2x_message.user); |
| 142 | putin_buf("\n"); |
| 143 | } |
| 144 | |
| 145 | #ifdef CONFIG_BL30_VERSION_SAVE |
| 146 | bl30_version_flag = GET_BL30_VERSION_SIZE; |
| 147 | if (!scpi_send_data(AOCPU_REE_CHANNEL, CMD_GET_BL30_VERSION, |
| 148 | &bl30_version_flag, 4, &bl30_version_size, 4)) { |
| 149 | if (bl30_version_size > 0) |
| 150 | build_info->bl30_message.init_flag = 1; |
| 151 | } else { |
| 152 | printf("bl30 version message get fail\n"); |
| 153 | } |
| 154 | #endif |
| 155 | if (build_info->bl30_message.init_flag) { |
| 156 | putin_buf("\tBL30: "); |
| 157 | //putin_buf(build_info->bl30_message.ver_str); |
| 158 | //putin_buf(","); |
| 159 | #ifdef CONFIG_BL30_VERSION_SAVE |
| 160 | bl30_version_flag = GET_BL30_VERSION_INFO; |
| 161 | for (int i = 0; i < (bl30_version_size / GET_BL30_LEN_ONCE + 1); i++) { |
| 162 | if (!scpi_send_data(AOCPU_REE_CHANNEL, CMD_GET_BL30_VERSION, |
| 163 | &bl30_version_flag, 4, &bl30_version_str_once, |
| 164 | GET_BL30_LEN_ONCE)) { |
| 165 | memcpy(&bl30_version_str[i * GET_BL30_LEN_ONCE], |
| 166 | bl30_version_str_once, GET_BL30_LEN_ONCE); |
| 167 | memset(bl30_version_str_once, 0, GET_BL30_LEN_ONCE); |
| 168 | } else { |
| 169 | printf("bl30 version message get fail\n"); |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | putin_buf(bl30_version_str); |
| 174 | #else |
| 175 | putin_buf(build_info->bl30_message.hash); |
| 176 | putin_buf(","); |
| 177 | putin_buf(build_info->bl30_message.time); |
| 178 | putin_buf(","); |
| 179 | putin_buf(build_info->bl30_message.user); |
| 180 | putin_buf("\n"); |
| 181 | #endif |
| 182 | } |
| 183 | |
| 184 | if (build_info->bl31_message.init_flag) { |
| 185 | putin_buf("\tBL31: "); |
| 186 | putin_buf(build_info->bl31_message.ver_str); |
| 187 | putin_buf(","); |
| 188 | putin_buf(build_info->bl31_message.hash); |
| 189 | putin_buf(","); |
| 190 | putin_buf(build_info->bl31_message.time); |
| 191 | putin_buf(","); |
| 192 | putin_buf(build_info->bl31_message.user); |
| 193 | putin_buf("\n"); |
| 194 | } |
| 195 | |
| 196 | if (build_info->bl32_message.init_flag) { |
| 197 | putin_buf("\tBL32 :"); |
| 198 | putin_buf(build_info->bl32_message.ver_str); |
| 199 | putin_buf(","); |
| 200 | putin_buf(build_info->bl32_message.hash); |
| 201 | putin_buf(","); |
| 202 | putin_buf(build_info->bl32_message.time); |
| 203 | putin_buf(","); |
| 204 | putin_buf(build_info->bl32_message.user); |
| 205 | putin_buf("\n"); |
| 206 | } |
| 207 | |
| 208 | putin_buf("\tBL33: "); |
| 209 | putin_buf(version_string); |
| 210 | putin_buf(","); |
| 211 | putin_buf(compile_user); |
| 212 | putin_buf("\n"); |
| 213 | putin_buf("}\n\0"); |
| 214 | |
| 215 | free_build_message(); |
| 216 | return 0; |
| 217 | } |
| 218 | #endif |
| 219 | printf("no BUILD_MESSAGE\n"); |
| 220 | |
| 221 | return -1; |
| 222 | } |
| 223 | |
| 224 | static int do_build_message_print(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 225 | { |
| 226 | #ifdef CONFIG_BUILD_MESSAGE |
| 227 | if (index > 0) { |
| 228 | printf("%s", version_buffer); |
| 229 | return 0; |
| 230 | } |
| 231 | #endif |
| 232 | printf("no version buffer data!\n"); |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | int fdt_bl_version(void *fdt) |
| 237 | { |
| 238 | #ifdef CONFIG_BUILD_MESSAGE |
| 239 | int nodeoffset; |
| 240 | int err; |
| 241 | |
| 242 | if (index > 0) { |
| 243 | err = fdt_check_header(fdt); |
| 244 | if (err < 0) { |
| 245 | printf("fdt_chosen: %s\n", fdt_strerror(err)); |
| 246 | return err; |
| 247 | } |
| 248 | |
| 249 | /* find or create "/chosen" node. */ |
| 250 | nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen"); |
| 251 | if (nodeoffset < 0) |
| 252 | return nodeoffset; |
| 253 | |
| 254 | err = fdt_setprop(fdt, nodeoffset, "bootloader_build", version_buffer, |
| 255 | strlen(version_buffer) + 1); |
| 256 | if (err < 0) { |
| 257 | printf("WARNING: could not set bl2_build %s.\n", |
| 258 | fdt_strerror(err)); |
| 259 | return err; |
| 260 | } |
| 261 | } |
| 262 | #endif |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | U_BOOT_CMD(build_message, 1, 1, do_build_message_print, |
| 267 | "print bl2/bl2e/bl2x/bl31/bl32/bl33 version", |
| 268 | "" |
| 269 | ); |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 270 | |
| 271 | static int do_bootloader_version(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 272 | { |
| 273 | char s_version[64]; |
| 274 | strcpy(s_version, "01.01."); |
| 275 | strcat(s_version, U_BOOT_DATE_TIME); |
| 276 | printf("s_version: %s\n", s_version); |
| 277 | env_set("bootloader_version", s_version); |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | U_BOOT_CMD( |
| 282 | get_bootloaderversion, 1, 0, do_bootloader_version, |
| 283 | "print bootloader version", |
| 284 | "" |
| 285 | ); |
Sam Wu | c7da175 | 2023-07-28 12:05:06 +0800 | [diff] [blame] | 286 | |
| 287 | static int do_get_cpuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 288 | { |
| 289 | unsigned char chipid[16]; |
| 290 | char chipid_str[32]; |
| 291 | int i, j; |
| 292 | char buf_tmp[4]; |
| 293 | char *buff = NULL; |
| 294 | |
| 295 | memset(chipid, 0, 16); |
| 296 | if (get_chip_id(chipid, 16)) { |
| 297 | env_set("cpu_id", "1122334455667788"); //default |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | memset(chipid_str, 0, 32); |
| 302 | buff = &chipid_str[0]; |
| 303 | for (i = 0, j = 0; i < 12; ++i) { |
| 304 | sprintf(&buf_tmp[0], "%02x", chipid[15 - i]); |
| 305 | if (strcmp(buf_tmp, "00") != 0) { |
| 306 | sprintf(buff + j, "%02x", chipid[15 - i]); |
| 307 | j = j + 2; |
| 308 | } |
| 309 | } |
| 310 | env_set("cpu_id", chipid_str); |
| 311 | printf("buff: %s\n", buff); |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | U_BOOT_CMD_COMPLETE( |
| 317 | get_cpuid, //get cpu id to env cpu_id |
| 318 | 1, //maxargs |
| 319 | 0, //repeatable |
| 320 | do_get_cpuid, //command function |
| 321 | "read cpuid to env cpu_id", //description |
| 322 | " argv: get_cpuid\n", //usage |
| 323 | var_complete |
| 324 | ); |