Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 2 | /* |
Marcel Ziswiler | 75b9327 | 2020-01-28 14:42:23 +0100 | [diff] [blame] | 3 | * Copyright (c) 2016-2020 Toradex |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 7 | #include <asm/global_data.h> |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 8 | #include "tdx-cfg-block.h" |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 9 | #include "tdx-eeprom.h" |
| 10 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 11 | #include <command.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 12 | #include <asm/cache.h> |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 13 | |
Marcel Ziswiler | 2658c8e | 2019-04-09 17:25:32 +0200 | [diff] [blame] | 14 | #if defined(CONFIG_TARGET_APALIS_IMX6) || \ |
Marcel Ziswiler | 0032061 | 2019-07-12 12:35:08 +0200 | [diff] [blame] | 15 | defined(CONFIG_TARGET_APALIS_IMX8) || \ |
Marcel Ziswiler | 2658c8e | 2019-04-09 17:25:32 +0200 | [diff] [blame] | 16 | defined(CONFIG_TARGET_COLIBRI_IMX6) || \ |
Marcel Ziswiler | eca26ba | 2020-01-28 14:42:24 +0100 | [diff] [blame] | 17 | defined(CONFIG_TARGET_COLIBRI_IMX8X) || \ |
| 18 | defined(CONFIG_TARGET_VERDIN_IMX8MM) || \ |
Marcel Ziswiler | 4a9c702 | 2020-10-28 11:58:08 +0200 | [diff] [blame] | 19 | defined(CONFIG_TARGET_VERDIN_IMX8MN) || \ |
| 20 | defined(CONFIG_TARGET_VERDIN_IMX8MP) |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 21 | #include <asm/arch/sys_proto.h> |
| 22 | #else |
| 23 | #define is_cpu_type(cpu) (0) |
| 24 | #endif |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 25 | #include <cli.h> |
| 26 | #include <console.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 27 | #include <env.h> |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 28 | #include <flash.h> |
| 29 | #include <malloc.h> |
| 30 | #include <mmc.h> |
| 31 | #include <nand.h> |
Simon Glass | 0ffb9d6 | 2017-05-31 19:47:48 -0600 | [diff] [blame] | 32 | #include <asm/mach-types.h> |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 33 | |
| 34 | DECLARE_GLOBAL_DATA_PTR; |
| 35 | |
| 36 | #define TAG_VALID 0xcf01 |
| 37 | #define TAG_MAC 0x0000 |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 38 | #define TAG_CAR_SERIAL 0x0021 |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 39 | #define TAG_HW 0x0008 |
| 40 | #define TAG_INVALID 0xffff |
| 41 | |
| 42 | #define TAG_FLAG_VALID 0x1 |
| 43 | |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 44 | #define TDX_EEPROM_ID_MODULE 0 |
| 45 | #define TDX_EEPROM_ID_CARRIER 1 |
| 46 | |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 47 | #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC) |
| 48 | #define TDX_CFG_BLOCK_MAX_SIZE 512 |
| 49 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND) |
| 50 | #define TDX_CFG_BLOCK_MAX_SIZE 64 |
| 51 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR) |
| 52 | #define TDX_CFG_BLOCK_MAX_SIZE 64 |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 53 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM) |
| 54 | #define TDX_CFG_BLOCK_MAX_SIZE 64 |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 55 | #else |
| 56 | #error Toradex config block location not set |
| 57 | #endif |
| 58 | |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 59 | #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA |
| 60 | #define TDX_CFG_BLOCK_EXTRA_MAX_SIZE 64 |
| 61 | #endif |
| 62 | |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 63 | struct toradex_tag { |
| 64 | u32 len:14; |
| 65 | u32 flags:2; |
| 66 | u32 id:16; |
| 67 | }; |
| 68 | |
| 69 | bool valid_cfgblock; |
| 70 | struct toradex_hw tdx_hw_tag; |
| 71 | struct toradex_eth_addr tdx_eth_addr; |
| 72 | u32 tdx_serial; |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 73 | #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA |
| 74 | u32 tdx_car_serial; |
| 75 | bool valid_cfgblock_carrier; |
| 76 | struct toradex_hw tdx_car_hw_tag; |
| 77 | #endif |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 78 | |
| 79 | const char * const toradex_modules[] = { |
| 80 | [0] = "UNKNOWN MODULE", |
| 81 | [1] = "Colibri PXA270 312MHz", |
| 82 | [2] = "Colibri PXA270 520MHz", |
| 83 | [3] = "Colibri PXA320 806MHz", |
| 84 | [4] = "Colibri PXA300 208MHz", |
| 85 | [5] = "Colibri PXA310 624MHz", |
| 86 | [6] = "Colibri PXA320 806MHz IT", |
| 87 | [7] = "Colibri PXA300 208MHz XT", |
| 88 | [8] = "Colibri PXA270 312MHz", |
| 89 | [9] = "Colibri PXA270 520MHz", |
| 90 | [10] = "Colibri VF50 128MB", /* not currently on sale */ |
| 91 | [11] = "Colibri VF61 256MB", |
| 92 | [12] = "Colibri VF61 256MB IT", |
| 93 | [13] = "Colibri VF50 128MB IT", |
| 94 | [14] = "Colibri iMX6 Solo 256MB", |
| 95 | [15] = "Colibri iMX6 DualLite 512MB", |
| 96 | [16] = "Colibri iMX6 Solo 256MB IT", |
| 97 | [17] = "Colibri iMX6 DualLite 512MB IT", |
| 98 | [18] = "UNKNOWN MODULE", |
| 99 | [19] = "UNKNOWN MODULE", |
| 100 | [20] = "Colibri T20 256MB", |
| 101 | [21] = "Colibri T20 512MB", |
| 102 | [22] = "Colibri T20 512MB IT", |
| 103 | [23] = "Colibri T30 1GB", |
| 104 | [24] = "Colibri T20 256MB IT", |
| 105 | [25] = "Apalis T30 2GB", |
| 106 | [26] = "Apalis T30 1GB", |
| 107 | [27] = "Apalis iMX6 Quad 1GB", |
| 108 | [28] = "Apalis iMX6 Quad 2GB IT", |
| 109 | [29] = "Apalis iMX6 Dual 512MB", |
| 110 | [30] = "Colibri T30 1GB IT", |
| 111 | [31] = "Apalis T30 1GB IT", |
| 112 | [32] = "Colibri iMX7 Solo 256MB", |
| 113 | [33] = "Colibri iMX7 Dual 512MB", |
| 114 | [34] = "Apalis TK1 2GB", |
| 115 | [35] = "Apalis iMX6 Dual 1GB IT", |
Stefan Agner | 01875e9 | 2018-05-30 19:01:47 +0200 | [diff] [blame] | 116 | [36] = "Colibri iMX6ULL 256MB", |
Marcel Ziswiler | 2658c8e | 2019-04-09 17:25:32 +0200 | [diff] [blame] | 117 | [37] = "Apalis iMX8 QuadMax 4GB Wi-Fi / BT IT", |
| 118 | [38] = "Colibri iMX8 QuadXPlus 2GB Wi-Fi / BT IT", |
Stefan Agner | 01875e9 | 2018-05-30 19:01:47 +0200 | [diff] [blame] | 119 | [39] = "Colibri iMX7 Dual 1GB (eMMC)", |
Marcel Ziswiler | 2658c8e | 2019-04-09 17:25:32 +0200 | [diff] [blame] | 120 | [40] = "Colibri iMX6ULL 512MB Wi-Fi / BT IT", |
Stefan Agner | 01875e9 | 2018-05-30 19:01:47 +0200 | [diff] [blame] | 121 | [41] = "Colibri iMX7 Dual 512MB EPDC", |
| 122 | [42] = "Apalis TK1 4GB", |
Gerard Salvatella | 5ab3b1d | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 123 | [43] = "Colibri T20 512MB IT SETEK", |
| 124 | [44] = "Colibri iMX6ULL 512MB IT", |
| 125 | [45] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth", |
Marcel Ziswiler | f60ffec | 2019-04-09 17:25:33 +0200 | [diff] [blame] | 126 | [46] = "Apalis iMX8 QuadXPlus 2GB Wi-Fi / BT IT", |
| 127 | [47] = "Apalis iMX8 QuadMax 4GB IT", |
| 128 | [48] = "Apalis iMX8 QuadPlus 2GB Wi-Fi / BT", |
| 129 | [49] = "Apalis iMX8 QuadPlus 2GB", |
| 130 | [50] = "Colibri iMX8 QuadXPlus 2GB IT", |
| 131 | [51] = "Colibri iMX8 DualX 1GB Wi-Fi / Bluetooth", |
| 132 | [52] = "Colibri iMX8 DualX 1GB", |
Marcel Ziswiler | 75b9327 | 2020-01-28 14:42:23 +0100 | [diff] [blame] | 133 | [53] = "Apalis iMX8 QuadXPlus 2GB ECC IT", |
| 134 | [54] = "Apalis iMX8 DualXPlus 1GB", |
Marcel Ziswiler | eca26ba | 2020-01-28 14:42:24 +0100 | [diff] [blame] | 135 | [55] = "Verdin iMX8M Mini Quad 2GB Wi-Fi / BT IT", |
Marcel Ziswiler | 4a9c702 | 2020-10-28 11:58:08 +0200 | [diff] [blame] | 136 | [56] = "Verdin iMX8M Nano Quad 1GB Wi-Fi / BT", /* not currently on sale */ |
Marcel Ziswiler | eca26ba | 2020-01-28 14:42:24 +0100 | [diff] [blame] | 137 | [57] = "Verdin iMX8M Mini DualLite 1GB", |
Marcel Ziswiler | 4a9c702 | 2020-10-28 11:58:08 +0200 | [diff] [blame] | 138 | [58] = "Verdin iMX8M Plus Quad 4GB Wi-Fi / BT IT", |
| 139 | [59] = "Verdin iMX8M Mini Quad 2GB IT", |
| 140 | [60] = "Verdin iMX8M Mini DualLite 1GB WB IT", |
| 141 | [61] = "Verdin iMX8M Plus Quad 2GB", |
Marcel Ziswiler | c066a44 | 2021-10-06 18:55:34 +0200 | [diff] [blame] | 142 | [62] = "Colibri iMX6ULL 1GB IT (eMMC)", |
| 143 | [63] = "Verdin iMX8M Plus Quad 4GB IT", |
| 144 | [64] = "Verdin iMX8M Plus Quad 2GB Wi-Fi / BT IT", |
| 145 | [65] = "Verdin iMX8M Plus QuadLite 1GB IT", |
| 146 | [66] = "Verdin iMX8M Plus Quad 8GB Wi-Fi / BT", |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 147 | }; |
| 148 | |
Igor Opaniuk | 414df35 | 2020-07-15 13:30:54 +0300 | [diff] [blame] | 149 | const char * const toradex_carrier_boards[] = { |
| 150 | [0] = "UNKNOWN CARRIER BOARD", |
| 151 | [155] = "Dahlia", |
| 152 | [156] = "Verdin Development Board", |
| 153 | }; |
| 154 | |
| 155 | const char * const toradex_display_adapters[] = { |
| 156 | [0] = "UNKNOWN DISPLAY ADAPTER", |
| 157 | [157] = "Verdin DSI to HDMI Adapter", |
| 158 | [159] = "Verdin DSI to LVDS Adapter", |
| 159 | }; |
| 160 | |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 161 | #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC |
| 162 | static int tdx_cfg_block_mmc_storage(u8 *config_block, int write) |
| 163 | { |
| 164 | struct mmc *mmc; |
| 165 | int dev = CONFIG_TDX_CFG_BLOCK_DEV; |
| 166 | int offset = CONFIG_TDX_CFG_BLOCK_OFFSET; |
| 167 | uint part = CONFIG_TDX_CFG_BLOCK_PART; |
| 168 | uint blk_start; |
| 169 | int ret = 0; |
| 170 | |
| 171 | /* Read production parameter config block from eMMC */ |
| 172 | mmc = find_mmc_device(dev); |
| 173 | if (!mmc) { |
| 174 | puts("No MMC card found\n"); |
| 175 | ret = -ENODEV; |
| 176 | goto out; |
| 177 | } |
Stefan Agner | dd20234 | 2019-07-12 12:35:05 +0200 | [diff] [blame] | 178 | if (mmc_init(mmc)) { |
| 179 | puts("MMC init failed\n"); |
| 180 | return -EINVAL; |
| 181 | } |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 182 | if (part != mmc_get_blk_desc(mmc)->hwpart) { |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 183 | if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) { |
| 184 | puts("MMC partition switch failed\n"); |
| 185 | ret = -ENODEV; |
| 186 | goto out; |
| 187 | } |
| 188 | } |
| 189 | if (offset < 0) |
| 190 | offset += mmc->capacity; |
| 191 | blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len; |
| 192 | |
| 193 | if (!write) { |
| 194 | /* Careful reads a whole block of 512 bytes into config_block */ |
| 195 | if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1, |
| 196 | (unsigned char *)config_block) != 1) { |
| 197 | ret = -EIO; |
| 198 | goto out; |
| 199 | } |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 200 | } else { |
| 201 | /* Just writing one 512 byte block */ |
| 202 | if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1, |
| 203 | (unsigned char *)config_block) != 1) { |
| 204 | ret = -EIO; |
| 205 | goto out; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | out: |
| 210 | /* Switch back to regular eMMC user partition */ |
| 211 | blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0); |
| 212 | |
| 213 | return ret; |
| 214 | } |
| 215 | #endif |
| 216 | |
| 217 | #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND |
| 218 | static int read_tdx_cfg_block_from_nand(unsigned char *config_block) |
| 219 | { |
| 220 | size_t size = TDX_CFG_BLOCK_MAX_SIZE; |
Stefan Agner | 8843b6d | 2018-08-06 09:19:18 +0200 | [diff] [blame] | 221 | struct mtd_info *mtd = get_nand_dev_by_index(0); |
| 222 | |
| 223 | if (!mtd) |
| 224 | return -ENODEV; |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 225 | |
| 226 | /* Read production parameter config block from NAND page */ |
Stefan Agner | 8843b6d | 2018-08-06 09:19:18 +0200 | [diff] [blame] | 227 | return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET, |
Grygorii Strashko | bb31462 | 2017-06-26 19:13:06 -0500 | [diff] [blame] | 228 | &size, NULL, TDX_CFG_BLOCK_MAX_SIZE, |
| 229 | config_block); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static int write_tdx_cfg_block_to_nand(unsigned char *config_block) |
| 233 | { |
| 234 | size_t size = TDX_CFG_BLOCK_MAX_SIZE; |
| 235 | |
| 236 | /* Write production parameter config block to NAND page */ |
Grygorii Strashko | bb31462 | 2017-06-26 19:13:06 -0500 | [diff] [blame] | 237 | return nand_write_skip_bad(get_nand_dev_by_index(0), |
| 238 | CONFIG_TDX_CFG_BLOCK_OFFSET, |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 239 | &size, NULL, TDX_CFG_BLOCK_MAX_SIZE, |
| 240 | config_block, WITH_WR_VERIFY); |
| 241 | } |
| 242 | #endif |
| 243 | |
| 244 | #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR |
| 245 | static int read_tdx_cfg_block_from_nor(unsigned char *config_block) |
| 246 | { |
| 247 | /* Read production parameter config block from NOR flash */ |
| 248 | memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET, |
| 249 | TDX_CFG_BLOCK_MAX_SIZE); |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int write_tdx_cfg_block_to_nor(unsigned char *config_block) |
| 254 | { |
| 255 | /* Write production parameter config block to NOR flash */ |
| 256 | return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET, |
| 257 | TDX_CFG_BLOCK_MAX_SIZE); |
| 258 | } |
| 259 | #endif |
| 260 | |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 261 | #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM |
| 262 | static int read_tdx_cfg_block_from_eeprom(unsigned char *config_block) |
| 263 | { |
| 264 | return read_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block, |
| 265 | TDX_CFG_BLOCK_MAX_SIZE); |
| 266 | } |
| 267 | |
| 268 | static int write_tdx_cfg_block_to_eeprom(unsigned char *config_block) |
| 269 | { |
| 270 | return write_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block, |
| 271 | TDX_CFG_BLOCK_MAX_SIZE); |
| 272 | } |
| 273 | #endif |
| 274 | |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 275 | int read_tdx_cfg_block(void) |
| 276 | { |
| 277 | int ret = 0; |
| 278 | u8 *config_block = NULL; |
| 279 | struct toradex_tag *tag; |
| 280 | size_t size = TDX_CFG_BLOCK_MAX_SIZE; |
| 281 | int offset; |
| 282 | |
| 283 | /* Allocate RAM area for config block */ |
| 284 | config_block = memalign(ARCH_DMA_MINALIGN, size); |
| 285 | if (!config_block) { |
| 286 | printf("Not enough malloc space available!\n"); |
| 287 | return -ENOMEM; |
| 288 | } |
| 289 | |
| 290 | memset(config_block, 0, size); |
| 291 | |
| 292 | #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC) |
| 293 | ret = tdx_cfg_block_mmc_storage(config_block, 0); |
| 294 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND) |
| 295 | ret = read_tdx_cfg_block_from_nand(config_block); |
| 296 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR) |
| 297 | ret = read_tdx_cfg_block_from_nor(config_block); |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 298 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM) |
| 299 | ret = read_tdx_cfg_block_from_eeprom(config_block); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 300 | #else |
| 301 | ret = -EINVAL; |
| 302 | #endif |
| 303 | if (ret) |
| 304 | goto out; |
| 305 | |
| 306 | /* Expect a valid tag first */ |
| 307 | tag = (struct toradex_tag *)config_block; |
| 308 | if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) { |
| 309 | valid_cfgblock = false; |
| 310 | ret = -EINVAL; |
| 311 | goto out; |
| 312 | } |
| 313 | valid_cfgblock = true; |
| 314 | offset = 4; |
| 315 | |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 316 | /* |
| 317 | * check if there is enough space for storing tag and value of the |
| 318 | * biggest element |
| 319 | */ |
| 320 | while (offset + sizeof(struct toradex_tag) + |
| 321 | sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) { |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 322 | tag = (struct toradex_tag *)(config_block + offset); |
| 323 | offset += 4; |
| 324 | if (tag->id == TAG_INVALID) |
| 325 | break; |
| 326 | |
| 327 | if (tag->flags == TAG_FLAG_VALID) { |
| 328 | switch (tag->id) { |
| 329 | case TAG_MAC: |
| 330 | memcpy(&tdx_eth_addr, config_block + offset, |
| 331 | 6); |
| 332 | |
| 333 | /* NIC part of MAC address is serial number */ |
| 334 | tdx_serial = ntohl(tdx_eth_addr.nic) >> 8; |
| 335 | break; |
| 336 | case TAG_HW: |
| 337 | memcpy(&tdx_hw_tag, config_block + offset, 8); |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /* Get to next tag according to current tags length */ |
| 343 | offset += tag->len * 4; |
| 344 | } |
| 345 | |
| 346 | /* Cap product id to avoid issues with a yet unknown one */ |
Marcel Ziswiler | 8c9127c | 2019-03-25 17:18:29 +0100 | [diff] [blame] | 347 | if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) / |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 348 | sizeof(toradex_modules[0]))) |
| 349 | tdx_hw_tag.prodid = 0; |
| 350 | |
| 351 | out: |
| 352 | free(config_block); |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | static int get_cfgblock_interactive(void) |
| 357 | { |
| 358 | char message[CONFIG_SYS_CBSIZE]; |
| 359 | char *soc; |
| 360 | char it = 'n'; |
Marcel Ziswiler | 35e3c6e | 2019-07-12 12:35:06 +0200 | [diff] [blame] | 361 | char wb = 'n'; |
Marcel Ziswiler | eca26ba | 2020-01-28 14:42:24 +0100 | [diff] [blame] | 362 | int len = 0; |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 363 | |
Stefan Agner | 68f5878 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 364 | /* Unknown module by default */ |
| 365 | tdx_hw_tag.prodid = 0; |
| 366 | |
Marcel Ziswiler | 384ca7f | 2022-04-13 11:33:32 +0200 | [diff] [blame] | 367 | sprintf(message, "Is the module an IT version? [y/N] "); |
Denys Drozdov | 8f6eaa9 | 2021-10-06 18:55:33 +0200 | [diff] [blame] | 368 | |
| 369 | len = cli_readline(message); |
| 370 | it = console_buffer[0]; |
Marcel Ziswiler | eca26ba | 2020-01-28 14:42:24 +0100 | [diff] [blame] | 371 | |
Marcel Ziswiler | 35e3c6e | 2019-07-12 12:35:06 +0200 | [diff] [blame] | 372 | #if defined(CONFIG_TARGET_APALIS_IMX8) || \ |
| 373 | defined(CONFIG_TARGET_COLIBRI_IMX6ULL) || \ |
Marcel Ziswiler | 4a9c702 | 2020-10-28 11:58:08 +0200 | [diff] [blame] | 374 | defined(CONFIG_TARGET_COLIBRI_IMX8X) || \ |
| 375 | defined(CONFIG_TARGET_VERDIN_IMX8MM) || \ |
| 376 | defined(CONFIG_TARGET_VERDIN_IMX8MP) |
Marcel Ziswiler | 35e3c6e | 2019-07-12 12:35:06 +0200 | [diff] [blame] | 377 | sprintf(message, "Does the module have Wi-Fi / Bluetooth? [y/N] "); |
| 378 | len = cli_readline(message); |
| 379 | wb = console_buffer[0]; |
| 380 | #endif |
| 381 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 382 | soc = env_get("soc"); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 383 | if (!strcmp("mx6", soc)) { |
Stefan Agner | 68f5878 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 384 | #ifdef CONFIG_TARGET_APALIS_IMX6 |
| 385 | if (it == 'y' || it == 'Y') { |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 386 | if (is_cpu_type(MXC_CPU_MX6Q)) |
| 387 | tdx_hw_tag.prodid = APALIS_IMX6Q_IT; |
| 388 | else |
| 389 | tdx_hw_tag.prodid = APALIS_IMX6D_IT; |
Stefan Agner | 68f5878 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 390 | } else { |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 391 | if (is_cpu_type(MXC_CPU_MX6Q)) |
| 392 | tdx_hw_tag.prodid = APALIS_IMX6Q; |
| 393 | else |
| 394 | tdx_hw_tag.prodid = APALIS_IMX6D; |
Stefan Agner | 68f5878 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 395 | } |
| 396 | #elif CONFIG_TARGET_COLIBRI_IMX6 |
Gerard Salvatella | 5ab3b1d | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 397 | if (it == 'y' || it == 'Y') { |
Stefan Agner | 68f5878 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 398 | if (is_cpu_type(MXC_CPU_MX6DL)) |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 399 | tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT; |
Stefan Agner | 68f5878 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 400 | else if (is_cpu_type(MXC_CPU_MX6SOLO)) |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 401 | tdx_hw_tag.prodid = COLIBRI_IMX6S_IT; |
Gerard Salvatella | 5ab3b1d | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 402 | } else { |
Stefan Agner | 68f5878 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 403 | if (is_cpu_type(MXC_CPU_MX6DL)) |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 404 | tdx_hw_tag.prodid = COLIBRI_IMX6DL; |
Stefan Agner | 68f5878 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 405 | else if (is_cpu_type(MXC_CPU_MX6SOLO)) |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 406 | tdx_hw_tag.prodid = COLIBRI_IMX6S; |
Gerard Salvatella | 5ab3b1d | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 407 | } |
Stefan Agner | 68f5878 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 408 | #elif CONFIG_TARGET_COLIBRI_IMX6ULL |
Stefan Agner | 68f5878 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 409 | if (it == 'y' || it == 'Y') { |
| 410 | if (wb == 'y' || wb == 'Y') |
| 411 | tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT; |
| 412 | else |
Marcel Ziswiler | c066a44 | 2021-10-06 18:55:34 +0200 | [diff] [blame] | 413 | if (gd->ram_size == 0x20000000) |
| 414 | tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT; |
| 415 | else |
| 416 | tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT_EMMC; |
Stefan Agner | 68f5878 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 417 | } else { |
| 418 | if (wb == 'y' || wb == 'Y') |
| 419 | tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT; |
| 420 | else |
| 421 | tdx_hw_tag.prodid = COLIBRI_IMX6ULL; |
| 422 | } |
| 423 | #endif |
Gerard Salvatella | 5ab3b1d | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 424 | } else if (!strcmp("imx7d", soc)) |
Marcel Ziswiler | c066a44 | 2021-10-06 18:55:34 +0200 | [diff] [blame] | 425 | if (gd->ram_size == 0x20000000) |
| 426 | tdx_hw_tag.prodid = COLIBRI_IMX7D; |
| 427 | else |
| 428 | tdx_hw_tag.prodid = COLIBRI_IMX7D_EMMC; |
Gerard Salvatella | 5ab3b1d | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 429 | else if (!strcmp("imx7s", soc)) |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 430 | tdx_hw_tag.prodid = COLIBRI_IMX7S; |
Marcel Ziswiler | 0032061 | 2019-07-12 12:35:08 +0200 | [diff] [blame] | 431 | else if (is_cpu_type(MXC_CPU_IMX8QM)) { |
| 432 | if (it == 'y' || it == 'Y') { |
| 433 | if (wb == 'y' || wb == 'Y') |
| 434 | tdx_hw_tag.prodid = APALIS_IMX8QM_WIFI_BT_IT; |
| 435 | else |
| 436 | tdx_hw_tag.prodid = APALIS_IMX8QM_IT; |
| 437 | } else { |
| 438 | if (wb == 'y' || wb == 'Y') |
| 439 | tdx_hw_tag.prodid = APALIS_IMX8QP_WIFI_BT; |
| 440 | else |
| 441 | tdx_hw_tag.prodid = APALIS_IMX8QP; |
| 442 | } |
| 443 | } else if (is_cpu_type(MXC_CPU_IMX8QXP)) { |
Denys Drozdov | 00b1e93 | 2022-04-13 11:33:25 +0200 | [diff] [blame] | 444 | #ifdef CONFIG_TARGET_COLIBRI_IMX8X |
Marcel Ziswiler | 7e2954f | 2019-07-12 12:35:07 +0200 | [diff] [blame] | 445 | if (it == 'y' || it == 'Y') { |
| 446 | if (wb == 'y' || wb == 'Y') |
| 447 | tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT; |
| 448 | else |
| 449 | tdx_hw_tag.prodid = COLIBRI_IMX8QXP_IT; |
| 450 | } else { |
| 451 | if (wb == 'y' || wb == 'Y') |
| 452 | tdx_hw_tag.prodid = COLIBRI_IMX8DX_WIFI_BT; |
| 453 | else |
| 454 | tdx_hw_tag.prodid = COLIBRI_IMX8DX; |
| 455 | } |
Marcel Ziswiler | 75b9327 | 2020-01-28 14:42:23 +0100 | [diff] [blame] | 456 | #endif |
Marcel Ziswiler | 3834f66 | 2020-10-28 11:58:15 +0200 | [diff] [blame] | 457 | } else if (is_cpu_type(MXC_CPU_IMX8MMDL)) { |
| 458 | if (wb == 'y' || wb == 'Y') |
| 459 | tdx_hw_tag.prodid = VERDIN_IMX8MMDL_WIFI_BT_IT; |
| 460 | else |
| 461 | tdx_hw_tag.prodid = VERDIN_IMX8MMDL; |
Marcel Ziswiler | 4a9c702 | 2020-10-28 11:58:08 +0200 | [diff] [blame] | 462 | } else if (is_cpu_type(MXC_CPU_IMX8MM)) { |
Marcel Ziswiler | 3834f66 | 2020-10-28 11:58:15 +0200 | [diff] [blame] | 463 | if (wb == 'y' || wb == 'Y') |
| 464 | tdx_hw_tag.prodid = VERDIN_IMX8MMQ_WIFI_BT_IT; |
| 465 | else |
| 466 | tdx_hw_tag.prodid = VERDIN_IMX8MMQ_IT; |
Marcel Ziswiler | 4a9c702 | 2020-10-28 11:58:08 +0200 | [diff] [blame] | 467 | } else if (is_cpu_type(MXC_CPU_IMX8MN)) { |
| 468 | tdx_hw_tag.prodid = VERDIN_IMX8MNQ_WIFI_BT; |
Marcel Ziswiler | c066a44 | 2021-10-06 18:55:34 +0200 | [diff] [blame] | 469 | } else if (is_cpu_type(MXC_CPU_IMX8MPL)) { |
| 470 | tdx_hw_tag.prodid = VERDIN_IMX8MPQL_IT; |
Marcel Ziswiler | 4a9c702 | 2020-10-28 11:58:08 +0200 | [diff] [blame] | 471 | } else if (is_cpu_type(MXC_CPU_IMX8MP)) { |
| 472 | if (wb == 'y' || wb == 'Y') |
Marcel Ziswiler | c066a44 | 2021-10-06 18:55:34 +0200 | [diff] [blame] | 473 | if (gd->ram_size == 0x80000000) |
| 474 | tdx_hw_tag.prodid = VERDIN_IMX8MPQ_2GB_WIFI_BT_IT; |
| 475 | else if (gd->ram_size == 0x200000000) |
| 476 | tdx_hw_tag.prodid = VERDIN_IMX8MPQ_8GB_WIFI_BT; |
| 477 | else |
| 478 | tdx_hw_tag.prodid = VERDIN_IMX8MPQ_WIFI_BT_IT; |
Marcel Ziswiler | 4a9c702 | 2020-10-28 11:58:08 +0200 | [diff] [blame] | 479 | else |
Marcel Ziswiler | c066a44 | 2021-10-06 18:55:34 +0200 | [diff] [blame] | 480 | if (it == 'y' || it == 'Y') |
| 481 | tdx_hw_tag.prodid = VERDIN_IMX8MPQ_IT; |
| 482 | else |
| 483 | tdx_hw_tag.prodid = VERDIN_IMX8MPQ; |
Marcel Ziswiler | 7e2954f | 2019-07-12 12:35:07 +0200 | [diff] [blame] | 484 | } else if (!strcmp("tegra20", soc)) { |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 485 | if (it == 'y' || it == 'Y') |
| 486 | if (gd->ram_size == 0x10000000) |
| 487 | tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT; |
| 488 | else |
| 489 | tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT; |
| 490 | else |
| 491 | if (gd->ram_size == 0x10000000) |
| 492 | tdx_hw_tag.prodid = COLIBRI_T20_256MB; |
| 493 | else |
| 494 | tdx_hw_tag.prodid = COLIBRI_T20_512MB; |
Gerard Salvatella | 5ab3b1d | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 495 | } |
Tom Rini | eb6aadc | 2021-08-30 09:16:32 -0400 | [diff] [blame] | 496 | #if defined(CONFIG_TARGET_APALIS_T30) || defined(CONFIG_TARGET_COLIBRI_T30) |
Gerard Salvatella | 5ab3b1d | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 497 | else if (!strcmp("tegra30", soc)) { |
Tom Rini | eb6aadc | 2021-08-30 09:16:32 -0400 | [diff] [blame] | 498 | #ifdef CONFIG_TARGET_APALIS_T30 |
| 499 | if (it == 'y' || it == 'Y') |
| 500 | tdx_hw_tag.prodid = APALIS_T30_IT; |
| 501 | else |
| 502 | if (gd->ram_size == 0x40000000) |
| 503 | tdx_hw_tag.prodid = APALIS_T30_1GB; |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 504 | else |
Tom Rini | eb6aadc | 2021-08-30 09:16:32 -0400 | [diff] [blame] | 505 | tdx_hw_tag.prodid = APALIS_T30_2GB; |
| 506 | #else |
| 507 | if (it == 'y' || it == 'Y') |
| 508 | tdx_hw_tag.prodid = COLIBRI_T30_IT; |
| 509 | else |
| 510 | tdx_hw_tag.prodid = COLIBRI_T30; |
| 511 | #endif |
Gerard Salvatella | 5ab3b1d | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 512 | } |
Tom Rini | eb6aadc | 2021-08-30 09:16:32 -0400 | [diff] [blame] | 513 | #endif /* CONFIG_TARGET_APALIS_T30 || CONFIG_TARGET_COLIBRI_T30 */ |
Gerard Salvatella | 5ab3b1d | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 514 | else if (!strcmp("tegra124", soc)) { |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 515 | tdx_hw_tag.prodid = APALIS_TK1_2GB; |
| 516 | } else if (!strcmp("vf500", soc)) { |
| 517 | if (it == 'y' || it == 'Y') |
| 518 | tdx_hw_tag.prodid = COLIBRI_VF50_IT; |
| 519 | else |
| 520 | tdx_hw_tag.prodid = COLIBRI_VF50; |
| 521 | } else if (!strcmp("vf610", soc)) { |
| 522 | if (it == 'y' || it == 'Y') |
| 523 | tdx_hw_tag.prodid = COLIBRI_VF61_IT; |
| 524 | else |
| 525 | tdx_hw_tag.prodid = COLIBRI_VF61; |
Stefan Agner | 68f5878 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | if (!tdx_hw_tag.prodid) { |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 529 | printf("Module type not detectable due to unknown SoC\n"); |
| 530 | return -1; |
| 531 | } |
| 532 | |
| 533 | while (len < 4) { |
| 534 | sprintf(message, "Enter the module version (e.g. V1.1B): V"); |
| 535 | len = cli_readline(message); |
| 536 | } |
| 537 | |
| 538 | tdx_hw_tag.ver_major = console_buffer[0] - '0'; |
| 539 | tdx_hw_tag.ver_minor = console_buffer[2] - '0'; |
| 540 | tdx_hw_tag.ver_assembly = console_buffer[3] - 'A'; |
| 541 | |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 542 | while (len < 8) { |
| 543 | sprintf(message, "Enter module serial number: "); |
| 544 | len = cli_readline(message); |
| 545 | } |
| 546 | |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 547 | tdx_serial = dectoul(console_buffer, NULL); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 552 | static int get_cfgblock_barcode(char *barcode, struct toradex_hw *tag, |
| 553 | u32 *serial) |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 554 | { |
Denys Drozdov | 3a1c657 | 2021-04-07 15:28:24 +0300 | [diff] [blame] | 555 | char revision[3] = {barcode[6], barcode[7], '\0'}; |
| 556 | |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 557 | if (strlen(barcode) < 16) { |
| 558 | printf("Argument too short, barcode is 16 chars long\n"); |
| 559 | return -1; |
| 560 | } |
| 561 | |
| 562 | /* Get hardware information from the first 8 digits */ |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 563 | tag->ver_major = barcode[4] - '0'; |
| 564 | tag->ver_minor = barcode[5] - '0'; |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 565 | tag->ver_assembly = dectoul(revision, NULL); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 566 | |
| 567 | barcode[4] = '\0'; |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 568 | tag->prodid = dectoul(barcode, NULL); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 569 | |
| 570 | /* Parse second part of the barcode (serial number */ |
| 571 | barcode += 8; |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 572 | *serial = dectoul(barcode, NULL); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 577 | static int write_tag(u8 *config_block, int *offset, int tag_id, |
| 578 | u8 *tag_data, size_t tag_data_size) |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 579 | { |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 580 | struct toradex_tag *tag; |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 581 | |
| 582 | if (!offset || !config_block) |
| 583 | return -EINVAL; |
| 584 | |
| 585 | tag = (struct toradex_tag *)(config_block + *offset); |
| 586 | tag->id = tag_id; |
| 587 | tag->flags = TAG_FLAG_VALID; |
| 588 | /* len is provided as number of 32bit values after the tag */ |
| 589 | tag->len = (tag_data_size + sizeof(u32) - 1) / sizeof(u32); |
| 590 | *offset += sizeof(struct toradex_tag); |
| 591 | if (tag_data && tag_data_size) { |
| 592 | memcpy(config_block + *offset, tag_data, |
| 593 | tag_data_size); |
| 594 | *offset += tag_data_size; |
| 595 | } |
| 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA |
| 601 | int read_tdx_cfg_block_carrier(void) |
| 602 | { |
| 603 | int ret = 0; |
| 604 | u8 *config_block = NULL; |
| 605 | struct toradex_tag *tag; |
| 606 | size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE; |
| 607 | int offset; |
| 608 | |
| 609 | /* Allocate RAM area for carrier config block */ |
| 610 | config_block = memalign(ARCH_DMA_MINALIGN, size); |
| 611 | if (!config_block) { |
| 612 | printf("Not enough malloc space available!\n"); |
| 613 | return -ENOMEM; |
| 614 | } |
| 615 | |
| 616 | memset(config_block, 0, size); |
| 617 | |
| 618 | ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block, |
| 619 | size); |
| 620 | if (ret) |
| 621 | return ret; |
| 622 | |
| 623 | /* Expect a valid tag first */ |
| 624 | tag = (struct toradex_tag *)config_block; |
| 625 | if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) { |
| 626 | valid_cfgblock_carrier = false; |
| 627 | ret = -EINVAL; |
| 628 | goto out; |
| 629 | } |
| 630 | valid_cfgblock_carrier = true; |
| 631 | offset = 4; |
| 632 | |
| 633 | while (offset + sizeof(struct toradex_tag) + |
| 634 | sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) { |
| 635 | tag = (struct toradex_tag *)(config_block + offset); |
| 636 | offset += 4; |
| 637 | if (tag->id == TAG_INVALID) |
| 638 | break; |
| 639 | |
| 640 | if (tag->flags == TAG_FLAG_VALID) { |
| 641 | switch (tag->id) { |
| 642 | case TAG_CAR_SERIAL: |
| 643 | memcpy(&tdx_car_serial, config_block + offset, |
| 644 | sizeof(tdx_car_serial)); |
| 645 | break; |
| 646 | case TAG_HW: |
| 647 | memcpy(&tdx_car_hw_tag, config_block + |
| 648 | offset, 8); |
| 649 | break; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | /* Get to next tag according to current tags length */ |
| 654 | offset += tag->len * 4; |
| 655 | } |
| 656 | out: |
| 657 | free(config_block); |
| 658 | return ret; |
| 659 | } |
| 660 | |
Igor Opaniuk | e9ad67a | 2020-07-15 13:30:56 +0300 | [diff] [blame] | 661 | int check_pid8_sanity(char *pid8) |
| 662 | { |
| 663 | char s_carrierid_verdin_dev[5]; |
| 664 | char s_carrierid_dahlia[5]; |
| 665 | |
| 666 | sprintf(s_carrierid_verdin_dev, "0%d", VERDIN_DEVELOPMENT_BOARD); |
| 667 | sprintf(s_carrierid_dahlia, "0%d", DAHLIA); |
| 668 | |
| 669 | /* sane value check, first 4 chars which represent carrier id */ |
| 670 | if (!strncmp(pid8, s_carrierid_verdin_dev, 4)) |
| 671 | return 0; |
| 672 | |
| 673 | if (!strncmp(pid8, s_carrierid_dahlia, 4)) |
| 674 | return 0; |
| 675 | |
| 676 | return -EINVAL; |
| 677 | } |
| 678 | |
| 679 | int try_migrate_tdx_cfg_block_carrier(void) |
| 680 | { |
| 681 | char pid8[8]; |
| 682 | int offset = 0; |
| 683 | int ret = CMD_RET_SUCCESS; |
| 684 | size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE; |
| 685 | u8 *config_block; |
| 686 | |
| 687 | memset(pid8, 0x0, 8); |
| 688 | ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, (u8 *)pid8, 8); |
| 689 | if (ret) |
| 690 | return ret; |
| 691 | |
| 692 | if (check_pid8_sanity(pid8)) |
| 693 | return -EINVAL; |
| 694 | |
| 695 | /* Allocate RAM area for config block */ |
| 696 | config_block = memalign(ARCH_DMA_MINALIGN, size); |
| 697 | if (!config_block) { |
| 698 | printf("Not enough malloc space available!\n"); |
| 699 | return CMD_RET_FAILURE; |
| 700 | } |
| 701 | |
| 702 | memset(config_block, 0xff, size); |
| 703 | /* we try parse PID8 concatenating zeroed serial number */ |
| 704 | tdx_car_hw_tag.ver_major = pid8[4] - '0'; |
| 705 | tdx_car_hw_tag.ver_minor = pid8[5] - '0'; |
| 706 | tdx_car_hw_tag.ver_assembly = pid8[7] - '0'; |
| 707 | |
| 708 | pid8[4] = '\0'; |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 709 | tdx_car_hw_tag.prodid = dectoul(pid8, NULL); |
Igor Opaniuk | e9ad67a | 2020-07-15 13:30:56 +0300 | [diff] [blame] | 710 | |
| 711 | /* Valid Tag */ |
| 712 | write_tag(config_block, &offset, TAG_VALID, NULL, 0); |
| 713 | |
| 714 | /* Product Tag */ |
| 715 | write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag, |
| 716 | sizeof(tdx_car_hw_tag)); |
| 717 | |
| 718 | /* Serial Tag */ |
| 719 | write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial, |
| 720 | sizeof(tdx_car_serial)); |
| 721 | |
| 722 | memset(config_block + offset, 0, 32 - offset); |
| 723 | ret = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block, |
| 724 | size); |
| 725 | if (ret) { |
| 726 | printf("Failed to write Toradex Extra config block: %d\n", |
| 727 | ret); |
| 728 | ret = CMD_RET_FAILURE; |
| 729 | goto out; |
| 730 | } |
| 731 | |
| 732 | printf("Successfully migrated to Toradex Config Block from PID8\n"); |
| 733 | |
| 734 | out: |
| 735 | free(config_block); |
| 736 | return ret; |
| 737 | } |
| 738 | |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 739 | static int get_cfgblock_carrier_interactive(void) |
| 740 | { |
| 741 | char message[CONFIG_SYS_CBSIZE]; |
| 742 | int len; |
| 743 | |
| 744 | printf("Supported carrier boards:\n"); |
| 745 | printf("CARRIER BOARD NAME\t\t [ID]\n"); |
| 746 | for (int i = 0; i < sizeof(toradex_carrier_boards) / |
| 747 | sizeof(toradex_carrier_boards[0]); i++) |
| 748 | if (toradex_carrier_boards[i]) |
| 749 | printf("%s \t\t [%d]\n", toradex_carrier_boards[i], i); |
| 750 | |
| 751 | sprintf(message, "Choose your carrier board (provide ID): "); |
| 752 | len = cli_readline(message); |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 753 | tdx_car_hw_tag.prodid = dectoul(console_buffer, NULL); |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 754 | |
| 755 | do { |
| 756 | sprintf(message, "Enter carrier board version (e.g. V1.1B): V"); |
| 757 | len = cli_readline(message); |
| 758 | } while (len < 4); |
| 759 | |
| 760 | tdx_car_hw_tag.ver_major = console_buffer[0] - '0'; |
| 761 | tdx_car_hw_tag.ver_minor = console_buffer[2] - '0'; |
| 762 | tdx_car_hw_tag.ver_assembly = console_buffer[3] - 'A'; |
| 763 | |
| 764 | while (len < 8) { |
| 765 | sprintf(message, "Enter carrier board serial number: "); |
| 766 | len = cli_readline(message); |
| 767 | } |
| 768 | |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 769 | tdx_car_serial = dectoul(console_buffer, NULL); |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 770 | |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | static int do_cfgblock_carrier_create(struct cmd_tbl *cmdtp, int flag, int argc, |
| 775 | char * const argv[]) |
| 776 | { |
| 777 | u8 *config_block; |
| 778 | size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE; |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 779 | int offset = 0; |
| 780 | int ret = CMD_RET_SUCCESS; |
| 781 | int err; |
Dominik Sliwa | f1e1059 | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 782 | int force_overwrite = 0; |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 783 | |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 784 | if (argc >= 3) { |
| 785 | if (argv[2][0] == '-' && argv[2][1] == 'y') |
| 786 | force_overwrite = 1; |
| 787 | } |
| 788 | |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 789 | /* Allocate RAM area for config block */ |
| 790 | config_block = memalign(ARCH_DMA_MINALIGN, size); |
| 791 | if (!config_block) { |
| 792 | printf("Not enough malloc space available!\n"); |
| 793 | return CMD_RET_FAILURE; |
| 794 | } |
| 795 | |
| 796 | memset(config_block, 0xff, size); |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 797 | read_tdx_cfg_block_carrier(); |
| 798 | if (valid_cfgblock_carrier && !force_overwrite) { |
| 799 | char message[CONFIG_SYS_CBSIZE]; |
| 800 | |
| 801 | sprintf(message, "A valid Toradex Carrier config block is present, still recreate? [y/N] "); |
| 802 | |
| 803 | if (!cli_readline(message)) |
| 804 | goto out; |
| 805 | |
| 806 | if (console_buffer[0] != 'y' && |
| 807 | console_buffer[0] != 'Y') |
| 808 | goto out; |
| 809 | } |
| 810 | |
| 811 | if (argc < 3 || (force_overwrite && argc < 4)) { |
| 812 | err = get_cfgblock_carrier_interactive(); |
| 813 | } else { |
| 814 | if (force_overwrite) |
| 815 | err = get_cfgblock_barcode(argv[3], &tdx_car_hw_tag, |
| 816 | &tdx_car_serial); |
| 817 | else |
| 818 | err = get_cfgblock_barcode(argv[2], &tdx_car_hw_tag, |
| 819 | &tdx_car_serial); |
| 820 | } |
| 821 | |
| 822 | if (err) { |
| 823 | ret = CMD_RET_FAILURE; |
| 824 | goto out; |
| 825 | } |
| 826 | |
| 827 | /* Valid Tag */ |
| 828 | write_tag(config_block, &offset, TAG_VALID, NULL, 0); |
| 829 | |
| 830 | /* Product Tag */ |
| 831 | write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag, |
| 832 | sizeof(tdx_car_hw_tag)); |
| 833 | |
| 834 | /* Serial Tag */ |
| 835 | write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial, |
| 836 | sizeof(tdx_car_serial)); |
| 837 | |
| 838 | memset(config_block + offset, 0, 32 - offset); |
| 839 | err = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block, |
| 840 | size); |
| 841 | if (err) { |
| 842 | printf("Failed to write Toradex Extra config block: %d\n", |
| 843 | ret); |
| 844 | ret = CMD_RET_FAILURE; |
| 845 | goto out; |
| 846 | } |
| 847 | |
| 848 | printf("Toradex Extra config block successfully written\n"); |
| 849 | |
| 850 | out: |
| 851 | free(config_block); |
| 852 | return ret; |
| 853 | } |
| 854 | |
| 855 | #endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */ |
| 856 | |
| 857 | static int do_cfgblock_create(struct cmd_tbl *cmdtp, int flag, int argc, |
| 858 | char * const argv[]) |
| 859 | { |
| 860 | u8 *config_block; |
| 861 | size_t size = TDX_CFG_BLOCK_MAX_SIZE; |
| 862 | int offset = 0; |
| 863 | int ret = CMD_RET_SUCCESS; |
| 864 | int err; |
| 865 | int force_overwrite = 0; |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 866 | |
Dominik Sliwa | f1e1059 | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 867 | if (argc >= 3) { |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 868 | #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA |
| 869 | if (!strcmp(argv[2], "carrier")) |
| 870 | return do_cfgblock_carrier_create(cmdtp, flag, |
| 871 | --argc, ++argv); |
| 872 | #endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */ |
Dominik Sliwa | f1e1059 | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 873 | if (argv[2][0] == '-' && argv[2][1] == 'y') |
| 874 | force_overwrite = 1; |
| 875 | } |
| 876 | |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 877 | /* Allocate RAM area for config block */ |
| 878 | config_block = memalign(ARCH_DMA_MINALIGN, size); |
| 879 | if (!config_block) { |
| 880 | printf("Not enough malloc space available!\n"); |
| 881 | return CMD_RET_FAILURE; |
| 882 | } |
| 883 | |
| 884 | memset(config_block, 0xff, size); |
| 885 | |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 886 | read_tdx_cfg_block(); |
| 887 | if (valid_cfgblock) { |
| 888 | #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND) |
| 889 | /* |
| 890 | * On NAND devices, recreation is only allowed if the page is |
| 891 | * empty (config block invalid...) |
| 892 | */ |
Marcel Ziswiler | 6cf2f78 | 2019-07-12 12:35:09 +0200 | [diff] [blame] | 893 | printf("NAND erase block %d need to be erased before creating a Toradex config block\n", |
Grygorii Strashko | bb31462 | 2017-06-26 19:13:06 -0500 | [diff] [blame] | 894 | CONFIG_TDX_CFG_BLOCK_OFFSET / |
| 895 | get_nand_dev_by_index(0)->erasesize); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 896 | goto out; |
| 897 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR) |
| 898 | /* |
| 899 | * On NOR devices, recreation is only allowed if the sector is |
| 900 | * empty and write protection is off (config block invalid...) |
| 901 | */ |
Marcel Ziswiler | 6cf2f78 | 2019-07-12 12:35:09 +0200 | [diff] [blame] | 902 | printf("NOR sector at offset 0x%02x need to be erased and unprotected before creating a Toradex config block\n", |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 903 | CONFIG_TDX_CFG_BLOCK_OFFSET); |
| 904 | goto out; |
| 905 | #else |
Dominik Sliwa | f1e1059 | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 906 | if (!force_overwrite) { |
| 907 | char message[CONFIG_SYS_CBSIZE]; |
| 908 | |
| 909 | sprintf(message, |
| 910 | "A valid Toradex config block is present, still recreate? [y/N] "); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 911 | |
Dominik Sliwa | f1e1059 | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 912 | if (!cli_readline(message)) |
| 913 | goto out; |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 914 | |
Dominik Sliwa | f1e1059 | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 915 | if (console_buffer[0] != 'y' && |
| 916 | console_buffer[0] != 'Y') |
| 917 | goto out; |
| 918 | } |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 919 | #endif |
| 920 | } |
| 921 | |
| 922 | /* Parse new Toradex config block data... */ |
Dominik Sliwa | f1e1059 | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 923 | if (argc < 3 || (force_overwrite && argc < 4)) { |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 924 | err = get_cfgblock_interactive(); |
Dominik Sliwa | f1e1059 | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 925 | } else { |
| 926 | if (force_overwrite) |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 927 | err = get_cfgblock_barcode(argv[3], &tdx_hw_tag, |
| 928 | &tdx_serial); |
Dominik Sliwa | f1e1059 | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 929 | else |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 930 | err = get_cfgblock_barcode(argv[2], &tdx_hw_tag, |
| 931 | &tdx_serial); |
Dominik Sliwa | f1e1059 | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 932 | } |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 933 | if (err) { |
| 934 | ret = CMD_RET_FAILURE; |
| 935 | goto out; |
| 936 | } |
| 937 | |
| 938 | /* Convert serial number to MAC address (the storage format) */ |
| 939 | tdx_eth_addr.oui = htonl(0x00142dUL << 8); |
| 940 | tdx_eth_addr.nic = htonl(tdx_serial << 8); |
| 941 | |
| 942 | /* Valid Tag */ |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 943 | write_tag(config_block, &offset, TAG_VALID, NULL, 0); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 944 | |
| 945 | /* Product Tag */ |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 946 | write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_hw_tag, |
| 947 | sizeof(tdx_hw_tag)); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 948 | |
| 949 | /* MAC Tag */ |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 950 | write_tag(config_block, &offset, TAG_MAC, (u8 *)&tdx_eth_addr, |
| 951 | sizeof(tdx_eth_addr)); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 952 | |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 953 | memset(config_block + offset, 0, 32 - offset); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 954 | #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC) |
| 955 | err = tdx_cfg_block_mmc_storage(config_block, 1); |
| 956 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND) |
| 957 | err = write_tdx_cfg_block_to_nand(config_block); |
| 958 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR) |
| 959 | err = write_tdx_cfg_block_to_nor(config_block); |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 960 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM) |
| 961 | err = write_tdx_cfg_block_to_eeprom(config_block); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 962 | #else |
| 963 | err = -EINVAL; |
| 964 | #endif |
| 965 | if (err) { |
| 966 | printf("Failed to write Toradex config block: %d\n", ret); |
| 967 | ret = CMD_RET_FAILURE; |
| 968 | goto out; |
| 969 | } |
| 970 | |
| 971 | printf("Toradex config block successfully written\n"); |
| 972 | |
| 973 | out: |
| 974 | free(config_block); |
| 975 | return ret; |
| 976 | } |
| 977 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 978 | static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc, |
| 979 | char *const argv[]) |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 980 | { |
| 981 | int ret; |
| 982 | |
| 983 | if (argc < 2) |
| 984 | return CMD_RET_USAGE; |
| 985 | |
| 986 | if (!strcmp(argv[1], "create")) { |
| 987 | return do_cfgblock_create(cmdtp, flag, argc, argv); |
| 988 | } else if (!strcmp(argv[1], "reload")) { |
| 989 | ret = read_tdx_cfg_block(); |
| 990 | if (ret) { |
| 991 | printf("Failed to reload Toradex config block: %d\n", |
| 992 | ret); |
| 993 | return CMD_RET_FAILURE; |
| 994 | } |
| 995 | return CMD_RET_SUCCESS; |
| 996 | } |
| 997 | |
| 998 | return CMD_RET_USAGE; |
| 999 | } |
| 1000 | |
Igor Opaniuk | 67c9bfc | 2020-07-15 13:30:55 +0300 | [diff] [blame] | 1001 | U_BOOT_CMD( |
| 1002 | cfgblock, 5, 0, do_cfgblock, |
| 1003 | "Toradex config block handling commands", |
| 1004 | "create [-y] [barcode] - (Re-)create Toradex config block\n" |
| 1005 | "create carrier [-y] [barcode] - (Re-)create Toradex Carrier config block\n" |
| 1006 | "cfgblock reload - Reload Toradex config block from flash" |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 1007 | ); |