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