1 /*
2  * Copyright (c) 2020-2023, Intel Corporation. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef SOCFPGA_VAB_H
8 #define SOCFPGA_VAB_H
9 
10 
11 #include <stdlib.h>
12 #include "socfpga_fcs.h"
13 
14 struct fcs_hps_vab_certificate_data {
15 	uint32_t vab_cert_magic_num;			/* offset 0x10 */
16 	uint32_t flags;
17 	uint8_t rsvd0_1[8];
18 	uint8_t fcs_sha384[FCS_SHA384_WORD_SIZE];	/* offset 0x20 */
19 };
20 
21 struct fcs_hps_vab_certificate_header {
22 	uint32_t cert_magic_num;			/* offset 0 */
23 	uint32_t cert_data_sz;
24 	uint32_t cert_ver;
25 	uint32_t cert_type;
26 	struct fcs_hps_vab_certificate_data d;		/* offset 0x10 */
27 	/* keychain starts at offset 0x50 */
28 };
29 
30 /* Macros */
31 #define IS_BYTE_ALIGNED(x, a)		(((x) & ((typeof(x))(a) - 1)) == 0)
32 #define BYTE_ALIGN(x, a)		__ALIGN_MASK((x), (typeof(x))(a)-1)
33 #define __ALIGN_MASK(x, mask)		(((x)+(mask))&~(mask))
34 #define VAB_CERT_HEADER_SIZE		sizeof(struct fcs_hps_vab_certificate_header)
35 #define VAB_CERT_MAGIC_OFFSET		offsetof(struct fcs_hps_vab_certificate_header, d)
36 #define VAB_CERT_FIT_SHA384_OFFSET	offsetof(struct fcs_hps_vab_certificate_data, fcs_sha384[0])
37 #define SDM_CERT_MAGIC_NUM		0x25D04E7F
38 #define CHUNKSZ_PER_WD_RESET		(256 * 1024)
39 
40 /* SHA related return Macro */
41 #define ENOVABIMG		1 /* VAB certificate not available */
42 #define EIMGERR		2 /* Image format/size not valid */
43 #define ETIMEOUT		3 /* Execution timeout */
44 #define EPROCESS		4 /* Process error */
45 #define EKEYREJECTED		5/* Key was rejected by service */
46 
47 /* Function Definitions */
48 static size_t get_img_size(uint8_t *img_buf, size_t img_buf_sz);
49 int socfpga_vendor_authentication(void **p_image, size_t *p_size);
50 static uint32_t get_unaligned_le32(const void *p);
51 void sha384_csum_wd(const unsigned char *input, unsigned int ilen,
52 unsigned char *output, unsigned int chunk_sz);
53 
54 #endif
55