blob: bfa339008e1d6066ac8bd4780b2b4b8cb35159d8 [file] [log] [blame]
Simon Glassfdacd502025-01-15 18:27:03 -07001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Verified Boot for Embedded (VBE) common functions
4 *
5 * Copyright 2024 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#ifndef __VBE_COMMON_H
10#define __VBE_COMMON_H
11
12#include <linux/types.h>
13
14struct udevice;
15
16enum {
17 MAX_VERSION_LEN = 256,
18
19 NVD_HDR_VER_SHIFT = 0,
20 NVD_HDR_VER_MASK = 0xf,
21 NVD_HDR_SIZE_SHIFT = 4,
22 NVD_HDR_SIZE_MASK = 0xf << NVD_HDR_SIZE_SHIFT,
23
24 /* Firmware key-version is in the top 16 bits of fw_ver */
25 FWVER_KEY_SHIFT = 16,
26 FWVER_FW_MASK = 0xffff,
27
28 NVD_HDR_VER_CUR = 1, /* current version */
29};
30
31/**
32 * struct vbe_nvdata - basic storage format for non-volatile data
33 *
34 * This is used for all VBE methods
35 *
36 * @crc8: crc8 for the entire record except @crc8 field itself
37 * @hdr: header size and version (NVD_HDR_...)
38 * @spare1: unused, must be 0
39 * @fw_vernum: version and key version (FWVER_...)
40 * @flags: Flags controlling operation (enum vbe_flags)
41 */
42struct vbe_nvdata {
43 u8 crc8;
44 u8 hdr;
45 u16 spare1;
46 u32 fw_vernum;
47 u32 flags;
48 u8 spare2[0x34];
49};
50
Simon Glass15d320d2025-01-15 18:27:07 -070051/**
52 * vbe_get_blk() - Obtain the block device to use for VBE
53 *
54 * Decodes the string to produce a block device
55 *
56 * @storage: String indicating the device to use, e.g. "mmc1"
57 * @blkp: Returns associated block device, on success
58 * Return 0 if OK, -ENODEV if @storage does not end with a number, -E2BIG if
59 * the device name is more than 15 characters, -ENXIO if the block device could
60 * not be found
61 */
62int vbe_get_blk(const char *storage, struct udevice **blkp);
63
Simon Glass9846e5f2025-01-15 18:27:08 -070064/**
65 * vbe_read_version() - Read version-string from a block device
66 *
67 * Reads the VBE version-string from a device. This function reads a single
68 * block from the device, so the string cannot be larger than that. It uses a
69 * temporary buffer for the read, then copies in up to @size bytes
70 *
71 * @blk: Device to read from
72 * @offset: Offset to read, in bytes
73 * @version: Place to put the string
74 * @max_size: Maximum size of @version
75 * Return: 0 if OK, -E2BIG if @max_size > block size, -EBADF if the offset is
76 * not block-aligned, -EIO if an I/O error occurred
77 */
78int vbe_read_version(struct udevice *blk, ulong offset, char *version,
79 int max_size);
80
Simon Glass72424b82025-01-15 18:27:09 -070081/**
82 * vbe_read_nvdata() - Read non-volatile data from a block device
83 *
84 * Reads the VBE nvdata from a device. This function reads a single block from
85 * the device, so the nvdata cannot be larger than that.
86 *
87 * @blk: Device to read from
88 * @offset: Offset to read, in bytes
89 * @size: Number of bytes to read
90 * @buf: Buffer to hold the data
91 * Return: 0 if OK, -E2BIG if @size > block size, -EBADF if the offset is not
92 * block-aligned, -EIO if an I/O error occurred, -EPERM if the header version is
93 * incorrect, the header size is invalid or the data fails its CRC check
94 */
95int vbe_read_nvdata(struct udevice *blk, ulong offset, ulong size, u8 *buf);
96
Simon Glass988f16a2025-01-15 18:27:10 -070097/**
98 * vbe_read_fit() - Read an image from a FIT
99 *
100 * This handles most of the VBE logic for reading from a FIT. It reads the FIT
101 * metadata, decides which image to load and loads it to a suitable address,
102 * ready for jumping to the next phase of VBE.
103 *
104 * This supports transition from VPL to SPL as well as SPL to U-Boot proper. For
105 * now, TPL->VPL is not supported.
106 *
107 * Both embedded and external data are supported for the FIT
108 *
109 * @blk: Block device containing FIT
110 * @area_offset: Byte offset of the VBE area in @blk containing the FIT
111 * @area_size: Size of the VBE area
112 * @load_addrp: If non-null, returns the address where the image was loaded
113 * @lenp: If non-null, returns the size of the image loaded, in bytes
114 * @namep: If non-null, returns the name of the FIT-image node that was loaded
115 * (allocated by this function)
116 * Return: 0 if OK, -EINVAL if the area does not contain an FDT (the underlying
117 * format for FIT), -E2BIG if the FIT extends past @area_size, -ENOMEM if there
118 * was not space to allocate the image-node name, other error if a read error
119 * occurred (see blk_read()), or something went wrong with the actually
120 * FIT-parsing (see fit_image_load()).
121 */
122int vbe_read_fit(struct udevice *blk, ulong area_offset, ulong area_size,
123 ulong *load_addrp, ulong *lenp, char **namep);
124
Simon Glassfdacd502025-01-15 18:27:03 -0700125#endif /* __VBE_ABREC_H */