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