Skip to content

Commit b93019d

Browse files
IRISZZWWayne Ren
authored andcommitted
example: bootloader: add ntshell cls and eflash command and bootloader startup (#61)
* add startup from iotdk bootspi flash support * add corresponding ntshell cmds Signed-off-by: Watson Zeng <[email protected]>
1 parent 49a1162 commit b93019d

File tree

8 files changed

+388
-2
lines changed

8 files changed

+388
-2
lines changed

example/baremetal/bootloader/README.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ Buidling and Running
9696
$ cd <embarc_root>/example/baremetal/bootloader
9797
$ gmake BOARD=emsk BD_VER=22 CUR_CORE=arcem7d TOOLCHAIN=mw bin
9898
99+
- Generate a secondary bootloader binary file for iotdk use eflash
100+
101+
.. code-block:: console
102+
103+
$ cd <embarc_root>/example/baremetal/bootloader
104+
$ gmake BOARD=iotdk BD_VER=10 CUR_CORE=arcem9d TOOLCHAIN=mw LOCATION=eflash bin
105+
99106
If the binary file is generated successfully, you will output as follows:
100107

101108
.. code-block:: console

example/baremetal/bootloader/appl_mem_config.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@
66
#define EXT_RAM_SIZE 0x100000 /* the mem space covered by bootloader */
77
#endif
88

9+
#ifdef BOARD_IOTDK
10+
#ifdef USE_EFLASH_LOCATION
11+
#define REGION_ROM EXT_ROM
12+
#endif
13+
#endif
914
#endif

example/baremetal/bootloader/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ int main(void)
235235
}
236236
led_write(0xFF, 0xFF); /* Start application */
237237

238-
fp = (fp_t)(boot_cfg.ram_startaddress + 4);
238+
fp = (fp_t)(*((uint32_t *)boot_cfg.ram_startaddress));
239239
fp(); /* jump to program */
240240
return E_SYS;
241241
}

example/baremetal/bootloader/makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ APPL ?= simple_bootloader
33

44
BOARD ?= emsk
55

6+
LOCATION ?=
7+
8+
ifeq ($(LOCATION), eflash)
9+
APPL_DEFINES += -DUSE_EFLASH_LOCATION
10+
endif
11+
612
ifeq ($(BOARD), emsk)
713
EXT_DEV_LIST += sensor/temperature/adt7420
814
endif
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/* ------------------------------------------
2+
* Copyright (c) 2018, Synopsys, Inc. All rights reserved.
3+
4+
* Redistribution and use in source and binary forms, with or without modification,
5+
* are permitted provided that the following conditions are met:
6+
7+
* 1) Redistributions of source code must retain the above copyright notice, this
8+
* list of conditions and the following disclaimer.
9+
10+
* 2) Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation and/or
12+
* other materials provided with the distribution.
13+
14+
* 3) Neither the name of the Synopsys, Inc., nor the names of its contributors may
15+
* be used to endorse or promote products derived from this software without
16+
* specific prior written permission.
17+
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*
29+
--------------------------------------------- */
30+
31+
/**
32+
* \file
33+
* \brief filesystem operation commands: cls
34+
*/
35+
36+
#include "cmds_fs_cfg.h"
37+
#if NTSHELL_USE_CMDS_FS_CLS
38+
#include "cmd_fs_common.h"
39+
40+
static NTSHELL_IO_PREDEF;
41+
/* show help of command */
42+
static void cmd_cls_help(char *cmd_name, void *extobj)
43+
{
44+
VALID_EXTOBJ_NORTN(extobj);
45+
46+
if (cmd_name == NULL) {
47+
/* cmd_name not valid */
48+
return;
49+
}
50+
CMD_DEBUG("Usage: %s [OPTION]...\r\n"
51+
"Clear the console output\r\n"
52+
" -h/H/? Show the help information\r\n"
53+
"Examples: \r\n"
54+
" cls Clear the console output\r\n"
55+
" cls -h Show the help information\r\n", cmd_name);
56+
57+
error_exit:
58+
return;
59+
}
60+
61+
/*command: cls*/
62+
static int cmd_cls(int argc, char **argv, void *extobj)
63+
{
64+
int32_t ercd = E_OK;
65+
//char *working_directory = NULL;
66+
int32_t opt;
67+
68+
VALID_EXTOBJ(extobj, -1);
69+
NTSHELL_IO_GET(extobj);
70+
71+
if(argc == 1) {
72+
//working_directory = fs_working_dir();
73+
CMD_DEBUG("\x1b[%10A");
74+
CMD_DEBUG("\x1b[?2J");
75+
return E_OK;
76+
} else if (*argv[1] != '-') {
77+
ercd = E_SYS;
78+
CMD_DEBUG("command error!\r\n");
79+
CMD_DEBUG("Try '%s -h' for more information\r\n", argv[0]);
80+
goto error_exit;
81+
}
82+
83+
opterr = 0;
84+
optind = 1;
85+
86+
while ((opt=getopt(argc, argv, "hH?")) != -1) {
87+
switch (opt) {
88+
case 'h':
89+
case '?':
90+
case 'H':
91+
cmd_cls_help(argv[0], extobj);
92+
goto error_exit;
93+
break;
94+
default:
95+
CMD_DEBUG("%s: unrecognized option:%c\r\n", argv[0], opt);
96+
CMD_DEBUG("Try '%s -h' for more information\r\n",argv[0]);
97+
break;
98+
}
99+
}
100+
101+
return E_OK;
102+
103+
error_exit:
104+
return ercd;
105+
}
106+
107+
static CMD_TABLE_T cls_cmd = {"cls", "Clear the console output", cmd_cls, NULL};
108+
/**
109+
* register cls command
110+
*/
111+
CMD_TABLE_T * register_ntshell_cmd_cls(CMD_TABLE_T *prev)
112+
{
113+
return ntshell_usrcmd_register(&cls_cmd, prev);
114+
}
115+
#endif /*NTSHELL_USE_CMDS_FS_PWD*/
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
/* ------------------------------------------
2+
* Copyright (c) 2018, Synopsys, Inc. All rights reserved.
3+
4+
* Redistribution and use in source and binary forms, with or without modification,
5+
* are permitted provided that the following conditions are met:
6+
7+
* 1) Redistributions of source code must retain the above copyright notice, this
8+
* list of conditions and the following disclaimer.
9+
10+
* 2) Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation and/or
12+
* other materials provided with the distribution.
13+
14+
* 3) Neither the name of the Synopsys, Inc., nor the names of its contributors may
15+
* be used to endorse or promote products derived from this software without
16+
* specific prior written permission.
17+
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*
29+
--------------------------------------------- */
30+
31+
/**
32+
* \file
33+
* \brief filesystem operation commands: flash
34+
*/
35+
36+
#include "cmds_fs_cfg.h"
37+
#if NTSHELL_USE_CMDS_FS_FLASH
38+
#include "cmd_fs_common.h"
39+
40+
static NTSHELL_IO_PREDEF;
41+
/* show help of command */
42+
static void cmd_flash_help(char *cmd_name, void *extobj)
43+
{
44+
VALID_EXTOBJ_NORTN(extobj);
45+
46+
if (cmd_name == NULL) {
47+
/* cmd_name not valid */
48+
return;
49+
}
50+
CMD_DEBUG("Usage: %s [OPTION]...\r\n"
51+
"Write bin file to flash(eflash or bootspi flash)\r\n"
52+
" -h/H/? Show the help information\r\n"
53+
"Examples: \r\n"
54+
" flash -eflash test.bin Write bin file to eflash\r\n"
55+
" flash -bootspi test.bin Write bin file to bootspi flash\r\n"
56+
" flash -h Show the help information\r\n", cmd_name);
57+
58+
error_exit:
59+
return;
60+
}
61+
62+
uint8_t buffer[SMIC_EFLASH_PAGE_SIZE] = {0};
63+
uint8_t buffer_r[SMIC_EFLASH_PAGE_SIZE] = {0};
64+
/*command: flash*/
65+
static int cmd_flash(int argc, char **argv, void *extobj)
66+
{
67+
int32_t ercd = E_OK;
68+
uint8_t res = 0;
69+
int32_t opt;
70+
int32_t file_size;
71+
uint32_t page_size;
72+
VALID_EXTOBJ(extobj, -1);
73+
NTSHELL_IO_GET(extobj);
74+
EFLASH_DEFINE(eflash_test, EFLASH_CRTL_BASE);
75+
BOOTSPI_DEFINE(bootspi_test, BOOTSPI_CRTL_BASE);
76+
SMIC_EFLASH_INFO eflash_info;
77+
if(argc == 1) {
78+
ercd = E_SYS;
79+
CMD_DEBUG("command error!\r\n");
80+
CMD_DEBUG("Try '%s -h' for more information\r\n", argv[0]);
81+
goto error_exit;
82+
} else if (argv[1][0]=='-' && argv[1][1]=='e') {
83+
res = f_open(&cmd_files[0], argv[2], FA_READ);
84+
if(res != FR_OK) {
85+
ercd = E_SYS;
86+
fs_put_err(res, extobj);
87+
goto error_exit;
88+
}
89+
file_size = f_size(&cmd_files[0]);
90+
if(file_size == -1) {
91+
ercd = E_SYS;
92+
CMD_DEBUG("filename: %s, file_size = -1\r\n", argv[1]);
93+
f_close(&cmd_files[0]);
94+
goto error_exit;
95+
}
96+
smic_eflash_open(eflash_test);
97+
smic_eflash_control(eflash_test, SMIC_EFLASH_GET_INFO, (void *)(&eflash_info));
98+
page_size = eflash_info.eflash_page_size;
99+
if(file_size > eflash_info.eflash_page_size * eflash_info.eflash_page_cnt) {
100+
ercd = E_SYS;
101+
CMD_DEBUG("filename: %s, file_size = %d > eflash size = %d \r\n", argv[1],
102+
file_size, eflash_info.eflash_page_size * eflash_info.eflash_page_cnt);
103+
f_close(&cmd_files[0]);
104+
smic_eflash_close(eflash_test);
105+
goto error_exit;
106+
}
107+
smic_eflash_control(eflash_test, SMIC_EFLASH_MACRO_ERASE, NULL);
108+
uint32_t txlen = file_size;
109+
int32_t buf_pt=0;
110+
while(txlen >0) {
111+
uint32_t send_size=0;
112+
if (txlen > page_size) {
113+
send_size = page_size;
114+
} else {
115+
send_size = txlen;
116+
}
117+
f_lseek(&cmd_files[0], buf_pt);
118+
f_read(&cmd_files[0], buffer, send_size, &send_size);
119+
smic_eflash_write(eflash_test, buf_pt, send_size, buffer);
120+
smic_eflash_read(eflash_test, buf_pt, send_size, buffer_r);
121+
for(int i = 0; i < send_size; i++) {
122+
if(buffer[i] != buffer_r[i]) {
123+
ercd = E_SYS;
124+
CMD_DEBUG("eflash write failed !\r\n");
125+
f_close(&cmd_files[0]);
126+
smic_eflash_close(eflash_test);
127+
goto error_exit;
128+
}
129+
}
130+
buf_pt += send_size;
131+
txlen -= send_size;
132+
}
133+
f_close(&cmd_files[0]);
134+
smic_eflash_close(eflash_test);
135+
return E_OK;
136+
} else if (argv[1][0]=='-' && argv[1][1]=='b') {
137+
res = f_open(&cmd_files[0], argv[2], FA_READ);
138+
if(res != FR_OK) {
139+
ercd = E_SYS;
140+
fs_put_err(res, extobj);
141+
goto error_exit;
142+
}
143+
file_size = f_size(&cmd_files[0]);
144+
if(file_size == -1) {
145+
ercd = E_SYS;
146+
CMD_DEBUG("filename: %s, file_size = -1\r\n", argv[1]);
147+
f_close(&cmd_files[0]);
148+
goto error_exit;
149+
}
150+
smic_bootspi_open(bootspi_test);
151+
smic_bootspi_control(bootspi_test, SMIC_BOOTSPI_RESET, NULL);
152+
page_size = SMIC_BOOTSPI_PAGE_SIZE;
153+
if(file_size > SMIC_BOOTSPI_BLK_SIZE * SMIC_BOOTSPI_BLKS_PER_CHIP) {
154+
ercd = E_SYS;
155+
CMD_DEBUG("filename: %s, file_size = %d > eflash size = %d \r\n", argv[1],
156+
file_size, SMIC_BOOTSPI_BLK_SIZE * SMIC_BOOTSPI_BLKS_PER_CHIP);
157+
f_close(&cmd_files[0]);
158+
smic_bootspi_close(bootspi_test);
159+
goto error_exit;
160+
}
161+
smic_bootspi_control(bootspi_test, SMIC_BOOTSPI_CHIP_REASE, NULL);
162+
uint32_t txlen = file_size;
163+
int32_t buf_pt=0;
164+
while(txlen >0) {
165+
uint32_t send_size=0;
166+
if (txlen > page_size) {
167+
send_size = page_size;
168+
} else {
169+
send_size = txlen;
170+
}
171+
f_lseek(&cmd_files[0], buf_pt);
172+
f_read(&cmd_files[0], buffer, send_size, &send_size);
173+
smic_bootspi_write(bootspi_test, buf_pt, send_size, buffer);
174+
smic_bootspi_read(bootspi_test, buf_pt, send_size, buffer_r);
175+
for(int i = 0; i < send_size; i++) {
176+
if(buffer[i] != buffer_r[i]) {
177+
ercd = E_SYS;
178+
CMD_DEBUG("bootspi flash write failed !\r\n");
179+
f_close(&cmd_files[0]);
180+
smic_bootspi_close(bootspi_test);
181+
goto error_exit;
182+
}
183+
}
184+
buf_pt += send_size;
185+
txlen -= send_size;
186+
}
187+
f_close(&cmd_files[0]);
188+
smic_bootspi_close(bootspi_test);
189+
return E_OK;
190+
}
191+
192+
opterr = 0;
193+
optind = 1;
194+
195+
while ((opt=getopt(argc, argv, "hH?")) != -1) {
196+
switch (opt) {
197+
case 'h':
198+
case '?':
199+
case 'H':
200+
cmd_flash_help(argv[0], extobj);
201+
goto error_exit;
202+
break;
203+
default:
204+
CMD_DEBUG("%s: unrecognized option:%c\r\n", argv[0], opt);
205+
CMD_DEBUG("Try '%s -h' for more information\r\n",argv[0]);
206+
break;
207+
}
208+
}
209+
210+
return E_OK;
211+
212+
error_exit:
213+
return ercd;
214+
}
215+
216+
static CMD_TABLE_T flash_cmd = {"flash", "Write bin file to flash(eflash or bootspi flash)", cmd_flash, NULL};
217+
/**
218+
* register flash command
219+
*/
220+
CMD_TABLE_T * register_ntshell_cmd_flash(CMD_TABLE_T *prev)
221+
{
222+
return ntshell_usrcmd_register(&flash_cmd, prev);
223+
}
224+
#endif /*NTSHELL_USE_CMDS_FS_PWD*/

0 commit comments

Comments
 (0)