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