1 固件升級(jí)
在一些項(xiàng)目交期比較急的情況下,可以先把基本功能做出來(lái),加入固件升級(jí)的功能,后續(xù)即使發(fā)現(xiàn)重大BUG,也不用返廠更新固件,只需要把加密固件發(fā)給客戶自行更新,也可以使用物聯(lián)網(wǎng)的方式進(jìn)行遠(yuǎn)程固件升級(jí)。
2 FLASH劃分
2.1 內(nèi)部FLASH
以STM32F103ZET6為例,內(nèi)部FLASH的扇區(qū)大小是2KB

2.2 外部FLASH
整個(gè)FLASH作為FAT32使用,以W25Q128為例,有16MB的空間
3 生成加密文件
3.1 加密算法
用的是tinycrypt中的aes加密算法修改IV和KEY的定義
#define TEST_TINY_AES_IV
"0123456789ABCDEF"
#define TEST_TINY_AES_KEY
"0123456789ABCDEF0123456789ABCDEF"
在使用前選擇是作為加密還是解密
tinycrypt_enc_init();
tinycrypt_dec_init();
3.2 壓縮算法
用的是quicklz壓縮算法,壓縮率比較低,154KB的固件壓縮之后是118KB
3.3 app再加工
在keil編譯出app固件后,使用外部應(yīng)用程序來(lái)對(duì)app進(jìn)行壓縮,再進(jìn)行加密,再為整個(gè)固件增加CRC32校驗(yàn),外部程序在linux平臺(tái)下同樣適用,只需要在目標(biāo)主機(jī)上編譯生成目標(biāo)文件即可。
3.3.1 參數(shù)檢查
主要有FLASH總大小、FLASH扇區(qū)大小、bootloader大小、bootloader輸入文件名、app輸入文件名、輸出app+crc文件名、輸出app加密壓縮后的文件名等
if (argc == 13)
    {
        combine_file.size_total = atoi(argv[1]);
        combine_file.size_sector = atoi(argv[2]);
        combine_file.size_bootloader = atoi(argv[3]);
        combine_file.check_type = atoi(argv[4]);
        sprintf(combine_file.fn_bootloader,"%s",argv[5]);
        sprintf(combine_file.fn_app_in,"%s",argv[6]);
        sprintf(combine_file.fn_app_out,"%s",argv[7]);
        sprintf(combine_file.fn_app_mask,"_upfw_app.bin",argv[7]);
        sprintf(combine_file.fn_product,"%s",argv[8]);
        get_debug_switch = atoi(argv[9]);
        combine_file.size_app_max = atoi(argv[10]);
        combine_file.flash_base = atoi(argv[11]);
        sprintf(combine_file.fn_user,"%s",argv[12]);
    }
3.3.2 文件讀取和加工
FILE *p_bootloader_file_source=NULL;
    FILE *p_app_file_source=NULL;
    FILE *p_app_file_out=NULL;
    FILE *p_app_file_out_mask=NULL;
    FILE *p_product_file_out=NULL;
    FILE *p_user_file_out=NULL;
    p_bootloader_file_source = fopen(combine_file.fn_bootloader,"rb+");
    if (!p_bootloader_file_source)
    {
        if (get_debug_switch > 0)
        {
            printf("read bootloader file error=%s\\r\\n",combine_file.fn_bootloader);
        }
        goto error;
    }
    p_app_file_source = fopen(combine_file.fn_app_in,"rb+");
    if (!p_app_file_source)
    {
        if (get_debug_switch > 0)
        {
            printf("p_app_file_source error=%d\\r\\n",p_app_file_source);
        }
        goto error;
    }
    //讀取bootloader文件
    fseek(p_bootloader_file_source, 0, SEEK_END);
    uint32_t get_bootloader_file_size = ftell(p_bootloader_file_source);
    if (get_debug_switch > 0)
    {
        printf("bootloader file size=%d\\r\\n",get_bootloader_file_size);
    }
    if ((0 == get_bootloader_file_size) || (get_bootloader_file_size > combine_file.size_total))
    {
        if (get_debug_switch > 0)
        {
           printf("bootloader file size invalid=%d\\r\\n",get_bootloader_file_size);
        }
        goto error;
    }
    fseek(p_bootloader_file_source, 0L, SEEK_SET);
    uint8_t *bootloader_data_buffer = (uint8_t *)malloc(sizeof(uint8_t)*get_bootloader_file_size);
    if (!bootloader_data_buffer)
    {
        if (get_debug_switch > 0)
        {
            printf("bootloader buffer malloc error\\r\\n");
        }
        goto error;
    }
    fread(bootloader_data_buffer,1,get_bootloader_file_size,p_bootloader_file_source);
    //讀取APP來(lái)源文件
    fseek(p_app_file_source, 0, SEEK_END);
    uint32_t get_app_file_size = ftell(p_app_file_source);
    if (get_debug_switch > 0)
    {
        printf("file size[app]=%d\\r\\n",get_app_file_size);
        printf("file size[app + crc]=%d\\r\\n",get_app_file_size+4);
    }
    if ((0 == get_app_file_size) || (get_app_file_size > (combine_file.size_app_max-8)))
    {
        if (get_debug_switch > 0)
        {
           printf("app file size invalid\\r\\n");
        }
        goto error;
    }
    fseek(p_app_file_source, 0L, SEEK_SET);
    uint8_t *app_data_buffer = (uint8_t *)malloc(sizeof(uint8_t)*get_app_file_size);
    if (!app_data_buffer)
    {
        if (get_debug_switch > 0)
        {
           printf("app buffer malloc error\\r\\n");
        }
        goto error;
    }
    fread(app_data_buffer,1,get_app_file_size,p_app_file_source);
    //把a(bǔ)pp內(nèi)容進(jìn)行校驗(yàn)生成帶校驗(yàn)的app文件
    uint32_t get_crc_value = crc32_customized(CONFIG_CRC32_POLY,CONFIG_CRC32_INIT_VALUE,CONFIG_CRC32_OUT_XOR,app_data_buffer,get_app_file_size);
    if (get_debug_switch > 0)
    {
        printf("get_crc_value = %x\\r\\n",get_crc_value);
    }
    //緩存大小是 bootloader+sector+app
    //新建一個(gè)bootloader大小+sector+app大小的緩存
    //生產(chǎn)用的文件需要帶CRC校驗(yàn)和字節(jié)大小,客戶用的APP文件只包含CRC校驗(yàn)沒(méi)有大小,大小在升級(jí)的時(shí)候自動(dòng)加上去
    //后續(xù)再優(yōu)化APPSIZE和APPCRC的存儲(chǔ)位置,節(jié)省FLASH空間
    uint32_t size_product = combine_file.size_bootloader+combine_file.size_parameter+get_app_file_size;
    uint8_t *product_data_buffer = (uint8_t *)malloc(sizeof(uint8_t)*(size_product));
    if (get_debug_switch > 0)
    {
        printf("product file size = %d\\r\\n",size_product);
    }
    if (!product_data_buffer)
    {
        if (get_debug_switch > 0)
        {
            printf("bootloader buffer malloc error\\r\\n");
        }
        goto error;
    }
    //填充0xff
    printf("fill 0xff\\n");
    memset(product_data_buffer,0xff,size_product);
    printf("write bootloader\\n");
    //寫入 bootloader
    memcpy(product_data_buffer,bootloader_data_buffer,get_bootloader_file_size);
    //加入 CRC和SIZE信息
    memcpy(&product_data_buffer[combine_file.size_bootloader],&get_app_file_size,4);
    printf("write app\\n");
    //寫入app
    memcpy(&product_data_buffer[combine_file.size_bootloader+combine_file.size_parameter],app_data_buffer,get_app_file_size);
    if (get_debug_switch > 0)
    {
        printf("app at=[%08x]\\r\\n",combine_file.flash_base+combine_file.size_bootloader+combine_file.size_parameter);
    }
    //輸出 app_out
    p_app_file_out = fopen(combine_file.fn_app_out,"wb+");
    if (!p_app_file_out)
    {
        if (get_debug_switch > 0)
        {
           printf("write app crc file error\\r\\n");
        }
        goto error;
    }
    fwrite(app_data_buffer,1,get_app_file_size,p_app_file_out);
    //輸出 加密后的文件
    uint8_t *app_mask_data_buffer = 0;
    printf("combine_file.check_type=[%d]\\r\\n",combine_file.check_type);
    p_app_file_out_mask = fopen(combine_file.fn_app_mask,"wb+");
    if (!p_app_file_out_mask)
    {
        if (get_debug_switch > 0)
        {
           printf("write app mask file error\\r\\n");
        }
        goto error;
    }
3.3.3 文件加密和壓縮處理
if (T_CRYPT == combine_file.check_type)
    {
        int res = 0;
        uint8_t *set_data_out = 0;
        tinycrypt_enc_init();
        uint8_t *buffer_out_temp = (uint8_t *) malloc(sizeof(uint8_t)*get_app_file_size);
        if (!buffer_out_temp)
        {
            printf("buffer_out_temp malloc error \\r\\n");
        }
        uint32_t size_out = 0;
        if (quicklz_compress_buffer(buffer_out_temp,app_data_buffer,get_app_file_size,&size_out) >= 0)
        {
            if (!size_out)
            {
                printf("size_out error \\r\\n");
                goto error;
            }
            set_data_out = (uint8_t *) malloc(sizeof(uint8_t)*size_out);
            if (!set_data_out)
            {
                printf("set_data_out malloc error \\r\\n");
                res = -1;
                goto error;
            }
            memset(buffer_data,0,COMPRESS_BUFFER_SIZE);
            aes_en_buffer(set_data_out,buffer_out_temp,size_out);
            fwrite(set_data_out,1,size_out,p_app_file_out_mask);
            uint32_t aes_crc32  =crc32_customized(CONFIG_CRC32_POLY,CONFIG_CRC32_INIT_VALUE,CONFIG_CRC32_OUT_XOR,set_data_out,size_out);
            if (get_debug_switch > 0)
            {
                printf("aes_crc32 mask = %x\\r\\n",aes_crc32);
            }
            fwrite(&aes_crc32,1,4,p_app_file_out_mask);
        }
        if (buffer_out_temp)
        {
            free(buffer_out_temp);
        }
        if (set_data_out)
        {
            free(set_data_out);
        }
}
3.4 bootloader的設(shè)計(jì)
在啟動(dòng)后掛載文件系統(tǒng),檢查是否存在xxx.bin的文件,如果存在則檢查文件是否合法,合法就進(jìn)入解密和解壓縮階段,最后把文件的crc和size寫入flash,復(fù)位跳轉(zhuǎn)到app對(duì)應(yīng)的地址上運(yùn)行
int result = dfs_elm_mount(&g_fat,0,0);
        PRINTF_APP_MAIN("result=%d\\r\\n",result);
        //if (0 == check_boot(CONFIG_UBOOT_WAITING_TIME_S))
        {
            uint8_t get_firmware_name[32]={0};
            if (check_firmware_file(get_firmware_name))
            {
                PRINTF_APP_MAIN("find the firmware file name=%s\\r\\n",get_firmware_name);
                FRESULT res = f_open(&_update_file,get_firmware_name,FA_OPEN_EXISTING|FA_READ);
                uint32_t get_crc_result = 0;
                PRINTF_APP_MAIN("res=%d\\r\\n",res);
                if (FR_OK == res)
                {
                    uint32_t read_bytes = 0;
                    uint32_t total_bytes = _update_file.fsize-4;
                    uint32_t left_bytes = total_bytes;
                    uint32_t read_actual_bytes = 0;
                    uint32_t index_read = 0;
                    PRINTF_APP_MAIN("update_file.fsize=%d\\r\\n",_update_file.fsize);
                    PRINTF_APP_MAIN("total_bytes=%d\\r\\n",total_bytes);
                    /**/
                    if (total_bytes <= CONFIG_FLASH_APP_SIZE)
                    {
                        volatile uint32_t check_crypt_crc = 0;
                        hardware_crc32_short_start();
                        while (left_bytes > 0)
                        {
                            if (left_bytes > CONFIG_FLASH_SECTOR_SIZE)
                            {
                                read_bytes = CONFIG_FLASH_SECTOR_SIZE;
                            }
                            else
                            {
                                read_bytes = left_bytes;
                            }
                            FRESULT read_res = f_read(&_update_file,read_file,read_bytes,&read_actual_bytes);
                            if (0 == read_actual_bytes)
                            {
                                break;
                            }
                            check_crypt_crc = hardware_crc32_short(read_file,read_actual_bytes);
                            left_bytes = left_bytes - read_actual_bytes;
                        }
                        check_crypt_crc &= 0xFFFFFFFF;
                        (check_crypt_crc ^= 0x00000000);
                        PRINTF_APP_MAIN("check_crypt_crc=%x\\r\\n",check_crypt_crc);
                        FRESULT read_last_res = f_read(&_update_file,read_file,4,&read_actual_bytes);
                        if (FR_OK == read_last_res)
                        {
                            uint32_t source_crc = 0;
                            memcpy(&source_crc,read_file,4);
                            f_close(&_update_file);
                            memset(read_file,0,CONFIG_FLASH_SECTOR_SIZE);
                            PRINTF_APP_MAIN("source_crc=%x\\r\\n",source_crc);
                            if (check_crypt_crc == source_crc)
                            {
                                res = f_open(&_update_file,get_firmware_name,FA_READ);
                                if (FR_OK == res)  
                                {
                                    PRINTF_APP_MAIN("crypt_decode_part\\r\\n");
                                    if (crypt_decode_part(&_update_file,CONFIG_FLASH_APP_ADDRESS,&get_crc_result,&total_bytes) >= 0)
                                    {
                                        f_close(&_update_file);
                                        PRINTF_APP_MAIN("crypt_decode_part after\\r\\n");
                                        PRINTF_APP_MAIN("total_bytes=%d\\r\\n",total_bytes);
                                        PRINTF_APP_MAIN("get_crc_result=%x\\r\\n",get_crc_result);
                                        uint8_t set_data_buffer[20];
                                        set_data_buffer[0] = (uint8_t)(total_bytes);
                                        set_data_buffer[1] = (uint8_t)(total_bytes>>8);
                                        set_data_buffer[2] = (uint8_t)(total_bytes>>16);
                                        set_data_buffer[3] = (uint8_t)(total_bytes>>24);
                                        set_data_buffer[4] = (uint8_t)(get_crc_result);
                                        set_data_buffer[5] = (uint8_t)(get_crc_result>>8);
                                        set_data_buffer[6] = (uint8_t)(get_crc_result>>16);
                                        set_data_buffer[7] = (uint8_t)(get_crc_result>>24);        
                                        memset(&set_data_buffer[8],0xff,12);
                                        uint8_t idx_check=0;
                                        PRINTF_APP_MAIN("app info[");
                                        for (idx_check=0; idx_check<20; idx_check++)
                                        {
                                            PRINTF_APP_MAIN("%02x ",set_data_buffer[idx_check]);
                                            if(idx_check==3)
                                            {
                                                PRINTF_APP_MAIN("| ");
                                            }
                                        }
                                        PRINTF_APP_MAIN("]\\n");
                                        g_flash_write_bytes(CONFIG_FLASH_APP_SIZE_ADDRESS,set_data_buffer,LENGTH_OF_ARRAY(set_data_buffer));
                                        f_unlink(get_firmware_name);
                                        hw_cpu_reset(); 
                                    }
                                }
                            }
                        }
                    }
                }
            }
            //else
            {
                int res_umount = dfs_elm_unmount(&g_fat);
                PRINTF_APP_MAIN("res_umount=%d\\r\\n",res_umount);
                PRINTF_APP_MAIN("without update file then check app format\\r\\n"); 
                uint32_t get_app_size = flash_read_word(CONFIG_FLASH_APP_SIZE_ADDRESS);
                uint32_t get_app_crc = flash_read_word(CONFIG_FLASH_APP_CRC_ADDRESS);
                PRINTF_APP_MAIN("get_app_size=%d\\r\\n",get_app_size);
                PRINTF_APP_MAIN("get_app_crc=%x\\r\\n",get_app_crc);
                memset(read_file,0,CONFIG_FLASH_SECTOR_SIZE);
                if ((get_app_size > 0) && (get_app_size<= CONFIG_FLASH_APP_SIZE))
                {
                    uint32_t read_bytes,data_offset,calc_crc_val = 0;
                    uint32_t left_bytes = get_app_size;
                    uint32_t flash_addr_index = CONFIG_FLASH_APP_ADDRESS;
                    PRINTF_APP_MAIN("left_bytes=%d\\r\\n",left_bytes);
                    calc_crc_val = crc32_customized(CONFIG_CRC32_POLY,CONFIG_CRC32_INIT_VALUE,CONFIG_CRC32_OUT_XOR,CONFIG_FLASH_APP_ADDRESS,get_app_size);
                    PRINTF_APP_MAIN("calc_crc_val=%x\\r\\n",calc_crc_val);
                    if (get_app_crc == calc_crc_val)
                    {
                        PRINTF_APP_MAIN("run app\\r\\n");
                        iap_load_app(CONFIG_FLASH_APP_ADDRESS);
                    }
                }
            }
可以加個(gè)后門,比如上電后某個(gè)腳為某個(gè)電平停留在bootloader階段,進(jìn)行OTA響應(yīng)。
3.5 在KEIL上的應(yīng)用
3.5.1 準(zhǔn)備資源
編寫腳本文件另存為
firmware_update.bat
@echo off
set /a CONFIG_DEBUG_SWITCH=1
set /a CONFIG_FLASH_BASE=0x08000000
set /a CONFIG_FLASH_SECTOR_SIZE=(2*1024)
set /a CONFIG_FLASH_TOTAL_SIZE=(512*1024)
set /a CONFIG_BOOTLOADER_SIZE=(40*1024)
set /a CONFIG_APP_MAX_SIZE=(235*1024)
set /a CONFIG_CHECK_TYPE=3
set CONFIG_FN_BOOTLOADER=bootloader.bin
set CONFIG_FN_APP_SOURCE=app_.bin
set CONFIG_FN_APP_OUT=_upfw_.bin
set CONFIG_FN_PRODUCT=product_.bin
set CONFIG_FN_USER=sys.bin
::call clean.bat
copy %CONFIG_FN_BOOTLOADER% .\\bin
copy data_crypt.exe .\\bin
copy %CONFIG_FN_USER% .\\bin
cd bin/
data_crypt %CONFIG_FLASH_TOTAL_SIZE% %CONFIG_FLASH_SECTOR_SIZE% %CONFIG_BOOTLOADER_SIZE% %CONFIG_CHECK_TYPE% %CONFIG_FN_BOOTLOADER% %CONFIG_FN_APP_SOURCE% %CONFIG_FN_APP_OUT% %CONFIG_FN_PRODUCT% %CONFIG_DEBUG_SWITCH% %CONFIG_APP_MAX_SIZE% %CONFIG_FLASH_BASE% %CONFIG_FN_USER%
start %~dp0\\bin
3.5.2 Bootloader的KEIL配置
在keil的User 標(biāo)簽中添加AfterBuild/Rebuild腳本
$K\\ARM\\ARMCC\\bin\\fromelf.exe --bin --output=bin@L.bin ! L
將bootloader.bin存放到mdk項(xiàng)目文件目錄下
3.5.3 APP的KEIL配置

3.5.4 編譯后生成以下目標(biāo)文件
upfw_app.bin是壓縮和加密后的文件
app .bin是未加工的應(yīng)用程序
product_.bin是用于量產(chǎn)時(shí)的燒錄文件

4 應(yīng)用場(chǎng)景
4.1 通過(guò)電腦升級(jí)固件
枚舉成大容量存儲(chǔ)設(shè)備,通過(guò)USB線連接到電腦后會(huì)枚舉出一個(gè)U盤,把_upfw_app.bin復(fù)制到U盤根目錄下后重啟即可
可以參考這篇文章做一個(gè)USB大容量存儲(chǔ)設(shè)備STM32大容量存儲(chǔ)設(shè)備
4.2 通過(guò)U盤升級(jí)
工作在USB主機(jī)模式下,
把_upfw_app.bin復(fù)制到一個(gè)U盤根目錄下,插入U(xiǎn)盤后,主機(jī)識(shí)別到U盤根目錄下的文件,拷貝到文件系統(tǒng),復(fù)位重啟即可
4.3 通過(guò)離線燒錄器燒錄
在量產(chǎn)時(shí)可以使用合并好的未加密的product_.bin
4.4 OTA升級(jí)
可通過(guò)ymodem協(xié)議把加密文件傳輸?shù)轿募到y(tǒng)根目錄下,再重啟即可,
可以參考涂鴉智能的串口協(xié)議的設(shè)計(jì),可以參考rtthread自帶的ymodem例子,也可以參考小米用到的xmodem來(lái)設(shè)計(jì)自己的OTA
5 總結(jié)
加入OTA后,分配給APP的FLASH大小可能會(huì)少很多,把運(yùn)用在實(shí)際項(xiàng)目上的經(jīng)驗(yàn)復(fù)盤到自己的開(kāi)發(fā)板上,總結(jié)好理論經(jīng)驗(yàn),多做幾次實(shí)際操作,按照這樣的方法,應(yīng)該可以內(nèi)化吸收,把經(jīng)驗(yàn)轉(zhuǎn)換成我們內(nèi)在的東西,再刻意地運(yùn)用在新的項(xiàng)目上。
做任何事應(yīng)該都有方法,賺錢有方法,工作有方法,幫助他人有方法,教育孩子有方法,努力是沒(méi)有用的,有了正確的方法和方向的努力才有用。
- 
                                FlaSh
                                +關(guān)注
關(guān)注
10文章
1705瀏覽量
154336 - 
                                物聯(lián)網(wǎng)
                                +關(guān)注
關(guān)注
2938文章
47085瀏覽量
404770 - 
                                STM32
                                +關(guān)注
關(guān)注
2302文章
11108瀏覽量
370260 - 
                                固件
                                +關(guān)注
關(guān)注
10文章
568瀏覽量
24518 - 
                                固件升級(jí)
                                +關(guān)注
關(guān)注
0文章
36瀏覽量
12383 
發(fā)布評(píng)論請(qǐng)先 登錄
Ethernet遠(yuǎn)程固件升級(jí)
昂達(dá)vx979+固件升級(jí)
什么是MP3固件升級(jí)
USB設(shè)備固件升級(jí)_cn
MCU固件升級(jí)的閃存劃分方法分享
固件升級(jí)和軟件升級(jí)區(qū)別
如何對(duì)物聯(lián)設(shè)備進(jìn)行遠(yuǎn)程固件升級(jí)?
多路手機(jī)固件升級(jí)工具設(shè)計(jì)
    
          
        
        
固件升級(jí)的設(shè)計(jì)
                
 
    
    
    
           
            
            
                
            
評(píng)論