77#include "bootuf2.h"
88#include "usbd_core.h"
99
10- char file_INFO [] = {
10+ const char file_INFO [] = {
1111 "CherryUSB UF2 BOOT\r\n"
1212 "Model: " CONFIG_PRODUCT "\r\n"
1313 "Board-ID: " CONFIG_BOARD "\r\n"
14+ "Bootloader Reason: " DEFAULT_REASON
1415};
1516
1617const char file_IDEX [] = {
@@ -37,11 +38,18 @@ const char file_JOIN[] = {
3738
3839const char file_ID__ [12 ] = BOOTUF2_FAMILYID_ARRAY ;
3940
41+ enum {
42+ FILE_ID = 0 ,
43+ FILE_INFO = 1 ,
44+ FILE_INDEX = 2 ,
45+ FILE_JOIN = 3 ,
46+ };
47+
4048static struct bootuf2_FILE files [] = {
41- [0 ] = { .Name = file_ID__ , .Content = NULL , .FileSize = 0 },
42- [1 ] = { .Name = "INFO_UF2TXT" , .Content = file_INFO , .FileSize = sizeof ( file_INFO ) - 1 },
43- [2 ] = { .Name = "INDEX HTM" , .Content = file_IDEX , .FileSize = sizeof (file_IDEX ) - 1 },
44- [3 ] = { .Name = "JOIN HTM" , .Content = file_JOIN , .FileSize = sizeof (file_JOIN ) - 1 },
49+ [FILE_ID ] = { .Name = file_ID__ , .Content = NULL , .FileSize = 0 },
50+ [FILE_INFO ] = { .Name = "INFO_UF2TXT" , .Content = NULL , .FileSize = 0 }, // will be added when init
51+ [FILE_INDEX ] = { .Name = "INDEX HTM" , .Content = file_IDEX , .FileSize = sizeof (file_IDEX ) - 1 },
52+ [FILE_JOIN ] = { .Name = "JOIN HTM" , .Content = file_JOIN , .FileSize = sizeof (file_JOIN ) - 1 },
4553};
4654
4755struct bootuf2_data {
@@ -245,11 +253,74 @@ int bootuf2_flash_write_internal(struct bootuf2_data *ctx, struct bootuf2_BLOCK
245253
246254 return 0 ;
247255}
248-
256+ static void replaceSubstring (char * str , const char * oldWord , const char * newWord ) {
257+ char * result ; // 存放新字符串的缓冲区
258+ char * pos , * temp ;
259+ int count = 0 ;
260+ int oldLen = strlen (oldWord );
261+ int newLen = strlen (newWord );
262+
263+ // 1. 统计 oldWord 在 str 中出现的次数
264+ temp = str ;
265+ while ((pos = strstr (temp , oldWord )) != NULL ) {
266+ count ++ ;
267+ temp = pos + oldLen ;
268+ }
269+
270+ // 2. 分配足够大小的缓冲区(注意:如果 newLen < oldLen,分配空间会小于原始字符串大小,
271+ // 但为了代码简单,这里直接分配原始串长度加上扩展部分)
272+ result = (char * ) malloc (strlen (str ) + count * (newLen - oldLen ) + 1 );
273+ if (result == NULL ) {
274+ // 内存分配失败,直接返回
275+ return ;
276+ }
277+
278+ // 3. 进行字符串替换构造新的结果字符串
279+ temp = str ; // 临时指针,指向原字符串
280+ char * r = result ; // 指向新字符串的写入位置
281+
282+ while ((pos = strstr (temp , oldWord )) != NULL ) {
283+ // 复制 pos 之前的部分
284+ int len = pos - temp ;
285+ memcpy (r , temp , len );
286+ r += len ;
287+
288+ // 将 newWord 复制到结果中
289+ memcpy (r , newWord , newLen );
290+ r += newLen ;
291+
292+ // 更新 temp 指针,跳过被替换的 oldWord
293+ temp = pos + oldLen ;
294+ }
295+ // 复制剩下的部分
296+ strcpy (r , temp );
297+
298+ // 4. 将结果拷贝回原来的字符串中
299+ strcpy (str , result );
300+ free (result );
301+ }
302+ void bootuf2_SetReason (const char * reason ) {
303+ if (files [FILE_INFO ].Content != NULL ) {
304+ free (files [FILE_INFO ].Content );
305+ }
306+
307+ // modify it to show why we keep in bootloader mode
308+ // because of file_INFO is a string in .data section, we need to copy it and modify it
309+ char * const file_INFO_ = (char * )malloc (strlen (file_INFO ) + 100 );
310+ memset (file_INFO_ , 0 , strlen (file_INFO ) + 100 );
311+ strcpy (file_INFO_ , file_INFO );
312+ replaceSubstring (file_INFO_ , DEFAULT_REASON , reason );
313+ files [FILE_INFO ].Content = file_INFO_ ;
314+ files [FILE_INFO ].FileSize = strlen (file_INFO_ );
315+ }
249316void bootuf2_init (void )
250317{
251318 struct bootuf2_data * ctx ;
252319
320+ if (files [FILE_INFO ].Content == NULL ) {
321+ bootuf2_SetReason ("DEFAULT_REASON" );
322+ }
323+
253324 ctx = & bootuf2_disk ;
254325
255326 fcalculate_cluster (ctx );
0 commit comments