From patchwork Fri Feb 3 16:09:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Heiko_St=C3=BCbner?= X-Patchwork-Id: 723802 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 3vFMmJ6623z9s76 for ; Sat, 4 Feb 2017 03:30:24 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 506D3B387B; Fri, 3 Feb 2017 17:30:17 +0100 (CET) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id P7CvATbpqmQh; Fri, 3 Feb 2017 17:30:17 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id B3B27B387E; Fri, 3 Feb 2017 17:30:13 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 793F74ABEE for ; Fri, 3 Feb 2017 17:30:08 +0100 (CET) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id uIaXAhbEr0KK for ; Fri, 3 Feb 2017 17:30:08 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from gloria.sntech.de (gloria.sntech.de [95.129.55.99]) by theia.denx.de (Postfix) with ESMTPS id 466B54ABD8 for ; Fri, 3 Feb 2017 17:30:05 +0100 (CET) Received: from host-78-129-33-179.dynamic.voo.be ([78.129.33.179] helo=phil.sntech) by gloria.sntech.de with esmtpsa (TLS1.1:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1cZgRM-0002I2-RF; Fri, 03 Feb 2017 17:10:24 +0100 From: Heiko Stuebner To: sjg@chromium.org Date: Fri, 3 Feb 2017 17:09:26 +0100 Message-Id: <20170203160939.27594-4-heiko@sntech.de> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170203160939.27594-1-heiko@sntech.de> References: <20170203160939.27594-1-heiko@sntech.de> Cc: romain.perier@collabora.com, u-boot@lists.denx.de Subject: [U-Boot] [PATCH v3 03/16] rockchip: mkimage: Allow encoding of loader code in spl images X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.15 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" 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 Reviewed-by: Simon Glass Reviewed-by: Kever Yang Tested-by: Kever Yang --- 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