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