blob: 671adcc4101adb7c192fb0cb2ab7898f60e25b31 [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>
12
Lukasz Majewskice89eb82018-05-02 16:10:52 +020013#if defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT)
14
Stefan Roese033848e2012-08-16 17:55:41 +000015#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
24static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
25{
26 out_le32(addr, data);
27}
28
29static inline u32 raw_bootcount_load(volatile u32 *addr)
30{
31 return in_le32(addr);
32}
33#else
34static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
35{
36 out_be32(addr, data);
37}
38
39static inline u32 raw_bootcount_load(volatile u32 *addr)
40{
41 return in_be32(addr);
42}
43#endif
Lukasz Majewskice89eb82018-05-02 16:10:52 +020044
45DECLARE_GLOBAL_DATA_PTR;
46static 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
63static 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)
82void bootcount_store(ulong a) {};
83ulong bootcount_load(void) { return 0; }
84#endif /* CONFIG_SPL_BUILD && !CONFIG_SPL_BOOTCOUNT_LIMIT */
85#else
86static inline int bootcount_error(void) { return 0; }
87static inline void bootcount_inc(void) {}
88#endif /* CONFIG_SPL_BOOTCOUNT_LIMIT || CONFIG_BOOTCOUNT_LIMIT */
Lukasz Majewskidc6301b2018-05-02 16:10:51 +020089#endif /* _BOOTCOUNT_H__ */