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