1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #include <command.h>
4 #include <bootcount.h>
5 
do_bootcount_print(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])6 static int do_bootcount_print(struct cmd_tbl *cmdtp, int flag, int argc,
7 			      char *const argv[])
8 {
9 	printf("%lu\n", bootcount_load());
10 	return CMD_RET_SUCCESS;
11 }
12 
do_bootcount_reset(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])13 static int do_bootcount_reset(struct cmd_tbl *cmdtp, int flag, int argc,
14 			      char *const argv[])
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 
24 static struct cmd_tbl bootcount_sub[] = {
25 	U_BOOT_CMD_MKENT(print, 1, 1, do_bootcount_print, "", ""),
26 	U_BOOT_CMD_MKENT(reset, 1, 1, do_bootcount_reset, "", ""),
27 };
28 
do_bootcount(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])29 static int do_bootcount(struct cmd_tbl *cmdtp, int flag, int argc,
30 			char *const argv[])
31 {
32 	struct cmd_tbl *cp;
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 
48 U_BOOT_LONGHELP(bootcount,
49 	"print - print current bootcounter\n"
50 	"reset - reset the bootcounter");
51 
52 U_BOOT_CMD(bootcount, 2, 1, do_bootcount,
53 	   "bootcount",
54 	   bootcount_help_text
55 );
56