Alex Kiernan | b11ed7d | 2018-05-12 05:49:47 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | |
| 3 | #include <common.h> |
| 4 | #include <command.h> |
| 5 | #include <bootcount.h> |
| 6 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 7 | static int do_bootcount_print(struct cmd_tbl *cmdtp, int flag, int argc, |
| 8 | char *const argv[]) |
Alex Kiernan | b11ed7d | 2018-05-12 05:49:47 +0000 | [diff] [blame] | 9 | { |
| 10 | printf("%lu\n", bootcount_load()); |
| 11 | return CMD_RET_SUCCESS; |
| 12 | } |
| 13 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 14 | static int do_bootcount_reset(struct cmd_tbl *cmdtp, int flag, int argc, |
| 15 | char *const argv[]) |
Alex Kiernan | b11ed7d | 2018-05-12 05:49:47 +0000 | [diff] [blame] | 16 | { |
| 17 | /* |
| 18 | * note that we're explicitly not resetting the environment |
| 19 | * variable, so you still have the old bootcounter available |
| 20 | */ |
| 21 | bootcount_store(0); |
| 22 | return CMD_RET_SUCCESS; |
| 23 | } |
| 24 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 25 | static struct cmd_tbl bootcount_sub[] = { |
Alex Kiernan | b11ed7d | 2018-05-12 05:49:47 +0000 | [diff] [blame] | 26 | U_BOOT_CMD_MKENT(print, 1, 1, do_bootcount_print, "", ""), |
| 27 | U_BOOT_CMD_MKENT(reset, 1, 1, do_bootcount_reset, "", ""), |
| 28 | }; |
| 29 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 30 | static int do_bootcount(struct cmd_tbl *cmdtp, int flag, int argc, |
| 31 | char *const argv[]) |
Alex Kiernan | b11ed7d | 2018-05-12 05:49:47 +0000 | [diff] [blame] | 32 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 33 | struct cmd_tbl *cp; |
Alex Kiernan | b11ed7d | 2018-05-12 05:49:47 +0000 | [diff] [blame] | 34 | |
| 35 | if (argc < 2) |
| 36 | return CMD_RET_USAGE; |
| 37 | |
| 38 | /* drop initial "bootcount" arg */ |
| 39 | argc--; |
| 40 | argv++; |
| 41 | |
| 42 | cp = find_cmd_tbl(argv[0], bootcount_sub, ARRAY_SIZE(bootcount_sub)); |
| 43 | if (cp) |
| 44 | return cp->cmd(cmdtp, flag, argc, argv); |
| 45 | |
| 46 | return CMD_RET_USAGE; |
| 47 | } |
| 48 | |
Simon Glass | 5b4d810 | 2023-02-05 15:40:56 -0700 | [diff] [blame] | 49 | #if IS_ENABLED(CONFIG_SYS_LONGHELP) |
Alex Kiernan | b11ed7d | 2018-05-12 05:49:47 +0000 | [diff] [blame] | 50 | static char bootcount_help_text[] = |
| 51 | "print - print current bootcounter\n" |
| 52 | "reset - reset the bootcounter" |
| 53 | ; |
| 54 | #endif |
| 55 | |
| 56 | U_BOOT_CMD(bootcount, 2, 1, do_bootcount, |
| 57 | "bootcount", |
Simon Glass | 5b4d810 | 2023-02-05 15:40:56 -0700 | [diff] [blame] | 58 | #if IS_ENABLED(CONFIG_SYS_LONGHELP) |
Alex Kiernan | b11ed7d | 2018-05-12 05:49:47 +0000 | [diff] [blame] | 59 | bootcount_help_text |
| 60 | #endif |
| 61 | ); |