From patchwork Tue Apr 12 19:23:58 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergei Shtylyov X-Patchwork-Id: 90854 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id DA5FFB6F3A for ; Wed, 13 Apr 2011 05:32:34 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 9939B281F5; Tue, 12 Apr 2011 21:32:33 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id WiLrwhoGwsci; Tue, 12 Apr 2011 21:32:33 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 0C1BD281D0; Tue, 12 Apr 2011 21:32:31 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 20F04281D0 for ; Tue, 12 Apr 2011 21:32:28 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id WJ6bL4olZvCu for ; Tue, 12 Apr 2011 21:32:27 +0200 (CEST) X-Greylist: delayed 401 seconds by postgrey-1.27 at theia; Tue, 12 Apr 2011 21:32:25 CEST X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail.dev.rtsoft.ru (mail.dev.rtsoft.ru [213.79.90.226]) by theia.denx.de (Postfix) with SMTP id 06A81281CE for ; Tue, 12 Apr 2011 21:32:25 +0200 (CEST) Received: (qmail 8413 invoked from network); 12 Apr 2011 19:25:44 -0000 Received: from unknown (HELO wasted.dev.rtsoft.ru) (192.168.1.70) by 0 with SMTP; 12 Apr 2011 19:25:44 -0000 To: u-boot@lists.denx.de Content-Disposition: inline From: Sergei Shtylyov Organization: MontaVista Software Inc. Date: Tue, 12 Apr 2011 23:23:58 +0400 MIME-Version: 1.0 Message-Id: <201104122323.59105.sshtylyov@ru.mvista.com> Subject: [U-Boot] [PATCH] part_dos: allocate sector buffer dynamically X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.9 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de Apple iPod nanos have sector sizes of 2 or 4 KiB, which crashes U-Boot when it tries to read the MBR into 512-byte buffer situated on stack. Instead allocate this buffer dynamically to be safe with any large sector size. Signed-off-by: Sergei Shtylyov --- The same change is probably needed for disk/part_amiga.c but I'm not really sure if Amiga supports USB... :-) disk/part_dos.c | 59 +++++++++++++++++++++++++++++++++++++++----------------- disk/part_dos.h | 7 ------ 2 files changed, 42 insertions(+), 24 deletions(-) Index: u-boot/disk/part_dos.c =================================================================== --- u-boot.orig/disk/part_dos.c +++ u-boot/disk/part_dos.c @@ -32,6 +32,7 @@ #include #include +#include #include #include "part_dos.h" @@ -87,14 +88,20 @@ static int test_block_type(unsigned char int test_part_dos (block_dev_desc_t *dev_desc) { - unsigned char buffer[DEFAULT_SECTOR_SIZE]; + unsigned char *buffer; + + buffer = malloc(dev_desc->blksz); + if (!buffer) + return -1; if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1) || (buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) || (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) { - return (-1); + free(buffer); + return -1; } - return (0); + free(buffer); + return 0; } /* Print a partition that is relative to its Extended partition table @@ -102,26 +109,32 @@ int test_part_dos (block_dev_desc_t *dev static void print_partition_extended (block_dev_desc_t *dev_desc, int ext_part_sector, int relative, int part_num) { - unsigned char buffer[DEFAULT_SECTOR_SIZE]; + unsigned char *buffer; dos_partition_t *pt; int i; + buffer = malloc(dev_desc->blksz); + if (!buffer) { + printf ("** Can't allocate buffer **\n"); + return; + } + if (dev_desc->block_read(dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) { printf ("** Can't read partition table on %d:%d **\n", dev_desc->dev, ext_part_sector); - return; + goto exit; } i=test_block_type(buffer); if(i==-1) { printf ("bad MBR sector signature 0x%02x%02x\n", buffer[DOS_PART_MAGIC_OFFSET], buffer[DOS_PART_MAGIC_OFFSET + 1]); - return; + goto exit; } if(i==DOS_PBR) { printf (" 1\t\t 0\t%10ld\t%2x\n", dev_desc->lba, buffer[DOS_PBR_MEDIA_TYPE_OFFSET]); - return; + goto exit; } /* Print all primary/logical partitions */ pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); @@ -156,7 +169,8 @@ static void print_partition_extended (bl } } - return; +exit: + free(buffer); } @@ -166,21 +180,27 @@ static int get_partition_info_extended ( int relative, int part_num, int which_part, disk_partition_t *info) { - unsigned char buffer[DEFAULT_SECTOR_SIZE]; + unsigned char *buffer; dos_partition_t *pt; - int i; + int i, result = -1; + + buffer = malloc(dev_desc->blksz); + if (!buffer) { + printf ("** Can't allocate buffer **\n"); + return -1; + } if (dev_desc->block_read (dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) { printf ("** Can't read partition table on %d:%d **\n", dev_desc->dev, ext_part_sector); - return -1; + goto exit; } if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 || buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) { printf ("bad MBR sector signature 0x%02x%02x\n", buffer[DOS_PART_MAGIC_OFFSET], buffer[DOS_PART_MAGIC_OFFSET + 1]); - return -1; + goto exit; } /* Print all primary/logical partitions */ @@ -223,7 +243,8 @@ static int get_partition_info_extended ( } /* sprintf(info->type, "%d, pt->sys_ind); */ sprintf ((char *)info->type, "U-Boot"); - return 0; + result = 0; + goto exit; } /* Reverse engr the fdisk part# assignment rule! */ @@ -239,12 +260,16 @@ static int get_partition_info_extended ( if (is_extended (pt->sys_ind)) { int lba_start = le32_to_int (pt->start4) + relative; - return get_partition_info_extended (dev_desc, lba_start, - ext_part_sector == 0 ? lba_start : relative, - part_num, which_part, info); + result = get_partition_info_extended(dev_desc, + lba_start, ext_part_sector == 0 ? + lba_start : relative, + part_num, which_part, info); + goto exit; } } - return -1; +exit: + free(buffer); + return result; } void print_part_dos (block_dev_desc_t *dev_desc) Index: u-boot/disk/part_dos.h =================================================================== --- u-boot.orig/disk/part_dos.h +++ u-boot/disk/part_dos.h @@ -25,13 +25,6 @@ #define _DISK_PART_DOS_H -#ifdef CONFIG_ISO_PARTITION -/* Make the buffers bigger if ISO partition support is enabled -- CD-ROMS - have 2048 byte blocks */ -#define DEFAULT_SECTOR_SIZE 2048 -#else -#define DEFAULT_SECTOR_SIZE 512 -#endif #define DOS_PART_TBL_OFFSET 0x1be #define DOS_PART_MAGIC_OFFSET 0x1fe #define DOS_PBR_FSTYPE_OFFSET 0x36