OtaPalStatus_t otaPal_CloseFile()

in components/ota_pal/source/ota_pal.c [585:657]


OtaPalStatus_t otaPal_CloseFile(OtaFileContext_t *const pFileContext)
{
    OtaPalMainStatus_t mainErr = OtaPalSuccess;

    if (!_esp_ota_ctx_validate(pFileContext))
    {
        return OTA_PAL_COMBINE_ERR(OtaPalFileClose, 0);
    }

    if (pFileContext->pSignature == NULL)
    {
        LogError(("Image Signature not found"));
        _esp_ota_ctx_clear(&ota_ctx);
        mainErr = OtaPalSignatureCheckFailed;
    }
    else if (ota_ctx.data_write_len == 0)
    {
        LogError(("No data written to partition"));
        mainErr = OtaPalSignatureCheckFailed;
    }
    else
    {
        /* Verify the file signature, close the file and return the signature verification result. */
        mainErr = OTA_PAL_MAIN_ERR(otaPal_CheckFileSignature(pFileContext));

        if (mainErr != OtaPalSuccess)
        {
            esp_partition_erase_range(ota_ctx.update_partition, 0, ota_ctx.update_partition->size);
        }
        else
        {
             /* Create ota file. */
            prvCreateOtaFile( pFileContext );

            /* Apply the patch. */
            if( 0 != prvApplyPatch( ) )
            {
                 return OTA_PAL_COMBINE_ERR( OtaPalFileClose, 0 );
            }

            /* Write ASN1 decoded signature at the end of firmware image for bootloader to validate during bootup */
            esp_sec_boot_sig_t *sec_boot_sig = (esp_sec_boot_sig_t *)pvPortMalloc(sizeof(esp_sec_boot_sig_t));

            if (sec_boot_sig != NULL)
            {
                memset(sec_boot_sig->sec_ver, 0x00, sizeof(sec_boot_sig->sec_ver));
                memset(sec_boot_sig->pad, 0xFF, sizeof(sec_boot_sig->pad));
                mainErr = asn1_to_raw_ecdsa(pFileContext->pSignature->data, pFileContext->pSignature->size, sec_boot_sig->raw_ecdsa_sig);

                if (mainErr == OtaPalSuccess)
                {
                    esp_err_t ret = esp_ota_write_with_offset(ota_ctx.update_handle, sec_boot_sig, ECDSA_SIG_SIZE, ota_ctx.data_write_len);

                    if (ret != ESP_OK)
                    {
                        return OTA_PAL_COMBINE_ERR( OtaPalFileClose, 0) ;
                    }

                    ota_ctx.data_write_len += ECDSA_SIG_SIZE;
                }

                free(sec_boot_sig);
                ota_ctx.valid_image = true;
            }
            else
            {
                mainErr = OtaPalFileClose;
            }
        }
    }

    return OTA_PAL_COMBINE_ERR(mainErr, 0);
}