/*!
 * \brief Set Sony ELF boot image output format
 *
 * \param biw MbBiWriter
 *
 * \return
 *   * #MB_BI_OK if the format is successfully enabled
 *   * #MB_BI_WARN if the format is already enabled
 *   * \<= #MB_BI_FAILED if an error occurs
 */
int mb_bi_writer_set_format_sony_elf(MbBiWriter *biw)
{
    SonyElfWriterCtx *const ctx = static_cast<SonyElfWriterCtx *>(
            calloc(1, sizeof(SonyElfWriterCtx)));
    if (!ctx) {
        mb_bi_writer_set_error(biw, -errno,
                               "Failed to allocate SonyElfWriterCtx: %s",
                               strerror(errno));
        return MB_BI_FAILED;
    }

    _segment_writer_init(&ctx->segctx);

    return _mb_bi_writer_register_format(biw,
                                         ctx,
                                         MB_BI_FORMAT_SONY_ELF,
                                         MB_BI_FORMAT_NAME_SONY_ELF,
                                         nullptr,
                                         &sony_elf_writer_get_header,
                                         &sony_elf_writer_write_header,
                                         &sony_elf_writer_get_entry,
                                         &sony_elf_writer_write_entry,
                                         &sony_elf_writer_write_data,
                                         &sony_elf_writer_finish_entry,
                                         &sony_elf_writer_close,
                                         &sony_elf_writer_free);
}
MB_BEGIN_C_DECLS

/*!
 * \brief Set Bump boot image output format
 *
 * \param biw MbBiWriter
 *
 * \return
 *   * #MB_BI_OK if the format is successfully enabled
 *   * #MB_BI_WARN if the format is already enabled
 *   * \<= #MB_BI_FAILED if an error occurs
 */
int mb_bi_writer_set_format_bump(MbBiWriter *biw)
{
    AndroidWriterCtx *const ctx = static_cast<AndroidWriterCtx *>(
                                      calloc(1, sizeof(AndroidWriterCtx)));
    if (!ctx) {
        mb_bi_writer_set_error(biw, -errno,
                               "Failed to allocate AndroidWriterCtx: %s",
                               strerror(errno));
        return MB_BI_FAILED;
    }

    if (!SHA1_Init(&ctx->sha_ctx)) {
        mb_bi_writer_set_error(biw, MB_BI_ERROR_INTERNAL_ERROR,
                               "Failed to initialize SHA_CTX");
        free(ctx);
        return false;
    }

    _segment_writer_init(&ctx->segctx);

    ctx->is_bump = true;

    return _mb_bi_writer_register_format(biw,
                                         ctx,
                                         MB_BI_FORMAT_BUMP,
                                         MB_BI_FORMAT_NAME_BUMP,
                                         nullptr,
                                         &android_writer_get_header,
                                         &android_writer_write_header,
                                         &android_writer_get_entry,
                                         &android_writer_write_entry,
                                         &android_writer_write_data,
                                         &android_writer_finish_entry,
                                         &android_writer_close,
                                         &android_writer_free);
}