1/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * (C) Copyright 2015 Google,  Inc
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
7#ifndef _RKCOMMON_H
8#define _RKCOMMON_H
9
10enum {
11	RK_BLK_SIZE		= 512,
12	RK_SIZE_ALIGN		= 2048,
13	RK_INIT_OFFSET		= 4,
14	RK_MAX_BOOT_SIZE	= 512 << 10,
15	RK_SPL_HDR_START	= RK_INIT_OFFSET * RK_BLK_SIZE,
16	RK_SPL_HDR_SIZE		= 4,
17};
18
19/**
20 * rkcommon_check_params() - check params
21 *
22 * Return: 0 if OK, -1 if ERROR.
23 */
24int rkcommon_check_params(struct image_tool_params *params);
25
26/**
27 * rkcommon_get_spl_hdr() - get 4-bytes spl hdr for a Rockchip boot image
28 *
29 * Rockchip's bootrom requires the spl loader to start with a 4-bytes
30 * header. The content of this header depends on the chip type.
31 */
32const char *rkcommon_get_spl_hdr(struct image_tool_params *params);
33
34/**
35 * rkcommon_get_spl_size() - get spl size for a Rockchip boot image
36 *
37 * Different chip may have different sram size. And if we want to jump
38 * back to the bootrom after spl, we may need to reserve some sram space
39 * for the bootrom.
40 * The spl loader size should be sram size minus reserved size(if needed)
41 */
42int rkcommon_get_spl_size(struct image_tool_params *params);
43
44/**
45 * rkcommon_set_header() - set up the header for a Rockchip boot image
46 *
47 * This sets up a 2KB header which can be interpreted by the Rockchip boot ROM.
48 *
49 * @buf:	Pointer to header place (must be at least 2KB in size)
50 */
51void rkcommon_set_header(void *buf,  struct stat *sbuf,  int ifd,
52			 struct image_tool_params *params);
53
54/**
55 * rkcommon_verify_header() - verify the header for a Rockchip boot image
56 *
57 * @buf:	Pointer to the image file
58 * @file_size:	Size of entire bootable image file (incl. all padding)
59 * Return: 0 if OK
60 */
61int rkcommon_verify_header(unsigned char *buf, int size,
62			   struct image_tool_params *params);
63
64/**
65 * rkcommon_print_header() - print the header for a Rockchip boot image
66 *
67 * This prints the header, spl_name and whether this is a SD/MMC or SPI image.
68 *
69 * @buf:	Pointer to the image (can be a read-only file-mapping)
70 */
71void rkcommon_print_header(const void *buf, struct image_tool_params *params);
72
73/**
74 * rkcommon_need_rc4_spl() - check if rc4 encoded spl is required
75 *
76 * Some socs cannot disable the rc4-encryption of the spl binary.
77 * rc4 encryption is disabled normally except on socs that cannot
78 * handle unencrypted binaries.
79 * Return: true or false depending on rc4 being required.
80 */
81bool rkcommon_need_rc4_spl(struct image_tool_params *params);
82
83/**
84 * rkcommon_rc4_encode_spl() - encode the spl binary
85 *
86 * Encrypts the SPL binary using the generic rc4 key as required
87 * by some socs.
88 *
89 * @buf:	Pointer to the SPL data (header and SPL binary)
90 * @offset:	offset inside buf to start at
91 * @size:	number of bytes to encode
92 */
93void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size);
94
95/**
96 * rkcommon_vrec_header() - allocate memory for the header
97 *
98 * @params:     Pointer to the tool params structure
99 * @tparams:    Pointer tot the image type structure (for setting
100 *              the header and header_size)
101 *
102 * Return: 0 (always)
103 */
104int rkcommon_vrec_header(struct image_tool_params *params,
105			 struct image_type_params *tparams);
106
107#endif
108