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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7A57FD46C1D for ; Sat, 31 Jan 2026 00:56:30 +0000 (UTC) Received: from kara.freedesktop.org (unknown [131.252.210.166]) by gabe.freedesktop.org (Postfix) with ESMTPS id 5F08310E971; Sat, 31 Jan 2026 00:56:30 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.b="jUKPMPo/"; dkim-atps=neutral Received: from kara.freedesktop.org (localhost [127.0.0.1]) by kara.freedesktop.org (Postfix) with ESMTP id A391941B0A; Sat, 31 Jan 2026 00:47:22 +0000 (UTC) ARC-Seal: i=2; cv=pass; a=rsa-sha256; d=lists.freedesktop.org; s=20240201; t=1769820442; b=pJ0N4DbDXo/kBqM7MyO6OZZpBfXY8AIJWYQRy6xkGeEMaDJue6/oYQY0jAPaTlyKvCRXo Hw7HVZukCXpSblzrX+Ir39K1BhAoLzvvLNuT1z41q8nhwrnWB3vIaWYbe6OIvIGd1iBQHDf Wbo3CbbX+JS4N7GGuecLO+joFkojXdHRP+he0WJupb7yj7y0AD2ZQdkbTXES9UCumZVp5WP BMapj7v7S1K4EfaKl2BrssL6znXy4b+2UgK/9p7fsROJeMJgCjDeygpQUojXzOcNzdUf5bt t/XYYWHcWmUFKufXu4ez323eyMOqBp6tJkCT2hipkMP3LeKdnr3/1VpTllDQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=lists.freedesktop.org; s=20240201; t=1769820442; h=from : sender : reply-to : subject : date : message-id : to : cc : mime-version : content-type : content-transfer-encoding : content-id : content-description : resent-date : resent-from : resent-sender : resent-to : resent-cc : resent-message-id : in-reply-to : references : list-id : list-help : list-unsubscribe : list-subscribe : list-post : list-owner : list-archive; bh=2DohWhvVmvtq8FzQqlSwBfVvo/5AOAJNi9yyjB25P2Q=; b=pR2+gqyQfryiGHAUdJXUANsVd0E+XUm1nOCy8lZF8kldJ3I7epuUbU/xralyz9sln9iN2 +xrgr5fhwmELbWG/73xQKlNJ2trLuZoFlZB5k6pa9uv7Gz2zgdoOjEb6LzF3EiEWnOeBO74 PU9rXeNqNcSV/mBBGzkxE9YA3WsroY+qULNaiiwpdLNwpWePgt+ji1HQazDAXoMFwSoPZ+t rUCUzbbv6XQ7eHvSotr4ITAJ4dNDny2cWvsY0lP5jT/rnNJbfButbOP9L6oFTmxZMoc8g8k eMMHI5ToKa35H8ON50sGpKhu40HzCbwGLxH7q7YroLscFz0tPfrFyzAscvyw== ARC-Authentication-Results: i=2; mail.freedesktop.org; dkim=pass header.d=Nvidia.com; arc=pass; dmarc=pass (Used From Domain Record) header.from=nvidia.com policy.dmarc=reject Authentication-Results: mail.freedesktop.org; dkim=pass header.d=Nvidia.com; arc=pass; dmarc=pass (Used From Domain Record) header.from=nvidia.com policy.dmarc=reject Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by kara.freedesktop.org (Postfix) with ESMTPS id 9B2B540F63 for ; Sat, 31 Jan 2026 00:47:19 +0000 (UTC) Received: from CH1PR05CU001.outbound.protection.outlook.com (mail-northcentralusazon11010003.outbound.protection.outlook.com [52.101.193.3]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0F70A10E109 for ; Sat, 31 Jan 2026 00:56:27 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=tcroE8stxlWH+TnHKhLiyIHHelKJtBCtKG+JGth9XXlkTLVaX0K0GO/62C7ZletTTucdSezD0gUOVsXhLSnIKvoi9UogTEDg4nX5nBiLCQxzaEFEFSi3bGaIM0gGHijJPdspBLsfXdcvEwOcyikB5SDJAQTyiWYo68PBya4CN/QtezH8PhKsaRtZO2fnlHPHyfA16T3EapC2qfPXQC6oomqzfjEi+oJKL34EnALFy1peOEK6MFWhXhC0tPvHrRnAOwwQD0dv4Ku6V71jOKmn+OfKkqABGGrT0j1N7PjedaA1foABN52tP86Rbiht8fp0dbPZb0JE0t6qdtCRudNkaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=2DohWhvVmvtq8FzQqlSwBfVvo/5AOAJNi9yyjB25P2Q=; b=sKN1TigfnPdVQ1GN5FGk+pIiAh7SjrCCL5ING7iq41pSyHabQFuRmCbYNjle8hy6OfHw+wIAi46uZHnqLAjWzsgQrK4+UlbWEky5YIVfqLwFvCEuNff5DZP2VLbnjrVYaeWDK3MpDQsFoYsshoRn2psuGsV+TVpjr+DVZXuC12EWP+Hdqq1Yu2xvOvOkzeToQ6C87f1rW0sVvBcCjIkKEW6XOpzrlZzDfPMyWhtRTuHmeO98Wc9B8/lrwh7SbCiXyQ7EsY1vu3fs0bhenLoh0gljBsiWl8DiMeb/BAi8P7K/1C+2taw+kP61+yKFU+7HJUCVRyFkI2uCgukccVLhOg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=2DohWhvVmvtq8FzQqlSwBfVvo/5AOAJNi9yyjB25P2Q=; b=jUKPMPo/FQ9O88YMkV6bm7aTROzDE+ZIfeHGiz00IgE32XLbqgtOQoWCblPwXD0n09xKBQdSKae0WeKHASmtKiA7JMkIRtVYVkRFtj9DKMXec0dB5pHUDaMXH7DeZ/1O//q0wmPgR300cZgI4dFY0byh/2cQ3vsDCOSdbC9lYYXFMqIahLJW+ozP8UmKNIIYXgrpcvkGP008cXyCR5F/jWdj+mzdbyHOujqpmgxGTuMrH1P7sGdPTUc4GF13ikyGUX/EbhbYT4XBFTotk1fWfWDuE5WXvMdg0qbJzxBcGhBg59yDVmZAwj/sTyb0ZwVPCeG61MMoS3ezoI9JeLHeZQ== Received: from DM3PR12MB9416.namprd12.prod.outlook.com (2603:10b6:0:4b::8) by BL1PR12MB5945.namprd12.prod.outlook.com (2603:10b6:208:398::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9564.11; Sat, 31 Jan 2026 00:56:23 +0000 Received: from DM3PR12MB9416.namprd12.prod.outlook.com ([fe80::8cdd:504c:7d2a:59c8]) by DM3PR12MB9416.namprd12.prod.outlook.com ([fe80::8cdd:504c:7d2a:59c8%7]) with mapi id 15.20.9564.007; Sat, 31 Jan 2026 00:56:23 +0000 From: John Hubbard To: Danilo Krummrich Subject: [PATCH v2 11/30] gpu: nova-core: don't assume 64-bit firmware images Date: Fri, 30 Jan 2026 16:55:45 -0800 Message-ID: <20260131005604.454172-12-jhubbard@nvidia.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260131005604.454172-1-jhubbard@nvidia.com> References: <20260131005604.454172-1-jhubbard@nvidia.com> X-NVConfidentiality: public Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: BYAPR05CA0005.namprd05.prod.outlook.com (2603:10b6:a03:c0::18) To DM3PR12MB9416.namprd12.prod.outlook.com (2603:10b6:0:4b::8) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM3PR12MB9416:EE_|BL1PR12MB5945:EE_ X-MS-Office365-Filtering-Correlation-Id: 9d54dd68-f8f3-4e6e-455e-08de60638ddc X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|1800799024|366016|7416014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?ci4tJBKd9kb/0qlrmTDnfOnS5xtc5kxFKFvf0/Pgqsm5mWAaRlVAQTg+squx?= =?us-ascii?Q?heYMRbD2Gf2dhmnS5eTJ3vXQejxFqGdvJXb9K0VkTV9Ljhq/BlvLbvZSCl9R?= =?us-ascii?Q?o8LCJzZNIWqOHRpNqO9q3LAGqlhRG6383ooQsIvYhUhYp27bIxv20/6tHczF?= =?us-ascii?Q?frb5ZnlpHN0rjXZ4edOizTxnzV/8WzQDYkkhQ8CavEqEpswYSHbKuA+R5HGV?= =?us-ascii?Q?ClmT8eVW7szV0wiB8N5UaWO8wOvl/Nh86sPOqLixJs0mgwz4M0y3chs1Pl53?= =?us-ascii?Q?VAgdQ4QHzJHEQhdZov3VclHg2Ztt0Vry7xS6AGHM74BgPBFK0P/6tUFMh1zc?= =?us-ascii?Q?+b/HYcfC5zU1mF3EAbGBmgh3QTZU2qOuSw3duJAVPYNLVgm7OWICUJR6yvhk?= =?us-ascii?Q?d2nZANebYewG2+koysVjVWF/A5AqpLQPYLzG9dH/vWAlzIp9V+T+UpTKH6hs?= =?us-ascii?Q?SWneEf/zJCKi/kfT7Cu+1jrgS6pkiZWGU/b50RH/vH6ZG57az4rp53hsLHa/?= =?us-ascii?Q?x6HoByu7hHnJc/rQ/sKhn4oL9PiOU/f7E4jdzo4FVeeuUpq5IXaUckgEOIpA?= =?us-ascii?Q?/YrmwIzK7uWtvPEONJ/cD204XqeZ+BT84ocBUYrVn0S7cl0RvYTno4OljcML?= =?us-ascii?Q?qBr5LaZKjP/VxfnVUGy6qmx2hzLMKO0uGEjdZY4SKdcRCIewZhB7NgsHMuiM?= =?us-ascii?Q?RtUoiM2c/dLxD8Itfo3QsXzmYgLvn2L7OtBVk/2jmY5lhFXX9V/ot5LAA7Tk?= =?us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9416.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(376014)(1800799024)(366016)(7416014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?IXtO++L1KIntai7jDonA/B7p6ARX5h5djaIQc+VsCE/g6CfRfDreXUE5ogoD?= =?us-ascii?Q?8QxhjAxmWZOBFMgze22qmP7ofrkyS/jHOGf4nVI4sTKmRWGsfQf3frvNhrHg?= =?us-ascii?Q?uu0KZZgK+RhaJMurXhjK4X/tm808XZ8ygNJvZdt3uK1X7Q8Fvu+CNLrdndEd?= =?us-ascii?Q?MUcmSvBFw6vuWZAqHxAMYiHT+Gsn0B3srYtZoTlkeQQCBQgji6CyIXv+4fJv?= =?us-ascii?Q?XTR8BCljODQj8n8PjNNNAEOvqoP5msF42v1hnXRK3F03gCJg1oE0Nfy3hucu?= =?us-ascii?Q?ObN0nCHn2vmYr40xNpOxqz0uxhe76wqIszsVHBUF3Td3Q7TdY5oooxCf82hx?= =?us-ascii?Q?UA/Mq761wpADYuAOnzwYnDmaq9oyAj1VU3oT8VI08m5zl3TXQ+PMoaFbELGa?= =?us-ascii?Q?ranK5cEdBerXvfcrMvkv3+y1arMT/gYheh19OhuNwQ0QAOrrQvFU+0vD4rmJ?= =?us-ascii?Q?u2GspYm6UZsiPOP9+0w9xB/+RtY1KjUuFltD+aGB0r7yPNhd49VK8uHJarw6?= =?us-ascii?Q?TzdpT7Jm9im+UP0qAVXYFgCsxAVqy4eAwItUrwdv4s9yNucMBsd57L3k8eaW?= =?us-ascii?Q?BlDTvEkl/GErHNN1NNr3CJV6AaxvHw5TV0XM0u1UNS6Y4kEfDoMLH8QdgRhl?= =?us-ascii?Q?2zfyo4xzrR8ALNlllgHUQ6glFVnUo4inANxX9zxNaT970wTl36hce+QwF5d9?= =?us-ascii?Q?wkEt8XVR+AQWYAbX6sw3k6THrGsBNVOq0AbkP45pgWz9Ip2X+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imf+Kv/YNyZ7x5CupmjuoohI0fm9lKKnGB0CKUm4Gbl?= =?us-ascii?Q?l2Z6ipqlPiONG+n236vj0riYiliJjmaJ/q3WzL/fAumAtpVvrxoxxomLDcV7?= =?us-ascii?Q?x4SSRJpAVQ=3D=3D?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9d54dd68-f8f3-4e6e-455e-08de60638ddc X-MS-Exchange-CrossTenant-AuthSource: DM3PR12MB9416.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Jan 2026 00:56:23.2528 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: SMHSLs8FWho4AAoMGudHnsvWCFFRzNJ4j4ElRbundhlG41VgUeMBIGtqF+Gs5NERx2re5voI3smlZx95AdC/VQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5945 Message-ID-Hash: Z467DCUN6EQ2QACCZX7SEKI5PMKDMKWT X-Message-ID-Hash: Z467DCUN6EQ2QACCZX7SEKI5PMKDMKWT X-MailFrom: jhubbard@nvidia.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Alexandre Courbot , Joel Fernandes , Alistair Popple , Eliot Courtney , Zhi Wang , Simona Vetter , Bjorn Helgaas , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , nouveau@lists.freedesktop.org, rust-for-linux@vger.kernel.org, LKML X-Mailman-Version: 3.3.8 Precedence: list List-Id: Nouveau development list Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Add ElfHeader and ElfSectionHeader traits to abstract out differences between ELF32 and ELF64. Implement these for ELF64. This is in preparation for upcoming ELF32 section support, and for auto-selecting ELF32 or ELF64. Signed-off-by: John Hubbard --- drivers/gpu/nova-core/firmware.rs | 99 ++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 27 deletions(-) diff --git a/drivers/gpu/nova-core/firmware.rs b/drivers/gpu/nova-core/firmware.rs index 72cefc3142ea..6ed76a7e15f1 100644 --- a/drivers/gpu/nova-core/firmware.rs +++ b/drivers/gpu/nova-core/firmware.rs @@ -453,17 +453,60 @@ mod elf { transmute::FromBytes, // }; - /// Newtype to provide a [`FromBytes`] implementation. + /// Trait to abstract over ELF header differences (32-bit vs 64-bit). + trait ElfHeader: FromBytes { + fn shnum(&self) -> u16; + fn shoff(&self) -> u64; + fn shstrndx(&self) -> u16; + } + + /// Trait to abstract over ELF section header differences (32-bit vs 64-bit). + trait ElfSectionHeader: FromBytes { + fn name(&self) -> u32; + fn offset(&self) -> u64; + fn size(&self) -> u64; + } + + /// Newtype to provide [`FromBytes`] and [`ElfHeader`] implementations. #[repr(transparent)] struct Elf64Hdr(bindings::elf64_hdr); // SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability. unsafe impl FromBytes for Elf64Hdr {} + impl ElfHeader for Elf64Hdr { + fn shnum(&self) -> u16 { + self.0.e_shnum + } + + fn shoff(&self) -> u64 { + self.0.e_shoff + } + + fn shstrndx(&self) -> u16 { + self.0.e_shstrndx + } + } + + /// Newtype to provide [`FromBytes`] and [`ElfSectionHeader`] implementations. #[repr(transparent)] struct Elf64SHdr(bindings::elf64_shdr); // SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability. unsafe impl FromBytes for Elf64SHdr {} + impl ElfSectionHeader for Elf64SHdr { + fn name(&self) -> u32 { + self.0.sh_name + } + + fn offset(&self) -> u64 { + self.0.sh_offset + } + + fn size(&self) -> u64 { + self.0.sh_size + } + } + /// Returns a NULL-terminated string from the ELF image at `offset`. fn elf_str(elf: &[u8], offset: u64) -> Option<&str> { let idx = usize::try_from(offset).ok()?; @@ -471,47 +514,49 @@ fn elf_str(elf: &[u8], offset: u64) -> Option<&str> { CStr::from_bytes_until_nul(bytes).ok()?.to_str().ok() } - /// Tries to extract section with name `name` from the ELF64 image `elf`, and returns it. - pub(super) fn elf64_section<'a, 'b>(elf: &'a [u8], name: &'b str) -> Option<&'a [u8]> { - let hdr = &elf - .get(0..size_of::()) - .and_then(Elf64Hdr::from_bytes)? - .0; + fn elf_section_generic<'a, H, S>(elf: &'a [u8], name: &str) -> Option<&'a [u8]> + where + H: ElfHeader, + S: ElfSectionHeader, + { + let hdr = H::from_bytes(elf.get(0..size_of::())?)?; - // Get all the section headers. - let mut shdr = { - let shdr_num = usize::from(hdr.e_shnum); - let shdr_start = usize::try_from(hdr.e_shoff).ok()?; - let shdr_end = shdr_num - .checked_mul(size_of::()) - .and_then(|v| v.checked_add(shdr_start))?; + let shdr_num = usize::from(hdr.shnum()); + let shdr_start = usize::try_from(hdr.shoff()).ok()?; + let shdr_end = shdr_num + .checked_mul(size_of::()) + .and_then(|v| v.checked_add(shdr_start))?; - elf.get(shdr_start..shdr_end) - .map(|slice| slice.chunks_exact(size_of::()))? - }; + // Get all the section headers as an iterator over byte chunks. + let shdr_bytes = elf.get(shdr_start..shdr_end)?; + let mut shdr_iter = shdr_bytes.chunks_exact(size_of::()); // Get the strings table. - let strhdr = shdr + let strhdr = shdr_iter .clone() - .nth(usize::from(hdr.e_shstrndx)) - .and_then(Elf64SHdr::from_bytes)?; + .nth(usize::from(hdr.shstrndx())) + .and_then(S::from_bytes)?; // Find the section which name matches `name` and return it. - shdr.find_map(|sh| { - let hdr = Elf64SHdr::from_bytes(sh)?; - let name_offset = strhdr.0.sh_offset.checked_add(u64::from(hdr.0.sh_name))?; + shdr_iter.find_map(|sh_bytes| { + let sh = S::from_bytes(sh_bytes)?; + let name_offset = strhdr.offset().checked_add(u64::from(sh.name()))?; let section_name = elf_str(elf, name_offset)?; if section_name != name { return None; } - let start = usize::try_from(hdr.0.sh_offset).ok()?; - let end = usize::try_from(hdr.0.sh_size) + let start = usize::try_from(sh.offset()).ok()?; + let end = usize::try_from(sh.size()) .ok() - .and_then(|sh_size| start.checked_add(sh_size))?; - + .and_then(|sz| start.checked_add(sz))?; elf.get(start..end) }) } + + /// Extract the section with name `name` from the ELF64 image `elf`. + pub(super) fn elf64_section<'a>(elf: &'a [u8], name: &str) -> Option<&'a [u8]> { + elf_section_generic::(elf, name) + } } -- 2.52.0