possible bug in mcuboot/boot/nxp_mcux_sdk/flashapi/flash_api.c

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

possible bug in mcuboot/boot/nxp_mcux_sdk/flashapi/flash_api.c

385 次查看
mastupristi
Senior Contributor I

Hi,

I use RT1021 and I'm testing mcuboot.

I want image to be at most 512kB, so in sblconfig.h I define:

#define CONFIG_MCUBOOT_MAX_IMG_SECTORS 128 /*4kB sector * 128 = 512kB */

 

and in flash_partitioning.h I have defined:

#define BOOT_FLASH_ACT_APP              0x60100000
#define BOOT_FLASH_CAND_APP             0x60180000

 

However, I get an error:

Failed reading sectors; BOOT_MAX_IMG_SECTORS=128 - too small?

I debugged and found the point where I think there is a bug.

 

https://github.com/nxp-mcuxpresso/mcuboot/blob/mcux_main/boot/nxp_mcux_sdk/flashapi/flash_api.c#L378

int flash_area_get_sectors(int fa_id, uint32_t *count, struct flash_sector *sectors)
{
    const struct flash_area *fa;
    uint32_t max_cnt = *count;
    uint32_t rem_len;
    int rc = -1;

    if (flash_area_open(fa_id, &fa))
        goto out;

    if (*count < 1)
        goto fa_close_out;

    rem_len = fa->fa_size;
    *count  = 0;
    while ((rem_len > 0) && (*count < max_cnt))
    {
        if (rem_len < MFLASH_SECTOR_SIZE)
        {
            goto fa_close_out;
        }

        sectors[*count].fs_off  = MFLASH_SECTOR_SIZE * (*count);
        sectors[*count].fs_size = MFLASH_SECTOR_SIZE;
        *count                  = *count + 1;
        rem_len -= MFLASH_SECTOR_SIZE;
    }

    if (*count >= max_cnt)
    {
        goto fa_close_out;
    }

    rc = 0;

fa_close_out:
    flash_area_close(fa);
out:
    return rc;
}

 

If slot size is equal to image size, the while loop iterate until both conditions are false, but it is not an error if rem_len is 0 at the end of loop, it means that I have used al entry in sectors array, but no other  entry is required.

I think that the if condition should be changed in:

    if (rem_len > 0)
    {
        goto fa_close_out;
    }

 

what do you think about this?

best regards

Max

标记 (2)
0 项奖励
回复
1 回复

366 次查看
EdwinHz
NXP TechSupport
NXP TechSupport

Hi @mastupristi,

Thanks for reporting this issue.

I will pass it on to the SDK team so they can investigate it further and provide a change if they see it necessary.

BR,
Edwin.

0 项奖励
回复
%3CLINGO-SUB%20id%3D%22lingo-sub-2159996%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3Emcuboot%2F%E5%90%AF%E5%8A%A8%2Fnxp_mcux_sdk%2Fflashapi%2Fflashapi%2Fflash_api.c%20%E4%B8%AD%E5%8F%AF%E8%83%BD%E5%AD%98%E5%9C%A8%E9%94%99%E8%AF%AF%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2159996%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%E6%82%A8%E5%A5%BD%EF%BC%8C%3C%2FP%3E%3CP%3E%E6%88%91%E4%BD%BF%E7%94%A8%20RT1021%EF%BC%8C%E6%AD%A3%E5%9C%A8%E6%B5%8B%E8%AF%95%20mcuboot%E3%80%82%3C%2FP%3E%3CP%3E%E6%88%91%E5%B8%8C%E6%9C%9B%E5%9B%BE%E5%83%8F%E7%9A%84%E6%9C%80%E5%A4%A7%E5%AE%B9%E9%87%8F%E4%B8%BA%20512kB%EF%BC%8C%E5%9B%A0%E6%AD%A4%E5%9C%A8%20sblconfig.h%20%E4%B8%AD%E6%88%91%E7%9A%84%E5%AE%9A%E4%B9%89%E6%98%AF%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%20translate%3D%22no%22%3E%23define%20CONFIG_MCUBOOT_MAX_IMG_SECTORS%20128%20%2F*4kB%20sector%20*%20128%20%3D%20512kB%20*%2F%3C%2FCODE%3E%3C%2FPRE%3E%3CBR%20%2F%3E%3CP%3E%E5%92%8C%20flash_partitioning.h%20%E4%B8%AD%E6%88%91%E5%B7%B2%E7%BB%8F%E4%B8%8B%E4%BA%86%E5%AE%9A%E4%B9%89%EF%BC%9A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%20translate%3D%22no%22%3E%23define%20BOOT_FLASH_ACT_APP%20%20%20%20%20%20%20%20%20%20%20%20%20%200x60100000%0A%23define%20BOOT_FLASH_CAND_APP%20%20%20%20%20%20%20%20%20%20%20%20%200x60180000%3C%2FCODE%3E%3C%2FPRE%3E%3CBR%20%2F%3E%3CP%3E%E4%BD%86%E6%98%AF%EF%BC%8C%E6%88%91%E6%94%B6%E5%88%B0%E4%BA%86%E4%B8%80%E4%B8%AA%E9%94%99%E8%AF%AF%EF%BC%9A%3C%2FP%3E%3CPRE%20translate%3D%22no%22%3EFailed%20reading%20sectors%3B%20BOOT_MAX_IMG_SECTORS%3D128%20-%20too%20small%3F%3C%2FPRE%3E%3CP%3E%E6%88%91%E8%BF%9B%E8%A1%8C%E4%BA%86%E8%B0%83%E8%AF%95%EF%BC%8C%E6%89%BE%E5%88%B0%E4%BA%86%E6%88%91%E8%AE%A4%E4%B8%BA%E5%AD%98%E5%9C%A8%E9%94%99%E8%AF%AF%E7%9A%84%E5%9C%B0%E6%96%B9%E3%80%82%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fnxp-mcuxpresso%2Fmcuboot%2Fblob%2Fmcux_main%2Fboot%2Fnxp_mcux_sdk%2Fflashapi%2Fflash_api.c%23L378%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Fnxp-mcuxpresso%2Fmcuboot%2Fblob%2Fmcux_main%2Fboot%2Fnxp_mcux_sdk%2Fflashapi%2Fflash_api.c%23L378%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%20translate%3D%22no%22%3Eint%20flash_area_get_sectors(int%20fa_id%2C%20uint32_t%20*count%2C%20struct%20flash_sector%20*sectors)%0A%7B%0A%20%20%20%20const%20struct%20flash_area%20*fa%3B%0A%20%20%20%20uint32_t%20max_cnt%20%3D%20*count%3B%0A%20%20%20%20uint32_t%20rem_len%3B%0A%20%20%20%20int%20rc%20%3D%20-1%3B%0A%0A%20%20%20%20if%20(flash_area_open(fa_id%2C%20%26amp%3Bfa))%0A%20%20%20%20%20%20%20%20goto%20out%3B%0A%0A%20%20%20%20if%20(*count%20%26lt%3B%201)%0A%20%20%20%20%20%20%20%20goto%20fa_close_out%3B%0A%0A%20%20%20%20rem_len%20%3D%20fa-%26gt%3Bfa_size%3B%0A%20%20%20%20*count%20%20%3D%200%3B%0A%20%20%20%20while%20((rem_len%20%26gt%3B%200)%20%26amp%3B%26amp%3B%20(*count%20%26lt%3B%20max_cnt))%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(rem_len%20%26lt%3B%20MFLASH_SECTOR_SIZE)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20goto%20fa_close_out%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20sectors%5B*count%5D.fs_off%20%20%3D%20MFLASH_SECTOR_SIZE%20*%20(*count)%3B%0A%20%20%20%20%20%20%20%20sectors%5B*count%5D.fs_size%20%3D%20MFLASH_SECTOR_SIZE%3B%0A%20%20%20%20%20%20%20%20*count%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20*count%20%2B%201%3B%0A%20%20%20%20%20%20%20%20rem_len%20-%3D%20MFLASH_SECTOR_SIZE%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20if%20(*count%20%26gt%3B%3D%20max_cnt)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20goto%20fa_close_out%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20rc%20%3D%200%3B%0A%0Afa_close_out%3A%0A%20%20%20%20flash_area_close(fa)%3B%0Aout%3A%0A%20%20%20%20return%20rc%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CBR%20%2F%3E%3CP%3E%E5%A6%82%E6%9E%9C%E6%8F%92%E6%A7%BD%E5%A4%A7%E5%B0%8F%E7%AD%89%E4%BA%8E%E5%9B%BE%E5%83%8F%E5%A4%A7%E5%B0%8F%EF%BC%8Cwhile%20%E5%BE%AA%E7%8E%AF%E4%BC%9A%E4%B8%80%E7%9B%B4%E9%87%8D%E5%A4%8D%EF%BC%8C%E7%9B%B4%E5%88%B0%E4%B8%A4%E4%B8%AA%E6%9D%A1%E4%BB%B6%E9%83%BD%E4%B8%BA%E5%81%87%EF%BC%8C%E4%BD%86%E5%A6%82%E6%9E%9C%E5%BE%AA%E7%8E%AF%E7%BB%93%E6%9D%9F%E6%97%B6%20rem_len%20%E4%B8%BA%200%EF%BC%8C%E5%88%99%E4%B8%8D%E4%BC%9A%E5%87%BA%E9%94%99%EF%BC%8C%E8%BF%99%E6%84%8F%E5%91%B3%E7%9D%80%E6%88%91%E5%B7%B2%E7%BB%8F%E4%BD%BF%E7%94%A8%E4%BA%86%E6%89%87%E5%8C%BA%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E4%B8%80%E4%B8%AA%E6%9D%A1%E7%9B%AE%EF%BC%8C%E4%BD%86%E4%B8%8D%E9%9C%80%E8%A6%81%E5%85%B6%E4%BB%96%E6%9D%A1%E7%9B%AE%E3%80%82%3C%2FP%3E%3CP%3E%E6%88%91%E8%AE%A4%E4%B8%BA%E5%BA%94%E8%AF%A5%E4%BF%AE%E6%94%B9%20if%20%E6%9D%A1%E4%BB%B6%EF%BC%9A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%20translate%3D%22no%22%3E%20%20%20%20if%20(rem_len%20%26gt%3B%200)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20goto%20fa_close_out%3B%0A%20%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CBR%20%2F%3E%3CP%3E%E6%82%A8%E5%AF%B9%E6%AD%A4%E6%9C%89%E4%BD%95%E7%9C%8B%E6%B3%95%EF%BC%9F%3C%2FP%3E%3CP%3E%E9%A1%BA%E7%A5%9D%E5%95%86%E7%A5%BA%EF%BC%81%3C%2FP%3E%3CP%3E%E6%9C%80%E5%A4%A7%E5%80%BC%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2160180%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20possible%20bug%20in%20mcuboot%2Fboot%2Fnxp_mcux_sdk%2Fflashapi%2Fflash_api.c%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2160180%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%E5%97%A8%EF%BC%8C%3CA%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F124967%22%20target%3D%22_blank%22%3E%40mastupristi%3C%2FA%3E%E3%80%81%3C%2FP%3E%0A%3CP%3E%E6%84%9F%E8%B0%A2%E6%82%A8%E6%8A%A5%E5%91%8A%E8%BF%99%E4%B8%AA%E9%97%AE%E9%A2%98%E3%80%82%3C%2FP%3E%0A%3CP%3E%E6%88%91%E5%B0%86%E6%8A%8A%E8%BF%99%E4%B8%AA%E9%97%AE%E9%A2%98%E8%BD%AC%E7%BB%99%20SDK%20%E5%9B%A2%E9%98%9F%EF%BC%8C%E4%BB%A5%E4%BE%BF%E4%BB%96%E4%BB%AC%E8%BF%9B%E4%B8%80%E6%AD%A5%E8%B0%83%E6%9F%A5%EF%BC%8C%E5%B9%B6%E5%9C%A8%E8%AE%A4%E4%B8%BA%E6%9C%89%E5%BF%85%E8%A6%81%E6%97%B6%E6%8F%90%E4%BE%9B%E4%BF%AE%E6%94%B9%E3%80%82%3C%2FP%3E%0A%3CP%3EBR%2C%3CBR%20%2F%3EEdwin.%3C%2FP%3E%3C%2FLINGO-BODY%3E