From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CDA41C433F5 for ; Wed, 17 Nov 2021 17:54:04 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 417F260FED for ; Wed, 17 Nov 2021 17:54:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 417F260FED Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=softathome.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id AA56682F67; Wed, 17 Nov 2021 18:53:45 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=softathome.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id D419682C88; Wed, 17 Nov 2021 18:52:51 +0100 (CET) Received: from smtp.smtpout.orange.fr (smtp01.smtpout.orange.fr [80.12.242.123]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id B434982F50 for ; Wed, 17 Nov 2021 18:52:35 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=softathome.com Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=philippe.reynes@softathome.com Received: from localhost.localdomain ([90.0.151.89]) by smtp.orange.fr with ESMTPA id nP6emD1hA1UGBnP6omXdHB; Wed, 17 Nov 2021 18:52:35 +0100 X-ME-Helo: localhost.localdomain X-ME-Auth: ZDI3NDIxNif3YzVhYiQzN2FlZDdmZTc4NTQ2Nic3MzI2ZDdk X-ME-Date: Wed, 17 Nov 2021 18:52:35 +0100 X-ME-IP: 90.0.151.89 From: Philippe Reynes To: sjg@chromium.org, mr.nuke.me@gmail.com, joel.peshkin@broadcom.com Cc: u-boot@lists.denx.de, Philippe Reynes Subject: [RFC PATCH v3 8/8] tools: gen_pre_load_header.sh: initial import Date: Wed, 17 Nov 2021 18:52:15 +0100 Message-Id: <20211117175215.24262-9-philippe.reynes@softathome.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20211117175215.24262-1-philippe.reynes@softathome.com> References: <20211117175215.24262-1-philippe.reynes@softathome.com> X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.35 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean This commit adds a script gen_pre_load_header.sh that generate the header used by the image pre-load stage. Signed-off-by: Philippe Reynes --- tools/gen_pre_load_header.sh | 174 +++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100755 tools/gen_pre_load_header.sh diff --git a/tools/gen_pre_load_header.sh b/tools/gen_pre_load_header.sh new file mode 100755 index 0000000000..8256fa80ee --- /dev/null +++ b/tools/gen_pre_load_header.sh @@ -0,0 +1,174 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +# +# default value +# +size='4096' +algo='sha256,rsa2048' +padding='pkcs-1.5' +key='' +verbose='false' +input='' +output='' + +usage() { + printf "Usage: $0 -a -k [-p ] [-s ] [-v] -i -o \n" +} + +# +# parse arguments +# +while getopts 'a:hi:k:o:p:s:v' flag; do + case "${flag}" in + a) algo="${OPTARG}" ;; + h) usage + exit 0 ;; + i) input="${OPTARG}" ;; + k) key="${OPTARG}" ;; + o) output="${OPTARG}" ;; + p) padding="${OPTARG}" ;; + s) size="${OPTARG}" ;; + v) verbose='true' ;; + *) usage + exit 1 ;; + esac +done + +# +# check that mandatory arguments are provided +# +if [ -z "$key" -o -z "$input" -o -z "$output" ] +then + usage + exit 0 +fi + +hash=$(echo $algo | cut -d',' -f1) +sign=$(echo $algo | cut -d',' -f2) + +echo "status:" +echo "size = $size" +echo "algo = $algo" +echo "hash = $hash" +echo "sign = $sign" +echo "padding = $padding" +echo "key = $key" +echo "verbose = $verbose" + +# +# check if input file exist +# +if [ ! -f "$input" ] +then + echo "Error: file '$input' doesn't exist" + exit 1 +fi + +# +# check if output is not empty +# +if [ -z "$output" ] +then + echo "Error: output is empty" + exit 1 +fi + +# +# check that size is bigger than 0 +# +if [ $size -le 0 ] +then + echo "Error: $size lower than 0" + exit 1 +fi + +# +# check if the key file exist +# +if [ ! -f "$key" ] +then + echo "Error: file $key doesn't exist\n" + exit 1 +fi + +# +# check if the hash is valid and supported +# +print_supported_hash() { + echo "Supported hash:" + echo "- sha1" + echo "- sha256" + echo "- sha384" + echo "- sha512" +} + +case "$hash" in + "sha1") hashOption="-sha1" ;; + "sha256") hashOption="-sha256" ;; + "sha384") hashOption="-sha384" ;; + "sha512") hashOption="-sha512" ;; + *) echo "Error: $hash is an invalid hash" + print_supported_hash + exit 1;; +esac + +# +# check if the sign is valid and supported +# +print_supported_sign() { + echo "Supported sign:" + echo "- rsa1024" + echo "- rsa2048" + echo "- rsa4096" +} + +case "$sign" in + "rsa1024") ;; + "rsa2048") ;; + "rsa4096") ;; + *) echo "Error: $sign is an invalid signature type" + print_supported_sign + exit 1;; +esac + +# +# check if the padding is valid and supported +# +print_supported_padding() { + echo "Supported padding:" + echo "- pkcs-1.5" + echo "- pss" +} + +case "$padding" in + "pkcs-1.5") optionPadding='' ;; + "pss") optionPadding='-sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:-2' ;; + *) echo "Error: $padding is an invalid padding" + print_supported_padding + exit 1;; +esac + + +# +# generate the sigature +# +sig=$(openssl dgst $optionHash -sign $key $optionPadding $input | xxd -p) + +# +# generate the header +# +# 0 = magic +# 4 = image size +# 8 = signature +# +h=$(printf "%08x" 0x55425348) +i=$(stat --printf="%s" $input) +i=$(printf "%08x" $i) + +echo "$h$i$sig" | xxd -r -p > $output + +# +# fill the header with '\0' to reach the expected size +# +truncate -s $size $output -- 2.17.1