blob: 8206a4870dc8c8811cd53e27a278c450d6323380 [file] [log] [blame]
/*
* Copyright (c) 2018 The Fuchsia Authors
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef _ZIRCON_UBOOT_ZIRCON_H_
#define _ZIRCON_UBOOT_ZIRCON_H_
#include <abr/abr.h>
#include <abr/ops.h>
#include <zircon/boot/driver-config.h>
#include <zircon/boot/image.h>
#include <zircon_uboot/gpt.h>
#if defined(DEV_BUILD_CONFIG)
#define BOOTLOADER_BUILD_VARIANT "dev"
#elif defined(DOGFOOD_BUILD_CONFIG)
#define BOOTLOADER_BUILD_VARIANT "dogfood"
#elif defined(PROD_BUILD_CONFIG)
#define BOOTLOADER_BUILD_VARIANT "prod"
#else
#error Unknown bootloader build variant
#endif
/**
* Modifies the ZBI to add bootloader-generated items.
*
* This should be called exactly once after the prebuilt ZBI has been loaded
* into memory, before booting it.
*
* @zbi: ZBI container.
* @capacity: Total available space the container can expand to.
* @slot: Zircon slot we're booting into.
*
* Returns: 0 on success, nonzero on failure.
*/
int zircon_fixup_zbi(zbi_header_t *zbi, size_t capacity, AbrSlotIndex slot);
/**
* Modifies the ZBI to add bootloader-generated items.
*
* This should be called exactly once after the prebuilt ZBI has been loaded
* into memory, before booting it.
*
* @zbi: ZBI container.
* @capacity: Total available space the container can expand to.
*
* Returns: 0 on success, nonzero on failure.
*/
int zircon_fixup_zbi_no_slot(zbi_header_t *zbi, size_t capacity);
/**
* Adds a ZBI item to the given container.
*
* This is a thin wrapper around libzbi's zbi_create_entry_with_payload(),
* which adds:
* - additional length checks to prevent uint32_t overflow
* - automatic logging if appending the item fails
*
* @zbi: ZBI container.
* @capacity: ZBI container capacity.
* @type: ZBI item type to add.
* @extra: ZBI item extra value.
* @payload: ZBI item payload.
* @size: ZBI item payload size.
*
* Return: 0 on success, nonzero on error.
*/
int append_zbi_item_or_log(void *zbi, size_t capacity, uint32_t type,
uint32_t extra, const void *payload, size_t size);
/**
* Maps slot index to partition name.
* Returns empty string for invalid slot_index.
*/
const char *zircon_slot_idx_to_part_name(AbrSlotIndex slot_index);
/* AbrOps implementation. */
const AbrOps *zircon_abr_ops(void);
/**
* Name of partition containing A/B/R config data - normally "misc" or
* "durable_boot".
*/
extern const char *zircon_abr_partition_name;
/**
* Loads zircon image using A/B/R scheme.
*
* @loadaddr: Address at which to load the zircon image.
* @loadsize: Size of the load buffer.
* @force_recovery: trigger a force boot into recovery (R slot).
* @img_offset: Set to entry point offset. Zero if unknown.
* @slot: On success, will be set to the slot that was loaded.
*
* Return: 0 if successful, negative value on failure.
*/
int zircon_abr_img_load(unsigned char *loadaddr, size_t loadsize,
bool force_recovery, size_t *img_offset,
AbrSlotIndex *slot);
/**
* Stages a ZBI_TYPE_BOOTLOADER_FILE item.
*
* This functionality is used by fastboot before the ZBI is loaded into
* memory, so we can't append it to the prebuilt ZBI directly. Instead we
* stage it in a buffer and it will be appended by zircon_fixup_zbi().
*
* @name: file name.
* @data: file contents.
* @data_len: contents length.
*
* Returns: 0 on success, nonzero on failure.
*/
int zircon_stage_zbi_file(const char *name, const uint8_t *data,
size_t data_len);
#endif // _ZIRCON_UBOOT_ZIRCON_H_