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 <environment.h> |
| 9 | #include <malloc.h> |
| 10 | #include <asm/byteorder.h> |
| 11 | #include <config.h> |
| 12 | #include <asm/arch/io.h> |
| 13 | #include <partition_table.h> |
| 14 | #include <libavb.h> |
| 15 | #include <version.h> |
| 16 | #include <amlogic/storage.h> |
| 17 | #include <fastboot.h> |
| 18 | #include <u-boot/sha1.h> |
| 19 | |
| 20 | #ifdef CONFIG_CMD_CAR_PARAMS |
| 21 | |
| 22 | // calibrator_message used 1024K-1026K by misc partition |
| 23 | typedef struct calibrator_message { |
| 24 | uint32_t magic; //magic number |
| 25 | uint8_t rsv[4]; |
| 26 | int64_t camera[9]; //camera Internal parameter matrix |
| 27 | int64_t distortion[5]; //Distortion matrix |
| 28 | int64_t matrix_rtos[9]; //projection matrix for rtos |
| 29 | int64_t matrix_gles[9];//projection matrix for gles |
| 30 | int64_t car[5]; //car parameters |
| 31 | |
| 32 | // rounds up to 2048-byte. |
| 33 | uint8_t reserve[1740]; |
| 34 | |
| 35 | // CRC32 of all 28 bytes preceding this field (little endian |
| 36 | // format). |
| 37 | uint32_t crc32_le; |
| 38 | } calibrator_message; |
| 39 | |
| 40 | #define CALIBRATOR_OFFSET (1024 * 1024) |
| 41 | #define CALIBRATOR_SIZE 2048 |
| 42 | #define CALIBRATOR_MAGIC 0x51504948 /* magic number */ |
| 43 | |
| 44 | static uint32_t car_crc32(const uint8_t *buf, size_t size) |
| 45 | { |
| 46 | static uint32_t crc_table[256]; |
| 47 | uint32_t ret = -1; |
| 48 | |
| 49 | // Compute the CRC-32 table only once. |
| 50 | if (!crc_table[1]) { |
| 51 | for (uint32_t i = 0; i < 256; ++i) { |
| 52 | uint32_t crc = i; |
| 53 | |
| 54 | for (uint32_t j = 0; j < 8; ++j) { |
| 55 | uint32_t mask = -(crc & 1); |
| 56 | |
| 57 | crc = (crc >> 1) ^ (0xEDB88320 & mask); |
| 58 | } |
| 59 | crc_table[i] = crc; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | for (size_t i = 0; i < size; ++i) |
| 64 | ret = (ret >> 8) ^ crc_table[(ret ^ buf[i]) & 0xFF]; |
| 65 | |
| 66 | return ~ret; |
| 67 | } |
| 68 | |
| 69 | bool calibrator_info_validate(calibrator_message *info) |
| 70 | { |
| 71 | uint32_t crc; |
| 72 | |
| 73 | if (info->magic != CALIBRATOR_MAGIC) { |
| 74 | printf("Magic 0x%x is incorrect.\n", info->magic); |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | crc = car_crc32((const uint8_t *)info, |
| 79 | sizeof(calibrator_message) - sizeof(uint32_t)); |
| 80 | if (info->crc32_le != crc) { |
| 81 | printf("crc 0x%x != 0x%x\n", info->crc32_le, crc); |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | void calibrator_info_reset(calibrator_message *info) |
| 89 | { |
| 90 | memset(info, '\0', sizeof(calibrator_message)); |
| 91 | info->magic = CALIBRATOR_MAGIC; |
| 92 | } |
| 93 | |
| 94 | void dump_info(calibrator_message *info) |
| 95 | { |
| 96 | int i; |
| 97 | |
| 98 | printf("info->magic = 0x%x\n", info->magic); |
| 99 | for (i = 0; i < 9; ++i) { |
| 100 | printf("info->camera[%d] = %lld\n", |
| 101 | i, info->camera[i]); |
| 102 | } |
| 103 | for (i = 0; i < 5; ++i) { |
| 104 | printf("info->distortion[%d] = %lld\n", |
| 105 | i, info->distortion[i]); |
| 106 | } |
| 107 | for (i = 0; i < 9; ++i) { |
| 108 | printf("info->matrix_rtos[%d] = %lld\n", |
| 109 | i, info->matrix_rtos[i]); |
| 110 | } |
| 111 | for (i = 0; i < 9; ++i) { |
| 112 | printf("info->matrix_gles[%d] = %lld\n", |
| 113 | i, info->matrix_gles[i]); |
| 114 | } |
| 115 | for (i = 0; i < 5; ++i) { |
| 116 | printf("info->car[%d] = %lld\n", |
| 117 | i, info->car[i]); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | int calibrator_info_open_partition(unsigned char *miscbuf) |
| 122 | { |
| 123 | char *partition = "misc"; |
| 124 | |
| 125 | printf("Start read %s partition datas!\n", partition); |
| 126 | if (store_read((const char *)partition, |
| 127 | CALIBRATOR_OFFSET, CALIBRATOR_SIZE, miscbuf) < 0) { |
| 128 | printf("failed to store read %s.\n", partition); |
| 129 | return -1; |
| 130 | } |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | bool calibrator_info_save(calibrator_message *info) |
| 135 | { |
| 136 | char *partition = "misc"; |
| 137 | |
| 138 | printf("save calibrator-info\n"); |
| 139 | info->crc32_le = car_crc32((const uint8_t *)info, |
| 140 | sizeof(calibrator_message) - sizeof(uint32_t)); |
| 141 | |
| 142 | dump_info(info); |
| 143 | store_write((const char *)partition, CALIBRATOR_OFFSET, CALIBRATOR_SIZE, |
| 144 | (unsigned char *)info); |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | static int do_ReadCarParams(cmd_tbl_t *cmdtp, |
| 149 | int flag, |
| 150 | int argc, |
| 151 | char * const argv[]) |
| 152 | { |
| 153 | calibrator_message *info = NULL; |
| 154 | unsigned char *loadaddr = 0; |
| 155 | |
| 156 | if (argc > 2) |
| 157 | loadaddr = (unsigned char *)simple_strtoul(argv[2], NULL, 16); |
| 158 | else |
| 159 | loadaddr = (unsigned char *)simple_strtoul(env_get("car_mem_addr"), NULL, 16); |
| 160 | |
| 161 | info = (calibrator_message *)loadaddr; |
| 162 | |
| 163 | calibrator_info_open_partition(loadaddr); |
| 164 | |
| 165 | if (!calibrator_info_validate(info)) { |
| 166 | printf("calibrator-info is invalid. resetting\n"); |
| 167 | calibrator_info_reset(info); |
| 168 | calibrator_info_save(info); |
| 169 | } else { |
| 170 | dump_info(info); |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | #endif |
| 177 | |
| 178 | U_BOOT_CMD(read_car_params, 3, 0, do_ReadCarParams, |
| 179 | "read_car_params", |
| 180 | "\nThis command will read car params to mem\n" |
| 181 | "partition by mark to decide whether execute command!\n" |
| 182 | "So you can execute command: read_car_params" |
| 183 | ); |
| 184 | |