/*
 * This file is auto-generated, do not modify!
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include <glib.h>

#include "fu-amd-kria-persistent-struct.h"
#include "fu-byte-array.h"
#include "fu-bytes.h"
#include "fu-mem-private.h"
#include "fu-string.h"

#ifdef G_LOG_DOMAIN
  #undef G_LOG_DOMAIN
#endif
#define G_LOG_DOMAIN "FuStruct"

/**
 * fu_struct_amd_kria_persist_reg_ref: (skip):
 **/
FuStructAmdKriaPersistReg *
fu_struct_amd_kria_persist_reg_ref(FuStructAmdKriaPersistReg *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_amd_kria_persist_reg_unref: (skip):
 **/
void
fu_struct_amd_kria_persist_reg_unref(FuStructAmdKriaPersistReg *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructAmdKriaPersistReg refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_amd_kria_persist_reg_new_internal: (skip):
 **/
static FuStructAmdKriaPersistReg *
fu_struct_amd_kria_persist_reg_new_internal(void)
{
    FuStructAmdKriaPersistReg *st = g_new0(FuStructAmdKriaPersistReg, 1);
    st->refcount = 1;
    return st;
}

/* getters */
/**
 * fu_struct_amd_kria_persist_reg_get_id_sig: (skip):
 **/
static gchar *
fu_struct_amd_kria_persist_reg_get_id_sig(const FuStructAmdKriaPersistReg *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    return fu_memstrsafe(st->buf->data, st->buf->len, 0, 4, NULL);
}
/**
 * fu_struct_amd_kria_persist_reg_get_ver: (skip):
 **/
guint32
fu_struct_amd_kria_persist_reg_get_ver(const FuStructAmdKriaPersistReg *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 4, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_amd_kria_persist_reg_get_len: (skip):
 **/
guint32
fu_struct_amd_kria_persist_reg_get_len(const FuStructAmdKriaPersistReg *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 8, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_amd_kria_persist_reg_get_checksum: (skip):
 **/
guint32
fu_struct_amd_kria_persist_reg_get_checksum(const FuStructAmdKriaPersistReg *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 12, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_amd_kria_persist_reg_get_last_booted_img: (skip):
 **/
guint8
fu_struct_amd_kria_persist_reg_get_last_booted_img(const FuStructAmdKriaPersistReg *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[16];
}
/**
 * fu_struct_amd_kria_persist_reg_get_requested_booted_img: (skip):
 **/
guint8
fu_struct_amd_kria_persist_reg_get_requested_booted_img(const FuStructAmdKriaPersistReg *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[17];
}
/**
 * fu_struct_amd_kria_persist_reg_get_img_b_bootable: (skip):
 **/
guint8
fu_struct_amd_kria_persist_reg_get_img_b_bootable(const FuStructAmdKriaPersistReg *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[18];
}
/**
 * fu_struct_amd_kria_persist_reg_get_img_a_bootable: (skip):
 **/
guint8
fu_struct_amd_kria_persist_reg_get_img_a_bootable(const FuStructAmdKriaPersistReg *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[19];
}
/**
 * fu_struct_amd_kria_persist_reg_get_img_a_offset: (skip):
 **/
guint32
fu_struct_amd_kria_persist_reg_get_img_a_offset(const FuStructAmdKriaPersistReg *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 20, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_amd_kria_persist_reg_get_img_b_offset: (skip):
 **/
guint32
fu_struct_amd_kria_persist_reg_get_img_b_offset(const FuStructAmdKriaPersistReg *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 24, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_amd_kria_persist_reg_get_recovery_offset: (skip):
 **/
guint32
fu_struct_amd_kria_persist_reg_get_recovery_offset(const FuStructAmdKriaPersistReg *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 28, G_LITTLE_ENDIAN);
}

/* setters */
/**
 * fu_struct_amd_kria_persist_reg_to_string: (skip):
 **/
static gchar *
fu_struct_amd_kria_persist_reg_to_string(const FuStructAmdKriaPersistReg *st)
{
    g_autoptr(GString) str = g_string_new("FuStructAmdKriaPersistReg:\n");
    g_return_val_if_fail(st != NULL, NULL);
    {
        g_autofree gchar *tmp = fu_struct_amd_kria_persist_reg_get_id_sig(st);
        if (tmp != NULL)
            g_string_append_printf(str, "  id_sig: %s\n", tmp);
    }
    g_string_append_printf(str, "  ver: 0x%x\n",
                           (guint) fu_struct_amd_kria_persist_reg_get_ver(st));
    g_string_append_printf(str, "  len: 0x%x\n",
                           (guint) fu_struct_amd_kria_persist_reg_get_len(st));
    g_string_append_printf(str, "  checksum: 0x%x\n",
                           (guint) fu_struct_amd_kria_persist_reg_get_checksum(st));
    g_string_append_printf(str, "  last_booted_img: 0x%x\n",
                           (guint) fu_struct_amd_kria_persist_reg_get_last_booted_img(st));
    g_string_append_printf(str, "  requested_booted_img: 0x%x\n",
                           (guint) fu_struct_amd_kria_persist_reg_get_requested_booted_img(st));
    g_string_append_printf(str, "  img_b_bootable: 0x%x\n",
                           (guint) fu_struct_amd_kria_persist_reg_get_img_b_bootable(st));
    g_string_append_printf(str, "  img_a_bootable: 0x%x\n",
                           (guint) fu_struct_amd_kria_persist_reg_get_img_a_bootable(st));
    g_string_append_printf(str, "  img_a_offset: 0x%x\n",
                           (guint) fu_struct_amd_kria_persist_reg_get_img_a_offset(st));
    g_string_append_printf(str, "  img_b_offset: 0x%x\n",
                           (guint) fu_struct_amd_kria_persist_reg_get_img_b_offset(st));
    g_string_append_printf(str, "  recovery_offset: 0x%x\n",
                           (guint) fu_struct_amd_kria_persist_reg_get_recovery_offset(st));
    if (str->len > 0)
        g_string_set_size(str, str->len - 1);
    return g_string_free(g_steal_pointer(&str), FALSE);
}
static gboolean
fu_struct_amd_kria_persist_reg_validate_internal(FuStructAmdKriaPersistReg *st, GError **error)
{
    g_return_val_if_fail(st != NULL, FALSE);
    if (strncmp((const gchar *) (st->buf->data + 0), "ABUM", 4) != 0) {
        g_autofree gchar *str = fu_struct_amd_kria_persist_reg_get_id_sig(st);
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "constant FuStructAmdKriaPersistReg.id_sig was not valid, "
                    "expected 'ABUM' and got '%s'",
                    str);
        return FALSE;
    }
    return TRUE;
}
static gboolean
fu_struct_amd_kria_persist_reg_parse_internal(FuStructAmdKriaPersistReg *st, GError **error)
{
    if (g_log_get_debug_enabled()) {
        g_autofree gchar *str = fu_struct_amd_kria_persist_reg_to_string(st);
        g_debug("%s", str);
    }
    if (!fu_struct_amd_kria_persist_reg_validate_internal(st, error))
        return FALSE;
    return TRUE;
}
/**
 * fu_struct_amd_kria_persist_reg_parse_stream: (skip):
 **/
FuStructAmdKriaPersistReg *
fu_struct_amd_kria_persist_reg_parse_stream(GInputStream *stream, gsize offset, GError **error)
{
    g_autoptr(FuStructAmdKriaPersistReg) st = fu_struct_amd_kria_persist_reg_new_internal();
    st->buf = fu_input_stream_read_byte_array(stream, offset, 32, NULL, error);
    if (st->buf == NULL) {
        g_prefix_error(error, "FuStructAmdKriaPersistReg failed read of 0x%x: ", (guint) 32);
        return NULL;
    }
    if (st->buf->len != 32) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "FuStructAmdKriaPersistReg requested 0x%x and got 0x%x",
                    (guint) 32,
                    (guint) st->buf->len);
        return NULL;
    }
    if (!fu_struct_amd_kria_persist_reg_parse_internal(st, error))
        return NULL;
    return g_steal_pointer(&st);
}
