1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2017 General Electric Company. All rights reserved.
4 */
5
6 #include <bootcount.h>
7 #include <fs.h>
8 #include <mapmem.h>
9
10 #define BC_MAGIC 0xbc
11
bootcount_store(ulong a)12 void bootcount_store(ulong a)
13 {
14 u8 *buf;
15 loff_t len;
16 int ret;
17
18 if (fs_set_blk_dev(CONFIG_SYS_BOOTCOUNT_EXT_INTERFACE,
19 CONFIG_SYS_BOOTCOUNT_EXT_DEVPART, FS_TYPE_EXT)) {
20 puts("Error selecting device\n");
21 return;
22 }
23
24 buf = map_sysmem(CONFIG_SYS_BOOTCOUNT_ADDR, 2);
25 buf[0] = BC_MAGIC;
26 buf[1] = (a & 0xff);
27 unmap_sysmem(buf);
28
29 ret = fs_write(CONFIG_SYS_BOOTCOUNT_EXT_NAME,
30 CONFIG_SYS_BOOTCOUNT_ADDR, 0, 2, &len);
31 if (ret != 0)
32 puts("Error storing bootcount\n");
33 }
34
bootcount_load(void)35 ulong bootcount_load(void)
36 {
37 u8 *buf;
38 loff_t len_read;
39 int ret;
40
41 if (fs_set_blk_dev(CONFIG_SYS_BOOTCOUNT_EXT_INTERFACE,
42 CONFIG_SYS_BOOTCOUNT_EXT_DEVPART, FS_TYPE_EXT)) {
43 puts("Error selecting device\n");
44 return 0;
45 }
46
47 ret = fs_read(CONFIG_SYS_BOOTCOUNT_EXT_NAME, CONFIG_SYS_BOOTCOUNT_ADDR,
48 0, 2, &len_read);
49 if (ret != 0 || len_read != 2) {
50 puts("Error loading bootcount\n");
51 return 0;
52 }
53
54 buf = map_sysmem(CONFIG_SYS_BOOTCOUNT_ADDR, 2);
55 if (buf[0] == BC_MAGIC)
56 ret = buf[1];
57
58 unmap_sysmem(buf);
59
60 return ret;
61 }
62