blob: 7967931113658bacc0521225f0391e78a3708700 [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)
zhikui.cui31f7eab2024-05-17 09:11:12 +0000167
zhikui.cui3b92f382024-08-20 05:56:50 +0000168static inline bool store_boot_layout_is_discrete_default(void)
169{
170 return (BOOTLOADER_MODE_ADVANCE_INIT && g_ssp.boot_layout == BOOT_DISCRETE_DEFAULT);
171}
172
zhikui.cui31f7eab2024-05-17 09:11:12 +0000173static inline bool store_boot_layout_is_discrete_all(void)
174{
175 return g_ssp.boot_layout == BOOT_DISCRETE_ALL;
176}
177
178static inline bool store_boot_layout_is_discrete_bl2(void)
179{
180 return (g_ssp.boot_layout == BOOT_DISCRETE_BL2 ||
181 g_ssp.boot_layout_compat == BOOT_DISCRETE_BL2);
182}
183
Bo Lv72d0e902023-01-02 14:27:34 +0000184/**
185 * @usage: init all the valid storage device
186 *
187 * @init_flag: it's only works for MLC/EMMC driver
188 * 0 NAND_BOOT_NORMAL:normal init, but can't operates the phy data area
189 * 1 NAND_BOOT_UPGRATE:same as 0, but operation on phy data area is allowed
190 * 2 NAND_BOOT_ERASE_PROTECT_CACHE:only erase rsv area
191 * 3 NAND_BOOT_ERASE_ALL:erase whole device
192 * 4 NAND_BOOT_SCRUB_ALL:erase whole device
193 * 5 NAND_SCAN_ID_INIT:only read nand id
194 *
195 * @return: init result
196 * 0 = failed
197 * other = device_index that device probe successfully
198 */
199int store_init(u32 init_flag);
200
201/**
202 * @usage: register a storage device to the valid list
203 *
204 * @param: the description pointer of your storage device
205 *
206 * @return: registration result
207 * 0 = success
208 * 1 = fail
209 */
210int store_register(struct storage_t *store_dev);
211
212/**
213 * @usage: unregister a storage device from the valid list
214 *
215 * @store_dev: the description pointer of your storage device
216 */
217void store_unregister(struct storage_t *store_dev);
218
219/**
220 * @usage: check the type of device is valid on this board
221 *
222 * @type: the device type that you want to check
223 *
224 * @return: is the device valid
225 * 0 = invalid
226 * 1 = valid
227 */
228u8 store_device_valid(enum boot_type_e type);
229
230/**
231 * @usage: set the 'type' device as current device, and you can operates it
232 *
233 * @type: the device type that you want to set
234 *
235 * @return: result of the operation
236 * 0 = success
237 * other = fail
238 */
239int store_set_device(enum boot_type_e type);
240
241/**
242 * @usage: get the type of current storage device
243 *
244 * @return: storage device type
245 */
246enum boot_type_e store_get_type(void);
247
248/**
249 * @usage: get information about the current device
250 *
251 * @info: the pointer for the information
252 *
253 * @return: result of the operation
254 * 0 = success
255 * other = fail
256 */
257int store_get_device_info(struct storage_info_t *info);
258
259/**
260 * @usage: read data from storage device
261 *
262 * @name: partition name, when it's null the target
263 * will regards as whole device.
264 * @off: offset to the 0 address of partition/device
265 * @size: the amount of bytes to read
266 * @buf: pointer of target buffer
267 *
268 * @return: result of the operation
269 * 0 = success
270 * other = fail
271 */
272int store_read(const char *name, loff_t off, size_t size, void *buf);
273
274/**
275 * @usage: write data to storage device
276 *
277 * @name: partition name, when it's null the target
278 * will regards as whole device.
279 * @off: offset to the 0 address of partition/device
280 * @size: the amount of bytes to write
281 * @buf: pointer of source buffer
282 *
283 * @return: result of the operation
284 * 0 = success
285 * other = fail
286 */
287int store_write(const char *name, loff_t off, size_t size, void *buf);
288
289/**
290 * @usage: erase the storage device
291 *
292 * @name: partition name, when it's null the target
293 * will regards as whole device.
294 * @off: offset to the 0 address of partition/device
295 * @size: the amount of bytes to erase
296 * @scrub: scrub flag(scrub operates will works only when the device support)
297 * 0 = no scrub, just erase
298 * 1 = use scrub operates instead of erase
299 * @return: result of the operation
300 * 0 = success
301 * other = fail
302 */
303int store_erase(const char *name, loff_t off, size_t size, int scrub);
304
305/**
306 * @usage: get the partition size or capacity of device
307 *
308 * @name: partition name, when it's null the target
309 * will regards as whole device.
310 *
311 * @return: the amount of bytes to the partition or device size
312 */
313u64 store_part_size(const char *name);
314
315/**
316 * @usage: get the copy number of [name]
317 *
318 * @name: only can be "bl2" or "tpl"/"fip" in discrete mode
319 * be "bootloader" in compact mode
320 * @return: the copy number of the "bootloader" or "tpl"
321 */
322u8 store_boot_copy_num(const char *name);
323
324/**
325 * @usage: get the 1st boot copy number of current device.
326 * for eMMC: 0 -> user partition; 1 -> boot0; 2 -> boot1
327 */
328u8 store_boot_copy_start(void);
329
330/**
331 * @usage: get the bootup index of [name]
332 *
333 * @name: do not care discrete mode or compact mode
334 * "bl2" "spl" could be used as the one romboot loaded
335 * "fip" "devfip" "tpl" or "bootloader" would be the main u-boot.
336 * @return: the copy number of the "bootloader" or "tpl"
337 */
338u8 store_bootup_bootidx(const char *name);
339
340/**
341 * @usage: restore the bootidx/bootdev etc.
342 */
343void store_restore_bootidx(void);
344
345/**
346 * @usage: get the copy size of [name]
347 *
348 * @name: name: only can be "bl2" or "tpl"/"fip" in discrete mode
349 * be "bootloader" in compact mode
350 *
351 * @return: the size of every copy
352 */
353u64 store_boot_copy_size(const char *name);
354
355/**
356 * @usage: read the [name] data from storage device
357 *
zhikui.cui31f7eab2024-05-17 09:11:12 +0000358 * @name: fixed"bootloader"
359 * @copy: which copy you want read, if %0xff store_boot_read() would check if
360 * the valid copy number of bootloader is meet with the minimum
Bo Lv72d0e902023-01-02 14:27:34 +0000361 * @size: the amount of bytes to read
362 * @buf: pointer of the target buffer
363 *
364 * @return: result of the operation
365 * 0 = success
366 * other = fail
367 */
368int store_boot_read(const char *name, u8 copy, size_t size, void *buf);
369
370/**
371 * @usage: write the [name] data into storage device
372 *
zhikui.cui31f7eab2024-05-17 09:11:12 +0000373 * @name: fixed "bootloader"
Bo Lv72d0e902023-01-02 14:27:34 +0000374 * @copy: which copy you want write,
375 * it will write to all copies when copy = BOOT_OPS_ALL
376 * @size: the amount of bytes to write
377 * @buf: pointer of the source buffer
378 *
379 * @return: result of the operation
380 * 0 = success
381 * other = fail
382 */
383int store_boot_write(const char *name, u8 copy, size_t size, void *buf);
384
385/**
386 * @usage: erase the [name] data
387 *
388 * @name: only can be "bl2" or "tpl"/"fip" in discrete mode
389 * be "bootloader" in compact mode
390 * @copy: which copy you want erase,
391 * it will erase all copies when copy = BOOT_OPS_ALL
392 *
393 * @return: result of the operation
394 * 0 = success
395 * other = fail
396 */
397int store_boot_erase(const char *name, u8 copy);
398
399/**
400 * @usage: get the rsv info size
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 *
408 * @return: the amount bytes of the rsv info
409 */
410u32 store_rsv_size(const char *name);
411
412/**
413 * @usage: read the rsv info from storage device
414 *
415 * @name: rsv info name, please refer to
416 * RSV_KEY "key"
417 * RSV_ENV "env"
418 * RSV_DTB "dtb"
419 * RSV_BBT "bbt"
420 * @size: the amount of bytes to read
421 * @buf: pointer of the target buffer
422 *
423 * @return: result of the operation
424 * 0 = success
425 * other = fail
426 */
427int store_rsv_read(const char *name, size_t size, void *buf);
428
429/**
430 * @usage: write the rsv info to the storage device
431 *
432 * @name: rsv info name, please refer to
433 * RSV_KEY "key"
434 * RSV_ENV "env"
435 * RSV_DTB "dtb"
436 * RSV_BBT "bbt"
437 * @size: the amount of bytes to write
438 * @buf: pointer of the source buffer
439 *
440 * @return: result of the operation
441 * 0 = success
442 * other = fail
443 */
444int store_rsv_write(const char *name, size_t size, void *buf);
445
446/**
447 * @usage: erase the rsv info
448 *
449 * @name: rsv info name, please refer to
450 * RSV_KEY "key"
451 * RSV_ENV "env"
452 * RSV_DTB "dtb"
453 * RSV_BBT "bbt"
454 * it will erase all reserve information
455 * when name is null
456 *
457 * @return: result of the operation
458 * 0 = success
459 * other = fail
460 */
461int store_rsv_erase(const char *name);
462
463/**
464 * @usage: turn on/off the protection of rsv info
465 *
466 * @name: rsv info name, please refer to
467 * RSV_KEY "key"
468 * RSV_ENV "env"
469 * RSV_DTB "dtb"
470 * RSV_BBT "bbt"
471 * it will operates all reserve information
472 * when name is null
473 * @ops: turn on/off the rsv info protection
474 * true = turn on the protection
475 * flase = turn off the protection
476 *
477 * @return: result of the operation
478 * 0 = success
479 * other = fail
480 */
481int store_rsv_protect(const char *name, bool ops);
482
483/**
484 * @usage: get bootloader mode for current storage
485 *
486 * @return: result of the operation
487 * 0 = COMPACT_BOOTLOADER
488 * 1 = DISCRETE_BOOTLOADER
489 */
490int store_get_device_bootloader_mode(void);
491
492int sheader_need(void);
493void sheader_load(void *addr);
494
495int store_gpt_read(void *buf);
496int store_gpt_write(void *buf);
497int store_gpt_erase(void);
498
Sam Wu9b53f962023-08-01 19:46:50 +0800499int check_valid_dts(unsigned char *buffer);
jinbiaod6719f12023-12-22 05:34:45 +0000500int store_boot_copy_enable(int index);
Sam Wu9b53f962023-08-01 19:46:50 +0800501
zhikui.cui31f7eab2024-05-17 09:11:12 +0000502u8 mtd_store_boot_copy_num(const char *part_name);
503
Bo Lv72d0e902023-01-02 14:27:34 +0000504#endif/* __STORAGE_H__ */