Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Stefan Roese | 033848e | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2012 |
| 4 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
Stefan Roese | 033848e | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 5 | */ |
Lukasz Majewski | dc6301b | 2018-05-02 16:10:51 +0200 | [diff] [blame] | 6 | #ifndef _BOOTCOUNT_H__ |
| 7 | #define _BOOTCOUNT_H__ |
Stefan Roese | 033848e | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 8 | |
| 9 | #include <common.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
Stefan Roese | 033848e | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | #include <asm/byteorder.h> |
Simon Glass | 07dc93c | 2019-08-01 09:46:47 -0600 | [diff] [blame] | 13 | #include <env.h> |
Stefan Roese | 033848e | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 14 | |
Philipp Tomsich | ce86031 | 2018-11-27 23:00:18 +0100 | [diff] [blame] | 15 | #ifdef CONFIG_DM_BOOTCOUNT |
| 16 | |
| 17 | struct 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 48 | * Return: 0 if OK, -ve on error |
Philipp Tomsich | ce86031 | 2018-11-27 23:00:18 +0100 | [diff] [blame] | 49 | */ |
| 50 | int 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 57 | * Return: 0 if OK, -ve on error |
Philipp Tomsich | ce86031 | 2018-11-27 23:00:18 +0100 | [diff] [blame] | 58 | */ |
| 59 | int dm_bootcount_set(struct udevice *dev, u32 bootcount); |
| 60 | |
| 61 | #endif |
| 62 | |
Simon Glass | 6853b16 | 2019-11-14 12:57:18 -0700 | [diff] [blame] | 63 | /** bootcount_store() - store the current bootcount */ |
| 64 | void bootcount_store(ulong); |
| 65 | |
| 66 | /** |
| 67 | * bootcount_load() - load the current bootcount |
| 68 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 69 | * Return: bootcount, read from the appropriate location |
Simon Glass | 6853b16 | 2019-11-14 12:57:18 -0700 | [diff] [blame] | 70 | */ |
| 71 | ulong bootcount_load(void); |
| 72 | |
Philippe Reynes | 8d82a20 | 2020-12-11 19:56:47 +0100 | [diff] [blame] | 73 | #if defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_TPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT) |
Lukasz Majewski | ce89eb8 | 2018-05-02 16:10:52 +0200 | [diff] [blame] | 74 | |
Stefan Roese | 033848e | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 75 | #ifdef CONFIG_SYS_BOOTCOUNT_LE |
| 76 | static inline void raw_bootcount_store(volatile u32 *addr, u32 data) |
| 77 | { |
| 78 | out_le32(addr, data); |
| 79 | } |
| 80 | |
| 81 | static inline u32 raw_bootcount_load(volatile u32 *addr) |
| 82 | { |
| 83 | return in_le32(addr); |
| 84 | } |
| 85 | #else |
| 86 | static inline void raw_bootcount_store(volatile u32 *addr, u32 data) |
| 87 | { |
| 88 | out_be32(addr, data); |
| 89 | } |
| 90 | |
| 91 | static inline u32 raw_bootcount_load(volatile u32 *addr) |
| 92 | { |
| 93 | return in_be32(addr); |
| 94 | } |
| 95 | #endif |
Lukasz Majewski | ce89eb8 | 2018-05-02 16:10:52 +0200 | [diff] [blame] | 96 | |
| 97 | DECLARE_GLOBAL_DATA_PTR; |
| 98 | static inline int bootcount_error(void) |
| 99 | { |
| 100 | unsigned long bootcount = bootcount_load(); |
| 101 | unsigned long bootlimit = env_get_ulong("bootlimit", 10, 0); |
| 102 | |
| 103 | if (bootlimit && bootcount > bootlimit) { |
| 104 | printf("Warning: Bootlimit (%lu) exceeded.", bootlimit); |
| 105 | if (!(gd->flags & GD_FLG_SPL_INIT)) |
| 106 | printf(" Using altbootcmd."); |
| 107 | printf("\n"); |
| 108 | |
| 109 | return 1; |
| 110 | } |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static inline void bootcount_inc(void) |
| 116 | { |
| 117 | unsigned long bootcount = bootcount_load(); |
| 118 | |
| 119 | if (gd->flags & GD_FLG_SPL_INIT) { |
| 120 | bootcount_store(++bootcount); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | #ifndef CONFIG_SPL_BUILD |
| 125 | /* Only increment bootcount when no bootcount support in SPL */ |
Philippe Reynes | 8d82a20 | 2020-12-11 19:56:47 +0100 | [diff] [blame] | 126 | #if !defined(CONFIG_SPL_BOOTCOUNT_LIMIT) && !defined(CONFIG_TPL_BOOTCOUNT_LIMIT) |
Lukasz Majewski | ce89eb8 | 2018-05-02 16:10:52 +0200 | [diff] [blame] | 127 | bootcount_store(++bootcount); |
| 128 | #endif |
| 129 | env_set_ulong("bootcount", bootcount); |
| 130 | #endif /* !CONFIG_SPL_BUILD */ |
| 131 | } |
| 132 | |
Lukasz Majewski | ce89eb8 | 2018-05-02 16:10:52 +0200 | [diff] [blame] | 133 | #else |
| 134 | static inline int bootcount_error(void) { return 0; } |
| 135 | static inline void bootcount_inc(void) {} |
Philippe Reynes | 8d82a20 | 2020-12-11 19:56:47 +0100 | [diff] [blame] | 136 | #endif /* CONFIG_SPL_BOOTCOUNT_LIMIT || CONFIG_TPL_BOOTCOUNT_LIMIT || CONFIG_BOOTCOUNT_LIMIT */ |
Lukasz Majewski | dc6301b | 2018-05-02 16:10:51 +0200 | [diff] [blame] | 137 | #endif /* _BOOTCOUNT_H__ */ |