From: Kever Yang <kever.yang@rock-chips.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v3 03/16] rockchip: mkimage: Allow encoding of loader code in spl images
Date: Fri, 17 Feb 2017 11:23:14 +0800 [thread overview]
Message-ID: <58A66CA2.4080908@rock-chips.com> (raw)
In-Reply-To: <20170203160939.27594-4-heiko@sntech.de>
Hi Heiko,
On 02/04/2017 12:09 AM, Heiko Stuebner wrote:
> Rockchip SoCs allow the spl code to be rc4-encoded, not only the
> image header, but only newer SoCs allow this encoding to be disabled.
>
> The rk3188 is not part of those and requires its boot code to be
> rc4-encoded with the regular key. So add the ability to do this
> encoding via a setting on a per-soc basis when building spl images.
>
> Signed-off-by: Heiko Stuebner <heiko@sntech.de>
> ---
> tools/rkcommon.c | 33 +++++++++++++++++++++++++++++----
> tools/rkcommon.h | 22 ++++++++++++++++++++++
> tools/rkimage.c | 3 +++
> tools/rksd.c | 4 ++++
> tools/rkspi.c | 4 ++++
> 5 files changed, 62 insertions(+), 4 deletions(-)
>
> diff --git a/tools/rkcommon.c b/tools/rkcommon.c
> index 0a072aa83c..ed9b24137a 100644
> --- a/tools/rkcommon.c
> +++ b/tools/rkcommon.c
> @@ -46,17 +46,19 @@ struct header0_info {
> * @imagename: Image name(passed by "mkimage -n")
> * @spl_hdr: Boot ROM requires a 4-bytes spl header
> * @spl_size: Spl size(include extra 4-bytes spl header)
> + * @spl_rc4: RC4 encode the SPL binary (same key as header)
> */
> struct spl_info {
> const char *imagename;
> const char *spl_hdr;
> const uint32_t spl_size;
> + const bool spl_rc4;
> };
>
> static struct spl_info spl_infos[] = {
> - { "rk3036", "RK30", 0x1000 },
> - { "rk3288", "RK32", 0x8000 },
> - { "rk3399", "RK33", 0x20000 },
> + { "rk3036", "RK30", 0x1000, false },
> + { "rk3288", "RK32", 0x8000, false },
> + { "rk3399", "RK33", 0x20000, false },
> };
>
> static unsigned char rc4_key[16] = {
> @@ -113,6 +115,16 @@ int rkcommon_get_spl_size(struct image_tool_params *params)
> return info->spl_size;
> }
>
> +bool rkcommon_need_rc4_spl(struct image_tool_params *params)
> +{
> + struct spl_info *info = rkcommon_get_spl_info(params->imagename);
> +
> + /*
> + * info would not be NULL, because of we checked params before.
> + */
> + return info->spl_rc4;
> +}
> +
> int rkcommon_set_header(void *buf, uint file_size,
> struct image_tool_params *params)
> {
> @@ -124,7 +136,7 @@ int rkcommon_set_header(void *buf, uint file_size,
> memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
> hdr = (struct header0_info *)buf;
> hdr->signature = RK_SIGNATURE;
> - hdr->disable_rc4 = 1;
> + hdr->disable_rc4 = !rkcommon_need_rc4_spl(params);
> hdr->init_offset = RK_INIT_OFFSET;
>
> hdr->init_size = (file_size + RK_BLK_SIZE - 1) / RK_BLK_SIZE;
> @@ -135,3 +147,16 @@ int rkcommon_set_header(void *buf, uint file_size,
>
> return 0;
> }
> +
> +void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
> +{
> + unsigned int remaining = size;
> +
> + while (remaining > 0) {
> + int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining;
> +
> + rc4_encode(buf + offset, step, rc4_key);
> + offset += RK_BLK_SIZE;
> + remaining -= step;
> + }
> +}
> diff --git a/tools/rkcommon.h b/tools/rkcommon.h
> index c69540f5f3..b4f6f327dc 100644
> --- a/tools/rkcommon.h
> +++ b/tools/rkcommon.h
> @@ -55,4 +55,26 @@ int rkcommon_get_spl_size(struct image_tool_params *params);
> int rkcommon_set_header(void *buf, uint file_size,
> struct image_tool_params *params);
>
> +/**
> + * rkcommon_need_rc4_spl() - check if rc4 encoded spl is required
> + *
> + * Some socs cannot disable the rc4-encryption of the spl binary.
> + * rc4 encryption is disabled normally except on socs that cannot
> + * handle unencrypted binaries.
> + * @return true or false depending on rc4 being required.
> + */
> +bool rkcommon_need_rc4_spl(struct image_tool_params *params);
> +
> +/**
> + * rkcommon_rc4_encode_spl() - encode the spl binary
> + *
> + * Encrypts the SPL binary using the generic rc4 key as required
> + * by some socs.
> + *
> + * @buf: Pointer to the SPL data (header and SPL binary)
> + * @offset: offset inside buf to start at
> + * @size: number of bytes to encode
> + */
> +void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size);
> +
> #endif
> diff --git a/tools/rkimage.c b/tools/rkimage.c
> index ef31cb6944..44d098c775 100644
> --- a/tools/rkimage.c
> +++ b/tools/rkimage.c
> @@ -28,6 +28,9 @@ static void rkimage_set_header(void *buf, struct stat *sbuf, int ifd,
> {
> memcpy(buf + RK_SPL_HDR_START, rkcommon_get_spl_hdr(params),
> RK_SPL_HDR_SIZE);
> +
> + if (rkcommon_need_rc4_spl(params))
> + rkcommon_rc4_encode_spl(buf, 4, params->file_size);
> }
>
> static int rkimage_extract_subimage(void *buf, struct image_tool_params *params)
> diff --git a/tools/rksd.c b/tools/rksd.c
> index a2baa74d31..ff2233ff2d 100644
> --- a/tools/rksd.c
> +++ b/tools/rksd.c
> @@ -41,6 +41,10 @@ static void rksd_set_header(void *buf, struct stat *sbuf, int ifd,
>
> memcpy(buf + RK_SPL_HDR_START, rkcommon_get_spl_hdr(params),
> RK_SPL_HDR_SIZE);
> +
> + if (rkcommon_need_rc4_spl(params))
> + rkcommon_rc4_encode_spl(buf, RK_SPL_START - 4,
> + params->file_size - RK_SPL_START + 4);
> }
>
> static int rksd_extract_subimage(void *buf, struct image_tool_params *params)
> diff --git a/tools/rkspi.c b/tools/rkspi.c
> index a0b0051d38..0271d2e817 100644
> --- a/tools/rkspi.c
> +++ b/tools/rkspi.c
> @@ -48,6 +48,10 @@ static void rkspi_set_header(void *buf, struct stat *sbuf, int ifd,
> memcpy(buf + RK_SPL_HDR_START, rkcommon_get_spl_hdr(params),
> RK_SPL_HDR_SIZE);
>
> + if (rkcommon_need_rc4_spl(params))
> + rkcommon_rc4_encode_spl(buf, RK_SPL_START - 4,
> + params->file_size - RK_SPL_START + 4);
> +
> /*
> * Spread the image out so we only use the first 2KB of each 4KB
> * region. This is a feature of the SPI format required by the Rockchip
Reviewed-by: Kever Yang <kever.yang@rock-chips.com>
Tested-by: Kever Yang <kever.yang@rock-chips.com>
Thanks,
- Kever
next prev parent reply other threads:[~2017-02-17 3:23 UTC|newest]
Thread overview: 45+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-02-03 16:09 [U-Boot] [PATCH v3 00/16] rk3188 uboot support Heiko Stuebner
2017-02-03 16:09 ` [U-Boot] [PATCH v3 01/16] dm: allow limiting pre-reloc markings to spl or tpl Heiko Stuebner
2017-02-06 15:34 ` Simon Glass
2017-02-17 0:36 ` Heiko Stübner
2017-02-22 3:59 ` Simon Glass
2017-02-03 16:09 ` [U-Boot] [PATCH v3 02/16] rockchip: move bootrom helper compilation to a hidden option Heiko Stuebner
2017-02-06 15:34 ` Simon Glass
2017-02-03 16:09 ` [U-Boot] [PATCH v3 03/16] rockchip: mkimage: Allow encoding of loader code in spl images Heiko Stuebner
2017-02-06 15:35 ` Simon Glass
2017-02-17 3:23 ` Kever Yang [this message]
2017-02-03 16:09 ` [U-Boot] [PATCH v3 04/16] rockchip: mkimage: Add support rk3188 serial Heiko Stuebner
2017-02-06 15:35 ` Simon Glass
2017-02-17 3:29 ` Kever Yang
2017-02-03 16:09 ` [U-Boot] [PATCH v3 05/16] rockchip: serial: Adapt rockchip of-platdata driver for rk3188 Heiko Stuebner
2017-02-06 15:35 ` Simon Glass
2017-02-03 16:09 ` [U-Boot] [PATCH v3 06/16] rockchip: rk3188: Add header files for PMU and GRF Heiko Stuebner
2017-02-03 16:09 ` [U-Boot] [PATCH v3 07/16] rockchip: rk3188: Add pinctrl driver Heiko Stuebner
2017-02-03 16:09 ` [U-Boot] [PATCH v3 08/16] rockchip: rk3188: Add sysreset driver Heiko Stuebner
2017-02-06 15:35 ` Simon Glass
2017-02-03 16:09 ` [U-Boot] [PATCH v3 09/16] rockchip: rk3188: Add rk3066/rk3188 clock bindings Heiko Stuebner
2017-02-03 16:09 ` [U-Boot] [PATCH v3 10/16] rockchip: rk3188: Add clock driver Heiko Stuebner
2017-02-06 15:35 ` Simon Glass
2017-02-18 14:19 ` Heiko Stuebner
2017-02-21 18:07 ` Simon Glass
2017-02-21 20:35 ` Simon Glass
2017-02-03 16:09 ` [U-Boot] [PATCH v3 11/16] rockchip: rk3188: Add core devicetree files Heiko Stuebner
2017-02-06 15:35 ` Simon Glass
2017-02-10 23:17 ` Heiko Stuebner
2017-02-03 16:09 ` [U-Boot] [PATCH v3 12/16] rockchip: rk3188: Add core support Heiko Stuebner
2017-02-06 15:35 ` Simon Glass
2017-02-03 16:09 ` [U-Boot] [PATCH v3 13/16] rockchip: rk3188: Add sdram driver Heiko Stuebner
2017-02-06 15:35 ` Simon Glass
2017-02-17 20:39 ` Heiko Stuebner
2017-02-17 20:44 ` Simon Glass
2017-02-03 16:09 ` [U-Boot] [PATCH v3 14/16] rockchip: rk3188: Add main, spl and tpl boards Heiko Stuebner
2017-02-06 15:35 ` Simon Glass
2017-02-11 0:47 ` Heiko Stuebner
2017-02-03 16:09 ` [U-Boot] [PATCH v3 15/16] rockchip: rk3188: Add Radxa Rock board Heiko Stuebner
2017-02-06 15:35 ` Simon Glass
2017-02-03 16:09 ` [U-Boot] [TEST-ONLY 16/16] Add a temporary script that can create a bootimage for rk3188 Heiko Stuebner
2017-02-06 15:35 ` Simon Glass
2017-02-16 20:43 ` [U-Boot] [PATCH v3 00/16] rk3188 uboot support Simon Glass
2017-02-17 0:41 ` Heiko Stübner
2017-02-17 20:11 ` Heiko Stuebner
2017-02-17 3:21 ` Kever Yang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=58A66CA2.4080908@rock-chips.com \
--to=kever.yang@rock-chips.com \
--cc=u-boot@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.