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