OtaPalStatus_t xOtaPalSetPlatformImageState()

in source/ota_pal.c [132:253]


OtaPalStatus_t xOtaPalSetPlatformImageState( OtaFileContext_t * const pFileContext,
                                             OtaImageState_t eState )
{
    OtaPalStatus_t result = OtaPalSuccess;
    struct boot_ucb ucb;

    PRINTF( "[OTA-NXP] SetPlatformImageState %d\r\n", eState );

    boot_ucb_read( &ucb );

    switch( eState )
    {
        case OtaImageStateAccepted:

            if( ucb.state == BOOT_STATE_PENDING_COMMIT )
            {
                ucb.state = BOOT_STATE_VOID;

                if( 0 != boot_ucb_write( &ucb ) )
                {
                    PRINTF( "[OTA-NXP] FLASH operation failed during commit\r\n" );
                    result = OTA_PAL_COMBINE_ERR( OtaPalCommitFailed, 0 );
                }

                boot_wdtdis(); /* disable watchdog */

                if( 0 != boot_overwrite_rollback() )
                {
                    /* rollback image may be partially overwritten - do not return error as that would initiate a rollback */
                    PRINTF( "[OTA-NXP] FLASH operation failed during overwrite\r\n" );
                    ucb.rollback_img = NULL;

                    if( 0 != boot_ucb_write( &ucb ) )
                    {
                        PRINTF( "[OTA-NXP] FLASH operation failed during commit\r\n" );
                    }
                    else
                    {
                        PRINTF( "[OTA-NXP] rollback disabled\r\n" );
                    }
                }
            }
            else
            {
                PRINTF( "[OTA-NXP] Image is not in pending commit state\r\n" );
                result = OTA_PAL_COMBINE_ERR( OtaPalCommitFailed, 0 );
            }

            break;

        case OtaImageStateRejected:

            if( ucb.state == BOOT_STATE_PENDING_COMMIT )
            {
                if( ucb.rollback_img == NULL )
                {
                    PRINTF( "[OTA-NXP] Attempt to reject image without possibility for rollback\r\n" );
                    result = OTA_PAL_COMBINE_ERR( OtaPalRejectFailed, 0 );
                }

                ucb.state = BOOT_STATE_INVALID;

                if( 0 != boot_ucb_write( &ucb ) )
                {
                    PRINTF( "[OTA-NXP] FLASH operation failed during reject\r\n" );
                    result = OTA_PAL_COMBINE_ERR( OtaPalRejectFailed, 0 );
                }
            }
            else if( ucb.state == BOOT_STATE_NEW )
            {
                ucb.state = BOOT_STATE_VOID;

                if( 0 != boot_ucb_write( &ucb ) )
                {
                    PRINTF( "[OTA-NXP] FLASH operation failed during reject\r\n" );
                    result = OTA_PAL_COMBINE_ERR( OtaPalRejectFailed, 0 );
                }
            }

            break;

        case OtaImageStateAborted:

            if( ucb.state == BOOT_STATE_PENDING_COMMIT )
            {
                if( ucb.rollback_img == NULL )
                {
                    PRINTF( "[OTA-NXP] Attempt to abort without possibility for rollback\r\n" );
                    result = OTA_PAL_COMBINE_ERR( OtaPalAbortFailed, 0 );
                }

                ucb.state = BOOT_STATE_INVALID;

                if( 0 != boot_ucb_write( &ucb ) )
                {
                    PRINTF( "[OTA-NXP] FLASH operation failed during abort\r\n" );
                    result = OTA_PAL_COMBINE_ERR( OtaPalAbortFailed, 0 );
                }
            }
            else if( ucb.state == BOOT_STATE_NEW )
            {
                ucb.state = BOOT_STATE_VOID;

                if( 0 != boot_ucb_write( &ucb ) )
                {
                    PRINTF( "[OTA-NXP] FLASH operation failed during abort\r\n" );
                    result = OTA_PAL_COMBINE_ERR( OtaPalAbortFailed, 0 );
                }
            }

            break;

        case OtaImageStateTesting:
            break;

        default:
            result = OTA_PAL_COMBINE_ERR( OtaPalBadImageState, 0 );
            break;
    }

    return result;
}