From nobody Sun Nov 16 02:28:11 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1762278577; cv=none; d=zohomail.com; s=zohoarc; b=OJurazfR2TeKT/3qIYneDSo7L1lNpD4h+osUOpLUmR9DsCoLytUHBu/FTZNzK67sKJe4L6YEFjvRxSQQCCOgZZOlJc9aofJafuWyKzikbCJLU7hq25Uc/VIn+fMp7mR/Gf9z7e/jtNDH4WLXsu72UEUetAcNtLZqYo/Q08b3Bb8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1762278577; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To:Cc; bh=yILkf0T8P7LvQ7q+vTBoHw4z3M0o0Y7BvJLfczUoIPQ=; b=AUU6TziqHnhbZl76tEojz7gx/ZGu/xm6r6SaYPHseBPlPxQnd11nimiqrfurhjmHM+1dJcn/4wJ3IbZmjh9Z1t2iQTem0Me/gG/QKgN75NEY1+INqkiliP+gnFZ92UGlavnTxtIzBNXGmpYiazG/0QDrttfk6LeU/UCERMxAiCA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1762278577159916.8998512498551; Tue, 4 Nov 2025 09:49:37 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vGL9t-0004VM-R3; Tue, 04 Nov 2025 12:49:31 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vGL9S-0004NA-4E for qemu-devel@nongnu.org; Tue, 04 Nov 2025 12:49:12 -0500 Received: from mail-ed1-x529.google.com ([2a00:1450:4864:20::529]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vGL9Q-0005lR-1A for qemu-devel@nongnu.org; Tue, 04 Nov 2025 12:49:01 -0500 Received: by mail-ed1-x529.google.com with SMTP id 4fb4d7f45d1cf-640e9a53ff6so1978789a12.0 for ; Tue, 04 Nov 2025 09:48:58 -0800 (PST) Received: from localhost.localdomain ([87.213.113.147]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-640e67eb64dsm2578187a12.5.2025.11.04.09.48.55 for (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 04 Nov 2025 09:48:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1762278537; x=1762883337; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=yILkf0T8P7LvQ7q+vTBoHw4z3M0o0Y7BvJLfczUoIPQ=; b=UWegAhQaCZb/abTL3JEeJDU19DE8LmrJoeHSEeSFdabK41hb9waHQ2YLxtI15oyW/8 S9tMsVR8GZDQ+re+9PT5BgARexzRjjJudEToek+s6hKSbSUuaKhHCBXeUqVZL1C+dkTG dR0LoRwJSs6RSCVjbi6tsxTfSTUdRdKt1FvTzYCjH4XIbgAnmQT56NWDUGzOyr+aKj4g ZPbAs7fnOjGJL6+fYRR1NBMsClKTRt/aataOOVAZp0jquqtL/ZPb2ul73k+Mgdycsz0B tJeW9yL05Z7/3lihxGYuahu6JdFIDAhMxzD/V1uLmFMUWRGGaTo6m0kPgtj/G+hxzITF 9niw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762278537; x=1762883337; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yILkf0T8P7LvQ7q+vTBoHw4z3M0o0Y7BvJLfczUoIPQ=; b=IKEfiU5UsCaGibeygxuRQOLACD5DjYAtbRZlAzju4UZCl0Wiu2hLT5Y0LNN3PNq/e/ whOCpjdOuX9xLzET80F3AQBew7i1zuS04HtTWAwDAlK71/tb9PXna8D3eBKkGWcl4mYn lfPlaRxmKKw1jVPB8ppLEQ6mcz0dpqjaHsf15qktssigkrl790n8r7aIhwPqNUXYItnV rnCJ7pev4sTyn5gP0pPKs2jkHuYfWeB74078L3kaZ0fA2wcLXOprvvpkUHOVK9ELePhF 3RK7m8LG4ABI9hHJ6YU7JJ/w651ij31y+ZvSKYd7Z4oQ/tQ50xGcyfwc0f32kpKMEg5X 0AwQ== X-Gm-Message-State: AOJu0YzwBEJI06NK3aSAauk4DjyiWx83fPTLjlo78YKlgS7iTLdIpPS+ RhqDlDM/XVG3cJYBTJuj2tN0F6c59NwX33M0KXelP+Zt78hGDRhm7SKRKSnzJY+8zKCheDtt/On BtECufT+7mg== X-Gm-Gg: ASbGncsr2LwytYL/eAAeDKgXOhIPweCElE6ZFIaj9S8WxW6zNIANZzKng69Qe4BTWaC VqDNJXbYsw6kdRzXYtWem9Otj81578EoP5MeOZZH/bBtiv71CehXvAVKqWfvtSCIJNU/9WKE9bt Kn3TgmO6GajOJPvDPp6Jl/4pxRuUbY8Hvju61ELXRtwc2xJZ8Q7ZvNZR3xf9YdGbOdig05NbGmh Evhu3m46ySVlG7S/EkcZNJpRNaTgs8MWYP3yRPy4jPO64VROqNJQRdciWN4GFhKzyTyXJGLZ670 VgBnxbDcMOzA01Dx6Psl6+66lVs4Ok/MceEisoC68brD+WB1SGdEIeyHmxBCWz24LvE6x1C+pd5 2Qpxytig28YYpfKroajA7fqvbGuHfhPDoGqAbrVojWPcze5AOkkIK2Yamrgu/WkPLzAPPQrqlgl SjnG6pG9kGZIhL3JAcs0jQGg== X-Google-Smtp-Source: AGHT+IHuxkydOGlIXGjtwpqfsdvJvR+JXJ9f3GWwoXPGDASM3isHjZ4w1HIz3iGPaTOt45dEaioyyg== X-Received: by 2002:a05:6402:13cb:b0:640:9ce9:a8e6 with SMTP id 4fb4d7f45d1cf-641058a22c3mr79564a12.4.1762278536551; Tue, 04 Nov 2025 09:48:56 -0800 (PST) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Subject: [PULL 5/8] scripts: Add helper script to generate eMMC block device images Date: Tue, 4 Nov 2025 18:48:20 +0100 Message-ID: <20251104174823.92412-6-philmd@linaro.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251104174823.92412-1-philmd@linaro.org> References: <20251104174823.92412-1-philmd@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::529; envelope-from=philmd@linaro.org; helo=mail-ed1-x529.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1762278579731154100 From: Jan Kiszka As an eMMC block device image may consist of more than just the user data partition, provide a helper script that can compose the image from boot partitions, an RPMB partition and the user data image. The script also does the required size validation and/or rounding. Signed-off-by: Jan Kiszka Reviewed-by: Philippe Mathieu-Daud=C3=A9 Tested-by: Philippe Mathieu-Daud=C3=A9 Message-ID: Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- scripts/mkemmc.sh | 216 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100755 scripts/mkemmc.sh diff --git a/scripts/mkemmc.sh b/scripts/mkemmc.sh new file mode 100755 index 00000000000..429388213c6 --- /dev/null +++ b/scripts/mkemmc.sh @@ -0,0 +1,216 @@ +#!/bin/sh -e +# SPDX-License-Identifier: GPL-2.0-only +# +# Create eMMC block device image from boot, RPMB and user data images +# +# Copyright (c) Siemens, 2025 +# +# Authors: +# Jan Kiszka +# + +usage() { + echo "$0 [OPTIONS] USER_IMG[:SIZE] OUTPUT_IMG" + echo "" + echo "SIZE must be a power of 2 up to 2G and multiples of 512 byte fro= m there on." + echo "If no SIZE is specified, the size of USER_ING will be used (roun= ded up)." + echo "" + echo "Supported options:" + echo " -b BOOT1_IMG[:SIZE] Add boot partitions. SIZE must be multip= les of 128K. If" + echo " no SIZE is specified, the size of BOOT= 1_IMG will be" + echo " used (rounded up). BOOT1_IMG will be s= tored in boot" + echo " partition 1, and a boot partition 2 of= the same size" + echo " will be created as empty (all zeros) u= nless -B is" + echo " specified as well." + echo " -B BOOT2_IMG Fill boot partition 2 with BOOT2_IMG. Mu= st be combined" + echo " with -b which is also defining the par= tition size." + echo " -r RPMB_IMG[:SIZE] Add RPMB partition. SIZE must be multipl= es of 128K. If" + echo " no SIZE is specified, the size of RPMB= _IMG will be" + echo " used (rounded up)." + echo " -h, --help This help" + echo "" + echo "All SIZE parameters support the units K, M, G. If SIZE is smalle= r than the" + echo "associated image, it will be truncated in the output image." + exit "$1" +} + +process_size() { + name=3D$1 + image_file=3D$2 + alignment=3D$3 + image_arg=3D$4 + if [ "${image_arg#*:}" =3D "$image_arg" ]; then + if ! size=3D$(wc -c < "$image_file" 2>/dev/null); then + echo "Missing $name image '$image_file'." >&2 + exit 1 + fi + if [ "$alignment" =3D 128 ]; then + size=3D$(( (size + 128 * 1024 - 1) & ~(128 * 1024 - 1) )) + elif [ $size -gt $((2 * 1024 * 1024 * 1024)) ]; then + size=3D$(( (size + 511) & ~511 )) + elif [ $(( size & (size - 1) )) -gt 0 ]; then + n=3D0 + while [ "$size" -gt 0 ]; do + size=3D$((size >> 1)) + n=3D$((n + 1)) + done + size=3D$((1 << n)) + fi + else + value=3D"${image_arg#*:}" + if [ "${value%K}" !=3D "$value" ]; then + size=3D${value%K} + multiplier=3D1024 + elif [ "${value%M}" !=3D "$value" ]; then + size=3D${value%M} + multiplier=3D$((1024 * 1024)) + elif [ "${value%G}" !=3D "$value" ]; then + size=3D${value%G} + multiplier=3D$((1024 * 1024 * 1024)) + else + size=3D$value + multiplier=3D1 + fi + # check if "$size" is a valid integer by doing a self-comparison + if [ "$size" -eq "$size" ] 2>/dev/null; then + size=3D$((size * multiplier)) + else + echo "Invalid value '$value' specified for $image_file image s= ize." >&2 + exit 1 + fi + if [ "$alignment" =3D 128 ]; then + if [ $(( size & (128 * 1024 - 1) )) -ne 0 ]; then + echo "The $name image size must be multiples of 128K." >&2 + exit 1 + fi + elif [ $size -gt $((2 * 1024 * 1024 * 1024)) ]; then + if [ $(( size & 511)) -ne 0 ]; then + echo "The $name image size must be multiples of 512 (if >2= G)." >&2 + exit 1 + fi + elif [ $(( size & (size - 1) )) -gt 0 ]; then + echo "The $name image size must be power of 2 (up to 2G)." >&2 + exit 1 + fi + fi + echo $size +} + +check_truncation() { + image_file=3D$1 + output_size=3D$2 + if [ "$image_file" =3D "/dev/zero" ]; then + return + fi + if ! actual_size=3D$(wc -c < "$image_file" 2>/dev/null); then + echo "Missing image '$image_file'." >&2 + exit 1 + fi + if [ "$actual_size" -gt "$output_size" ]; then + echo "Warning: image '$image_file' will be truncated on output." + fi +} + +userimg=3D +outimg=3D +bootimg1=3D +bootimg2=3D/dev/zero +bootsz=3D0 +rpmbimg=3D +rpmbsz=3D0 + +while [ $# -gt 0 ]; do + case "$1" in + -b) + shift + [ $# -ge 1 ] || usage 1 + bootimg1=3D${1%%:*} + bootsz=3D$(process_size boot "$bootimg1" 128 "$1") + shift + ;; + -B) + shift + [ $# -ge 1 ] || usage 1 + bootimg2=3D$1 + shift + ;; + -r) + shift + [ $# -ge 1 ] || usage 1 + rpmbimg=3D${1%%:*} + rpmbsz=3D$(process_size RPMB "$rpmbimg" 128 "$1") + shift + ;; + -h|--help) + usage 0 + ;; + *) + if [ -z "$userimg" ]; then + userimg=3D${1%%:*} + usersz=3D$(process_size user "$userimg" U "$1") + elif [ -z "$outimg" ]; then + outimg=3D$1 + else + usage 1 + fi + shift + ;; + esac +done + +[ -n "$outimg" ] || usage 1 + +if [ "$bootsz" -gt $((32640 * 1024)) ]; then + echo "Boot image size is larger than 32640K." >&2 + exit 1 +fi +if [ "$rpmbsz" -gt $((16384 * 1024)) ]; then + echo "RPMB image size is larger than 16384K." >&2 + exit 1 +fi + +echo "Creating eMMC image" + +truncate -s 0 "$outimg" +pos=3D0 + +if [ "$bootsz" -gt 0 ]; then + echo " Boot partition 1 and 2: $((bootsz / 1024))K each" + blocks=3D$(( bootsz / (128 * 1024) )) + check_truncation "$bootimg1" "$bootsz" + dd if=3D"$bootimg1" of=3D"$outimg" conv=3Dsparse bs=3D128K count=3D$bl= ocks \ + status=3Dnone + check_truncation "$bootimg2" "$bootsz" + dd if=3D"$bootimg2" of=3D"$outimg" conv=3Dsparse bs=3D128K count=3D$bl= ocks \ + seek=3D$blocks status=3Dnone + pos=3D$((2 * bootsz)) +fi + +if [ "$rpmbsz" -gt 0 ]; then + echo " RPMB partition: $((rpmbsz / 1024))K" + blocks=3D$(( rpmbsz / (128 * 1024) )) + check_truncation "$rpmbimg" "$rpmbsz" + dd if=3D"$rpmbimg" of=3D"$outimg" conv=3Dsparse bs=3D128K count=3D$blo= cks \ + seek=3D$(( pos / (128 * 1024) )) status=3Dnone + pos=3D$((pos + rpmbsz)) +fi + +if [ "$usersz" -lt 1024 ]; then + echo " User data: $usersz bytes" +elif [ "$usersz" -lt $((1024 * 1024)) ]; then + echo " User data: $(( (usersz + 1023) / 1024 ))K ($use= rsz)" +elif [ "$usersz" -lt $((1024 * 1024 * 1024)) ]; then + echo " User data: $(( (usersz + 1048575) / 1048576))M = ($usersz)" +else + echo " User data: $(( (usersz + 1073741823) / 10737418= 24))G ($usersz)" +fi +check_truncation "$userimg" "$usersz" +dd if=3D"$userimg" of=3D"$outimg" conv=3Dsparse bs=3D128K seek=3D$(( pos /= (128 * 1024) )) \ + count=3D$(( (usersz + 128 * 1024 - 1) / (128 * 1024) )) status=3Dnone +pos=3D$((pos + usersz)) +truncate -s $pos "$outimg" + +echo "" +echo "Instantiate by appending to the qemu command line:" +echo " -drive file=3D$outimg,if=3Dnone,format=3Draw,id=3Demmc-img" +echo " -device emmc,boot-partition-size=3D$bootsz,rpmb-partition-size=3D$= rpmbsz,drive=3Demmc-img" --=20 2.51.0