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