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

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

404 Views
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

Tags (2)
0 Kudos
Reply
1 Reply

385 Views
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 Kudos
Reply
%3CLINGO-SUB%20id%3D%22lingo-sub-2159996%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3Epossible%20bug%20in%20mcuboot%2Fboot%2Fnxp_mcux_sdk%2Fflashapi%2Fflash_api.c%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2159996%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EI%20use%20RT1021%20and%20I'm%20testing%20mcuboot.%3C%2FP%3E%3CP%3EI%20want%20image%20to%20be%20at%20most%20512kB%2C%20so%20in%20sblconfig.h%20I%20define%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%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%3Eand%20in%26nbsp%3Bflash_partitioning.h%20I%20have%20defined%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%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%3EHowever%2C%20I%20get%20an%20error%3A%3C%2FP%3E%3CPRE%3EFailed%20reading%20sectors%3B%20BOOT_MAX_IMG_SECTORS%3D128%20-%20too%20small%3F%3C%2FPRE%3E%3CP%3EI%20debugged%20and%20found%20the%20point%20where%20I%20think%20there%20is%20a%20bug.%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%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%3EIf%20slot%20size%20is%20equal%20to%20image%20size%2C%20the%20while%20loop%20iterate%20until%20both%20conditions%20are%20false%2C%20but%20it%20is%20not%20an%20error%20if%20rem_len%20is%200%20at%20the%20end%20of%20loop%2C%20it%20means%20that%20I%20have%20used%20al%20entry%20in%20sectors%20array%2C%20but%20no%20other%26nbsp%3B%20entry%20is%20required.%3C%2FP%3E%3CP%3EI%20think%20that%20the%20if%20condition%20should%20be%20changed%20in%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%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%3Ewhat%20do%20you%20think%20about%20this%3F%3C%2FP%3E%3CP%3Ebest%20regards%3C%2FP%3E%3CP%3EMax%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%3EHi%26nbsp%3B%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%2C%3C%2FP%3E%0A%3CP%3EThanks%20for%20reporting%20this%20issue.%3C%2FP%3E%0A%3CP%3EI%20will%20pass%20it%20on%20to%20the%20SDK%20team%20so%20they%20can%20investigate%20it%20further%20and%20provide%20a%20change%20if%20they%20see%20it%20necessary.%3C%2FP%3E%0A%3CP%3EBR%2C%3CBR%20%2F%3EEdwin.%3C%2FP%3E%3C%2FLINGO-BODY%3E