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 phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C1160C6FD18 for ; Fri, 31 Mar 2023 10:23:43 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8707885D4A; Fri, 31 Mar 2023 12:23:41 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; secure) header.d=gmx.de header.i=xypron.glpk@gmx.de header.b="mTBOvUUQ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 9A14185BAC; Fri, 31 Mar 2023 12:23:38 +0200 (CEST) Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 679E185D91 for ; Fri, 31 Mar 2023 12:23:32 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=xypron.glpk@gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1680258210; i=xypron.glpk@gmx.de; bh=7m8q+XJhoykLahiDjSyrBbrnBIGRxG/ildEoaQn38U8=; h=X-UI-Sender-Class:Date:Subject:From:To:Cc:References:In-Reply-To; b=mTBOvUUQkIIss5sn3dGtAXfILmHTB1sBTIYOb97EmAMZNBnx4zSoxemlLBbIz1UtI 05Zqiatg23fRMqD4zdLe50RW0fl12DcDc+lGf/u/ukM3FBHWab3XWa5muCuGbUB3FV C+jg+SslV3L4KGOmeskSDDThV/q1IiNCw1jLj5Hs1hgzKkW9s4X6vKF4o3z196I+F8 /C97//u6Ys2Uy0aYXBcLQSqjMwCywwEDTEooZgjc4yFybH+2ztEC7CPw9jiKKbPWw8 p18gqE2FPmhO9VYSw/jgAhrc+UnO0mI7ymafXwjCjrixMuNW+knZZGRkCLODtRFuaV Lfitx0J0wztvw== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [192.168.123.94] ([88.152.145.137]) by mail.gmx.net (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MsYux-1qar6X2xGF-00u5hs; Fri, 31 Mar 2023 12:23:30 +0200 Message-ID: <43dc959b-eec7-aaad-d2db-301a64072d50@gmx.de> Date: Fri, 31 Mar 2023 12:23:30 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Subject: Re: [PATCH 33/38] fs: fat: Support reading from a larger block size Content-Language: en-US From: Heinrich Schuchardt To: Simon Glass Cc: Bin Meng , =?UTF-8?Q?Beno=c3=aet_Th=c3=a9baudeau?= , Ilias Apalodimas , U-Boot Mailing List References: <20230330213227.2142353-1-sjg@chromium.org> <20230331103047.33.Ia13846500fab3d5a1d5573db11a040d233994fa6@changeid> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:S2RqaKFancyrX66BM8wPUcf2FvG/5FWij9AjGu5IX38OJCtGGta T4uNmNcc8NhJeem5sSjc5EQrdUCus9HMOzkyokWYBUHW1VUSIdy6VUoLkH3jXCXWy7/o2Rr PNEa9CQci9f/KcpIv6IUQ7ZHQ5Bs+QVJ3kQiT1xObYnW5KesYBnpMYmoJiEE57wTx+cyxnC jaJliz4jLK3MBm8v4MeKg== UI-OutboundReport: notjunk:1;M01:P0:TBINZR2Mr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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" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean On 3/31/23 00:55, Heinrich Schuchardt wrote: > > > Am 30. M=C3=A4rz 2023 23:32:22 MESZ schrieb Simon Glass : >> At present it is not possible to read from some CDROM drives since the >> FAT sector size does not match the media's block size. Add a conversion >> option for this, so that reading is possible. >> >> This does increase SPL size for read-only FAT support by 25 bytes but >> all but 6 are covered by the previous patch. We could reduce the >> overhead of this feature to 0 bytes by making the code uglier (using >> a static variable). > > 512 and 2048 are not the only physical sector sizes. Some hard drives us= e 4096. To complete the logic you have first to take into account the size of a FAT sector (BPB_BytsPerSec) defined in the FAT boot sector which can take values 512, 1024, 2048, or 4096. Cf. https://academy.cba.mit.edu/classes/networking_communications/SD/FAT.p= df Best regards Heinrich > > This change deserves a test case. > > Best regards > > Heinrich > >> >> Signed-off-by: Simon Glass >> --- >> >> fs/fat/Kconfig | 13 ++++++ >> fs/fat/fat.c | 107 ++++++++++++++++++++++++++++++++++++++++----- >> fs/fat/fat_write.c | 8 ++-- >> 3 files changed, 114 insertions(+), 14 deletions(-) >> >> diff --git a/fs/fat/Kconfig b/fs/fat/Kconfig >> index 9bb11eac9f7a..b0aa888c6cc4 100644 >> --- a/fs/fat/Kconfig >> +++ b/fs/fat/Kconfig >> @@ -22,3 +22,16 @@ config FS_FAT_MAX_CLUSTSIZE >> is the smallest amount of disk space that can be used to hold a >> file. Unless you have an extremely tight memory memory constraints, >> leave the default. >> + >> +config FAT_BLK_XLATE >> + bool "Enable FAT filesystem on a device with a larger block size" >> + depends on FS_FAT >> + help >> + This provides a simple translation mechanism for reading FAT >> + filesystems which don't use the same sector size as the underlying >> + media. For example, the FAT filesystem may use 512 bytes but the >> + media uses 2048, e.g. on a CDROM drive. >> + >> + This only supports the case where the FAT filesystem's sector size = is >> + smaller than the media's block size. It does not support creating o= r >> + writing files. >> diff --git a/fs/fat/fat.c b/fs/fat/fat.c >> index f0df7988e172..02c6d55a99b3 100644 >> --- a/fs/fat/fat.c >> +++ b/fs/fat/fat.c >> @@ -43,13 +43,93 @@ static struct disk_partition cur_part_info; >> #define DOS_FS_TYPE_OFFSET 0x36 >> #define DOS_FS32_TYPE_OFFSET 0x52 >> >> -static int disk_read(__u32 block, __u32 nr_blocks, void *buf) >> +/** >> + * disk_read_conv() - Read blocks and break them into smaller ones >> + * >> + * This is used when the FAT filesystem is hosted on a block device wi= th a >> + * block size greated than 512 bytes, e.g. the 2048 bytes of a CDROM d= rive. It >> + * reads the blocks into a buffer and pulls out what is requested by t= he calling >> + * function. >> + * >> + * It uses an internal 2KB buffer on the stack. >> + * >> + * @mydata: Filesystem information >> + * @block: Block number to read, in terms of mydata->sect_size >> + * @nr_blocks: Number of blocks to read, in terms of mydata->sect_size >> + * @buf: Buffer for data >> + */ >> +static int disk_read_conv(fsdata *mydata, __u32 block, __u32 nr_blocks= , >> + void *buf) >> +{ >> + uint factor, whole, remain, upto; >> + ulong base, index; >> + uint to_copy; >> + u8 tbuf[2048]; >> + int ret; >> + >> + log_debug("mydata %x, cur_dev %lx, block %x, nr_block %x\n", >> + mydata->sect_size, cur_dev->blksz, block, nr_blocks); >> + if (mydata->sect_size > cur_dev->blksz || >> + cur_dev->blksz > sizeof(tbuf)) { >> + log_err("Block size %lx not supported\n", cur_dev->blksz); >> + return -EIO; >> + } >> + factor =3D cur_dev->blksz / mydata->sect_size; >> + >> + /* get the first partial block */ >> + base =3D cur_part_info.start + block / factor; >> + index =3D block % factor; >> + log_debug("cur_part_info.start %llx, block %x, base %lx, index %lx\n"= , >> + (unsigned long long)cur_part_info.start, block, base, index); >> + ret =3D blk_dread(cur_dev, base, 1, tbuf); >> + if (ret !=3D 1) >> + return -EIO; >> + >> + to_copy =3D min((ulong)nr_blocks, factor - index); >> + log_debug("to_copy %x\n", to_copy); >> + memcpy(buf, tbuf + index * mydata->sect_size, >> + to_copy * mydata->sect_size); >> + upto =3D to_copy; >> + >> + /* load any whole blocks */ >> + remain =3D nr_blocks - upto; >> + whole =3D remain / factor; >> + log_debug("factor %x, whole %x, remain %x\n", factor, whole, remain); >> + if (whole) { >> + ret =3D blk_dread(cur_dev, base + 1, whole, >> + buf + upto * mydata->sect_size); >> + if (ret !=3D whole) >> + return -EIO; >> + upto +=3D whole * factor; >> + remain =3D nr_blocks - upto; >> + } >> + >> + /* load any blocks at the end */ >> + log_debug("end: remain %x\n", remain); >> + if (remain) { >> + ret =3D blk_dread(cur_dev, base + 1 + whole, 1, tbuf); >> + if (ret !=3D 1) >> + return -EIO; >> + memcpy(buf + upto * mydata->sect_size, tbuf, >> + remain * mydata->sect_size); >> + upto +=3D remain; >> + } >> + >> + return upto; >> +} >> + >> +static int disk_read(fsdata *mydata, __u32 block, __u32 nr_blocks, voi= d *buf) >> { >> ulong ret; >> >> if (!cur_dev) >> return -1; >> >> + /* support converting from a larger block size */ >> + if (IS_ENABLED(CONFIG_FAT_BLK_XLATE) && mydata && >> + mydata->sect_size !=3D cur_dev->blksz) >> + return disk_read_conv(mydata, block, nr_blocks, buf); >> + >> ret =3D blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf= ); >> >> if (ret !=3D nr_blocks) >> @@ -66,7 +146,7 @@ int fat_set_blk_dev(struct blk_desc *dev_desc, struc= t disk_partition *info) >> cur_part_info =3D *info; >> >> /* Make sure it has a valid FAT header */ >> - if (disk_read(0, 1, buffer) !=3D 1) { >> + if (disk_read(NULL, 0, 1, buffer) !=3D 1) { >> cur_dev =3D NULL; >> return -1; >> } >> @@ -211,7 +291,7 @@ static __u32 get_fatent(fsdata *mydata, __u32 entry= ) >> if (flush_dirty_fat_buffer(mydata) < 0) >> return -1; >> >> - if (disk_read(startblock, getsize, bufptr) < 0) { >> + if (disk_read(mydata, startblock, getsize, bufptr) < 0) { >> debug("Error reading FAT blocks\n"); >> return ret; >> } >> @@ -265,7 +345,7 @@ get_cluster(fsdata *mydata, __u32 clustnum, __u8 *b= uffer, unsigned long size) >> debug("FAT: Misaligned buffer address (%p)\n", buffer); >> >> while (size >=3D mydata->sect_size) { >> - ret =3D disk_read(startsect++, 1, tmpbuf); >> + ret =3D disk_read(mydata, startsect++, 1, tmpbuf); >> if (ret !=3D 1) { >> debug("Error reading data (got %d)\n", ret); >> return -1; >> @@ -279,7 +359,7 @@ get_cluster(fsdata *mydata, __u32 clustnum, __u8 *b= uffer, unsigned long size) >> __u32 bytes_read; >> __u32 sect_count =3D size / mydata->sect_size; >> >> - ret =3D disk_read(startsect, sect_count, buffer); >> + ret =3D disk_read(mydata, startsect, sect_count, buffer); >> if (ret !=3D sect_count) { >> debug("Error reading data (got %d)\n", ret); >> return -1; >> @@ -292,7 +372,7 @@ get_cluster(fsdata *mydata, __u32 clustnum, __u8 *b= uffer, unsigned long size) >> if (size) { >> ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size); >> >> - ret =3D disk_read(startsect, 1, tmpbuf); >> + ret =3D disk_read(mydata, startsect, 1, tmpbuf); >> if (ret !=3D 1) { >> debug("Error reading data (got %d)\n", ret); >> return -1; >> @@ -504,7 +584,7 @@ read_bootsectandvi(boot_sector *bs, volume_info *vo= linfo, int *fatsize) >> return -1; >> } >> >> - if (disk_read(0, 1, block) < 0) { >> + if (disk_read(NULL, 0, 1, block) < 0) { >> debug("Error: reading block\n"); >> goto fail; >> } >> @@ -586,9 +666,14 @@ static int get_fs_info(fsdata *mydata) >> mydata->sect_size =3D (bs.sector_size[1] << 8) + bs.sector_size[0]; >> mydata->clust_size =3D bs.cluster_size; >> if (mydata->sect_size !=3D cur_part_info.blksz) { >> - printf("** FAT sector size mismatch (fs=3D%hu, dev=3D%lu)\n", >> - mydata->sect_size, cur_part_info.blksz); >> - return -1; >> + if (IS_ENABLED(CONFIG_FAT_BLK_XLATE)) { >> + printf("Warning: FAT sector size mismatch (fs=3D%u, dev=3D%lu): tra= nslating for read-only\n", >> + mydata->sect_size, cur_part_info.blksz); >> + } else { >> + printf("** FAT sector size mismatch (fs=3D%u, dev=3D%lu), see CONFI= G_FAT_BLK_XLATE\n", >> + mydata->sect_size, cur_part_info.blksz); >> + return -1; >> + } >> } >> if (mydata->clust_size =3D=3D 0) { >> printf("** FAT cluster size not set\n"); >> @@ -846,7 +931,7 @@ void *fat_next_cluster(fat_itr *itr, unsigned int *= nbytes) >> * dent at a time and iteratively constructing the vfat long >> * name. >> */ >> - ret =3D disk_read(sect, read_size, itr->block); >> + ret =3D disk_read(itr->fsdata, sect, read_size, itr->block); >> if (ret < 0) { >> debug("Error: reading block\n"); >> return NULL; >> diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c >> index 4d2d4db07fa6..baef52af6363 100644 >> --- a/fs/fat/fat_write.c >> +++ b/fs/fat/fat_write.c >> @@ -474,7 +474,7 @@ static int set_fatent_value(fsdata *mydata, __u32 e= ntry, __u32 entry_value) >> >> startblock +=3D mydata->fat_sect; >> >> - if (disk_read(startblock, getsize, bufptr) < 0) { >> + if (disk_read(NULL, startblock, getsize, bufptr) < 0) { >> debug("Error reading FAT blocks\n"); >> return -1; >> } >> @@ -709,7 +709,8 @@ get_set_cluster(fsdata *mydata, __u32 clustnum, lof= f_t pos, __u8 *buffer, >> /* partial write at beginning */ >> if (pos) { >> wsize =3D min(bytesperclust - pos, size); >> - ret =3D disk_read(startsect, mydata->clust_size, tmpbuf_cluster); >> + ret =3D disk_read(NULL, startsect, mydata->clust_size, >> + tmpbuf_cluster); >> if (ret !=3D mydata->clust_size) { >> debug("Error reading data (got %d)\n", ret); >> return -1; >> @@ -775,7 +776,8 @@ get_set_cluster(fsdata *mydata, __u32 clustnum, lof= f_t pos, __u8 *buffer, >> /* partial write at end */ >> if (size) { >> wsize =3D size; >> - ret =3D disk_read(startsect, mydata->clust_size, tmpbuf_cluster); >> + ret =3D disk_read(NULL, startsect, mydata->clust_size, >> + tmpbuf_cluster); >> if (ret !=3D mydata->clust_size) { >> debug("Error reading data (got %d)\n", ret); >> return -1;