lin
2025-04-14 e0c033f30287744d392a8d700693b1c0b78afc7c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2013
 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
 */
 
#include <common.h>
#include <environment.h>
 
extern u32 rtc_read_data(int index);
extern void rtc_write_data(int index, u32 val);
 
#define RTC_BOOTCOUNT_INDEX   7
#define RTC_BOOTCOUNT_OFFSET  0
#define RTC_BOOTCOUNT_BIT     8
 
#define RTC_BOOTCOUNT_MASK (((1 << RTC_BOOTCOUNT_BIT) -1) << RTC_BOOTCOUNT_OFFSET)
 
static void set_bootcount_to_rtc(ulong a)
{
   u32 val = ((a << RTC_BOOTCOUNT_OFFSET) & RTC_BOOTCOUNT_MASK);
   rtc_write_data(RTC_BOOTCOUNT_INDEX, val);
}
 
static ulong get_bootcount_from_rtc(void)
{
   u32 val = 0;
   val = (rtc_read_data(RTC_BOOTCOUNT_INDEX) & RTC_BOOTCOUNT_MASK);
   return (val >> RTC_BOOTCOUNT_OFFSET);
}
 
void bootcount_store(ulong a)
{
   int upgrade_available = env_get_ulong("upgrade_available", 10, 0);
 
   if (upgrade_available) {
       set_bootcount_to_rtc(a);
   }
}
 
ulong bootcount_load(void)
{
   int upgrade_available = env_get_ulong("upgrade_available", 10, 0);
   ulong val = 0;
 
   if (upgrade_available)
       val = get_bootcount_from_rtc();
 
   return val;
}