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