Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: (GPL-2.0+ OR MIT) */ |
| 2 | /* |
| 3 | * Copyright (c) 2019 Amlogic, Inc. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | #ifndef __STORAGE_H__ |
| 7 | #define __STORAGE_H__ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
| 11 | #include <console.h> |
| 12 | #include <watchdog.h> |
| 13 | #include <malloc.h> |
| 14 | #include <asm/byteorder.h> |
Bichao Zheng | 91df781 | 2023-08-08 17:36:56 +0800 | [diff] [blame] | 15 | |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 16 | /* storage plat data */ |
| 17 | #include <asm/amlogic/arch/storage.h> |
| 18 | |
| 19 | #define RSV_INVALID 140 /* rsv invalid error */ |
| 20 | |
| 21 | #define DISPROTECT_KEY BIT(0) |
| 22 | #define DISPROTECT_SECURE BIT(1) |
| 23 | #define DISPROTECT_FBBT BIT(2) |
| 24 | #define DISPROTECT_HYNIX BIT(3) |
| 25 | |
| 26 | #define PART_PROTECT_FLAG BIT(4) |
| 27 | enum boot_type_e { |
| 28 | BOOT_EMMC = 1, |
| 29 | BOOT_SD = 1 << 1, |
| 30 | BOOT_NAND_NFTL = 1 << 2, |
| 31 | BOOT_NAND_MTD = 1 << 3, |
| 32 | BOOT_SNAND = 1 << 4,/* spi nand */ |
| 33 | BOOT_SNOR = 1 << 5,/* spi nor */ |
| 34 | BOOT_NONE = 0xFF |
| 35 | }; |
| 36 | |
| 37 | #define RSV_KEY "key" |
| 38 | #define RSV_ENV "env" |
| 39 | #define RSV_DTB "dtb" |
| 40 | #define RSV_BBT "bbt" |
| 41 | #define RSV_DDR_PARA "ddr_para" |
| 42 | |
| 43 | struct nand_startup_parameter { |
| 44 | int page_size; |
| 45 | int block_size; |
| 46 | int layout_reserve_size; |
| 47 | int pages_per_block; |
| 48 | int setup_data; |
| 49 | /* */ |
| 50 | int page0_disable; |
zhikui.cui | 31f7eab | 2024-05-17 09:11:12 +0000 | [diff] [blame] | 51 | uint32_t reserve[5]; |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
zhikui.cui | 31f7eab | 2024-05-17 09:11:12 +0000 | [diff] [blame] | 54 | union storage_independent_parameter { |
| 55 | struct nand_startup_parameter nsp; |
| 56 | }; |
| 57 | |
| 58 | struct storage_boot_entry { |
| 59 | uint32_t offset; |
| 60 | uint32_t size; |
| 61 | }; |
| 62 | |
| 63 | enum BOOT_LAYOUT_VERS { |
| 64 | BOOT_DISCRETE_DEFAULT = 0, |
| 65 | BOOT_DISCRETE_ALL, |
| 66 | BOOT_DISCRETE_BL2, |
| 67 | BOOT_DISCRETE_MAX, |
| 68 | }; |
| 69 | |
| 70 | struct storage_startup_parameter { |
| 71 | uint8_t boot_device; |
| 72 | uint8_t boot_seq; |
| 73 | uint8_t boot_backups; |
| 74 | unsigned char boot_layout; |
| 75 | struct storage_boot_entry boot_entry[MAX_BOOT_AREA_ENTRIES]; |
| 76 | union storage_independent_parameter sip; |
| 77 | unsigned char boot_layout_compat; |
| 78 | }; |
| 79 | extern struct storage_startup_parameter g_ssp; |
| 80 | |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 81 | typedef struct boot_area_entry { |
| 82 | /* name */ |
| 83 | char name[11]; |
| 84 | /* index */ |
| 85 | uint8_t idx; |
| 86 | uint64_t offset; |
| 87 | uint64_t size; |
| 88 | } boot_area_entry_t; |
| 89 | |
| 90 | struct boot_layout { |
| 91 | boot_area_entry_t *boot_entry; |
| 92 | }; |
| 93 | |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 94 | |
| 95 | struct storage_info_t { |
| 96 | u8 name[32]; |
| 97 | u8 id[8]; |
| 98 | u32 read_unit; |
| 99 | u32 write_unit; |
| 100 | u32 erase_unit; |
| 101 | u64 caps;/* total size */ |
| 102 | u8 mode;/* bootloader mode, compact or discrete */ |
| 103 | }; |
| 104 | |
| 105 | struct storage_t { |
| 106 | enum boot_type_e type; |
| 107 | struct storage_info_t info; |
| 108 | u32 init_flag; |
| 109 | struct list_head list; |
| 110 | int (*get_part_count)(void); |
| 111 | int (*list_part_name)(int idx, char *part_name); |
| 112 | /* when part_name is null, default to ops in whole chip */ |
| 113 | /* int (*block_is_bad)(const char *part_name, loff_t off); */ |
| 114 | u64 (*get_part_size)(const char *part_name); |
| 115 | int (*read)(const char *part_name, loff_t off, |
| 116 | size_t size, void *dest); |
| 117 | int (*write)(const char *part_name, loff_t off, |
| 118 | size_t size, void *source); |
| 119 | int (*erase)(const char *part_name, loff_t off, |
| 120 | size_t size, int flag); |
| 121 | |
| 122 | #define BOOT_OPS_ALL 0xFF/* for cpy parameter operates all copies */ |
| 123 | u8 (*get_copies)(const char *part_name); |
| 124 | u64 (*get_copy_size)(const char *part_name); |
| 125 | int (*boot_read)(const char *part_name, |
| 126 | u8 cpy, size_t size, void *dest); |
| 127 | int (*boot_write)(const char *part_name, |
| 128 | u8 cpy, size_t size, void *source); |
| 129 | int (*boot_erase)(const char *part_name, u8 cpy); |
| 130 | int (*gpt_read)(void *dest); |
| 131 | int (*gpt_write)(void *source); |
| 132 | int (*gpt_erase)(void); |
jinbiao | d6719f1 | 2023-12-22 05:34:45 +0000 | [diff] [blame] | 133 | int (*boot_copy_enable)(int index); |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 134 | u32 (*get_rsv_size)(const char *rsv_name); |
| 135 | int (*read_rsv)(const char *rsv_name, size_t size, void *buf); |
| 136 | int (*write_rsv)(const char *rsv_name, size_t size, void *buf); |
| 137 | int (*erase_rsv)(const char *rsv_name); |
| 138 | int (*protect_rsv)(const char *rsv_name, |
| 139 | bool ops);/*true:on false:off*/ |
Bichao Zheng | 5ced7b5 | 2023-07-25 15:20:20 +0800 | [diff] [blame] | 140 | int (*param_ops)(void); |
jinbiao | 1c8ee41 | 2024-12-16 16:42:58 +0800 | [diff] [blame^] | 141 | int (*ffu_op)(u64 ffu_ver, void *addr, u64 cnt); |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | struct device_node_t { |
| 145 | enum boot_type_e index; |
| 146 | char *type; |
| 147 | int (*pre)(void); |
| 148 | int (*probe)(u32 init_flag); |
| 149 | }; |
| 150 | |
| 151 | /** |
| 152 | * we use a valid device list to manage the storage devices, |
| 153 | * and every type of device can only exist ONE in this list. |
| 154 | * we will scan and add the valid storage device to the list |
| 155 | * in the init process, and of cause you can register the device |
| 156 | * by you own. |
| 157 | */ |
| 158 | #define NAND_BOOT_NORMAL 0 |
| 159 | #define NAND_BOOT_UPGRATE 1 |
| 160 | #define NAND_BOOT_ERASE_PROTECT_CACHE 2 |
| 161 | #define NAND_BOOT_ERASE_ALL 3 |
| 162 | #define NAND_BOOT_SCRUB_ALL 4 |
| 163 | #define NAND_SCAN_ID_INIT 5 |
| 164 | |
| 165 | #define STORE_SCRUB BIT(0) |
| 166 | #define STORE_ERASE_DATA BIT(1) |
| 167 | #define STORE_ERASE_RSV BIT(2) |
Liang Yang | 51736e4 | 2024-10-09 19:03:13 +0800 | [diff] [blame] | 168 | /*erase partition erase_len except bb */ |
| 169 | #define STORE_ERASE_LEN_BB BIT(3) |
zhikui.cui | 31f7eab | 2024-05-17 09:11:12 +0000 | [diff] [blame] | 170 | |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 171 | /** |
| 172 | * @usage: init all the valid storage device |
| 173 | * |
| 174 | * @init_flag: it's only works for MLC/EMMC driver |
| 175 | * 0 NAND_BOOT_NORMAL:normal init, but can't operates the phy data area |
| 176 | * 1 NAND_BOOT_UPGRATE:same as 0, but operation on phy data area is allowed |
| 177 | * 2 NAND_BOOT_ERASE_PROTECT_CACHE:only erase rsv area |
| 178 | * 3 NAND_BOOT_ERASE_ALL:erase whole device |
| 179 | * 4 NAND_BOOT_SCRUB_ALL:erase whole device |
| 180 | * 5 NAND_SCAN_ID_INIT:only read nand id |
| 181 | * |
| 182 | * @return: init result |
| 183 | * 0 = failed |
| 184 | * other = device_index that device probe successfully |
| 185 | */ |
| 186 | int store_init(u32 init_flag); |
| 187 | |
| 188 | /** |
| 189 | * @usage: register a storage device to the valid list |
| 190 | * |
| 191 | * @param: the description pointer of your storage device |
| 192 | * |
| 193 | * @return: registration result |
| 194 | * 0 = success |
| 195 | * 1 = fail |
| 196 | */ |
| 197 | int store_register(struct storage_t *store_dev); |
| 198 | |
| 199 | /** |
| 200 | * @usage: unregister a storage device from the valid list |
| 201 | * |
| 202 | * @store_dev: the description pointer of your storage device |
| 203 | */ |
| 204 | void store_unregister(struct storage_t *store_dev); |
| 205 | |
| 206 | /** |
| 207 | * @usage: check the type of device is valid on this board |
| 208 | * |
| 209 | * @type: the device type that you want to check |
| 210 | * |
| 211 | * @return: is the device valid |
| 212 | * 0 = invalid |
| 213 | * 1 = valid |
| 214 | */ |
| 215 | u8 store_device_valid(enum boot_type_e type); |
| 216 | |
| 217 | /** |
| 218 | * @usage: set the 'type' device as current device, and you can operates it |
| 219 | * |
| 220 | * @type: the device type that you want to set |
| 221 | * |
| 222 | * @return: result of the operation |
| 223 | * 0 = success |
| 224 | * other = fail |
| 225 | */ |
| 226 | int store_set_device(enum boot_type_e type); |
| 227 | |
| 228 | /** |
| 229 | * @usage: get the type of current storage device |
| 230 | * |
| 231 | * @return: storage device type |
| 232 | */ |
| 233 | enum boot_type_e store_get_type(void); |
| 234 | |
| 235 | /** |
| 236 | * @usage: get information about the current device |
| 237 | * |
| 238 | * @info: the pointer for the information |
| 239 | * |
| 240 | * @return: result of the operation |
| 241 | * 0 = success |
| 242 | * other = fail |
| 243 | */ |
| 244 | int store_get_device_info(struct storage_info_t *info); |
| 245 | |
| 246 | /** |
| 247 | * @usage: read data from storage device |
| 248 | * |
| 249 | * @name: partition name, when it's null the target |
| 250 | * will regards as whole device. |
| 251 | * @off: offset to the 0 address of partition/device |
| 252 | * @size: the amount of bytes to read |
| 253 | * @buf: pointer of target buffer |
| 254 | * |
| 255 | * @return: result of the operation |
| 256 | * 0 = success |
| 257 | * other = fail |
| 258 | */ |
| 259 | int store_read(const char *name, loff_t off, size_t size, void *buf); |
| 260 | |
| 261 | /** |
| 262 | * @usage: write data to storage device |
| 263 | * |
| 264 | * @name: partition name, when it's null the target |
| 265 | * will regards as whole device. |
| 266 | * @off: offset to the 0 address of partition/device |
| 267 | * @size: the amount of bytes to write |
| 268 | * @buf: pointer of source buffer |
| 269 | * |
| 270 | * @return: result of the operation |
| 271 | * 0 = success |
| 272 | * other = fail |
| 273 | */ |
| 274 | int store_write(const char *name, loff_t off, size_t size, void *buf); |
| 275 | |
| 276 | /** |
| 277 | * @usage: erase the storage device |
| 278 | * |
| 279 | * @name: partition name, when it's null the target |
| 280 | * will regards as whole device. |
| 281 | * @off: offset to the 0 address of partition/device |
| 282 | * @size: the amount of bytes to erase |
| 283 | * @scrub: scrub flag(scrub operates will works only when the device support) |
| 284 | * 0 = no scrub, just erase |
| 285 | * 1 = use scrub operates instead of erase |
| 286 | * @return: result of the operation |
| 287 | * 0 = success |
| 288 | * other = fail |
| 289 | */ |
| 290 | int store_erase(const char *name, loff_t off, size_t size, int scrub); |
| 291 | |
| 292 | /** |
| 293 | * @usage: get the partition size or capacity of device |
| 294 | * |
| 295 | * @name: partition name, when it's null the target |
| 296 | * will regards as whole device. |
| 297 | * |
| 298 | * @return: the amount of bytes to the partition or device size |
| 299 | */ |
| 300 | u64 store_part_size(const char *name); |
| 301 | |
| 302 | /** |
| 303 | * @usage: get the copy number of [name] |
| 304 | * |
| 305 | * @name: only can be "bl2" or "tpl"/"fip" in discrete mode |
| 306 | * be "bootloader" in compact mode |
| 307 | * @return: the copy number of the "bootloader" or "tpl" |
| 308 | */ |
| 309 | u8 store_boot_copy_num(const char *name); |
| 310 | |
| 311 | /** |
| 312 | * @usage: get the 1st boot copy number of current device. |
| 313 | * for eMMC: 0 -> user partition; 1 -> boot0; 2 -> boot1 |
| 314 | */ |
| 315 | u8 store_boot_copy_start(void); |
| 316 | |
| 317 | /** |
| 318 | * @usage: get the bootup index of [name] |
| 319 | * |
| 320 | * @name: do not care discrete mode or compact mode |
| 321 | * "bl2" "spl" could be used as the one romboot loaded |
| 322 | * "fip" "devfip" "tpl" or "bootloader" would be the main u-boot. |
| 323 | * @return: the copy number of the "bootloader" or "tpl" |
| 324 | */ |
| 325 | u8 store_bootup_bootidx(const char *name); |
| 326 | |
| 327 | /** |
| 328 | * @usage: restore the bootidx/bootdev etc. |
| 329 | */ |
| 330 | void store_restore_bootidx(void); |
| 331 | |
| 332 | /** |
| 333 | * @usage: get the copy size of [name] |
| 334 | * |
| 335 | * @name: name: only can be "bl2" or "tpl"/"fip" in discrete mode |
| 336 | * be "bootloader" in compact mode |
| 337 | * |
| 338 | * @return: the size of every copy |
| 339 | */ |
| 340 | u64 store_boot_copy_size(const char *name); |
| 341 | |
| 342 | /** |
| 343 | * @usage: read the [name] data from storage device |
| 344 | * |
zhikui.cui | 31f7eab | 2024-05-17 09:11:12 +0000 | [diff] [blame] | 345 | * @name: fixed"bootloader" |
| 346 | * @copy: which copy you want read, if %0xff store_boot_read() would check if |
| 347 | * the valid copy number of bootloader is meet with the minimum |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 348 | * @size: the amount of bytes to read |
| 349 | * @buf: pointer of the target buffer |
| 350 | * |
| 351 | * @return: result of the operation |
| 352 | * 0 = success |
| 353 | * other = fail |
| 354 | */ |
| 355 | int store_boot_read(const char *name, u8 copy, size_t size, void *buf); |
| 356 | |
| 357 | /** |
| 358 | * @usage: write the [name] data into storage device |
| 359 | * |
zhikui.cui | 31f7eab | 2024-05-17 09:11:12 +0000 | [diff] [blame] | 360 | * @name: fixed "bootloader" |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 361 | * @copy: which copy you want write, |
| 362 | * it will write to all copies when copy = BOOT_OPS_ALL |
| 363 | * @size: the amount of bytes to write |
| 364 | * @buf: pointer of the source buffer |
| 365 | * |
| 366 | * @return: result of the operation |
| 367 | * 0 = success |
| 368 | * other = fail |
| 369 | */ |
| 370 | int store_boot_write(const char *name, u8 copy, size_t size, void *buf); |
| 371 | |
| 372 | /** |
| 373 | * @usage: erase the [name] data |
| 374 | * |
| 375 | * @name: only can be "bl2" or "tpl"/"fip" in discrete mode |
| 376 | * be "bootloader" in compact mode |
| 377 | * @copy: which copy you want erase, |
| 378 | * it will erase all copies when copy = BOOT_OPS_ALL |
| 379 | * |
| 380 | * @return: result of the operation |
| 381 | * 0 = success |
| 382 | * other = fail |
| 383 | */ |
| 384 | int store_boot_erase(const char *name, u8 copy); |
| 385 | |
| 386 | /** |
| 387 | * @usage: get the rsv info size |
| 388 | * |
| 389 | * @name: rsv info name, please refer to |
| 390 | * RSV_KEY "key" |
| 391 | * RSV_ENV "env" |
| 392 | * RSV_DTB "dtb" |
| 393 | * RSV_BBT "bbt" |
| 394 | * |
| 395 | * @return: the amount bytes of the rsv info |
| 396 | */ |
| 397 | u32 store_rsv_size(const char *name); |
| 398 | |
| 399 | /** |
| 400 | * @usage: read the rsv info from storage device |
| 401 | * |
| 402 | * @name: rsv info name, please refer to |
| 403 | * RSV_KEY "key" |
| 404 | * RSV_ENV "env" |
| 405 | * RSV_DTB "dtb" |
| 406 | * RSV_BBT "bbt" |
| 407 | * @size: the amount of bytes to read |
| 408 | * @buf: pointer of the target buffer |
| 409 | * |
| 410 | * @return: result of the operation |
| 411 | * 0 = success |
| 412 | * other = fail |
| 413 | */ |
| 414 | int store_rsv_read(const char *name, size_t size, void *buf); |
| 415 | |
| 416 | /** |
| 417 | * @usage: write the rsv info to the storage device |
| 418 | * |
| 419 | * @name: rsv info name, please refer to |
| 420 | * RSV_KEY "key" |
| 421 | * RSV_ENV "env" |
| 422 | * RSV_DTB "dtb" |
| 423 | * RSV_BBT "bbt" |
| 424 | * @size: the amount of bytes to write |
| 425 | * @buf: pointer of the source buffer |
| 426 | * |
| 427 | * @return: result of the operation |
| 428 | * 0 = success |
| 429 | * other = fail |
| 430 | */ |
| 431 | int store_rsv_write(const char *name, size_t size, void *buf); |
| 432 | |
| 433 | /** |
| 434 | * @usage: erase the rsv info |
| 435 | * |
| 436 | * @name: rsv info name, please refer to |
| 437 | * RSV_KEY "key" |
| 438 | * RSV_ENV "env" |
| 439 | * RSV_DTB "dtb" |
| 440 | * RSV_BBT "bbt" |
| 441 | * it will erase all reserve information |
| 442 | * when name is null |
| 443 | * |
| 444 | * @return: result of the operation |
| 445 | * 0 = success |
| 446 | * other = fail |
| 447 | */ |
| 448 | int store_rsv_erase(const char *name); |
| 449 | |
| 450 | /** |
| 451 | * @usage: turn on/off the protection of rsv info |
| 452 | * |
| 453 | * @name: rsv info name, please refer to |
| 454 | * RSV_KEY "key" |
| 455 | * RSV_ENV "env" |
| 456 | * RSV_DTB "dtb" |
| 457 | * RSV_BBT "bbt" |
| 458 | * it will operates all reserve information |
| 459 | * when name is null |
| 460 | * @ops: turn on/off the rsv info protection |
| 461 | * true = turn on the protection |
| 462 | * flase = turn off the protection |
| 463 | * |
| 464 | * @return: result of the operation |
| 465 | * 0 = success |
| 466 | * other = fail |
| 467 | */ |
| 468 | int store_rsv_protect(const char *name, bool ops); |
| 469 | |
| 470 | /** |
| 471 | * @usage: get bootloader mode for current storage |
| 472 | * |
| 473 | * @return: result of the operation |
| 474 | * 0 = COMPACT_BOOTLOADER |
| 475 | * 1 = DISCRETE_BOOTLOADER |
| 476 | */ |
| 477 | int store_get_device_bootloader_mode(void); |
| 478 | |
| 479 | int sheader_need(void); |
| 480 | void sheader_load(void *addr); |
| 481 | |
| 482 | int store_gpt_read(void *buf); |
| 483 | int store_gpt_write(void *buf); |
| 484 | int store_gpt_erase(void); |
| 485 | |
Sam Wu | 9b53f96 | 2023-08-01 19:46:50 +0800 | [diff] [blame] | 486 | int check_valid_dts(unsigned char *buffer); |
jinbiao | d6719f1 | 2023-12-22 05:34:45 +0000 | [diff] [blame] | 487 | int store_boot_copy_enable(int index); |
Sam Wu | 9b53f96 | 2023-08-01 19:46:50 +0800 | [diff] [blame] | 488 | |
zhikui.cui | 31f7eab | 2024-05-17 09:11:12 +0000 | [diff] [blame] | 489 | u8 mtd_store_boot_copy_num(const char *part_name); |
| 490 | |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 491 | #endif/* __STORAGE_H__ */ |