From nobody Thu Oct 30 15:22:16 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1525450936127166.67481442894234; Fri, 4 May 2018 09:22:16 -0700 (PDT) Received: from localhost ([::1]:35230 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fEdTH-00051p-6g for importer@patchew.org; Fri, 04 May 2018 12:22:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55276) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fEd8E-0003NR-3M for qemu-devel@nongnu.org; Fri, 04 May 2018 12:00:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fEd88-0001r7-C0 for qemu-devel@nongnu.org; Fri, 04 May 2018 12:00:26 -0400 Received: from mail-qk0-x241.google.com ([2607:f8b0:400d:c09::241]:44545) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fEd88-0001r0-7b for qemu-devel@nongnu.org; Fri, 04 May 2018 12:00:20 -0400 Received: by mail-qk0-x241.google.com with SMTP id z8so16989932qki.11 for ; Fri, 04 May 2018 09:00:20 -0700 (PDT) Received: from x1.local ([138.117.48.222]) by smtp.gmail.com with ESMTPSA id a83sm8587066qkb.22.2018.05.04.09.00.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 May 2018 09:00:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LXw6Y46Gbl1DJe7pGpYk8xOwyPzlejaO/V3ePgy56BY=; b=I87c/fjeS/6KtR6EJerga7pvZQL81yh832csfvXlekAfkD5IXQbgYoIDPCm7Dw/CEZ krWDdpWy+2KgMN5GFqjWIlr8J7PtSZCynXJHvHTE4agosRPOM09LQvgRKkwis+N19yIe 0ybtczTlXGfY++J3siz6Z9/ceZgFhuTSln4/h0WQZyl+h+egN9X2mfw/0gVDLGyq5Ia5 5fl2/bOHEXWCiWGTXP6JCgplOK8wnhD+5slYuVYsoZRBNqA7Cg0gqt5X48KZz3+IIJ+G dHKxrzHd4GMz2/KafSYZPEVug4xzHiSZuQMvGaXcBPA03Z8yXXIIYA8pP6hHVApHDQgq afAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=LXw6Y46Gbl1DJe7pGpYk8xOwyPzlejaO/V3ePgy56BY=; b=M7rSkD15F6AQGpYCXVDyHgibzeZGlbFq5VUh3cY4IFiX1SAXs5K/RUFTe6KSjQSKCe fiijgSw6N24Cid/RWjS8RqGg5N3FgTMUy5BOtSDZZJmTE0tmzGYEQerFWpj8WctpWlQ3 qWG7fiKZGTJLKKW+1TFvlzumsnwwq+HXriJqxe8BpranQTS6WWGrw0eIDLgHYadIUAIi yXJ3VrIkW4XkNhbsLzk734ui4eavXTx09igqk+K6LgaSAjRsOmJhRP5gJIaqa549A39C JOWda/7Fyxt6sSjYKE/9Y9HEw+XPfu8t/H773J+mSqiLIgdEwC0OEzIsb1H6CKOjsXN1 r5XQ== X-Gm-Message-State: ALQs6tBf8z7bRmKUeJ91tN/aOoSb5KDiG4s1MJ3UFwpmeDfnJQxLtx1T ZJUslpSxXBBf2DT0Gnt3Uvv+29Ky X-Google-Smtp-Source: AB8JxZrFKHAztdwV2Ec/18A8pI7NSUd5ltUNuogKQyRCwu/IJK6bdWloWzd5r6nIq7q92scgoQBaIw== X-Received: by 10.55.31.199 with SMTP id n68mr22904671qkh.418.1525449619581; Fri, 04 May 2018 09:00:19 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: Peter Maydell , "Edgar E . Iglesias" Date: Fri, 4 May 2018 12:59:16 -0300 Message-Id: <20180504155918.21287-19-f4bug@amsat.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180504155918.21287-1-f4bug@amsat.org> References: <20180504155918.21287-1-f4bug@amsat.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400d:c09::241 Subject: [Qemu-devel] [PATCH 18/20] sdcard: Fix sd_crc16() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Alistair Francis , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Stefan Hajnoczi , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 The current sd_crc16() function does not pass the Spec test, change it by a working one. Code generated with pycrc v0.9.1 (https://pycrc.org) using the configuratio= n: - Width =3D 16 - Poly =3D 0x1021 - XorIn =3D 0x0000 - ReflectIn =3D False - XorOut =3D 0x0000 - ReflectOut =3D False - Algorithm =3D bit-by-bit-fast Copyright of the generated source code =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D The code generated by pycrc is not considered a substantial portion of the software, therefore the licence does not cover the generated code, and the author of pycrc will not claim any copyright on the generated code. Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- hw/sd/sdmmc-internal.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/hw/sd/sdmmc-internal.c b/hw/sd/sdmmc-internal.c index 0e82e69d99..d6f9b3b51c 100644 --- a/hw/sd/sdmmc-internal.c +++ b/hw/sd/sdmmc-internal.c @@ -90,23 +90,31 @@ uint8_t sd_crc7(const void *message, size_t width) return shift_reg; } =20 -uint16_t sd_crc16(const void *message, size_t width) +/* 16 bit XMODEM CRC (polynomial 0x1021) */ +uint16_t sd_crc16(const void *data, size_t data_len) { - int i, bit; - uint16_t shift_reg =3D 0x0000; - const uint16_t *msg =3D (const uint16_t *)message; - width <<=3D 1; - - for (i =3D 0; i < width; i++, msg++) { - for (bit =3D 15; bit >=3D 0; bit--) { - shift_reg <<=3D 1; - if ((shift_reg >> 15) ^ ((*msg >> bit) & 1)) { - shift_reg ^=3D 0x1011; + const unsigned char *d =3D (const unsigned char *)data; + uint16_t crc =3D 0x0000; + unsigned char c; + unsigned int i; + bool bit; + + while (data_len--) { + c =3D *d++; + for (i =3D 0x80; i > 0; i >>=3D 1) { + bit =3D crc & 0x8000; + if (c & i) { + bit =3D !bit; + } + crc <<=3D 1; + if (bit) { + crc ^=3D 0x1021; } } + crc &=3D 0xffff; } =20 - return shift_reg; + return crc; } =20 static uint8_t sd_calc_frame48_crc7(uint8_t cmd, uint32_t arg, bool is_res= ponse) --=20 2.17.0