From patchwork Mon Jul 3 13:25:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?C=C3=A9dric_Le_Goater?= X-Patchwork-Id: 1802764 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Qvn4T70dXz20ZF for ; Mon, 3 Jul 2023 23:35:53 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qGJZZ-0001P0-7S; Mon, 03 Jul 2023 09:26:33 -0400 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 1qGJZW-0001G7-Vp for qemu-devel@nongnu.org; Mon, 03 Jul 2023 09:26:31 -0400 Received: from mail.ozlabs.org ([2404:9400:2221:ea00::3] helo=gandalf.ozlabs.org) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qGJZU-0000tg-KL for qemu-devel@nongnu.org; Mon, 03 Jul 2023 09:26:30 -0400 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by gandalf.ozlabs.org (Postfix) with ESMTP id 4QvmsZ1zNcz4wqZ; Mon, 3 Jul 2023 23:26:26 +1000 (AEST) Received: from authenticated.ozlabs.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mail.ozlabs.org (Postfix) with ESMTPSA id 4QvmsW5xbzz4wZp; Mon, 3 Jul 2023 23:26:23 +1000 (AEST) From: =?utf-8?q?C=C3=A9dric_Le_Goater?= To: qemu-devel@nongnu.org Cc: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Bin Meng , Peter Maydell , Lucien Murray-Pitts , Joel Stanley , =?utf-8?q?C=C3=A9dric_Le_Goater?= Subject: [PATCH 23/32] hw/sd: Support boot area in emmc image Date: Mon, 3 Jul 2023 15:25:00 +0200 Message-ID: <20230703132509.2474225-24-clg@kaod.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230703132509.2474225-1-clg@kaod.org> References: <20230703132509.2474225-1-clg@kaod.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2404:9400:2221:ea00::3; envelope-from=SRS0=mKPa=CV=kaod.org=clg@ozlabs.org; helo=gandalf.ozlabs.org X-Spam_score_int: -39 X-Spam_score: -4.0 X-Spam_bar: ---- X-Spam_report: (-4.0 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Joel Stanley This assumes a specially constructued image: dd if=/dev/zero of=mmc-bootarea.img count=2 bs=1M dd if=u-boot-spl.bin of=mmc-bootarea.img conv=notrunc dd if=u-boot.bin of=mmc-bootarea.img conv=notrunc count=64 bs=1K cat mmc-bootarea.img obmc-phosphor-image.wic > mmc.img truncate --size 16GB mmc.img truncate --size 128MB mmc-bootarea.img For now this still requires a mtd image to load the SPL: qemu-system-arm -M tacoma-bmc -nographic \ -global driver=sd-card,property=emmc,value=true \ -drive file=mmc.img,if=sd,index=2 \ -drive file=mmc-bootarea.img,if=mtd,format=raw Signed-off-by: Joel Stanley [clg: - definition renames - Introduced bootpart_offset - Introduced sd_boot_capacity_bytes() helper (Philippe) ] Signed-off-by: Cédric Le Goater --- include/hw/sd/sd.h | 1 + hw/sd/sd.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/include/hw/sd/sd.h b/include/hw/sd/sd.h index 1565cd2b353c..619f1852384c 100644 --- a/include/hw/sd/sd.h +++ b/include/hw/sd/sd.h @@ -133,6 +133,7 @@ struct SDCardClass { const struct SDProto *proto; void (*set_csd)(SDState *sd, uint64_t size); + uint32_t (*bootpart_offset)(SDState *sd); }; #define TYPE_SD_BUS "sd-bus" diff --git a/hw/sd/sd.c b/hw/sd/sd.c index 212658050441..6da17a8d0972 100644 --- a/hw/sd/sd.c +++ b/hw/sd/sd.c @@ -665,6 +665,12 @@ static inline uint64_t sd_addr_to_wpnum(uint64_t addr) return addr >> (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT); } + +static unsigned sd_boot_capacity_bytes(SDState *sd) +{ + return sd->ext_csd[EXT_CSD_BOOT_MULT] << 17; +} + static void sd_reset(DeviceState *dev) { SDState *sd = SD_CARD(dev); @@ -862,9 +868,40 @@ void sd_set_cb(SDState *sd, qemu_irq readonly, qemu_irq insert) qemu_set_irq(insert, sd->blk ? blk_is_inserted(sd->blk) : 0); } +/* + * This requires a disk image that has two boot partitions inserted at the + * beginning of it. The size of the boot partitions are configured in the + * ext_csd structure, which is hardcoded in qemu. They are currently set to + * 1MB each. + */ +static uint32_t sd_emmc_bootpart_offset(SDState *sd) +{ + unsigned int access = sd->ext_csd[EXT_CSD_PART_CONFIG] & + EXT_CSD_PART_CONFIG_ACC_MASK; + unsigned int boot_capacity = sd_boot_capacity_bytes(sd); + + switch (access) { + case EXT_CSD_PART_CONFIG_ACC_DEFAULT: + return boot_capacity * 2; + case EXT_CSD_PART_CONFIG_ACC_BOOT0: + return 0; + case EXT_CSD_PART_CONFIG_ACC_BOOT0 + 1: + return boot_capacity * 1; + default: + g_assert_not_reached(); + } +} + +static uint32_t sd_bootpart_offset(SDState *sd) +{ + SDCardClass *sc = SD_CARD_GET_CLASS(sd); + return sc->bootpart_offset ? sc->bootpart_offset(sd) : 0; +} + static void sd_blk_read(SDState *sd, uint64_t addr, uint32_t len) { trace_sdcard_read_block(addr, len); + addr += sd_bootpart_offset(sd); if (!sd->blk || blk_pread(sd->blk, addr, len, sd->data, 0) < 0) { fprintf(stderr, "sd_blk_read: read error on host side\n"); } @@ -873,6 +910,7 @@ static void sd_blk_read(SDState *sd, uint64_t addr, uint32_t len) static void sd_blk_write(SDState *sd, uint64_t addr, uint32_t len) { trace_sdcard_write_block(addr, len); + addr += sd_bootpart_offset(sd); if (!sd->blk || blk_pwrite(sd->blk, addr, len, sd->data, 0) < 0) { fprintf(stderr, "sd_blk_write: write error on host side\n"); } @@ -2597,6 +2635,7 @@ static void emmc_class_init(ObjectClass *klass, void *data) dc->realize = emmc_realize; sc->proto = &sd_proto_emmc; sc->set_csd = sd_emmc_set_csd; + sc->bootpart_offset = sd_emmc_bootpart_offset; } static const TypeInfo emmc_info = {