blob: fccee7e15bf73c2ec9605fbfa2eee3a1a26702c2 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Stefan Roese0044c422012-08-16 17:55:41 +00002/*
3 * (C) Copyright 2012
4 * Stefan Roese, DENX Software Engineering, sr@denx.de.
Stefan Roese0044c422012-08-16 17:55:41 +00005 */
Lukasz Majewskic1cd21d2018-05-02 16:10:51 +02006#ifndef _BOOTCOUNT_H__
7#define _BOOTCOUNT_H__
Stefan Roese0044c422012-08-16 17:55:41 +00008
9#include <common.h>
Simon Glass401d1c42020-10-30 21:38:53 -060010#include <asm/global_data.h>
Stefan Roese0044c422012-08-16 17:55:41 +000011#include <asm/io.h>
12#include <asm/byteorder.h>
Simon Glass168068f2019-08-01 09:46:47 -060013#include <env.h>
Stefan Roese0044c422012-08-16 17:55:41 +000014
Philipp Tomsichebb73de2018-11-27 23:00:18 +010015#ifdef CONFIG_DM_BOOTCOUNT
16
17struct bootcount_ops {
18 /**
19 * get() - get the current bootcount value
20 *
21 * Returns the current counter value of the bootcount backing
22 * store.
23 *
24 * @dev: Device to read from
25 * @bootcount: Address to put the current bootcount value
26 */
27 int (*get)(struct udevice *dev, u32 *bootcount);
28
29 /**
30 * set() - set a bootcount value (e.g. to reset or increment)
31 *
32 * Sets the value in the bootcount backing store.
33 *
34 * @dev: Device to read from
35 * @bootcount: New bootcount value to store
36 */
37 int (*set)(struct udevice *dev, const u32 bootcount);
38};
39
40/* Access the operations for a bootcount device */
41#define bootcount_get_ops(dev) ((struct bootcount_ops *)(dev)->driver->ops)
42
43/**
44 * dm_bootcount_get() - Read the current value from a bootcount storage
45 *
46 * @dev: Device to read from
47 * @bootcount: Place to put the current bootcount
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010048 * Return: 0 if OK, -ve on error
Philipp Tomsichebb73de2018-11-27 23:00:18 +010049 */
50int dm_bootcount_get(struct udevice *dev, u32 *bootcount);
51
52/**
53 * dm_bootcount_set() - Write a value to a bootcount storage
54 *
55 * @dev: Device to read from
56 * @bootcount: Value to be written to the backing storage
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010057 * Return: 0 if OK, -ve on error
Philipp Tomsichebb73de2018-11-27 23:00:18 +010058 */
59int dm_bootcount_set(struct udevice *dev, u32 bootcount);
60
61#endif
62
Simon Glassf0835832019-11-14 12:57:18 -070063/** bootcount_store() - store the current bootcount */
64void bootcount_store(ulong);
65
66/**
67 * bootcount_load() - load the current bootcount
68 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010069 * Return: bootcount, read from the appropriate location
Simon Glassf0835832019-11-14 12:57:18 -070070 */
71ulong bootcount_load(void);
72
Philippe Reynes4d145f22020-12-11 19:56:47 +010073#if defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_TPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT)
Lukasz Majewski0da70412018-05-02 16:10:52 +020074
Stefan Roese0044c422012-08-16 17:55:41 +000075#if !defined(CONFIG_SYS_BOOTCOUNT_LE) && !defined(CONFIG_SYS_BOOTCOUNT_BE)
76# if __BYTE_ORDER == __LITTLE_ENDIAN
77# define CONFIG_SYS_BOOTCOUNT_LE
78# else
79# define CONFIG_SYS_BOOTCOUNT_BE
80# endif
81#endif
82
83#ifdef CONFIG_SYS_BOOTCOUNT_LE
84static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
85{
86 out_le32(addr, data);
87}
88
89static inline u32 raw_bootcount_load(volatile u32 *addr)
90{
91 return in_le32(addr);
92}
93#else
94static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
95{
96 out_be32(addr, data);
97}
98
99static inline u32 raw_bootcount_load(volatile u32 *addr)
100{
101 return in_be32(addr);
102}
103#endif
Lukasz Majewski0da70412018-05-02 16:10:52 +0200104
105DECLARE_GLOBAL_DATA_PTR;
106static inline int bootcount_error(void)
107{
108 unsigned long bootcount = bootcount_load();
109 unsigned long bootlimit = env_get_ulong("bootlimit", 10, 0);
110
111 if (bootlimit && bootcount > bootlimit) {
112 printf("Warning: Bootlimit (%lu) exceeded.", bootlimit);
113 if (!(gd->flags & GD_FLG_SPL_INIT))
114 printf(" Using altbootcmd.");
115 printf("\n");
116
117 return 1;
118 }
119
120 return 0;
121}
122
123static inline void bootcount_inc(void)
124{
125 unsigned long bootcount = bootcount_load();
126
127 if (gd->flags & GD_FLG_SPL_INIT) {
128 bootcount_store(++bootcount);
129 return;
130 }
131
132#ifndef CONFIG_SPL_BUILD
133 /* Only increment bootcount when no bootcount support in SPL */
Philippe Reynes4d145f22020-12-11 19:56:47 +0100134#if !defined(CONFIG_SPL_BOOTCOUNT_LIMIT) && !defined(CONFIG_TPL_BOOTCOUNT_LIMIT)
Lukasz Majewski0da70412018-05-02 16:10:52 +0200135 bootcount_store(++bootcount);
136#endif
137 env_set_ulong("bootcount", bootcount);
138#endif /* !CONFIG_SPL_BUILD */
139}
140
Lukasz Majewski0da70412018-05-02 16:10:52 +0200141#else
142static inline int bootcount_error(void) { return 0; }
143static inline void bootcount_inc(void) {}
Philippe Reynes4d145f22020-12-11 19:56:47 +0100144#endif /* CONFIG_SPL_BOOTCOUNT_LIMIT || CONFIG_TPL_BOOTCOUNT_LIMIT || CONFIG_BOOTCOUNT_LIMIT */
Lukasz Majewskic1cd21d2018-05-02 16:10:51 +0200145#endif /* _BOOTCOUNT_H__ */