xref: /openbmc/u-boot/include/bootcount.h (revision 252d41f1)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * (C) Copyright 2012
4  * Stefan Roese, DENX Software Engineering, sr@denx.de.
5  */
6 #ifndef _BOOTCOUNT_H__
7 #define _BOOTCOUNT_H__
8 
9 #include <common.h>
10 #include <asm/io.h>
11 #include <asm/byteorder.h>
12 
13 #if defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT)
14 
15 #if !defined(CONFIG_SYS_BOOTCOUNT_LE) && !defined(CONFIG_SYS_BOOTCOUNT_BE)
16 # if __BYTE_ORDER == __LITTLE_ENDIAN
17 #  define CONFIG_SYS_BOOTCOUNT_LE
18 # else
19 #  define CONFIG_SYS_BOOTCOUNT_BE
20 # endif
21 #endif
22 
23 #ifdef CONFIG_SYS_BOOTCOUNT_LE
24 static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
25 {
26 	out_le32(addr, data);
27 }
28 
29 static inline u32 raw_bootcount_load(volatile u32 *addr)
30 {
31 	return in_le32(addr);
32 }
33 #else
34 static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
35 {
36 	out_be32(addr, data);
37 }
38 
39 static inline u32 raw_bootcount_load(volatile u32 *addr)
40 {
41 	return in_be32(addr);
42 }
43 #endif
44 
45 DECLARE_GLOBAL_DATA_PTR;
46 static inline int bootcount_error(void)
47 {
48 	unsigned long bootcount = bootcount_load();
49 	unsigned long bootlimit = env_get_ulong("bootlimit", 10, 0);
50 
51 	if (bootlimit && bootcount > bootlimit) {
52 		printf("Warning: Bootlimit (%lu) exceeded.", bootlimit);
53 		if (!(gd->flags & GD_FLG_SPL_INIT))
54 			printf(" Using altbootcmd.");
55 		printf("\n");
56 
57 		return 1;
58 	}
59 
60 	return 0;
61 }
62 
63 static inline void bootcount_inc(void)
64 {
65 	unsigned long bootcount = bootcount_load();
66 
67 	if (gd->flags & GD_FLG_SPL_INIT) {
68 		bootcount_store(++bootcount);
69 		return;
70 	}
71 
72 #ifndef CONFIG_SPL_BUILD
73 	/* Only increment bootcount when no bootcount support in SPL */
74 #ifndef CONFIG_SPL_BOOTCOUNT_LIMIT
75 	bootcount_store(++bootcount);
76 #endif
77 	env_set_ulong("bootcount", bootcount);
78 #endif /* !CONFIG_SPL_BUILD */
79 }
80 
81 #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT)
82 void bootcount_store(ulong a) {};
83 ulong bootcount_load(void) { return 0; }
84 #endif /* CONFIG_SPL_BUILD && !CONFIG_SPL_BOOTCOUNT_LIMIT */
85 #else
86 static inline int bootcount_error(void) { return 0; }
87 static inline void bootcount_inc(void) {}
88 #endif /* CONFIG_SPL_BOOTCOUNT_LIMIT || CONFIG_BOOTCOUNT_LIMIT */
89 #endif /* _BOOTCOUNT_H__ */
90