blob: 0509ff0e6afc59007a60f50dcd217e5af65dd886 [file] [log] [blame]
Bo Lv72d0e902023-01-02 14:27:34 +00001/* 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 Zheng91df7812023-08-08 17:36:56 +080015
Bo Lv72d0e902023-01-02 14:27:34 +000016/* 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)
27enum 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
43struct 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.cui31f7eab2024-05-17 09:11:12 +000051 uint32_t reserve[5];
Bo Lv72d0e902023-01-02 14:27:34 +000052};
53
zhikui.cui31f7eab2024-05-17 09:11:12 +000054union storage_independent_parameter {
55 struct nand_startup_parameter nsp;
56};
57
58struct storage_boot_entry {
59 uint32_t offset;
60 uint32_t size;
61};
62
63enum BOOT_LAYOUT_VERS {
64 BOOT_DISCRETE_DEFAULT = 0,
65 BOOT_DISCRETE_ALL,
66 BOOT_DISCRETE_BL2,
67 BOOT_DISCRETE_MAX,
68};
69
70struct 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};
79extern struct storage_startup_parameter g_ssp;
80
Bo Lv72d0e902023-01-02 14:27:34 +000081typedef 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
90struct boot_layout {
91 boot_area_entry_t *boot_entry;
92};
93
Bo Lv72d0e902023-01-02 14:27:34 +000094
95struct 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
105struct 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);
jinbiaod6719f12023-12-22 05:34:45 +0000133 int (*boot_copy_enable)(int index);
Bo Lv72d0e902023-01-02 14:27:34 +0000134 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 Zheng5ced7b52023-07-25 15:20:20 +0800140 int (*param_ops)(void);
Bo Lv72d0e902023-01-02 14:27:34 +0000141};
142
143struct device_node_t {
144 enum boot_type_e index;
145 char *type;
146 int (*pre)(void);
147 int (*probe)(u32 init_flag);
148};
149
150/**
151 * we use a valid device list to manage the storage devices,
152 * and every type of device can only exist ONE in this list.
153 * we will scan and add the valid storage device to the list
154 * in the init process, and of cause you can register the device
155 * by you own.
156 */
157#define NAND_BOOT_NORMAL 0
158#define NAND_BOOT_UPGRATE 1
159#define NAND_BOOT_ERASE_PROTECT_CACHE 2
160#define NAND_BOOT_ERASE_ALL 3
161#define NAND_BOOT_SCRUB_ALL 4
162#define NAND_SCAN_ID_INIT 5
163
164#define STORE_SCRUB BIT(0)
165#define STORE_ERASE_DATA BIT(1)
166#define STORE_ERASE_RSV BIT(2)
Liang Yang51736e42024-10-09 19:03:13 +0800167/*erase partition erase_len except bb */
168#define STORE_ERASE_LEN_BB BIT(3)
zhikui.cui31f7eab2024-05-17 09:11:12 +0000169
Bo Lv72d0e902023-01-02 14:27:34 +0000170/**
171 * @usage: init all the valid storage device
172 *
173 * @init_flag: it's only works for MLC/EMMC driver
174 * 0 NAND_BOOT_NORMAL:normal init, but can't operates the phy data area
175 * 1 NAND_BOOT_UPGRATE:same as 0, but operation on phy data area is allowed
176 * 2 NAND_BOOT_ERASE_PROTECT_CACHE:only erase rsv area
177 * 3 NAND_BOOT_ERASE_ALL:erase whole device
178 * 4 NAND_BOOT_SCRUB_ALL:erase whole device
179 * 5 NAND_SCAN_ID_INIT:only read nand id
180 *
181 * @return: init result
182 * 0 = failed
183 * other = device_index that device probe successfully
184 */
185int store_init(u32 init_flag);
186
187/**
188 * @usage: register a storage device to the valid list
189 *
190 * @param: the description pointer of your storage device
191 *
192 * @return: registration result
193 * 0 = success
194 * 1 = fail
195 */
196int store_register(struct storage_t *store_dev);
197
198/**
199 * @usage: unregister a storage device from the valid list
200 *
201 * @store_dev: the description pointer of your storage device
202 */
203void store_unregister(struct storage_t *store_dev);
204
205/**
206 * @usage: check the type of device is valid on this board
207 *
208 * @type: the device type that you want to check
209 *
210 * @return: is the device valid
211 * 0 = invalid
212 * 1 = valid
213 */
214u8 store_device_valid(enum boot_type_e type);
215
216/**
217 * @usage: set the 'type' device as current device, and you can operates it
218 *
219 * @type: the device type that you want to set
220 *
221 * @return: result of the operation
222 * 0 = success
223 * other = fail
224 */
225int store_set_device(enum boot_type_e type);
226
227/**
228 * @usage: get the type of current storage device
229 *
230 * @return: storage device type
231 */
232enum boot_type_e store_get_type(void);
233
234/**
235 * @usage: get information about the current device
236 *
237 * @info: the pointer for the information
238 *
239 * @return: result of the operation
240 * 0 = success
241 * other = fail
242 */
243int store_get_device_info(struct storage_info_t *info);
244
245/**
246 * @usage: read data from storage device
247 *
248 * @name: partition name, when it's null the target
249 * will regards as whole device.
250 * @off: offset to the 0 address of partition/device
251 * @size: the amount of bytes to read
252 * @buf: pointer of target buffer
253 *
254 * @return: result of the operation
255 * 0 = success
256 * other = fail
257 */
258int store_read(const char *name, loff_t off, size_t size, void *buf);
259
260/**
261 * @usage: write data to storage device
262 *
263 * @name: partition name, when it's null the target
264 * will regards as whole device.
265 * @off: offset to the 0 address of partition/device
266 * @size: the amount of bytes to write
267 * @buf: pointer of source buffer
268 *
269 * @return: result of the operation
270 * 0 = success
271 * other = fail
272 */
273int store_write(const char *name, loff_t off, size_t size, void *buf);
274
275/**
276 * @usage: erase the storage device
277 *
278 * @name: partition name, when it's null the target
279 * will regards as whole device.
280 * @off: offset to the 0 address of partition/device
281 * @size: the amount of bytes to erase
282 * @scrub: scrub flag(scrub operates will works only when the device support)
283 * 0 = no scrub, just erase
284 * 1 = use scrub operates instead of erase
285 * @return: result of the operation
286 * 0 = success
287 * other = fail
288 */
289int store_erase(const char *name, loff_t off, size_t size, int scrub);
290
291/**
292 * @usage: get the partition size or capacity of device
293 *
294 * @name: partition name, when it's null the target
295 * will regards as whole device.
296 *
297 * @return: the amount of bytes to the partition or device size
298 */
299u64 store_part_size(const char *name);
300
301/**
302 * @usage: get the copy number of [name]
303 *
304 * @name: only can be "bl2" or "tpl"/"fip" in discrete mode
305 * be "bootloader" in compact mode
306 * @return: the copy number of the "bootloader" or "tpl"
307 */
308u8 store_boot_copy_num(const char *name);
309
310/**
311 * @usage: get the 1st boot copy number of current device.
312 * for eMMC: 0 -> user partition; 1 -> boot0; 2 -> boot1
313 */
314u8 store_boot_copy_start(void);
315
316/**
317 * @usage: get the bootup index of [name]
318 *
319 * @name: do not care discrete mode or compact mode
320 * "bl2" "spl" could be used as the one romboot loaded
321 * "fip" "devfip" "tpl" or "bootloader" would be the main u-boot.
322 * @return: the copy number of the "bootloader" or "tpl"
323 */
324u8 store_bootup_bootidx(const char *name);
325
326/**
327 * @usage: restore the bootidx/bootdev etc.
328 */
329void store_restore_bootidx(void);
330
331/**
332 * @usage: get the copy size of [name]
333 *
334 * @name: name: only can be "bl2" or "tpl"/"fip" in discrete mode
335 * be "bootloader" in compact mode
336 *
337 * @return: the size of every copy
338 */
339u64 store_boot_copy_size(const char *name);
340
341/**
342 * @usage: read the [name] data from storage device
343 *
zhikui.cui31f7eab2024-05-17 09:11:12 +0000344 * @name: fixed"bootloader"
345 * @copy: which copy you want read, if %0xff store_boot_read() would check if
346 * the valid copy number of bootloader is meet with the minimum
Bo Lv72d0e902023-01-02 14:27:34 +0000347 * @size: the amount of bytes to read
348 * @buf: pointer of the target buffer
349 *
350 * @return: result of the operation
351 * 0 = success
352 * other = fail
353 */
354int store_boot_read(const char *name, u8 copy, size_t size, void *buf);
355
356/**
357 * @usage: write the [name] data into storage device
358 *
zhikui.cui31f7eab2024-05-17 09:11:12 +0000359 * @name: fixed "bootloader"
Bo Lv72d0e902023-01-02 14:27:34 +0000360 * @copy: which copy you want write,
361 * it will write to all copies when copy = BOOT_OPS_ALL
362 * @size: the amount of bytes to write
363 * @buf: pointer of the source buffer
364 *
365 * @return: result of the operation
366 * 0 = success
367 * other = fail
368 */
369int store_boot_write(const char *name, u8 copy, size_t size, void *buf);
370
371/**
372 * @usage: erase the [name] data
373 *
374 * @name: only can be "bl2" or "tpl"/"fip" in discrete mode
375 * be "bootloader" in compact mode
376 * @copy: which copy you want erase,
377 * it will erase all copies when copy = BOOT_OPS_ALL
378 *
379 * @return: result of the operation
380 * 0 = success
381 * other = fail
382 */
383int store_boot_erase(const char *name, u8 copy);
384
385/**
386 * @usage: get the rsv info size
387 *
388 * @name: rsv info name, please refer to
389 * RSV_KEY "key"
390 * RSV_ENV "env"
391 * RSV_DTB "dtb"
392 * RSV_BBT "bbt"
393 *
394 * @return: the amount bytes of the rsv info
395 */
396u32 store_rsv_size(const char *name);
397
398/**
399 * @usage: read the rsv info from storage device
400 *
401 * @name: rsv info name, please refer to
402 * RSV_KEY "key"
403 * RSV_ENV "env"
404 * RSV_DTB "dtb"
405 * RSV_BBT "bbt"
406 * @size: the amount of bytes to read
407 * @buf: pointer of the target buffer
408 *
409 * @return: result of the operation
410 * 0 = success
411 * other = fail
412 */
413int store_rsv_read(const char *name, size_t size, void *buf);
414
415/**
416 * @usage: write the rsv info to the storage device
417 *
418 * @name: rsv info name, please refer to
419 * RSV_KEY "key"
420 * RSV_ENV "env"
421 * RSV_DTB "dtb"
422 * RSV_BBT "bbt"
423 * @size: the amount of bytes to write
424 * @buf: pointer of the source buffer
425 *
426 * @return: result of the operation
427 * 0 = success
428 * other = fail
429 */
430int store_rsv_write(const char *name, size_t size, void *buf);
431
432/**
433 * @usage: erase the rsv info
434 *
435 * @name: rsv info name, please refer to
436 * RSV_KEY "key"
437 * RSV_ENV "env"
438 * RSV_DTB "dtb"
439 * RSV_BBT "bbt"
440 * it will erase all reserve information
441 * when name is null
442 *
443 * @return: result of the operation
444 * 0 = success
445 * other = fail
446 */
447int store_rsv_erase(const char *name);
448
449/**
450 * @usage: turn on/off the protection of rsv info
451 *
452 * @name: rsv info name, please refer to
453 * RSV_KEY "key"
454 * RSV_ENV "env"
455 * RSV_DTB "dtb"
456 * RSV_BBT "bbt"
457 * it will operates all reserve information
458 * when name is null
459 * @ops: turn on/off the rsv info protection
460 * true = turn on the protection
461 * flase = turn off the protection
462 *
463 * @return: result of the operation
464 * 0 = success
465 * other = fail
466 */
467int store_rsv_protect(const char *name, bool ops);
468
469/**
470 * @usage: get bootloader mode for current storage
471 *
472 * @return: result of the operation
473 * 0 = COMPACT_BOOTLOADER
474 * 1 = DISCRETE_BOOTLOADER
475 */
476int store_get_device_bootloader_mode(void);
477
478int sheader_need(void);
479void sheader_load(void *addr);
480
481int store_gpt_read(void *buf);
482int store_gpt_write(void *buf);
483int store_gpt_erase(void);
484
Sam Wu9b53f962023-08-01 19:46:50 +0800485int check_valid_dts(unsigned char *buffer);
jinbiaod6719f12023-12-22 05:34:45 +0000486int store_boot_copy_enable(int index);
Sam Wu9b53f962023-08-01 19:46:50 +0800487
zhikui.cui31f7eab2024-05-17 09:11:12 +0000488u8 mtd_store_boot_copy_num(const char *part_name);
489
Bo Lv72d0e902023-01-02 14:27:34 +0000490#endif/* __STORAGE_H__ */