xref: /openbmc/u-boot/tools/rkspi.c (revision f458c8da)
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_SPL_HDR_START	= RK_CODE1_OFFSET * RK_BLK_SIZE,
18 	RKSPI_SPL_START		= RKSPI_SPL_HDR_START + 4,
19 	RKSPI_HEADER_LEN	= RKSPI_SPL_START,
20 	RKSPI_SECT_LEN		= RK_BLK_SIZE * 4,
21 };
22 
23 static char dummy_hdr[RKSPI_HEADER_LEN];
24 
25 static int rkspi_check_params(struct image_tool_params *params)
26 {
27 	return 0;
28 }
29 
30 static int rkspi_verify_header(unsigned char *buf, int size,
31 			       struct image_tool_params *params)
32 {
33 	return 0;
34 }
35 
36 static void rkspi_print_header(const void *buf)
37 {
38 }
39 
40 static void rkspi_set_header(void *buf, struct stat *sbuf, int ifd,
41 			     struct image_tool_params *params)
42 {
43 	int sector;
44 	unsigned int size;
45 	int ret;
46 
47 	size = params->orig_file_size;
48 	ret = rkcommon_set_header(buf, size);
49 	debug("size %x\n", size);
50 	if (ret) {
51 		/* TODO(sjg@chromium.org): This method should return an error */
52 		printf("Warning: SPL image is too large (size %#x) and will not boot\n",
53 		       size);
54 	}
55 
56 	memcpy(buf + RKSPI_SPL_HDR_START, "RK32", 4);
57 
58 	/*
59 	 * Spread the image out so we only use the first 2KB of each 4KB
60 	 * region. This is a feature of the SPI format required by the Rockchip
61 	 * boot ROM. Its rationale is unknown.
62 	 */
63 	for (sector = size / RKSPI_SECT_LEN - 1; sector >= 0; sector--) {
64 		printf("sector %u\n", sector);
65 		memmove(buf + sector * RKSPI_SECT_LEN * 2,
66 			buf + sector * RKSPI_SECT_LEN,
67 			RKSPI_SECT_LEN);
68 		memset(buf + sector * RKSPI_SECT_LEN * 2 + RKSPI_SECT_LEN,
69 		       '\0', RKSPI_SECT_LEN);
70 	}
71 }
72 
73 static int rkspi_extract_subimage(void *buf, struct image_tool_params *params)
74 {
75 	return 0;
76 }
77 
78 static int rkspi_check_image_type(uint8_t type)
79 {
80 	if (type == IH_TYPE_RKSPI)
81 		return EXIT_SUCCESS;
82 	else
83 		return EXIT_FAILURE;
84 }
85 
86 /* We pad the file out to a fixed size - this method returns that size */
87 static int rkspi_vrec_header(struct image_tool_params *params,
88 			     struct image_type_params *tparams)
89 {
90 	int pad_size;
91 
92 	pad_size = (RK_MAX_CODE1_SIZE + 0x7ff) / 0x800 * 0x800;
93 	params->orig_file_size = pad_size;
94 
95 	/* We will double the image size due to the SPI format */
96 	pad_size *= 2;
97 	pad_size += RKSPI_SPL_HDR_START;
98 	debug("pad_size %x\n", pad_size);
99 
100 	return pad_size - params->file_size;
101 }
102 
103 /*
104  * rk_spi parameters
105  */
106 U_BOOT_IMAGE_TYPE(
107 	rkspi,
108 	"Rockchip SPI Boot Image support",
109 	RKSPI_HEADER_LEN,
110 	dummy_hdr,
111 	rkspi_check_params,
112 	rkspi_verify_header,
113 	rkspi_print_header,
114 	rkspi_set_header,
115 	rkspi_extract_subimage,
116 	rkspi_check_image_type,
117 	NULL,
118 	rkspi_vrec_header
119 );
120