]> Git Repo - J-u-boot.git/blob - arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c
stm32mp: stm32prog: support for script
[J-u-boot.git] / arch / arm / mach-stm32mp / cmd_stm32prog / cmd_stm32prog.c
1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2 /*
3  * Copyright (C) 2020, STMicroelectronics - All Rights Reserved
4  */
5
6 #include <common.h>
7 #include <command.h>
8 #include <dfu.h>
9 #include <image.h>
10 #include <asm/arch/stm32prog.h>
11 #include "stm32prog.h"
12
13 struct stm32prog_data *stm32prog_data;
14
15 static void enable_vidconsole(void)
16 {
17 #ifdef CONFIG_DM_VIDEO
18         char *stdname;
19         char buf[64];
20
21         stdname = env_get("stdout");
22         if (!stdname || !strstr(stdname, "vidconsole")) {
23                 if (!stdname)
24                         snprintf(buf, sizeof(buf), "serial,vidconsole");
25                 else
26                         snprintf(buf, sizeof(buf), "%s,vidconsole", stdname);
27                 env_set("stdout", buf);
28         }
29
30         stdname = env_get("stderr");
31         if (!stdname || !strstr(stdname, "vidconsole")) {
32                 if (!stdname)
33                         snprintf(buf, sizeof(buf), "serial,vidconsole");
34                 else
35                         snprintf(buf, sizeof(buf), "%s,vidconsole", stdname);
36                 env_set("stderr", buf);
37         }
38 #endif
39 }
40
41 static int do_stm32prog(cmd_tbl_t *cmdtp, int flag, int argc,
42                         char * const argv[])
43 {
44         ulong   addr, size;
45         int dev, ret;
46         enum stm32prog_link_t link = LINK_UNDEFINED;
47         bool reset = false;
48         struct image_header_s header;
49         struct stm32prog_data *data;
50
51         if (argc < 3 ||  argc > 5)
52                 return CMD_RET_USAGE;
53
54         if (!strcmp(argv[1], "usb"))
55                 link = LINK_USB;
56         else if (!strcmp(argv[1], "serial"))
57                 link = LINK_SERIAL;
58
59         if (link == LINK_UNDEFINED) {
60                 pr_err("not supported link=%s\n", argv[1]);
61                 return CMD_RET_USAGE;
62         }
63
64         dev = (int)simple_strtoul(argv[2], NULL, 10);
65
66         addr = STM32_DDR_BASE;
67         size = 0;
68         if (argc > 3) {
69                 addr = simple_strtoul(argv[3], NULL, 16);
70                 if (!addr)
71                         return CMD_RET_FAILURE;
72         }
73         if (argc > 4)
74                 size = simple_strtoul(argv[4], NULL, 16);
75
76         /* check STM32IMAGE presence */
77         if (size == 0 &&
78             !stm32prog_header_check((struct raw_header_s *)addr, &header)) {
79                 size = header.image_length + BL_HEADER_SIZE;
80
81                 /* uImage detected in STM32IMAGE, execute the script */
82                 if (IMAGE_FORMAT_LEGACY ==
83                     genimg_get_format((void *)(addr + BL_HEADER_SIZE)))
84                         return image_source_script(addr + BL_HEADER_SIZE,
85                                                    "script@1");
86         }
87
88         enable_vidconsole();
89
90         data = (struct stm32prog_data *)malloc(sizeof(*data));
91
92         if (!data) {
93                 pr_err("Alloc failed.");
94                 return CMD_RET_FAILURE;
95         }
96         stm32prog_data = data;
97
98         ret = stm32prog_init(data, addr, size);
99         if (ret)
100                 printf("Invalid or missing layout file.");
101
102         /* prepare DFU for device read/write */
103         ret = stm32prog_dfu_init(data);
104         if (ret)
105                 goto cleanup;
106
107         switch (link) {
108         case LINK_SERIAL:
109                 ret = stm32prog_serial_init(data, dev);
110                 if (ret)
111                         goto cleanup;
112                 reset = stm32prog_serial_loop(data);
113                 break;
114         case LINK_USB:
115                 reset = stm32prog_usb_loop(data, dev);
116                 break;
117         default:
118                 goto cleanup;
119         }
120
121         stm32prog_clean(data);
122         free(stm32prog_data);
123         stm32prog_data = NULL;
124
125         puts("Download done\n");
126         if (reset) {
127                 puts("Reset...\n");
128                 run_command("reset", 0);
129         }
130
131         return CMD_RET_SUCCESS;
132
133 cleanup:
134         stm32prog_clean(data);
135         free(stm32prog_data);
136         stm32prog_data = NULL;
137
138         return CMD_RET_FAILURE;
139 }
140
141 U_BOOT_CMD(stm32prog, 5, 0, do_stm32prog,
142            "<link> <dev> [<addr>] [<size>]\n"
143            "start communication with tools STM32Cubeprogrammer on <link> with Flashlayout at <addr>",
144            "<link> = serial|usb\n"
145            "<dev>  = device instance\n"
146            "<addr> = address of flashlayout\n"
147            "<size> = size of flashlayout\n"
148 );
149
150 bool stm32prog_get_tee_partitions(void)
151 {
152         if (stm32prog_data)
153                 return stm32prog_data->tee_detected;
154
155         return false;
156 }
157
158 bool stm32prog_get_fsbl_nor(void)
159 {
160         if (stm32prog_data)
161                 return stm32prog_data->fsbl_nor_detected;
162
163         return false;
164 }
This page took 0.035943 seconds and 4 git commands to generate.