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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id DF741EB64DC for ; Mon, 3 Jul 2023 15:07:54 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 4CC5B86607; Mon, 3 Jul 2023 17:07:47 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="EdWyEKkY"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id E094486614; Mon, 3 Jul 2023 17:07:45 +0200 (CEST) Received: from mail-oo1-xc30.google.com (mail-oo1-xc30.google.com [IPv6:2607:f8b0:4864:20::c30]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id EBD3486281 for ; Mon, 3 Jul 2023 17:07:25 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jpewhacker@gmail.com Received: by mail-oo1-xc30.google.com with SMTP id 006d021491bc7-565d65adcf2so2473786eaf.3 for ; Mon, 03 Jul 2023 08:07:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1688396843; x=1690988843; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=FtcQiNdmWOhwWt998RvdInJ9iPoeII/lviPw7JfsXp0=; b=EdWyEKkYI2BSmIUkwet7UDSz3XscGCOBZBt/D1e6OOA2KaO/6taIHNb3NGETUrXkbV XT9E+qPRBsN+EwVeNXrVUfO/n41143NMw6O/vN6OQh+3UF8oFaBm3T8IcRV2IeGhzP6O Yu1VojoCA+vYYqkM+83VO0RJKm2vawsEoM76bhhUBFI2q1mJ+RjDWZD3Qu6yhpczxWmX pd33fQLpK1tfeF2UArlqt5zfbJyklLDkDMERE6E/5/AmZT27qCbZRKbUiR2ZeeGnT+0R Yyints5/18Ja6fgoVBNK8b9qHspriwJYS+5xvrf86HSfoPYEVhfQrlEUKqs0Srl+DVz0 vITg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688396843; x=1690988843; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=FtcQiNdmWOhwWt998RvdInJ9iPoeII/lviPw7JfsXp0=; b=CMZ7fGWtJcigN5LLde+/rz4ahh0ZEpgsQF+ftLIgC0WSMkz4N9OQdajNDq80bTisF0 lVgpS50/rJBm4XLZJ0r9gsfIhf6VuHkbymGKIUICLTexg6kBcRoMMhBBjMZDU9yDKVpY IYE7ky6WpcIUfmOxxZ/FVGmLe+MC6n76l/agdIA25ekdscYX6N93JEWCms8cjYoHD+d7 UG3E/Xy90Tp1MhHBPS+5Wv6Jptj8r6VT85JnVPiboRYvr6tzmArWmfut6345A1c6GDyN 9RynCscdHy18kEDRZonaQbIhV1lECVEvQ6kG0EugY7vO5iHLKTasTMd1tpdS1V8xzl69 bh7w== X-Gm-Message-State: AC+VfDzFxCzHC0MuR4la9GqeLoxdarSA3WLY6Q5qM+JvTIT7nWgA7nwE xD3QkjxeB8DEqfzGtxrhz/CCcyrfOEiolA== X-Google-Smtp-Source: ACHHUZ5rM51rR879vEsYlKXuxPu5OMXL+uv5PXfRnEfGUwZ5Dog0r1JwSpkJ+G7xvnLfqUQypu0Nag== X-Received: by 2002:a4a:2c84:0:b0:565:4308:5943 with SMTP id o126-20020a4a2c84000000b0056543085943mr7633802ooo.1.1688396843315; Mon, 03 Jul 2023 08:07:23 -0700 (PDT) Received: from localhost.localdomain ([204.77.163.107]) by smtp.gmail.com with ESMTPSA id t12-20020a4ad0ac000000b00565ef8db270sm2543382oor.9.2023.07.03.08.07.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jul 2023 08:07:22 -0700 (PDT) From: Joshua Watt X-Google-Original-From: Joshua Watt To: u-boot@lists.denx.de Cc: Joshua Watt , Igor Opaniuk , Sam Protsenko , Simon Glass , Marek Vasut , Patrick Delaunay , Stefan Roese , Rasmus Villemoes , Nikhil M Jain , Heinrich Schuchardt Subject: [PATCH] android_ab: Try backup booloader_message Date: Mon, 3 Jul 2023 10:07:13 -0500 Message-Id: <20230703150716.4117372-1-JPEWhacker@gmail.com> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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.8 at phobos.denx.de X-Virus-Status: Clean Some devices keep 2 copies of the bootloader_message in the misc partition and write each in sequence when updating. This ensures that there is always one valid copy of the bootloader_message. Teach u-boot to optionally try a backup bootloader_message from a specified offset if the primary one fails its CRC check. Signed-off-by: Joshua Watt --- boot/android_ab.c | 77 ++++++++++++++++++++++++++++++++++++++-------- common/Kconfig | 9 ++++++ doc/android/ab.rst | 6 ++++ 3 files changed, 80 insertions(+), 12 deletions(-) diff --git a/boot/android_ab.c b/boot/android_ab.c index 60ae002978..73b55c196c 100644 --- a/boot/android_ab.c +++ b/boot/android_ab.c @@ -85,11 +85,13 @@ static int ab_control_default(struct bootloader_control *abc) */ static int ab_control_create_from_disk(struct blk_desc *dev_desc, const struct disk_partition *part_info, - struct bootloader_control **abc) + struct bootloader_control **abc, + ulong offset) { ulong abc_offset, abc_blocks, ret; - abc_offset = offsetof(struct bootloader_message_ab, slot_suffix); + abc_offset = offset + + offsetof(struct bootloader_message_ab, slot_suffix); if (abc_offset % part_info->blksz) { log_err("ANDROID: Boot control block not block aligned.\n"); return -EINVAL; @@ -135,11 +137,12 @@ static int ab_control_create_from_disk(struct blk_desc *dev_desc, */ static int ab_control_store(struct blk_desc *dev_desc, const struct disk_partition *part_info, - struct bootloader_control *abc) + struct bootloader_control *abc, ulong offset) { ulong abc_offset, abc_blocks, ret; - abc_offset = offsetof(struct bootloader_message_ab, slot_suffix) / + abc_offset = offset + + offsetof(struct bootloader_message_ab, slot_suffix) / part_info->blksz; abc_blocks = DIV_ROUND_UP(sizeof(struct bootloader_control), part_info->blksz); @@ -189,8 +192,11 @@ int ab_select_slot(struct blk_desc *dev_desc, struct disk_partition *part_info, int slot, i, ret; bool store_needed = false; char slot_suffix[4]; +#if ANDROID_AB_BACKUP_OFFSET + struct bootloader_control *backup_abc = NULL; +#endif - ret = ab_control_create_from_disk(dev_desc, part_info, &abc); + ret = ab_control_create_from_disk(dev_desc, part_info, &abc, 0); if (ret < 0) { /* * This condition represents an actual problem with the code or @@ -200,22 +206,53 @@ int ab_select_slot(struct blk_desc *dev_desc, struct disk_partition *part_info, return ret; } +#if ANDROID_AB_BACKUP_OFFSET + ret = ab_control_create_from_disk(dev_desc, part_info, &backup_abc, + ANDROID_AB_BACKUP_OFFSET); + if (ret < 0) { + free(abc); + return ret; + } +#endif + crc32_le = ab_control_compute_crc(abc); if (abc->crc32_le != crc32_le) { log_err("ANDROID: Invalid CRC-32 (expected %.8x, found %.8x),", crc32_le, abc->crc32_le); - log_err("re-initializing A/B metadata.\n"); - - ret = ab_control_default(abc); - if (ret < 0) { - free(abc); - return -ENODATA; +#if ANDROID_AB_BACKUP_OFFSET + crc32_le = ab_control_compute_crc(backup_abc); + if (backup_abc->crc32_le != crc32_le) { + log_err("ANDROID: Invalid backup CRC-32 ") + log_err("expected %.8x, found %.8x),", + crc32_le, backup_abc->crc32_le); +#endif + + log_err("re-initializing A/B metadata.\n"); + + ret = ab_control_default(abc); + if (ret < 0) { +#if ANDROID_AB_BACKUP_OFFSET + free(backup_abc); +#endif + free(abc); + return -ENODATA; + } +#if ANDROID_AB_BACKUP_OFFSET + } else { + /* + * Backup is valid. Copy it to the primary + */ + memcpy(abc, backup_abc, sizeof(*abc)); } +#endif store_needed = true; } if (abc->magic != BOOT_CTRL_MAGIC) { log_err("ANDROID: Unknown A/B metadata: %.8x\n", abc->magic); +#if ANDROID_AB_BACKUP_OFFSET + free(backup_abc); +#endif free(abc); return -ENODATA; } @@ -223,6 +260,9 @@ int ab_select_slot(struct blk_desc *dev_desc, struct disk_partition *part_info, if (abc->version > BOOT_CTRL_VERSION) { log_err("ANDROID: Unsupported A/B metadata version: %.8x\n", abc->version); +#if ANDROID_AB_BACKUP_OFFSET + free(backup_abc); +#endif free(abc); return -ENODATA; } @@ -297,8 +337,21 @@ int ab_select_slot(struct blk_desc *dev_desc, struct disk_partition *part_info, if (store_needed) { abc->crc32_le = ab_control_compute_crc(abc); - ab_control_store(dev_desc, part_info, abc); + ab_control_store(dev_desc, part_info, abc, 0); } + +#if ANDROID_AB_BACKUP_OFFSET + /* + * If the backup doesn't match the primary, write the primary + * to the backup offset + */ + if (memcmp(backup_abc, abc, sizeof(*abc)) != 0) { + ab_control_store(dev_desc, part_info, abc, + ANDROID_AB_BACKUP_OFFSET); + } + free(backup_abc); +#endif + free(abc); if (slot < 0) diff --git a/common/Kconfig b/common/Kconfig index bbabadb35e..ed30bd48cd 100644 --- a/common/Kconfig +++ b/common/Kconfig @@ -935,6 +935,15 @@ config ANDROID_AB allows a bootloader to try a new version of the system but roll back to previous version if the new one didn't boot all the way. +config ANDROID_AB_BACKUP_OFFSET + hex "Offset of backup bootloader control" + depends on ANDROID_AB + default 0x0 + help + If non-zero, a backup bootloader message starting at this offset in + the partition will tried in the event that the primary one (starting + at offset 0) fails its checksum. + endmenu menu "Blob list" diff --git a/doc/android/ab.rst b/doc/android/ab.rst index 961895c32e..2adf88781d 100644 --- a/doc/android/ab.rst +++ b/doc/android/ab.rst @@ -31,6 +31,12 @@ boot script. This command analyzes and processes A/B metadata stored on a special partition (e.g. ``misc``) and determines which slot should be used for booting up. +If the A/B metadata partition has a backup bootloader_message block that is used +to ensure one is always valid even in the event of interruption when writing, it +can be enabled in your board configuration file:: + + CONFIG_ANDROID_AB_BACKUP_OFFSET=0x1000 + Command usage ------------- -- 2.33.0