blob: 0271d2e817bfa0e1d543b4b3c414b4351568238e [file] [log] [blame]
Simon Glassc17b1452015-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 Glassc17b1452015-08-30 16:55:26 -060017 RKSPI_SECT_LEN = RK_BLK_SIZE * 4,
18};
19
Jeffy Chen06cdc342015-11-27 12:07:17 +080020static char dummy_hdr[RK_IMAGE_HEADER_LEN];
Simon Glassc17b1452015-08-30 16:55:26 -060021
22static int rkspi_verify_header(unsigned char *buf, int size,
23 struct image_tool_params *params)
24{
25 return 0;
26}
27
28static void rkspi_print_header(const void *buf)
29{
30}
31
32static void rkspi_set_header(void *buf, struct stat *sbuf, int ifd,
33 struct image_tool_params *params)
34{
35 int sector;
36 unsigned int size;
37 int ret;
38
39 size = params->orig_file_size;
Jeffy Chen06cdc342015-11-27 12:07:17 +080040 ret = rkcommon_set_header(buf, size, params);
Simon Glassc17b1452015-08-30 16:55:26 -060041 debug("size %x\n", size);
42 if (ret) {
43 /* TODO(sjg@chromium.org): This method should return an error */
44 printf("Warning: SPL image is too large (size %#x) and will not boot\n",
45 size);
46 }
47
Jeffy Chen06cdc342015-11-27 12:07:17 +080048 memcpy(buf + RK_SPL_HDR_START, rkcommon_get_spl_hdr(params),
49 RK_SPL_HDR_SIZE);
Simon Glassc17b1452015-08-30 16:55:26 -060050
Heiko Stübner3b404d42017-02-18 19:46:27 +010051 if (rkcommon_need_rc4_spl(params))
52 rkcommon_rc4_encode_spl(buf, RK_SPL_START - 4,
53 params->file_size - RK_SPL_START + 4);
54
Simon Glassc17b1452015-08-30 16:55:26 -060055 /*
56 * Spread the image out so we only use the first 2KB of each 4KB
57 * region. This is a feature of the SPI format required by the Rockchip
58 * boot ROM. Its rationale is unknown.
59 */
60 for (sector = size / RKSPI_SECT_LEN - 1; sector >= 0; sector--) {
Simon Glass8a123522015-12-29 05:22:44 -070061 debug("sector %u\n", sector);
Simon Glassc17b1452015-08-30 16:55:26 -060062 memmove(buf + sector * RKSPI_SECT_LEN * 2,
63 buf + sector * RKSPI_SECT_LEN,
64 RKSPI_SECT_LEN);
65 memset(buf + sector * RKSPI_SECT_LEN * 2 + RKSPI_SECT_LEN,
66 '\0', RKSPI_SECT_LEN);
67 }
68}
69
70static int rkspi_extract_subimage(void *buf, struct image_tool_params *params)
71{
72 return 0;
73}
74
75static int rkspi_check_image_type(uint8_t type)
76{
77 if (type == IH_TYPE_RKSPI)
78 return EXIT_SUCCESS;
79 else
80 return EXIT_FAILURE;
81}
82
83/* We pad the file out to a fixed size - this method returns that size */
84static int rkspi_vrec_header(struct image_tool_params *params,
85 struct image_type_params *tparams)
86{
87 int pad_size;
88
Jeffy Chen06cdc342015-11-27 12:07:17 +080089 pad_size = (rkcommon_get_spl_size(params) + 0x7ff) / 0x800 * 0x800;
Simon Glassc17b1452015-08-30 16:55:26 -060090 params->orig_file_size = pad_size;
91
92 /* We will double the image size due to the SPI format */
93 pad_size *= 2;
Jeffy Chen06cdc342015-11-27 12:07:17 +080094 pad_size += RK_SPL_HDR_START;
Simon Glassc17b1452015-08-30 16:55:26 -060095 debug("pad_size %x\n", pad_size);
96
97 return pad_size - params->file_size;
98}
99
100/*
101 * rk_spi parameters
102 */
103U_BOOT_IMAGE_TYPE(
104 rkspi,
105 "Rockchip SPI Boot Image support",
Jeffy Chen06cdc342015-11-27 12:07:17 +0800106 RK_IMAGE_HEADER_LEN,
Simon Glassc17b1452015-08-30 16:55:26 -0600107 dummy_hdr,
Jeffy Chen06cdc342015-11-27 12:07:17 +0800108 rkcommon_check_params,
Simon Glassc17b1452015-08-30 16:55:26 -0600109 rkspi_verify_header,
110 rkspi_print_header,
111 rkspi_set_header,
112 rkspi_extract_subimage,
113 rkspi_check_image_type,
114 NULL,
115 rkspi_vrec_header
116);