Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ian Ray | 542a48e | 2017-11-08 15:35:13 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017 General Electric Company. All rights reserved. |
Ian Ray | 542a48e | 2017-11-08 15:35:13 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <bootcount.h> |
| 7 | #include <fs.h> |
| 8 | #include <mapmem.h> |
| 9 | |
Frédéric Danis | cf01a7e | 2020-03-17 17:59:09 +0100 | [diff] [blame] | 10 | #define BC_MAGIC 0xbd |
| 11 | #define BC_VERSION 1 |
| 12 | |
| 13 | typedef struct { |
| 14 | u8 magic; |
| 15 | u8 version; |
| 16 | u8 bootcount; |
| 17 | u8 upgrade_available; |
| 18 | } bootcount_ext_t; |
| 19 | |
| 20 | static u8 upgrade_available = 1; |
Ian Ray | 542a48e | 2017-11-08 15:35:13 +0000 | [diff] [blame] | 21 | |
| 22 | void bootcount_store(ulong a) |
| 23 | { |
Frédéric Danis | cf01a7e | 2020-03-17 17:59:09 +0100 | [diff] [blame] | 24 | bootcount_ext_t *buf; |
Ian Ray | 542a48e | 2017-11-08 15:35:13 +0000 | [diff] [blame] | 25 | loff_t len; |
| 26 | int ret; |
| 27 | |
Vasileios Amoiridis | 604e30d | 2024-06-28 19:35:41 +0200 | [diff] [blame^] | 28 | if (fs_set_blk_dev(CONFIG_SYS_BOOTCOUNT_FS_INTERFACE, |
| 29 | CONFIG_SYS_BOOTCOUNT_FS_DEVPART, FS_TYPE_ANY)) { |
Ian Ray | 542a48e | 2017-11-08 15:35:13 +0000 | [diff] [blame] | 30 | puts("Error selecting device\n"); |
| 31 | return; |
| 32 | } |
| 33 | |
Frédéric Danis | cf01a7e | 2020-03-17 17:59:09 +0100 | [diff] [blame] | 34 | /* Only update bootcount during upgrade process */ |
| 35 | if (!upgrade_available) |
| 36 | return; |
| 37 | |
| 38 | buf = map_sysmem(CONFIG_SYS_BOOTCOUNT_ADDR, sizeof(bootcount_ext_t)); |
| 39 | buf->magic = BC_MAGIC; |
| 40 | buf->version = BC_VERSION; |
| 41 | buf->bootcount = (a & 0xff); |
| 42 | buf->upgrade_available = upgrade_available; |
Ian Ray | 542a48e | 2017-11-08 15:35:13 +0000 | [diff] [blame] | 43 | unmap_sysmem(buf); |
| 44 | |
Vasileios Amoiridis | 604e30d | 2024-06-28 19:35:41 +0200 | [diff] [blame^] | 45 | ret = fs_write(CONFIG_SYS_BOOTCOUNT_FS_NAME, |
Frédéric Danis | cf01a7e | 2020-03-17 17:59:09 +0100 | [diff] [blame] | 46 | CONFIG_SYS_BOOTCOUNT_ADDR, 0, sizeof(bootcount_ext_t), |
| 47 | &len); |
Ian Ray | 542a48e | 2017-11-08 15:35:13 +0000 | [diff] [blame] | 48 | if (ret != 0) |
| 49 | puts("Error storing bootcount\n"); |
| 50 | } |
| 51 | |
| 52 | ulong bootcount_load(void) |
| 53 | { |
Frédéric Danis | cf01a7e | 2020-03-17 17:59:09 +0100 | [diff] [blame] | 54 | bootcount_ext_t *buf; |
Ian Ray | 542a48e | 2017-11-08 15:35:13 +0000 | [diff] [blame] | 55 | loff_t len_read; |
| 56 | int ret; |
| 57 | |
Vasileios Amoiridis | 604e30d | 2024-06-28 19:35:41 +0200 | [diff] [blame^] | 58 | if (fs_set_blk_dev(CONFIG_SYS_BOOTCOUNT_FS_INTERFACE, |
| 59 | CONFIG_SYS_BOOTCOUNT_FS_DEVPART, FS_TYPE_ANY)) { |
Ian Ray | 542a48e | 2017-11-08 15:35:13 +0000 | [diff] [blame] | 60 | puts("Error selecting device\n"); |
| 61 | return 0; |
| 62 | } |
| 63 | |
Vasileios Amoiridis | 604e30d | 2024-06-28 19:35:41 +0200 | [diff] [blame^] | 64 | ret = fs_read(CONFIG_SYS_BOOTCOUNT_FS_NAME, CONFIG_SYS_BOOTCOUNT_ADDR, |
Frédéric Danis | cf01a7e | 2020-03-17 17:59:09 +0100 | [diff] [blame] | 65 | 0, sizeof(bootcount_ext_t), &len_read); |
| 66 | if (ret != 0 || len_read != sizeof(bootcount_ext_t)) { |
Ian Ray | 542a48e | 2017-11-08 15:35:13 +0000 | [diff] [blame] | 67 | puts("Error loading bootcount\n"); |
| 68 | return 0; |
| 69 | } |
| 70 | |
Frédéric Danis | cf01a7e | 2020-03-17 17:59:09 +0100 | [diff] [blame] | 71 | buf = map_sysmem(CONFIG_SYS_BOOTCOUNT_ADDR, sizeof(bootcount_ext_t)); |
| 72 | if (buf->magic == BC_MAGIC && buf->version == BC_VERSION) { |
| 73 | upgrade_available = buf->upgrade_available; |
| 74 | if (upgrade_available) |
| 75 | ret = buf->bootcount; |
| 76 | } else { |
| 77 | puts("Incorrect bootcount file\n"); |
| 78 | } |
Ian Ray | 542a48e | 2017-11-08 15:35:13 +0000 | [diff] [blame] | 79 | |
| 80 | unmap_sysmem(buf); |
| 81 | |
| 82 | return ret; |
| 83 | } |