blob: d2d3fdda424eadc545aac09253d65ae574268ec7 [file] [log] [blame]
Simon Glass10b84fe2015-08-30 16:55:26 -06001/*
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
16enum {
Simon Glass10b84fe2015-08-30 16:55:26 -060017 RKSPI_SECT_LEN = RK_BLK_SIZE * 4,
18};
19
Simon Glass10b84fe2015-08-30 16:55:26 -060020static int rkspi_verify_header(unsigned char *buf, int size,
21 struct image_tool_params *params)
22{
23 return 0;
24}
25
26static void rkspi_print_header(const void *buf)
27{
28}
29
30static 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;
Jeffy Chen7bf274b2015-11-27 12:07:17 +080038 ret = rkcommon_set_header(buf, size, params);
Simon Glass10b84fe2015-08-30 16:55:26 -060039 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 not boot\n",
43 size);
44 }
45
Simon Glass10b84fe2015-08-30 16:55:26 -060046 /*
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--) {
Simon Glass25525eb2015-12-29 05:22:44 -070052 debug("sector %u\n", sector);
Simon Glass10b84fe2015-08-30 16:55:26 -060053 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
61static int rkspi_extract_subimage(void *buf, struct image_tool_params *params)
62{
63 return 0;
64}
65
66static 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/* We pad the file out to a fixed size - this method returns that size */
75static int rkspi_vrec_header(struct image_tool_params *params,
76 struct image_type_params *tparams)
77{
78 int pad_size;
79
Philipp Tomsich111bcc42017-03-15 12:08:43 +010080 rkcommon_vrec_header(params, tparams);
81
Jeffy Chen7bf274b2015-11-27 12:07:17 +080082 pad_size = (rkcommon_get_spl_size(params) + 0x7ff) / 0x800 * 0x800;
Simon Glass10b84fe2015-08-30 16:55:26 -060083 params->orig_file_size = pad_size;
84
85 /* We will double the image size due to the SPI format */
86 pad_size *= 2;
Jeffy Chen7bf274b2015-11-27 12:07:17 +080087 pad_size += RK_SPL_HDR_START;
Simon Glass10b84fe2015-08-30 16:55:26 -060088 debug("pad_size %x\n", pad_size);
89
Philipp Tomsich111bcc42017-03-15 12:08:43 +010090 return pad_size - params->file_size - tparams->header_size;
Simon Glass10b84fe2015-08-30 16:55:26 -060091}
92
93/*
94 * rk_spi parameters
95 */
96U_BOOT_IMAGE_TYPE(
97 rkspi,
98 "Rockchip SPI Boot Image support",
Philipp Tomsich111bcc42017-03-15 12:08:43 +010099 0,
100 NULL,
Jeffy Chen7bf274b2015-11-27 12:07:17 +0800101 rkcommon_check_params,
Simon Glass10b84fe2015-08-30 16:55:26 -0600102 rkspi_verify_header,
103 rkspi_print_header,
104 rkspi_set_header,
105 rkspi_extract_subimage,
106 rkspi_check_image_type,
107 NULL,
108 rkspi_vrec_header
109);