]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
0044c42e SR |
2 | /* |
3 | * (C) Copyright 2012 | |
4 | * Stefan Roese, DENX Software Engineering, [email protected]. | |
0044c42e | 5 | */ |
c1cd21db LM |
6 | #ifndef _BOOTCOUNT_H__ |
7 | #define _BOOTCOUNT_H__ | |
0044c42e SR |
8 | |
9 | #include <common.h> | |
10 | #include <asm/io.h> | |
11 | #include <asm/byteorder.h> | |
168068fb | 12 | #include <env.h> |
0044c42e | 13 | |
ebb73de1 PT |
14 | #ifdef CONFIG_DM_BOOTCOUNT |
15 | ||
16 | struct 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 | */ | |
49 | int 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 | */ | |
58 | int dm_bootcount_set(struct udevice *dev, u32 bootcount); | |
59 | ||
60 | #endif | |
61 | ||
0da70412 LM |
62 | #if defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT) |
63 | ||
0044c42e SR |
64 | #if !defined(CONFIG_SYS_BOOTCOUNT_LE) && !defined(CONFIG_SYS_BOOTCOUNT_BE) |
65 | # if __BYTE_ORDER == __LITTLE_ENDIAN | |
66 | # define CONFIG_SYS_BOOTCOUNT_LE | |
67 | # else | |
68 | # define CONFIG_SYS_BOOTCOUNT_BE | |
69 | # endif | |
70 | #endif | |
71 | ||
72 | #ifdef CONFIG_SYS_BOOTCOUNT_LE | |
73 | static inline void raw_bootcount_store(volatile u32 *addr, u32 data) | |
74 | { | |
75 | out_le32(addr, data); | |
76 | } | |
77 | ||
78 | static inline u32 raw_bootcount_load(volatile u32 *addr) | |
79 | { | |
80 | return in_le32(addr); | |
81 | } | |
82 | #else | |
83 | static inline void raw_bootcount_store(volatile u32 *addr, u32 data) | |
84 | { | |
85 | out_be32(addr, data); | |
86 | } | |
87 | ||
88 | static inline u32 raw_bootcount_load(volatile u32 *addr) | |
89 | { | |
90 | return in_be32(addr); | |
91 | } | |
92 | #endif | |
0da70412 LM |
93 | |
94 | DECLARE_GLOBAL_DATA_PTR; | |
95 | static inline int bootcount_error(void) | |
96 | { | |
97 | unsigned long bootcount = bootcount_load(); | |
98 | unsigned long bootlimit = env_get_ulong("bootlimit", 10, 0); | |
99 | ||
100 | if (bootlimit && bootcount > bootlimit) { | |
101 | printf("Warning: Bootlimit (%lu) exceeded.", bootlimit); | |
102 | if (!(gd->flags & GD_FLG_SPL_INIT)) | |
103 | printf(" Using altbootcmd."); | |
104 | printf("\n"); | |
105 | ||
106 | return 1; | |
107 | } | |
108 | ||
109 | return 0; | |
110 | } | |
111 | ||
112 | static inline void bootcount_inc(void) | |
113 | { | |
114 | unsigned long bootcount = bootcount_load(); | |
115 | ||
116 | if (gd->flags & GD_FLG_SPL_INIT) { | |
117 | bootcount_store(++bootcount); | |
118 | return; | |
119 | } | |
120 | ||
121 | #ifndef CONFIG_SPL_BUILD | |
122 | /* Only increment bootcount when no bootcount support in SPL */ | |
123 | #ifndef CONFIG_SPL_BOOTCOUNT_LIMIT | |
124 | bootcount_store(++bootcount); | |
125 | #endif | |
126 | env_set_ulong("bootcount", bootcount); | |
127 | #endif /* !CONFIG_SPL_BUILD */ | |
128 | } | |
129 | ||
130 | #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT) | |
131 | void bootcount_store(ulong a) {}; | |
132 | ulong bootcount_load(void) { return 0; } | |
133 | #endif /* CONFIG_SPL_BUILD && !CONFIG_SPL_BOOTCOUNT_LIMIT */ | |
134 | #else | |
135 | static inline int bootcount_error(void) { return 0; } | |
136 | static inline void bootcount_inc(void) {} | |
137 | #endif /* CONFIG_SPL_BOOTCOUNT_LIMIT || CONFIG_BOOTCOUNT_LIMIT */ | |
c1cd21db | 138 | #endif /* _BOOTCOUNT_H__ */ |