1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2012 Boundary Devices Inc.
4 */
5 #include <common.h>
6 #include <linux/errno.h>
7 #include <asm/io.h>
8 #include <asm/mach-imx/boot_mode.h>
9 #include <malloc.h>
10 #include <command.h>
11
12 static const struct boot_mode *modes[2];
13
search_modes(char * arg)14 static const struct boot_mode *search_modes(char *arg)
15 {
16 int i;
17
18 for (i = 0; i < ARRAY_SIZE(modes); i++) {
19 const struct boot_mode *p = modes[i];
20 if (p) {
21 while (p->name) {
22 if (!strcmp(p->name, arg))
23 return p;
24 p++;
25 }
26 }
27 }
28 return NULL;
29 }
30
create_usage(char * dest)31 static int create_usage(char *dest)
32 {
33 int i;
34 int size = 0;
35
36 for (i = 0; i < ARRAY_SIZE(modes); i++) {
37 const struct boot_mode *p = modes[i];
38 if (p) {
39 while (p->name) {
40 int len = strlen(p->name);
41 if (dest) {
42 memcpy(dest, p->name, len);
43 dest += len;
44 *dest++ = '|';
45 }
46 size += len + 1;
47 p++;
48 }
49 }
50 }
51 if (dest)
52 memcpy(dest - 1, " [noreset]", 11); /* include trailing 0 */
53 size += 10;
54
55 if (dest)
56 memcpy(dest - 1, "\nbmode - getprisec", 19);
57 size += 18;
58
59 return size;
60 }
61
boot_mode_getprisec(void)62 __weak int boot_mode_getprisec(void)
63 {
64 return 0;
65 }
66
do_boot_mode(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])67 static int do_boot_mode(struct cmd_tbl *cmdtp, int flag, int argc,
68 char *const argv[])
69 {
70 const struct boot_mode *p;
71 int reset_requested = 1;
72
73 if (argc < 2)
74 return CMD_RET_USAGE;
75 if (!strcmp(argv[1], "getprisec"))
76 return boot_mode_getprisec();
77 p = search_modes(argv[1]);
78 if (!p)
79 return CMD_RET_USAGE;
80 if (argc == 3) {
81 if (strcmp(argv[2], "noreset"))
82 return CMD_RET_USAGE;
83 reset_requested = 0;
84 }
85
86 boot_mode_apply(p->cfg_val);
87 if (reset_requested && p->cfg_val)
88 do_reset(NULL, 0, 0, NULL);
89 return 0;
90 }
91
92 U_BOOT_CMD(
93 bmode, 3, 0, do_boot_mode,
94 NULL,
95 "");
96
add_board_boot_modes(const struct boot_mode * p)97 void add_board_boot_modes(const struct boot_mode *p)
98 {
99 int size;
100 char *dest;
101
102 struct cmd_tbl *entry = ll_entry_get(struct cmd_tbl, bmode, cmd);
103
104 if (entry->usage) {
105 free(entry->usage);
106 entry->usage = NULL;
107 }
108
109 modes[0] = p;
110 modes[1] = soc_boot_modes;
111 size = create_usage(NULL);
112 dest = malloc(size);
113 if (dest) {
114 create_usage(dest);
115 entry->usage = dest;
116 }
117 }
118