Skip to content

Commit

Permalink
FATFS: Write volume label to the BPB too
Browse files Browse the repository at this point in the history
If the BPB is either v4.1 or v7 long, then its volume label field
should be written.

Note:
  This site https://jdebp.uk/FGA/bios-parameter-block.html suggests that
it is perfectly valid to have a v7 long BPB with a FAT12 or FAT16
filesystem, although more usually it's used for FAT32. However I can't
see any confirmation of this elsewhere, haven't seen an example of this
in the wild, and have no means of generating a test article. More
importantly since we are writing to the filesystem, it's important to
not have any false positives or we could cause corruption. So for now
this combination, should it exist, will not be updated. See the
discussion here dosemu2/fdpp#202.

Part of a fix for [FDOS/label#17]
  • Loading branch information
andrewbird committed Nov 5, 2022
1 parent 80da86a commit 07edd0b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions hdr/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ extern request /* I/O Request packets */
/* dsk.c */
COUNT ASMCFUNC FAR blk_driver(rqptr rp);
ddt * getddt(int dev);
COUNT writelabelBPB(char drive, const char *name);

/* error.c */
COUNT char_error(request * rq, struct dhdr FAR * lpDevice);
Expand Down
35 changes: 34 additions & 1 deletion kernel/dsk.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,40 @@ ddt *getddt(int dev)
return &(((ddt *) Dyn.Buffer)[dev]);
}

STATIC WORD getbpb(ddt *pddt);
STATIC WORD RWzero(ddt *pddt, UWORD mode);

COUNT writelabelBPB(char drive, const char *name)
{
ddt *pddt = getddt(drive - 'A');
struct FS_info *fs;
int offset;
int ret;

ret = getbpb(pddt);
if (ret != 0)
return ret;

if (DiskTransferBuffer[0x26] == 0x29 &&
pddt->ddt_bpb.bpb_nfsect != 0) // BPB v4.1
offset = 0x27;
else if (DiskTransferBuffer[0x42] == 0x29 &&
pddt->ddt_bpb.bpb_nfsect == 0) // BPB v7 long
offset = 0x43;
else
return -1;

/* store volume name */
fs = (struct FS_info *)&DiskTransferBuffer[offset];
memcpy(&fs->volume[0], name, 11);

ret = RWzero(pddt, LBA_WRITE);
if (ret != 0)
return ret;

return 0;
}

STATIC VOID tmark(ddt *pddt)
{
pddt->ddt_fh.ddt_lasttime = ReadPCClock();
Expand All @@ -122,7 +156,6 @@ STATIC dsk_proc mediachk, bldbpb, blockio, IoctlQueblk,
Genblkdev, Getlogdev, Setlogdev, blk_Open, blk_Close,
blk_Media, blk_noerr, blk_nondr, blk_error;

STATIC WORD getbpb(ddt * pddt);
#ifdef PROTO
STATIC WORD dskerr(COUNT);
#else
Expand Down
1 change: 1 addition & 0 deletions kernel/fatfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ int dos_open(char *path, unsigned flags, unsigned attrib, int fd)
return ret;
status = S_CREATED;

writelabelBPB(path[0], path + 3);
goto doit;
}

Expand Down

0 comments on commit 07edd0b

Please sign in to comment.