From patchwork Tue Oct 15 18:43:42 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heinrich Schuchardt X-Patchwork-Id: 1177305 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gmx.de Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; secure) header.d=gmx.net header.i=@gmx.net header.b="hkKS8Whp"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 46t48b5thXz9sNx for ; Wed, 16 Oct 2019 05:44:13 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 9F3F7C21DEC; Tue, 15 Oct 2019 18:44:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=FREEMAIL_FROM, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 2946EC21C29; Tue, 15 Oct 2019 18:44:05 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id EAEC4C21C29; Tue, 15 Oct 2019 18:44:03 +0000 (UTC) Received: from mout.gmx.net (mout.gmx.net [212.227.15.19]) by lists.denx.de (Postfix) with ESMTPS id 9CECCC21BE5 for ; Tue, 15 Oct 2019 18:44:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1571165038; bh=x+sYpJY769y9iRREfNxEYnBohilTzcnvlwZ7tYSscyA=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date; b=hkKS8WhpNnsXrvo53GIALBnU+hnWdZ66kxmIEcFkdkIKtEjqio8AzUqUyZfnNnvWy isz2VygiNXZsv+aE6zYK3lrVrki5W85BedrNNQUqrLZYsXO+FF0geZ3OXZh5GQ5DAz RHU53I/XafprqaCMi6fUk5HlDUkMtpwN2kCi+Jxc= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from LT02.fritz.box ([95.222.45.45]) by mail.gmx.com (mrgmx005 [212.227.17.184]) with ESMTPSA (Nemesis) id 1MvsEn-1i1gID0Tt1-00szPB; Tue, 15 Oct 2019 20:43:58 +0200 From: Heinrich Schuchardt To: Tom Rini Date: Tue, 15 Oct 2019 20:43:42 +0200 Message-Id: <20191015184342.10530-1-xypron.glpk@gmx.de> X-Mailer: git-send-email 2.23.0 MIME-Version: 1.0 X-Provags-ID: V03:K1:eSLRnkb6TTTekCZtqwKy6oevYOuG3Mep9umkUDY8qK8Zn2RtJOa ZteVcVy5CKUkYYyi8fbGq4vF4tEynS1fPNk/MZ+TGhIKY4NMGeWpv/oO/q/GPjWh0dbje2R 4qMAuGp+VGNYpopBVxHCaDbF2TRVXuK3X9ospwv75ichANgZJZ+jMFY5JLk30BfVazoIsst QqaTIdYepEeOoVIBpHlmA== X-UI-Out-Filterresults: notjunk:1; V03:K0:oEVZDw1uILY=:uOK2f52OOUp3/r9gNpKsqG 6cq9qNr4xM2cOp/wdWAlv7j7qM1SKqoE9ZI8P7Oi2Ue1ZXKAuRp0MgdR52s9Ny7gTd75TJ9jv wh+gQKZHbWjq2Sk2ZsobeIuZPN6SsbYBwvYB6ngfpdh5tHN9DUJMAHKcXPiBFJ3ZM4hDInbKx 2BSGdyCMgBb31J1Sg5Ky5bqLUEIygM2mzPbhEBqOm2G51Eh+r+Dl5DfWATUZKsaoSWIpQG4pR SIqouea/8wFn+r857iqFZFjORKfd4me+dIlrmjntJgV2i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Emge Subject: [U-Boot] [PATCH 1/1] disk: part_dos: correctly detect DOS PBR X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 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" The signature 0x55 0xAA in bytes 510 and 511 of the first sector can either indicate a DOS partition table of the first sector of a FAT file system. The current code tries to check if the partition table is valid by looking at the boot indicator of the partition entries. But first of all it does not count from 0 to 3 but only from 0 to 2. And second it misses to increment the pointer for the partition entry. If it is a FAT file system can be discovered by looking for the text 'FAT' at offset 0x36 or 'FAT32' at offset 0x52. In a DOS PBR there are no partition entries, so those bytes are undefined. Don't require the byte at offset 0x1BE to differ from 0x00 and 0x80. With the patch the logic is changed as follows: If the partition table has either an invalid boot flag for any partition or has no partition at all, check if the first sector is a DOS PBR by looking at the FAT* signature. Signed-off-by: Heinrich Schuchardt --- disk/part_dos.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) -- 2.23.0 diff --git a/disk/part_dos.c b/disk/part_dos.c index 8ddc13b50c..83ff40d310 100644 --- a/disk/part_dos.c +++ b/disk/part_dos.c @@ -67,28 +67,39 @@ static int test_block_type(unsigned char *buffer) { int slot; struct dos_partition *p; + int part_count = 0; if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) || (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) { return (-1); } /* no DOS Signature at all */ p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET]; - for (slot = 0; slot < 3; slot++) { - if (p->boot_ind != 0 && p->boot_ind != 0x80) { - if (!slot && - (strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET], - "FAT", 3) == 0 || - strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET], - "FAT32", 5) == 0)) { - return DOS_PBR; /* is PBR */ - } else { - return -1; - } - } + + /* Check that the boot indicators are valid and count the partitions. */ + for (slot = 0; slot < 4; ++slot, ++p) { + if (p->boot_ind != 0 && p->boot_ind != 0x80) + break; + if (p->sys_ind) + ++part_count; } - return DOS_MBR; /* Is MBR */ -} + /* + * If the partition table is invalid or empty, + * check if this is a DOS PBR + */ + if (slot != 4 || !part_count) { + if (!strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET], + "FAT", 3) || + !strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET], + "FAT32", 5)) + return DOS_PBR; /* This is a DOS PBR and not an MBR */ + } + if (slot == 4) + return DOS_MBR; /* This is an DOS MBR */ + + /* This is neither a DOS MBR nor a DOS PBR */ + return -1; +} static int part_test_dos(struct blk_desc *dev_desc) {