您现在的位置是:首页 > 正文

nRF52832 bootloader DFU固件升级源码详解(结合RTT日志打印)

2024-02-29 12:39:42阅读 0

一、前文

在bootloader代码中添加大量日志,通过RTT打印日志。
通过日志大致了解bootloader中DFU固件升级的代码逻辑。

大致流程:

  • nrf_dfu_command_req:命令触发,进入DFU升级模式
  • dfu_handle_prevalidate:校验Image固件的哈希值
  • nrf_dfu_find_cache:根据s_dfu_settings参数,判断新固件的flash存储地址
  • nrf_dfu_data_req:新固件数据写入到flash存储地址
  • nrf_dfu_postvalidate:校验新固件哈希值,并更新s_dfu_settings参数
  • on_dfu_complete,dfu传输完成,重启设备
  • nrf_dfu_continue_bank:重启后,继续上次固件分区的操作
  • nrf_dfu_app_continue:新固件从bank1复制到bank0,bank1擦除
  • nrf_dfu_app_is_valid:判断bank0的固件是否有效
  • nrf_bootloader_app_start:从bootloader跳转app

二、init

00> :INFO:Inside main
00> :INFO:In nrf_bootloader_init
00> :INFO:nrf_dfu_init
00> :INFO:-------------- nrf_dfu_settings_init --------------
00> :INFO:  crc=0x50e2fe84
00> :INFO:  write_offset=0x0
00> :INFO:  settings_version=1
00> :INFO:  app_version=2
00> :INFO:  bootloader_version=0
00> :INFO:  bank_laytout=0
00> :INFO:  bank_current=0
00> :INFO:  bank_0.bank_code=1
00> :INFO:  bank_0.image_size=0xeb14
00> :INFO:  bank_0.image_crc=0xa7f5f361
00> :INFO:  bank_1.bank_code=0
00> :INFO:  bank_1.image_size=0x0
00> :INFO:  bank_1.image_crc=0x0
00> :INFO:  progress.data_size=0x0
00> :INFO:  progress.cmd_size=0x0
00> :INFO:  progress.cmd_offset=0x0
00> :INFO:  progress.cmd_crc=0x0
00> :INFO:  progress.fw_image_crc=0x0
00> :INFO:  progress.fw_image_offset=0x0
00> :INFO:-------------- nrf_dfu_continue_bank -------------- 
00> :INFO:  src_addr=0x1f000
00> :INFO:  bank_code=1
00> :INFO:  image_size=0xeb14
00> :INFO:  image_crc=0xa7f5f361
00> :INFO:  Valid App
00> :INFO:nrf_dfu_app_is_valid. CRC fail: 0xe00bbbd!=0xa7f5f361
00> :INFO:nrf_dfu_transports_init start
00> :INFO:nrf_dfu_transports_init num_transports: 1
00> :INFO:ble_stack_init, vector table: 0x00073000
00> SDH:WARNING:sd_ble_enable: RAM start should be adjusted to 0x20002798
00> SDH:WARNING:RAM size should be adjusted to 0xd868 
00> :INFO:nrf_dfu_transports_init end
00> :INFO:------- nrf_dfu_flash_init-------
00> :INFO:Waiting for events

三、nrf_dfu_command_req

  • nrf_dfu_command_req:命令触发,进入DFU升级模式
00> :INFO:nrf_dfu_command_req, Valid Command: NRF_DFU_OBJECT_OP_SELECT
00> :INFO:nrf_dfu_command_req, Before OP create command
00> :INFO:nrf_dfu_command_req, Before OP write command
00> :INFO:nrf_dfu_command_req, Before OP write command
00> :INFO:nrf_dfu_command_req, Before OP write command
00> :INFO:nrf_dfu_command_req, Before OP write command
00> :INFO:nrf_dfu_command_req, Before OP write command
00> :INFO:nrf_dfu_command_req, Before OP write command
00> :INFO:nrf_dfu_command_req, Before OP write command
00> :INFO:nrf_dfu_command_req, Before OP write command
00> :INFO:nrf_dfu_command_req, Before OP execute command
00> :INFO:nrf_dfu_command_req, Valid command execute
00> :INFO:
00> 12 8A 01 0A 44 08 01 12 40 08 02 10 34 1A 02 8C  .Š..D...@...4..Œ
00> 01 20 00 28 00 30 00 38 94 D6 03 42 24 08 03 12  . .(.0.8”Ö.B$...
00> 20 0B CD 46 83 80 90 50 6A BC 88 92 02 E4 D8 E2   .ÍFƒ.Pj¼ˆ’.äØâ
00> 79 12 17 ED 09 76 C6 33 3D B1 8C BB 04 7B 5E 45  y..í.vÆ3=±Œ».{^E
00> EE 48 00 52 04 08 01 12 00 10 00 1A 40 26 9E FB  îH.R........@&žû
00> 02 B2 CF C2 F7 45 58 6C A9 AA 10 4E 58 D7 CE 74  .²ÏÂ÷EXl©ª.NX×.t
00> 6B 55 25 C5 F6 DD 7A C4 AE DC A7 57 D8 E9 FB 94  kU%ÅöÝzĮܧWØéû”
00> 1D 9E FB 36 C3 B2 28 3D FC BE B0 B3 3A 0C 09 98  .žû6ò(=ü¾°³:..˜
00> FC F5 C7 A2 09 92 50 E0 92 95 D3 FD 51           üõÇ¢.’Pà’•ÓýQ   
00> :INFO:
00> :INFO:nrf_dfu_command_req, Handling signed command

四、dfu_handle_prevalidate

  • dfu_handle_prevalidate:校验Image固件的哈希值
00> :INFO:dfu_handle_prevalidate, Req version: 2, Present: 2
00> :INFO:dfu_handle_prevalidate, Init command:
00> :INFO:
00> 12 8A 01 0A 44 08 01 12 40 08 02 10 34 1A 02 8C  .Š..D...@...4..Œ
00> 01 20 00 28 00 30 00 38 94 D6 03 42 24 08 03 12  . .(.0.8”Ö.B$...
00> 20 0B CD 46 83 80 90 50 6A BC 88 92 02 E4 D8 E2   .ÍFƒ.Pj¼ˆ’.äØâ
00> 79 12 17 ED 09 76 C6 33 3D B1 8C BB 04 7B 5E 45  y..í.vÆ3=±Œ».{^E
00> EE 48 00 52 04 08 01 12 00 10 00 1A 40 26 9E FB  îH.R........@&žû
00> 02 B2 CF C2 F7 45 58 6C A9 AA 10 4E 58 D7 CE 74  .²ÏÂ÷EXl©ª.NX×.t
00> 6B 55 25 C5 F6 DD 7A C4 AE DC A7 57 D8 E9 FB 94  kU%ÅöÝzĮܧWØéû”
00> 1D 9E FB 36 C3 B2 28 3D FC BE B0 B3 3A 0C 09 98  .žû6ò(=ü¾°³:..˜
00> FC F5 C7 A2 09 92 50 E0 92 95 D3 FD 51           üõÇ¢.’Pà’•ÓýQ   
00> :INFO:
00> :INFO:dfu_handle_prevalidate, p_Init command:
00> :INFO:
00> 08 02 10 34 1A 02 8C 01 20 00 28 00 30 00 38 94  ...4..Œ. .(.0.8”
00> D6 03 42 24 08 03 12 20 0B CD 46 83 80 90 50 6A  Ö.B$... .ÍFƒ.Pj
00> BC 88 92 02 E4 D8 E2 79 12 17 ED 09 76 C6 33 3D  ¼ˆ’.äØây..í.vÆ3=
00> B1 8C BB 04 7B 5E 45 EE 48 00 52 04 08 01 12 00  ±Œ».{^EîH.R.....
00> :INFO:
00> :INFO:dfu_handle_prevalidate, Signature
00> :INFO:
00> 26 9E FB 02 B2 CF C2 F7 45 58 6C A9 AA 10 4E 58  &žû.²ÏÂ÷EXl©ª.NX
00> D7 CE 74 6B 55 25 C5 F6 DD 7A C4 AE DC A7 57 D8  ×.tkU%ÅöÝzĮܧWØ
00> E9 FB 94 1D 9E FB 36 C3 B2 28 3D FC BE B0 B3 3A  éû”.žû6ò(=ü¾°³:
00> 0C 09 98 FC F5 C7 A2 09 92 50 E0 92 95 D3 FD 51  ..˜üõÇ¢.’Pà’•ÓýQ
00> :INFO:
00> :INFO:dfu_handle_prevalidate, signature len: 64
00> :INFO:dfu_handle_prevalidate, Image verified
00> :INFO:dfu_handle_prevalidate, Write address set to 0x0002e000
00> :INFO:dfu_handle_prevalidate, DFU prevalidate SUCCESSFUL!

五、nrf_dfu_find_cache

  • nrf_dfu_find_cache:根据s_dfu_settings参数,判断新固件的flash存储地址
00> :INFO:-------------- nrf_dfu_find_cache --------------
00> :INFO:  free_size=311296
00> :INFO:  size_req=60180
00> :INFO:  dual_bank_only=0, p_address=0x20002c08
00> :INFO:  bank_layout: 0
00> :INFO:  bank_0.bank_code: 0x01: bank_0.image_size: 60180
00> :INFO:  bank_1.bank_code: 0x00: bank_1.image_size: 0
00> :INFO:  free_size before bank select: 311296
00> :INFO:  free_size: 249856, size_req: 60180
00> :INFO:  Using bank_1

uint32_t nrf_dfu_find_cache(uint32_t size_req, bool dual_bank_only, uint32_t * p_address)
{
    // TODO: Prevalidate p_address and p_bank

    uint32_t free_size =  DFU_REGION_TOTAL_SIZE - DFU_APP_DATA_RESERVED;
    nrf_dfu_bank_t * p_bank;

	
    NRF_LOG_INFO("-------------- nrf_dfu_find_cache --------------\r\n");
    NRF_LOG_INFO("	free_size=%d\r\n", free_size);
    NRF_LOG_INFO("	size_req=%d\r\n", size_req);

    // Simple check if size requirement can me met
    if(free_size < size_req)
    {
				NRF_LOG_INFO("	error: free_size < size_req!!!\r\n");
        return NRF_ERROR_NO_MEM;
    }

    NRF_LOG_INFO("	dual_bank_only=%d, p_address=0x%08x\r\n", dual_bank_only, (uint32_t)p_address);
    NRF_LOG_INFO("	bank_layout: %d\r\n", s_dfu_settings.bank_layout);
    NRF_LOG_INFO("	bank_0.bank_code: 0x%02x: bank_0.image_size: %d\r\n", s_dfu_settings.bank_0.bank_code, s_dfu_settings.bank_0.image_size);
    NRF_LOG_INFO("	bank_1.bank_code: 0x%02x: bank_1.image_size: %d\r\n", s_dfu_settings.bank_1.bank_code, s_dfu_settings.bank_1.image_size);

    // Setting bank_0 as candidate
    p_bank = &s_dfu_settings.bank_0;

    // Setting candidate address
    (*p_address) = MAIN_APPLICATION_START_ADDR;

    // Calculate free size
    if (s_dfu_settings.bank_0.bank_code == NRF_DFU_BANK_VALID_APP)
    {
        // Valid app present.

        NRF_LOG_INFO("	free_size before bank select: %d\r\n", free_size);

        free_size -= align_to_page(p_bank->image_size, CODE_PAGE_SIZE);

        NRF_LOG_INFO("	free_size: %d, size_req: %d\r\n", free_size, size_req);

        // Check if we can fit the new in the free space or if removal of old app is required.
        if(size_req > free_size)
        {
            // Not enough room in free space (bank_1)
            if ((dual_bank_only))
            {
                NRF_LOG_INFO("	Failure: dual bank restriction\r\n");
                return NRF_ERROR_NO_MEM;
            }

            // Can only support single bank update, clearing old app.
            s_dfu_settings.bank_layout = NRF_DFU_BANK_LAYOUT_SINGLE;
            s_dfu_settings.bank_current = NRF_DFU_CURRENT_BANK_0;
            p_bank = &s_dfu_settings.bank_0;
            NRF_LOG_INFO("	Enforcing single bank\r\n");
        }
        else
        {
            // Room in bank_1 for update
            // Ensure we are using dual bank layout
            s_dfu_settings.bank_layout = NRF_DFU_BANK_LAYOUT_DUAL;
            s_dfu_settings.bank_current = NRF_DFU_CURRENT_BANK_1;
            p_bank = &s_dfu_settings.bank_1;
            // Set to first free page boundry after previous app
            (*p_address) += align_to_page(s_dfu_settings.bank_0.image_size, CODE_PAGE_SIZE);
            NRF_LOG_INFO("	Using bank_1\r\n");
        }
    }
    else
    {
        // No valid app present. Promoting dual bank.
        s_dfu_settings.bank_layout  = NRF_DFU_BANK_LAYOUT_DUAL;
        s_dfu_settings.bank_current = NRF_DFU_CURRENT_BANK_0;

        p_bank = &s_dfu_settings.bank_0;
        NRF_LOG_INFO("	No previous, using bank 0\r\n");
    }

    // Set the bank-code to invalid, and reset size/CRC
    memset(p_bank, 0, sizeof(nrf_dfu_bank_t));

    // Store the Firmware size in the bank for continuations
    p_bank->image_size = size_req;
    return NRF_SUCCESS;
}

六、nrf_dfu_data_req

  • nrf_dfu_data_req:新固件数据写入到flash存储地址
00> :INFO:nrf_dfu_flash_erase: 0x0007f000, num: 1
00> :INFO:-------------- nrf_dfu_settings_write --------------
00> :INFO:  crc=0xe5ffe6f9
00> :INFO:  write_offset=0x0
00> :INFO:  settings_version=1
00> :INFO:  app_version=2
00> :INFO:  bootloader_version=0
00> :INFO:  bank_laytout=0
00> :INFO:  bank_current=1
00> :INFO:  bank_0.bank_code=1
00> :INFO:  bank_0.image_size=0xeb14
00> :INFO:  bank_0.image_crc=0xa7f5f361
00> :INFO:  bank_1.bank_code=0
00> :INFO:  bank_1.image_size=0xeb14
00> :INFO:  bank_1.image_crc=0x0
00> :INFO:  progress.data_size=0x0
00> :INFO:  progress.cmd_size=0x8d
00> :INFO:  progress.cmd_offset=0x8d
00> :INFO:  progress.cmd_crc=0x385d2a18
00> :INFO:  progress.fw_image_crc=0x0
00> :INFO:  progress.fw_image_offset=0x0
00> :INFO:nrf_dfu_flash_store, dest: 0x0007f000, src: 0x200036cc, len: 87
00> :INFO:----------------------------------------------------
00> :INFO:nrf_dfu_command_req, Setting DFU flag to initialized
00> :INFO:nrf_dfu_data_req, Valid Data Read info
00> :INFO:nrf_dfu_data_req, Before OP create
00> :INFO:nrf_dfu_data_req, Valid Data Create
00> :INFO:nrf_dfu_flash_erase: 0x0002e000, num: 1
00> :INFO:nrf_dfu_data_req, Creating object with size: 4096. Offset: 0x00000000, CRC: 0x00000000
00> :INFO:nrf_dfu_flash_store, dest: 0x0002e000, src: 0x20002dbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002e100, src: 0x20002ebc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002e200, src: 0x20002fbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002e300, src: 0x20002cbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002e400, src: 0x20002dbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002e500, src: 0x20002ebc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002e600, src: 0x20002fbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002e700, src: 0x20002cbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002e800, src: 0x20002dbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002e900, src: 0x20002ebc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002ea00, src: 0x20002fbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002eb00, src: 0x20002cbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002ec00, src: 0x20002dbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002ed00, src: 0x20002ebc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002ee00, src: 0x20002fbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002ef00, src: 0x20002cbc, len: 64
00> :INFO:nrf_dfu_data_req, Before OP crc
00> :INFO:nrf_dfu_data_req, Before OP execute
00> :INFO:nrf_dfu_data_req, Valid Data Execute
00> :INFO:nrf_dfu_flash_erase: 0x0007f000, num: 1
00> :INFO:-------------- nrf_dfu_settings_write --------------
00> :INFO:  crc=0x43c36595
00> :INFO:  write_offset=0x1000
00> :INFO:  settings_version=1
00> :INFO:  app_version=2
00> :INFO:  bootloader_version=0
00> :INFO:  bank_laytout=0
00> :INFO:  bank_current=1
00> :INFO:  bank_0.bank_code=1
00> :INFO:  bank_0.image_size=0xeb14
00> :INFO:  bank_0.image_crc=0xa7f5f361
00> :INFO:  bank_1.bank_code=0
00> :INFO:  bank_1.image_size=0xeb14
00> :INFO:  bank_1.image_crc=0x0
00> :INFO:  progress.data_size=0x0
00> :INFO:  progress.cmd_size=0x8d
00> :INFO:  progress.cmd_offset=0x8d
00> :INFO:  progress.cmd_crc=0x385d2a18
00> :INFO:  progress.fw_image_crc=0xc8d01c5f
00> :INFO:  progress.fw_image_offset=0x1000
00> :INFO:nrf_dfu_flash_store, dest: 0x0007f000, src: 0x200036cc, len: 87
00> :INFO:----------------------------------------------------
00> :INFO:nrf_dfu_data_req, Before OP create
00> :INFO:nrf_dfu_data_req, Valid Data Create
00> :INFO:nrf_dfu_flash_erase: 0x0002f000, num: 1
00> :INFO:nrf_dfu_data_req, Creating object with size: 4096. Offset: 0x00001000, CRC: 0xc8d01c5f
00> :INFO:nrf_dfu_flash_store, dest: 0x0002f000, src: 0x20002ebc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002f100, src: 0x20002fbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002f200, src: 0x20002cbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002f300, src: 0x20002dbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002f400, src: 0x20002ebc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002f500, src: 0x20002fbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002f600, src: 0x20002cbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002f700, src: 0x20002dbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002f800, src: 0x20002ebc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002f900, src: 0x20002fbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002fa00, src: 0x20002cbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002fb00, src: 0x20002dbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002fc00, src: 0x20002ebc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002fd00, src: 0x20002fbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002fe00, src: 0x20002cbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x0002ff00, src: 0x20002dbc, len: 64
00> :INFO:nrf_dfu_data_req, Before OP crc
00> :INFO:nrf_dfu_data_req, Before OP execute
00> :INFO:nrf_dfu_data_req, Valid Data Execute
00> :INFO:nrf_dfu_flash_erase: 0x0007f000, num: 1
00> :INFO:-------------- nrf_dfu_settings_write --------------
00> :INFO:  crc=0x12e1649e
00> :INFO:  write_offset=0x2000
00> :INFO:  settings_version=1
00> :INFO:  app_version=2
00> :INFO:  bootloader_version=0
00> :INFO:  bank_laytout=0
00> :INFO:  bank_current=1
00> :INFO:  bank_0.bank_code=1
00> :INFO:  bank_0.image_size=0xeb14
00> :INFO:  bank_0.image_crc=0xa7f5f361
00> :INFO:  bank_1.bank_code=0
00> :INFO:  bank_1.image_size=0xeb14
00> :INFO:  bank_1.image_crc=0x0
00> :INFO:  progress.data_size=0x0
00> :INFO:  progress.cmd_size=0x8d
00> :INFO:  progress.cmd_offset=0x8d
00> :INFO:  progress.cmd_crc=0x385d2a18
00> :INFO:  progress.fw_image_crc=0xfcd38ab0
00> :INFO:  progress.fw_image_offset=0x2000
00> :INFO:nrf_dfu_flash_store, dest: 0x0007f000, src: 0x200036cc, len: 87
00> :INFO:----------------------------------------------------
00> :INFO:nrf_dfu_data_req, Before OP create
00> :INFO:nrf_dfu_data_req, Valid Data Create
00> :INFO:nrf_dfu_flash_erase: 0x00030000, num: 1
00> :INFO:nrf_dfu_data_req, Creating object with size: 4096. Offset: 0x00002000, CRC: 0xfcd38ab0
00> :INFO:nrf_dfu_flash_store, dest: 0x00030000, src: 0x20002fbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x00030100, src: 0x20002cbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x00030200, src: 0x20002dbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x00030300, src: 0x20002ebc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x00030400, src: 0x20002fbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x00030500, src: 0x20002cbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x00030600, src: 0x20002dbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x00030700, src: 0x20002ebc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x00030800, src: 0x20002fbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x00030900, src: 0x20002cbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x00030a00, src: 0x20002dbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x00030b00, src: 0x20002ebc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x00030c00, src: 0x20002fbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x00030d00, src: 0x20002cbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x00030e00, src: 0x20002dbc, len: 64
00> :INFO:nrf_dfu_flash_store, dest: 0x00030f00, src: 0x20002ebc, len: 64
00> :INFO:nrf_dfu_data_req, Before OP crc
00> :INFO:nrf_dfu_data_req, Before OP execute
00> :INFO:nrf_dfu_data_req, Valid Data Execute

七、nrf_dfu_postvalidate

  • nrf_dfu_postvalidate:校验新固件哈希值,并更新s_dfu_settings参数
00> :INFO:nrf_dfu_data_req, Waiting for 0 pending flash operations before doing postvalidate.
00> :INFO:nrf_dfu_data_req, Doing postvalidate
00> :INFO:nrf_dfu_postvalidate, Current bank is bank 1
00> :INFO:nrf_dfu_postvalidate, Successfully run the postvalidation check!

八、on_dfu_complete

  • on_dfu_complete,dfu传输完成,重启设备
static void on_dfu_complete(fs_evt_t const * const evt, fs_ret_t result)
{
    NRF_LOG_INFO("on_dfu_complete, Resetting device. \r\n");
    (void)nrf_dfu_transports_close();
    NVIC_SystemReset();
    return;
}
00> :INFO:Waiting for other flash operation to finish.
00> :INFO:Waiting for other flash operation to finish.
00> :INFO:nrf_dfu_flash_erase: 0x0007f000, num: 1
00> :INFO:-------------- nrf_dfu_settings_write --------------
00> :INFO:  crc=0xcc92671c
00> :INFO:  write_offset=0x0
00> :INFO:  settings_version=1
00> :INFO:  app_version=2
00> :INFO:  bootloader_version=0
00> :INFO:  bank_laytout=0
00> :INFO:  bank_current=1
00> :INFO:  bank_0.bank_code=1
00> :INFO:  bank_0.image_size=0xeb14
00> :INFO:  bank_0.image_crc=0xa7f5f361
00> :INFO:  bank_1.bank_code=1
00> :INFO:  bank_1.image_size=0xeb14
00> :INFO:  bank_1.image_crc=0xa7f5f361
00> :INFO:  progress.data_size=0x0
00> :INFO:  progress.cmd_size=0x0
00> :INFO:  progress.cmd_offset=0x0
00> :INFO:  progress.cmd_crc=0x0
00> :INFO:  progress.fw_image_crc=0x0
00> :INFO:  progress.fw_image_offset=0x0
00> :INFO:nrf_dfu_flash_store, dest: 0x0007f000, src: 0x200036cc, len: 87
00> :INFO:----------------------------------------------------
00> :INFO:on_dfu_complete, Resetting device. 
00> :INFO:num transports: 1
00> :INFO:ble_dfu_transport_close, Waiting for buffers to be cleared before disconnect

九、nrf_dfu_continue_bank

  • nrf_dfu_continue_bank:重启后,继续上次固件分区的操作
00> :INFO:-------------- nrf_dfu_continue_bank -------------- 
00> :INFO:  src_addr=0x2e000
00> :INFO:  bank_code=1
00> :INFO:  image_size=0xeb14
00> :INFO:  image_crc=0xa7f5f361
00> :INFO:  Valid App
static uint32_t nrf_dfu_continue_bank(nrf_dfu_bank_t * p_bank, uint32_t src_addr, uint32_t * p_enter_dfu_mode)
{
    uint32_t ret_val = NRF_SUCCESS;
	
	NRF_LOG_INFO("-------------- nrf_dfu_continue_bank -------------- \r\n");
    NRF_LOG_INFO("	src_addr=0x%x\r\n", src_addr);
    NRF_LOG_INFO("	bank_code=%d\r\n", p_bank->bank_code);
    NRF_LOG_INFO("	image_size=0x%x\r\n", p_bank->image_size);
    NRF_LOG_INFO("	image_crc=0x%x\r\n", p_bank->image_crc);
	
    switch (p_bank->bank_code)
    {
       case NRF_DFU_BANK_VALID_APP:
            NRF_LOG_INFO("	Valid App\r\n");
            if(s_dfu_settings.bank_current == NRF_DFU_CURRENT_BANK_1)
            {
                // Only continue copying if valid app in bank1
                ret_val = nrf_dfu_app_continue(src_addr);
            }
            break;

       case NRF_DFU_BANK_VALID_SD:
            NRF_LOG_INFO("	Valid SD\r\n");
            // There is a valid SD that needs to be copied (or continued)
            ret_val = nrf_dfu_sd_continue(src_addr, p_bank);
            (*p_enter_dfu_mode) = 1;
            break;

        case NRF_DFU_BANK_VALID_BL:
            NRF_LOG_INFO("	Valid BL\r\n");
            // There is a valid BL that must be copied (or continued)
            ret_val = nrf_dfu_bl_continue(src_addr, p_bank);
            break;

        case NRF_DFU_BANK_VALID_SD_BL:
            NRF_LOG_INFO("	Single: Valid SD + BL\r\n");
            // There is a valid SD + BL that must be copied (or continued)
            ret_val = nrf_dfu_sd_bl_continue(src_addr, p_bank);
            // Set the bank-code to invalid, and reset size/CRC
            (*p_enter_dfu_mode) = 1;
            break;

        case NRF_DFU_BANK_INVALID:
        default:
            NRF_LOG_INFO("	Single: Invalid bank\r\n");
            break;
    }

    return ret_val;
}

十、nrf_dfu_app_continue

  • nrf_dfu_app_continue:新固件从bank1复制到bank0,bank1擦除
00> :INFO:-------------- nrf_dfu_app_continue --------------
00> :INFO:  src_addr=0x2e000
00> :INFO:  target_addr=0x2e000
00> :INFO:nrf_dfu_flash_erase: 0x0001f000, num: 1
00> :INFO:nrf_dfu_flash_store, dest: 0x0001f000, src: 0x0002e000, len: 4096
00> :INFO:nrf_dfu_flash_erase: 0x0002e000, num: 1
00> :INFO:nrf_dfu_flash_erase: 0x0007f000, num: 1
00> :INFO:-------------- nrf_dfu_settings_write --------------
00> :INFO:  crc=0x668b1d2a
00> :INFO:  write_offset=0x1000
00> :INFO:  settings_version=1
00> :INFO:  app_version=2
00> :INFO:  bootloader_version=0
00> :INFO:  bank_laytout=0
00> :INFO:  bank_current=1
00> :INFO:  bank_0.bank_code=1
00> :INFO:  bank_0.image_size=0xeb14
00> :INFO:  bank_0.image_crc=0xa7f5f361
00> :INFO:  bank_1.bank_code=1
00> :INFO:  bank_1.image_size=0xeb14
00> :INFO:  bank_1.image_crc=0xa7f5f361
00> :INFO:  progress.data_size=0x0
00> :INFO:  progress.cmd_size=0x0
00> :INFO:  progress.cmd_offset=0x0
00> :INFO:  progress.cmd_crc=0x0
00> :INFO:  progress.fw_image_crc=0x0
00> :INFO:  progress.fw_image_offset=0x0
00> :INFO:nrf_dfu_flash_store, dest: 0x0007f000, src: 0x200036cc, len: 87
00> :INFO:----------------------------------------------------
00> :INFO:  src_addr=0x2f000
00> :INFO:  target_addr=0x2f000
00> :INFO:nrf_dfu_flash_erase: 0x00020000, num: 1
00> :INFO:nrf_dfu_flash_store, dest: 0x00020000, src: 0x0002f000, len: 4096
00> :INFO:nrf_dfu_flash_erase: 0x0002f000, num: 1
00> :INFO:nrf_dfu_flash_erase: 0x0007f000, num: 1
00> :INFO:-------------- nrf_dfu_settings_write --------------
00> :INFO:  crc=0x43d19531
00> :INFO:  write_offset=0x2000
00> :INFO:  settings_version=1
00> :INFO:  app_version=2
00> :INFO:  bootloader_version=0
00> :INFO:  bank_laytout=0
00> :INFO:  bank_current=1
00> :INFO:  bank_0.bank_code=1
00> :INFO:  bank_0.image_size=0xeb14
00> :INFO:  bank_0.image_crc=0xa7f5f361
00> :INFO:  bank_1.bank_code=1
00> :INFO:  bank_1.image_size=0xeb14
00> :INFO:  bank_1.image_crc=0xa7f5f361
00> :INFO:  progress.data_size=0x0
00> :INFO:  progress.cmd_size=0x0
00> :INFO:  progress.cmd_offset=0x0
00> :INFO:  progress.cmd_crc=0x0
00> :INFO:  progress.fw_image_crc=0x0
00> :INFO:  progress.fw_image_offset=0x0
00> :INFO:nrf_dfu_flash_store, dest: 0x0007f000, src: 0x200036cc, len: 87
00> :INFO:----------------------------------------------------
00> :INFO:  src_addr=0x30000
00> :INFO:  target_addr=0x30000
00> :INFO:nrf_dfu_flash_erase: 0x00021000, num: 1
00> :INFO:nrf_dfu_flash_store, dest: 0x00021000, src: 0x00030000, len: 4096
00> :INFO:nrf_dfu_flash_erase: 0x00030000, num: 1
00> :INFO:nrf_dfu_flash_erase: 0x0007f000, num: 1
00> :INFO:-------------- nrf_dfu_settings_write --------------
00> :INFO:  crc=0xe9c8ef07
00> :INFO:  write_offset=0x3000
00> :INFO:  settings_version=1
00> :INFO:  app_version=2
00> :INFO:  bootloader_version=0
00> :INFO:  bank_laytout=0
00> :INFO:  bank_current=1
00> :INFO:  bank_0.bank_code=1
00> :INFO:  bank_0.image_size=0xeb14
00> :INFO:  bank_0.image_crc=0xa7f5f361
00> :INFO:  bank_1.bank_code=1
00> :INFO:  bank_1.image_size=0xeb14
00> :INFO:  bank_1.image_crc=0xa7f5f361
00> :INFO:  progress.data_size=0x0
00> :INFO:  progress.cmd_size=0x0
00> :INFO:  progress.cmd_offset=0x0
00> :INFO:  progress.cmd_crc=0x0
00> :INFO:  progress.fw_image_crc=0x0
00> :INFO:  progress.fw_image_offset=0x0
00> :INFO:nrf_dfu_flash_store, dest: 0x0007f000, src: 0x200036cc, len: 87
00> :INFO:----------------------------------------------------

十一、nrf_dfu_app_is_valid

  • nrf_dfu_app_is_valid:判断bank0的固件是否有效
00> :INFO:nrf_dfu_app_is_valid, App was valid
00> :INFO:nrf_dfu_app_is_valid, App was valid
bool nrf_dfu_app_is_valid(void)
{
    if (s_dfu_settings.bank_0.bank_code != NRF_DFU_BANK_VALID_APP)
    {
       // Bank 0 has no valid app. Nothing to boot
       NRF_LOG_INFO("nrf_dfu_app_is_valid, app invalid!\r\n");
       return false;
    }

    // If CRC == 0, this means CRC check is skipped.
    if (s_dfu_settings.bank_0.image_crc != 0)
    {
        uint32_t crc = crc32_compute((uint8_t*) CODE_REGION_1_START,
                                     s_dfu_settings.bank_0.image_size,
                                     NULL);

        if (crc != s_dfu_settings.bank_0.image_crc)
        {
            // CRC does not match with what is stored.
						NRF_LOG_INFO("nrf_dfu_app_is_valid. CRC fail: 0x%x!=0x%x\r\n", crc, s_dfu_settings.bank_0.image_crc);
            return  false;
        }
    }

    NRF_LOG_INFO("nrf_dfu_app_is_valid, App was valid\r\n");
    return true;
}

十二、nrf_bootloader_app_start

  • nrf_bootloader_app_start:从bootloader跳转app
if (nrf_dfu_app_is_valid())
{
    NRF_LOG_INFO("Jumping to: 0x%08x\r\n", MAIN_APPLICATION_START_ADDR);
    nrf_bootloader_app_start(MAIN_APPLICATION_START_ADDR);
}
void nrf_bootloader_app_start(uint32_t start_addr)
{
    NRF_LOG_INFO("Running nrf_bootloader_app_start with address: 0x%08x\r\n", start_addr);

#ifdef BLE_STACK_SUPPORT_REQD
    uint32_t err_code;

    //NRF_LOG_INFO("Initializing SD in mbr\r\n");
    err_code = nrf_dfu_mbr_init_sd();
    if(err_code != NRF_SUCCESS)
    {
        NRF_LOG_INFO("Failed running nrf_dfu_mbr_init_sd\r\n");
        return;
    }

#endif

    // Disable interrupts
    NRF_LOG_INFO("Disabling interrupts\r\n");

    NVIC->ICER[0]=0xFFFFFFFF;
#if defined(__NRF_NVIC_ISER_COUNT) && __NRF_NVIC_ISER_COUNT == 2
    NVIC->ICER[1]=0xFFFFFFFF;
#endif

#ifdef BLE_STACK_SUPPORT_REQD
    // Set the sd softdevice vector table base address
    NRF_LOG_INFO("Setting SD vector table base: 0x%08x\r\n", start_addr);
    err_code = sd_softdevice_vector_table_base_set(start_addr);
    if(err_code != NRF_SUCCESS)
    {
        NRF_LOG_INFO("Failed running sd_softdevice_vector_table_base_set\r\n");
        return;
    }
#endif

    // Run application
    nrf_bootloader_app_start_impl(start_addr);
}

觉得好,就一键三连呗(点赞+收藏+关注)

网站文章