xref: /openbmc/u-boot/tools/rkspi.c (revision fe9ee579)
1 /*
2  * (C) Copyright 2015 Google,  Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  *
7  * See README.rockchip for details of the rkspi format
8  */
9 
10 #include "imagetool.h"
11 #include <image.h>
12 #include <rc4.h>
13 #include "mkimage.h"
14 #include "rkcommon.h"
15 
16 enum {
17 	RKSPI_SECT_LEN		= RK_BLK_SIZE * 4,
18 };
19 
20 static int rkspi_verify_header(unsigned char *buf, int size,
21 			       struct image_tool_params *params)
22 {
23 	return 0;
24 }
25 
26 static void rkspi_print_header(const void *buf)
27 {
28 }
29 
30 static void rkspi_set_header(void *buf, struct stat *sbuf, int ifd,
31 			     struct image_tool_params *params)
32 {
33 	int sector;
34 	unsigned int size;
35 	int ret;
36 
37 	size = params->orig_file_size;
38 	ret = rkcommon_set_header(buf, size, params);
39 	debug("size %x\n", size);
40 	if (ret) {
41 		/* TODO(sjg@chromium.org): This method should return an error */
42 		printf("Warning: SPL image is too large (size %#x) and will "
43 		       "not boot\n", size);
44 	}
45 
46 	/*
47 	 * Spread the image out so we only use the first 2KB of each 4KB
48 	 * region. This is a feature of the SPI format required by the Rockchip
49 	 * boot ROM. Its rationale is unknown.
50 	 */
51 	for (sector = size / RKSPI_SECT_LEN - 1; sector >= 0; sector--) {
52 		debug("sector %u\n", sector);
53 		memmove(buf + sector * RKSPI_SECT_LEN * 2,
54 			buf + sector * RKSPI_SECT_LEN,
55 			RKSPI_SECT_LEN);
56 		memset(buf + sector * RKSPI_SECT_LEN * 2 + RKSPI_SECT_LEN,
57 		       '\0', RKSPI_SECT_LEN);
58 	}
59 }
60 
61 static int rkspi_extract_subimage(void *buf, struct image_tool_params *params)
62 {
63 	return 0;
64 }
65 
66 static int rkspi_check_image_type(uint8_t type)
67 {
68 	if (type == IH_TYPE_RKSPI)
69 		return EXIT_SUCCESS;
70 	else
71 		return EXIT_FAILURE;
72 }
73 
74 /*
75  * The SPI payload needs to be padded out to make space for odd half-sector
76  * layout used in flash (i.e. only the first 2K of each 4K sector is used).
77  */
78 static int rkspi_vrec_header(struct image_tool_params *params,
79 			     struct image_type_params *tparams)
80 {
81 	int padding = rkcommon_vrec_header(params, tparams, 2048);
82 	/*
83 	 * The file size has not been adjusted at this point (our caller will
84 	 * eventually add the header/padding to the file_size), so we need to
85 	 * add up the header_size, file_size and padding ourselves.
86 	 */
87 	int padded_size = tparams->header_size + params->file_size + padding;
88 
89 	/*
90 	 * We need to store the original file-size (i.e. before padding), as
91 	 * imagetool does not set this during its adjustment of file_size.
92 	 */
93 	params->orig_file_size = padded_size;
94 
95 	/*
96 	 * Converting to the SPI format (i.e. splitting each 4K page into two
97 	 * 2K subpages and then padding these 2K pages up to take a complete
98 	 * 4K sector again) will will double the image size.
99 	 *
100 	 * Thus we return the padded_size as an additional padding requirement
101 	 * (be sure to add this to the padding returned from the common code).
102 	 */
103 	return padded_size + padding;
104 }
105 
106 /*
107  * rk_spi parameters
108  */
109 U_BOOT_IMAGE_TYPE(
110 	rkspi,
111 	"Rockchip SPI Boot Image support",
112 	0,
113 	NULL,
114 	rkcommon_check_params,
115 	rkspi_verify_header,
116 	rkspi_print_header,
117 	rkspi_set_header,
118 	rkspi_extract_subimage,
119 	rkspi_check_image_type,
120 	NULL,
121 	rkspi_vrec_header
122 );
123