blob: 5e3b66e676b83ef2a6b371f3a38a0e0ad8c802c3 [file] [log] [blame]
Alex Kiernanb11ed7d2018-05-12 05:49:47 +00001// SPDX-License-Identifier: GPL-2.0+
2
Alex Kiernanb11ed7d2018-05-12 05:49:47 +00003#include <command.h>
4#include <bootcount.h>
5
Simon Glass09140112020-05-10 11:40:03 -06006static int do_bootcount_print(struct cmd_tbl *cmdtp, int flag, int argc,
7 char *const argv[])
Alex Kiernanb11ed7d2018-05-12 05:49:47 +00008{
9 printf("%lu\n", bootcount_load());
10 return CMD_RET_SUCCESS;
11}
12
Simon Glass09140112020-05-10 11:40:03 -060013static int do_bootcount_reset(struct cmd_tbl *cmdtp, int flag, int argc,
14 char *const argv[])
Alex Kiernanb11ed7d2018-05-12 05:49:47 +000015{
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 Glass09140112020-05-10 11:40:03 -060024static struct cmd_tbl bootcount_sub[] = {
Alex Kiernanb11ed7d2018-05-12 05:49:47 +000025 U_BOOT_CMD_MKENT(print, 1, 1, do_bootcount_print, "", ""),
26 U_BOOT_CMD_MKENT(reset, 1, 1, do_bootcount_reset, "", ""),
27};
28
Simon Glass09140112020-05-10 11:40:03 -060029static int do_bootcount(struct cmd_tbl *cmdtp, int flag, int argc,
30 char *const argv[])
Alex Kiernanb11ed7d2018-05-12 05:49:47 +000031{
Simon Glass09140112020-05-10 11:40:03 -060032 struct cmd_tbl *cp;
Alex Kiernanb11ed7d2018-05-12 05:49:47 +000033
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 Rini36162182023-10-07 15:13:08 -040048U_BOOT_LONGHELP(bootcount,
Alex Kiernanb11ed7d2018-05-12 05:49:47 +000049 "print - print current bootcounter\n"
Tom Rini36162182023-10-07 15:13:08 -040050 "reset - reset the bootcounter");
Alex Kiernanb11ed7d2018-05-12 05:49:47 +000051
52U_BOOT_CMD(bootcount, 2, 1, do_bootcount,
53 "bootcount",
Alex Kiernanb11ed7d2018-05-12 05:49:47 +000054 bootcount_help_text
Alex Kiernanb11ed7d2018-05-12 05:49:47 +000055);