diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index 17abfb8..bf632c4 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -16,7 +16,7 @@ jobs: - name: Setup Zig uses: mlugg/setup-zig@v1 with: - version: 0.14.0 + version: 0.15.2 - name: Build Demo run: | diff --git a/build.zig b/build.zig index 796c21d..8cecee0 100644 --- a/build.zig +++ b/build.zig @@ -15,7 +15,7 @@ pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const link_libc = !(b.option(bool, "no-libc", "Prevents linking of libc by default") orelse false); + const link_libc = b.option(bool, "libc", "Forces the linking of libc"); const config = blk: { var config = Config{}; @@ -91,11 +91,11 @@ pub fn build(b: *std.Build) void { } if (maybe_volume_names) |volume_names| { - var volumes = std.ArrayList([]const u8).init(b.allocator); + var volumes: std.ArrayList([]const u8) = .empty; var iter = std.mem.splitScalar(u8, volume_names, ','); while (iter.next()) |name| { - volumes.append(name) catch @panic("out of memory"); + volumes.append(b.allocator, name) catch @panic("out of memory"); } config.volumes = .{ .named = volumes.items }; } @@ -109,20 +109,20 @@ pub fn build(b: *std.Build) void { config.sector_size = .{ .dynamic = .{ .minimum = std.meta.stringToEnum(SectorOption, min) orelse bad_config( - "Invalid value for -Dsector-size: '{}'", - .{std.zig.fmtEscapes(sector_config)}, + "Invalid value for -Dsector-size: '{f}'", + .{std.zig.fmtString(sector_config)}, ), .maximum = std.meta.stringToEnum(SectorOption, max) orelse bad_config( - "Invalid value for -Dsector-size: '{}'", - .{std.zig.fmtEscapes(sector_config)}, + "Invalid value for -Dsector-size: '{f}'", + .{std.zig.fmtString(sector_config)}, ), }, }; } else { config.sector_size = .{ .static = std.meta.stringToEnum(SectorOption, sector_config) orelse bad_config( - "Invalid value for -Dsector-size: '{}'", - .{std.zig.fmtEscapes(sector_config)}, + "Invalid value for -Dsector-size: '{f}'", + .{std.zig.fmtString(sector_config)}, ), }; } @@ -136,7 +136,7 @@ pub fn build(b: *std.Build) void { .style = .blank, .include_path = "ffconf.h", }, .{ - .FFCONF_DEF = 5380, + .FFCONF_DEF = 5385, }); switch (config.volumes) { @@ -147,13 +147,13 @@ pub fn build(b: *std.Build) void { }); }, .named => |strings| { - var list = std.ArrayList(u8).init(b.allocator); + var list: std.ArrayList(u8) = .empty; for (strings) |name| { if (list.items.len > 0) { - list.appendSlice(", ") catch @panic("out of memory"); + list.appendSlice(b.allocator, ", ") catch @panic("out of memory"); } - list.writer().print("\"{}\"", .{ - std.fmt.fmtSliceHexUpper(name), + list.writer(b.allocator).print("\"{X}\"", .{ + name, }) catch @panic("out of memory"); } config_header.addValues(.{ @@ -198,28 +198,27 @@ pub fn build(b: *std.Build) void { } // module: - const upstream = b.dependency("fatfs", .{}); const mod_options = b.addOptions(); mod_options.addOption(bool, "has_rtc", (config.rtc != .static)); // create copy of the upstream without ffconf.h const upstream_copy = b.addWriteFiles(); - _ = upstream_copy.addCopyFile(upstream.path("source/ff.c"), "ff.c"); - _ = upstream_copy.addCopyFile(upstream.path("source/ff.h"), "ff.h"); - _ = upstream_copy.addCopyFile(upstream.path("source/diskio.h"), "diskio.h"); - _ = upstream_copy.addCopyFile(upstream.path("source/ffunicode.c"), "ffunicode.c"); - _ = upstream_copy.addCopyFile(upstream.path("source/ffsystem.c"), "ffsystem.c"); + _ = upstream_copy.addCopyFile(b.path("vendor/fatfs/source/ff.c"), "ff.c"); + _ = upstream_copy.addCopyFile(b.path("vendor/fatfs/source/ff.h"), "ff.h"); + _ = upstream_copy.addCopyFile(b.path("vendor/fatfs/source/diskio.h"), "diskio.h"); + _ = upstream_copy.addCopyFile(b.path("vendor/fatfs/source/ffunicode.c"), "ffunicode.c"); + _ = upstream_copy.addCopyFile(b.path("vendor/fatfs/source/ffsystem.c"), "ffsystem.c"); const upstream_copy_dir = upstream_copy.getDirectory(); - const zfat_lib_mod = b.createModule(.{ + const zfat_mod = b.addModule("zfat", .{ + .root_source_file = b.path("src/fatfs.zig"), .target = target, .optimize = optimize, .link_libc = link_libc, }); - zfat_lib_mod.addCSourceFiles(.{ + zfat_mod.addCSourceFiles(.{ .root = upstream_copy_dir, - .files = &.{ "ff.c", "ffunicode.c", @@ -227,33 +226,23 @@ pub fn build(b: *std.Build) void { }, .flags = &.{"-std=c99"}, }); - zfat_lib_mod.addConfigHeader(config_header); - - const zfat = b.addLibrary(.{ - .linkage = .static, - .name = "zfat", - .root_module = zfat_lib_mod, - }); - zfat.installHeader(upstream_copy_dir.path(b, "ff.h"), "ff.h"); - zfat.installHeader(upstream_copy_dir.path(b, "diskio.h"), "diskio.h"); - zfat.installConfigHeader(config_header); - - const zfat_mod = b.addModule("zfat", .{ - .root_source_file = b.path("src/fatfs.zig"), - .target = target, - .optimize = optimize, - }); - zfat_mod.linkLibrary(zfat); + zfat_mod.addIncludePath(upstream_copy_dir.path(b, ".")); + zfat_mod.addConfigHeader(config_header); zfat_mod.addOptions("config", mod_options); // usage demo: const exe = b.addExecutable(.{ .name = "zfat-demo", - .root_source_file = b.path("demo/main.zig"), - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("demo/main.zig"), + .target = target, + .optimize = optimize, + .link_libc = true, + .imports = &.{ + .{ .name = "zfat", .module = zfat_mod }, + }, + }), }); - exe.root_module.addImport("zfat", zfat_mod); const demo_exe = b.addInstallArtifact(exe, .{}); demo_step.dependOn(&demo_exe.step); @@ -285,7 +274,7 @@ fn add_config_field(config_header: *std.Build.Step.ConfigHeader, config: Config, } fn add_config_option(b: *std.Build, config: *Config, comptime field: @TypeOf(.tag), desc: []const u8) void { - const T = std.meta.FieldType(Config, field); + const T = @FieldType(Config, @tagName(field)); if (b.option(T, @tagName(field), desc)) |value| @field(config, @tagName(field)) = value; } diff --git a/build.zig.zon b/build.zig.zon index 0668383..7ea505d 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -2,12 +2,7 @@ .name = .zfat, .fingerprint = 0x4212c7b5f54ad348, .version = "0.15.0", - .dependencies = .{ - .fatfs = .{ - .url = "http://elm-chan.org/fsw/ff/arc/ff15a.zip", - .hash = "N-V-__8AAFQITQCnpmdR7PARImvk-cgb-9lZmjKolexSWkUL", - }, - }, + .dependencies = .{}, .paths = .{ "build.zig", "build.zig.zon", diff --git a/demo/main.zig b/demo/main.zig index 1088d5b..9069c90 100644 --- a/demo/main.zig +++ b/demo/main.zig @@ -30,7 +30,33 @@ pub fn main() !u8 { var file = try fatfs.File.create("0:/firmware.uf2"); defer file.close(); - try file.writer().writeAll("Hello, World!\r\n"); + var buffer: [64]u8 = undefined; + + const test_string = "Hello, World!\r\n"; + + var writer = file.writer(&buffer); + try writer.writer.writeAll(test_string); + try writer.writer.flush(); + + try std.testing.expectEqual(true, file.endOfFile()); + try std.testing.expectEqual(test_string.len, file.size()); + + try file.seekTo(test_string.len / 2); + try std.testing.expectEqual(false, file.endOfFile()); + try std.testing.expectEqual(test_string.len / 2, file.tell()); + try std.testing.expectEqual(test_string.len, file.size()); + + try std.testing.expectEqual(false, file.hasError()); + + try file.rewind(); + try std.testing.expectEqual(0, file.tell()); + + var reader = file.reader(&buffer); + const written_string = try reader.reader.take(test_string.len); + try std.testing.expectEqualStrings(test_string, written_string); + + _ = try file.sync(); + _ = try file.truncate(); } return 0; diff --git a/flake.lock b/flake.lock deleted file mode 100644 index 1ba4434..0000000 --- a/flake.lock +++ /dev/null @@ -1,147 +0,0 @@ -{ - "nodes": { - "flake-compat": { - "flake": false, - "locked": { - "lastModified": 1696426674, - "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", - "owner": "edolstra", - "repo": "flake-compat", - "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", - "type": "github" - }, - "original": { - "owner": "edolstra", - "repo": "flake-compat", - "type": "github" - } - }, - "flake-utils": { - "inputs": { - "systems": "systems" - }, - "locked": { - "lastModified": 1710146030, - "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } - }, - "flake-utils_2": { - "inputs": { - "systems": "systems_2" - }, - "locked": { - "lastModified": 1705309234, - "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } - }, - "nixpkgs": { - "locked": { - "lastModified": 1718229064, - "narHash": "sha256-ZFav8A9zPNfjZg/wrxh1uZeMJHELRfRgFP+meq01XYk=", - "owner": "nixos", - "repo": "nixpkgs", - "rev": "5c2ec3a5c2ee9909904f860dadc19bc12cd9cc44", - "type": "github" - }, - "original": { - "owner": "nixos", - "ref": "nixos-23.11", - "repo": "nixpkgs", - "type": "github" - } - }, - "nixpkgs_2": { - "locked": { - "lastModified": 1708161998, - "narHash": "sha256-6KnemmUorCvlcAvGziFosAVkrlWZGIc6UNT9GUYr0jQ=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "84d981bae8b5e783b3b548de505b22880559515f", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixos-23.11", - "repo": "nixpkgs", - "type": "github" - } - }, - "root": { - "inputs": { - "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs", - "zig": "zig" - } - }, - "systems": { - "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default", - "type": "github" - } - }, - "systems_2": { - "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default", - "type": "github" - } - }, - "zig": { - "inputs": { - "flake-compat": "flake-compat", - "flake-utils": "flake-utils_2", - "nixpkgs": "nixpkgs_2" - }, - "locked": { - "lastModified": 1718324667, - "narHash": "sha256-AZGskEGjvUmeb+fgBv4lxtCUtXmYBI+ABOlV+og9X14=", - "owner": "mitchellh", - "repo": "zig-overlay", - "rev": "b2c14e5f842af6b2bf03e634f73fd84f6956d4ba", - "type": "github" - }, - "original": { - "owner": "mitchellh", - "repo": "zig-overlay", - "type": "github" - } - } - }, - "root": "root", - "version": 7 -} diff --git a/flake.nix b/flake.nix deleted file mode 100644 index 49ae555..0000000 --- a/flake.nix +++ /dev/null @@ -1,52 +0,0 @@ -{ - description = "TurtleFont, a small vector graphics font file."; - - inputs = { - nixpkgs.url = "github:nixos/nixpkgs/nixos-23.11"; - flake-utils.url = "github:numtide/flake-utils"; - zig.url = "github:mitchellh/zig-overlay"; - }; - - outputs = { - self, - nixpkgs, - flake-utils, - ... - } @ inputs: let - overlays = [ - # Other overlays - (final: prev: { - zigpkgs = inputs.zig.packages.${prev.system}; - }) - ]; - - # Our supported systems are the same supported systems as the Zig binaries - systems = builtins.attrNames inputs.zig.packages; - in - flake-utils.lib.eachSystem systems ( - system: let - pkgs = import nixpkgs {inherit overlays system;}; - in let - zig = pkgs.zigpkgs."0.13.0"; - in rec { - packages.default = pkgs.stdenv.mkDerivation { - name = "turtlefont"; - src = ./.; - nativeBuildInputs = [ - zig - pkgs.gdb - ]; - - configurePhase = ""; - - buildPhase = '' - zig build - ''; - - installPhase = '' - mv zig-out $out - ''; - }; - } - ); -} diff --git a/src/fatfs.zig b/src/fatfs.zig index 2211cfc..f86d8aa 100644 --- a/src/fatfs.zig +++ b/src/fatfs.zig @@ -186,7 +186,7 @@ pub const Dir = struct { pub const ReadDirError = ErrorSet(&.{ FR_DISK_ERR, FR_INT_ERR, FR_INVALID_OBJECT, FR_TIMEOUT, FR_NOT_ENOUGH_CORE }); pub fn next(dir: *Self) ReadDirError.Error!?FileInfo { - var res: c.FILINFO = undefined; + var res: c.FILINFO = .{}; try ReadDirError.throw(api.readdir(&dir.raw, &res)); if (res.fname[0] == 0) return null; @@ -222,10 +222,7 @@ pub const Attributes = packed struct(u8) { std.debug.assert(@bitOffsetOf(Attributes, "archive") == 5); } - pub fn format(attrs: Attributes, fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { - _ = fmt; - _ = options; - + pub fn format(attrs: Attributes, writer: *std.Io.Writer) !void { var keys = std.BoundedArray([]const u8, 8){}; if (attrs.read_only) keys.appendAssumeCapacity("read_only"); if (attrs.hidden) keys.appendAssumeCapacity("hidden"); @@ -285,10 +282,7 @@ pub const FileInfo = struct { pub const max_name_len = if (@hasDecl(c, "FF_LFN_BUF")) c.FF_LFN_BUF else 12; pub const max_altname_len = if (@hasDecl(c, "FF_SFN_BUF")) c.FF_SFN_BUF else 0; - pub fn format(info: FileInfo, fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { - _ = fmt; - _ = options; - + pub fn format(info: FileInfo, writer: *std.Io.Writer) !void { try writer.print( \\{s}{{ .size={}, .date = {}, .time = {}, .kind = .{s}, .attributes = {}, .name = '{}', .altname = '{}' }} , .{ @@ -338,9 +332,7 @@ pub const Date = struct { }); } - pub fn format(date: Date, comptime fmt: []const u8, opt: std.fmt.FormatOptions, writer: anytype) !void { - _ = fmt; - _ = opt; + pub fn format(date: Date, writer: *std.Io.Writer) !void { try writer.print("{d:0>4}-{d:0>2}-{d:0>2}", .{ date.year, @intFromEnum(date.month), @@ -393,9 +385,7 @@ pub const Time = struct { }); } - pub fn format(time: Time, comptime fmt: []const u8, opt: std.fmt.FormatOptions, writer: anytype) !void { - _ = fmt; - _ = opt; + pub fn format(time: Time, writer: *std.Io.Writer) !void { try writer.print("{d:0>2}:{d:0>2}:{d:0>2}", .{ time.hour, time.minute, @@ -537,14 +527,71 @@ pub const File = struct { return written; } - pub const Reader = std.io.Reader(*Self, ReadError.Error, read); - pub fn reader(file: *Self) Reader { - return Reader{ .context = file }; + pub const Reader = struct { + file: *Self, + err: ?ReadError.Error = null, + reader: std.Io.Reader, + }; + + pub const Writer = struct { + file: *Self, + err: ?WriteError.Error = null, + writer: std.Io.Writer, + }; + + pub fn reader(file: *Self, buffer: []u8) Reader { + return .{ + .file = file, + .reader = .{ + .buffer = buffer, + .seek = 0, + .end = 0, + .vtable = comptime &.{ + .stream = reader_stream, + }, + }, + }; } - pub const Writer = std.io.Writer(*Self, WriteError.Error, write); - pub fn writer(file: *Self) Writer { - return Writer{ .context = file }; + pub fn writer(file: *Self, buffer: []u8) Writer { + return .{ + .file = file, + .writer = .{ + .buffer = buffer, + .vtable = &.{ + .drain = writer_drain, + }, + }, + }; + } + + fn reader_stream(r: *std.Io.Reader, w: *std.Io.Writer, limit: std.Io.Limit) std.Io.Reader.StreamError!usize { + const wrap: *Reader = @fieldParentPtr("reader", r); + + const buffer = limit.slice(try w.writableSliceGreedy(1)); + + const len = wrap.file.read(buffer) catch |err| { + wrap.err = err; + return error.ReadFailed; + }; + if (len == 0) + return error.EndOfStream; + w.advance(len); + return len; + } + + fn writer_drain(w: *std.Io.Writer, data: []const []const u8, splat: usize) std.Io.Writer.Error!usize { + _ = splat; + const wrap: *Writer = @fieldParentPtr("writer", w); + const buffered = w.buffered(); + if (buffered.len != 0) return w.consume(wrap.file.write(buffered) catch |err| { + wrap.err = err; + return error.WriteFailed; + }); + return wrap.file.write(data[0]) catch |err| { + wrap.err = err; + return error.WriteFailed; + }; } }; diff --git a/vendor/fatfs/README.md b/vendor/fatfs/README.md deleted file mode 100644 index c5ddf05..0000000 --- a/vendor/fatfs/README.md +++ /dev/null @@ -1 +0,0 @@ -Offline copy of the download http://www.elm-chan.org/fsw/ff/00index_e.html with the original source files. diff --git a/vendor/fatfs/documents/00index_e.html b/vendor/fatfs/documents/00index_e.html index 81185ac..a984b5f 100644 --- a/vendor/fatfs/documents/00index_e.html +++ b/vendor/fatfs/documents/00index_e.html @@ -17,7 +17,7 @@

FatFs - Generic FAT Filesystem Module

layer -

FatFs is a generic FAT/exFAT filesystem module for small embedded systems. The FatFs module is written in compliance with ANSI C (C89) and completely separated from the disk I/O layer. Therefore it is independent of the platform. It can be incorporated into small microcontrollers with limited resource, such as 8051, PIC, AVR, ARM, Z80, RX and etc. Also Petit FatFs module for tiny microcontrollers is available here.

+

FatFs is a generic FAT/exFAT filesystem module for small embedded systems. The FatFs module is written in compliance with ANSI C (C89) and completely separated from the disk control layer. Therefore it is independent of the platforms and storage devices. It can be incorporated into small microcontrollers with limited resource, such as 8051, PIC, AVR, ARM, Z80, RX and etc. Also Petit FatFs module for tiny microcontrollers is available here🔗.

Features

@@ -76,7 +79,7 @@

Required Functions

disk_ioctl (GET_SECTOR_SIZE)FF_MAX_SS != FF_MIN_SS disk_ioctl (CTRL_TRIM)FF_USE_TRIM == 1 ff_uni2oem
ff_oem2uni
ff_wtoupperFF_USE_LFN != 0Unicode support functions.
Add optional module ffunicode.c to the project. -ff_cre_syncobj
ff_del_syncobj
ff_req_grant
ff_rel_grantFF_FS_REENTRANT == 1O/S dependent functions.
Sample code is available in ffsystem.c. +ff_mutex_create
ff_mutex_delete
ff_mutex_take
ff_mutex_giveFF_FS_REENTRANT == 1O/S dependent functions.
Sample code is available in ffsystem.c. ff_mem_alloc
ff_mem_freeFF_USE_LFN == 3

FatFs cares about neither what kind of storage device is used nor how it is implemented. Only a requirement is that it is a block device read/written in fixed-size blocks that accessible via the disk I/O functions defined above.

@@ -103,17 +106,17 @@

Memory Usage

ARM7
32bit
ARM7
Thumb
CM3
Thumb-2
AVRH8/300HPIC24RL78V850ESSH-2ARX600IA-32 CompilerGCCGCCGCCGCCCH38C30CC78K0RCA850SHCRXCMSC -.text (Def, R/W)10.4k6.7k6.1k12.5k11.0k11.6k13.0k8.9k9.2k6.5k8.9k -.text (Min, R/W) 7.0k4.7k4.2k 8.5k 7.6k 8.1k 9.5k6.2k6.4k4.6k6.4k -.text (Def, R/O) 4.9k3.2k2.7k 6.1k 5.2k 5.5k 6.5k4.3k4.2k3.2k4.3k -.text (Min, R/O) 3.7k2.5k2.1k 4.4k 4.0k 4.3k 5.1k3.4k3.3k2.5k3.5k +.text (Def, R/W)10.5k6.7k6.1k12.6k11.0k11.4k13.0k8.9k9.2k6.5k8.9k +.text (Min, R/W) 7.0k4.7k4.2k 8.5k 7.6k 7.9k 9.4k6.3k6.4k4.7k6.3k +.text (Def, R/O) 4.9k3.2k2.8k 6.1k 5.2k 5.5k 6.5k4.4k4.2k3.2k4.3k +.text (Min, R/O) 3.7k2.5k2.1k 4.4k 4.0k 4.2k 5.0k3.4k3.3k2.5k3.4k .bssV*4 + 2V*4 + 2V*4 + 2V*2 + 2V*4 + 2V*2 + 2V*2 + 2V*4 + 2V*4 + 2V*4 + 2V*4 + 2 Work area
(FF_FS_TINY == 0)V*564
+ F*552V*564
+ F*552V*564
+ F*552V*560
+ F*546V*560
+ F*546V*560
+ F*546V*560
+ F*546V*564
+ F*552V*564
+ F*552V*564
+ F*552V*564
+ F*552 Work area
(FF_FS_TINY == 1)V*564
+ F*40V*564
+ F*40V*564
+ F*40V*560
+ F*34V*560
+ F*34V*560
+ F*34V*560
+ F*34V*564
+ F*40V*564
+ F*40V*564
+ F*40V*564
+ F*40

These are the memory usage of FatFs module without lower layer on some target systems in following condition. V denotes number of mounted volumes and F denotes number of open files. Every samples here are optimezed in code size.

-FatFs R0.14b options:
+FatFs R0.15b options:
 FF_FS_READONLY   0 (R/W, read/write) or 1 (R/O, read only)
 FF_FS_MINIMIZE   0 (Def, with all basic functions) or 3 (Min, with fully minimized)
 FF_FS_TINY       0 (Default) or 1 (Tiny file object)
@@ -166,169 +169,170 @@ 

Reducing Module Size

Long File Name

-

FatFs module supports the long file name (LFN) extension of the FAT filesystem. The two different file names, short file name (SFN) and LFN, of a file is transparent on the API. The support for LFN feature is disabled by default. To enable the LFN, set FF_USE_LFN to 1, 2 or 3, and add ffunicode.c to the project. The LFN feature requiers a certain working buffer. The buffer size can be configured by FF_MAX_LFN according to the available memory. The length of LFN can be up to 255 characters, so that the FF_MAX_LFN should be set to 255 for any existing file names. If the size of working buffer is insufficient for the input file name, the file function fails with FR_INVALID_NAME. When use any re-entry to the API with LFN feature in RTOS environment, FF_USE_LFN must be set to 2 or 3. In this case, the file function allocates the working buffer on the stack or heap. The LFN working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled.

-

Impact upon Module Size

- - - - - - - - - -
With LFN at CM3 + gcc
FF_CODE_PAGECode size
437-869 (SBCS)+3.3k
932 (Japanese)+62k
936 (Simplified Chinese)+177k
949 (Korean)+140k
950 (Traditional Chinese)+111k
0 (All code pages)+486k
-

When the LFN is enabled, the module size will be increased depends on the configured code page. Right table shows the increment of code size in some code pages. Especially, in the CJK region, tens of thousands of characters are being used. Unfortunately, it requires a huge OEM-Unicode bidirectional conversion table and the module size will be drastically increased as shown in the table.

-

As the result, the FatFs with LFN enabled with DBCS code pages will not able to be ported on the most 8-bit MCU systems. If the target system is in legacy-free, in only Unicode and any ANSI/OEM code is not used at all, the code page setting gets meaningless. You will able to reduce the code size by configureing FatFs for Unicode API with any SBCS code page.

-

There ware some restrictions on using LFN for open source project, because the LFN extension on the FAT filesystem was a patent of Microsoft Corporation. However the related patents all have expired and using the LFN feature is free for any projects.

-
- -
-

Unicode API

-

By default, FatFs uses ANSI/OEM code set on the API even in LFN configuration. FatFs can also switch the character encoding on the API to Unicode by configuration option FF_LFN_UNICODE. This means that FatFs is compliant with the full featured LFN specification. The data type TCHAR specifies path name strings on the API is an alias of either char(ANSI/OEM or UTF-8), WCHAR(UTF-16) or DWORD(UTF-32) depends on that option. For more information, refer to the description in the file name.

-

Note that setting of code page, FF_CODE_PAGE, has actually no meaning when FatFs is configured for the Unicode API. It should be set 437 to reduce the module size. However it still affects code conversion of string I/O functions when FF_STRF_ENCODE == 0, and also backward compatibility with legacy systems. In this case, the code page may need to be configured properly if it is considered a problem.

-
- -
-

exFAT Filesystem

-

The exFAT (Microsoft's Extended File Allocation Table) filesystem is a succession of the FAT/FAT32 filesystem which has been widely used in embedded systems, consumer devices and portable storage media. It is adopted by SDA (SD Association) as the filesystem for SDXC card, 64 GB and larger, and they are being shipped with this format. Therefore the exFAT is one of the standard filesystems for removable media as well as FAT. The exFAT filesystem allows the file size beyond the 4 GB limit what FAT filesystem allows up to and some filesystem overhead, especially cluster allocation delay, are reduced as well. These features allow to record the large data without dividing into some files and improve the write throughput to the file.

-

Note that the exFAT filesystem is a patent of Microsoft Corporation. The exFAT feature of FatFs is an implementation based on US. Pat. App. Pub. No. 2009/0164440 A1. FatFs module can switch the exFAT on or off by a configuration option, FF_FS_EXFAT. When enable the exFAT for the commercial products, a license by Microsoft will be needed depends on the final destination of the products.

-

Remarks: Enabling exFAT discards C89 compatibility and it wants C99 because of need for 64-bit integer type.

-
- -
-

64-bit LBA

-

LBA (Logical Block Addressing) is an addressing method to specify the location of data block, called sector, on the storage media. It is a simple linear address beginning from 0 as the first sector, 1 as the second sector and so on. The host system does not need to consider how the data block is located and managed in the storage device. FatFs supports only LBA for the media access. 32-bit LBA is a common size in the most LBA scheme. It can address up to 232 sectors, 2 TB in 512 bytes/sector. When a storage device larger than 2 TB is used, larger sector size or 64-bit LBA will be needed to address the entire sectors of the storage device.

-

By default, FatFs works in 32-bit LBA for media access. FatFs can also switch it to 64-bit LBA by a configuration option FF_LBA64. It also enables GPT (GUID Partition Table) for partiotion management on the storage device. For further information about GPT, refer to f_mkfs and f_fdisk function.

-
- -
-

Re-entrancy

-

The file operations of two tasks to the different volumes each other is always re-entrant regardless of the configurations except when LFN is enabled with static working buffer (FF_USE_LFN = 1). It can work concurrently without any mutual exclusion.

-

The file operations of two tasks to the same volume is not re-entrant in default. FatFs can also be configured to make it thread-safe by option FF_FS_REENTRANT. In this case, also the OS dependent synchronization control functions, ff_cre_syncobj/ff_del_syncobj/ff_req_grant/ff_rel_grant, need to be added to the project. There are some examples in the ffsystem.c. When a file function is called while the volume is being accessed by another task, the file function to the volume will be suspended until that task leaves the file function. If the wait time exceeded a period defined by FF_TIMEOUT, the file function will abort with FR_TIMEOUT. The timeout feature might not be supported on the some RTOSs.

-

There is an exception on the re-entrancy for f_mount/f_mkfs function. These volume management functions are not re-entrant to the same volume. When use these functions, other tasks need to avoid to access the volume.

-
- - - - - - - - -
FunctionCase 1Case 2Case 3
disk_statusYesYesYes(*)
disk_initializeNoYesYes(*)
disk_readNoYesYes(*)
disk_writeNoYesYes(*)
disk_ioctlNoYesYes(*)
get_fattimeNoYesYes
- -Case 1: Same volume.
-Case 2: Different volume on the same drive.
-Case 3: Different volume on the different drive.
-(*) In only different drive number. -
-
-

Remarks: This section describes on the re-entrancy of the FatFs module itself. The FF_FS_REENTRANT option enables only exclusive use of each filesystem objects and FatFs does not that prevent to re-enter the storage device control functions. Thus the device control layer needs to be always thread-safe when FatFs API is re-entered for different volumes. Right table shows which control function can be re-entered when FatFs API is re-entered on some conditions.

-
- -
-

Duplicated File Open

-

FatFs module does not support the read/write collision control of duplicated open to a file. The duplicated open is permitted only when each of open method to a file is read mode. The duplicated open with one or more write mode to a file is always prohibited, and also open file must not be renamed or deleted. A violation of these rules can cause data collaption.

-

The file lock control can be enabled by FF_FS_LOCK option. The value of option defines the number of open objects to manage simultaneously. In this case, if any opening, renaming or removing against the file shareing rule that described above is attempted, the file function will be rejected with FR_LOCKED. If number of open objects, files and sub-directories, is equal to FF_FS_LOCK, an extra f_open/f_opendir function will fail with FR_TOO_MANY_OPEN_FILES.

-
- -
-

Performance Effective File Access

-

For good read/write throughput on the small embedded systems with limited size of memory, application programmer should consider what process is done in the FatFs module. The file data on the volume is transferred in following sequence by f_read function.

-

Figure 1. Sector unaligned read (short)
- -

-

Figure 2. Sector unaligned read (long)
- -

-

Figure 3. Fully sector aligned read
- -

-

The file I/O buffer is a sector buffer to read/write a part of data on the sector. The sector buffer is either file private sector buffer on each file object or shared sector buffer in the filesystem object. The buffer configuration option FF_FS_TINY determins which sector buffer is used for the file data transfer. When tiny buffer configuration (1) is selected, data memory consumption is reduced FF_MAX_SS bytes each file object. In this case, FatFs module uses only a sector buffer in the filesystem object for file data transfer and FAT/directory access. The disadvantage of the tiny buffer configuration is: the FAT data cached in the sector buffer will be lost by file data transfer and it must be reloaded at every cluster boundary. However it will be suitable for most application from view point of the decent performance and low memory comsumption.

-

Figure 1 shows that a partial sector, sector unaligned part of the file, is transferred via the file I/O buffer. At long data transfer shown in Figure 2, middle of transfer data that covers one or more sector is transferred to the application buffer directly. Figure 3 shows that the case of entier transfer data is aligned to the sector boundary. In this case, file I/O buffer is not used. On the direct transfer, the maximum extent of sectors are read with disk_read function at a time but the multiple sector transfer is divided at cluster boundary even if it is contiguous.

-

Therefore taking effort to sector aligned read/write accesss eliminates buffered data transfer and the read/write performance will be improved. Besides the effect, cached FAT data will not be flushed by file data transfer at the tiny configuration, so that it can achieve same performance as non-tiny configuration with small memory footprint.

-
- -
-

Considerations on Flash Memory Media

-

To maximize the write performance of flash memory media, such as SDC, CFC and U Disk, it must be controlled in consideration of its characteristitcs.

-

Using Mutiple-Sector Write

-
-Figure 6. Comparison between Multiple/Single Sector Write
-fig.6 -
-

The write throughput of the flash memory media becomes the worst at single sector write transaction. The write throughput increases as the number of sectors per a write transaction as shown in Figure 6. This effect more appers at faster interface speed and the performance ratio often becomes grater than ten. This result is clearly explaining how fast is multiple block write (W:16K, 32 sectors) than single block write (W:100, 1 sector), and also larger card tends to be slow at single block write. Number of write transactions also affects life time of the flash memory media. When compared at same amount of write data, the single sector write in Figure 6 above wears flash memory media 16 times more than multiple sector write in Figure 6 below. Single sector write is pretty pain for the flash memory media.

-

Therefore the application program should write the data in large block as possible. The ideal write chunk size and alighment is size of sector, and size of cluster is the best. Of course all layers between the application and the storage device must have consideration on multiple sector write, however most of open-source memory card drivers lack it. Do not split a multiple sector write request into single sector write transactions or the write throughput gets poor. Note that FatFs module and its sample disk drivers supprt multiple sector read/write operation.

-

Forcing Memory Erase

-

When remove a file with f_unlink function, the data clusters occupied by the file are marked 'free' on the FAT. But the data sectors containing the file data are not that applied any process, so that the file data left occupies a part of the flash memory array as 'live block'. If the file data can be erased on removing the file, those data blocks will be turned into the free block pool. This may skip internal block erase operation to the data block on next write operation. As the result the write performance might be improved. FatFs can manage this function by setting FF_USE_TRIM to 1. Note that because this effect is from an expectation of internal process of the storage device, it is not that always effective. Most applications will not need this function. Also f_unlink function can take a time when remove a large file.

-
- -
-

Critical Section

-

If a write operation to the FAT volume is interrupted due to an accidental failure, such as sudden blackout, wrong media removal and unrecoverable disk error, the FAT structure on the volume can be broken. Following images shows the critical section of the FatFs module.

-
-Figure 4. Long critical section
-fig.4 -
-
-Figure 5. Minimized critical section
-fig.5 -
-
-

An interruption in the red section can cause a cross link; as a result, the object being changed can be lost. If an interruption in the yellow section is occured, there is one or more possibility listed below.

-
    -
  • The file data being rewrited is collapsed.
  • -
  • The file being appended returns initial state.
  • -
  • The file created as new is gone.
  • -
  • The file created as new or overwritten remains but no content.
  • -
  • Efficiency of disk use gets worse due to lost clusters.
  • -
-

Each case does not affect any file not opened in write mode. To minimize risk of data loss, the critical section can be minimized by minimizing the time that file is opened in write mode or using f_sync function as shown in Figure 5.

-
- -
-

Various Usable Functions for FatFs Projects

-

These are examples of extended use of FatFs APIs. New item will be added when useful code example is found.

-
    -
  1. Open or Create File for Append (superseded by FA_OPEN_APPEND flag added at R0.12)
  2. -
  3. Delete Non-empty Sub-directory (for R0.12 and later)
  4. -
  5. Create Contiguous File (superseded by f_expand function added at R0.12)
  6. -
  7. Test if the File is Contiguous or Not
  8. -
  9. Compatibility Checker for Storage Device Control Module
  10. -
  11. Performance Checker for Storage Device Control Module
  12. -
  13. FAT Volume Image Creator (Pre-creating built-in FAT volume)
  14. -
  15. Virtual Drive Feature (refer to lpc176x/ in ffsample.zip)
  16. -
  17. Embedded Unicode String Utilities (OEMxxx→Unicode, Unicode→OEMxxx, Unicode→Unicode)
  18. -
-
- -
-

About FatFs License

-

FatFs has being developped as a personal project of the author, ChaN. It is free from the code anyone else wrote at current release. Following code block shows a copy of the FatFs license document that included in the source files.

-
-/*----------------------------------------------------------------------------/
-/  FatFs - Generic FAT Filesystem Module  Rx.xx                               /
-/-----------------------------------------------------------------------------/
-/
-/ Copyright (C) 20xx, ChaN, all right reserved.
-/
-/ FatFs module is an open source software. Redistribution and use of FatFs in
-/ source and binary forms, with or without modification, are permitted provided
-/ that the following condition is met:
-/
-/ 1. Redistributions of source code must retain the above copyright notice,
-/    this condition and the following disclaimer.
-/
-/ This software is provided by the copyright holder and contributors "AS IS"
-/ and any warranties related to this software are DISCLAIMED.
-/ The copyright owner or contributors be NOT LIABLE for any damages caused
-/ by use of this software.
-/----------------------------------------------------------------------------*/
-
-

Therefore FatFs license is one of the BSD-style licenses but there is a significant feature. FatFs is mainly intended for embedded systems. In order to extend the usability for commercial products, the redistributions of FatFs in binary form, such as embedded code, binary library and any forms without source code, does not need to include about FatFs in the documentations. This is equivalent to the 1-clause BSD license. Of course FatFs is compatible with the most of open source software licenses includes GNU GPL. When you redistribute the FatFs source code with any changes or create a fork, the license can also be changed to GNU GPL, BSD-style license or any open source software license that compatible with FatFs license.

-
- -

Return Home

- - +

FatFs module supports the long file name (LFN) extension of the FAT filesystem. The two different file names, short file name (SFN) and LFN, of a file are transparent on the API. The support for LFN feature is disabled by default. To enable the LFN, set FF_USE_LFN to 1, 2 or 3, and add ffunicode.c to the project. The LFN feature requiers a certain working buffer. The buffer size can be configured by FF_MAX_LFN according to the available memory. The LFN specification allows the length of LFN up to 255 characters, so that the FF_MAX_LFN should be set to 255 for every existing file name. If the size of working buffer is insufficient for the input file name, the file function fails with FR_INVALID_NAME. When use any re-entry to the file API with LFN feature in RTOS environment, FF_USE_LFN must be set to 2 or 3. In this case, the file function allocates the working buffer on the stack or heap. The LFN working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled.

+

Code Page and Module Size

+ + + + + + + + + +
With LFN at CM3 + gcc
FF_CODE_PAGECode size
437-869 (SBCS)+3.3k
932 (Japanese)+62k
936 (Simplified Chinese)+177k
949 (Korean)+140k
950 (Traditional Chinese)+111k
0 (All code pages)+486k
+

When the LFN is enabled, the module size will be increased depends on the configured code page. Right table shows the increment of code size in some code pages. Especially, in the CJK region, tens of thousands of characters are being used. Unfortunately, it requires a huge OEM-Unicode bidirectional conversion table and the module size will be drastically increased as shown in the table.

+

As the result, the FatFs with LFN enabled with DBCS code pages will not able to be ported on the most 8-bit MCU systems. If the target system is in legacy-free, in only Unicode and any ANSI/OEM code is not used at all, the code page setting is meaningless. You will able to reduce the code size by configureing FatFs for Unicode API with some SBCS code page.

+

There ware some restrictions on using the LFN in open source project, because the LFN extension on the FAT filesystem was a patent of Microsoft Corporation. However the related patents all have expired and using the LFN feature is free for any project.

+
+ +
+

Unicode API

+

By default, FatFs uses ANSI/OEM code set on the API even in LFN configuration. FatFs can also switch the character encoding on the API to Unicode by configuration option FF_LFN_UNICODE. This means that FatFs is compliant with the full featured LFN specification. The data type TCHAR specifies path name strings on the API is an alias of either char(ANSI/OEM or UTF-8), WCHAR(UTF-16) or DWORD(UTF-32) depends on that option. For more information, refer to the description in the file name.

+

Note that setting of code page, FF_CODE_PAGE, has actually no meaning when FatFs is configured for the Unicode API. It should be set 437 to reduce the module size. However it still affects code conversion of string I/O functions when FF_STRF_ENCODE == 0, and also backward compatibility with legacy systems. In this case, the code page may need to be configured properly if it is considered a problem.

+
+ +
+

exFAT Filesystem

+

The exFAT (Microsoft's Extended File Allocation Table) filesystem is a succession of the FAT/FAT32 filesystem which has been widely used in embedded systems, consumer devices and portable storage media. It is adopted by SDA (SD Association) as the filesystem for SDXC card, 64 GB and larger, and they are being shipped with this format. Therefore the exFAT is one of the standard filesystems for removable media as well as FAT. The exFAT filesystem allows the file size beyond the 4 GB limit what FAT filesystem allows up to and some filesystem overhead, especially cluster allocation delay, are reduced as well. These features allow to record the large data without dividing into some files and improve the write throughput to the file.

+

Note that the exFAT filesystem is a patent of Microsoft Corporation. The exFAT feature of FatFs is an implementation based on US. Pat. App. Pub. No. 2009/0164440 A1. FatFs module can switch the exFAT on or off by a configuration option, FF_FS_EXFAT. When enable the exFAT for the commercial products, a license by Microsoft will be needed depends on the final destination of the products.

+

Remarks: Enabling exFAT discards C89 compatibility and it wants C99 because of need for 64-bit integer type.

+
+ +
+

64-bit LBA

+

LBA (Logical Block Addressing) is an addressing method to specify the location of data block, called sector, on the storage media. It is a simple linear address beginning from 0 as the first sector, 1 as the second sector and so on. The host system does not need to consider how the data block is located and managed in the storage device. FatFs supports only LBA for the media access. 32-bit LBA is a common size in the most LBA scheme. It can address up to 232 sectors, 2 TB in 512 bytes/sector. When a storage device larger than 2 TB is used, larger sector size or 64-bit LBA will be needed to address the entire sectors of the storage device.

+

By default, FatFs works in 32-bit LBA for media access. FatFs can also switch it to 64-bit LBA by a configuration option FF_LBA64. It also enables GPT (GUID Partition Table) for partition management on the storage device. For further information about GPT, refer to f_mkfs and f_fdisk function.

+
+ +
+

Re-entrancy

+

The file operations of two tasks to the different volumes each other is always re-entrant and it can work concurrently without any mutual exclusion regardless of the configurations except when LFN is enabled with static working buffer (FF_USE_LFN = 1).

+

The file operations of two tasks to the same volume is not thread-safe by default. FatFs can also be configured to make it thread-safe by an option FF_FS_REENTRANT. When a file function is called while the volume is being accessed by another task, the file function to the volume will be suspended until that task leaves the file function. If the wait time exceeded a period defined by FF_TIMEOUT, the file function will abort with FR_TIMEOUT. The timeout feature might not be supported on the some OSs. To enable this feature, OS dependent synchronization control functions, ff_mutex_create/ff_mutex_delete/ff_mutex_take/ff_mutex_give, need to be added to the project. There is an example code in the ffsystem.c for some OSs.

+

Note that there is an exception on the re-entrancy for f_mount and f_mkfs function. You will know why it is. These volume management functions are always not thread-safe to the volume being processed. When use these functions, other tasks need to avoid to access the corresponding volume.

+
+ + + + + + + + +
FunctionCase 1Case 2Case 3
disk_statusYesYesYes(*)
disk_initializeNoYesYes(*)
disk_readNoYesYes(*)
disk_writeNoYesYes(*)
disk_ioctlNoYesYes(*)
get_fattimeNoYesYes
+ +Case 1: Same volume.
+Case 2: Different volume on the same drive.
+Case 3: Different volume on the different drive.
+(*) In only different drive number. +
+
+

Remarks: This section describes on the re-entrancy of the FatFs module itself. The FF_FS_REENTRANT option enables only exclusive use of each filesystem objects and FatFs does not that prevent to re-enter the storage device control functions. Thus the device control layer needs to be always thread-safe when FatFs API is re-entered for different volumes. Right table shows which control function can be re-entered when FatFs API is re-entered on some conditions.

+
+ +
+

Duplicated File Open

+

FatFs module does not support the read/write collision control of duplicated open to a file. The duplicated open is permitted only when each of open method to a file is read mode. The duplicated open with one or more write mode to a file is always prohibited, and also open file must not be renamed or deleted. A violation of these rules can cause data collaption.

+

The file lock control can be enabled by FF_FS_LOCK option. The value of option defines the number of open objects to manage simultaneously. In this case, if any opening, renaming or removing against the file shareing rule that described above is attempted, the file function will be rejected with FR_LOCKED. If number of open objects, files and sub-directories, is equal to FF_FS_LOCK, an extra f_open/f_opendir function will fail with FR_TOO_MANY_OPEN_FILES.

+
+ +
+

Performance Effective File Access

+

For good read/write throughput on the small embedded systems with limited size of memory, application programmer should consider what process is done in the FatFs module. The file data on the volume is transferred in following sequence by f_read function.

+

Figure 1. Sector unaligned read (short)
+ +

+

Figure 2. Sector unaligned read (long)
+ +

+

Figure 3. Fully sector aligned read
+ +

+

The file I/O buffer is a sector buffer to read/write a part of data on the sector. The sector buffer is either file private sector buffer on each file object or shared sector buffer in the filesystem object. The buffer configuration option FF_FS_TINY determins which sector buffer is used for the file data transfer. When tiny buffer configuration (1) is selected, data memory consumption is reduced FF_MAX_SS bytes each file object. In this case, FatFs module uses only a sector buffer in the filesystem object for file data transfer and FAT/directory access. The disadvantage of the tiny buffer configuration is: the FAT data cached in the sector buffer will be lost by file data transfer and it must be reloaded at every cluster boundary. However it will be suitable for most application from view point of the decent performance and low memory comsumption.

+

Figure 1 shows that a partial sector, sector unaligned part of the file, is transferred via the file I/O buffer. At long data transfer shown in Figure 2, middle of transfer data that covers one or more sector is transferred to the application buffer directly. Figure 3 shows that the case of entier transfer data is aligned to the sector boundary. In this case, file I/O buffer is not used. On the direct transfer, the maximum extent of sectors are read with disk_read function at a time but the multiple sector transfer is divided at cluster boundary even if it is contiguous.

+

Therefore taking effort to sector aligned read/write accesss eliminates buffered data transfer and the read/write performance will be improved. Besides the effect, cached FAT data will not be flushed by file data transfer at the tiny configuration, so that it can achieve same performance as non-tiny configuration with small memory footprint.

+
+ +
+

Considerations on Flash Memory Media

+

To maximize the write performance of flash memory media, such as SDC, CFC and U Disk, it must be controlled in consideration of its characteristitcs.

+

Using Mutiple-Sector Write

+
+Figure 6. Comparison between Multiple/Single Sector Write
+fig.6 +
+

The write throughput of the flash memory media becomes the worst at single sector write transaction. The write throughput increases as the number of sectors per a write transaction as shown in Figure 6. This effect more appers at faster interface speed and the performance ratio often becomes grater than ten. This result is clearly explaining how fast is multiple block write (W:16K, 32 sectors) than single block write (W:100, 1 sector), and also larger card tends to be slow at single block write. Number of write transactions also affects life time of the flash memory media. When compared at same amount of write data, the single sector write in Figure 6 above wears flash memory media 16 times more than multiple sector write in Figure 6 below. Single sector write is pretty pain for the flash memory media.

+

Therefore the application program should write the data in large block as possible. The ideal write chunk size and alighment is size of sector, and size of cluster is the best. Of course all layers between the application and the storage device must have consideration on multiple sector write, however most of open-source memory card drivers lack it. Do not split a multiple sector write request into single sector write transactions or the write throughput gets poor. Note that FatFs module and its sample disk drivers supprt multiple sector read/write operation.

+

Forcing Memory Erase

+

When remove a file with f_unlink function, the data clusters occupied by the file are marked 'free' on the FAT. But the data sectors containing the file data are not that applied any process, so that the file data left occupies a part of the flash memory array as 'live block'. If the file data can be erased on removing the file, those data blocks will be turned into the free block pool. This may skip internal block erase operation to the data block on next write operation. As the result the write performance might be improved. FatFs can manage this function by setting FF_USE_TRIM to 1. Note that because this effect is from an expectation of internal process of the storage device, it is not that always effective. Most applications will not need this function. Also f_unlink function can take a time when remove a large file.

+
+ +
+

Critical Section

+

If a write operation to the FAT volume is interrupted due to an accidental failure, such as sudden blackout, wrong media removal and unrecoverable disk error, the FAT structure on the volume can be broken. Following images shows the critical section of the FatFs module.

+
+Figure 4. Long critical section
+fig.4 +
+
+Figure 5. Minimized critical section
+fig.5 +
+
+

An interruption in the red section can cause a cross link; as a result, the object being changed can be lost. If an interruption in the yellow section is occured, there is one or more possibility listed below.

+ +

Each case does not affect any file not opened in write mode. To minimize risk of data loss, the critical section can be minimized by minimizing the time that file is opened in write mode or using f_sync function as shown in Figure 5.

+
+ +
+

Various Usable Functions for FatFs Projects

+

These are examples of extended use of FatFs APIs. New item will be added when useful code example is found.

+
    +
  1. Open or Create File for Append (superseded by FA_OPEN_APPEND flag added at R0.12)
  2. +
  3. Delete Non-empty Sub-directory (for R0.12 and later)
  4. +
  5. Create Contiguous File (superseded by f_expand function added at R0.12)
  6. +
  7. Test if the File is Contiguous or Not
  8. +
  9. Compatibility Checker for Storage Device Control Module
  10. +
  11. Performance Checker for Storage Device Control Module
  12. +
  13. FAT Volume Image Creator (Pre-creating built-in FAT volume)
  14. +
  15. Virtual Drive Feature (refer to lpc176x/ in ffsample.zip)
  16. +
  17. Embedded Unicode String Utilities (OEMxxx→Unicode, Unicode→OEMxxx, Unicode→Unicode)
  18. +
+
+ +
+

About FatFs License

+

FatFs has being developped as a personal project of the author, ChaN. It is free from the code anyone else wrote at current release. Following code block shows a copy of the FatFs license document that included in the source files.

+
+/*----------------------------------------------------------------------------/
+/  FatFs - Generic FAT Filesystem Module  Rx.xx                               /
+/-----------------------------------------------------------------------------/
+/
+/ Copyright (C) 20xx, ChaN, all right reserved.
+/
+/ FatFs module is an open source software. Redistribution and use of FatFs in
+/ source and binary forms, with or without modification, are permitted provided
+/ that the following condition is met:
+/
+/ 1. Redistributions of source code must retain the above copyright notice,
+/    this condition and the following disclaimer.
+/
+/ This software is provided by the copyright holder and contributors "AS IS"
+/ and any warranties related to this software are DISCLAIMED.
+/ The copyright owner or contributors be NOT LIABLE for any damages caused
+/ by use of this software.
+/----------------------------------------------------------------------------*/
+
+

Therefore FatFs license is one of the BSD-style licenses but there is a significant feature. FatFs is mainly intended for embedded systems. In order to extend the usability for commercial products, the redistributions of FatFs in binary form, such as embedded code, binary library and any forms without source code, does not need to include about FatFs in the documentations. This is equivalent to the 1-clause BSD license. Of course FatFs is compatible with the most of open source software licenses includes GNU GPL. When you redistribute the FatFs source code with any changes or create a fork, the license can also be changed to GNU GPL, BSD-style license or any open source software license that compatible with FatFs license.

+
+ + +

Return

+ + diff --git a/vendor/fatfs/documents/doc/chdir.html b/vendor/fatfs/documents/doc/chdir.html index 93e116c..a91ab0f 100644 --- a/vendor/fatfs/documents/doc/chdir.html +++ b/vendor/fatfs/documents/doc/chdir.html @@ -3,8 +3,6 @@ - - FatFs - f_chdir @@ -83,6 +81,7 @@

See Also

f_chdrive, f_getcwd

+

Return

diff --git a/vendor/fatfs/documents/doc/chdrive.html b/vendor/fatfs/documents/doc/chdrive.html index 4dfc7d8..fd821c4 100644 --- a/vendor/fatfs/documents/doc/chdrive.html +++ b/vendor/fatfs/documents/doc/chdrive.html @@ -3,8 +3,6 @@ - - FatFs - f_chdrive @@ -65,6 +63,7 @@

See Also

f_chdir, f_getcwd

+

Return

diff --git a/vendor/fatfs/documents/doc/chmod.html b/vendor/fatfs/documents/doc/chmod.html index 91e520f..b7173fd 100644 --- a/vendor/fatfs/documents/doc/chmod.html +++ b/vendor/fatfs/documents/doc/chmod.html @@ -3,8 +3,6 @@ - - FatFs - f_chmod @@ -27,7 +25,7 @@

f_chmod

Parameters

path
-
Pointer to the null-terminated string that specifies an object to be changed
+
Pointer to the null-terminated string that specifies an object to be changed. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
attr
Attribute flags to be set in one or more combination of the following flags. The specified flags are set and others are cleard.
@@ -84,6 +82,7 @@

Example

+

Return

diff --git a/vendor/fatfs/documents/doc/close.html b/vendor/fatfs/documents/doc/close.html index f4cd2df..3fe9827 100644 --- a/vendor/fatfs/documents/doc/close.html +++ b/vendor/fatfs/documents/doc/close.html @@ -3,8 +3,6 @@ - -FatFs - f_close @@ -60,6 +58,7 @@

See Also

f_open, f_read, f_write, f_sync, FIL, FATFS

+

Return

diff --git a/vendor/fatfs/documents/doc/closedir.html b/vendor/fatfs/documents/doc/closedir.html index 3f5df57..bf4febe 100644 --- a/vendor/fatfs/documents/doc/closedir.html +++ b/vendor/fatfs/documents/doc/closedir.html @@ -3,8 +3,6 @@ - -FatFs - f_closedir @@ -59,6 +57,7 @@

See Also

f_opendir, f_readdir, DIR

+

Return

diff --git a/vendor/fatfs/documents/doc/config.html b/vendor/fatfs/documents/doc/config.html index a4c7814..d3977df 100644 --- a/vendor/fatfs/documents/doc/config.html +++ b/vendor/fatfs/documents/doc/config.html @@ -3,15 +3,13 @@ - -FatFs - Configuration Options

Configuration Options

-

There are many options to configure the functions of FatFs for requirement of each project. The configuration options are defined in the ffconf.h.

+

There are many options to configure the features of FatFs for various requirements of each project. The configuration options are defined in ffconf.h.

- - + +
ValueDescription
0Disable string functions.
1Enable string functions without LF-CRLF conversion.
2Enable string functions with LF-CRLF conversion.
1Enable string functions without LF - CRLF conversion.
2Enable string functions with LF - CRLF conversion.

This option switches support for long long integer argument in f_printf.

-

Disable (0) or Enable (1). C standard needs to be C99 or later to enable this feature.

+

Disable (0) or Enable (1). When enable this feature, C standard needs to be C99 or later. This option has no effect when FF_USE_STRFUNC == 0.

-

This option switches support for floating point argument in f_printf. C standard needs to be C99 or later to enable this feature.

+

This option switches support for floating point argument in f_printf. When enable this feature, C standard needs to be C99 or later and math.h is included in ff.c. This option has no effect when FF_USE_STRFUNC == 0.

- +
ValueDescription
0Disable floating point argument.
1Enable floating point argument in type 'f', 'e' and 'E'.
2Enable with decimal separator ',' instead of '.'.
2Same as 1 but with decimal separator ',' instead of '.' in output string.

FF_STRF_ENCODE

-

When character encoding on the API is Unicode (FF_LFN_UNICODE >= 1), string I/O functions enabled by FF_USE_STRFUNC convert the character encoding in it. This option defines the assumption of character encoding on the file to be read/written via those functions. When LFN is not enabled or FF_LFN_UNICODE == 0, the string functions work without any code conversion and this option has no effect.

+

When the character encoding on the API is Unicode (FF_LFN_UNICODE >= 1), string I/O functions enabled by FF_USE_STRFUNC convert the character encoding in it. This option defines the assumption of character encoding on the file to be read/written via the string I/O functions. When LFN is not enabled or FF_LFN_UNICODE == 0, the string I/O functions work without any code conversion and this option has no effect.

@@ -210,8 +209,8 @@

FF_FS_RPATH

This option configures relative path function. For more information, read here.

ValueCharacter encoding on the file
0ANSI/OEM in current code page
- - + +
ValueDescription
0Disable relative path function and remove related functions.
1Enable relative path function. f_chdir and f_chdrive function is available.
0Disable relative path and remove related functions.
1Enable relative path. f_chdir and f_chdrive function is available.
2f_getcwd function is available in addition to 1
@@ -228,9 +227,9 @@

FF_STR_VOLUME_ID

This option switches the support for string volume ID. When arbitrary string for the volume ID is enabled for the drive prefix, also pre-defined strings by FF_VOLUME_STRS or user defined strings can be used as drive prefix in the path name. Numeric drive number is always valid regardless of this option, and also either format of drive prefix can be enabled by this option.

- - - + + +
ValueDescriptionExample
0Only DOS/Windows style drive prefix in numeric ID can be used.1:/filename
1Also DOS/Windows style drive prefix in string ID can be used.flash:/filename
2Also Unix style drive prefix in string ID can be used./flash/filename
0DOS/Windows style drive prefix in numeric ID.1:/filename
10 + DOS/Windows style drive prefix in arbitry string ID.flash:/filename
20 + Unix style drive prefix in arbitry string ID./flash/filename

FF_VOLUME_STRS

@@ -241,7 +240,7 @@

FF_VOLUME_STRS

FF_MULTI_PARTITION

-

Disable (0) or Enable (1). This option switches multi-partition function. By default (0), each logical drive number is bound to the same physical drive number and only a volume in the physical drive is mounted. When enabled, each logical drive is bound to the partition on the physical drive listed in the user defined partition resolution table VolToPart[]. Also f_fdisk funciton will be available. For more information, read here.

+

This option switches multi-partition featuer. By default (0), each logical drive number is bound to the same physical drive number and only one volume found in the physical drive is mounted. When it is enabled (1), each logical drive is bound to the specific partition listed in the user defined partition resolution table VolToPart[]. Also f_fdisk funciton is available to create the arbitrary partitions on the physical drive. For more information, read here.

FF_MIN_SS, FF_MAX_SS

This set of options defines the extent of sector size used for the low level disk I/O interface, disk_read and disk_write function. Valid values are 512, 1024, 2048 and 4096. FF_MIN_SS defines minimum sector size and FF_MAX_SS defines the maximum sector size. Always set both 512 for memory card and harddisk. But a larger value may be required for on-board flash memory and some type of optical media. When FF_MAX_SS > FF_MIN_SS, support of variable sector size is enabled and GET_SECTOR_SIZE command needs to be implemented to the disk_ioctl function.

@@ -250,7 +249,7 @@

FF_LBA64

This option switches media access interface to 64-bit LBA and enables GUID Partition Table (GPT) for partition management, Enabled (1) or Disabled (0). exFAT filesystem needs to be enabled to enable this feature.

FF_MIN_GPT

-

This option specifies the threshold of determination of partitioning format when create patitions on the drive in f_mkfs and f_fdisk function. When number of sectors on the drive is equal or larger than this value, the drive will be partitioned in GPT. This option has no effect when FF_LBA64 == 0.

+

This option specifies the threshold of determination of partitioning format when create patitions on the drive in f_mkfs and f_fdisk function. When number of available sectors is equal or larger than this value, the drive will be partitioned in GPT. This option has no effect when FF_LBA64 == 0.

FF_USE_TRIM

Disable (0) or Enable (1). This option switches ATA-TRIM function. To enable Trim function, also CTRL_TRIM command should be implemented to the disk_ioctl function.

@@ -268,13 +267,16 @@

FF_FS_EXFAT

This option switches support for exFAT filesystem in addition to the FAT/FAT32 filesystem, Enabled (1) or Disabled (0). To enable exFAT, also LFN must be enabled and configureing FF_LFN_UNICODE >= 1 and FF_MAX_LFN == 255 is recommended for full-featured exFAT function. Note that enabling exFAT discards ANSI C (C89) compatibility and wants C99 because of need for 64-bit integer type.

FF_FS_NORTC

-

Use RTC (0) or Do not use RTC (1). This option controls timestamp function. If the system does not have any RTC function or valid timestamp is not needed, set FF_FS_NORTC to 1 to disable the timestamp function. Every objects modified by FatFs will have a fixed timestamp defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR. To use the timestamp function, set FF_FS_NORTC == 0 and add get_fattime function to the project to get current time form the RTC. This option has no effect in read-only configuration.

+

Use RTC (0) or Do not use RTC (1). This option controls timestamp featuer. If the system does not have an RTC or valid timestamp is not needed, set FF_FS_NORTC to 1 to disable the timestamp function. Every objects modified by FatFs will have a constant timestamp defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR. To use the timestamp featuer, set FF_FS_NORTC == 0 and add get_fattime function to the project to get current time form the RTC. This option has no effect in read-only configuration.

FF_NORTC_MON, FF_NORTC_MDAY, FF_NORTC_YEAR

This set of options defines the time to be used in no RTC systems. This option has no effect in read-only configuration or FF_FS_NORTC == 0.

+

FF_FS_CTIME

+

This option enables (1) or disables (0) to access the file creation time with FILINFO structure.

+

FF_FS_NOFSINFO

-

0 to 3. If you need to know correct free space on the FAT32 volume, set bit 0 of this option, and f_getfree function at first time after volume mount will force a full FAT scan. Bit 1 controls the use of last allocated cluster number for new allocation.

+

0 to 3. If you need to know correct free space on the FAT32 volume, set bit 0 of this option, and f_getfree function at first time after the volume mounted will force a full FAT scan. Bit 1 controls the use of last allocated cluster number for new allocation.

@@ -284,24 +286,22 @@

FF_FS_NOFSINFO

ValueDescription
bit0=0Use free cluster count in the FSINFO if available.

FF_FS_LOCK

-

This option switches file lock function to control duplicated file open and illegal operations to open objects. Note that the file lock function is independent of re-entrancy. This option must be 0 in read-only configuration.

+

This option switches file lock feature to control duplicated file open and illegal operations to the open objects. Note that this feature is independent of re-entrancy. This option must be 0 in read-only configuration.

- - + +
ValueDescription
0Disable file lock function. To avoid collapsing file by wrong file operation, application program needs to avoid illegal open, remove and rename to the open objects.
>0Enable file lock function. The value defines how many files/sub-directories can be opened simultaneously under the file lock control. Illigal operations to the open object will be rejected with FR_LOCKED.
0Disable file lock feature. To avoid to collapse files due to wrong file operations, application program needs to avoid illegal open, remove and rename to the open objects.
>0Enable file lock feature. The value defines how many files/sub-directories can be opened simultaneously under the file lock feature. Illigal operations to the open object will be rejected with FR_LOCKED.

FF_FS_REENTRANT

-

Disable (0) or Enable (1). This option switches the re-entrancy (thread safe) of the FatFs module itself. Note that file/directory access to the different volume is always re-entrant and it can work simultaneously regardless of this option, however, volume management functions, f_mount, f_mkfs and f_fdisk, are always not re-entrant. Only file/directory access to the same volume, in other words, exclusive use of each filesystem object, is under control of this function. To enable this feature, also user provided synchronization handlers, ff_req_grant, ff_rel_grant, ff_del_syncobj and ff_cre_syncobj, need to be added to the project. Sample code is available in ffsystem.c.

+

Disable (0) or Enable (1). This option switches the re-entrancy (thread safe) of the FatFs module itself. Note that file/directory access to the different volume is always re-entrant and it can work simultaneously regardless of this option, however, volume management functions, f_mount, f_mkfs and f_fdisk, are always not re-entrant. Only file/directory access to the same volume, in other words, exclusive use of each filesystem object, is under control in this feature. To enable this feature, also user provided synchronization handlers, ff_mutex_take, ff_mutex_give, ff_mutex_create and ff_mutex_delete, need to be added to the project. Sample code is available in ffsystem.c.

FF_FS_TIMEOUT

-

Number of time ticks to abort the file function with FR_TIMEOUT when wait time is too long. This option has no effect when FF_FS_REENTRANT == 0.

- -

FF_SYNC_t

-

This option defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*, SemaphoreHandle_t and etc. A header file for O/S definitions needs to be included somewhere in the scope of ff.c. This option has no effect when FF_FS_REENTRANT == 0.

+

Number of O/S time ticks to abort the file function with FR_TIMEOUT when the wait time exceeds this period. This option has no effect when FF_FS_REENTRANT == 0.

+

Return

diff --git a/vendor/fatfs/documents/doc/dinit.html b/vendor/fatfs/documents/doc/dinit.html index c668c66..688118c 100644 --- a/vendor/fatfs/documents/doc/dinit.html +++ b/vendor/fatfs/documents/doc/dinit.html @@ -3,8 +3,6 @@ - - FatFs - disk_initialize @@ -41,6 +39,7 @@

Description

Remarks: This function needs to be under control of FatFs module. Application program MUST NOT call this function while FatFs is in use, or FAT structure on the volume can be broken. To re-initialize the filesystem, use f_mount function instead.

+

Return

diff --git a/vendor/fatfs/documents/doc/dioctl.html b/vendor/fatfs/documents/doc/dioctl.html index 5208782..3c45bc9 100644 --- a/vendor/fatfs/documents/doc/dioctl.html +++ b/vendor/fatfs/documents/doc/dioctl.html @@ -3,8 +3,6 @@ - - FatFs - disk_ioctl @@ -57,11 +55,11 @@

Description

- - - - - + + + + +
Standard ioctl command used by FatFs
CommandDescription
CTRL_SYNCMakes sure that the device has finished pending write process. If the disk I/O layer or storage device has a write-back cache, the dirty cache data must be committed to media immediately. Nothing to do for this command if each write operation to the media is completed within the disk_write function.
GET_SECTOR_COUNTRetrieves number of available sectors, the largest allowable LBA + 1, on the drive into the LBA_t variable pointed by buff. This command is used by f_mkfs and f_fdisk function to determine the size of volume/partition to be created. It is required when FF_USE_MKFS == 1.
GET_SECTOR_SIZERetrieves sector size, minimum data unit for generic read/write, into the WORD variable pointed by buff. Valid sector sizes are 512, 1024, 2048 and 4096. This command is required only if FF_MAX_SS > FF_MIN_SS. When FF_MAX_SS == FF_MIN_SS, this command will be never used and the read/write function must work in FF_MAX_SS bytes/sector.
GET_BLOCK_SIZERetrieves erase block size of the flash memory media in unit of sector into the DWORD variable pointed by buff. The allowable value is 1 to 32768 in power of 2. Return 1 if the value is unknown or non flash memory media. This command is used by only f_mkfs function and it attempts to align data area on the erase block boundary. It is required when FF_USE_MKFS == 1.
CTRL_TRIMInforms the device that the data on the block of sectors is no longer needed and it can be erased. The sector block is specified in an LBA_t array {<Start LBA>, <End LBA>} pointed by buff. This is an identical command to Trim of ATA device. Nothing to do for this command if this funcion is not supported or not a flash memory device. FatFs does not check the result code and the file function is not affected even if the sector block was not erased well. This command is called on remove a cluster chain and in the f_mkfs function. It is required when FF_USE_TRIM == 1.
CTRL_SYNCMakes sure that the device has finished pending write process. If the disk I/O layer or storage device has a write-back cache, the dirty cache data must be committed to the medium immediately. Nothing to do for this command if each write operation to the medium is completed in the disk_write function.
GET_SECTOR_COUNTRetrieves number of available sectors (the largest allowable LBA + 1) on the drive into the LBA_t variable that pointed by buff. This command is used by f_mkfs and f_fdisk function to determine the size of volume/partition to be created.
GET_SECTOR_SIZERetrieves sector size (minimum data unit for generic read/write) into the WORD variable that pointed by buff. Valid sector sizes are 512, 1024, 2048 and 4096. This command is required only if FF_MAX_SS > FF_MIN_SS. When FF_MAX_SS == FF_MIN_SS, this command will never be used and the disk_read and disk_write function must work in FF_MAX_SS bytes/sector.
GET_BLOCK_SIZERetrieves erase block size in unit of sector of the flash memory media into the DWORD variable that pointed by buff. The allowable value is 1 to 32768 in power of 2. Return 1 if it is unknown or in non flash memory media. This command is used by f_mkfs function with block size not specified and it attempts to align the data area on the suggested block boundary. Note that FatFs does not have FTL (flash translation layer), so that either disk I/O layter or storage device must have an FTL in it.
CTRL_TRIMInforms the disk I/O layter or the storage device that the data on the block of sectors is no longer needed and it can be erased. The sector block is specified in an LBA_t array {<Start LBA>, <End LBA>} that pointed by buff. This is an identical command to Trim of ATA device. Nothing to do for this command if this funcion is not supported or not a flash memory device. FatFs does not check the result code and the file function is not affected even if the sector block was not erased well. This command is called on remove a cluster chain and in the f_mkfs function. It is required when FF_USE_TRIM == 1.

FatFs will never use any device dependent command nor user defined command. Following table shows an example of non-standard commands which may be useful for some applications.

@@ -73,9 +71,9 @@

Description

CTRL_POWER_OFFPuts the device off state. Shut-down the power to the device and deinitialize the device interface if needed. STA_NOINIT in the current status flags must be set. The device goes active state by disk_initialize function. CTRL_LOCKLocks media eject mechanism. CTRL_UNLOCKUnlocks media eject mechanism. -CTRL_EJECTEjects media cartridge. STA_NOINIT and STA_NODISK in status flag are set after the function succeeded. +CTRL_EJECTEjects media cartridge. STA_NOINIT and STA_NODISK in status flag are set after the function succeeds. CTRL_GET_SMARTReads SMART information. -MMC_GET_TYPEGets card type. The type flags, bit0:MMCv3, bit1:SDv1, bit2:SDv2+ and bit3:LBA, is stored to a BYTE variable pointed by buff. (MMC/SDC specific command) +MMC_GET_TYPEGets card type. The type flags, bit0:MMCv3, bit1:SDv1, bit2:SDv2+ and bit3:LBA, is stored to a BYTE variable pointed by buff. (MMC/SDC specific command) MMC_GET_CSDReads CSD register and sets it into a 16-byte buffer pointed by buff. (MMC/SDC specific command) MMC_GET_CIDReads CID register and sets it into a 16-byte buffer pointed by buff. (MMC/SDC specific command) MMC_GET_OCRReads OCR register and sets it into a 4-byte buffer pointed by buff. (MMC/SDC specific command) diff --git a/vendor/fatfs/documents/doc/dread.html b/vendor/fatfs/documents/doc/dread.html index 46d2860..c87c9db 100644 --- a/vendor/fatfs/documents/doc/dread.html +++ b/vendor/fatfs/documents/doc/dread.html @@ -3,8 +3,6 @@ - - FatFs - disk_read @@ -13,7 +11,7 @@

disk_read

-

The disk_read function is called to read data from the sector(s) of storage device.

+

The disk_read function is called to read data from the storage device.

 DRESULT disk_read (
   BYTE pdrv,     /* [IN] Physical drive number */
@@ -56,14 +54,14 @@ 

Return Value

Description

-

Read/write operation to the generic storage devices, such as memory card, hadddisk and optical disk, is done in unit of block of data bytes called sector. FatFs supports the sector size in range of 512 to 4096 bytes. When FatFs is configured for fixed sector size (FF_MIN_SS == FF_MAX_SS, this is the most case), the generic read/write function must work at the sector size only. When FatFs is configured for variable sector size (FF_MIN_SS < FF_MAX_SS), the sector size of medium is inquired with disk_ioctl function after disk_initialize function succeeded.

+

Read/write operation to the generic storage devices, such as memory card, hadddisk and optical disk, is done in unit of block of data bytes called sector. FatFs supports the sector size in range of 512 to 4096 bytes. When FatFs is configured for fixed sector size (FF_MIN_SS == FF_MAX_SS, this is the most case), the generic read/write function must work at this sector size only. When FatFs is configured for variable sector size (FF_MIN_SS < FF_MAX_SS), the sector size of medium is inquired with disk_ioctl function after disk_initialize function succeeds.

There are some considerations about the memory addres passed via buff. It is not that always aligned with the word boundary, because the argument is defined as BYTE*. The unaligned transfer request can occure at direct transfer. If the bus architecture, especially DMA controller, does not allow unaligned memory access, it should be solved in this function. If it is the case, there are some workarounds described below to avoid this issue.

  • Convert word transfer to byte transfer with some trick in this function. - Recommended.
  • On the f_read() calls, avoid long read request that includes a whole of sector. - Any direct transfer never occures.
  • -
  • On the f_read(fp, dat, btw, bw) calls, make sure that (((UINT)dat & 3) == (f_tell(fp) & 3)) is true. - Word alignment of buff is guaranteed.
  • +
  • On the f_read(fp, data, btw, bw) calls, make sure that (((UINT)data & 3) == (f_tell(fp) & 3)) is true. - Word alignment of buff is guaranteed.
-

Also the memory area may be out of reach in DMA. This is the case if it is located on the tightly coupled memory which is usually used for stack. Use double buffered transfer, or avoid to define file I/O buffer, FATFS and FIL structure as local variables where on the stack.

+

Also the memory area may be out of reach in DMA. This is the case if it is located on the tightly coupled memory which is usually used for stack. Use double buffered transfer, or avoid to define file I/O buffer, FATFS and FIL structure as local variables where on the stack.

Generally, a multiple sector read request must not be split into single sector transactions to the storage device, or read throughput gets worse.

diff --git a/vendor/fatfs/documents/doc/dstat.html b/vendor/fatfs/documents/doc/dstat.html index a891489..404ca24 100644 --- a/vendor/fatfs/documents/doc/dstat.html +++ b/vendor/fatfs/documents/doc/dstat.html @@ -3,8 +3,6 @@ - - FatFs - disk_status @@ -43,6 +41,7 @@

Return Values

+

Return

diff --git a/vendor/fatfs/documents/doc/dwrite.html b/vendor/fatfs/documents/doc/dwrite.html index 1007284..eaf314d 100644 --- a/vendor/fatfs/documents/doc/dwrite.html +++ b/vendor/fatfs/documents/doc/dwrite.html @@ -3,8 +3,6 @@ - - FatFs - disk_write @@ -13,7 +11,7 @@

disk_write

-

The disk_write function is called to write data to the sector(s) of storage device.

+

The disk_write function is called to write data to the storage device.

 DRESULT disk_write (
   BYTE pdrv,        /* [IN] Physical drive number */
@@ -60,7 +58,7 @@ 

Return Values

Description

The specified memory address is not that always aligned to word boundary because the argument is defined as BYTE*. For more information, refer to the description of disk_read function.

Generally, a multiple sector write request (count > 1) must not be split into single sector transactions to the storage device, or the file write throughput will be drastically decreased.

-

FatFs expects delayed write function of the disk control layer. The write operation to the media does not need to be completed when return from this function by what write operation is in progress or data is only stored into the write-back cache. But write data on the buff is invalid after return from this function. The write completion request is done by CTRL_SYNC command of disk_ioctl function. Therefore, if a delayed write function is implemented, the write throughput of the filesystem will be improved.

+

FatFs expects the disk control layer may have a delayed write feature. The write operation to the media does not need to be completed when return from this function by what media write operation is in progress or data is stored into the write-back cache. The write data on the buff is invalid after return from this function. The write completion request is done by CTRL_SYNC command of disk_ioctl function. Therefore, if a delayed write feature is implemented, the write throughput of the filesystem will be improved.

Remarks: Application program MUST NOT call this function, or FAT structure on the volume can be collapsed.

diff --git a/vendor/fatfs/documents/doc/eof.html b/vendor/fatfs/documents/doc/eof.html index efacfbf..549ae4e 100644 --- a/vendor/fatfs/documents/doc/eof.html +++ b/vendor/fatfs/documents/doc/eof.html @@ -3,8 +3,6 @@ - - FatFs - f_eof @@ -57,6 +55,7 @@

See Also

f_open, f_lseek, FIL

+

Return

diff --git a/vendor/fatfs/documents/doc/error.html b/vendor/fatfs/documents/doc/error.html index bee9685..b122dc2 100644 --- a/vendor/fatfs/documents/doc/error.html +++ b/vendor/fatfs/documents/doc/error.html @@ -3,8 +3,6 @@ - - FatFs - f_error @@ -57,6 +55,7 @@

See Also

f_open, FIL

+

Return

diff --git a/vendor/fatfs/documents/doc/expand.html b/vendor/fatfs/documents/doc/expand.html index 7620ba9..86a0824 100644 --- a/vendor/fatfs/documents/doc/expand.html +++ b/vendor/fatfs/documents/doc/expand.html @@ -3,8 +3,6 @@ - - FatFs - f_expand @@ -28,7 +26,7 @@

f_expand

Parameters

fp
-
Pointer to the open file object.
+
Pointer to the open file object. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
fsz
Number of bytes in size to prepare or allocate for the file. The data type FSIZE_t is an alias of either DWORD(32-bit) or QWORD(64-bit) depends on the configuration option FF_FS_EXFAT.
opt
@@ -61,7 +59,7 @@

Description

When opt is 0, the function finds a contiguous data area and set it as suggested point for next allocation. The subsequent cluster allocation begins at top of the contiguous area found by this function. Thus the file allocation is guaranteed be contiguous and without allocation delay until the file size reaches this size unless any other changes to the volume is performed.

The contiguous file has an advantage for time-critical read/write operations. It eliminates some overheads in the filesystem and the storage device caused by random access for fragmented file.

-

Also the contiguous file can be easily accessed directly via low-level disk functions. However, this is not recommended in consideration of portability and future compatibility. If the file has not been confirmed be contiguous, use this function to examine if the file is contiguous or not.

+

The contiguou files can easily be accessed via low-level disk functions. However, this is not recommended in consideration of portability and future compatibility. If the file has not been confirmed be contiguous, use this function to examine if the file is contiguous or not.

@@ -76,16 +74,16 @@

Example

/* Creating a contiguous file */ /* Create a new file */ - res = f_open(fp = malloc(sizeof (FIL)), "file.dat", FA_WRITE|FA_CREATE_ALWAYS); + res = f_open(fp = malloc(sizeof (FIL)), "file.dat", FA_WRITE|FA_CREATE_ALWAYS); if (res) { /* Check if the file has been opened */ free(fp); die("Failed to open the file."); } - /* Alloacte a 100 MiB of contiguous area to the file */ + /* Allocate a 100 MiB of contiguous area to the file */ res = f_expand(fp, 104857600, 1); if (res) { /* Check if the file has been expanded */ - f_close(fp); + f_close(fp); free(fp); die("Failed to allocate contiguous area."); } @@ -101,7 +99,7 @@

Example

lba = fp->obj.fs->database + fp->obj.fs->csize * (fp->obj.sclust - 2); /* Write 2048 sectors from top of the file at a time */ - res = disk_write(drv, buffer, lba, 2048); + res = disk_write(drv, data, lba, 2048);
@@ -112,6 +110,7 @@

See Also

f_open, f_lseek, FIL

+

Return

diff --git a/vendor/fatfs/documents/doc/fattime.html b/vendor/fatfs/documents/doc/fattime.html index 002733a..227c06e 100644 --- a/vendor/fatfs/documents/doc/fattime.html +++ b/vendor/fatfs/documents/doc/fattime.html @@ -3,8 +3,6 @@ - - FatFs - get_fattime diff --git a/vendor/fatfs/documents/doc/fdisk.html b/vendor/fatfs/documents/doc/fdisk.html index d8c40bd..1f0fd49 100644 --- a/vendor/fatfs/documents/doc/fdisk.html +++ b/vendor/fatfs/documents/doc/fdisk.html @@ -3,8 +3,6 @@ - - FatFs - f_fdisk @@ -49,7 +47,7 @@

Return Values

Description

-

The f_fdisk function creates partitions on the physical drive. The partitioning format can be in generic MBR or GPT. The partition map table specifies how to divide the physical drive. The first item specifies the size of the first partition and the partitions are located on the drive in order of from the first item. When the value of item is less than or equal to 100, it specifies the partition size in percentage of the entire drive space. When it is larger than 100, it specifies number of sectors. The partition map table is terminated by a zero, no space is remaining for next allocation or 4th partition is created in MBR format. If the specified size is larger than remaining space on the drive, the partition is truncated at end of the drive.

+

The f_fdisk function creates partitions on the physical drive. The partitioning format can be in generic MBR or GPT. The partition map table specifies how to divide the physical drive. The first item specifies the size of the first partition and the partitions are located on the drive in order of from the first item. When the value of item is less than or equal to 100, it specifies the partition size in percentage of the entire drive space. When it is larger than 100, it specifies number of sectors. The partition map table is terminated by a zero, 4th partition in MBR format or no remainin space for next allocation. If the specified size is larger than remaining space on the drive, the partition is truncated at end of the drive.

By default, partitions are created in MBR format. It can create upto four primary partitions on a drive. GPT format is used to create the partitions when 64-bit LBA is enabled (FF_LBA64 = 1) and the drive size is equal to or larger than FF_MIN_GPT sectors. It can create over ten partitions on a drive.

@@ -64,23 +62,23 @@

Example

/* Volume mapping table defined by user (required when FF_MULTI_PARTITION == 1) */ PARTITION VolToPart[FF_VOLUMES] = { - {0, 1}, /* "0:" ==> 1st partition in PD#0 */ - {0, 2}, /* "1:" ==> 2nd partition in PD#0 */ - {1, 0} /* "2:" ==> PD#1 as removable drive */ + {0, 1}, /* "0:" ==> 1st partition in physical drive 0 */ + {0, 2}, /* "1:" ==> 2nd partition in physical drive 0 */ + {1, 0} /* "2:" ==> Physical drive 1 as removable drive */ };
     /* Initialize a brand-new disk drive mapped to physical drive 0 */
 
     BYTE work[FF_MAX_SS];         /* Working buffer */
-    LBA_t plist[] = {50, 50, 0};  /* Divide the drive into two partitions */
-                 /* {0x10000000, 100}; 256M sectors for 1st partition and left all for 2nd partition */
+    LBA_t plist[] = {50, 50, 0};  /* Divide the drive by 2 */
+                 /* {0x10000000, 100}; 256M sectors for the 1st partition and the remaining for the 2nd partition */
                  /* {20, 20, 20, 0}; 20% for 3 partitions each and remaing space is left not allocated */
 
-    f_fdisk(0, plist, work);                    /* Divide physical drive 0 */
+    f_fdisk(0, plist, work);            /* Divide the physical drive 0 */
 
-    f_mkfs("0:", 0, work, sizeof work); /* Create FAT volume on the logical drive 0 */
-    f_mkfs("1:", 0, work, sizeof work); /* Create FAT volume on the logical drive 1 */
+    f_mkfs("0:", 0, work, sizeof work); /* Create FAT volume on the logical drive 0 */
+    f_mkfs("1:", 0, work, sizeof work); /* Create FAT volume on the logical drive 1 */
 
 
@@ -90,6 +88,7 @@

See Also

Volume management, f_mkfs

+

Return

diff --git a/vendor/fatfs/documents/doc/filename.html b/vendor/fatfs/documents/doc/filename.html index 5e4f48a..bf28457 100644 --- a/vendor/fatfs/documents/doc/filename.html +++ b/vendor/fatfs/documents/doc/filename.html @@ -3,8 +3,6 @@ - - FatFs - Path Names @@ -36,15 +34,15 @@

Format of the Path Names

/..Invalid nameThe root directory (sticks the top level)

Also the drive prefix can be in pre-defined arbitrary string. When the option FF_STR_VOLUME_ID == 1, also arbitrary string volume ID can be used as drive prefix. e.g. "flash:file1.txt", "ram:temp.dat" or "sd:". If the srting does not match any volume ID, the function fails with FR_INVALID_DRIVE.

-

When FF_STR_VOLUME_ID == 2, Unix style drive prefix can be used. e.g. "/flash/file1.txt", "/ram/temp.dat" or "/sd". If a heading separator is exist, it is treated as start of drive prefix and in absolute path. Any form as "root directory in current drive" and "current directory in specified drive" cannot be used. Double dot name cannot traverse the drives such as "/flash/../ram/foo.dat".

+

When FF_STR_VOLUME_ID == 2, Unix style drive prefix can be used. e.g. "/flash/file1.txt", "/ram/temp.dat" or "/sd". If a heading separator is exist, it is treated as an absolute path with a heading node ID. Any form as "root directory in current drive" and "current directory in specified drive" cannot be used. Double dot name cannot traverse the volumes such as "/flash/../ram/foo.dat".

Remark: In this revision, double dot name ".." cannot follow the parent directory on the exFAT volume. It will work as "." and stay there.

Legal Characters and Case Sensitivity

In the generic FAT filesystems, the legal characters for object name (file/directory name) are, 0-9 A-Z ! # $ % & ' ( ) - @ ^ _ ` { } ~ in ASCII and extended characters \x80 to \xFF. In the FAT filesystems with LFN extention, also + , ; = [ ], white space and extended characters U+000080 to U+10FFFF are legal for the object name. White spaces and dots can be placed anywhere in the path name except end of the name. Trailing white spaces and dots are ignored.

-

FAT filesystem is case-insensitive to the object names on the volume. Object name on the FAT volume is compared in case-insensitive. For instance, these three names, file.txt, File.Txt and FILE.TXT, are identical on the FAT filesystem. This is applied to extended charactres as well. When an object is created on the FAT volume, up converted name is recorded to the SFN entry, and the raw name is recorded to the LFN entry when LFN extension is exist.

-

As for the MS-DOS and PC DOS for CJK (DOS/DBCS), extended characters ware recorded to the SFN entry without up-case conversion and compared in case-sensitive. This causes a problem on compatibility with Windows system when the object with extended characters is created on the volume by DOS/DBCS system; therfore the object names with DBCS extended characters should not be used on the FAT volume shared by those systems. FatFs works with case-sensitive to the extended characters in only non-LFN with DBCS configuration (DOS/DBCS specs). But in LFN configuration, FatFs works with case-insensitive to the extended character (WindowsNT specs).

+

FAT filesystem is case-insensitive to the object names on the volume. Object names on the FAT volume are compared in case-insensitive. For instance, these three names, file.txt, File.Txt and FILE.TXT, are identical on the FAT filesystem. This is applied to extended charactres as well. When an object is created on the FAT volume, up converted name is recorded to the SFN entry, and the original name is recorded to the LFN entry if LFN extension is enabled.

+

On the MS-DOS and PC DOS for Chinese, Japanese and Korean (DOS/DBCS), extended characters are recorded to the SFN entry without up-case conversion and compared in case-sensitive. This causes a problem on compatibility with Windows system when the object with extended characters is created on the volume by DOS/DBCS system; therfore the object names with DBCS extended characters should not be used on the FAT volume shared by those systems. FatFs works with case-sensitive to the extended characters in only non-LFN with DBCS configuration (DOS/DBCS specs). However, FatFs works with case-insensitive to the extended character (WindowsNT specs) in LFN configuration.

@@ -61,7 +59,7 @@

Unicode API

Volume Management

-

By default, each logical drive is associated with the physical drive in same drive number. An FAT volume on the physical drive is serched in the volume mount process. It reads boot sectors and checks it if it is an FAT boot sector in order of LBA 0 as SFD format, 1st partition, 2nd partition, 3rd partition, ..., as MBR or GPT format.

+

By default, each logical drive is associated with the physical drive in same drive number. An FAT volume on the physical drive is serched in the volume mount process. It reads boot sectors and checks it if it is an FAT VBR in order of LBA 0 as SFD format, 1st partition, 2nd partition, 3rd partition, ..., as MBR or GPT format.

When multiple partition feature is enabled, FF_MULTI_PARTITION = 1, each individual logical drive is associated with arbitrary partition or physical drive specified by volume management table, VolToPart[]. The table needs to be defined by user to resolve mappings of the logical drive numbers and the associated partitions or drives. Following code is an example of the volume management table.

 Example: "0:", "1:" and "2:" are associated with three partitions on the physical drive 0 (a non-removable drive)
@@ -73,8 +71,10 @@ 

Volume Management

{0, 3}, /* "2:" ==> 3rd partition on the pd#0 */ {1, 0} /* "3:" ==> pd#1 as removable drive (auto-search) */ }; + + +relationship between logical drive and physical drive
-
relationship between logical drive and physical drive

There are some considerations when enable the multi-partition configuration.

  • The physical drive that hosts two or more mounted partitions should be non-removable, or all volumes on the drive must be unmounted when remove the medium.
  • @@ -82,11 +82,12 @@

    Volume Management

  • On the MBR format drive, up to four primary partitions (1-4) can be specified. The partition number 1 specifies the first item in the partition table and the partition number 2 specifies the second one, and so on. The logical patitions (5-) in the extended partition is not supported.
  • On the GPT format drive, the partition number 1 specifies the first Microsoft BDP found in the partition table and the partition number 2 specifies the second one found, and so on.
  • Windows 10 earlier than 1703 does not support multiple volumes on the physical drive with removable class. Only the first parition found on the drive will be mounted. Windows OS does not support SFD format on the physical drive with non-removable class.
  • -
  • Some systems manage the on-board storage in non-standard partition format and each partition is mapped as physical drive in disk_* functions. For such system, FF_MULTI_PARTITION should be always 0.
  • -
  • For further information about the volume management, refer to the description in f_fdisk and f_mkfs.
  • +
  • Some systems manage the on-board storage in non-standard partition format and each partition is mapped as physical drive in disk_* functions. For such system, FF_MULTI_PARTITION should be always 0 and use FM_SFD flag in f_mkfs.
  • +
  • For further information about the volume management, refer to the description in f_mkfs and f_fdisk.
+

Return

diff --git a/vendor/fatfs/documents/doc/findfirst.html b/vendor/fatfs/documents/doc/findfirst.html index 0cd1704..79d04fa 100644 --- a/vendor/fatfs/documents/doc/findfirst.html +++ b/vendor/fatfs/documents/doc/findfirst.html @@ -3,8 +3,6 @@ - - FatFs - f_findfirst @@ -28,7 +26,7 @@

f_findfirst

Parameters

dp
-
Pointer to the blank directory object.
+
Pointer to the blank directory object. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
fno
Pointer to the file information structure to store the information about the found item.
path
@@ -62,18 +60,18 @@

Return Values

Description

After the directory specified by path could be opened, it starts to search the directory for items with the matching pattern specified by pattern. If the first item is found, the information about the item is stored into the file information structure fno. If not found, fno->fname[] has a null string.

-

The matching pattern string can contain wildcard terms. For example:

+

The matching pattern string can contain wildcards. For example:

  • ? - An any character.
  • ??? - An any string in length of three characters.
  • * - An any string in length of zero or longer.
  • ????* - An any string in length of four characters or longer.
-

Since the matching algorithm uses recursion, number of wildcard terms in the matching pattern is limited to four to limit the stack usage. Any pattern with too many wildcard terms does not match any name. In LFN configuration, only fname[] is tested when FF_USE_FIND == 1 and also altname[] is tested when FF_USE_FIND == 2. There are some differences listed below between FatFs and standard systems in matching condition.

+

Since the matching algorithm uses recursion, number of wildcards in the matching pattern is limited to four to limit the stack usage. The pattern with too many wildcards does not match any name. In LFN configuration, only fname[] is tested when FF_USE_FIND == 1 and also altname[] is tested when FF_USE_FIND == 2. There are some differences listed below between FatFs and standard systems in matching condition.

    -
  • "*.*" never matches any name without extension while it matches any name with or without extension in standard systems.
  • -
  • Any pattern terminated with a dot never matches any name while it matches the name without extensiton in standard systems.
  • -
  • DBCS extended characters are compared in case-sensitive when LFN is enabled with ANSI/OEM code API.
  • +
  • "*.*" does not match any name without extension while it matches any name with or without extension in standard systems.
  • +
  • The pattern terminated with a dot does not match any name while it matches the name without extensiton in standard systems.
  • +
  • DBCS extended characters are compared in case-sensitive when LFN is enabled with !FF_LFN_UNICODE.
@@ -99,10 +97,10 @@

Examples

while (fr == FR_OK && fno.fname[0]) { /* Repeat while an item is found */ printf("%s\n", fno.fname); /* Print the object name */ - fr = f_findnext(&dj, &fno); /* Search for next item */ + fr = f_findnext(&dj, &fno); /* Search for next item */ } - f_closedir(&dj); + f_closedir(&dj); }
diff --git a/vendor/fatfs/documents/doc/findnext.html b/vendor/fatfs/documents/doc/findnext.html index f39f0fb..1800f61 100644 --- a/vendor/fatfs/documents/doc/findnext.html +++ b/vendor/fatfs/documents/doc/findnext.html @@ -3,8 +3,6 @@ - - FatFs - f_findnext @@ -64,6 +62,7 @@

See Also

f_findfirst, f_closedir, DIR, FILINFO

+

Return

diff --git a/vendor/fatfs/documents/doc/forward.html b/vendor/fatfs/documents/doc/forward.html index 97a793d..056a48e 100644 --- a/vendor/fatfs/documents/doc/forward.html +++ b/vendor/fatfs/documents/doc/forward.html @@ -3,8 +3,6 @@ - - FatFs - f_forward @@ -28,13 +26,13 @@

f_forward

Parameters

fp
-
Pointer to the open file object.
+
Pointer to the open file object. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
func
Pointer to the user-defined data streaming function. For details, refer to the sample code.
btf
Number of bytes to forward in range of UINT.
bf
-
Pointer to the UINT variable to return number of bytes forwarded.
+
Pointer to the variable in UINT type to return number of bytes forwarded.
@@ -54,7 +52,7 @@

Return Values

Description

-

The f_forward function reads the data from the file and forward it to the outgoing stream without data buffer. This is suitable for small memory system because it does not require any data buffer at application module. The file pointer of the file object increases in number of bytes forwarded. In case of *bf is less than btf without error, it means the requested bytes could not be transferred due to end of file or stream goes busy during data transfer.

+

The f_forward function reads the data from the file and forward it to the outgoing stream. This function is suitable for small memory system, because it does not require any data buffer in the application module. The file pointer of the file object advances in number of bytes forwarded. In case of *bf is less than btf without error, it means the requested size of data could not be transferred due to end of file or stream goes busy during data transfer.

@@ -109,11 +107,11 @@

Example

UINT dmy; /* Open the audio file in read only mode */ - rc = f_open(&fil, fn, FA_READ); + rc = f_open(&fil, fn, FA_READ); if (rc) return rc; /* Repeat until the file pointer reaches end of the file */ - while (rc == FR_OK && !f_eof(&fil)) { + while (rc == FR_OK && !f_eof(&fil)) { /* some processes... */ @@ -122,7 +120,7 @@

Example

} /* Close the file and return */ - f_close(&fil); + f_close(&fil); return rc; } @@ -134,6 +132,7 @@

See Also

f_open, fgets, f_write, f_close, FIL

+

Return

diff --git a/vendor/fatfs/documents/doc/getcwd.html b/vendor/fatfs/documents/doc/getcwd.html index 6eb9a58..22a31e1 100644 --- a/vendor/fatfs/documents/doc/getcwd.html +++ b/vendor/fatfs/documents/doc/getcwd.html @@ -3,8 +3,6 @@ - - FatFs - f_getcwd @@ -78,6 +76,7 @@

See Also

f_chdrive, f_chdir

+

Return

diff --git a/vendor/fatfs/documents/doc/getfree.html b/vendor/fatfs/documents/doc/getfree.html index 5e4f419..ac378a7 100644 --- a/vendor/fatfs/documents/doc/getfree.html +++ b/vendor/fatfs/documents/doc/getfree.html @@ -3,8 +3,6 @@ - - FatFs - f_getfree diff --git a/vendor/fatfs/documents/doc/getlabel.html b/vendor/fatfs/documents/doc/getlabel.html index 0ec6d0d..d7d3362 100644 --- a/vendor/fatfs/documents/doc/getlabel.html +++ b/vendor/fatfs/documents/doc/getlabel.html @@ -3,8 +3,6 @@ - - FatFs - f_getlabel @@ -27,7 +25,7 @@

f_getlabel

Parameters

path
-
Pointer to the null-terminated string that specifies the logical drive. Null-string specifies the default drive.
+
Pointer to the null-terminated string that specifies the logical drive. Null-string specifies the default drive. If a null pointer is given, the function fails with FR_INVALID_DRIVE.
label
Pointer to the buffer to store the volume label. If the volume has no label, a null-string will be returned. Set null pointer if this information is not needed. The buffer size should be shown below at least to avoid buffer overflow.
diff --git a/vendor/fatfs/documents/doc/gets.html b/vendor/fatfs/documents/doc/gets.html index 89d9560..f4a22ca 100644 --- a/vendor/fatfs/documents/doc/gets.html +++ b/vendor/fatfs/documents/doc/gets.html @@ -3,8 +3,6 @@ - -FatFs - f_gets @@ -45,13 +43,13 @@

Return Values

Description

The read operation continues until a '\n' is stored, reached end of the file or the buffer is filled with len - 1 characters. The read string is terminated with a '\0'. When no character to read or any error occured during read operation, it returns a null pointer. The status of EOF and error can be examined with f_eof and f_error function.

-

When FatFs is configured to Unicode API (FF_LFN_UNICODE >= 1), data types on the srting fuctions, f_putc, f_puts, f_printf and f_gets, is also switched to Unicode. The character encoding on the file to be read via this function is assumed as FF_STRF_ENCODE. If the character encoding on the file differs from that on the API, it is converted in this function. In this case, input characters with wrong encoding will be lost.

+

When FatFs is configured to Unicode API (FF_LFN_UNICODE >= 1), data types on the srting fuctions, f_putc, f_puts, f_printf and f_gets, is also switched to Unicode. The character encoding on the file to be read via this function is assumed as FF_STRF_ENCODE. If the character encoding differs between file data and API, it is converted in this function. Input characters with wrong encoding for output will be lost.

QuickInfo

-

This is a wrapper function of f_read function. Available when FF_USE_STRFUNC >= 1. When it is set to 2, '\r's contained in the file are stripped out.

+

This is a wrapper function of f_read function. Available when FF_USE_STRFUNC >= 1. When it is set to 2, '\r's contained in the read data are stripped off.

@@ -60,6 +58,7 @@

See Also

f_open, f_read, f_putc, f_puts, f_printf, f_close, FIL

+

Return

diff --git a/vendor/fatfs/documents/doc/lseek.html b/vendor/fatfs/documents/doc/lseek.html index ffd1f62..9740b8c 100644 --- a/vendor/fatfs/documents/doc/lseek.html +++ b/vendor/fatfs/documents/doc/lseek.html @@ -3,8 +3,6 @@ - -FatFs - f_lseek @@ -32,7 +30,7 @@

f_lseek

Parameters

fp
-
Pointer to the open file object.
+
Pointer to the open file object. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
ofs
Byte offset from top of the file to set read/write pointer. The data type FSIZE_t is an alias of either DWORD(32-bit) or QWORD(64-bit) depends on the configuration option FF_FS_EXFAT.
@@ -53,17 +51,18 @@

Return Values

Description

-

File read/write ponter in the open file object points the data byte to be read/written at next read/write operation. It advances as the number of bytes read/written. The f_lseek function moves the file read/write pointer without any read/write operation to the file. The f_rewind function is impremented as a macro.

+

File read/write ponter in the open file object points the data byte to be read/written at next read/write operation. It advances as the number of bytes read/written. The f_lseek function moves the file read/write pointer without read/write operation to the file. The f_rewind function is impremented as a macro.

 #define f_rewind(fp) f_lseek((fp), 0)
 
-

If an offset beyond the file size is specified in write mode, the file size is expanded to the specified offset. The file data in the expanded part is undefined, because no data is written to the file in this process. This is suitable to pre-allocate a data area to the file quickly for fast write operation. When a contiguous data area needs to be allocated to the file, use f_expand function instead. After the f_lseek function succeeded, the current read/write pointer should be checked in order to make sure the read/write pointer has been moved correctry. In case of the read/write pointer is not pointing expected offset, either of followings has been occured.

+

When an offset beyond the file size is specified in write mode, the file size is expanded to the specified offset in this function. The file data in the expanded part is undefined, because no data is written to the file in this process. Be careful about these behaviours differ from POSIX fseek function. This is suitable to pre-allocate a data area to the file for subsequent fast write operation. If a contiguous data area needs to be allocated to the file, use f_expand function instead.

+

After the f_lseek function succeeded, the current read/write pointer should be checked in order to make sure the read/write pointer has been moved correctry. In case of the read/write pointer is not pointing expected offset, either of followings has been occured.

  • End of file. The specified ofs was clipped at end of the file in read-only mode.
  • Disk full. There is no free space on the volume to expand the file.
-

The fast seek feature enables fast backward/long seek operations without FAT access by using an on-memory CLMT (cluster link map table). It is applied to f_read and f_write function as well, however, the file size cannot be expanded by f_write, f_lseek function while the file is at fast seek mode.

-

The fast seek mode is available when FF_USE_FASTSEEK = 1. The CLMT must be created into the DWORD array prior to use the fast seek mode. To create the CLMT, set address of the DWORD array to the member cltbl in the open file object, set the size of array in unit of items to the cltbl[0] and then call f_lseek function with ofs = CREATE_LINKMAP. After the function succeeded, no FAT access is occured in subsequent f_read, f_write, f_lseek function to the file. The number of items used or required is returned into the cltbl[0]. The number of items needed is (number of the file fragments + 1) * 2. For example, 12 items in the array will be used for the file fragmented in 5 portions. If the function failed with FR_NOT_ENOUGH_CORE, the size of given array is insufficient for the file.

+

The fast seek feature enables fast backward/long seek operations without FAT access by using an on-memory CLMT (cluster link map table). It is applied to f_read and f_write function as well, however, the file size cannot be expanded by f_write, f_lseek function while the file is in fast seek mode.

+

The fast seek mode is available when FF_USE_FASTSEEK = 1. The CLMT must be created into a DWORD array prior to use the fast seek mode. To create the CLMT, set address of the DWORD array to the member cltbl in the open file object, set the size of array in unit of items to the cltbl[0] and then call f_lseek function with ofs = CREATE_LINKMAP. The number of items used or required is returned into the cltbl[0]. The number of items needed is (number of the file fragments + 1) * 2. For example, 12 items in the array will be used for the file fragmented in 5 portions. If the function failed with FR_NOT_ENOUGH_CORE, the size of given array is insufficient for the file. After the function succeeded, no FAT access is occured in subsequent f_read, f_write, f_lseek function to the file. To disable the fast seek mode, set null pointer to the cltbl.

@@ -78,7 +77,7 @@

Example

     /* Open file */
     fp = malloc(sizeof (FIL));
-    res = f_open(fp, "file.dat", FA_READ|FA_WRITE);
+    res = f_open(fp, "file.dat", FA_READ|FA_WRITE);
     if (res) ...
 
     /* Set read/write pointer to 5000 */
@@ -96,15 +95,15 @@ 

Example

 /* Cluster pre-allocation (to prevent buffer overrun on streaming write) */
 
-    res = f_open(fp, recfile, FA_CREATE_NEW | FA_WRITE);   /* Create a file */
+    res = f_open(fp, recfile, FA_CREATE_NEW | FA_WRITE);   /* Create a file */
 
     res = f_lseek(fp, PRE_SIZE);             /* Expand file size (cluster pre-allocation) */
-    if (res || f_tell(fp) != PRE_SIZE) ...   /* Check if the file has been expanded successfly */
+    if (res || f_tell(fp) != PRE_SIZE) ...   /* Check if the file has been expanded successfly */
 
     res = f_lseek(fp, OFS_DATA);             /* Record data stream with free from cluster allocation delay */
     ...                                      /* Write operation should be aligned to sector boundary to optimize the write throughput */
 
-    res = f_truncate(fp);                    /* Truncate unused area */
+    res = f_truncate(fp);                    /* Truncate unused area */
     res = f_lseek(fp, OFS_HEADER);           /* Set file header */
     ...
 
@@ -115,7 +114,7 @@ 

Example

DWORD clmt[SZ_TBL]; /* Cluster link map table buffer */ - res = f_open(fp, fname, FA_READ | FA_WRITE); /* Open a file */ + res = f_open(fp, fname, FA_READ | FA_WRITE); /* Open a file */ res = f_lseek(fp, ofs1); /* This is normal seek (cltbl is nulled on file open) */ @@ -134,6 +133,7 @@

See Also

f_open, f_truncate, f_expand, FIL

+

Return

diff --git a/vendor/fatfs/documents/doc/mkdir.html b/vendor/fatfs/documents/doc/mkdir.html index de32f83..7027cd8 100644 --- a/vendor/fatfs/documents/doc/mkdir.html +++ b/vendor/fatfs/documents/doc/mkdir.html @@ -3,8 +3,6 @@ - - FatFs - f_mkdir @@ -75,6 +73,7 @@

Example

+

Return

diff --git a/vendor/fatfs/documents/doc/mkfs.html b/vendor/fatfs/documents/doc/mkfs.html index 3f3bd70..411c863 100644 --- a/vendor/fatfs/documents/doc/mkfs.html +++ b/vendor/fatfs/documents/doc/mkfs.html @@ -3,8 +3,6 @@ - - FatFs - f_mkfs @@ -30,23 +28,23 @@

Parameters

path
Pointer to the null-terminated string specifies the logical drive to be formatted. If it does not have a drive number in it, it means to specify the default drive. The logical drive may or may not have been mounted for the format process.
opt
-
Specifies the format option structure MKFS_PARM holding format options. If a null pointer is given, it gives the function all options in default value. The structure has five members described below:
+
Specifies the format option structure MKFS_PARM holding format options. If a null pointer is given, it gives the function every option in default value. The structure has five members in order of described below:
BYTE fmt
-
Specifies combination of FAT type flags, FM_FAT, FM_FAT32, FM_EXFAT and bitwise-or of these three, FM_ANY. FM_EXFAT is ignored when exFAT is not enabled. These flags specify which FAT type to be created on the volume. If two or more types are specified, one out of them will be selected depends on the volume size and au_size. The flag FM_SFD specifies to create the volume on the drive in SFD format. The default value is FM_ANY.
-
DWORD au_size
-
Specifies size of the allocation unit (cluter) in unit of byte. The valid value is power of 2 between sector size and 128 * sector size inclusive for FAT/FAT32 volume, or up to 16 MB for exFAT volume. If a zero (default value) or any invalid value is given, the function uses default allocation unit size depends on the volume size.
-
UINT n_align
-
Specifies alignment of the volume data area (file allocation pool, usually erase block boundary of flash memory media) in unit of sector. The valid value for this member is between 1 and 32768 inclusive in power of 2. If a zero (the default value) or any invalid value is given, the function obtains the block size from lower layer with disk_ioctl function.
+
Specifies a combination of FAT type flags, FM_FAT, FM_FAT32, FM_EXFAT and bitwise-or of these three, FM_ANY. FM_EXFAT is ignored when exFAT is not enabled. These flags specify which type of FAT volume to be created. If two or more types are specified, one out of them will be selected depends on the volume size and au_size. The flag FM_SFD specifies to create the volume on the drive in SFD format. The default value is FM_ANY.
BYTE n_fat
Specifies number of FAT copies on the FAT/FAT32 volume. Valid value for this member is 1 or 2. The default value (0) and any invaid value gives 1. If the FAT type is exFAT, this member has no effect.
+
UINT align
+
Specifies alignment of the volume data area (file allocation pool, usually erase block boundary of flash memory media) in unit of sector. The valid value for this member is between 1 and 32768 inclusive in power of 2. If a zero (the default value) or an invalid value is given, the function obtains the block size from lower layer with disk_ioctl function.
UINT n_root
Specifies number of root directory entries on the FAT volume. Valid value for this member is up to 32768 and aligned to sector size / 32. The default value (0) and any invaid value gives 512. If the FAT type is FAT32 or exFAT, this member has no effect.
+
DWORD au_size
+
Specifies size of the cluster (allocation unit) in unit of byte. The valid value for this member is between sector size and 128 * sector size inclusive in power of 2 for FAT/FAT32 volume and up to 16 MB in power of 2 for exFAT volume. If a zero (default value) or an invalid value is given, the function uses a default cluster size depends on the volume size.
work
-
Pointer to the working buffer used for the format process. If a null pointer is given with FF_USE_LFN == 3, the function obtains a memory block for the working buffer in this function.
+
Pointer to the working buffer used for the format process. If a null pointer is given with FF_USE_LFN == 3, the function uses a len bytes of heap memory in this function.
len
-
Size of the working buffer in unit of byte. It needs to be FF_MAX_SS at least. Plenty of working buffer reduces number of write transactions to the drive and the format process will finish quickly.
+
Size of the working buffer in unit of byte. It needs to be FF_MAX_SS at least. Plenty of working buffer reduces number of write transactions to the drive, thus the format process will finish quickly.
@@ -69,9 +67,9 @@

Description

The FAT sub-type, FAT12/FAT16/FAT32, of FAT volume except exFAT is determined by only number of clusters on the volume and nothing else, according to the FAT specification issued by Microsoft. Thus the FAT sub-type of created volume depends on the volume size and the cluster size. In case of the combination of FAT type and cluter size specified by argument is not valid for the volume size, the function will fail with FR_MKFS_ABORTED.

The allocation unit, also known as cluster, is a unit of disk space allocation for files. When the size of allocation unit is 32768 bytes, a file with 100 bytes in size occupies 32768 bytes of disk space. The space efficiency of disk usage gets worse as increasing size of allocation unit, but, on the other hand, the read/write performance increases. Therefore the size of allocation unit is a trade-off between space efficiency and performance. For the large volumes in GB order, 32768 bytes or larger, automatically selected by default, is recommended for most case unless extremely many small files are created in the volume.

When the logical drive to be formatted is associated with a physical drive (FF_MULTI_PARTITION == 0 or VolToPart[].pt == 0) and FM_SFD flag is not specified, a partition occupies entire drive space is created and then the FAT volume is created in the partition. When FM_SFD flag is specified, the FAT volume is created without any disk partitioning.

-

When the logical drive to be formatted is associated with a specific partition by multiple partition feature (FF_MULTI_PARTITION == 1 and VolToPart[].pt > 0), the FAT volume is created in the partition of the physical drive specified by volume mapping table and FM_SFD flag is ignored. The hosting physical drive needs to be partitioned with f_fdisk function or any partitioning tool prior to create the FAT volume with this function. If the partition is not exist, the function aborts with FR_MKFS_ABORTED.

-

There are three standard disk partitioning formats, MBR, GPT and SFD. The MBR format, also known as FDISK format, is usually used for harddisk, memory card and U disk. It can divide a physical drive into one or more partitions with a partition table. The GPT, GUID Partition Table, is a newly defined patitioning format for large storage devices. FatFs suppors the GPT only when 64-bit LBA is enabled. The SFD, Super-Floppy Disk, is non-partitioned disk format. The FAT volume is located at LBA 0 and occupies the entire physical drive without any disk partitioning. It is usually used for floppy disk, optical disk and most super-floppy media. Some combination of systems and media support only either partitioned format or non-partitioned format and the other is not supported.

-

Some systems manage the partitions in the on-board storage in non-standard format. The partitions are mapped as physical drives identified by pdrv in disk_* functions. For such systems, SFD format is suitable to create the FAT volume in the partition.

+

When the logical drive to be formatted is associated with a specific partition by multiple partition feature (FF_MULTI_PARTITION == 1 and VolToPart[].pt > 0), the FAT volume is created in the partition of the physical drive specified by volume mapping table and FM_SFD flag is ignored. The hosting physical drive needs to be partitioned with f_fdisk function or some partitioning tool prior to create the FAT volume with this function. If the partition is not exist, the function fails with FR_MKFS_ABORTED.

+

There are three standard disk partitioning formats, MBR, GPT and SFD. The MBR format, also known as FDISK format, is usually used for harddisk, memory card and U disk. It can divide a physical drive into one or more partitions with a partition table. The GPT, GUID Partition Table, is a newly defined patitioning format for large storage devices. FatFs suppors the GPT only when 64-bit LBA is enabled. The SFD, Super-Floppy Disk, is non-partitioned disk format. The FAT volume is located at LBA 0 and occupies the entire physical drive without disk partitioning. It is usually used for floppy disk, optical disk and most super-floppy media. Some combination of systems and media support only either partitioned format or non-partitioned format and the other is not supported.

+

Some systems manage the partitions of on-board storage in non-standard format. The partitions are mapped as physical drives identified by pdrv in disk_* functions. For such systems, SFD format is suitable to create the FAT volume in the partition.

@@ -97,21 +95,21 @@

Example

if (res) ... /* Give a work area to the default drive */ - f_mount(&fs, "", 0); + f_mount(&fs, "", 0); /* Create a file as new */ - res = f_open(&fil, "hello.txt", FA_CREATE_NEW | FA_WRITE); + res = f_open(&fil, "hello.txt", FA_CREATE_NEW | FA_WRITE); if (res) ... /* Write a message */ - f_write(&fil, "Hello, World!\r\n", 15, &bw); + f_write(&fil, "Hello, World!\r\n", 15, &bw); if (bw != 15) ... /* Close the file */ - f_close(&fil); + f_close(&fil); /* Unregister work area */ - f_mount(0, "", 0); + f_mount(0, "", 0); ...
@@ -122,6 +120,7 @@

See Also

Example of volume size and format parameters, Volume management, f_fdisk

+

Return

diff --git a/vendor/fatfs/documents/doc/mount.html b/vendor/fatfs/documents/doc/mount.html index 57f40be..f0383b9 100644 --- a/vendor/fatfs/documents/doc/mount.html +++ b/vendor/fatfs/documents/doc/mount.html @@ -3,8 +3,6 @@ - -FatFs - f_mount @@ -92,19 +90,19 @@

Example

FATFS *fs; /* Ponter to the filesystem object */ - fs = malloc(sizeof (FATFS)); /* Get work area for the volume */ - f_mount(fs, "", 0); /* Mount the default drive */ + fs = malloc(sizeof (FATFS)); /* Get work area for the volume */ + f_mount(fs, "", 0); /* Mount the default drive */ - f_open(... /* Here any file API can be used */ + f_open(... /* Here any file API can be used */ ... - f_mount(fs, "", 0); /* Re-mount the default drive to reinitialize the filesystem */ + f_mount(fs, "", 0); /* Re-mount the default drive to reinitialize the filesystem */ ... - f_mount(0, "", 0); /* Unmount the default drive */ - free(fs); /* Here the work area can be discarded */ + f_unmount(""); /* Unmount the default drive */ + free(fs); /* Here the work area can be discarded */ ... } @@ -117,6 +115,7 @@

See Also

f_open, FATFS

+

Return

diff --git a/vendor/fatfs/documents/doc/open.html b/vendor/fatfs/documents/doc/open.html index dd2a6e6..b533249 100644 --- a/vendor/fatfs/documents/doc/open.html +++ b/vendor/fatfs/documents/doc/open.html @@ -3,8 +3,6 @@ - -FatFs - f_open @@ -27,19 +25,19 @@

f_open

Parameters

fp
-
Pointer to the blank file object structure.
+
Pointer to the blank file object structure. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
path
-
Pointer to the null-terminated string that specifies the file name to open or create.
+
Pointer to the null-terminated string that specifies the file name to open or create. If a null pointer is given, the function fails with FR_INVALID_DRIVE.
mode
Mode flags that specifies the type of access and open method for the file. It is specified by a combination of following flags.
- - - - + + + +
FlagsMeaning
FA_READSpecifies read access to the file. Data can be read from the file.
FA_WRITESpecifies write access to the file. Data can be written to the file. Combine with FA_READ for read-write access.
FA_OPEN_EXISTINGOpens a file. The function fails if the file is not existing. (Default)
FA_CREATE_NEWCreates a new file. The function fails with FR_EXIST if the file is existing.
FA_CREATE_ALWAYSCreates a new file. If the file is existing, it will be truncated and overwritten.
FA_OPEN_ALWAYSOpens the file if it is existing. If not, a new file will be created.
FA_OPEN_EXISTINGOpens the file. The function fails if the file is not existing. (Default)
FA_CREATE_ALWAYSCreates a new file. If the file is existing, the file is truncated and overwritten.
FA_CREATE_NEWCreates a new file. The function fails if the file is existing.
FA_OPEN_ALWAYSOpens the file. If it is not exist, a new file is created.
FA_OPEN_APPENDSame as FA_OPEN_ALWAYS except the read/write pointer is set end of the file.
Mode flags in POSIX fopen() function corresponds to FatFs mode flags as follows:
@@ -86,7 +84,8 @@

Return Values

Description

-

The f_open function opens a file and creates a file object. The file object is used for subsequent read/write operations to the file to identify the file. Open file should be closed with f_close function after the session of the file access. If any change to the file is made and not closed prior to power down, media removal or re-mount, or the file can be collapsed.

+

The f_open function opens a file and creates a file object. It is the identifier for subsequent read/write operations to the file. After the function succeeded, the file object is valid. If the function failed, the file object is set invalid.

+

Open file should be closed with f_close function after the session of the file access. If any change to the file has been made and not closed prior to power off, media removal or re-mount, or the file can be collapsed.

If duplicated file open is needed, read here carefully. However duplicated open of a file with any write mode flag is always prohibited.

@@ -112,19 +111,19 @@

Example

/* Give a work area to the default drive */ - f_mount(&FatFs, "", 0); + f_mount(&FatFs, "", 0); /* Open a text file */ fr = f_open(&fil, "message.txt", FA_READ); if (fr) return (int)fr; /* Read every line and display it */ - while (f_gets(line, sizeof line, &fil)) { + while (f_gets(line, sizeof line, &fil)) { printf(line); } /* Close the file */ - f_close(&fil); + f_close(&fil); return 0; } @@ -142,8 +141,8 @@

Example

/* Give work areas to each logical drive */ - f_mount(&fs0, "0:", 0); - f_mount(&fs1, "1:", 0); + f_mount(&fs0, "0:", 0); + f_mount(&fs1, "1:", 0); /* Open source file on the drive 1 */ fr = f_open(&fsrc, "1:file.bin", FA_READ); @@ -155,19 +154,19 @@

Example

/* Copy source to destination */ for (;;) { - fr = f_read(&fsrc, buffer, sizeof buffer, &br); /* Read a chunk of data from the source file */ + fr = f_read(&fsrc, buffer, sizeof buffer, &br); /* Read a chunk of data from the source file */ if (br == 0) break; /* error or eof */ - fr = f_write(&fdst, buffer, br, &bw); /* Write it to the destination file */ + fr = f_write(&fdst, buffer, br, &bw); /* Write it to the destination file */ if (bw < br) break; /* error or disk full */ } /* Close open files */ - f_close(&fsrc); - f_close(&fdst); + f_close(&fsrc); + f_close(&fdst); /* Unregister work area prior to discard it */ - f_unmount("0:"); - f_unmount("1:"); + f_unmount("0:"); + f_unmount("1:"); return (int)fr; } @@ -180,6 +179,7 @@

See Also

f_read, f_write, f_close, FIL, FATFS

+

Return

diff --git a/vendor/fatfs/documents/doc/opendir.html b/vendor/fatfs/documents/doc/opendir.html index 215c37a..1955bc3 100644 --- a/vendor/fatfs/documents/doc/opendir.html +++ b/vendor/fatfs/documents/doc/opendir.html @@ -3,8 +3,6 @@ - - FatFs - f_opendir @@ -26,9 +24,9 @@

f_opendir

Parameters

dp
-
Pointer to the blank directory object to create a new one.
+
Pointer to the blank directory object to create a new one. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
path
-
Pointer to the null-terminated string that specifies the directory name to be opened.
+
Pointer to the null-terminated string that specifies the directory name to be opened. If a null pointer is given, the function fails with FR_INVALID_DRIVE.
@@ -70,6 +68,7 @@

See Also

f_readdir, f_closedir, DIR

+

Return

diff --git a/vendor/fatfs/documents/doc/printf.html b/vendor/fatfs/documents/doc/printf.html index 557fb57..c1e1f29 100644 --- a/vendor/fatfs/documents/doc/printf.html +++ b/vendor/fatfs/documents/doc/printf.html @@ -1,114 +1,114 @@ - - - - - - - - -FatFs - f_printf - - - - -
-

f_printf

-

The f_printf function writes formatted string to the file.

-
-int f_printf (
-  FIL* fp,          /* [IN] File object */
-  const TCHAR* fmt, /* [IN] Format stirng */
-  ...
-);
-
-
- -
-

Parameters

-
-
fp
-
Pointer to the open file object structure.
-
fmt
-
Pointer to the null '\0' terminated format string. The terminator character will not be output.
-
...
-
Optional arguments...
- -
-
- - -
-

Return Values

-

When the string was written successfuly, it returns number of character encoding units written to the file. When the function failed due to disk full or an error, a negative value will be returned.

-
- - -
-

Description

-

The format control directive is a sub-set of standard library shown as follows:

-
-    %[flag][width][precision][size]type
-
-
-
flag
Padding options. A - specifies left-aligned. A 0 specifies zero padded. The default setting is in right-aligned and space padded.
-
width
Minimum width of the field, 1-99 or *. If the width of generated string is less than the specified value, rest field is padded with spaces or zeros. An * specifies the value comes from an argument in int type.
-
precision
Specifies number of fractional digits or maximum width of string, .0-.99 or .*. If number is omitted, it will be same as .0. Default setting is 6 for number and no limit for string.
-
size
Specifies size of integer argument, l(long) and ll(long long). If sizeof (long) == sizeof (int) is true (this is typical of 32-bit systems), prefix l can be omitted for long integer argument. The default size is int for integer arrument and floating point argument is always assumed double.
-
type
Specifies type of the output format and the argument as shown below. The length of generated string is in assumtion of int is 32-bit. - - - - - - - - - - - -
TypeFormatArgumentLength
cCharacterint,
long,
long long
1 character.
dSigned decimal1 to 11 (20 for ll) characters.
uUnsigned decimal1 to 10 (20 for ll) characters.
oUnsigned octal1 to 11 (22 for ll) characters.
x XUnsigned hexdecimal1 to 8 (16 for ll) characters.
bUnsigned binary1 to 32 characters. Limited to lower 32 digits when ll is specified.
sStringTCHAR*As input string. Null pointer generates a null string.
fFloating point
(decimal)
double1 to 31 characters. If the number of characters exceeds 31, it writes "±OV". Not a number and infinite write "NaN" and "±INF".
e EFloating point
(e notation)
4 to 31 characters. If the number of characters exceeds 31 or exponent exceeds +99, it writes "±OV".
-
-
-

When FatFs is configured for Unicode API (FF_LFN_UNICODE >= 1), character encoding on the string fuctions, f_putc, f_puts, f_printf and f_gets function, is also switched to Unicode. The Unicode characters in multiple encoding unit, such as surrogate pair and multi-byte sequence, should not be divided into two function calls, or the character will be lost. The character encoding on the file to be written via this function is selected by FF_STRF_ENCODE. The characters with wrong encoding or invalid for the output encoding will be lost.

-
- - -
-

QuickInfo

-

This is a wrapper function of f_write function. Available when FF_FS_READONLY == 0 and FF_USE_STRFUNC >= 1. When FF_USE_STRFUNC == 2, '\n's in the generated string are written as '\r'+'\n' each.

-
- - -
-

Example

-
-    f_printf(fp, "%d", 1234);             /* "1234" */
-    f_printf(fp, "%6d,%3d%%", -200, 5);   /* "  -200,  5%" */
-    f_printf(fp, "%-6u", 100);            /* "100   " */
-    f_printf(fp, "%ld", 12345678);        /* "12345678" */
-    f_printf(fp, "%llu", 0x100000000);    /* "4294967296"   (FF_PRINT_LLI) */
-    f_printf(fp, "%lld", -1LL);           /* "-1"           (FF_PRINT_LLI) */
-    f_printf(fp, "%04x", 0xA3);           /* "00a3" */
-    f_printf(fp, "%08lX", 0x123ABC);      /* "00123ABC" */
-    f_printf(fp, "%016b", 0x550F);        /* "0101010100001111" */
-    f_printf(fp, "%*d", 6, 100);          /* "   100" */
-    f_printf(fp, "%s", "abcdefg");        /* "abcdefg" */
-    f_printf(fp, "%5s", "abc");           /* "  abc" */
-    f_printf(fp, "%-5s", "abc");          /* "abc  " */
-    f_printf(fp, "%.5s", "abcdefg");      /* "abcde" */
-    f_printf(fp, "%-5.2s", "abcdefg");    /* "ab   " */
-    f_printf(fp, "%c", 'a');              /* "a" */
-    f_printf(fp, "%12f", 10.0);           /* "   10.000000" (FF_PRINT_FLOAT) */
-    f_printf(fp, "%.4E", 123.45678);      /* "1.2346E+02"   (FF_PRINT_FLOAT) */
-
-
- - -
-

See Also

-

f_open, f_putc, f_puts, f_gets, f_close, FIL

-
- -

Return

- - + + + + + + +FatFs - f_printf + + + + +
+

f_printf

+

The f_printf function writes formatted string to the file.

+
+int f_printf (
+  FIL* fp,          /* [IN] File object */
+  const TCHAR* fmt, /* [IN] Format stirng */
+  ...
+);
+
+
+ +
+

Parameters

+
+
fp
+
Pointer to the open file object structure.
+
fmt
+
Pointer to the null '\0' terminated format string. The terminator character will not be output.
+
...
+
Optional arguments...
+ +
+
+ + +
+

Return Values

+

When the string was written successfuly, it returns number of character encoding units written to the file. When the function failed due to disk full or an error, a negative value will be returned.

+
+ + +
+

Description

+

The format control directive is a sub-set of standard library shown as follows:

+
+    %[flag][width][precision][size]type
+
+
+
flag
Padding option. A - specifies left-aligned. A 0 specifies zero padded. The default setting is in right-aligned and space padded.
+
width
Minimum width of the field, 1-99 or *. If the width of generated string is less than the minimum width, rest field is padded with spaces or zeros. An * specifies the value comes from an argument in int type. The default setting is zero.
+
precision
Specifies number of fractional digits or maximum width of string, .0-.99 or .*. If the number is omitted, it is same as .0. Default setting is 6 for number and no limit for string.
+
size
Specifies size of integer argument, l(long) and ll(long long). If sizeof (long) == sizeof (int) is true (this is typical of 32-bit systems), prefix l can be omitted for long integer argument. The default size is int for integer argument and floating point argument is always assumed double as the default argument promotion.
+
type
Specifies type of the output format and the argument as shown below. The length of generated string is in assumtion of int is 32-bit. + + + + + + + + + + + +
TypeFormatArgumentLength
cCharacterint,
long,
long long
1 character.
dSigned decimal1 to 11 (20 for ll) characters.
uUnsigned decimal1 to 10 (20 for ll) characters.
oUnsigned octal1 to 11 (22 for ll) characters.
x XUnsigned hexdecimal1 to 8 (16 for ll) characters.
bUnsigned binary1 to 32 characters. Limited to lower 32 digits when ll is specified.
sStringTCHAR*As input string. A null pointer generates a zero-length string.
fFloating point
(decimal)
double1 to 31 characters. If the number of characters exceeds 31, it writes "±OV". Not a number and infinite value write "NaN" and "±INF" respectively.
e EFloating point
(e notation)
4 to 31 characters. If the number of characters exceeds 31 or exponent exceeds +99, it writes "±OV".
+
+
+

When FatFs is configured for Unicode API (FF_LFN_UNICODE >= 1), character encoding on the string fuctions, f_putc, f_puts, f_printf and f_gets function, is also switched to Unicode. The Unicode characters in multiple encoding unit, such as surrogate pair and multi-byte sequence, should not be divided into two function calls, or the character will be lost. The character encoding on the file to be written via this function is selected by FF_STRF_ENCODE. If the character encoding differs between file data and API, it is converted in this function. Input characters with wrong encoding for output will be lost.

+

If sprintf is used in the project and code conversion is not needed, f_write with sprintf will be efficient in code size and performance rather than f_printf.

+
+ + +
+

QuickInfo

+

This is a wrapper function of f_write function. Available when FF_FS_READONLY == 0 and FF_USE_STRFUNC >= 1. When FF_USE_STRFUNC == 2, '\n's in the generated string are written as '\r'+'\n' each.

+
+ + +
+

Example

+
+    f_printf(fp, "%d", 1234);             /* "1234" */
+    f_printf(fp, "%6d,%3d%%", -200, 5);   /* "  -200,  5%" */
+    f_printf(fp, "%-6u", 100);            /* "100   " */
+    f_printf(fp, "%ld", 12345678);        /* "12345678" */
+    f_printf(fp, "%llu", 0x100000000);    /* "4294967296"   (FF_PRINT_LLI) */
+    f_printf(fp, "%lld", -1LL);           /* "-1"           (FF_PRINT_LLI) */
+    f_printf(fp, "%04x", 0xA3);           /* "00a3" */
+    f_printf(fp, "%08lX", 0x123ABC);      /* "00123ABC" */
+    f_printf(fp, "%016b", 0x550F);        /* "0101010100001111" */
+    f_printf(fp, "%*d", 6, 100);          /* "   100" */
+    f_printf(fp, "%s", "abcdefg");        /* "abcdefg" */
+    f_printf(fp, "%5s", "abc");           /* "  abc" */
+    f_printf(fp, "%-5s", "abc");          /* "abc  " */
+    f_printf(fp, "%.5s", "abcdefg");      /* "abcde" */
+    f_printf(fp, "%-5.2s", "abcdefg");    /* "ab   " */
+    f_printf(fp, "%c", 'a');              /* "a" */
+    f_printf(fp, "%12f", 10.0);           /* "   10.000000" (FF_PRINT_FLOAT) */
+    f_printf(fp, "%.4E", 123.45678);      /* "1.2346E+02"   (FF_PRINT_FLOAT) */
+
+
+ + +
+

See Also

+

f_open, f_putc, f_puts, f_gets, f_close, FIL

+
+ + +

Return

+ + diff --git a/vendor/fatfs/documents/doc/putc.html b/vendor/fatfs/documents/doc/putc.html index 747b5fd..e48fbb1 100644 --- a/vendor/fatfs/documents/doc/putc.html +++ b/vendor/fatfs/documents/doc/putc.html @@ -3,8 +3,6 @@ - - FatFs - f_putc @@ -55,6 +53,7 @@

See Also

f_open, f_puts, f_printf, f_gets, f_close, FIL

+

Return

diff --git a/vendor/fatfs/documents/doc/puts.html b/vendor/fatfs/documents/doc/puts.html index 3695942..2def40f 100644 --- a/vendor/fatfs/documents/doc/puts.html +++ b/vendor/fatfs/documents/doc/puts.html @@ -3,8 +3,6 @@ - - FatFs - f_puts @@ -41,7 +39,7 @@

Return Value

Description

-

When FatFs is configured for Unicode API (FF_LFN_UNICODE >= 1), character encoding on the string fuctions, f_putc, f_puts, f_printf and f_gets function, is also switched to Unicode. The input Unicode characters in multiple encoding unit, such as surrogate pair and multi-byte sequence, should not be divided into two function calls, or the character will be lost. The character encoding on the file to be written via this functions is selected by FF_STRF_ENCODE. The characters with wrong encoding or invalid for the output encoding will be lost.

+

When FatFs is configured for Unicode API (FF_LFN_UNICODE >= 1), character encoding on the string fuctions, f_putc, f_puts, f_printf and f_gets function, is also switched to Unicode. The input Unicode characters in multiple encoding unit, such as surrogate pair and multi-byte sequence, should not be divided into two function calls, or the character will be lost. The character encoding on the file to be written via this functions is selected by FF_STRF_ENCODE. If the character encoding differs between file data and API, it is converted in this function. Input characters with wrong encoding for output will be lost.

@@ -56,6 +54,7 @@

See Also

f_open, f_putc, f_printf, f_gets, f_close, FIL

+

Return

diff --git a/vendor/fatfs/documents/doc/rc.html b/vendor/fatfs/documents/doc/rc.html index 52b496c..89823ec 100644 --- a/vendor/fatfs/documents/doc/rc.html +++ b/vendor/fatfs/documents/doc/rc.html @@ -3,8 +3,6 @@ - - FatFs - API Return Code @@ -49,7 +47,7 @@

Return Code of API Functions

Could not find the path. A directory in the path name could not be found.
FR_INVALID_NAME
-
The given string is invalid as the path name. One of the following possibilities is suspected. +
The given string is invalid as a path name. One of the following possibilities is suspected.
  • There is a character not allowed for the file name.
  • The file name is out of 8.3 format. (at non-LFN cfg.)
  • @@ -66,8 +64,8 @@

    Return Code of API Functions

  • Deleting the non-empty directory or current directory. (f_unlink)
  • Reading the file opened without FA_READ flag. (f_read)
  • Any modification to the file opened without FA_WRITE flag. (f_write, f_truncate, f_expand)
  • -
  • Could not create the object due to root directory full or disk full. (f_open, f_mkfs)
  • -
  • Could not allocate a contiguous area to the file. (f_expand)
  • +
  • Could not create the object due to root directory full or disk full. (f_open, f_mkdir)
  • +
  • Could not find a contiguous area for the file. (f_expand)
@@ -75,10 +73,10 @@

Return Code of API Functions

Name collision. An object with the same name is already existing in the directory.
FR_INVALID_OBJECT
-
The file/directory object is invalid or a null pointer is given. There are some reasons as follows: +
The file/directory object is invalid or the pointer is null. There are some reasons as follows:
    -
  • It has been closed, or the structure has been collapsed.
  • -
  • It has been invalidated. Open objects on the volume are invalidated by voulme mount process.
  • +
  • The file/directory has been closed.
  • +
  • The file/directory object has been invalidated or the structure has been collapsed. Open objects on the volume will be invalidated by a voulme mount process.
  • Physical drive is not ready to work due to a media removal.
@@ -93,8 +91,9 @@

Return Code of API Functions

Work area for the logical drive has not been registered by f_mount function.
FR_NO_FILESYSTEM
-
No valid FAT volume could not be found in the drive. One of the following possibilities is suspected. +
Valid FAT volume could not be found in the drive. One of the following possibilities is suspected.
    +
  • The FAT volume on the drive is collapsed.
  • Wrong lower layer implementation.
  • Wrong VolToPart[] settings. (FF_MULTI_PARTITION = 1)
@@ -129,6 +128,7 @@

Return Code of API Functions

The given parameter is invalid or there is an inconsistent for the volume.
+

Return

diff --git a/vendor/fatfs/documents/doc/read.html b/vendor/fatfs/documents/doc/read.html index 8ad2a1b..0dd7f53 100644 --- a/vendor/fatfs/documents/doc/read.html +++ b/vendor/fatfs/documents/doc/read.html @@ -3,8 +3,6 @@ - - FatFs - f_read @@ -28,13 +26,13 @@

f_read

Parameters

fp
-
Pointer to the open file object.
+
Pointer to the open file object structure. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
buff
Pointer to the buffer to store the read data.
btr
-
Number of bytes to read in range of UINT type. If you need to read data fast, you should read data in large chunk as possible.
+
Number of bytes to read in range of UINT type. If the file needs to be read fast, it should be read in large chunk as possible.
br
-
Pointer to the UINT variable that receives number of bytes read. This value is always valid after the function call regardless of the function return code. If the return value is equal to btr, the function return code should be FR_OK.
+
Pointer to the variable in UINT type that receives number of bytes read. This value is always valid after the function call regardless of the function return code. If the return value is equal to btr, the function return code should be FR_OK.
@@ -54,7 +52,7 @@

Return Values

Description

-

The function starts to read data from the file at the file offset pointed by read/write pointer. The read/write pointer advances as number of bytes read. After the function succeeded, *br should be checked to detect end of the file. In case of *br < btr, it means the read/write pointer reached end of the file during read operation.

+

The function starts to read data from the file at the file offset pointed by read/write pointer of the file object. The read/write pointer advances as number of bytes read. After the function succeeded, *br should be checked to detect end of the file. In case of *br < btr, it means the read/write pointer hit end of the file during read operation.

@@ -75,6 +73,7 @@

See Also

f_open, fgets, f_write, f_close, FIL

+

Return

diff --git a/vendor/fatfs/documents/doc/readdir.html b/vendor/fatfs/documents/doc/readdir.html index 52a96b9..9ff76c4 100644 --- a/vendor/fatfs/documents/doc/readdir.html +++ b/vendor/fatfs/documents/doc/readdir.html @@ -3,8 +3,6 @@ - - FatFs - f_readdir @@ -53,19 +51,19 @@

Return Values

Description

-

The f_readdir function reads a directory item, informations about the object, from the open directory. Items in the directory can be read in sequence by f_readdir function calls. When all items in the directory have been read and no item to read, a null string is stored into the fno->fname[] without any error. When a null pointer is given to the fno, the read index of the directory object is rewinded. The f_rewinddir function is implemented as a macro.

+

The f_readdir function reads a directory item, informations about the object, from the open directory. Items in the directory can be read by f_readdir function calls in order of the directory table. When all items in the directory have been read and no item to read any more, a null string in fno->fname[] will be returned without an error. If a null pointer is given to the fno, the read index of the directory object will be rewound. The f_rewinddir function is implemented as a macro.

 #define f_rewinddir(dp) f_readdir((dp), 0)
 
-

When LFN is enabled, a member altname[] is defined in the file information structure to store the short file name of the object. If the long file name is not accessible due to a reason listed below, short file name is stored to the fname[] and the altname[] has a null string.

+

When LFN is enabled, a member altname[] is defined in the file information structure to store the short file name of the object. If the long file name is not accessible due to any reason listed below, short file name is stored to the fname[] and the altname[] has a null string.

-

There is an issue on read directories in exFAT volume. The exFAT does not support short file name. This means no name can be returned on the condition above. If it is the case, "?" is returned as the file name to indicate that the object is not accessible. To avoid this problem, configure FatFs FF_LFN_UNICODE != 0 and FF_MAX_LFN == 255 to support the full feature of LFN specification.

-

Dot entries ("." and "..") in the sub-directory of FAT volume are filtered out and they will never appear in the read items because exFAT lacks dot entries in the sub-directory.

+

There is an issue on read the directories on the exFAT volume. The exFAT does not support short file name. This means no name can be returned on the condition above. If it is the case, "?" is returned as the file name to indicate that the object is not accessible. To avoid this problem, configure FatFs FF_LFN_UNICODE != 0 and FF_MAX_LFN == 255 to support the full feature of LFN specification.

+

Dot entries ("." and "..") in the sub-directory of FAT volume are filtered out and they will never appear in the read items because of the consistency with exFAT which lacks dot entries in the sub-directory.

@@ -78,6 +76,41 @@

QuickInfo

Sample Code

+/* List contents of a directory */
+
+FRESULT list_dir (const char *path)
+{
+    FRESULT res;
+    DIR dir;
+    FILINFO fno;
+    int nfile, ndir;
+
+
+    res = f_opendir(&dir, path);                   /* Open the directory */
+    if (res == FR_OK) {
+        nfile = ndir = 0;
+        for (;;) {
+            res = f_readdir(&dir, &fno);           /* Read a directory item */
+            if (fno.fname[0] == 0) break;          /* Error or end of dir */
+            if (fno.fattrib & AM_DIR) {            /* It is a directory */
+                printf("   <DIR>   %s\n", fno.fname);
+                ndir++;
+            } else {                               /* It is a file */
+                printf("%10u %s\n", fno.fsize, fno.fname);
+                nfile++;
+            }
+        }
+        f_closedir(&dir);
+        printf("%d dirs, %d files.\n", ndir, nfile);
+    } else {
+        printf("Failed to open \"%s\". (%u)\n", path, res);
+    }
+    return res;
+}
+
+
+/* Recursive scan of all items in the directory */
+
 FRESULT scan_files (
     char* path        /* Start node to be scanned (***also used as work area***) */
 )
@@ -88,22 +121,22 @@ 

Sample Code

static FILINFO fno; - res = f_opendir(&dir, path); /* Open the directory */ + res = f_opendir(&dir, path); /* Open the directory */ if (res == FR_OK) { for (;;) { - res = f_readdir(&dir, &fno); /* Read a directory item */ - if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */ - if (fno.fattrib & AM_DIR) { /* It is a directory */ + res = f_readdir(&dir, &fno); /* Read a directory item */ + if (fno.fname[0] == 0) break; /* Break on error or end of dir */ + if (fno.fattrib & AM_DIR) { /* The item is a directory */ i = strlen(path); sprintf(&path[i], "/%s", fno.fname); - res = scan_files(path); /* Enter the directory */ + res = scan_files(path); /* Enter the directory */ if (res != FR_OK) break; path[i] = 0; - } else { /* It is a file. */ + } else { /* The item is a file. */ printf("%s/%s\n", path, fno.fname); } } - f_closedir(&dir); + f_closedir(&dir); } return res; @@ -117,7 +150,7 @@

Sample Code

char buff[256]; - res = f_mount(&fs, "", 1); + res = f_mount(&fs, "", 1); if (res == FR_OK) { strcpy(buff, "/"); res = scan_files(buff); @@ -134,6 +167,7 @@

See Also

f_opendir, f_closedir, f_stat, FILINFO, DIR

+

Return

diff --git a/vendor/fatfs/documents/doc/rename.html b/vendor/fatfs/documents/doc/rename.html index 326af0d..9919bd8 100644 --- a/vendor/fatfs/documents/doc/rename.html +++ b/vendor/fatfs/documents/doc/rename.html @@ -3,8 +3,6 @@ - - FatFs - f_rename diff --git a/vendor/fatfs/documents/doc/sdir.html b/vendor/fatfs/documents/doc/sdir.html index eb18cc3..d137a07 100644 --- a/vendor/fatfs/documents/doc/sdir.html +++ b/vendor/fatfs/documents/doc/sdir.html @@ -3,8 +3,6 @@ - - FatFs - DIR @@ -33,6 +31,7 @@

DIR

+

Return

diff --git a/vendor/fatfs/documents/doc/setcp.html b/vendor/fatfs/documents/doc/setcp.html index a1d124b..9d5c774 100644 --- a/vendor/fatfs/documents/doc/setcp.html +++ b/vendor/fatfs/documents/doc/setcp.html @@ -3,8 +3,6 @@ - - FatFs - f_setcp diff --git a/vendor/fatfs/documents/doc/setlabel.html b/vendor/fatfs/documents/doc/setlabel.html index 9eb5e78..8871268 100644 --- a/vendor/fatfs/documents/doc/setlabel.html +++ b/vendor/fatfs/documents/doc/setlabel.html @@ -3,8 +3,6 @@ - - FatFs - f_setlabel @@ -25,7 +23,7 @@

f_setlabel

Parameters

label
-
Pointer to the null-terminated string that specifies the volume label to be set.
+
Pointer to the null-terminated string that specifies the volume label to be set. If a null pointer is given, the function fails with FR_INVALID_DRIVE.
@@ -49,7 +47,7 @@

Return Values

Description

-

When the string has a drive prefix, the volume label will be set to the volume specified by the drive prefix. Unix style volume ID cannot be used to specify the volume. If drive number is not specified, the volume label will be set to the default drive. If length of the given volume label is zero, the volume label on the volume will be removed. The format of the volume label is as shown below:

+

When the string has a drive prefix, the volume label will be set to the volume specified by the drive prefix. If drive number is not specified, the volume label will be set to the default drive. If length of the given volume label is zero, the volume label on the volume will be removed. The format of the volume label is as shown below:

-

Remark: The standard system (Windows) has a problem at the volume label with a heading \xE5 on the FAT volume. To avoid this problem, this function rejects such volume label as invalid name.

+

Remark: The standard system (Windows) has a problem in the volume label with a heading \xE5 on the FAT volume. To avoid this problem, this function rejects such volume label as invalid name.

@@ -77,6 +75,9 @@

Example

/* Remove volume label of the drive 2 */ f_setlabel("2:"); + + /* Set volume label in Unix style volume id */ + f_setlabel("/tfcard/LOG DISK");
diff --git a/vendor/fatfs/documents/doc/sfatfs.html b/vendor/fatfs/documents/doc/sfatfs.html index 6ecd6e5..b002ccf 100644 --- a/vendor/fatfs/documents/doc/sfatfs.html +++ b/vendor/fatfs/documents/doc/sfatfs.html @@ -3,8 +3,6 @@ - - FatFs - FATFS diff --git a/vendor/fatfs/documents/doc/sfile.html b/vendor/fatfs/documents/doc/sfile.html index 9234d32..c361483 100644 --- a/vendor/fatfs/documents/doc/sfile.html +++ b/vendor/fatfs/documents/doc/sfile.html @@ -3,8 +3,6 @@ - - FatFs - FIL @@ -38,6 +36,7 @@

FIL

+

Return

diff --git a/vendor/fatfs/documents/doc/sfileinfo.html b/vendor/fatfs/documents/doc/sfileinfo.html index db0f45e..33bd3ae 100644 --- a/vendor/fatfs/documents/doc/sfileinfo.html +++ b/vendor/fatfs/documents/doc/sfileinfo.html @@ -3,8 +3,6 @@ - - FatFs - FILINFO @@ -19,6 +17,10 @@

FILINFO

FSIZE_t fsize; /* File size */ WORD fdate; /* Last modified date */ WORD ftime; /* Last modified time */ +#if FF_FS_CRTIME + WORD crdate; /* Created date */ + WORD crtime; /* Created time */ +#endif BYTE fattrib; /* Attribute */ #if FF_USE_LFN TCHAR altname[FF_SFN_BUF + 1]; /* Alternative object name */ @@ -56,6 +58,12 @@

Members

Second / 2 (0..29)
+
crdate
+
The date when the file/directory was created. This member is available when FF_FS_CRTIME = 1. +
+
crtime
+
The time when the file/directory was created. This member is available when FF_FS_CRTIME = 1 +
fattrib
The attribute flags in combination of:
@@ -68,11 +76,12 @@

Members

fname[]
-
Null-terminated object name. A null string is stored when no item to read and it indicates this structure is invalid. The size of fname[] and altname[] each can be configured in LFN configuration.
+
Null-terminated object name is stored. If no item to read or an error occured in the function, a null string is stored to indicate this structure is invalid. The size of fname[] and altname[] each can be configured in LFN configuration.
altname[]
Alternative object name is stored if available. This member is not available in non-LFN configuration.
+

Return

diff --git a/vendor/fatfs/documents/doc/size.html b/vendor/fatfs/documents/doc/size.html index 0be1a76..6946145 100644 --- a/vendor/fatfs/documents/doc/size.html +++ b/vendor/fatfs/documents/doc/size.html @@ -3,8 +3,6 @@ - - FatFs - f_size @@ -57,6 +55,7 @@

See Also

f_open, f_lseek, FIL

+

Return

diff --git a/vendor/fatfs/documents/doc/stat.html b/vendor/fatfs/documents/doc/stat.html index 2c2680a..75c59eb 100644 --- a/vendor/fatfs/documents/doc/stat.html +++ b/vendor/fatfs/documents/doc/stat.html @@ -3,8 +3,6 @@ - - FatFs - f_stat @@ -26,7 +24,7 @@

f_stat

Parameters

path
-
Pointer to the null-terminated string that specifies the object to get its information. The object must not be the root direcotry.
+
Pointer to the null-terminated string that specifies the object to get its information. The object must not be the root direcotry. If a null pointer is given, the function fails with FR_INVALID_DRIVE.
fno
Pointer to the blank FILINFO structure to store the information of the object. Set null pointer if this information is not needed.
@@ -54,7 +52,8 @@

Return Values

Description

-

The f_stat function checks the existence of a file or sub-directory in the directory. If not exist, the function returns with FR_NO_FILE. If exist, the function returns with FR_OK and the informations about the object, size, timestamp and attribute, is stored to the file information structure. For details of the file information, refer to the FILINFO structure and f_readdir function.

+

The f_stat function checks the existence of a file or sub-directory. If it is not exist, the function returns with FR_NO_FILE. If it is exist, the function returns with FR_OK and the informations about the object, size, timestamp and attribute, is stored to the file information structure. For details of the file information, refer to the FILINFO structure and f_readdir function.

+

Note that the file information comes from the meta data in the directory. If the file has been opend and modified, the file will need to be synced or closed in order to obtain the latest file information. This function cannot be used to retrieve the long file name with the short file name.

@@ -69,16 +68,17 @@

Example

     FRESULT fr;
     FILINFO fno;
+    const char *fname = "file.txt";
 
 
-    printf("Test for 'file.txt'...\n");
+    printf("Test for \"%s\"...\n", fname);
 
-    fr = f_stat("file.txt", &fno);
+    fr = f_stat(fname, &fno);
     switch (fr) {
 
     case FR_OK:
         printf("Size: %lu\n", fno.fsize);
-        printf("Timestamp: %u/%02u/%02u, %02u:%02u\n",
+        printf("Timestamp: %u-%02u-%02u, %02u:%02u\n",
                (fno.fdate >> 9) + 1980, fno.fdate >> 5 & 15, fno.fdate & 31,
                fno.ftime >> 11, fno.ftime >> 5 & 63);
         printf("Attributes: %c%c%c%c%c\n",
@@ -90,7 +90,8 @@ 

Example

break; case FR_NO_FILE: - printf("It is not exist.\n"); + case FR_NO_PATH: + printf("\"%s\" is not exist.\n", fname); break; default: @@ -101,10 +102,11 @@

Example

-

References

+

See Also

f_opendir, f_readdir, FILINFO

+

Return

diff --git a/vendor/fatfs/documents/doc/sync.html b/vendor/fatfs/documents/doc/sync.html index 9fb42a0..f135c5b 100644 --- a/vendor/fatfs/documents/doc/sync.html +++ b/vendor/fatfs/documents/doc/sync.html @@ -3,8 +3,6 @@ - - FatFs - f_sync @@ -68,7 +66,7 @@

Description

w - f_write() S - f_sync()
-

However there is no sense in f_sync function immediataly before f_close function because it performs f_sync function in it. In other words, the differnce between those functions is that the file object is invalidated or not.

+

However there is no sense in f_sync function jsut before f_close function, because the f_close performs f_sync in it. Actually, the differnce between these functions is that the file object is invalidated or not.

@@ -83,6 +81,7 @@

See Also

f_close, Critical section

+

Return

diff --git a/vendor/fatfs/documents/doc/tell.html b/vendor/fatfs/documents/doc/tell.html index 075f033..c40ebe5 100644 --- a/vendor/fatfs/documents/doc/tell.html +++ b/vendor/fatfs/documents/doc/tell.html @@ -3,8 +3,6 @@ - - FatFs - f_tell @@ -57,6 +55,7 @@

See Also

f_open, f_lseek, FIL

+

Return

diff --git a/vendor/fatfs/documents/doc/truncate.html b/vendor/fatfs/documents/doc/truncate.html index 0ac012f..bbc335b 100644 --- a/vendor/fatfs/documents/doc/truncate.html +++ b/vendor/fatfs/documents/doc/truncate.html @@ -3,8 +3,6 @@ - - FatFs - f_truncate @@ -25,7 +23,7 @@

f_truncate

Parameter

fp
-
Pointer to the open file object to be truncated.
+
Pointer to the open file object to be truncated. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
diff --git a/vendor/fatfs/documents/doc/unlink.html b/vendor/fatfs/documents/doc/unlink.html index 7809648..f8e1d20 100644 --- a/vendor/fatfs/documents/doc/unlink.html +++ b/vendor/fatfs/documents/doc/unlink.html @@ -3,8 +3,6 @@ - - FatFs - f_unlink @@ -25,7 +23,7 @@

f_unlink

Parameter

path
-
Pointer to a null-terminated string that specifies the file or sub-directory to be removed.
+
Pointer to a null-terminated string that specifies the file or sub-directory to be removed. If a null pointer is given, the function fails with FR_INVALID_DRIVE.
diff --git a/vendor/fatfs/documents/doc/utime.html b/vendor/fatfs/documents/doc/utime.html index 40e39eb..08a89e3 100644 --- a/vendor/fatfs/documents/doc/utime.html +++ b/vendor/fatfs/documents/doc/utime.html @@ -3,8 +3,6 @@ - - FatFs - f_utime @@ -17,7 +15,7 @@

f_utime

 FRESULT f_utime (
   const TCHAR* path,  /* [IN] Object name */
-  const FILINFO* fno  /* [IN] Time and data to be set */
+  const FILINFO* fno  /* [IN] Time and date to be set */
 );
 
@@ -26,9 +24,9 @@

f_utime

Parameters

path
-
Pointer to the null-terminated string that specifies an object to be changed.
+
Pointer to the null-terminated string that specifies an object to be changed. If a null pointer is given, the function fails with FR_INVALID_DRIVE.
fno
-
Pointer to the file information structure that has a timestamp to be set in member fdate and ftime. Do not care any other members.
+
Pointer to the file information structure that has new timestamps to be set in the members. fdate and ftime are for the last modified time. When FF_FS_CRTIME = 1, crdate and crtime for the created time are available in addition. Every timestamp is in local time. If fdate or crdate is invalid (0), the modified time or created time is left unchanged respectively. Do not care any other member.
@@ -63,7 +61,7 @@

Description

Example

 FRESULT set_timestamp (
-    char *obj,     /* Pointer to the file name */
+    const char *fname,     /* Pointer to the file name */
     int year,
     int month,
     int mday,
@@ -76,8 +74,10 @@ 

Example

fno.fdate = (WORD)(((year - 1980) * 512U) | month * 32U | mday); fno.ftime = (WORD)(hour * 2048U | min * 32U | sec / 2U); - - return f_utime(obj, &fno); +#if FF_FS_CRTIME + fno.crdate = 0; /* Do not change created time in this code */ +#endif + return f_utime(fname, &fno); }
@@ -94,6 +94,7 @@

See Also

f_stat, FILINFO

+

Return

diff --git a/vendor/fatfs/documents/doc/write.html b/vendor/fatfs/documents/doc/write.html index 4897bf8..78d525e 100644 --- a/vendor/fatfs/documents/doc/write.html +++ b/vendor/fatfs/documents/doc/write.html @@ -3,8 +3,6 @@ - - FatFs - f_write @@ -28,13 +26,13 @@

f_write

Parameters

fp
-
Pointer to the open file object structure.
+
Pointer to the open file object structure. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
buff
Pointer to the data to be written.
btw
-
Specifies number of bytes to write in range of UINT type. If you need to write data fast, you should write data in large chunk as possible.
+
Specifies number of bytes to write in range of UINT type. If the data needs to be written fast, it should be written in large chunk as possible.
bw
-
Pointer to the UINT variable that receives the number of bytes written. This value is always valid after the function call regardless of the function return code. If the return value is equal to btw, the function return code should be FR_OK.
+
Pointer to the variable in UINT type that receives the number of bytes written. This value is always valid after the function call regardless of the function return code. If the return value is equal to btw, the function return code should be FR_OK.
@@ -54,7 +52,7 @@

Return Values

Description

-

The function starts to write data to the file at the file offset pointed by read/write pointer. The read/write pointer advances as number of bytes written. After the function succeeded, *bw should be checked to detect the disk full. In case of *bw < btw, it means the volume got full during the write operation. The function can take a time when the volume is full or close to full.

+

The function starts to write data to the file at the file offset pointed by read/write pointer of the file object. The read/write pointer advances as number of bytes written. After the function succeeded, *bw should be checked to detect the disk full. In case of *bw < btw, it means the volume got full during the write operation. The function can take a time when the volume is full or close to full.

@@ -75,6 +73,7 @@

See Also

f_open, f_read, fputc, fputs, fprintf, f_close, FIL

+

Return

diff --git a/vendor/fatfs/documents/res/app5.c b/vendor/fatfs/documents/res/app5.c index 9190dad..2739019 100644 --- a/vendor/fatfs/documents/res/app5.c +++ b/vendor/fatfs/documents/res/app5.c @@ -13,7 +13,7 @@ FRESULT test_contiguous_file ( *cont = 0; - fr = f_lseek(fp, 0); /* Validates and prepares the file */ + fr = f_rewind(fp); /* Validates and prepares the file */ if (fr != FR_OK) return fr; #if FF_MAX_SS == FF_MIN_SS diff --git a/vendor/fatfs/documents/res/uniconv.zip b/vendor/fatfs/documents/res/uniconv.zip new file mode 100644 index 0000000..be7d84f Binary files /dev/null and b/vendor/fatfs/documents/res/uniconv.zip differ diff --git a/vendor/fatfs/documents/updates.html b/vendor/fatfs/documents/updates.html new file mode 100644 index 0000000..e97fa9d --- /dev/null +++ b/vendor/fatfs/documents/updates.html @@ -0,0 +1,544 @@ + + + + + + + +FatFs - Updates and Migration Notes + + +

Updates and Migration Notes

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
RevisionUpdatesMigration Notes
R0.15b
Jun 21, 2025
+Added support for timestamp of created time. (FF_FS_CRTIME)
+Fixed FatFs fails to load the FsInfo in FAT32 volumes and the f_getfree always be forced a full FAT scan which takes a long time. (appeared at R0.15a)
+
+Small changes to the f_utime function to support for the timestamp of created time.
+
R0.15a
Nov 22, 2024
+Fixed a complie error when FF_FS_LOCK != 0. (appeared at R0.15)
+Fixed a potential issue when work FatFs concurrency with FF_FS_REENTRANT, FF_VOLUMES >= 2 and FF_FS_LOCK > 0.
+Made f_setlabel accept a volume label with Unix style volume ID when FF_STR_VOLUME_ID == 2.
+Made FatFs update PercInUse field in exFAT VBR. (A preceding f_getfree is needed for the accuracy)
+
+
R0.15
Nov 6, 2022
+Changed user provided synchronization functions in order to completely eliminate the platform dependency from FatFs code.
+Fixed a potential error in f_mount when FF_FS_REENTRANT.
+Fixed file lock control FF_FS_LOCK is not mutal excluded when FF_FS_REENTRANT && FF_VOLUMES > 1 is true.
+Fixed f_mkfs creates broken exFAT volume when the size of volume is >= 2^32 sectors.
+Fixed string functions cannot write the unicode characters not in BMP when FF_LFN_UNICODE == 2 (UTF-8).
+Fixed a compatibility issue in identification of GPT header.
+
+User provided synchronization functions, ff_cre_syncobj, ff_del_syncobj, ff_req_grant and ff_rel_grant, needed when FF_FS_REENTRANT are replaced with ff_mutex_create, ff_mutex_delete, ff_mutex_take and ff_mutex_give respectively. For example, see ffsystem.c.
+FF_SYNC_t is removed from the configuration options.
+
R0.14b
Apr 17, 2021
+Made FatFs uses standard library string.h for copy, compare and search instead of built-in string functions.
+Added support for long long integer and floating point to f_printf. (FF_STRF_LLI and FF_STRF_FP)
+Made path name parser ignores the terminating separator to allow "dir/".
+Improved the compatibility in Unix style path name feature.
+Fixed the file gets dead-locked when f_open failed with certain conditions. (appeared at R0.12a)
+Fixed f_mkfs can create wrong exFAT volume due to a timing dependent error. (appeared at R0.12)
+Fixed code page 855 cannot be set by f_setcp. (appeared at R0.13)
+Fixed some compiler warnings.
+
+From this revision, FatFs depends on string.h.
+
R0.14a
Dec 05, 2020
+Limited number of recursive calls in f_findnext to prevent stack overflow.
+Fixed old floppy disks formatted with MS-DOS 2.x and 3.x cannot be mounted.
+Fixed some compiler warnings.
+
+Number of wildcards in the matching pattern in f_findfirst is limited to 4.
+
R0.14
Oct 14, 2019
+Added support for 64-bit LBA and GUID partition table (FF_LBA64)
+Changed some API functions, f_mkfs and f_fdisk.
+Fixed f_open cannot find the file with file name in length of FF_MAX_LFN characters.
+Fixed f_readdir cannot retrieve long file names in length of FF_MAX_LFN - 1 characters.
+Fixed f_readdir returns file names with wrong case conversion. (appeared at R0.12)
+Fixed f_mkfs can fail to create exFAT volume in the second partition. (appeared at R0.12)
+
+Usage of f_mkfs and f_fdisk is changed and some features are added to these functions.
+
R0.13c
Oct 14, 2018
+Supported stdint.h for C99 and later. (integer.h was included in ff.h)
+Fixed reading a directory gets infinite loop when the last directory entry is not empty. (appeared at R0.12)
+Fixed creating a sub-directory in the fragmented sub-directory on the exFAT volume collapses FAT chain of the parent directory. (appeared at R0.12)
+Fixed f_getcwd cause output buffer overrun when the buffer has a valid drive number. (appeared at R0.13b)
+
+From this revision, FatFs depends on stdint.h in C99 or later.
+integer.h is removed.
+
R0.13b
Apr 07, 2018
+Added support for UTF-32 encoding on the API. (FF_LFN_UNICODE = 3)
+Added support for Unix style volume prefix. (FF_STR_VOLUME_ID = 2)
+Fixed accesing objects in the exFAT root directory beyond the cluster boundary can fail. (appeared at R0.12c)
+Fixed f_setlabel does not reject some invalid characters. (appeared at R0.09b)
+
+
R0.13a
Oct 14, 2017
+Added support for UTF-8 encoding on the API. (FF_LFN_UNICODE = 2)
+Added options for file name output buffer. (FF_LFN_BUF, FF_SFN_BUF)
+Added dynamic memory allocation option for working buffer of f_mkfs and f_fdisk.
+Fixed f_fdisk and f_mkfs create the partition table with wrong CHS parameters. (appeared at R0.09)
+Fixed f_unlink can cause lost clusters at fragmented file on the exFAT volume. (appeared at R0.12c)
+Fixed f_setlabel rejects some valid characters for exFAT volume. (appeared at R0.12)
+
+
R0.13
May 21, 2017
+Prefix of configuration item names are changed from "_" to "FF_".
+Added f_setcp, run-time code page configuration. (FF_CODE_PAGE = 0)
+Improved cluster allocation time on stretch a deep buried cluster chain.
+Improved processing time of f_mkdir with large cluster size by using FF_USE_LFN = 3.
+Improved exFAT NoFatChain flag of the fragmented file to be set after it is truncated and got contiguous.
+Fixed archive attribute is left not set when a file on the exFAT volume is renamed. (appeared at R0.12)
+Fixed exFAT FAT entry can be collapsed when write or lseek operation to the existing file is done. (appeared at R0.12c)
+Fixed creating a file can fail when a new cluster allocation to the exFAT directory occures. (appeared at R0.12c)
+
+ASCII only configuration, FF_CODE_PAGE = 1, is removed. Use FF_CODE_PAGE = 437 instead.
+
R0.12c
Mar 04, 2017
+Improved write throughput at the fragmented file on the exFAT volume.
+Made memory usage for exFAT be able to be reduced as decreasing _MAX_LFN.
+Fixed successive f_getfree can return wrong count on the FAT12/16 volume. (appeared at R0.12)
+Fixed configuration option _VOLUMES cannot be set 10. (appeared at R0.10c)
+
+
R0.12b
Sep 4, 2016
+Made f_rename be able to rename objects with the same name but case.
+Fixed an error in the case conversion teble of code page 866. (ff.c)
+Fixed writing data is truncated at the file offset 4GiB on the exFAT volume. (appeared at R0.12)
+Fixed creating a file in the root directory of exFAT volume can fail. (appeared at R0.12)
+Fixed f_mkfs creating exFAT volume with too small cluster size can collapse unallocated memory. (appeared at R0.12a)
+Fixed wrong object name can be returned when read directory at Unicode cfg. (appeared at R0.12)
+Fixed large file allocation/removing on the exFAT volume collapses allocation bitmap. (appeared at R0.12)
+Fixed some internal errors in f_expand and f_lseek. (appeared at R0.12)
+
+
R0.12a
Jul 10, 2016
+Added support for creating exFAT volume with some changes of f_mkfs.
+Added a file open method FA_OPEN_APPEND.
+f_forward is available regardless of _FS_TINY.
+Fixed f_mkfs creates broken volume. (appeared at R0.12)
+Fixed wrong memory read in create_name. (appeared at R0.12)
+Fixed compilation fails at some configurations, _USE_FASTSEEK and _USE_FORWARD.
+
+Usage of f_mkfs is changed.
+
R0.12
Apr 12, 2016
+Added support for exFAT file system. (_FS_EXFAT)
+Added f_expand. (_USE_EXPAND)
+Changed some members in FINFO and behavior of f_readdir.
+Added a configuration option _USE_CHMOD.
+Fixed errors in the case conversion teble of Unicode (cc*.c).
+
+Usage and members of FINFO sructure used in f_readdir is changed.
+Dot entries in the sub-directory are never appear in f_readdir.
+".." does not work as path name in exFAT volume.
+f_getcwd does not work in exFAT volume.
+Many members in FIL and DIR structure are changed.
+To use f_chmod, _USE_CHMOD needs to be set.
+_WORD_ACCESS is removed from the configuration options.
+
R0.11a
Sep 5, 2015
+Fixed wrong media change can lead a deadlock at thread-safe configuration.
+Added code page 771, 860, 861, 863, 864, 865 and 869. (_CODE_PAGE)
+Fixed errors in the case conversion teble of code page 437 and 850 (ff.c).
+Fixed errors in the case conversion teble of Unicode (cc*.c).
+
+Removed some code pages actually not exist on the standard systems. (_CODE_PAGE)
+
R0.11
Feb 9, 2015
+Added f_findfirst and f_findnext. (_USE_FIND)
+Fixed f_unlink does not remove cluster chain of the file. (appeared at R0.10c)
+Fixed _FS_NORTC option does not work properly. (appeared at R0.10c)
+
+
R0.10c
Nov 9, 2014
+Added a configuration option for the platforms without RTC. (_FS_NORTC)
+Fixed volume label created by Mac OS X cannot be retrieved with f_getlabel. (appeared at R0.09b)
+Fixed a potential problem of FAT access that can appear on disk error.
+Fixed null pointer dereference on attempting to delete the root direcotry. (appeared at R0.08)
+
+
R0.10b
May 19, 2014
+Fixed a hard error in the disk I/O layer can collapse the directory entry.
+Fixed LFN entry is not deleted on delete/rename an object with its lossy converted SFN. (appeared at R0.07)
+
+
R0.10a
Jan 15, 2014
+Added arbitrary strings as drive number in the path name. (_STR_VOLUME_ID)
+Added an option for minimum sector size. (_MIN_SS)
+2nd argument of f_rename can have a drive number and it will be ignored.
+Fixed f_mount with forced mount fails when drive number is larger than 0. (appeared at R0.10)
+Fixed f_close invalidates the file object without volume lock.
+Fixed volume lock is left acquired after return from f_closedir. (appeared at R0.10)
+Fixed creation of a directory entry with LFN fails on too many SFN collisions. (appeared at R0.07)
+
+
R0.10
Oct 2, 2013
+Added an option for character encoding on the file. (_STRF_ENCODE)
+Added f_closedir.
+Added forced full FAT scan option for f_getfree. (_FS_NOFSINFO)
+Added forced mount option with changes of f_mount.
+Improved behavior of volume auto detection.
+Improved write throughput of f_puts and f_printf.
+Changed argument of f_chdrive, f_mkfs, disk_read and disk_write.
+Fixed f_write can be truncated when the file size is close to 4 GB.
+Fixed f_open, f_mkdir and f_setlabel can return incorrect result code on error.
+
+
R0.09b
Jan 24, 2013
+Added f_getlabel and f_setlabel. (_USE_LABEL)
+
+
R0.09a
Aug 27, 2012
+Fixed assertion failure due to OS/2 EA on FAT12/16 volume.
+Changed file functions reject null object pointer to avoid crash.
+Changed option name _FS_SHARE to _FS_LOCK.
+
+
R0.09
Sep 6, 2011
+f_mkfs supports multiple partition on a physical drive.
+Added f_fdisk. (_MULTI_PARTITION = 2)
+
+
R0.08b
Jan 15, 2011
+Fast seek function is also applied to f_read and f_write.
+f_lseek reports required table size on creating CLMP.
+Extended format syntax of f_printf.
+Ignores duplicated directory separators in given path names.
+
+
R0.08a
Aug 16, 2010
+Added f_getcwd. (_FS_RPATH = 2)
+Added sector erase function. (_USE_ERASE)
+Moved file lock semaphore table from fs object to the bss.
+Fixed f_mkdir creates wrong directory on non-LFN cfg when the given name contains ';'.
+Fixed f_mkfs creates wrong FAT32 volume.
+
+
R0.08
May 15, 2010
+Added an option to _USE_LFN
+Added support of file lock. (_FS_SHARE)
+Added fast seek function. (_USE_FASTSEEK)
+Changed a type name on the API, XCHAR to TCHAR.
+Changed member, fname, in the FILINFO on Unicode cfg.
+String functions support UTF-8 encoding files on Unicode cfg.
+
+
R0.07e
Nov 3, 2009
+Separated out configuration options from ff.h to ffconf.h.
+Added a configuration option, _LFN_UNICODE.
+Fixed f_unlink fails to remove a sub-dir on _FS_RPATH.
+Fixed name matching error on the 13 char boundary.
+Changed f_readdir to return the SFN with always upper case on non-LFN cfg.
+
+
R0.07c
Jan 21, 2009
+Fixed f_unlink may return FR_OK on error.
+Fixed wrong cache control in f_lseek.
+Added support of relative path.
+Added f_chdir.
+Added f_chdrive.
+Added proper case conversion to extended characters.
+
+
R0.07a
Apr 14, 2009
+Separated out OS dependent code on re-entrant configuration.
+Added multiple sector size support.
+
+
R0.07
Apr 1, 2009
+Merged Tiny-FatFs into FatFs as a buffer configuration option.
+Added support for long file extension.
+Added multiple code page support.
+Added re-entrancy for multitask operation.
+Added auto cluster size selection to f_mkfs.
+Added rewind option to f_readdir.
+Changed result code of critical errors.
+Renamed string functions to avoid name collision.
+
+
R0.06
Apr 1, 2008
+Added f_forward. (Tiny-FatFs)
+Added string functions: f_gets, f_putc, f_puts and f_printf.
+Improved performance of f_lseek on moving to the same or following cluster.
+
+
R0.05a
Feb 3, 2008
+Added f_truncate.
+Added f_utime.
+Fixed off by one error at FAT sub-type determination.
+Fixed btr in f_read can be mistruncated.
+Fixed cached sector is left not flushed when create and close without write.
+
+
R0.05
Aug 26, 2007
+Changed arguments of f_read, f_write.
+Changed arguments of f_mkfs. (FatFs)
+Fixed f_mkfs on FAT32 creates incorrect FSInfo. (FatFs)
+Fixed f_mkdir on FAT32 creates broken directory. (FatFs)
+
+
R0.04b
May 5, 2007
+Added _USE_NTFLAG option.
+Added support for FSInfo in FAT32 volume.
+Fixed some problems corresponds to FAT32. (Tiny-FatFs)
+Fixed DBCS name can result FR_INVALID_NAME.
+Fixed short seek (<= csize) collapses the file object.
+
+
R0.04a
Apr 1, 2007
+Supported multiple partitions on a plysical drive. (FatFs)
+Added minimization level 3.
+Added a capability of extending file size to f_lseek.
+Fixed an endian sensitive code in f_mkfs. (FatFs)
+Fixed a problem corresponds to FAT32 support. (Tiny-FatFs)
+
+
R0.04
Feb 4, 2007
+Supported multiple drive system. (FatFs)
+Changed some APIs for multiple drive system.
+Added f_mkfs. (FatFs)
+Added _USE_FAT32 option. (Tiny-FatFs)
+
+
R0.03a
Dec 11, 2006
+Improved cluster scan algolithm to write files fast.
+Fixed f_mkdir creates broken directory on FAT32.
+
+
R0.03
Sep 22, 2006
+Added f_rename. +Changed option _FS_MINIMUM to _FS_MINIMIZE.
+
+
R0.02a
Jun 10, 2006
+Added a configuration option _FS_MINIMUM.
+
+
R0.02
Jun 01, 2006
+Added FAT12.
+Removed unbuffered mode.
+Fixed a problem on small (<32M) patition.
+
+
R0.01
Apr 29, 2006
+First release.
+
+
+

Return

+ + diff --git a/vendor/fatfs/documents/updates.txt b/vendor/fatfs/documents/updates.txt deleted file mode 100644 index 60b6e1a..0000000 --- a/vendor/fatfs/documents/updates.txt +++ /dev/null @@ -1,286 +0,0 @@ -R0.14b (April 17, 2021) - Made FatFs uses standard library for copy, compare and search instead of built-in string functions. - Added support for long long integer and floating point to f_printf(). (FF_STRF_LLI and FF_STRF_FP) - Made path name parser ignore the terminating separator to allow "dir/". - Improved the compatibility in Unix style path name feature. - Fixed the file gets dead-locked when f_open() failed with some conditions. (appeared at R0.12a) - Fixed f_mkfs() can create wrong exFAT volume due to a timing dependent error. (appeared at R0.12) - Fixed code page 855 cannot be set by f_setcp(). - Fixed some compiler warnings. - - -R0.14a (December 05, 2020) - Limited number of recursive calls in f_findnext(). - Fixed old floppy disks formatted with MS-DOS 2.x and 3.x cannot be mounted. - Fixed some compiler warnings. - - -R0.14 (October 14, 2019) - Added support for 64-bit LBA and GUID partition table (FF_LBA64) - Changed some API functions, f_mkfs() and f_fdisk(). - Fixed f_open() function cannot find the file with file name in length of FF_MAX_LFN characters. - Fixed f_readdir() function cannot retrieve long file names in length of FF_MAX_LFN - 1 characters. - Fixed f_readdir() function returns file names with wrong case conversion. (appeared at R0.12) - Fixed f_mkfs() function can fail to create exFAT volume in the second partition. (appeared at R0.12) - -R0.13c (October 14, 2018) - Supported stdint.h for C99 and later. (integer.h was included in ff.h) - Fixed reading a directory gets infinite loop when the last directory entry is not empty. (appeared at R0.12) - Fixed creating a sub-directory in the fragmented sub-directory on the exFAT volume collapses FAT chain of the parent directory. (appeared at R0.12) - Fixed f_getcwd() cause output buffer overrun when the buffer has a valid drive number. (appeared at R0.13b) - - -R0.13b (April 07, 2018) - Added support for UTF-32 encoding on the API. (FF_LFN_UNICODE = 3) - Added support for Unix style volume prefix. (FF_STR_VOLUME_ID = 2) - Fixed accesing any object on the exFAT root directory beyond the cluster boundary can fail. (appeared at R0.12c) - Fixed f_setlabel() does not reject some invalid characters. (appeared at R0.09b) - - -R0.13a (October 14, 2017) - Added support for UTF-8 encoding on the API. (FF_LFN_UNICODE = 2) - Added options for file name output buffer. (FF_LFN_BUF, FF_SFN_BUF). - Added dynamic memory allocation option for working buffer of f_mkfs() and f_fdisk(). - Fixed f_fdisk() and f_mkfs() create the partition table with wrong CHS parameters. (appeared at R0.09) - Fixed f_unlink() can cause lost clusters at fragmented file on the exFAT volume. (appeared at R0.12c) - Fixed f_setlabel() rejects some valid characters for exFAT volume. (appeared at R0.12) - - -R0.13 (May 21, 2017) - Changed heading character of configuration keywords "_" to "FF_". - Removed ASCII-only configuration, FF_CODE_PAGE = 1. Use FF_CODE_PAGE = 437 instead. - Added f_setcp(), run-time code page configuration. (FF_CODE_PAGE = 0) - Improved cluster allocation time on stretch a deep buried cluster chain. - Improved processing time of f_mkdir() with large cluster size by using FF_USE_LFN = 3. - Improved NoFatChain flag of the fragmented file to be set after it is truncated and got contiguous. - Fixed archive attribute is left not set when a file on the exFAT volume is renamed. (appeared at R0.12) - Fixed exFAT FAT entry can be collapsed when write or lseek operation to the existing file is done. (appeared at R0.12c) - Fixed creating a file can fail when a new cluster allocation to the exFAT directory occures. (appeared at R0.12c) - - -R0.12c (March 04, 2017) - Improved write throughput at the fragmented file on the exFAT volume. - Made memory usage for exFAT be able to be reduced as decreasing _MAX_LFN. - Fixed successive f_getfree() can return wrong count on the FAT12/16 volume. (appeared at R0.12) - Fixed configuration option _VOLUMES cannot be set 10. (appeared at R0.10c) - - -R0.12b (September 4, 2016) - Made f_rename() be able to rename objects with the same name but case. - Fixed an error in the case conversion teble of code page 866. (ff.c) - Fixed writing data is truncated at the file offset 4GiB on the exFAT volume. (appeared at R0.12) - Fixed creating a file in the root directory of exFAT volume can fail. (appeared at R0.12) - Fixed f_mkfs() creating exFAT volume with too small cluster size can collapse unallocated memory. (appeared at R0.12a) - Fixed wrong object name can be returned when read directory at Unicode cfg. (appeared at R0.12) - Fixed large file allocation/removing on the exFAT volume collapses allocation bitmap. (appeared at R0.12) - Fixed some internal errors in f_expand() and f_lseek(). (appeared at R0.12) - - -R0.12a (July 10, 2016) - Added support for creating exFAT volume with some changes of f_mkfs(). - Added a file open method FA_OPEN_APPEND. An f_lseek() following f_open() is no longer needed. - f_forward() is available regardless of _FS_TINY. - Fixed f_mkfs() creates wrong volume. (appeared at R0.12) - Fixed wrong memory read in create_name(). (appeared at R0.12) - Fixed compilation fails at some configurations, _USE_FASTSEEK and _USE_FORWARD. - - -R0.12 (April 12, 2016) - Added support for exFAT file system. (_FS_EXFAT) - Added f_expand(). (_USE_EXPAND) - Changed some members in FINFO structure and behavior of f_readdir(). - Added an option _USE_CHMOD and removed an option _WORD_ACCESS. - Fixed errors in the case conversion teble of Unicode (cc*.c). - - -R0.11a (September 5, 2015) - Fixed wrong media change can lead a deadlock at thread-safe configuration. - Added code page 771, 860, 861, 863, 864, 865 and 869. (_CODE_PAGE) - Removed some code pages actually not exist on the standard systems. (_CODE_PAGE) - Fixed errors in the case conversion teble of code page 437 and 850 (ff.c). - Fixed errors in the case conversion teble of Unicode (cc*.c). - - -R0.11 (February 9, 2015) - Added f_findfirst() and f_findnext(). (_USE_FIND) - Fixed f_unlink() does not remove cluster chain of the file. (appeared at R0.10c) - Fixed _FS_NORTC option does not work properly. (appeared at R0.10c) - - -R0.10c (November 9, 2014) - Added a configuration option for the platforms without RTC. (_FS_NORTC) - Fixed volume label created by Mac OS X cannot be retrieved with f_getlabel(). (appeared at R0.09b) - Fixed a potential problem of FAT access that can appear on disk error. - Fixed null pointer dereference on attempting to delete the root direcotry. (appeared at R0.08) - - -R0.10b (May 19, 2014) - Fixed a hard error in the disk I/O layer can collapse the directory entry. - Fixed LFN entry is not deleted on delete/rename an object with its lossy converted SFN. (appeared at R0.07) - - -R0.10a (January 15, 2014) - Added arbitrary strings as drive number in the path name. (_STR_VOLUME_ID) - Added an option for minimum sector size. (_MIN_SS) - 2nd argument of f_rename() can have a drive number and it will be ignored. - Fixed f_mount() with forced mount fails when drive number is larger than 0. (appeared at R0.10) - Fixed f_close() invalidates the file object without volume lock. - Fixed volume lock is left acquired after return from f_closedir(). (appeared at R0.10) - Fixed creation of a directory entry with LFN fails on too many SFN collisions. (appeared at R0.07) - - -R0.10 (October 2, 2013) - Added an option for character encoding on the file. (_STRF_ENCODE) - Added f_closedir(). - Added forced full FAT scan option for f_getfree(). (_FS_NOFSINFO) - Added forced mount option with changes of f_mount(). - Improved behavior of volume auto detection. - Improved write throughput of f_puts() and f_printf(). - Changed argument of f_chdrive(), f_mkfs(), disk_read() and disk_write(). - Fixed f_write() can be truncated when the file size is close to 4GB. - Fixed f_open(), f_mkdir() and f_setlabel() can return incorrect result code on error. - - -R0.09b (January 24, 2013) - Added f_getlabel() and f_setlabel(). (_USE_LABEL = 1) - - -R0.09a (August 27, 2012) - Fixed assertion failure due to OS/2 EA on FAT12/16 volume. - Changed file functions reject null object pointer to avoid crash. - Changed option name _FS_SHARE to _FS_LOCK. - - -R0.09 (September 6, 2011) - f_mkfs() supports multiple partition on a physical drive. - Added f_fdisk(). (_MULTI_PARTITION = 2) - - -R0.08b (January 15, 2011) - Fast seek function is also applied to f_read() and f_write(). - f_lseek() reports required table size on creating CLMP. - Extended format syntax of f_printf(). - Ignores duplicated directory separators in given path names. - - -R0.08a (August 16, 2010) - Added f_getcwd(). (_FS_RPATH = 2) - Added sector erase function. (_USE_ERASE) - Moved file lock semaphore table from fs object to the bss. - Fixed a wrong directory entry is created on non-LFN cfg when the given name contains ';'. - Fixed f_mkfs() creates wrong FAT32 volume. - - -R0.08 (May 15, 2010) - Added a memory configuration option. (_USE_LFN) - Added support of file lock. (_FS_SHARE) - Added fast seek function. (_USE_FASTSEEK) - Changed some types on the API, XCHAR->TCHAR. - Changed fname member in the FILINFO structure on Unicode cfg. - String functions support UTF-8 encoding files on Unicode cfg. - - -R0.07e (November 3, 2009) - Separated out configuration options from ff.h to ffconf.h. - Added a configuration option, _LFN_UNICODE. - Fixed f_unlink() fails to remove a sub-dir on _FS_RPATH. - Fixed name matching error on the 13 char boundary. - Changed f_readdir() to return the SFN with always upper case on non-LFN cfg. - - -R0.07c (Junuary 21, 2009) - Fixed f_unlink() may return FR_OK on error. - Fixed wrong cache control in f_lseek(). - Added support of relative path. - Added f_chdir(). - Added f_chdrive(). - Added proper case conversion to extended characters. - - -R0.07a (April 14, 2009) - Separated out OS dependent code on re-entrant configuration. - Added multiple sector size support. - - -R0.07 (April 1, 2009) - Merged Tiny-FatFs into FatFs as a buffer configuration option. - Added long file name support. - Added multiple code page support. - Added re-entrancy for multitask operation. - Added auto cluster size selection to f_mkfs(). - Added rewind option to f_readdir(). - Changed result code of critical errors. - Renamed string functions to avoid name collision. - - -R0.06 (April 1, 2008) - Added f_forward. (Tiny-FatFs) - Added string functions: fgets, fputc, fputs and fprintf. - Improved performance of f_lseek on moving to the same or following cluster. - - -R0.05a (February 3, 2008) - Added f_truncate. - Added f_utime. - Fixed off by one error at FAT sub-type determination. - Fixed btr in f_read can be mistruncated. - Fixed cached sector is left not flushed when create and close without write. - - -R0.05 (August 26, 2007) - Changed arguments of f_read, f_write. - Changed arguments of f_mkfs. (FatFs) - Fixed f_mkfs on FAT32 creates incorrect FSInfo. (FatFs) - Fixed f_mkdir on FAT32 creates incorrect directory. (FatFs) - - -R0.04b (May 5, 2007) - Added _USE_NTFLAG option. - Added FSInfo support. - Fixed some problems corresponds to FAT32. (Tiny-FatFs) - Fixed DBCS name can result FR_INVALID_NAME. - Fixed short seek (<= csize) collapses the file object. - - -R0.04a (April 1, 2007) - Supported multiple partitions on a plysical drive. (FatFs) - Added minimization level 3. - Added a capability of extending file size to f_lseek. - Fixed an endian sensitive code in f_mkfs. (FatFs) - Fixed a problem corresponds to FAT32 support. (Tiny-FatFs) - - -R0.04 (February 4, 2007) - Supported multiple drive system. (FatFs) - Changed some APIs for multiple drive system. - Added f_mkfs. (FatFs) - Added _USE_FAT32 option. (Tiny-FatFs) - - -R0.03a (December 11, 2006) - Improved cluster scan algolithm to write files fast. - Fixed f_mkdir creates incorrect directory on FAT32. - - -R0.03 (September 22, 2006) - Added f_rename. - Changed option _FS_MINIMUM to _FS_MINIMIZE. - - -R0.02a (June 10, 2006) - Added a configuration option _FS_MINIMUM. - - -R0.02 (Jun 01, 2006) - Added FAT12. - Removed unbuffered mode. - Fixed a problem on small (<32M) patition. - - -R0.01 (April 29, 2006) - First release - - -R0.00 (February 26, 2006) - Prototype (not released) - diff --git a/vendor/fatfs/ff15b.zip b/vendor/fatfs/ff15b.zip new file mode 100644 index 0000000..069a970 Binary files /dev/null and b/vendor/fatfs/ff15b.zip differ diff --git a/vendor/fatfs/source/00history.txt b/vendor/fatfs/source/00history.txt index d3c910c..feec22c 100644 --- a/vendor/fatfs/source/00history.txt +++ b/vendor/fatfs/source/00history.txt @@ -357,3 +357,27 @@ R0.14b (April 17, 2021) Fixed some compiler warnings. + +R0.15 (November 6, 2022) + Changed user provided synchronization functions in order to completely eliminate the platform dependency from FatFs code. + FF_SYNC_t is removed from the configuration options. + Fixed a potential error in f_mount when FF_FS_REENTRANT. + Fixed file lock control FF_FS_LOCK is not mutal excluded when FF_FS_REENTRANT && FF_VOLUMES > 1 is true. + Fixed f_mkfs() creates broken exFAT volume when the size of volume is >= 2^32 sectors. + Fixed string functions cannot write the unicode characters not in BMP when FF_LFN_UNICODE == 2 (UTF-8). + Fixed a compatibility issue in identification of GPT header. + + + +R0.15a (November 22, 2024) + Fixed a complie error when FF_FS_LOCK != 0. + Fixed a potential issue when work FatFs concurrency with FF_FS_REENTRANT, FF_VOLUMES >= 2 and FF_FS_LOCK > 0. + Made f_setlabel() accept a volume label in Unix style volume ID when FF_STR_VOLUME_ID == 2. + Made FatFs update PercInUse field in exFAT VBR. (A preceding f_getfree() is needed for the accuracy) + + + +R0.15b (June 21, 2025) + Added support for timestamp of created time. (FF_FS_CRTIME) + Fixed FatFs fails to load the FsInfo in FAT32 volumes and the f_getfree always be forced a full FAT scan which takes a long time. (appeared at R0.15a) + diff --git a/vendor/fatfs/source/00readme.txt b/vendor/fatfs/source/00readme.txt index 53e035f..39f6c8c 100644 --- a/vendor/fatfs/source/00readme.txt +++ b/vendor/fatfs/source/00readme.txt @@ -1,4 +1,4 @@ -FatFs Module Source Files R0.14b +FatFs Module Source Files R0.15 FILES diff --git a/vendor/fatfs/source/ff.c b/vendor/fatfs/source/ff.c index cbdf685..9390b3f 100644 --- a/vendor/fatfs/source/ff.c +++ b/vendor/fatfs/source/ff.c @@ -1,8 +1,8 @@ /*----------------------------------------------------------------------------/ -/ FatFs - Generic FAT Filesystem Module R0.14b / +/ FatFs - Generic FAT Filesystem Module R0.15b / /-----------------------------------------------------------------------------/ / -/ Copyright (C) 2021, ChaN, all right reserved. +/ Copyright (C) 2025, ChaN, all right reserved. / / FatFs module is an open source software. Redistribution and use of FatFs in / source and binary forms, with or without modification, are permitted provided @@ -30,17 +30,17 @@ ---------------------------------------------------------------------------*/ -#if FF_DEFINED != 86631 /* Revision ID */ +#if FF_DEFINED != 5385 /* Revision ID */ #error Wrong include file (ff.h). #endif /* Limits and boundaries */ -#define MAX_DIR 0x200000 /* Max size of FAT directory */ -#define MAX_DIR_EX 0x10000000 /* Max size of exFAT directory */ +#define MAX_DIR 0x200000 /* Max size of FAT directory (byte) */ +#define MAX_DIR_EX 0x10000000 /* Max size of exFAT directory (byte) */ #define MAX_FAT12 0xFF5 /* Max FAT12 clusters (differs from specs, but right for real DOS/Windows behavior) */ #define MAX_FAT16 0xFFF5 /* Max FAT16 clusters (differs from specs, but right for real DOS/Windows behavior) */ -#define MAX_FAT32 0x0FFFFFF5 /* Max FAT32 clusters (not specified, practical limit) */ +#define MAX_FAT32 0x0FFFFFF5 /* Max FAT32 clusters (not defined in specs, practical limit) */ #define MAX_EXFAT 0x7FFFFFFD /* Max exFAT clusters (differs from specs, implementation limit) */ @@ -89,7 +89,7 @@ #define ET_FILENAME 0xC1 /* Name extension */ -/* FatFs refers the FAT structure as simple byte array instead of structure member +/* FatFs refers the FAT structures as simple byte array instead of structure member / because the C structure is not binary compatible between different platforms */ #define BS_JmpBoot 0 /* x86 jump instruction (3-byte) */ @@ -113,7 +113,7 @@ #define BS_VolLab 43 /* Volume label string (8-byte) */ #define BS_FilSysType 54 /* Filesystem type string (8-byte) */ #define BS_BootCode 62 /* Boot code (448-byte) */ -#define BS_55AA 510 /* Signature word (WORD) */ +#define BS_55AA 510 /* Boot signature (WORD, for VBR and MBR) */ #define BPB_FATSz32 36 /* FAT32: FAT size [sector] (DWORD) */ #define BPB_ExtFlags32 40 /* FAT32: Extended flags (WORD) */ @@ -139,18 +139,18 @@ #define BPB_RootClusEx 96 /* exFAT: Root directory start cluster (DWORD) */ #define BPB_VolIDEx 100 /* exFAT: Volume serial number (DWORD) */ #define BPB_FSVerEx 104 /* exFAT: Filesystem version (WORD) */ -#define BPB_VolFlagEx 106 /* exFAT: Volume flags (WORD) */ +#define BPB_VolFlagEx 106 /* exFAT: Volume flags (WORD, out of check sum calculation) */ #define BPB_BytsPerSecEx 108 /* exFAT: Log2 of sector size in unit of byte (BYTE) */ #define BPB_SecPerClusEx 109 /* exFAT: Log2 of cluster size in unit of sector (BYTE) */ #define BPB_NumFATsEx 110 /* exFAT: Number of FATs (BYTE) */ #define BPB_DrvNumEx 111 /* exFAT: Physical drive number for int13h (BYTE) */ -#define BPB_PercInUseEx 112 /* exFAT: Percent in use (BYTE) */ +#define BPB_PercInUseEx 112 /* exFAT: Percent in use (BYTE, out of check sum calculation) */ #define BPB_RsvdEx 113 /* exFAT: Reserved (7-byte) */ #define BS_BootCodeEx 120 /* exFAT: Boot code (390-byte) */ #define DIR_Name 0 /* Short file name (11-byte) */ #define DIR_Attr 11 /* Attribute (BYTE) */ -#define DIR_NTres 12 /* Lower case flag (BYTE) */ +#define DIR_NTres 12 /* Low case flags of SFN (BYTE) */ #define DIR_CrtTime10 13 /* Created time sub-second (BYTE) */ #define DIR_CrtTime 14 /* Created time (DWORD) */ #define DIR_LstAccDate 18 /* Last accessed date (WORD) */ @@ -194,40 +194,41 @@ #define FSI_StrucSig 484 /* FAT32 FSI: Structure signature (DWORD) */ #define FSI_Free_Count 488 /* FAT32 FSI: Number of free clusters (DWORD) */ #define FSI_Nxt_Free 492 /* FAT32 FSI: Last allocated cluster (DWORD) */ +#define FSI_TrailSig 508 /* FAT32 FSI: Trailing signature (DWORD) */ #define MBR_Table 446 /* MBR: Offset of partition table in the MBR */ #define SZ_PTE 16 /* MBR: Size of a partition table entry */ #define PTE_Boot 0 /* MBR PTE: Boot indicator */ -#define PTE_StHead 1 /* MBR PTE: Start head */ -#define PTE_StSec 2 /* MBR PTE: Start sector */ -#define PTE_StCyl 3 /* MBR PTE: Start cylinder */ +#define PTE_StHead 1 /* MBR PTE: Start head in CHS */ +#define PTE_StSec 2 /* MBR PTE: Start sector in CHS */ +#define PTE_StCyl 3 /* MBR PTE: Start cylinder in CHS */ #define PTE_System 4 /* MBR PTE: System ID */ -#define PTE_EdHead 5 /* MBR PTE: End head */ -#define PTE_EdSec 6 /* MBR PTE: End sector */ -#define PTE_EdCyl 7 /* MBR PTE: End cylinder */ +#define PTE_EdHead 5 /* MBR PTE: End head in CHS */ +#define PTE_EdSec 6 /* MBR PTE: End sector in CHS */ +#define PTE_EdCyl 7 /* MBR PTE: End cylinder in CHS */ #define PTE_StLba 8 /* MBR PTE: Start in LBA */ #define PTE_SizLba 12 /* MBR PTE: Size in LBA */ -#define GPTH_Sign 0 /* GPT: Header signature (8-byte) */ -#define GPTH_Rev 8 /* GPT: Revision (DWORD) */ -#define GPTH_Size 12 /* GPT: Header size (DWORD) */ -#define GPTH_Bcc 16 /* GPT: Header BCC (DWORD) */ -#define GPTH_CurLba 24 /* GPT: Main header LBA (QWORD) */ -#define GPTH_BakLba 32 /* GPT: Backup header LBA (QWORD) */ -#define GPTH_FstLba 40 /* GPT: First LBA for partitions (QWORD) */ -#define GPTH_LstLba 48 /* GPT: Last LBA for partitions (QWORD) */ -#define GPTH_DskGuid 56 /* GPT: Disk GUID (16-byte) */ -#define GPTH_PtOfs 72 /* GPT: Partation table LBA (QWORD) */ -#define GPTH_PtNum 80 /* GPT: Number of table entries (DWORD) */ -#define GPTH_PteSize 84 /* GPT: Size of table entry (DWORD) */ -#define GPTH_PtBcc 88 /* GPT: Partation table BCC (DWORD) */ -#define SZ_GPTE 128 /* GPT: Size of partition table entry */ +#define GPTH_Sign 0 /* GPT HDR: Signature (8-byte) */ +#define GPTH_Rev 8 /* GPT HDR: Revision (DWORD) */ +#define GPTH_Size 12 /* GPT HDR: Header size (DWORD) */ +#define GPTH_Bcc 16 /* GPT HDR: Header BCC (DWORD) */ +#define GPTH_CurLba 24 /* GPT HDR: This header LBA (QWORD) */ +#define GPTH_BakLba 32 /* GPT HDR: Another header LBA (QWORD) */ +#define GPTH_FstLba 40 /* GPT HDR: First LBA for partition data (QWORD) */ +#define GPTH_LstLba 48 /* GPT HDR: Last LBA for partition data (QWORD) */ +#define GPTH_DskGuid 56 /* GPT HDR: Disk GUID (16-byte) */ +#define GPTH_PtOfs 72 /* GPT HDR: Partition table LBA (QWORD) */ +#define GPTH_PtNum 80 /* GPT HDR: Number of table entries (DWORD) */ +#define GPTH_PteSize 84 /* GPT HDR: Size of table entry (DWORD) */ +#define GPTH_PtBcc 88 /* GPT HDR: Partition table BCC (DWORD) */ +#define SZ_GPTE 128 /* GPT PTE: Size of a GPT partition table entry */ #define GPTE_PtGuid 0 /* GPT PTE: Partition type GUID (16-byte) */ #define GPTE_UpGuid 16 /* GPT PTE: Partition unique GUID (16-byte) */ -#define GPTE_FstLba 32 /* GPT PTE: First LBA (QWORD) */ -#define GPTE_LstLba 40 /* GPT PTE: Last LBA inclusive (QWORD) */ -#define GPTE_Flags 48 /* GPT PTE: Flags (QWORD) */ -#define GPTE_Name 56 /* GPT PTE: Name */ +#define GPTE_FstLba 32 /* GPT PTE: First LBA of partition (QWORD) */ +#define GPTE_LstLba 40 /* GPT PTE: Last LBA of partition (QWORD) */ +#define GPTE_Flags 48 /* GPT PTE: Partition flags (QWORD) */ +#define GPTE_Name 56 /* GPT PTE: Partition name */ /* Post process on fatal error in the file operations */ @@ -239,16 +240,16 @@ #if FF_USE_LFN == 1 #error Static LFN work area cannot be used in thread-safe configuration #endif -#define LEAVE_FF(fs, res) { unlock_fs(fs, res); return res; } +#define LEAVE_FF(fs, res) { unlock_volume(fs, res); return res; } #else #define LEAVE_FF(fs, res) return res #endif -/* Definitions of logical drive - physical location conversion */ +/* Definitions of logical drive to physical location conversion */ #if FF_MULTI_PARTITION -#define LD2PD(vol) VolToPart[vol].pd /* Get physical drive number */ -#define LD2PT(vol) VolToPart[vol].pt /* Get partition number (0:auto search, 1..:forced partition number) */ +#define LD2PD(vol) VolToPart[vol].pd /* Get physical drive number from the mapping table */ +#define LD2PT(vol) VolToPart[vol].pt /* Get partition number from the mapping table (0:auto search, 1-:forced partition number) */ #else #define LD2PD(vol) (BYTE)(vol) /* Each logical drive is associated with the same physical drive number */ #define LD2PT(vol) 0 /* Auto partition search */ @@ -278,15 +279,15 @@ /* File lock controls */ -#if FF_FS_LOCK != 0 +#if FF_FS_LOCK #if FF_FS_READONLY #error FF_FS_LOCK must be 0 at read-only configuration #endif -typedef struct { - FATFS *fs; /* Object ID 1, volume (NULL:blank entry) */ - DWORD clu; /* Object ID 2, containing directory (0:root) */ - DWORD ofs; /* Object ID 3, offset in the directory */ - WORD ctr; /* Object open counter, 0:none, 0x01..0xFF:read mode open count, 0x100:write mode */ +typedef struct { /* Open object identifier with status */ + FATFS* fs; /* Object ID 1, volume (NULL:blank entry) */ + DWORD clu; /* Object ID 2, containing directory (0:root) */ + DWORD ofs; /* Object ID 3, offset in the directory */ + UINT ctr; /* Object open status, 0:none, 0x01..0xFF:read mode open count, 0x100:write mode */ } FILESEM; #endif @@ -461,20 +462,24 @@ typedef struct { #if FF_VOLUMES < 1 || FF_VOLUMES > 10 #error Wrong FF_VOLUMES setting #endif -static FATFS* FatFs[FF_VOLUMES]; /* Pointer to the filesystem objects (logical drives) */ +static FATFS *FatFs[FF_VOLUMES]; /* Pointer to the filesystem objects (logical drives) */ static WORD Fsid; /* Filesystem mount ID */ #if FF_FS_RPATH != 0 -static BYTE CurrVol; /* Current drive */ +static BYTE CurrVol; /* Current drive number set by f_chdrive() */ #endif -#if FF_FS_LOCK != 0 +#if FF_FS_LOCK static FILESEM Files[FF_FS_LOCK]; /* Open object lock semaphores */ +#if FF_FS_REENTRANT +static volatile BYTE SysLock; /* System lock flag to protect Files[] (0:no mutex, 1:unlocked, 2:locked) */ +static volatile BYTE SysLockVolume; /* Volume id who is locking Files[] */ +#endif #endif #if FF_STR_VOLUME_ID #ifdef FF_VOLUME_STRS -static const char* const VolumeStr[FF_VOLUMES] = {FF_VOLUME_STRS}; /* Pre-defined volume ID */ +static const char *const VolumeStr[FF_VOLUMES] = {FF_VOLUME_STRS}; /* Pre-defined volume ID */ #endif #endif @@ -563,7 +568,8 @@ static WCHAR LfnBuf[FF_MAX_LFN + 1]; /* LFN working buffer */ #if FF_CODE_PAGE == 0 /* Run-time code page configuration */ #define CODEPAGE CodePage static WORD CodePage; /* Current code page */ -static const BYTE *ExCvt, *DbcTbl; /* Pointer to current SBCS up-case table and DBCS code range table below */ +static const BYTE* ExCvt; /* Pointer to SBCS up-case table Ct???[] (null:disabled) */ +static const BYTE* DbcTbl; /* Pointer to DBCS code range table Dc???[] (null:disabled) */ static const BYTE Ct437[] = TBL_CT437; static const BYTE Ct720[] = TBL_CT720; @@ -611,7 +617,7 @@ static const BYTE DbcTbl[] = MKCVTBL(TBL_DC, FF_CODE_PAGE); /* Load/Store multi-byte word in the FAT structure */ /*-----------------------------------------------------------------------*/ -static WORD ld_word (const BYTE* ptr) /* Load a 2-byte little-endian word */ +static WORD ld_16 (const BYTE* ptr) /* Load a 2-byte little-endian word */ { WORD rv; @@ -620,7 +626,7 @@ static WORD ld_word (const BYTE* ptr) /* Load a 2-byte little-endian word */ return rv; } -static DWORD ld_dword (const BYTE* ptr) /* Load a 4-byte little-endian word */ +static DWORD ld_32 (const BYTE* ptr) /* Load a 4-byte little-endian word */ { DWORD rv; @@ -632,7 +638,7 @@ static DWORD ld_dword (const BYTE* ptr) /* Load a 4-byte little-endian word */ } #if FF_FS_EXFAT -static QWORD ld_qword (const BYTE* ptr) /* Load an 8-byte little-endian word */ +static QWORD ld_64 (const BYTE* ptr) /* Load an 8-byte little-endian word */ { QWORD rv; @@ -649,13 +655,13 @@ static QWORD ld_qword (const BYTE* ptr) /* Load an 8-byte little-endian word */ #endif #if !FF_FS_READONLY -static void st_word (BYTE* ptr, WORD val) /* Store a 2-byte word in little-endian */ +static void st_16 (BYTE* ptr, WORD val) /* Store a 2-byte word in little-endian */ { *ptr++ = (BYTE)val; val >>= 8; *ptr++ = (BYTE)val; } -static void st_dword (BYTE* ptr, DWORD val) /* Store a 4-byte word in little-endian */ +static void st_32 (BYTE* ptr, DWORD val) /* Store a 4-byte word in little-endian */ { *ptr++ = (BYTE)val; val >>= 8; *ptr++ = (BYTE)val; val >>= 8; @@ -664,7 +670,7 @@ static void st_dword (BYTE* ptr, DWORD val) /* Store a 4-byte word in little-end } #if FF_FS_EXFAT -static void st_qword (BYTE* ptr, QWORD val) /* Store an 8-byte word in little-endian */ +static void st_64 (BYTE* ptr, QWORD val) /* Store an 8-byte word in little-endian */ { *ptr++ = (BYTE)val; val >>= 8; *ptr++ = (BYTE)val; val >>= 8; @@ -739,7 +745,7 @@ static DWORD tchar2uni ( /* Returns a character in UTF-16 encoding (>=0x10000 on #if FF_LFN_UNICODE == 1 /* UTF-16 input */ WCHAR wc; - uc = *p++; /* Get a unit */ + uc = *p++; /* Get an encoding unit */ if (IsSurrogate(uc)) { /* Surrogate? */ wc = *p++; /* Get low surrogate */ if (!IsSurrogateH(uc) || !IsSurrogateL(wc)) return 0xFFFFFFFF; /* Wrong surrogate? */ @@ -747,7 +753,7 @@ static DWORD tchar2uni ( /* Returns a character in UTF-16 encoding (>=0x10000 on } #elif FF_LFN_UNICODE == 2 /* UTF-8 input */ - BYTE b; + BYTE tb; int nf; uc = (BYTE)*p++; /* Get an encoding unit */ @@ -761,10 +767,10 @@ static DWORD tchar2uni ( /* Returns a character in UTF-16 encoding (>=0x10000 on } else { /* Wrong sequence */ return 0xFFFFFFFF; } - do { /* Get trailing bytes */ - b = (BYTE)*p++; - if ((b & 0xC0) != 0x80) return 0xFFFFFFFF; /* Wrong sequence? */ - uc = uc << 6 | (b & 0x3F); + do { /* Get and merge trailing bytes */ + tb = (BYTE)*p++; + if ((tb & 0xC0) != 0x80) return 0xFFFFFFFF; /* Wrong sequence? */ + uc = uc << 6 | (tb & 0x3F); } while (--nf != 0); if (uc < 0x80 || IsSurrogate(uc) || uc >= 0x110000) return 0xFFFFFFFF; /* Wrong code? */ if (uc >= 0x010000) uc = 0xD800DC00 | ((uc - 0x10000) << 6 & 0x3FF0000) | (uc & 0x3FF); /* Make a surrogate pair if needed */ @@ -776,14 +782,14 @@ static DWORD tchar2uni ( /* Returns a character in UTF-16 encoding (>=0x10000 on if (uc >= 0x010000) uc = 0xD800DC00 | ((uc - 0x10000) << 6 & 0x3FF0000) | (uc & 0x3FF); /* Make a surrogate pair if needed */ #else /* ANSI/OEM input */ - BYTE b; + BYTE sb; WCHAR wc; wc = (BYTE)*p++; /* Get a byte */ if (dbc_1st((BYTE)wc)) { /* Is it a DBC 1st byte? */ - b = (BYTE)*p++; /* Get 2nd byte */ - if (!dbc_2nd(b)) return 0xFFFFFFFF; /* Invalid code? */ - wc = (wc << 8) + b; /* Make a DBC */ + sb = (BYTE)*p++; /* Get 2nd byte */ + if (!dbc_2nd(sb)) return 0xFFFFFFFF; /* Invalid code? */ + wc = (wc << 8) + sb; /* Make a DBC */ } if (wc != 0) { wc = ff_oem2uni(wc, CODEPAGE); /* ANSI/OEM ==> Unicode */ @@ -875,7 +881,7 @@ static UINT put_utf ( /* Returns number of encoding units written (0:buffer over *buf++ = (TCHAR)wc; /* Store DBC 2nd byte */ return 2; } - if (wc == 0 || szb < 1) return 0; /* Invalid char or buffer overflow? */ + if (wc == 0 || szb < 1) return 0; /* Invalid character or buffer overflow? */ *buf++ = (TCHAR)wc; /* Store the character */ return 1; #endif @@ -887,21 +893,46 @@ static UINT put_utf ( /* Returns number of encoding units written (0:buffer over /*-----------------------------------------------------------------------*/ /* Request/Release grant to access the volume */ /*-----------------------------------------------------------------------*/ -static int lock_fs ( /* 1:Ok, 0:timeout */ - FATFS* fs /* Filesystem object */ + +static int lock_volume ( /* 1:Ok, 0:timeout */ + FATFS* fs, /* Filesystem object to lock */ + int syslock /* System lock required */ ) { - return ff_req_grant(fs->sobj); + int rv; + + +#if FF_FS_LOCK + rv = ff_mutex_take(fs->ldrv); /* Lock the volume */ + if (rv && syslock) { /* System lock reqiered? */ + rv = ff_mutex_take(FF_VOLUMES); /* Lock the system */ + if (rv) { + SysLockVolume = fs->ldrv; + SysLock = 2; /* System lock succeeded */ + } else { + ff_mutex_give(fs->ldrv); /* Failed system lock */ + } + } +#else + rv = syslock ? ff_mutex_take(fs->ldrv) : ff_mutex_take(fs->ldrv); /* Lock the volume (this is to prevent compiler warning) */ +#endif + return rv; } -static void unlock_fs ( +static void unlock_volume ( FATFS* fs, /* Filesystem object */ FRESULT res /* Result code to be returned */ ) { if (fs && res != FR_NOT_ENABLED && res != FR_INVALID_DRIVE && res != FR_TIMEOUT) { - ff_rel_grant(fs->sobj); +#if FF_FS_LOCK + if (SysLock == 2 && SysLockVolume == fs->ldrv) { /* Unlock system if it has been locked by this task */ + SysLock = 1; + ff_mutex_give(FF_VOLUMES); + } +#endif + ff_mutex_give(fs->ldrv); /* Unlock the volume */ } } @@ -909,12 +940,12 @@ static void unlock_fs ( -#if FF_FS_LOCK != 0 +#if FF_FS_LOCK /*-----------------------------------------------------------------------*/ -/* File lock control functions */ +/* File sharing control functions */ /*-----------------------------------------------------------------------*/ -static FRESULT chk_lock ( /* Check if the file can be accessed */ +static FRESULT chk_share ( /* Check if the file can be accessed */ DIR* dp, /* Directory object pointing the file to be checked */ int acc /* Desired access type (0:Read mode open, 1:Write mode open, 2:Delete or rename) */ ) @@ -941,16 +972,16 @@ static FRESULT chk_lock ( /* Check if the file can be accessed */ } -static int enq_lock (void) /* Check if an entry is available for a new object */ +static int enq_share (void) /* Check if an entry is available for a new object */ { UINT i; - for (i = 0; i < FF_FS_LOCK && Files[i].fs; i++) ; + for (i = 0; i < FF_FS_LOCK && Files[i].fs; i++) ; /* Find a free entry */ return (i == FF_FS_LOCK) ? 0 : 1; } -static UINT inc_lock ( /* Increment object open counter and returns its index (0:Internal error) */ +static UINT inc_share ( /* Increment object open counter and returns its index (0:Internal error) */ DIR* dp, /* Directory object pointing the file to register or increment */ int acc /* Desired access (0:Read, 1:Write, 2:Delete/Rename) */ ) @@ -965,7 +996,7 @@ static UINT inc_lock ( /* Increment object open counter and returns its index (0 } if (i == FF_FS_LOCK) { /* Not opened. Register it as new. */ - for (i = 0; i < FF_FS_LOCK && Files[i].fs; i++) ; + for (i = 0; i < FF_FS_LOCK && Files[i].fs; i++) ; /* Find a free entry */ if (i == FF_FS_LOCK) return 0; /* No free entry to register (int err) */ Files[i].fs = dp->obj.fs; Files[i].clu = dp->obj.sclust; @@ -981,30 +1012,32 @@ static UINT inc_lock ( /* Increment object open counter and returns its index (0 } -static FRESULT dec_lock ( /* Decrement object open counter */ +static FRESULT dec_share ( /* Decrement object open counter */ UINT i /* Semaphore index (1..) */ ) { - WORD n; + UINT n; FRESULT res; if (--i < FF_FS_LOCK) { /* Index number origin from 0 */ n = Files[i].ctr; - if (n == 0x100) n = 0; /* If write mode open, delete the entry */ + if (n == 0x100) n = 0; /* If write mode open, delete the object semaphore */ if (n > 0) n--; /* Decrement read mode open count */ Files[i].ctr = n; - if (n == 0) Files[i].fs = 0; /* Delete the entry if open count gets zero */ + if (n == 0) { /* Delete the object semaphore if open count becomes zero */ + Files[i].fs = 0; /* Free the entry << 1, there is a potential error in this process >>> */ + } res = FR_OK; } else { - res = FR_INT_ERR; /* Invalid index nunber */ + res = FR_INT_ERR; /* Invalid index number */ } return res; } -static void clear_lock ( /* Clear lock entries of the volume */ - FATFS *fs +static void clear_share ( /* Clear all lock entries of the volume */ + FATFS* fs ) { UINT i; @@ -1014,7 +1047,7 @@ static void clear_lock ( /* Clear lock entries of the volume */ } } -#endif /* FF_FS_LOCK != 0 */ +#endif /* FF_FS_LOCK */ @@ -1084,17 +1117,30 @@ static FRESULT sync_fs ( /* Returns FR_OK or FR_DISK_ERR */ res = sync_window(fs); if (res == FR_OK) { - if (fs->fs_type == FS_FAT32 && fs->fsi_flag == 1) { /* FAT32: Update FSInfo sector if needed */ - /* Create FSInfo structure */ - memset(fs->win, 0, sizeof fs->win); - st_word(fs->win + BS_55AA, 0xAA55); /* Boot signature */ - st_dword(fs->win + FSI_LeadSig, 0x41615252); /* Leading signature */ - st_dword(fs->win + FSI_StrucSig, 0x61417272); /* Structure signature */ - st_dword(fs->win + FSI_Free_Count, fs->free_clst); /* Number of free clusters */ - st_dword(fs->win + FSI_Nxt_Free, fs->last_clst); /* Last allocated culuster */ - fs->winsect = fs->volbase + 1; /* Write it into the FSInfo sector (Next to VBR) */ - disk_write(fs->pdrv, fs->win, fs->winsect, 1); + if (fs->fsi_flag == 1) { /* Allocation changed? */ fs->fsi_flag = 0; + if (fs->fs_type == FS_FAT32) { /* FAT32: Update FSInfo sector */ + /* Create FSInfo structure */ + memset(fs->win, 0, sizeof fs->win); + st_32(fs->win + FSI_LeadSig, 0x41615252); /* Leading signature */ + st_32(fs->win + FSI_StrucSig, 0x61417272); /* Structure signature */ + st_32(fs->win + FSI_Free_Count, fs->free_clst); /* Number of free clusters */ + st_32(fs->win + FSI_Nxt_Free, fs->last_clst); /* Last allocated culuster */ + st_32(fs->win + FSI_TrailSig, 0xAA550000); /* Trailing signature */ + disk_write(fs->pdrv, fs->win, fs->winsect = fs->volbase + 1, 1); /* Write it into the FSInfo sector (Next to VBR) */ + } +#if FF_FS_EXFAT + else if (fs->fs_type == FS_EXFAT) { /* exFAT: Update PercInUse field in BPB */ + if (disk_read(fs->pdrv, fs->win, fs->winsect = fs->volbase, 1) == RES_OK) { /* Load VBR */ + BYTE perc_inuse = (fs->free_clst <= fs->n_fatent - 2) ? (BYTE)((QWORD)(fs->n_fatent - 2 - fs->free_clst) * 100 / (fs->n_fatent - 2)) : 0xFF; /* Precent in use 0-100 or 0xFF(unknown) */ + + if (fs->win[BPB_PercInUseEx] != perc_inuse) { /* Write it back into VBR if needed */ + fs->win[BPB_PercInUseEx] = perc_inuse; + disk_write(fs->pdrv, fs->win, fs->winsect, 1); + } + } + } +#endif } /* Make sure that no pending write process in the lower layer */ if (disk_ioctl(fs->pdrv, CTRL_SYNC, 0) != RES_OK) res = FR_DISK_ERR; @@ -1156,12 +1202,12 @@ static DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, 2..0x7FFFFFF case FS_FAT16 : if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 2))) != FR_OK) break; - val = ld_word(fs->win + clst * 2 % SS(fs)); /* Simple WORD array */ + val = ld_16(fs->win + clst * 2 % SS(fs)); /* Simple WORD array */ break; case FS_FAT32 : if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK) break; - val = ld_dword(fs->win + clst * 4 % SS(fs)) & 0x0FFFFFFF; /* Simple DWORD array but mask out upper 4 bits */ + val = ld_32(fs->win + clst * 4 % SS(fs)) & 0x0FFFFFFF; /* Simple DWORD array but mask out upper 4 bits */ break; #if FF_FS_EXFAT case FS_EXFAT : @@ -1182,7 +1228,7 @@ static DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, 2..0x7FFFFFF val = 0x7FFFFFFF; /* Generate EOC */ } else { if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK) break; - val = ld_dword(fs->win + clst * 4 % SS(fs)) & 0x7FFFFFFF; + val = ld_32(fs->win + clst * 4 % SS(fs)) & 0x7FFFFFFF; } break; } @@ -1236,7 +1282,7 @@ static FRESULT put_fat ( /* FR_OK(0):succeeded, !=0:error */ case FS_FAT16: res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2))); if (res != FR_OK) break; - st_word(fs->win + clst * 2 % SS(fs), (WORD)val); /* Simple WORD array */ + st_16(fs->win + clst * 2 % SS(fs), (WORD)val); /* Simple WORD array */ fs->wflag = 1; break; @@ -1247,9 +1293,9 @@ static FRESULT put_fat ( /* FR_OK(0):succeeded, !=0:error */ res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))); if (res != FR_OK) break; if (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) { - val = (val & 0x0FFFFFFF) | (ld_dword(fs->win + clst * 4 % SS(fs)) & 0xF0000000); + val = (val & 0x0FFFFFFF) | (ld_32(fs->win + clst * 4 % SS(fs)) & 0xF0000000); } - st_dword(fs->win + clst * 4 % SS(fs), val); + st_32(fs->win + clst * 4 % SS(fs), val); fs->wflag = 1; break; } @@ -1430,7 +1476,7 @@ static FRESULT remove_chain ( /* FR_OK(0):succeeded, !=0:error */ res = put_fat(fs, clst, 0); /* Mark the cluster 'free' on the FAT */ if (res != FR_OK) return res; } - if (fs->free_clst < fs->n_fatent - 2) { /* Update FSINFO */ + if (fs->free_clst < fs->n_fatent - 2) { /* Update allocation information if it is valid */ fs->free_clst++; fs->fsi_flag |= 1; } @@ -1453,7 +1499,7 @@ static FRESULT remove_chain ( /* FR_OK(0):succeeded, !=0:error */ } #endif clst = nxt; /* Next cluster */ - } while (clst < fs->n_fatent); /* Repeat while not the last link */ + } while (clst < fs->n_fatent); /* Repeat until the last link */ #if FF_FS_EXFAT /* Some post processes for chain status */ @@ -1573,10 +1619,12 @@ static DWORD create_chain ( /* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:D } } - if (res == FR_OK) { /* Update FSINFO if function succeeded. */ + if (res == FR_OK) { /* Update allocation information if the function succeeded */ fs->last_clst = ncl; - if (fs->free_clst <= fs->n_fatent - 2) fs->free_clst--; - fs->fsi_flag |= 1; + if (fs->free_clst > 0 && fs->free_clst <= fs->n_fatent - 2) { + fs->free_clst--; + fs->fsi_flag |= 1; + } } else { ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1; /* Failed. Generate error status */ } @@ -1599,7 +1647,8 @@ static DWORD clmt_clust ( /* <2:Error, >=2:Cluster number */ FSIZE_t ofs /* File offset to be converted to cluster# */ ) { - DWORD cl, ncl, *tbl; + DWORD cl, ncl; + DWORD *tbl; FATFS *fs = fp->obj.fs; @@ -1821,9 +1870,9 @@ static DWORD ld_clust ( /* Returns the top cluster value of the SFN entry */ { DWORD cl; - cl = ld_word(dir + DIR_FstClusLO); + cl = ld_16(dir + DIR_FstClusLO); if (fs->fs_type == FS_FAT32) { - cl |= (DWORD)ld_word(dir + DIR_FstClusHI) << 16; + cl |= (DWORD)ld_16(dir + DIR_FstClusHI) << 16; } return cl; @@ -1837,9 +1886,9 @@ static void st_clust ( DWORD cl /* Value to be set */ ) { - st_word(dir + DIR_FstClusLO, (WORD)cl); + st_16(dir + DIR_FstClusLO, (WORD)cl); if (fs->fs_type == FS_FAT32) { - st_word(dir + DIR_FstClusHI, (WORD)(cl >> 16)); + st_16(dir + DIR_FstClusHI, (WORD)(cl >> 16)); } } #endif @@ -1852,31 +1901,31 @@ static void st_clust ( /*--------------------------------------------------------*/ static int cmp_lfn ( /* 1:matched, 0:not matched */ - const WCHAR* lfnbuf, /* Pointer to the LFN working buffer to be compared */ - BYTE* dir /* Pointer to the directory entry containing the part of LFN */ + const WCHAR* lfnbuf, /* Pointer to the LFN to be compared */ + BYTE* dir /* Pointer to the LFN entry */ ) { - UINT i, s; - WCHAR wc, uc; + UINT ni, di; + WCHAR pchr, chr; - if (ld_word(dir + LDIR_FstClusLO) != 0) return 0; /* Check LDIR_FstClusLO */ + if (ld_16(dir + LDIR_FstClusLO) != 0) return 0; /* Check if LDIR_FstClusLO is 0 */ - i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */ + ni = (UINT)((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the name to be compared */ - for (wc = 1, s = 0; s < 13; s++) { /* Process all characters in the entry */ - uc = ld_word(dir + LfnOfs[s]); /* Pick an LFN character */ - if (wc != 0) { - if (i >= FF_MAX_LFN + 1 || ff_wtoupper(uc) != ff_wtoupper(lfnbuf[i++])) { /* Compare it */ + for (pchr = 1, di = 0; di < 13; di++) { /* Process all characters in the entry */ + chr = ld_16(dir + LfnOfs[di]); /* Pick a character from the entry */ + if (pchr != 0) { + if (ni >= FF_MAX_LFN + 1 || ff_wtoupper(chr) != ff_wtoupper(lfnbuf[ni++])) { /* Compare it with name */ return 0; /* Not matched */ } - wc = uc; + pchr = chr; } else { - if (uc != 0xFFFF) return 0; /* Check filler */ + if (chr != 0xFFFF) return 0; /* Check filler */ } } - if ((dir[LDIR_Ord] & LLEF) && wc && lfnbuf[i]) return 0; /* Last segment matched but different length */ + if ((dir[LDIR_Ord] & LLEF) && pchr && lfnbuf[ni]) return 0; /* Last name segment matched but different length */ return 1; /* The part of LFN matched */ } @@ -1888,31 +1937,31 @@ static int cmp_lfn ( /* 1:matched, 0:not matched */ /*-----------------------------------------------------*/ static int pick_lfn ( /* 1:succeeded, 0:buffer overflow or invalid LFN entry */ - WCHAR* lfnbuf, /* Pointer to the LFN working buffer */ - BYTE* dir /* Pointer to the LFN entry */ + WCHAR* lfnbuf, /* Pointer to the name buffer to be stored */ + const BYTE* dir /* Pointer to the LFN entry */ ) { - UINT i, s; - WCHAR wc, uc; + UINT ni, di; + WCHAR pchr, chr; - if (ld_word(dir + LDIR_FstClusLO) != 0) return 0; /* Check LDIR_FstClusLO is 0 */ + if (ld_16(dir + LDIR_FstClusLO) != 0) return 0; /* Check if LDIR_FstClusLO is 0 */ - i = ((dir[LDIR_Ord] & ~LLEF) - 1) * 13; /* Offset in the LFN buffer */ + ni = (UINT)((dir[LDIR_Ord] & ~LLEF) - 1) * 13; /* Offset in the name buffer */ - for (wc = 1, s = 0; s < 13; s++) { /* Process all characters in the entry */ - uc = ld_word(dir + LfnOfs[s]); /* Pick an LFN character */ - if (wc != 0) { - if (i >= FF_MAX_LFN + 1) return 0; /* Buffer overflow? */ - lfnbuf[i++] = wc = uc; /* Store it */ + for (pchr = 1, di = 0; di < 13; di++) { /* Process all characters in the entry */ + chr = ld_16(dir + LfnOfs[di]); /* Pick a character from the entry */ + if (pchr != 0) { + if (ni >= FF_MAX_LFN + 1) return 0; /* Buffer overflow? */ + lfnbuf[ni++] = pchr = chr; /* Store it */ } else { - if (uc != 0xFFFF) return 0; /* Check filler */ + if (chr != 0xFFFF) return 0; /* Check filler */ } } - if (dir[LDIR_Ord] & LLEF && wc != 0) { /* Put terminator if it is the last LFN part and not terminated */ - if (i >= FF_MAX_LFN + 1) return 0; /* Buffer overflow? */ - lfnbuf[i] = 0; + if (dir[LDIR_Ord] & LLEF && pchr != 0) { /* Put terminator if it is the last LFN part and not terminated */ + if (ni >= FF_MAX_LFN + 1) return 0; /* Buffer overflow? */ + lfnbuf[ni] = 0; } return 1; /* The part of LFN is valid */ @@ -1932,24 +1981,24 @@ static void put_lfn ( BYTE sum /* Checksum of the corresponding SFN */ ) { - UINT i, s; - WCHAR wc; + UINT ni, di; + WCHAR chr; dir[LDIR_Chksum] = sum; /* Set checksum */ - dir[LDIR_Attr] = AM_LFN; /* Set attribute. LFN entry */ + dir[LDIR_Attr] = AM_LFN; /* Set attribute */ dir[LDIR_Type] = 0; - st_word(dir + LDIR_FstClusLO, 0); - - i = (ord - 1) * 13; /* Get offset in the LFN working buffer */ - s = wc = 0; - do { - if (wc != 0xFFFF) wc = lfn[i++]; /* Get an effective character */ - st_word(dir + LfnOfs[s], wc); /* Put it */ - if (wc == 0) wc = 0xFFFF; /* Padding characters for following items */ - } while (++s < 13); - if (wc == 0xFFFF || !lfn[i]) ord |= LLEF; /* Last LFN part is the start of LFN sequence */ - dir[LDIR_Ord] = ord; /* Set the LFN order */ + st_16(dir + LDIR_FstClusLO, 0); + + ni = (UINT)(ord - 1) * 13; /* Offset in the name */ + di = chr = 0; + do { /* Fill the directory entry */ + if (chr != 0xFFFF) chr = lfn[ni++]; /* Get an effective character */ + st_16(dir + LfnOfs[di], chr); /* Set it */ + if (chr == 0) chr = 0xFFFF; /* Padding characters after the terminator */ + } while (++di < 13); + if (chr == 0xFFFF || !lfn[ni]) ord |= LLEF; /* Last LFN part is the start of an enrty set */ + dir[LDIR_Ord] = ord; /* Set order in the entry set */ } #endif /* !FF_FS_READONLY */ @@ -1966,31 +2015,31 @@ static void gen_numname ( BYTE* dst, /* Pointer to the buffer to store numbered SFN */ const BYTE* src, /* Pointer to SFN in directory form */ const WCHAR* lfn, /* Pointer to LFN */ - UINT seq /* Sequence number */ + WORD seq /* Sequence number */ ) { BYTE ns[8], c; UINT i, j; WCHAR wc; - DWORD sreg; + DWORD crc_sreg; memcpy(dst, src, 11); /* Prepare the SFN to be modified */ if (seq > 5) { /* In case of many collisions, generate a hash number instead of sequential number */ - sreg = seq; - while (*lfn) { /* Create a CRC as hash value */ + crc_sreg = seq; + while (*lfn) { /* Create a CRC value as a hash of LFN */ wc = *lfn++; for (i = 0; i < 16; i++) { - sreg = (sreg << 1) + (wc & 1); + crc_sreg = (crc_sreg << 1) + (wc & 1); wc >>= 1; - if (sreg & 0x10000) sreg ^= 0x11021; + if (crc_sreg & 0x10000) crc_sreg ^= 0x11021; } } - seq = (UINT)sreg; + seq = (WORD)crc_sreg; } - /* Make suffix (~ + hexdecimal) */ + /* Make suffix (~ + 4-digit hexadecimal) */ i = 7; do { c = (BYTE)((seq % 16) + '0'); seq /= 16; @@ -2049,7 +2098,7 @@ static WORD xdir_sum ( /* Get checksum of the directoly entry block */ WORD sum; - szblk = (dir[XDIR_NumSec] + 1) * SZDIRE; /* Number of bytes of the entry block */ + szblk = ((UINT)dir[XDIR_NumSec] + 1) * SZDIRE; /* Number of bytes of the entry block */ for (i = sum = 0; i < szblk; i++) { if (i == XDIR_SetSum) { /* Skip 2-byte sum field */ i++; @@ -2092,26 +2141,26 @@ static DWORD xsum32 ( /* Returns 32-bit checksum */ -/*-----------------------------------*/ -/* exFAT: Get a directry entry block */ -/*-----------------------------------*/ +/*------------------------------------*/ +/* exFAT: Get a directory entry block */ +/*------------------------------------*/ static FRESULT load_xdir ( /* FR_INT_ERR: invalid entry block */ - DIR* dp /* Reading direcotry object pointing top of the entry block to load */ + DIR* dp /* Reading directory object pointing top of the entry block to load */ ) { FRESULT res; UINT i, sz_ent; - BYTE *dirb = dp->obj.fs->dirbuf; /* Pointer to the on-memory direcotry entry block 85+C0+C1s */ + BYTE *dirb = dp->obj.fs->dirbuf; /* Pointer to the on-memory directory entry block 85+C0+C1s */ - /* Load file directory entry */ + /* Load file-directory entry */ res = move_window(dp->obj.fs, dp->sect); if (res != FR_OK) return res; - if (dp->dir[XDIR_Type] != ET_FILEDIR) return FR_INT_ERR; /* Invalid order */ + if (dp->dir[XDIR_Type] != ET_FILEDIR) return FR_INT_ERR; /* Invalid order? */ memcpy(dirb + 0 * SZDIRE, dp->dir, SZDIRE); - sz_ent = (dirb[XDIR_NumSec] + 1) * SZDIRE; - if (sz_ent < 3 * SZDIRE || sz_ent > 19 * SZDIRE) return FR_INT_ERR; + sz_ent = ((UINT)dirb[XDIR_NumSec] + 1) * SZDIRE; /* Size of this entry block */ + if (sz_ent < 3 * SZDIRE || sz_ent > 19 * SZDIRE) return FR_INT_ERR; /* Invalid block size? */ /* Load stream extension entry */ res = dir_next(dp, 0); @@ -2119,9 +2168,9 @@ static FRESULT load_xdir ( /* FR_INT_ERR: invalid entry block */ if (res != FR_OK) return res; res = move_window(dp->obj.fs, dp->sect); if (res != FR_OK) return res; - if (dp->dir[XDIR_Type] != ET_STREAM) return FR_INT_ERR; /* Invalid order */ + if (dp->dir[XDIR_Type] != ET_STREAM) return FR_INT_ERR; /* Invalid order? */ memcpy(dirb + 1 * SZDIRE, dp->dir, SZDIRE); - if (MAXDIRB(dirb[XDIR_NumName]) > sz_ent) return FR_INT_ERR; + if (MAXDIRB(dirb[XDIR_NumName]) > sz_ent) return FR_INT_ERR; /* Invalid block size for the name? */ /* Load file name entries */ i = 2 * SZDIRE; /* Name offset to load */ @@ -2131,14 +2180,15 @@ static FRESULT load_xdir ( /* FR_INT_ERR: invalid entry block */ if (res != FR_OK) return res; res = move_window(dp->obj.fs, dp->sect); if (res != FR_OK) return res; - if (dp->dir[XDIR_Type] != ET_FILENAME) return FR_INT_ERR; /* Invalid order */ - if (i < MAXDIRB(FF_MAX_LFN)) memcpy(dirb + i, dp->dir, SZDIRE); + if (dp->dir[XDIR_Type] != ET_FILENAME) return FR_INT_ERR; /* Invalid order? */ + if (i < MAXDIRB(FF_MAX_LFN)) memcpy(dirb + i, dp->dir, SZDIRE); /* Load name entries only if the object is accessible */ } while ((i += SZDIRE) < sz_ent); /* Sanity check (do it for only accessible object) */ if (i <= MAXDIRB(FF_MAX_LFN)) { - if (xdir_sum(dirb) != ld_word(dirb + XDIR_SetSum)) return FR_INT_ERR; + if (xdir_sum(dirb) != ld_16(dirb + XDIR_SetSum)) return FR_INT_ERR; } + return FR_OK; } @@ -2152,10 +2202,10 @@ static void init_alloc_info ( FFOBJID* obj /* Object allocation information to be initialized */ ) { - obj->sclust = ld_dword(fs->dirbuf + XDIR_FstClus); /* Start cluster */ - obj->objsize = ld_qword(fs->dirbuf + XDIR_FileSize); /* Size */ - obj->stat = fs->dirbuf[XDIR_GenFlags] & 2; /* Allocation status */ - obj->n_frag = 0; /* No last fragment info */ + obj->sclust = ld_32(fs->dirbuf + XDIR_FstClus); /* Start cluster */ + obj->objsize = ld_64(fs->dirbuf + XDIR_FileSize); /* Size */ + obj->stat = fs->dirbuf[XDIR_GenFlags] & 2; /* Allocation status */ + obj->n_frag = 0; /* No last fragment info */ } @@ -2166,7 +2216,7 @@ static void init_alloc_info ( /*------------------------------------------------*/ static FRESULT load_obj_xdir ( - DIR* dp, /* Blank directory object to be used to access containing direcotry */ + DIR* dp, /* Blank directory object to be used to access containing directory */ const FFOBJID* obj /* Object with its containing directory information */ ) { @@ -2195,27 +2245,29 @@ static FRESULT load_obj_xdir ( /*----------------------------------------*/ static FRESULT store_xdir ( - DIR* dp /* Pointer to the direcotry object */ + DIR* dp /* Pointer to the directory object */ ) { FRESULT res; UINT nent; - BYTE *dirb = dp->obj.fs->dirbuf; /* Pointer to the direcotry entry block 85+C0+C1s */ + BYTE *dirb = dp->obj.fs->dirbuf; /* Pointer to the entry set 85+C0+C1s */ + - /* Create set sum */ - st_word(dirb + XDIR_SetSum, xdir_sum(dirb)); - nent = dirb[XDIR_NumSec] + 1; + st_16(dirb + XDIR_SetSum, xdir_sum(dirb)); /* Create check sum */ - /* Store the direcotry entry block to the directory */ - res = dir_sdi(dp, dp->blk_ofs); + /* Store the entry set to the directory */ + nent = dirb[XDIR_NumSec] + 1; /* Number of entries */ + res = dir_sdi(dp, dp->blk_ofs); /* Top of the entry set */ while (res == FR_OK) { + /* Set an entry to the directory */ res = move_window(dp->obj.fs, dp->sect); if (res != FR_OK) break; memcpy(dp->dir, dirb, SZDIRE); dp->obj.fs->wflag = 1; - if (--nent == 0) break; + + if (--nent == 0) break; /* All done? */ dirb += SZDIRE; - res = dir_next(dp, 0); + res = dir_next(dp, 0); /* Next entry */ } return (res == FR_OK || res == FR_DISK_ERR) ? res : FR_INT_ERR; } @@ -2223,40 +2275,40 @@ static FRESULT store_xdir ( /*-------------------------------------------*/ -/* exFAT: Create a new directory enrty block */ +/* exFAT: Create a new directory entry block */ /*-------------------------------------------*/ static void create_xdir ( - BYTE* dirb, /* Pointer to the direcotry entry block buffer */ + BYTE* dirb, /* Pointer to the directory entry block buffer */ const WCHAR* lfn /* Pointer to the object name */ ) { UINT i; - BYTE nc1, nlen; - WCHAR wc; + BYTE n_c1, nlen; + WCHAR chr; - /* Create file-directory and stream-extension entry */ + /* Create file-directory and stream-extension entry (1st and 2nd entry) */ memset(dirb, 0, 2 * SZDIRE); dirb[0 * SZDIRE + XDIR_Type] = ET_FILEDIR; dirb[1 * SZDIRE + XDIR_Type] = ET_STREAM; - /* Create file-name entries */ - i = SZDIRE * 2; /* Top of file_name entries */ - nlen = nc1 = 0; wc = 1; + /* Create file name entries (3rd enrty and follows) */ + i = SZDIRE * 2; /* Top of file name entries */ + nlen = n_c1 = 0; chr = 1; do { dirb[i++] = ET_FILENAME; dirb[i++] = 0; do { /* Fill name field */ - if (wc != 0 && (wc = lfn[nlen]) != 0) nlen++; /* Get a character if exist */ - st_word(dirb + i, wc); /* Store it */ + if (chr != 0 && (chr = lfn[nlen]) != 0) nlen++; /* Get a character if exist */ + st_16(dirb + i, chr); /* Store it */ i += 2; } while (i % SZDIRE != 0); - nc1++; - } while (lfn[nlen]); /* Fill next entry if any char follows */ + n_c1++; + } while (lfn[nlen]); /* Fill next C1 entry if any char follows */ dirb[XDIR_NumName] = nlen; /* Set name length */ - dirb[XDIR_NumSec] = 1 + nc1; /* Set secondary count (C0 + C1s) */ - st_word(dirb + XDIR_NameHash, xname_sum(lfn)); /* Set name hash */ + dirb[XDIR_NumSec] = 1 + n_c1; /* Set secondary count (C0 + C1s) */ + st_16(dirb + XDIR_NameHash, xname_sum(lfn)); /* Set name hash */ } #endif /* !FF_FS_READONLY */ @@ -2373,10 +2425,10 @@ static FRESULT dir_find ( /* FR_OK(0):succeeded, !=0:error */ #if FF_MAX_LFN < 255 if (fs->dirbuf[XDIR_NumName] > FF_MAX_LFN) continue; /* Skip comparison if inaccessible object name */ #endif - if (ld_word(fs->dirbuf + XDIR_NameHash) != hash) continue; /* Skip comparison if hash mismatched */ + if (ld_16(fs->dirbuf + XDIR_NameHash) != hash) continue; /* Skip comparison if hash mismatched */ for (nc = fs->dirbuf[XDIR_NumName], di = SZDIRE * 2, ni = 0; nc; nc--, di += 2, ni++) { /* Compare the name */ if ((di % SZDIRE) == 0) di += 2; - if (ff_wtoupper(ld_word(fs->dirbuf + di)) != ff_wtoupper(fs->lfnbuf[ni])) break; + if (ff_wtoupper(ld_16(fs->dirbuf + di)) != ff_wtoupper(fs->lfnbuf[ni])) break; } if (nc == 0 && !fs->lfnbuf[ni]) break; /* Name matched? */ } @@ -2391,26 +2443,27 @@ static FRESULT dir_find ( /* FR_OK(0):succeeded, !=0:error */ res = move_window(fs, dp->sect); if (res != FR_OK) break; c = dp->dir[DIR_Name]; - if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */ + if (c == 0) { res = FR_NO_FILE; break; } /* Reached end of directory table */ #if FF_USE_LFN /* LFN configuration */ dp->obj.attr = a = dp->dir[DIR_Attr] & AM_MASK; if (c == DDEM || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */ ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */ } else { - if (a == AM_LFN) { /* An LFN entry is found */ + if (a == AM_LFN) { /* Is it an LFN entry? */ if (!(dp->fn[NSFLAG] & NS_NOLFN)) { - if (c & LLEF) { /* Is it start of LFN sequence? */ - sum = dp->dir[LDIR_Chksum]; - c &= (BYTE)~LLEF; ord = c; /* LFN start order */ - dp->blk_ofs = dp->dptr; /* Start offset of LFN */ + if (c & LLEF) { /* Is it start of an entry set? */ + c &= (BYTE)~LLEF; + ord = c; /* Number of LFN entries */ + dp->blk_ofs = dp->dptr; /* Start offset of LFN */ + sum = dp->dir[LDIR_Chksum]; /* Sum of the SFN */ } /* Check validity of the LFN entry and compare it with given name */ ord = (c == ord && sum == dp->dir[LDIR_Chksum] && cmp_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF; } - } else { /* An SFN entry is found */ + } else { /* SFN entry */ if (ord == 0 && sum == sum_sfn(dp->dir)) break; /* LFN matched? */ if (!(dp->fn[NSFLAG] & NS_LOSS) && !memcmp(dp->dir, dp->fn, 11)) break; /* SFN matched? */ - ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */ + ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Not matched, reset LFN sequence */ } } #else /* Non LFN configuration */ @@ -2464,8 +2517,8 @@ static FRESULT dir_register ( /* FR_OK:succeeded, FR_DENIED:no free entry or too res = load_obj_xdir(&dj, &dp->obj); /* Load the object status */ if (res != FR_OK) return res; dp->obj.objsize += (DWORD)fs->csize * SS(fs); /* Increase the directory size by cluster size */ - st_qword(fs->dirbuf + XDIR_FileSize, dp->obj.objsize); - st_qword(fs->dirbuf + XDIR_ValidFileSize, dp->obj.objsize); + st_64(fs->dirbuf + XDIR_FileSize, dp->obj.objsize); + st_64(fs->dirbuf + XDIR_ValidFileSize, dp->obj.objsize); fs->dirbuf[XDIR_GenFlags] = dp->obj.stat | 1; /* Update the allocation status */ res = store_xdir(&dj); /* Store the object status */ if (res != FR_OK) return res; @@ -2481,7 +2534,7 @@ static FRESULT dir_register ( /* FR_OK:succeeded, FR_DENIED:no free entry or too if (sn[NSFLAG] & NS_LOSS) { /* When LFN is out of 8.3 format, generate a numbered name */ dp->fn[NSFLAG] = NS_NOLFN; /* Find only SFN */ for (n = 1; n < 100; n++) { - gen_numname(dp->fn, sn, fs->lfnbuf, n); /* Generate a numbered name */ + gen_numname(dp->fn, sn, fs->lfnbuf, (WORD)n); /* Generate a numbered name */ res = dir_find(dp); /* Check if the name collides with existing SFN */ if (res != FR_OK) break; } @@ -2599,7 +2652,6 @@ static void get_fileinfo ( #endif - fno->fname[0] = 0; /* Invaidate file info */ if (dp->sect == 0) return; /* Exit if read pointer has reached end of directory */ #if FF_USE_LFN /* LFN configuration */ @@ -2610,26 +2662,34 @@ static void get_fileinfo ( si = SZDIRE * 2; di = 0; /* 1st C1 entry in the entry block */ hs = 0; while (nc < fs->dirbuf[XDIR_NumName]) { - if (si >= MAXDIRB(FF_MAX_LFN)) { di = 0; break; } /* Truncated directory block? */ - if ((si % SZDIRE) == 0) si += 2; /* Skip entry type field */ - wc = ld_word(fs->dirbuf + si); si += 2; nc++; /* Get a character */ - if (hs == 0 && IsSurrogate(wc)) { /* Is it a surrogate? */ - hs = wc; continue; /* Get low surrogate */ + if (si >= MAXDIRB(FF_MAX_LFN)) { /* Truncated directory block? */ + di = 0; break; + } + if ((si % SZDIRE) == 0) si += 2; /* Skip entry type field */ + wc = ld_16(fs->dirbuf + si); si += 2; nc++; /* Get a character */ + if (hs == 0 && IsSurrogate(wc)) { /* Is it a surrogate? */ + hs = wc; continue; /* Get low surrogate */ } nw = put_utf((DWORD)hs << 16 | wc, &fno->fname[di], FF_LFN_BUF - di); /* Store it in API encoding */ - if (nw == 0) { di = 0; break; } /* Buffer overflow or wrong char? */ + if (nw == 0) { /* Buffer overflow or wrong char? */ + di = 0; break; + } di += nw; hs = 0; } if (hs != 0) di = 0; /* Broken surrogate pair? */ - if (di == 0) fno->fname[di++] = '?'; /* Inaccessible object name? */ + if (di == 0) fno->fname[di++] = '\?'; /* Inaccessible object name? */ fno->fname[di] = 0; /* Terminate the name */ fno->altname[0] = 0; /* exFAT does not support SFN */ - fno->fattrib = fs->dirbuf[XDIR_Attr] & AM_MASKX; /* Attribute */ - fno->fsize = (fno->fattrib & AM_DIR) ? 0 : ld_qword(fs->dirbuf + XDIR_FileSize); /* Size */ - fno->ftime = ld_word(fs->dirbuf + XDIR_ModTime + 0); /* Time */ - fno->fdate = ld_word(fs->dirbuf + XDIR_ModTime + 2); /* Date */ + fno->fattrib = fs->dirbuf[XDIR_Attr] & AM_MASKX; /* Attribute */ + fno->fsize = (fno->fattrib & AM_DIR) ? 0 : ld_64(fs->dirbuf + XDIR_FileSize); /* Size */ + fno->ftime = ld_16(fs->dirbuf + XDIR_ModTime + 0); /* Last modified time */ + fno->fdate = ld_16(fs->dirbuf + XDIR_ModTime + 2); /* Last modified date */ +#if FF_FS_CRTIME + fno->crtime = ld_16(fs->dirbuf + XDIR_CrtTime + 0); /* Created time */ + fno->crdate = ld_16(fs->dirbuf + XDIR_CrtTime + 2); /* Created date */ +#endif return; } else #endif @@ -2643,7 +2703,9 @@ static void get_fileinfo ( hs = wc; continue; /* Get low surrogate */ } nw = put_utf((DWORD)hs << 16 | wc, &fno->fname[di], FF_LFN_BUF - di); /* Store it in API encoding */ - if (nw == 0) { di = 0; break; } /* Buffer overflow or wrong char? */ + if (nw == 0) { /* Buffer overflow or wrong char? */ + di = 0; break; + } di += nw; hs = 0; } @@ -2663,9 +2725,13 @@ static void get_fileinfo ( wc = wc << 8 | dp->dir[si++]; } wc = ff_oem2uni(wc, CODEPAGE); /* ANSI/OEM -> Unicode */ - if (wc == 0) { di = 0; break; } /* Wrong char in the current code page? */ + if (wc == 0) { /* Wrong char in the current code page? */ + di = 0; break; + } nw = put_utf(wc, &fno->altname[di], FF_SFN_BUF - di); /* Store it in API encoding */ - if (nw == 0) { di = 0; break; } /* Buffer overflow? */ + if (nw == 0) { /* Buffer overflow? */ + di = 0; break; + } di += nw; #else /* ANSI/OEM output */ fno->altname[di++] = (TCHAR)wc; /* Store it without any conversion */ @@ -2674,8 +2740,8 @@ static void get_fileinfo ( fno->altname[di] = 0; /* Terminate the SFN (null string means SFN is invalid) */ if (fno->fname[0] == 0) { /* If LFN is invalid, altname[] needs to be copied to fname[] */ - if (di == 0) { /* If LFN and SFN both are invalid, this object is inaccesible */ - fno->fname[di++] = '?'; + if (di == 0) { /* If LFN and SFN both are invalid, this object is inaccessible */ + fno->fname[di++] = '\?'; } else { for (si = di = 0, lcf = NS_BODY; fno->altname[si]; si++, di++) { /* Copy altname[] to fname[] with case information */ wc = (WCHAR)fno->altname[si]; @@ -2700,10 +2766,14 @@ static void get_fileinfo ( fno->fname[di] = 0; /* Terminate the SFN */ #endif - fno->fattrib = dp->dir[DIR_Attr] & AM_MASK; /* Attribute */ - fno->fsize = ld_dword(dp->dir + DIR_FileSize); /* Size */ - fno->ftime = ld_word(dp->dir + DIR_ModTime + 0); /* Time */ - fno->fdate = ld_word(dp->dir + DIR_ModTime + 2); /* Date */ + fno->fattrib = dp->dir[DIR_Attr] & AM_MASK; /* Attribute */ + fno->fsize = ld_32(dp->dir + DIR_FileSize); /* Size */ + fno->ftime = ld_16(dp->dir + DIR_ModTime + 0); /* Last modified time */ + fno->fdate = ld_16(dp->dir + DIR_ModTime + 2); /* Last Modified date */ +#if FF_FS_CRTIME + fno->crtime = ld_16(dp->dir + DIR_CrtTime + 0); /* Created time */ + fno->crdate = ld_16(dp->dir + DIR_CrtTime + 2); /* Created date */ +#endif } #endif /* FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2 */ @@ -2734,9 +2804,9 @@ static DWORD get_achar ( /* Get a character and advance ptr */ chr = (BYTE)*(*ptr)++; /* Get a byte */ if (IsLower(chr)) chr -= 0x20; /* To upper ASCII char */ #if FF_CODE_PAGE == 0 - if (ExCvt && chr >= 0x80) chr = ExCvt[chr - 0x80]; /* To upper SBCS extended char */ + if (ExCvt && chr >= 0x80) chr = ExCvt[chr - 0x80]; /* To upper (SBCS extended char) */ #elif FF_CODE_PAGE < 900 - if (chr >= 0x80) chr = ExCvt[chr - 0x80]; /* To upper SBCS extended char */ + if (chr >= 0x80) chr = ExCvt[chr - 0x80]; /* To upper (SBCS extended char) */ #endif #if FF_CODE_PAGE == 0 || FF_CODE_PAGE >= 900 if (dbc_1st((BYTE)chr)) { /* Get DBC 2nd byte if needed */ @@ -2756,7 +2826,8 @@ static int pattern_match ( /* 0:mismatched, 1:matched */ UINT recur /* Recursion count */ ) { - const TCHAR *pptr, *nptr; + const TCHAR *pptr; + const TCHAR *nptr; DWORD pchr, nchr; UINT sk; @@ -2770,12 +2841,16 @@ static int pattern_match ( /* 0:mismatched, 1:matched */ do { pptr = pat; nptr = nam; /* Top of pattern and name to match */ for (;;) { - if (*pptr == '?' || *pptr == '*') { /* Wildcard term? */ + if (*pptr == '\?' || *pptr == '*') { /* Wildcard term? */ if (recur == 0) return 0; /* Too many wildcard terms? */ sk = 0; do { /* Analyze the wildcard term */ - if (*pptr++ == '?') sk++; else sk |= 0x100; - } while (*pptr == '?' || *pptr == '*'); + if (*pptr++ == '\?') { + sk++; + } else { + sk |= 0x100; + } + } while (*pptr == '\?' || *pptr == '*'); if (pattern_match(pptr, nptr, sk, recur - 1)) return 1; /* Test new branch (recursive call) */ nchr = *nptr; break; /* Branch mismatched */ } @@ -2805,10 +2880,11 @@ static FRESULT create_name ( /* FR_OK: successful, FR_INVALID_NAME: could not cr { #if FF_USE_LFN /* LFN configuration */ BYTE b, cf; - WCHAR wc, *lfn; + WCHAR wc; + WCHAR *lfn; + const TCHAR* p; DWORD uc; UINT i, ni, si, di; - const TCHAR *p; /* Create LFN into LFN working buffer */ @@ -2930,7 +3006,8 @@ static FRESULT create_name ( /* FR_OK: successful, FR_INVALID_NAME: could not cr #else /* FF_USE_LFN : Non-LFN configuration */ - BYTE c, d, *sfn; + BYTE c, d; + BYTE *sfn; UINT ni, si, i; const char *p; @@ -3030,7 +3107,7 @@ static FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */ dp->obj.c_ofs = fs->cdc_ofs; res = load_obj_xdir(&dj, &dp->obj); if (res != FR_OK) return res; - dp->obj.objsize = ld_dword(fs->dirbuf + XDIR_FileSize); + dp->obj.objsize = ld_32(fs->dirbuf + XDIR_FileSize); dp->obj.stat = fs->dirbuf[XDIR_GenFlags] & 2; } #endif @@ -3091,69 +3168,66 @@ static int get_ldnumber ( /* Returns logical drive number (-1:invalid drive numb const TCHAR** path /* Pointer to pointer to the path name */ ) { - const TCHAR *tp, *tt; - TCHAR tc; + const TCHAR *tp; + const TCHAR *tt; + TCHAR chr; int i; - int vol = -1; #if FF_STR_VOLUME_ID /* Find string volume ID */ - const char *sp; - char c; + const char *vsp; + char vchr; #endif tt = tp = *path; - if (!tp) return vol; /* Invalid path name? */ - do tc = *tt++; while (!IsTerminator(tc) && tc != ':'); /* Find a colon in the path */ + if (!tp) return -1; /* Invalid path name? */ + do { /* Find a colon in the path */ + chr = *tt++; + } while (!IsTerminator(chr) && chr != ':'); - if (tc == ':') { /* DOS/Windows style volume ID? */ + if (chr == ':') { /* Is there a DOS/Windows style volume ID? */ i = FF_VOLUMES; - if (IsDigit(*tp) && tp + 2 == tt) { /* Is there a numeric volume ID + colon? */ - i = (int)*tp - '0'; /* Get the LD number */ + if (IsDigit(*tp) && tp + 2 == tt) { /* Is it a numeric volume ID + colon? */ + i = (int)*tp - '0'; /* Get the logical drive number */ } -#if FF_STR_VOLUME_ID == 1 /* Arbitrary string is enabled */ +#if FF_STR_VOLUME_ID == 1 /* Arbitrary string volume ID is enabled */ else { - i = 0; + i = 0; /* Find volume ID string in the preconfigured table */ do { - sp = VolumeStr[i]; tp = *path; /* This string volume ID and path name */ - do { /* Compare the volume ID with path name */ - c = *sp++; tc = *tp++; - if (IsLower(c)) c -= 0x20; - if (IsLower(tc)) tc -= 0x20; - } while (c && (TCHAR)c == tc); - } while ((c || tp != tt) && ++i < FF_VOLUMES); /* Repeat for each id until pattern match */ + vsp = VolumeStr[i]; tp = *path; /* Preconfigured string and path name to test */ + do { /* Compare the volume ID with path name in case-insensitive */ + vchr = *vsp++; chr = *tp++; + if (IsLower(vchr)) vchr -= 0x20; + if (IsLower(chr)) chr -= 0x20; + } while (vchr && (TCHAR)vchr == chr); + } while ((vchr || tp != tt) && ++i < FF_VOLUMES); /* Repeat for each id until pattern match */ } #endif - if (i < FF_VOLUMES) { /* If a volume ID is found, get the drive number and strip it */ - vol = i; /* Drive number */ - *path = tt; /* Snip the drive prefix off */ - } - return vol; + if (i >= FF_VOLUMES) return -1; /* Not found or invalid volume ID */ + *path = tt; /* Snip the drive prefix off */ + return i; /* Return the found drive number */ } #if FF_STR_VOLUME_ID == 2 /* Unix style volume ID is enabled */ if (*tp == '/') { /* Is there a volume ID? */ while (*(tp + 1) == '/') tp++; /* Skip duplicated separator */ i = 0; do { - tt = tp; sp = VolumeStr[i]; /* Path name and this string volume ID */ - do { /* Compare the volume ID with path name */ - c = *sp++; tc = *(++tt); - if (IsLower(c)) c -= 0x20; - if (IsLower(tc)) tc -= 0x20; - } while (c && (TCHAR)c == tc); - } while ((c || (tc != '/' && !IsTerminator(tc))) && ++i < FF_VOLUMES); /* Repeat for each ID until pattern match */ - if (i < FF_VOLUMES) { /* If a volume ID is found, get the drive number and strip it */ - vol = i; /* Drive number */ - *path = tt; /* Snip the drive prefix off */ - } - return vol; - } -#endif - /* No drive prefix is found */ + vsp = VolumeStr[i]; tt = tp; /* Preconfigured string and path name to test */ + do { /* Compare the volume ID with path name in case-insensitive */ + vchr = *vsp++; chr = *(++tt); + if (IsLower(vchr)) vchr -= 0x20; + if (IsLower(chr)) chr -= 0x20; + } while (vchr && (TCHAR)vchr == chr); + } while ((vchr || (chr != '/' && !IsTerminator(chr))) && ++i < FF_VOLUMES); /* Repeat for each ID until pattern match */ + if (i >= FF_VOLUMES) return -1; /* Not found (invalid volume ID) */ + *path = tt; /* Snip the node name off */ + return i; /* Return the found drive number */ + } +#endif + /* No drive prefix */ #if FF_FS_RPATH != 0 - vol = CurrVol; /* Default drive is current drive */ + return (int)CurrVol; /* Default drive is current drive */ #else - vol = 0; /* Default drive is 0 */ + return 0; /* Default drive is 0 */ #endif - return vol; /* Return the default drive */ } @@ -3190,27 +3264,29 @@ static int test_gpt_header ( /* 0:Invalid, 1:Valid */ ) { UINT i; - DWORD bcc; + DWORD bcc, hlen; - if (memcmp(gpth + GPTH_Sign, "EFI PART" "\0\0\1\0" "\x5C\0\0", 16)) return 0; /* Check sign, version (1.0) and length (92) */ - for (i = 0, bcc = 0xFFFFFFFF; i < 92; i++) { /* Check header BCC */ + if (memcmp(gpth + GPTH_Sign, "EFI PART" "\0\0\1", 12)) return 0; /* Check signature and version (1.0) */ + hlen = ld_32(gpth + GPTH_Size); /* Check header size */ + if (hlen < 92 || hlen > FF_MIN_SS) return 0; + for (i = 0, bcc = 0xFFFFFFFF; i < hlen; i++) { /* Check header BCC */ bcc = crc32(bcc, i - GPTH_Bcc < 4 ? 0 : gpth[i]); } - if (~bcc != ld_dword(gpth + GPTH_Bcc)) return 0; - if (ld_dword(gpth + GPTH_PteSize) != SZ_GPTE) return 0; /* Table entry size (must be SZ_GPTE bytes) */ - if (ld_dword(gpth + GPTH_PtNum) > 128) return 0; /* Table size (must be 128 entries or less) */ + if (~bcc != ld_32(gpth + GPTH_Bcc)) return 0; + if (ld_32(gpth + GPTH_PteSize) != SZ_GPTE) return 0; /* Table entry size (must be SZ_GPTE bytes) */ + if (ld_32(gpth + GPTH_PtNum) > 128) return 0; /* Table size (must be 128 entries or less) */ return 1; } #if !FF_FS_READONLY && FF_USE_MKFS -/* Generate random value */ -static DWORD make_rand ( - DWORD seed, /* Seed value */ - BYTE* buff, /* Output buffer */ - UINT n /* Data length */ +/* Generate a random value */ +static DWORD make_rand ( /* Returns a seed value for next */ + DWORD seed, /* Seed value */ + BYTE *buff, /* Output buffer */ + UINT n /* Data length */ ) { UINT r; @@ -3246,7 +3322,7 @@ static UINT check_fs ( /* 0:FAT/FAT32 VBR, 1:exFAT VBR, 2:Not FAT and valid BS, fs->wflag = 0; fs->winsect = (LBA_t)0 - 1; /* Invaidate window */ if (move_window(fs, sect) != FR_OK) return 4; /* Load the boot sector */ - sign = ld_word(fs->win + BS_55AA); + sign = ld_16(fs->win + BS_55AA); #if FF_FS_EXFAT if (sign == 0xAA55 && !memcmp(fs->win + BS_JmpBoot, "\xEB\x76\x90" "EXFAT ", 11)) return 1; /* It is an exFAT VBR */ #endif @@ -3255,20 +3331,20 @@ static UINT check_fs ( /* 0:FAT/FAT32 VBR, 1:exFAT VBR, 2:Not FAT and valid BS, if (sign == 0xAA55 && !memcmp(fs->win + BS_FilSysType32, "FAT32 ", 8)) { return 0; /* It is an FAT32 VBR */ } - /* FAT volumes formatted with early MS-DOS lack BS_55AA and BS_FilSysType, so FAT VBR needs to be identified without them. */ - w = ld_word(fs->win + BPB_BytsPerSec); + /* FAT volumes created in the early MS-DOS era lack BS_55AA and BS_FilSysType, so FAT VBR needs to be identified without them. */ + w = ld_16(fs->win + BPB_BytsPerSec); b = fs->win[BPB_SecPerClus]; if ((w & (w - 1)) == 0 && w >= FF_MIN_SS && w <= FF_MAX_SS /* Properness of sector size (512-4096 and 2^n) */ && b != 0 && (b & (b - 1)) == 0 /* Properness of cluster size (2^n) */ - && ld_word(fs->win + BPB_RsvdSecCnt) != 0 /* Properness of reserved sectors (MNBZ) */ - && (UINT)fs->win[BPB_NumFATs] - 1 <= 1 /* Properness of FATs (1 or 2) */ - && ld_word(fs->win + BPB_RootEntCnt) != 0 /* Properness of root dir entries (MNBZ) */ - && (ld_word(fs->win + BPB_TotSec16) >= 128 || ld_dword(fs->win + BPB_TotSec32) >= 0x10000) /* Properness of volume sectors (>=128) */ - && ld_word(fs->win + BPB_FATSz16) != 0) { /* Properness of FAT size (MNBZ) */ + && ld_16(fs->win + BPB_RsvdSecCnt) != 0 /* Properness of number of reserved sectors (MNBZ) */ + && (UINT)fs->win[BPB_NumFATs] - 1 <= 1 /* Properness of number of FATs (1 or 2) */ + && ld_16(fs->win + BPB_RootEntCnt) != 0 /* Properness of root dir size (MNBZ) */ + && (ld_16(fs->win + BPB_TotSec16) >= 128 || ld_32(fs->win + BPB_TotSec32) >= 0x10000) /* Properness of volume size (>=128) */ + && ld_16(fs->win + BPB_FATSz16) != 0) { /* Properness of FAT size (MNBZ) */ return 0; /* It can be presumed an FAT VBR */ } } - return sign == 0xAA55 ? 2 : 3; /* Not an FAT VBR (valid or invalid BS) */ + return sign == 0xAA55 ? 2 : 3; /* Not an FAT VBR (with valid or invalid BS) */ } @@ -3277,7 +3353,7 @@ static UINT check_fs ( /* 0:FAT/FAT32 VBR, 1:exFAT VBR, 2:Not FAT and valid BS, static UINT find_volume ( /* Returns BS status found in the hosting drive */ FATFS* fs, /* Filesystem object */ - UINT part /* Partition to fined = 0:auto, 1..:forced */ + UINT part /* Partition to fined = 0:find as SFD and partitions, >0:forced partition number */ ) { UINT fmt, i; @@ -3296,14 +3372,14 @@ static UINT find_volume ( /* Returns BS status found in the hosting drive */ if (move_window(fs, 1) != FR_OK) return 4; /* Load GPT header sector (next to MBR) */ if (!test_gpt_header(fs->win)) return 3; /* Check if GPT header is valid */ - n_ent = ld_dword(fs->win + GPTH_PtNum); /* Number of entries */ - pt_lba = ld_qword(fs->win + GPTH_PtOfs); /* Table location */ + n_ent = ld_32(fs->win + GPTH_PtNum); /* Number of entries */ + pt_lba = ld_64(fs->win + GPTH_PtOfs); /* Table location */ for (v_ent = i = 0; i < n_ent; i++) { /* Find FAT partition */ if (move_window(fs, pt_lba + i * SZ_GPTE / SS(fs)) != FR_OK) return 4; /* PT sector */ ofs = i * SZ_GPTE % SS(fs); /* Offset in the sector */ if (!memcmp(fs->win + ofs + GPTE_PtGuid, GUID_MS_Basic, 16)) { /* MS basic data partition? */ v_ent++; - fmt = check_fs(fs, ld_qword(fs->win + ofs + GPTE_FstLba)); /* Load VBR and check status */ + fmt = check_fs(fs, ld_64(fs->win + ofs + GPTE_FstLba)); /* Load VBR and check status */ if (part == 0 && fmt <= 1) return fmt; /* Auto search (valid FAT volume found first) */ if (part != 0 && v_ent == part) return fmt; /* Forced partition order (regardless of it is valid or not) */ } @@ -3313,7 +3389,7 @@ static UINT find_volume ( /* Returns BS status found in the hosting drive */ #endif if (FF_MULTI_PARTITION && part > 4) return 3; /* MBR has 4 partitions max */ for (i = 0; i < 4; i++) { /* Load partition offset in the MBR */ - mbr_pt[i] = ld_dword(fs->win + MBR_Table + i * SZ_PTE + PTE_StLba); + mbr_pt[i] = ld_32(fs->win + MBR_Table + i * SZ_PTE + PTE_StLba); } i = part ? part - 1 : 0; /* Table index to find first */ do { /* Find an FAT volume */ @@ -3332,15 +3408,15 @@ static UINT find_volume ( /* Returns BS status found in the hosting drive */ static FRESULT mount_volume ( /* FR_OK(0): successful, !=0: an error occurred */ const TCHAR** path, /* Pointer to pointer to the path name (drive number) */ FATFS** rfs, /* Pointer to pointer to the found filesystem object */ - BYTE mode /* !=0: Check write protection for write access */ + BYTE mode /* Desiered access mode to check write protection */ ) { int vol; + FATFS *fs; DSTATUS stat; LBA_t bsect; DWORD tsect, sysect, fasize, nclst, szbfat; WORD nrsv; - FATFS *fs; UINT fmt; @@ -3353,7 +3429,7 @@ static FRESULT mount_volume ( /* FR_OK(0): successful, !=0: an error occurred */ fs = FatFs[vol]; /* Get pointer to the filesystem object */ if (!fs) return FR_NOT_ENABLED; /* Is the filesystem object available? */ #if FF_FS_REENTRANT - if (!lock_fs(fs)) return FR_TIMEOUT; /* Lock the volume */ + if (!lock_volume(fs, 1)) return FR_TIMEOUT; /* Lock the volume, and system if needed */ #endif *rfs = fs; /* Return pointer to the filesystem object */ @@ -3371,9 +3447,8 @@ static FRESULT mount_volume ( /* FR_OK(0): successful, !=0: an error occurred */ /* The filesystem object is not valid. */ /* Following code attempts to mount the volume. (find an FAT volume, analyze the BPB and initialize the filesystem object) */ - fs->fs_type = 0; /* Clear the filesystem object */ - fs->pdrv = LD2PD(vol); /* Volume hosting physical drive */ - stat = disk_initialize(fs->pdrv); /* Initialize the physical drive */ + fs->fs_type = 0; /* Invalidate the filesystem object */ + stat = disk_initialize(fs->pdrv); /* Initialize the volume hosting physical drive */ if (stat & STA_NOINIT) { /* Check if the initialization succeeded */ return FR_NOT_READY; /* Failed to initialize due to no medium or hard error */ } @@ -3385,11 +3460,11 @@ static FRESULT mount_volume ( /* FR_OK(0): successful, !=0: an error occurred */ if (SS(fs) > FF_MAX_SS || SS(fs) < FF_MIN_SS || (SS(fs) & (SS(fs) - 1))) return FR_DISK_ERR; #endif - /* Find an FAT volume on the drive */ + /* Find an FAT volume on the hosting drive */ fmt = find_volume(fs, LD2PT(vol)); - if (fmt == 4) return FR_DISK_ERR; /* An error occured in the disk I/O layer */ + if (fmt == 4) return FR_DISK_ERR; /* An error occurred in the disk I/O layer */ if (fmt >= 2) return FR_NO_FILESYSTEM; /* No FAT volume is found */ - bsect = fs->winsect; /* Volume offset */ + bsect = fs->winsect; /* Volume offset in the hosting physical drive */ /* An FAT volume is found (bsect). Following code initializes the filesystem object */ @@ -3401,16 +3476,16 @@ static FRESULT mount_volume ( /* FR_OK(0): successful, !=0: an error occurred */ for (i = BPB_ZeroedEx; i < BPB_ZeroedEx + 53 && fs->win[i] == 0; i++) ; /* Check zero filler */ if (i < BPB_ZeroedEx + 53) return FR_NO_FILESYSTEM; - if (ld_word(fs->win + BPB_FSVerEx) != 0x100) return FR_NO_FILESYSTEM; /* Check exFAT version (must be version 1.0) */ + if (ld_16(fs->win + BPB_FSVerEx) != 0x100) return FR_NO_FILESYSTEM; /* Check exFAT version (must be version 1.0) */ if (1 << fs->win[BPB_BytsPerSecEx] != SS(fs)) { /* (BPB_BytsPerSecEx must be equal to the physical sector size) */ return FR_NO_FILESYSTEM; } - maxlba = ld_qword(fs->win + BPB_TotSecEx) + bsect; /* Last LBA of the volume + 1 */ + maxlba = ld_64(fs->win + BPB_TotSecEx) + bsect; /* Last LBA of the volume + 1 */ if (!FF_LBA64 && maxlba >= 0x100000000) return FR_NO_FILESYSTEM; /* (It cannot be accessed in 32-bit LBA) */ - fs->fsize = ld_dword(fs->win + BPB_FatSzEx); /* Number of sectors per FAT */ + fs->fsize = ld_32(fs->win + BPB_FatSzEx); /* Number of sectors per FAT */ fs->n_fats = fs->win[BPB_NumFATsEx]; /* Number of FATs */ if (fs->n_fats != 1) return FR_NO_FILESYSTEM; /* (Supports only 1 FAT) */ @@ -3418,16 +3493,16 @@ static FRESULT mount_volume ( /* FR_OK(0): successful, !=0: an error occurred */ fs->csize = 1 << fs->win[BPB_SecPerClusEx]; /* Cluster size */ if (fs->csize == 0) return FR_NO_FILESYSTEM; /* (Must be 1..32768 sectors) */ - nclst = ld_dword(fs->win + BPB_NumClusEx); /* Number of clusters */ + nclst = ld_32(fs->win + BPB_NumClusEx); /* Number of clusters */ if (nclst > MAX_EXFAT) return FR_NO_FILESYSTEM; /* (Too many clusters) */ fs->n_fatent = nclst + 2; /* Boundaries and Limits */ fs->volbase = bsect; - fs->database = bsect + ld_dword(fs->win + BPB_DataOfsEx); - fs->fatbase = bsect + ld_dword(fs->win + BPB_FatOfsEx); - if (maxlba < (QWORD)fs->database + nclst * fs->csize) return FR_NO_FILESYSTEM; /* (Volume size must not be smaller than the size requiered) */ - fs->dirbase = ld_dword(fs->win + BPB_RootClusEx); + fs->database = bsect + ld_32(fs->win + BPB_DataOfsEx); + fs->fatbase = bsect + ld_32(fs->win + BPB_FatOfsEx); + if (maxlba < (QWORD)fs->database + nclst * fs->csize) return FR_NO_FILESYSTEM; /* (Volume size must not be smaller than the size required) */ + fs->dirbase = ld_32(fs->win + BPB_RootClusEx); /* Get bitmap location and check if it is contiguous (implementation assumption) */ so = i = 0; @@ -3437,30 +3512,31 @@ static FRESULT mount_volume ( /* FR_OK(0): successful, !=0: an error occurred */ if (move_window(fs, clst2sect(fs, (DWORD)fs->dirbase) + so) != FR_OK) return FR_DISK_ERR; so++; } - if (fs->win[i] == ET_BITMAP) break; /* Is it a bitmap entry? */ + if (fs->win[i] == ET_BITMAP) break; /* Is it a bitmap entry? */ i = (i + SZDIRE) % SS(fs); /* Next entry */ } - bcl = ld_dword(fs->win + i + 20); /* Bitmap cluster */ + bcl = ld_32(fs->win + i + 20); /* Bitmap cluster */ if (bcl < 2 || bcl >= fs->n_fatent) return FR_NO_FILESYSTEM; /* (Wrong cluster#) */ fs->bitbase = fs->database + fs->csize * (bcl - 2); /* Bitmap sector */ for (;;) { /* Check if bitmap is contiguous */ if (move_window(fs, fs->fatbase + bcl / (SS(fs) / 4)) != FR_OK) return FR_DISK_ERR; - cv = ld_dword(fs->win + bcl % (SS(fs) / 4) * 4); + cv = ld_32(fs->win + bcl % (SS(fs) / 4) * 4); if (cv == 0xFFFFFFFF) break; /* Last link? */ - if (cv != ++bcl) return FR_NO_FILESYSTEM; /* Fragmented? */ + if (cv != ++bcl) return FR_NO_FILESYSTEM; /* Fragmented bitmap? */ } #if !FF_FS_READONLY - fs->last_clst = fs->free_clst = 0xFFFFFFFF; /* Initialize cluster allocation information */ + fs->last_clst = fs->free_clst = 0xFFFFFFFF; /* Invalidate cluster allocation information */ + fs->fsi_flag = 0; /* Enable to sync PercInUse value in VBR */ #endif fmt = FS_EXFAT; /* FAT sub-type */ } else #endif /* FF_FS_EXFAT */ { - if (ld_word(fs->win + BPB_BytsPerSec) != SS(fs)) return FR_NO_FILESYSTEM; /* (BPB_BytsPerSec must be equal to the physical sector size) */ + if (ld_16(fs->win + BPB_BytsPerSec) != SS(fs)) return FR_NO_FILESYSTEM; /* (BPB_BytsPerSec must be equal to the physical sector size) */ - fasize = ld_word(fs->win + BPB_FATSz16); /* Number of sectors per FAT */ - if (fasize == 0) fasize = ld_dword(fs->win + BPB_FATSz32); + fasize = ld_16(fs->win + BPB_FATSz16); /* Number of sectors per FAT */ + if (fasize == 0) fasize = ld_32(fs->win + BPB_FATSz32); fs->fsize = fasize; fs->n_fats = fs->win[BPB_NumFATs]; /* Number of FATs */ @@ -3470,13 +3546,13 @@ static FRESULT mount_volume ( /* FR_OK(0): successful, !=0: an error occurred */ fs->csize = fs->win[BPB_SecPerClus]; /* Cluster size */ if (fs->csize == 0 || (fs->csize & (fs->csize - 1))) return FR_NO_FILESYSTEM; /* (Must be power of 2) */ - fs->n_rootdir = ld_word(fs->win + BPB_RootEntCnt); /* Number of root directory entries */ + fs->n_rootdir = ld_16(fs->win + BPB_RootEntCnt); /* Number of root directory entries */ if (fs->n_rootdir % (SS(fs) / SZDIRE)) return FR_NO_FILESYSTEM; /* (Must be sector aligned) */ - tsect = ld_word(fs->win + BPB_TotSec16); /* Number of sectors on the volume */ - if (tsect == 0) tsect = ld_dword(fs->win + BPB_TotSec32); + tsect = ld_16(fs->win + BPB_TotSec16); /* Number of sectors on the volume */ + if (tsect == 0) tsect = ld_32(fs->win + BPB_TotSec32); - nrsv = ld_word(fs->win + BPB_RsvdSecCnt); /* Number of reserved sectors */ + nrsv = ld_16(fs->win + BPB_RsvdSecCnt); /* Number of reserved sectors */ if (nrsv == 0) return FR_NO_FILESYSTEM; /* (Must not be 0) */ /* Determine the FAT sub type */ @@ -3496,9 +3572,9 @@ static FRESULT mount_volume ( /* FR_OK(0): successful, !=0: an error occurred */ fs->fatbase = bsect + nrsv; /* FAT start sector */ fs->database = bsect + sysect; /* Data start sector */ if (fmt == FS_FAT32) { - if (ld_word(fs->win + BPB_FSVer32) != 0) return FR_NO_FILESYSTEM; /* (Must be FAT32 revision 0.0) */ + if (ld_16(fs->win + BPB_FSVer32) != 0) return FR_NO_FILESYSTEM; /* (Must be FAT32 revision 0.0) */ if (fs->n_rootdir != 0) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must be 0) */ - fs->dirbase = ld_dword(fs->win + BPB_RootClus32); /* Root directory start cluster */ + fs->dirbase = ld_32(fs->win + BPB_RootClus32); /* Root directory start cluster */ szbfat = fs->n_fatent * 4; /* (Needed FAT size) */ } else { if (fs->n_rootdir == 0) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must not be 0) */ @@ -3510,31 +3586,29 @@ static FRESULT mount_volume ( /* FR_OK(0): successful, !=0: an error occurred */ #if !FF_FS_READONLY /* Get FSInfo if available */ - fs->last_clst = fs->free_clst = 0xFFFFFFFF; /* Initialize cluster allocation information */ - fs->fsi_flag = 0x80; -#if (FF_FS_NOFSINFO & 3) != 3 - if (fmt == FS_FAT32 /* Allow to update FSInfo only if BPB_FSInfo32 == 1 */ - && ld_word(fs->win + BPB_FSInfo32) == 1 + fs->last_clst = fs->free_clst = 0xFFFFFFFF; /* Invalidate cluster allocation information */ + fs->fsi_flag = 0x80; /* Disable FSInfo by default */ + if (fmt == FS_FAT32 + && ld_16(fs->win + BPB_FSInfo32) == 1 /* FAT32: Enable FSInfo feature only if FSInfo sector is next to VBR */ && move_window(fs, bsect + 1) == FR_OK) { fs->fsi_flag = 0; - if (ld_word(fs->win + BS_55AA) == 0xAA55 /* Load FSInfo data if available */ - && ld_dword(fs->win + FSI_LeadSig) == 0x41615252 - && ld_dword(fs->win + FSI_StrucSig) == 0x61417272) + if ( ld_32(fs->win + FSI_LeadSig) == 0x41615252 /* Load FSInfo data if available */ + && ld_32(fs->win + FSI_StrucSig) == 0x61417272 + && ld_32(fs->win + FSI_TrailSig) == 0xAA550000) { -#if (FF_FS_NOFSINFO & 1) == 0 - fs->free_clst = ld_dword(fs->win + FSI_Free_Count); +#if (FF_FS_NOFSINFO & 1) == 0 /* Get free cluster count if trust it */ + fs->free_clst = ld_32(fs->win + FSI_Free_Count); #endif -#if (FF_FS_NOFSINFO & 2) == 0 - fs->last_clst = ld_dword(fs->win + FSI_Nxt_Free); +#if (FF_FS_NOFSINFO & 2) == 0 /* Get next free cluster if rtust it */ + fs->last_clst = ld_32(fs->win + FSI_Nxt_Free); #endif } } -#endif /* (FF_FS_NOFSINFO & 3) != 3 */ #endif /* !FF_FS_READONLY */ } - fs->fs_type = (BYTE)fmt;/* FAT sub-type */ + fs->fs_type = (BYTE)fmt;/* FAT sub-type (the filesystem object gets valid) */ fs->id = ++Fsid; /* Volume mount ID */ #if FF_USE_LFN == 1 fs->lfnbuf = LfnBuf; /* Static LFN working buffer */ @@ -3545,8 +3619,8 @@ static FRESULT mount_volume ( /* FR_OK(0): successful, !=0: an error occurred */ #if FF_FS_RPATH != 0 fs->cdir = 0; /* Initialize current directory */ #endif -#if FF_FS_LOCK != 0 /* Clear file lock semaphores */ - clear_lock(fs); +#if FF_FS_LOCK /* Clear file lock semaphores */ + clear_share(fs); #endif return FR_OK; } @@ -3559,7 +3633,7 @@ static FRESULT mount_volume ( /* FR_OK(0): successful, !=0: an error occurred */ /*-----------------------------------------------------------------------*/ static FRESULT validate ( /* Returns FR_OK or FR_INVALID_OBJECT */ - FFOBJID* obj, /* Pointer to the FFOBJID, the 1st member in the FIL/DIR object, to check validity */ + FFOBJID* obj, /* Pointer to the FFOBJID, the 1st member in the FIL/DIR structure, to check validity */ FATFS** rfs /* Pointer to pointer to the owner filesystem object to return */ ) { @@ -3568,22 +3642,22 @@ static FRESULT validate ( /* Returns FR_OK or FR_INVALID_OBJECT */ if (obj && obj->fs && obj->fs->fs_type && obj->id == obj->fs->id) { /* Test if the object is valid */ #if FF_FS_REENTRANT - if (lock_fs(obj->fs)) { /* Obtain the filesystem object */ - if (!(disk_status(obj->fs->pdrv) & STA_NOINIT)) { /* Test if the phsical drive is kept initialized */ + if (lock_volume(obj->fs, 0)) { /* Take a grant to access the volume */ + if (!(disk_status(obj->fs->pdrv) & STA_NOINIT)) { /* Test if the hosting physical drive is kept initialized */ res = FR_OK; } else { - unlock_fs(obj->fs, FR_OK); + unlock_volume(obj->fs, FR_OK); /* Invalidated volume, abort to access */ } - } else { + } else { /* Could not take */ res = FR_TIMEOUT; } #else - if (!(disk_status(obj->fs->pdrv) & STA_NOINIT)) { /* Test if the phsical drive is kept initialized */ + if (!(disk_status(obj->fs->pdrv) & STA_NOINIT)) { /* Test if the hosting physical drive is kept initialized */ res = FR_OK; } #endif } - *rfs = (res == FR_OK) ? obj->fs : 0; /* Corresponding filesystem object */ + *rfs = (res == FR_OK) ? obj->fs : 0; /* Return corresponding filesystem object if it is valid */ return res; } @@ -3614,30 +3688,42 @@ FRESULT f_mount ( const TCHAR *rp = path; - /* Get logical drive number */ + /* Get volume ID (logical drive number) */ vol = get_ldnumber(&rp); if (vol < 0) return FR_INVALID_DRIVE; - cfs = FatFs[vol]; /* Pointer to fs object */ + cfs = FatFs[vol]; /* Pointer to the filesystem object of the volume */ - if (cfs) { -#if FF_FS_LOCK != 0 - clear_lock(cfs); + if (cfs) { /* Unregister current filesystem object if registered */ + FatFs[vol] = 0; +#if FF_FS_LOCK + clear_share(cfs); #endif -#if FF_FS_REENTRANT /* Discard sync object of the current volume */ - if (!ff_del_syncobj(cfs->sobj)) return FR_INT_ERR; +#if FF_FS_REENTRANT /* Discard mutex of the current volume */ + ff_mutex_delete(vol); #endif - cfs->fs_type = 0; /* Clear old fs object */ + cfs->fs_type = 0; /* Invalidate the filesystem object to be unregistered */ } - if (fs) { - fs->fs_type = 0; /* Clear new fs object */ -#if FF_FS_REENTRANT /* Create sync object for the new volume */ - if (!ff_cre_syncobj((BYTE)vol, &fs->sobj)) return FR_INT_ERR; + if (fs) { /* Register new filesystem object */ + fs->pdrv = LD2PD(vol); /* Volume hosting physical drive */ +#if FF_FS_REENTRANT /* Create a volume mutex */ + fs->ldrv = (BYTE)vol; /* Owner volume ID */ + if (!ff_mutex_create(vol)) return FR_INT_ERR; +#if FF_FS_LOCK + if (SysLock == 0) { /* Create a system mutex if needed */ + if (!ff_mutex_create(FF_VOLUMES)) { + ff_mutex_delete(vol); + return FR_INT_ERR; + } + SysLock = 1; /* System mutex is ready */ + } +#endif #endif + fs->fs_type = 0; /* Invalidate the new filesystem object */ + FatFs[vol] = fs; /* Register new fs object */ } - FatFs[vol] = fs; /* Register new fs object */ - if (opt == 0) return FR_OK; /* Do not mount now, it will be mounted later */ + if (opt == 0) return FR_OK; /* Do not mount now, it will be mounted in subsequent file functions */ res = mount_volume(&path, &fs, 0); /* Force mounted the volume */ LEAVE_FF(fs, res); @@ -3660,18 +3746,19 @@ FRESULT f_open ( DIR dj; FATFS *fs; #if !FF_FS_READONLY - DWORD cl, bcs, clst, tm; + DWORD cl, bcs, clst; LBA_t sc; FSIZE_t ofs; #endif DEF_NAMBUF - if (!fp) return FR_INVALID_OBJECT; + if (!fp) return FR_INVALID_OBJECT; /* Reject null pointer */ - /* Get logical drive number */ + /* Get logical drive number and mount the volume if needed */ mode &= FF_FS_READONLY ? FA_READ : FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_CREATE_NEW | FA_OPEN_ALWAYS | FA_OPEN_APPEND; res = mount_volume(&path, &fs, mode); + if (res == FR_OK) { dj.obj.fs = fs; INIT_NAMBUF(fs); @@ -3681,9 +3768,9 @@ FRESULT f_open ( if (dj.fn[NSFLAG] & NS_NONAME) { /* Origin directory itself? */ res = FR_INVALID_NAME; } -#if FF_FS_LOCK != 0 +#if FF_FS_LOCK else { - res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0); /* Check if the file can be used */ + res = chk_share(&dj, (mode & ~FA_READ) ? 1 : 0); /* Check if the file can be used */ } #endif } @@ -3691,19 +3778,19 @@ FRESULT f_open ( if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) { if (res != FR_OK) { /* No file, create new */ if (res == FR_NO_FILE) { /* There is no file to open, create a new entry */ -#if FF_FS_LOCK != 0 - res = enq_lock() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES; +#if FF_FS_LOCK + res = enq_share() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES; #else res = dir_register(&dj); #endif } mode |= FA_CREATE_ALWAYS; /* File is created */ } - else { /* Any object with the same name is already existing */ - if (dj.obj.attr & (AM_RDO | AM_DIR)) { /* Cannot overwrite it (R/O or DIR) */ - res = FR_DENIED; + else { /* An object with the same name is already existing */ + if (mode & FA_CREATE_NEW) { + res = FR_EXIST; /* Cannot create as new file */ } else { - if (mode & FA_CREATE_NEW) res = FR_EXIST; /* Cannot create as new file */ + if (dj.obj.attr & (AM_RDO | AM_DIR)) res = FR_DENIED; /* Cannot overwrite it (R/O or DIR) */ } } if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) { /* Truncate the file if overwrite mode */ @@ -3712,11 +3799,11 @@ FRESULT f_open ( /* Get current allocation info */ fp->obj.fs = fs; init_alloc_info(fs, &fp->obj); - /* Set directory entry block initial state */ + /* Set exFAT directory entry block initial state */ memset(fs->dirbuf + 2, 0, 30); /* Clear 85 entry except for NumSec */ memset(fs->dirbuf + 38, 0, 26); /* Clear C0 entry except for NumName and NameHash */ fs->dirbuf[XDIR_Attr] = AM_ARC; - st_dword(fs->dirbuf + XDIR_CrtTime, GET_FATTIME()); + st_32(fs->dirbuf + XDIR_CrtTime, GET_FATTIME()); /* Set created time */ fs->dirbuf[XDIR_GenFlags] = 1; res = store_xdir(&dj); if (res == FR_OK && fp->obj.sclust != 0) { /* Remove the cluster chain if exist */ @@ -3726,14 +3813,12 @@ FRESULT f_open ( } else #endif { - /* Set directory entry initial state */ - tm = GET_FATTIME(); /* Set created time */ - st_dword(dj.dir + DIR_CrtTime, tm); - st_dword(dj.dir + DIR_ModTime, tm); + /* Set FAT directory entry initial state */ + st_32(dj.dir + DIR_CrtTime, GET_FATTIME()); /* Set created time */ cl = ld_clust(fs, dj.dir); /* Get current cluster chain */ dj.dir[DIR_Attr] = AM_ARC; /* Reset attribute */ st_clust(fs, dj.dir, 0); /* Reset file allocation info */ - st_dword(dj.dir + DIR_FileSize, 0); + st_32(dj.dir + DIR_FileSize, 0); fs->wflag = 1; if (cl != 0) { /* Remove the cluster chain if exist */ sc = fs->winsect; @@ -3761,8 +3846,8 @@ FRESULT f_open ( if (mode & FA_CREATE_ALWAYS) mode |= FA_MODIFIED; /* Set file change flag if created or overwritten */ fp->dir_sect = fs->winsect; /* Pointer to the directory entry */ fp->dir_ptr = dj.dir; -#if FF_FS_LOCK != 0 - fp->obj.lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0); /* Lock the file for this session */ +#if FF_FS_LOCK + fp->obj.lockid = inc_share(&dj, (mode & ~FA_READ) ? 1 : 0); /* Lock the file for this session */ if (fp->obj.lockid == 0) res = FR_INT_ERR; #endif } @@ -3789,7 +3874,7 @@ FRESULT f_open ( #endif { fp->obj.sclust = ld_clust(fs, dj.dir); /* Get object allocation info */ - fp->obj.objsize = ld_dword(dj.dir + DIR_FileSize); + fp->obj.objsize = ld_32(dj.dir + DIR_FileSize); } #if FF_USE_FASTSEEK fp->cltbl = 0; /* Disable fast seek mode */ @@ -3825,8 +3910,8 @@ FRESULT f_open ( #endif } } -#if FF_FS_LOCK != 0 - if (res != FR_OK) dec_lock(fp->obj.lockid); /* Decrement file open counter if seek failed */ +#if FF_FS_LOCK + if (res != FR_OK) dec_share(fp->obj.lockid); /* Decrement file open counter if seek failed */ #endif } #endif @@ -3921,7 +4006,7 @@ FRESULT f_read ( fp->flag &= (BYTE)~FA_DIRTY; } #endif - if (disk_read(fs->pdrv, fp->buf, sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); /* Fill sector cache */ + if (disk_read(fs->pdrv, fp->buf, sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); /* Fill sector cache */ } #endif fp->sect = sect; @@ -4074,7 +4159,6 @@ FRESULT f_sync ( { FRESULT res; FATFS *fs; - DWORD tm; BYTE *dir; @@ -4088,7 +4172,6 @@ FRESULT f_sync ( } #endif /* Update the directory entry */ - tm = GET_FATTIME(); /* Modified time */ #if FF_FS_EXFAT if (fs->fs_type == FS_EXFAT) { res = fill_first_frag(&fp->obj); /* Fill first fragment on the FAT if needed */ @@ -4102,15 +4185,17 @@ FRESULT f_sync ( INIT_NAMBUF(fs); res = load_obj_xdir(&dj, &fp->obj); /* Load directory entry block */ if (res == FR_OK) { - fs->dirbuf[XDIR_Attr] |= AM_ARC; /* Set archive attribute to indicate that the file has been changed */ - fs->dirbuf[XDIR_GenFlags] = fp->obj.stat | 1; /* Update file allocation information */ - st_dword(fs->dirbuf + XDIR_FstClus, fp->obj.sclust); /* Update start cluster */ - st_qword(fs->dirbuf + XDIR_FileSize, fp->obj.objsize); /* Update file size */ - st_qword(fs->dirbuf + XDIR_ValidFileSize, fp->obj.objsize); /* (FatFs does not support Valid File Size feature) */ - st_dword(fs->dirbuf + XDIR_ModTime, tm); /* Update modified time */ + fs->dirbuf[XDIR_Attr] |= AM_ARC; /* Set archive attribute to indicate that the file has been changed */ + fs->dirbuf[XDIR_GenFlags] = fp->obj.stat | 1; /* Update file allocation information */ + st_32(fs->dirbuf + XDIR_FstClus, fp->obj.sclust); /* Update start cluster */ + st_64(fs->dirbuf + XDIR_FileSize, fp->obj.objsize); /* Update file size */ + st_64(fs->dirbuf + XDIR_ValidFileSize, fp->obj.objsize); /* (FatFs does not support Valid File Size feature) */ + st_32(fs->dirbuf + XDIR_ModTime, GET_FATTIME()); /* Update modified time */ fs->dirbuf[XDIR_ModTime10] = 0; - st_dword(fs->dirbuf + XDIR_AccTime, 0); - res = store_xdir(&dj); /* Restore it to the directory */ + fs->dirbuf[XDIR_ModTZ] = 0; + st_32(fs->dirbuf + XDIR_AccTime, 0); /* Invalidate last access time */ + fs->dirbuf[XDIR_AccTZ] = 0; + res = store_xdir(&dj); /* Restore it to the directory */ if (res == FR_OK) { res = sync_fs(fs); fp->flag &= (BYTE)~FA_MODIFIED; @@ -4124,13 +4209,13 @@ FRESULT f_sync ( res = move_window(fs, fp->dir_sect); if (res == FR_OK) { dir = fp->dir_ptr; - dir[DIR_Attr] |= AM_ARC; /* Set archive attribute to indicate that the file has been changed */ - st_clust(fp->obj.fs, dir, fp->obj.sclust); /* Update file allocation information */ - st_dword(dir + DIR_FileSize, (DWORD)fp->obj.objsize); /* Update file size */ - st_dword(dir + DIR_ModTime, tm); /* Update modified time */ - st_word(dir + DIR_LstAccDate, 0); + dir[DIR_Attr] |= AM_ARC; /* Set archive attribute to indicate that the file has been changed */ + st_clust(fp->obj.fs, dir, fp->obj.sclust); /* Update file allocation information */ + st_32(dir + DIR_FileSize, (DWORD)fp->obj.objsize); /* Update file size */ + st_32(dir + DIR_ModTime, GET_FATTIME()); /* Update modified time */ + st_16(dir + DIR_LstAccDate, 0); /* Invalidate last access date */ fs->wflag = 1; - res = sync_fs(fs); /* Restore it to the directory */ + res = sync_fs(fs); /* Restore it to the directory */ fp->flag &= (BYTE)~FA_MODIFIED; } } @@ -4163,14 +4248,14 @@ FRESULT f_close ( { res = validate(&fp->obj, &fs); /* Lock volume */ if (res == FR_OK) { -#if FF_FS_LOCK != 0 - res = dec_lock(fp->obj.lockid); /* Decrement file open counter */ +#if FF_FS_LOCK + res = dec_share(fp->obj.lockid); /* Decrement file open counter */ if (res == FR_OK) fp->obj.fs = 0; /* Invalidate file object */ #else fp->obj.fs = 0; /* Invalidate file object */ #endif #if FF_FS_REENTRANT - unlock_fs(fs, FR_OK); /* Unlock volume */ + unlock_volume(fs, FR_OK); /* Unlock volume */ #endif } } @@ -4215,8 +4300,7 @@ FRESULT f_chdir ( DEF_NAMBUF - /* Get logical drive */ - res = mount_volume(&path, &fs, 0); + res = mount_volume(&path, &fs, 0); /* Get logical drive and mount the volume if needed */ if (res == FR_OK) { dj.obj.fs = fs; INIT_NAMBUF(fs); @@ -4235,14 +4319,14 @@ FRESULT f_chdir ( if (dj.obj.attr & AM_DIR) { /* It is a sub-directory */ #if FF_FS_EXFAT if (fs->fs_type == FS_EXFAT) { - fs->cdir = ld_dword(fs->dirbuf + XDIR_FstClus); /* Sub-directory cluster */ - fs->cdc_scl = dj.obj.sclust; /* Save containing directory information */ + fs->cdir = ld_32(fs->dirbuf + XDIR_FstClus); /* Sub-directory cluster */ + fs->cdc_scl = dj.obj.sclust; /* Save containing directory information */ fs->cdc_size = ((DWORD)dj.obj.objsize & 0xFFFFFF00) | dj.obj.stat; fs->cdc_ofs = dj.blk_ofs; } else #endif { - fs->cdir = ld_clust(fs, dj.dir); /* Sub-directory cluster */ + fs->cdir = ld_clust(fs, dj.dir); /* Sub-directory cluster */ } } else { res = FR_NO_PATH; /* Reached but a file */ @@ -4344,7 +4428,9 @@ FRESULT f_getcwd ( #endif /* Add current directory path */ if (res == FR_OK) { - do *tp++ = buff[i++]; while (i < len); /* Copy stacked path string */ + do { /* Copy stacked path string */ + *tp++ = buff[i++]; + } while (i < len); } } FREE_NAMBUF(); @@ -4538,10 +4624,9 @@ FRESULT f_opendir ( DEF_NAMBUF - if (!dp) return FR_INVALID_OBJECT; + if (!dp) return FR_INVALID_OBJECT; /* Reject null pointer */ - /* Get logical drive */ - res = mount_volume(&path, &fs, 0); + res = mount_volume(&path, &fs, 0); /* Get logical drive and mount the volume if needed */ if (res == FR_OK) { dp->obj.fs = fs; INIT_NAMBUF(fs); @@ -4551,7 +4636,7 @@ FRESULT f_opendir ( if (dp->obj.attr & AM_DIR) { /* This object is a sub-directory */ #if FF_FS_EXFAT if (fs->fs_type == FS_EXFAT) { - dp->obj.c_scl = dp->obj.sclust; /* Get containing directory inforamation */ + dp->obj.c_scl = dp->obj.sclust; /* Get containing directory information */ dp->obj.c_size = ((DWORD)dp->obj.objsize & 0xFFFFFF00) | dp->obj.stat; dp->obj.c_ofs = dp->blk_ofs; init_alloc_info(fs, &dp->obj); /* Get object allocation info */ @@ -4567,10 +4652,10 @@ FRESULT f_opendir ( if (res == FR_OK) { dp->obj.id = fs->id; res = dir_sdi(dp, 0); /* Rewind directory */ -#if FF_FS_LOCK != 0 +#if FF_FS_LOCK if (res == FR_OK) { if (dp->obj.sclust != 0) { - dp->obj.lockid = inc_lock(dp, 0); /* Lock the sub directory */ + dp->obj.lockid = inc_share(dp, 0); /* Lock the sub directory */ if (!dp->obj.lockid) res = FR_TOO_MANY_OPEN_FILES; } else { dp->obj.lockid = 0; /* Root directory need not to be locked */ @@ -4582,7 +4667,7 @@ FRESULT f_opendir ( FREE_NAMBUF(); if (res == FR_NO_FILE) res = FR_NO_PATH; } - if (res != FR_OK) dp->obj.fs = 0; /* Invalidate the directory object if function faild */ + if (res != FR_OK) dp->obj.fs = 0; /* Invalidate the directory object if function failed */ LEAVE_FF(fs, res); } @@ -4604,14 +4689,14 @@ FRESULT f_closedir ( res = validate(&dp->obj, &fs); /* Check validity of the file object */ if (res == FR_OK) { -#if FF_FS_LOCK != 0 - if (dp->obj.lockid) res = dec_lock(dp->obj.lockid); /* Decrement sub-directory open counter */ +#if FF_FS_LOCK + if (dp->obj.lockid) res = dec_share(dp->obj.lockid); /* Decrement sub-directory open counter */ if (res == FR_OK) dp->obj.fs = 0; /* Invalidate directory object */ #else dp->obj.fs = 0; /* Invalidate directory object */ #endif #if FF_FS_REENTRANT - unlock_fs(fs, FR_OK); /* Unlock volume */ + unlock_volume(fs, FR_OK); /* Unlock volume */ #endif } return res; @@ -4637,7 +4722,7 @@ FRESULT f_readdir ( res = validate(&dp->obj, &fs); /* Check validity of the directory object */ if (res == FR_OK) { if (!fno) { - res = dir_sdi(dp, 0); /* Rewind the directory object */ + res = dir_sdi(dp, 0); /* Rewind the directory object */ } else { INIT_NAMBUF(fs); res = DIR_READ_FILE(dp); /* Read an item */ @@ -4650,6 +4735,8 @@ FRESULT f_readdir ( FREE_NAMBUF(); } } + + if (fno && res != FR_OK) fno->fname[0] = 0; /* Invalidate the file information if any error occured */ LEAVE_FF(fs, res); } @@ -4722,8 +4809,9 @@ FRESULT f_stat ( DEF_NAMBUF - /* Get logical drive */ + /* Get logical drive and mount the volume if needed */ res = mount_volume(&path, &dj.obj.fs, 0); + if (res == FR_OK) { INIT_NAMBUF(dj.obj.fs); res = follow_path(&dj, path); /* Follow the file path */ @@ -4737,6 +4825,7 @@ FRESULT f_stat ( FREE_NAMBUF(); } + if (fno && res != FR_OK) fno->fname[0] = 0; /* Invalidate the file information if any error occured */ LEAVE_FF(dj.obj.fs, res); } @@ -4750,7 +4839,7 @@ FRESULT f_stat ( FRESULT f_getfree ( const TCHAR* path, /* Logical drive number */ DWORD* nclst, /* Pointer to a variable to return number of free clusters */ - FATFS** fatfs /* Pointer to return pointer to corresponding filesystem object */ + FATFS** fatfs /* Pointer to a pointer to return corresponding filesystem object */ ) { FRESULT res; @@ -4761,22 +4850,27 @@ FRESULT f_getfree ( FFOBJID obj; - /* Get logical drive */ + /* Get logical drive and mount the volume if needed */ res = mount_volume(&path, &fs, 0); + if (res == FR_OK) { *fatfs = fs; /* Return ptr to the fs object */ /* If free_clst is valid, return it without full FAT scan */ if (fs->free_clst <= fs->n_fatent - 2) { *nclst = fs->free_clst; } else { - /* Scan FAT to obtain number of free clusters */ + /* Scan FAT to obtain the correct free cluster count */ nfree = 0; if (fs->fs_type == FS_FAT12) { /* FAT12: Scan bit field FAT entries */ clst = 2; obj.fs = fs; do { stat = get_fat(&obj, clst); - if (stat == 0xFFFFFFFF) { res = FR_DISK_ERR; break; } - if (stat == 1) { res = FR_INT_ERR; break; } + if (stat == 0xFFFFFFFF) { + res = FR_DISK_ERR; break; + } + if (stat == 1) { + res = FR_INT_ERR; break; + } if (stat == 0) nfree++; } while (++clst < fs->n_fatent); } else { @@ -4788,16 +4882,16 @@ FRESULT f_getfree ( clst = fs->n_fatent - 2; /* Number of clusters */ sect = fs->bitbase; /* Bitmap sector */ i = 0; /* Offset in the sector */ - do { /* Counts numbuer of bits with zero in the bitmap */ - if (i == 0) { + do { /* Counts numbuer of clear bits (free clusters) in the bitmap */ + if (i == 0) { /* New sector? */ res = move_window(fs, sect++); if (res != FR_OK) break; } - for (b = 8, bm = fs->win[i]; b && clst; b--, clst--) { - if (!(bm & 1)) nfree++; + for (b = 8, bm = ~fs->win[i]; b && clst; b--, clst--) { /* Count clear bits in a byte */ + nfree += bm & 1; bm >>= 1; } - i = (i + 1) % SS(fs); + i = (i + 1) % SS(fs); /* Next byte */ } while (clst); } else #endif @@ -4806,16 +4900,16 @@ FRESULT f_getfree ( sect = fs->fatbase; /* Top of the FAT */ i = 0; /* Offset in the sector */ do { /* Counts numbuer of entries with zero in the FAT */ - if (i == 0) { + if (i == 0) { /* New sector? */ res = move_window(fs, sect++); if (res != FR_OK) break; } if (fs->fs_type == FS_FAT16) { - if (ld_word(fs->win + i) == 0) nfree++; - i += 2; + if (ld_16(fs->win + i) == 0) nfree++; /* FAT16: Is this cluster free? */ + i += 2; /* Next entry */ } else { - if ((ld_dword(fs->win + i) & 0x0FFFFFFF) == 0) nfree++; - i += 4; + if ((ld_32(fs->win + i) & 0x0FFFFFFF) == 0) nfree++; /* FAT32: Is this cluster free? */ + i += 4; /* Next entry */ } i %= SS(fs); } while (--clst); @@ -4823,8 +4917,8 @@ FRESULT f_getfree ( } if (res == FR_OK) { /* Update parameters if succeeded */ *nclst = nfree; /* Return the free clusters */ - fs->free_clst = nfree; /* Now free_clst is valid */ - fs->fsi_flag |= 1; /* FAT32: FSInfo is to be updated */ + fs->free_clst = nfree; /* Now free cluster count is valid */ + fs->fsi_flag |= 1; /* FAT32/exfAT : Allocation information is to be updated */ } } } @@ -4848,7 +4942,8 @@ FRESULT f_truncate ( DWORD ncl; - res = validate(&fp->obj, &fs); /* Check validity of the file object */ + /* Check validity of the file object */ + res = validate(&fp->obj, &fs); if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res); if (!(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED); /* Check access mode */ @@ -4894,17 +4989,18 @@ FRESULT f_unlink ( ) { FRESULT res; + FATFS *fs; DIR dj, sdj; DWORD dclst = 0; - FATFS *fs; #if FF_FS_EXFAT FFOBJID obj; #endif DEF_NAMBUF - /* Get logical drive */ + /* Get logical drive and mount the volume if needed */ res = mount_volume(&path, &fs, FA_WRITE); + if (res == FR_OK) { dj.obj.fs = fs; INIT_NAMBUF(fs); @@ -4912,8 +5008,8 @@ FRESULT f_unlink ( if (FF_FS_RPATH && res == FR_OK && (dj.fn[NSFLAG] & NS_DOT)) { res = FR_INVALID_NAME; /* Cannot remove dot entry */ } -#if FF_FS_LOCK != 0 - if (res == FR_OK) res = chk_lock(&dj, 2); /* Check if it is an open object */ +#if FF_FS_LOCK + if (res == FR_OK) res = chk_share(&dj, 2); /* Check if it is an open object */ #endif if (res == FR_OK) { /* The object is accessible */ if (dj.fn[NSFLAG] & NS_NONAME) { @@ -4988,14 +5084,16 @@ FRESULT f_mkdir ( ) { FRESULT res; + FATFS *fs; DIR dj; FFOBJID sobj; - FATFS *fs; DWORD dcl, pcl, tm; DEF_NAMBUF - res = mount_volume(&path, &fs, FA_WRITE); /* Get logical drive */ + /* Get logical drive and mount the volume if needed */ + res = mount_volume(&path, &fs, FA_WRITE); + if (res == FR_OK) { dj.obj.fs = fs; INIT_NAMBUF(fs); @@ -5019,30 +5117,32 @@ FRESULT f_mkdir ( memset(fs->win + DIR_Name, ' ', 11); /* Create "." entry */ fs->win[DIR_Name] = '.'; fs->win[DIR_Attr] = AM_DIR; - st_dword(fs->win + DIR_ModTime, tm); + st_32(fs->win + DIR_ModTime, tm); st_clust(fs, fs->win, dcl); memcpy(fs->win + SZDIRE, fs->win, SZDIRE); /* Create ".." entry */ fs->win[SZDIRE + 1] = '.'; pcl = dj.obj.sclust; st_clust(fs, fs->win + SZDIRE, pcl); fs->wflag = 1; } - res = dir_register(&dj); /* Register the object to the parent directoy */ + res = dir_register(&dj); /* Register the object to the parent directory */ } } if (res == FR_OK) { #if FF_FS_EXFAT if (fs->fs_type == FS_EXFAT) { /* Initialize directory entry block */ - st_dword(fs->dirbuf + XDIR_ModTime, tm); /* Created time */ - st_dword(fs->dirbuf + XDIR_FstClus, dcl); /* Table start cluster */ - st_dword(fs->dirbuf + XDIR_FileSize, (DWORD)fs->csize * SS(fs)); /* Directory size needs to be valid */ - st_dword(fs->dirbuf + XDIR_ValidFileSize, (DWORD)fs->csize * SS(fs)); - fs->dirbuf[XDIR_GenFlags] = 3; /* Initialize the object flag */ - fs->dirbuf[XDIR_Attr] = AM_DIR; /* Attribute */ + st_32(fs->dirbuf + XDIR_CrtTime, tm); /* Created time */ + st_32(fs->dirbuf + XDIR_ModTime, tm); + st_32(fs->dirbuf + XDIR_FstClus, dcl); /* Table start cluster */ + st_32(fs->dirbuf + XDIR_FileSize, (DWORD)fs->csize * SS(fs)); /* Directory size needs to be valid */ + st_32(fs->dirbuf + XDIR_ValidFileSize, (DWORD)fs->csize * SS(fs)); + fs->dirbuf[XDIR_GenFlags] = 3; /* Initialize the object flag */ + fs->dirbuf[XDIR_Attr] = AM_DIR; /* Attribute */ res = store_xdir(&dj); } else #endif { - st_dword(dj.dir + DIR_ModTime, tm); /* Created time */ + st_32(dj.dir + DIR_CrtTime, tm); /* Created time */ + st_32(dj.dir + DIR_ModTime, tm); st_clust(fs, dj.dir, dcl); /* Table start cluster */ dj.dir[DIR_Attr] = AM_DIR; /* Attribute */ fs->wflag = 1; @@ -5073,23 +5173,26 @@ FRESULT f_rename ( ) { FRESULT res; - DIR djo, djn; FATFS *fs; + DIR djo, djn; BYTE buf[FF_FS_EXFAT ? SZDIRE * 2 : SZDIRE], *dir; LBA_t sect; DEF_NAMBUF - get_ldnumber(&path_new); /* Snip the drive number of new name off */ - res = mount_volume(&path_old, &fs, FA_WRITE); /* Get logical drive of the old object */ + /* Snip the drive number of new name off */ + get_ldnumber(&path_new); + + /* Get logical drive of the old object */ + res = mount_volume(&path_old, &fs, FA_WRITE); if (res == FR_OK) { djo.obj.fs = fs; INIT_NAMBUF(fs); res = follow_path(&djo, path_old); /* Check old object */ if (res == FR_OK && (djo.fn[NSFLAG] & (NS_DOT | NS_NONAME))) res = FR_INVALID_NAME; /* Check validity of name */ -#if FF_FS_LOCK != 0 +#if FF_FS_LOCK if (res == FR_OK) { - res = chk_lock(&djo, 2); + res = chk_share(&djo, 2); } #endif if (res == FR_OK) { /* Object to be renamed is found */ @@ -5108,10 +5211,10 @@ FRESULT f_rename ( res = dir_register(&djn); /* Register the new entry */ if (res == FR_OK) { nf = fs->dirbuf[XDIR_NumSec]; nn = fs->dirbuf[XDIR_NumName]; - nh = ld_word(fs->dirbuf + XDIR_NameHash); + nh = ld_16(fs->dirbuf + XDIR_NameHash); memcpy(fs->dirbuf, buf, SZDIRE * 2); /* Restore 85+C0 entry */ fs->dirbuf[XDIR_NumSec] = nf; fs->dirbuf[XDIR_NumName] = nn; - st_word(fs->dirbuf + XDIR_NameHash, nh); + st_16(fs->dirbuf + XDIR_NameHash, nh); if (!(fs->dirbuf[XDIR_Attr] & AM_DIR)) fs->dirbuf[XDIR_Attr] |= AM_ARC; /* Set archive attribute if it is a file */ /* Start of critical section where an interruption can cause a cross-link */ res = store_xdir(&djn); @@ -5141,7 +5244,7 @@ FRESULT f_rename ( } else { /* Start of critical section where an interruption can cause a cross-link */ res = move_window(fs, sect); - dir = fs->win + SZDIRE * 1; /* Ptr to .. entry */ + dir = fs->win + SZDIRE * 1; /* Pointer to .. entry */ if (res == FR_OK && dir[1] == '.') { st_clust(fs, dir, djn.obj.sclust); fs->wflag = 1; @@ -5179,17 +5282,19 @@ FRESULT f_rename ( FRESULT f_chmod ( const TCHAR* path, /* Pointer to the file path */ - BYTE attr, /* Attribute bits */ + BYTE attr, /* Attribute bits to set/clear */ BYTE mask /* Attribute mask to change */ ) { FRESULT res; - DIR dj; FATFS *fs; + DIR dj; DEF_NAMBUF - res = mount_volume(&path, &fs, FA_WRITE); /* Get logical drive */ + /* Get logical drive and mount the volume if needed */ + res = mount_volume(&path, &fs, FA_WRITE); + if (res == FR_OK) { dj.obj.fs = fs; INIT_NAMBUF(fs); @@ -5226,16 +5331,18 @@ FRESULT f_chmod ( FRESULT f_utime ( const TCHAR* path, /* Pointer to the file/directory name */ - const FILINFO* fno /* Pointer to the timestamp to be set */ + const FILINFO* fno /* Timestamp to be set */ ) { FRESULT res; - DIR dj; FATFS *fs; + DIR dj; DEF_NAMBUF - res = mount_volume(&path, &fs, FA_WRITE); /* Get logical drive */ + /* Get logical drive and mount the volume if needed */ + res = mount_volume(&path, &fs, FA_WRITE); + if (res == FR_OK) { dj.obj.fs = fs; INIT_NAMBUF(fs); @@ -5243,13 +5350,32 @@ FRESULT f_utime ( if (res == FR_OK && (dj.fn[NSFLAG] & (NS_DOT | NS_NONAME))) res = FR_INVALID_NAME; /* Check object validity */ if (res == FR_OK) { #if FF_FS_EXFAT - if (fs->fs_type == FS_EXFAT) { - st_dword(fs->dirbuf + XDIR_ModTime, (DWORD)fno->fdate << 16 | fno->ftime); + if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */ + if (fno->fdate) { /* Change last modified time if needed */ + st_32(fs->dirbuf + XDIR_ModTime, (DWORD)fno->fdate << 16 | fno->ftime); + fs->dirbuf[XDIR_ModTime10] = 0; + fs->dirbuf[XDIR_ModTZ] = 0; + } +#if FF_FS_CRTIME + if (fno->crdate) { /* Change created time if needed */ + st_32(fs->dirbuf + XDIR_CrtTime, (DWORD)fno->crdate << 16 | fno->crtime); + fs->dirbuf[XDIR_CrtTime10] = 0; + fs->dirbuf[XDIR_CrtTZ] = 0; + } +#endif res = store_xdir(&dj); } else #endif - { - st_dword(dj.dir + DIR_ModTime, (DWORD)fno->fdate << 16 | fno->ftime); + { /* On the FAT volume */ + if (fno->fdate) { /* Change last modified time if needed */ + st_32(dj.dir + DIR_ModTime, (DWORD)fno->fdate << 16 | fno->ftime); + } +#if FF_FS_CRTIME + if (fno->crdate) { /* Change created time if needed */ + st_32(dj.dir + DIR_CrtTime, (DWORD)fno->crdate << 16 | fno->crtime); + dj.dir[DIR_CrtTime10] = 0; + } +#endif fs->wflag = 1; } if (res == FR_OK) { @@ -5278,13 +5404,13 @@ FRESULT f_getlabel ( ) { FRESULT res; - DIR dj; FATFS *fs; + DIR dj; UINT si, di; WCHAR wc; - /* Get logical drive */ - res = mount_volume(&path, &fs, 0); + + res = mount_volume(&path, &fs, 0); /* Get logical drive and mount the volume if needed */ /* Get volume label */ if (res == FR_OK && label) { @@ -5299,12 +5425,14 @@ FRESULT f_getlabel ( UINT nw; for (si = di = hs = 0; si < dj.dir[XDIR_NumLabel]; si++) { /* Extract volume label from 83 entry */ - wc = ld_word(dj.dir + XDIR_Label + si * 2); + wc = ld_16(dj.dir + XDIR_Label + si * 2); if (hs == 0 && IsSurrogate(wc)) { /* Is the code a surrogate? */ hs = wc; continue; } nw = put_utf((DWORD)hs << 16 | wc, &label[di], 4); /* Store it in API encoding */ - if (nw == 0) { di = 0; break; } /* Encode error? */ + if (nw == 0) { /* Encode error? */ + di = 0; break; + } di += nw; hs = 0; } @@ -5319,7 +5447,9 @@ FRESULT f_getlabel ( #if FF_USE_LFN && FF_LFN_UNICODE >= 1 /* Unicode output */ if (dbc_1st((BYTE)wc) && si < 11) wc = wc << 8 | dj.dir[si++]; /* Is it a DBC? */ wc = ff_oem2uni(wc, CODEPAGE); /* Convert it into Unicode */ - if (wc == 0) { di = 0; break; } /* Invalid char in current code page? */ + if (wc == 0) { /* Invalid char in current code page? */ + di = 0; break; + } di += put_utf(wc, &label[di], 4); /* Store it in Unicode */ #else /* ANSI/OEM output */ label[di++] = (TCHAR)wc; @@ -5340,7 +5470,7 @@ FRESULT f_getlabel ( /* Get volume serial number */ if (res == FR_OK && vsn) { - res = move_window(fs, fs->volbase); + res = move_window(fs, fs->volbase); /* Load VBR */ if (res == FR_OK) { switch (fs->fs_type) { case FS_EXFAT: @@ -5351,10 +5481,10 @@ FRESULT f_getlabel ( di = BS_VolID32; break; - default: - di = BS_VolID; + default: /* FAT12/16 */ + di = fs->win[BS_BootSig] == 0x29 ? BS_VolID : 0; } - *vsn = ld_dword(fs->win + di); + *vsn = di ? ld_32(fs->win + di) : 0; /* Get VSN in the VBR */ } } @@ -5373,8 +5503,8 @@ FRESULT f_setlabel ( ) { FRESULT res; - DIR dj; FATFS *fs; + DIR dj; BYTE dirvn[22]; UINT di; WCHAR wc; @@ -5383,9 +5513,12 @@ FRESULT f_setlabel ( DWORD dc; #endif - /* Get logical drive */ + /* Get logical drive and mount the volume if needed */ res = mount_volume(&label, &fs, FA_WRITE); if (res != FR_OK) LEAVE_FF(fs, res); +#if FF_STR_VOLUME_ID == 2 + for ( ; *label == '/'; label++) ; /* Snip the separators off */ +#endif #if FF_FS_EXFAT if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */ @@ -5397,13 +5530,13 @@ FRESULT f_setlabel ( if (dc == 0xFFFFFFFF || di >= 10) { /* Wrong surrogate or buffer overflow */ dc = 0; } else { - st_word(dirvn + di * 2, (WCHAR)(dc >> 16)); di++; + st_16(dirvn + di * 2, (WCHAR)(dc >> 16)); di++; } } if (dc == 0 || strchr(&badchr[7], (int)dc) || di >= 11) { /* Check validity of the volume label */ LEAVE_FF(fs, FR_INVALID_NAME); } - st_word(dirvn + di * 2, (WCHAR)dc); di++; + st_16(dirvn + di * 2, (WCHAR)dc); di++; } } else #endif @@ -5530,14 +5663,20 @@ FRESULT f_expand ( for (;;) { /* Find a contiguous cluster block */ n = get_fat(&fp->obj, clst); if (++clst >= fs->n_fatent) clst = 2; - if (n == 1) { res = FR_INT_ERR; break; } - if (n == 0xFFFFFFFF) { res = FR_DISK_ERR; break; } + if (n == 1) { + res = FR_INT_ERR; break; + } + if (n == 0xFFFFFFFF) { + res = FR_DISK_ERR; break; + } if (n == 0) { /* Is it a free cluster? */ if (++ncl == tcl) break; /* Break if a contiguous cluster block is found */ } else { scl = clst; ncl = 0; /* Not a free cluster */ } - if (clst == stcl) { res = FR_DENIED; break; } /* No contiguous cluster? */ + if (clst == stcl) { /* No contiguous cluster? */ + res = FR_DENIED; break; + } } if (res == FR_OK) { /* A contiguous free area is found */ if (opt) { /* Allocate it now */ @@ -5651,7 +5790,7 @@ FRESULT f_forward ( #define N_SEC_TRACK 63 /* Sectors per track for determination of drive CHS */ #define GPT_ALIGN 0x100000 /* Alignment of partitions in GPT [byte] (>=128KB) */ -#define GPT_ITEMS 128 /* Number of GPT table size (>=128, sector aligned) */ +#define GPT_ITEMS 128 /* Number of GPT table items (>=128, sector aligned) */ /* Create partitions on the physical drive in format of MBR or GPT */ @@ -5659,8 +5798,8 @@ FRESULT f_forward ( static FRESULT create_partition ( BYTE drv, /* Physical drive number */ const LBA_t plst[], /* Partition list */ - BYTE sys, /* System ID (for only MBR, temp setting) */ - BYTE* buf /* Working buffer for a sector */ + BYTE sys, /* System ID for each partition (for only MBR) */ + BYTE *buf /* Working buffer for a sector */ ) { UINT i, cy; @@ -5689,19 +5828,19 @@ static FRESULT create_partition ( rnd = (DWORD)sz_drv + GET_FATTIME(); /* Random seed */ align = GPT_ALIGN / ss; /* Partition alignment for GPT [sector] */ sz_ptbl = GPT_ITEMS * SZ_GPTE / ss; /* Size of partition table [sector] */ - top_bpt = sz_drv - sz_ptbl - 1; /* Backup partiiton table start sector */ - nxt_alloc = 2 + sz_ptbl; /* First allocatable sector */ - sz_pool = top_bpt - nxt_alloc; /* Size of allocatable area */ + top_bpt = sz_drv - sz_ptbl - 1; /* Backup partition table start LBA */ + nxt_alloc = 2 + sz_ptbl; /* First allocatable LBA */ + sz_pool = top_bpt - nxt_alloc; /* Size of allocatable area [sector] */ bcc = 0xFFFFFFFF; sz_part = 1; - pi = si = 0; /* partition table index, size table index */ + pi = si = 0; /* partition table index, map index */ do { if (pi * SZ_GPTE % ss == 0) memset(buf, 0, ss); /* Clean the buffer if needed */ if (sz_part != 0) { /* Is the size table not termintated? */ - nxt_alloc = (nxt_alloc + align - 1) & ((QWORD)0 - align); /* Align partition start */ + nxt_alloc = (nxt_alloc + align - 1) & ((QWORD)0 - align); /* Align partition start LBA */ sz_part = plst[si++]; /* Get a partition size */ if (sz_part <= 100) { /* Is the size in percentage? */ - sz_part = sz_pool * sz_part / 100; - sz_part = (sz_part + align - 1) & ((QWORD)0 - align); /* Align partition end (only if in percentage) */ + sz_part = sz_pool * sz_part / 100; /* Sectors in percentage */ + sz_part = (sz_part + align - 1) & ((QWORD)0 - align); /* Align partition end LBA (only if in percentage) */ } if (nxt_alloc + sz_part > top_bpt) { /* Clip the size at end of the pool */ sz_part = (nxt_alloc < top_bpt) ? top_bpt - nxt_alloc : 0; @@ -5711,11 +5850,11 @@ static FRESULT create_partition ( ofs = pi * SZ_GPTE % ss; memcpy(buf + ofs + GPTE_PtGuid, GUID_MS_Basic, 16); /* Set partition GUID (Microsoft Basic Data) */ rnd = make_rand(rnd, buf + ofs + GPTE_UpGuid, 16); /* Set unique partition GUID */ - st_qword(buf + ofs + GPTE_FstLba, nxt_alloc); /* Set partition start sector */ - st_qword(buf + ofs + GPTE_LstLba, nxt_alloc + sz_part - 1); /* Set partition end sector */ - nxt_alloc += sz_part; /* Next allocatable sector */ + st_64(buf + ofs + GPTE_FstLba, nxt_alloc); /* Set partition start LBA */ + st_64(buf + ofs + GPTE_LstLba, nxt_alloc + sz_part - 1); /* Set partition end LBA */ + nxt_alloc += sz_part; /* Next allocatable LBA */ } - if ((pi + 1) * SZ_GPTE % ss == 0) { /* Write the buffer if it is filled up */ + if ((pi + 1) * SZ_GPTE % ss == 0) { /* Write the sector buffer if it is filled up */ for (i = 0; i < ss; bcc = crc32(bcc, buf[i++])) ; /* Calculate table check sum */ if (disk_write(drv, buf, 2 + pi * SZ_GPTE / ss, 1) != RES_OK) return FR_DISK_ERR; /* Write to primary table */ if (disk_write(drv, buf, top_bpt + pi * SZ_GPTE / ss, 1) != RES_OK) return FR_DISK_ERR; /* Write to secondary table */ @@ -5725,32 +5864,32 @@ static FRESULT create_partition ( /* Create primary GPT header */ memset(buf, 0, ss); memcpy(buf + GPTH_Sign, "EFI PART" "\0\0\1\0" "\x5C\0\0", 16); /* Signature, version (1.0) and size (92) */ - st_dword(buf + GPTH_PtBcc, ~bcc); /* Table check sum */ - st_qword(buf + GPTH_CurLba, 1); /* LBA of this header */ - st_qword(buf + GPTH_BakLba, sz_drv - 1); /* LBA of secondary header */ - st_qword(buf + GPTH_FstLba, 2 + sz_ptbl); /* LBA of first allocatable sector */ - st_qword(buf + GPTH_LstLba, top_bpt - 1); /* LBA of last allocatable sector */ - st_dword(buf + GPTH_PteSize, SZ_GPTE); /* Size of a table entry */ - st_dword(buf + GPTH_PtNum, GPT_ITEMS); /* Number of table entries */ - st_dword(buf + GPTH_PtOfs, 2); /* LBA of this table */ + st_32(buf + GPTH_PtBcc, ~bcc); /* Table check sum */ + st_64(buf + GPTH_CurLba, 1); /* LBA of this header */ + st_64(buf + GPTH_BakLba, sz_drv - 1); /* LBA of secondary header */ + st_64(buf + GPTH_FstLba, 2 + sz_ptbl); /* LBA of first allocatable sector */ + st_64(buf + GPTH_LstLba, top_bpt - 1); /* LBA of last allocatable sector */ + st_32(buf + GPTH_PteSize, SZ_GPTE); /* Size of a table entry */ + st_32(buf + GPTH_PtNum, GPT_ITEMS); /* Number of table entries */ + st_32(buf + GPTH_PtOfs, 2); /* LBA of this table */ rnd = make_rand(rnd, buf + GPTH_DskGuid, 16); /* Disk GUID */ for (i = 0, bcc= 0xFFFFFFFF; i < 92; bcc = crc32(bcc, buf[i++])) ; /* Calculate header check sum */ - st_dword(buf + GPTH_Bcc, ~bcc); /* Header check sum */ + st_32(buf + GPTH_Bcc, ~bcc); /* Header check sum */ if (disk_write(drv, buf, 1, 1) != RES_OK) return FR_DISK_ERR; /* Create secondary GPT header */ - st_qword(buf + GPTH_CurLba, sz_drv - 1); /* LBA of this header */ - st_qword(buf + GPTH_BakLba, 1); /* LBA of primary header */ - st_qword(buf + GPTH_PtOfs, top_bpt); /* LBA of this table */ - st_dword(buf + GPTH_Bcc, 0); + st_64(buf + GPTH_CurLba, sz_drv - 1); /* LBA of this header */ + st_64(buf + GPTH_BakLba, 1); /* LBA of primary header */ + st_64(buf + GPTH_PtOfs, top_bpt); /* LBA of this table */ + st_32(buf + GPTH_Bcc, 0); for (i = 0, bcc= 0xFFFFFFFF; i < 92; bcc = crc32(bcc, buf[i++])) ; /* Calculate header check sum */ - st_dword(buf + GPTH_Bcc, ~bcc); /* Header check sum */ + st_32(buf + GPTH_Bcc, ~bcc); /* Header check sum */ if (disk_write(drv, buf, sz_drv - 1, 1) != RES_OK) return FR_DISK_ERR; /* Create protective MBR */ memset(buf, 0, ss); - memcpy(buf + MBR_Table, gpt_mbr, 16); /* Create a GPT partition */ - st_word(buf + BS_55AA, 0xAA55); + memcpy(buf + MBR_Table, gpt_mbr, 16); /* Create a GPT partition */ + st_16(buf + BS_55AA, 0xAA55); if (disk_write(drv, buf, 0, 1) != RES_OK) return FR_DISK_ERR; } else @@ -5769,20 +5908,20 @@ static FRESULT create_partition ( if (nxt_alloc32 + sz_part32 > sz_drv32 || nxt_alloc32 + sz_part32 < nxt_alloc32) sz_part32 = sz_drv32 - nxt_alloc32; /* Clip at drive size */ if (sz_part32 == 0) break; /* End of table or no sector to allocate? */ - st_dword(pte + PTE_StLba, nxt_alloc32); /* Start LBA */ - st_dword(pte + PTE_SizLba, sz_part32); /* Number of sectors */ + st_32(pte + PTE_StLba, nxt_alloc32); /* Partition start LBA sector */ + st_32(pte + PTE_SizLba, sz_part32); /* Size of partition [sector] */ pte[PTE_System] = sys; /* System type */ - cy = (UINT)(nxt_alloc32 / n_sc / n_hd); /* Start cylinder */ - hd = (BYTE)(nxt_alloc32 / n_sc % n_hd); /* Start head */ - sc = (BYTE)(nxt_alloc32 % n_sc + 1); /* Start sector */ + cy = (UINT)(nxt_alloc32 / n_sc / n_hd); /* Partitio start CHS cylinder */ + hd = (BYTE)(nxt_alloc32 / n_sc % n_hd); /* Partition start CHS head */ + sc = (BYTE)(nxt_alloc32 % n_sc + 1); /* Partition start CHS sector */ pte[PTE_StHead] = hd; pte[PTE_StSec] = (BYTE)((cy >> 2 & 0xC0) | sc); pte[PTE_StCyl] = (BYTE)cy; - cy = (UINT)((nxt_alloc32 + sz_part32 - 1) / n_sc / n_hd); /* End cylinder */ - hd = (BYTE)((nxt_alloc32 + sz_part32 - 1) / n_sc % n_hd); /* End head */ - sc = (BYTE)((nxt_alloc32 + sz_part32 - 1) % n_sc + 1); /* End sector */ + cy = (UINT)((nxt_alloc32 + sz_part32 - 1) / n_sc / n_hd); /* Partition end CHS cylinder */ + hd = (BYTE)((nxt_alloc32 + sz_part32 - 1) / n_sc % n_hd); /* Partition end CHS head */ + sc = (BYTE)((nxt_alloc32 + sz_part32 - 1) % n_sc + 1); /* Partition end CHS sector */ pte[PTE_EdHead] = hd; pte[PTE_EdSec] = (BYTE)((cy >> 2 & 0xC0) | sc); pte[PTE_EdCyl] = (BYTE)cy; @@ -5790,7 +5929,7 @@ static FRESULT create_partition ( pte += SZ_PTE; /* Next entry */ } - st_word(buf + BS_55AA, 0xAA55); /* MBR signature */ + st_16(buf + BS_55AA, 0xAA55); /* MBR signature */ if (disk_write(drv, buf, 0, 1) != RES_OK) return FR_DISK_ERR; /* Write it to the MBR */ } @@ -5802,46 +5941,51 @@ static FRESULT create_partition ( FRESULT f_mkfs ( const TCHAR* path, /* Logical drive number */ const MKFS_PARM* opt, /* Format options */ - void* work, /* Pointer to working buffer (null: use heap memory) */ + void* work, /* Pointer to working buffer (null: use len bytes of heap memory) */ UINT len /* Size of working buffer [byte] */ ) { - static const WORD cst[] = {1, 4, 16, 64, 256, 512, 0}; /* Cluster size boundary for FAT volume (4Ks unit) */ - static const WORD cst32[] = {1, 2, 4, 8, 16, 32, 0}; /* Cluster size boundary for FAT32 volume (128Ks unit) */ + static const WORD cst[] = {1, 4, 16, 64, 256, 512, 0}; /* Cluster size boundary for FAT volume (4K sector unit) */ + static const WORD cst32[] = {1, 2, 4, 8, 16, 32, 0}; /* Cluster size boundary for FAT32 volume (128K sector unit) */ static const MKFS_PARM defopt = {FM_ANY, 0, 0, 0, 0}; /* Default parameter */ - BYTE fsopt, fsty, sys, *buf, *pte, pdrv, ipart; + BYTE fsopt, fsty, sys, pdrv, ipart; + BYTE *buf; + BYTE *pte; WORD ss; /* Sector size */ DWORD sz_buf, sz_blk, n_clst, pau, nsect, n, vsn; - LBA_t sz_vol, b_vol, b_fat, b_data; /* Size of volume, Base LBA of volume, fat, data */ + LBA_t sz_vol, b_vol, b_fat, b_data; /* Volume size, base LBA of volume, base LBA of FAT and base LBA of data */ LBA_t sect, lba[2]; - DWORD sz_rsv, sz_fat, sz_dir, sz_au; /* Size of reserved, fat, dir, data, cluster */ - UINT n_fat, n_root, i; /* Index, Number of FATs and Number of roor dir entries */ + DWORD sz_rsv, sz_fat, sz_dir, sz_au; /* Size of reserved area, FAT area, directry area, data area and cluster */ + UINT n_fat, n_root, i; /* Number of FATs, number of roor directory entries and some index */ int vol; DSTATUS ds; - FRESULT fr; + FRESULT res; /* Check mounted drive and clear work area */ - vol = get_ldnumber(&path); /* Get target logical drive */ + vol = get_ldnumber(&path); /* Get logical drive number to be formatted */ if (vol < 0) return FR_INVALID_DRIVE; if (FatFs[vol]) FatFs[vol]->fs_type = 0; /* Clear the fs object if mounted */ - pdrv = LD2PD(vol); /* Physical drive */ - ipart = LD2PT(vol); /* Partition (0:create as new, 1..:get from partition table) */ - if (!opt) opt = &defopt; /* Use default parameter if it is not given */ + pdrv = LD2PD(vol); /* Hosting physical drive */ + ipart = LD2PT(vol); /* Hosting partition (0:create as new, 1..:existing partition) */ - /* Get physical drive status (sz_drv, sz_blk, ss) */ + /* Initialize the hosting physical drive */ ds = disk_initialize(pdrv); if (ds & STA_NOINIT) return FR_NOT_READY; if (ds & STA_PROTECT) return FR_WRITE_PROTECTED; + + /* Get physical drive parameters (sz_drv, sz_blk and ss) */ + if (!opt) opt = &defopt; /* Use default parameter if it is not given */ sz_blk = opt->align; - if (sz_blk == 0 && disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_blk) != RES_OK) sz_blk = 1; - if (sz_blk == 0 || sz_blk > 0x8000 || (sz_blk & (sz_blk - 1))) sz_blk = 1; + if (sz_blk == 0) disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_blk); /* Block size from the parameter or lower layer */ + if (sz_blk == 0 || sz_blk > 0x8000 || (sz_blk & (sz_blk - 1))) sz_blk = 1; /* Use default if the block size is invalid */ #if FF_MAX_SS != FF_MIN_SS if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &ss) != RES_OK) return FR_DISK_ERR; if (ss > FF_MAX_SS || ss < FF_MIN_SS || (ss & (ss - 1))) return FR_DISK_ERR; #else ss = FF_MAX_SS; #endif + /* Options for FAT sub-type and FAT parameters */ fsopt = opt->fmt & (FM_ANY | FM_SFD); n_fat = (opt->n_fat >= 1 && opt->n_fat <= 2) ? opt->n_fat : 1; @@ -5863,7 +6007,7 @@ FRESULT f_mkfs ( if (FF_MULTI_PARTITION && ipart != 0) { /* Is the volume associated with any specific partition? */ /* Get partition location from the existing partition table */ if (disk_read(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Load MBR */ - if (ld_word(buf + BS_55AA) != 0xAA55) LEAVE_MKFS(FR_MKFS_ABORTED); /* Check if MBR is valid */ + if (ld_16(buf + BS_55AA) != 0xAA55) LEAVE_MKFS(FR_MKFS_ABORTED); /* Check if MBR is valid */ #if FF_LBA64 if (buf[MBR_Table + PTE_System] == 0xEE) { /* GPT protective MBR? */ DWORD n_ent, ofs; @@ -5872,14 +6016,14 @@ FRESULT f_mkfs ( /* Get the partition location from GPT */ if (disk_read(pdrv, buf, 1, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Load GPT header sector (next to MBR) */ if (!test_gpt_header(buf)) LEAVE_MKFS(FR_MKFS_ABORTED); /* Check if GPT header is valid */ - n_ent = ld_dword(buf + GPTH_PtNum); /* Number of entries */ - pt_lba = ld_qword(buf + GPTH_PtOfs); /* Table start sector */ + n_ent = ld_32(buf + GPTH_PtNum); /* Number of entries */ + pt_lba = ld_64(buf + GPTH_PtOfs); /* Table start sector */ ofs = i = 0; while (n_ent) { /* Find MS Basic partition with order of ipart */ if (ofs == 0 && disk_read(pdrv, buf, pt_lba++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Get PT sector */ if (!memcmp(buf + ofs + GPTE_PtGuid, GUID_MS_Basic, 16) && ++i == ipart) { /* MS basic data partition? */ - b_vol = ld_qword(buf + ofs + GPTE_FstLba); - sz_vol = ld_qword(buf + ofs + GPTE_LstLba) - b_vol + 1; + b_vol = ld_64(buf + ofs + GPTE_FstLba); + sz_vol = ld_64(buf + ofs + GPTE_LstLba) - b_vol + 1; break; } n_ent--; ofs = (ofs + SZ_GPTE) % ss; /* Next entry */ @@ -5891,8 +6035,8 @@ FRESULT f_mkfs ( { /* Get the partition location from MBR partition table */ pte = buf + (MBR_Table + (ipart - 1) * SZ_PTE); if (ipart > 4 || pte[PTE_System] == 0) LEAVE_MKFS(FR_MKFS_ABORTED); /* No partition? */ - b_vol = ld_dword(pte + PTE_StLba); /* Get volume start sector */ - sz_vol = ld_dword(pte + PTE_SizLba); /* Get volume size */ + b_vol = ld_32(pte + PTE_StLba); /* Get volume start sector */ + sz_vol = ld_32(pte + PTE_SizLba); /* Get volume size */ } } else { /* The volume is associated with a physical drive */ if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_vol) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); @@ -5911,13 +6055,13 @@ FRESULT f_mkfs ( } } } - if (sz_vol < 128) LEAVE_MKFS(FR_MKFS_ABORTED); /* Check if volume size is >=128s */ + if (sz_vol < 128) LEAVE_MKFS(FR_MKFS_ABORTED); /* Check if volume size is >=128 sectors */ /* Now start to create an FAT volume at b_vol and sz_vol */ do { /* Pre-determine the FAT type */ if (FF_FS_EXFAT && (fsopt & FM_EXFAT)) { /* exFAT possible? */ - if ((fsopt & FM_ANY) == FM_EXFAT || sz_vol >= 0x4000000 || sz_au > 128) { /* exFAT only, vol >= 64MS or sz_au > 128S ? */ + if ((fsopt & FM_ANY) == FM_EXFAT || sz_vol >= 0x4000000 || sz_au > 128) { /* exFAT only, vol >= 64M sectors or sz_au > 128 sectors? */ fsty = FS_EXFAT; break; } } @@ -5934,7 +6078,7 @@ FRESULT f_mkfs ( fsty = FS_FAT16; } while (0); - vsn = (DWORD)sz_vol + GET_FATTIME(); /* VSN generated from current time and partitiion size */ + vsn = (DWORD)sz_vol + GET_FATTIME(); /* VSN generated from current time and partition size */ #if FF_FS_EXFAT if (fsty == FS_EXFAT) { /* Create an exFAT volume */ @@ -5957,7 +6101,7 @@ FRESULT f_mkfs ( sz_fat = (DWORD)((sz_vol / sz_au + 2) * 4 + ss - 1) / ss; /* Number of FAT sectors */ b_data = (b_fat + sz_fat + sz_blk - 1) & ~((LBA_t)sz_blk - 1); /* Align data area to the erase block boundary */ if (b_data - b_vol >= sz_vol / 2) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume? */ - n_clst = (DWORD)(sz_vol - (b_data - b_vol)) / sz_au; /* Number of clusters */ + n_clst = (DWORD)((sz_vol - (b_data - b_vol)) / sz_au); /* Number of clusters */ if (n_clst <16) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too few clusters? */ if (n_clst > MAX_EXFAT) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too many clusters? */ @@ -6000,7 +6144,7 @@ FRESULT f_mkfs ( } } while (si); clen[1] = (szb_case + sz_au * ss - 1) / (sz_au * ss); /* Number of up-case table clusters */ - clen[2] = 1; /* Number of root dir clusters */ + clen[2] = 1; /* Number of root directory clusters */ /* Initialize the allocation bitmap */ sect = b_data; nsect = (szb_bit + ss - 1) / ss; /* Start of bitmap and number of bitmap sectors */ @@ -6019,12 +6163,13 @@ FRESULT f_mkfs ( do { memset(buf, 0, sz_buf * ss); i = 0; /* Clear work area and reset write offset */ if (clu == 0) { /* Initialize FAT [0] and FAT[1] */ - st_dword(buf + i, 0xFFFFFFF8); i += 4; clu++; - st_dword(buf + i, 0xFFFFFFFF); i += 4; clu++; + st_32(buf + i, 0xFFFFFFF8); i += 4; clu++; + st_32(buf + i, 0xFFFFFFFF); i += 4; clu++; } - do { /* Create chains of bitmap, up-case and root dir */ + + do { /* Create chains of bitmap, up-case and root directory */ while (nbit != 0 && i < sz_buf * ss) { /* Create a chain */ - st_dword(buf + i, (nbit > 1) ? clu + 1 : 0xFFFFFFFF); + st_32(buf + i, (nbit > 1) ? clu + 1 : 0xFFFFFFFF); i += 4; clu++; nbit--; } if (nbit == 0 && j < 3) nbit = clen[j++]; /* Get next chain length */ @@ -6036,14 +6181,14 @@ FRESULT f_mkfs ( /* Initialize the root directory */ memset(buf, 0, sz_buf * ss); - buf[SZDIRE * 0 + 0] = ET_VLABEL; /* Volume label entry (no label) */ - buf[SZDIRE * 1 + 0] = ET_BITMAP; /* Bitmap entry */ - st_dword(buf + SZDIRE * 1 + 20, 2); /* cluster */ - st_dword(buf + SZDIRE * 1 + 24, szb_bit); /* size */ - buf[SZDIRE * 2 + 0] = ET_UPCASE; /* Up-case table entry */ - st_dword(buf + SZDIRE * 2 + 4, sum); /* sum */ - st_dword(buf + SZDIRE * 2 + 20, 2 + clen[0]); /* cluster */ - st_dword(buf + SZDIRE * 2 + 24, szb_case); /* size */ + buf[SZDIRE * 0 + 0] = ET_VLABEL; /* Volume label entry (no label) */ + buf[SZDIRE * 1 + 0] = ET_BITMAP; /* Bitmap entry */ + st_32(buf + SZDIRE * 1 + 20, 2); /* cluster */ + st_32(buf + SZDIRE * 1 + 24, szb_bit); /* size */ + buf[SZDIRE * 2 + 0] = ET_UPCASE; /* Up-case table entry */ + st_32(buf + SZDIRE * 2 + 4, sum); /* sum */ + st_32(buf + SZDIRE * 2 + 20, 2 + clen[0]); /* cluster */ + st_32(buf + SZDIRE * 2 + 24, szb_case); /* size */ sect = b_data + sz_au * (clen[0] + clen[1]); nsect = sz_au; /* Start of the root directory and number of sectors */ do { /* Fill root directory sectors */ n = (nsect > sz_buf) ? sz_buf : nsect; @@ -6058,28 +6203,28 @@ FRESULT f_mkfs ( /* Main record (+0) */ memset(buf, 0, ss); memcpy(buf + BS_JmpBoot, "\xEB\x76\x90" "EXFAT ", 11); /* Boot jump code (x86), OEM name */ - st_qword(buf + BPB_VolOfsEx, b_vol); /* Volume offset in the physical drive [sector] */ - st_qword(buf + BPB_TotSecEx, sz_vol); /* Volume size [sector] */ - st_dword(buf + BPB_FatOfsEx, (DWORD)(b_fat - b_vol)); /* FAT offset [sector] */ - st_dword(buf + BPB_FatSzEx, sz_fat); /* FAT size [sector] */ - st_dword(buf + BPB_DataOfsEx, (DWORD)(b_data - b_vol)); /* Data offset [sector] */ - st_dword(buf + BPB_NumClusEx, n_clst); /* Number of clusters */ - st_dword(buf + BPB_RootClusEx, 2 + clen[0] + clen[1]); /* Root dir cluster # */ - st_dword(buf + BPB_VolIDEx, vsn); /* VSN */ - st_word(buf + BPB_FSVerEx, 0x100); /* Filesystem version (1.00) */ - for (buf[BPB_BytsPerSecEx] = 0, i = ss; i >>= 1; buf[BPB_BytsPerSecEx]++) ; /* Log2 of sector size [byte] */ + st_64(buf + BPB_VolOfsEx, b_vol); /* Volume offset in the physical drive [sector] */ + st_64(buf + BPB_TotSecEx, sz_vol); /* Volume size [sector] */ + st_32(buf + BPB_FatOfsEx, (DWORD)(b_fat - b_vol)); /* FAT offset [sector] */ + st_32(buf + BPB_FatSzEx, sz_fat); /* FAT size [sector] */ + st_32(buf + BPB_DataOfsEx, (DWORD)(b_data - b_vol)); /* Data offset [sector] */ + st_32(buf + BPB_NumClusEx, n_clst); /* Number of clusters */ + st_32(buf + BPB_RootClusEx, 2 + clen[0] + clen[1]); /* Root directory cluster number */ + st_32(buf + BPB_VolIDEx, vsn); /* VSN */ + st_16(buf + BPB_FSVerEx, 0x100); /* Filesystem version (1.00) */ + for (buf[BPB_BytsPerSecEx] = 0, i = ss; i >>= 1; buf[BPB_BytsPerSecEx]++) ; /* Log2 of sector size [byte] */ for (buf[BPB_SecPerClusEx] = 0, i = sz_au; i >>= 1; buf[BPB_SecPerClusEx]++) ; /* Log2 of cluster size [sector] */ buf[BPB_NumFATsEx] = 1; /* Number of FATs */ buf[BPB_DrvNumEx] = 0x80; /* Drive number (for int13) */ - st_word(buf + BS_BootCodeEx, 0xFEEB); /* Boot code (x86) */ - st_word(buf + BS_55AA, 0xAA55); /* Signature (placed here regardless of sector size) */ + st_16(buf + BS_BootCodeEx, 0xFEEB); /* Boot code (x86) */ + st_16(buf + BS_55AA, 0xAA55); /* Signature (placed here regardless of sector size) */ for (i = sum = 0; i < ss; i++) { /* VBR checksum */ if (i != BPB_VolFlagEx && i != BPB_VolFlagEx + 1 && i != BPB_PercInUseEx) sum = xsum32(buf[i], sum); } if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Extended bootstrap record (+1..+8) */ memset(buf, 0, ss); - st_word(buf + ss - 2, 0xAA55); /* Signature (placed at end of sector) */ + st_16(buf + ss - 2, 0xAA55); /* Signature (placed at end of sector) */ for (j = 1; j < 9; j++) { for (i = 0; i < ss; sum = xsum32(buf[i++], sum)) ; /* VBR checksum */ if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); @@ -6091,7 +6236,7 @@ FRESULT f_mkfs ( if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); } /* Sum record (+11) */ - for (i = 0; i < ss; i += 4) st_dword(buf + i, sum); /* Fill with checksum value */ + for (i = 0; i < ss; i += 4) st_32(buf + i, sum); /* Fill with checksum value */ if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); } @@ -6125,7 +6270,7 @@ FRESULT f_mkfs ( } sz_fat = (n + ss - 1) / ss; /* FAT size [sector] */ sz_rsv = 1; /* Number of reserved sectors */ - sz_dir = (DWORD)n_root * SZDIRE / ss; /* Root dir size [sector] */ + sz_dir = (DWORD)n_root * SZDIRE / ss; /* Root directory size [sector] */ } b_fat = b_vol + sz_rsv; /* FAT base */ b_data = b_fat + sz_fat * n_fat + sz_dir; /* Data base */ @@ -6179,48 +6324,48 @@ FRESULT f_mkfs ( /* Create FAT VBR */ memset(buf, 0, ss); memcpy(buf + BS_JmpBoot, "\xEB\xFE\x90" "MSDOS5.0", 11); /* Boot jump code (x86), OEM name */ - st_word(buf + BPB_BytsPerSec, ss); /* Sector size [byte] */ + st_16(buf + BPB_BytsPerSec, ss); /* Sector size [byte] */ buf[BPB_SecPerClus] = (BYTE)pau; /* Cluster size [sector] */ - st_word(buf + BPB_RsvdSecCnt, (WORD)sz_rsv); /* Size of reserved area */ + st_16(buf + BPB_RsvdSecCnt, (WORD)sz_rsv); /* Size of reserved area */ buf[BPB_NumFATs] = (BYTE)n_fat; /* Number of FATs */ - st_word(buf + BPB_RootEntCnt, (WORD)((fsty == FS_FAT32) ? 0 : n_root)); /* Number of root directory entries */ + st_16(buf + BPB_RootEntCnt, (WORD)((fsty == FS_FAT32) ? 0 : n_root)); /* Number of root directory entries */ if (sz_vol < 0x10000) { - st_word(buf + BPB_TotSec16, (WORD)sz_vol); /* Volume size in 16-bit LBA */ + st_16(buf + BPB_TotSec16, (WORD)sz_vol); /* Volume size in 16-bit LBA */ } else { - st_dword(buf + BPB_TotSec32, (DWORD)sz_vol); /* Volume size in 32-bit LBA */ + st_32(buf + BPB_TotSec32, (DWORD)sz_vol); /* Volume size in 32-bit LBA */ } buf[BPB_Media] = 0xF8; /* Media descriptor byte */ - st_word(buf + BPB_SecPerTrk, 63); /* Number of sectors per track (for int13) */ - st_word(buf + BPB_NumHeads, 255); /* Number of heads (for int13) */ - st_dword(buf + BPB_HiddSec, (DWORD)b_vol); /* Volume offset in the physical drive [sector] */ + st_16(buf + BPB_SecPerTrk, 63); /* Number of sectors per track (for int13) */ + st_16(buf + BPB_NumHeads, 255); /* Number of heads (for int13) */ + st_32(buf + BPB_HiddSec, (DWORD)b_vol); /* Volume offset in the physical drive [sector] */ if (fsty == FS_FAT32) { - st_dword(buf + BS_VolID32, vsn); /* VSN */ - st_dword(buf + BPB_FATSz32, sz_fat); /* FAT size [sector] */ - st_dword(buf + BPB_RootClus32, 2); /* Root directory cluster # (2) */ - st_word(buf + BPB_FSInfo32, 1); /* Offset of FSINFO sector (VBR + 1) */ - st_word(buf + BPB_BkBootSec32, 6); /* Offset of backup VBR (VBR + 6) */ + st_32(buf + BS_VolID32, vsn); /* VSN */ + st_32(buf + BPB_FATSz32, sz_fat); /* FAT size [sector] */ + st_32(buf + BPB_RootClus32, 2); /* Root directory cluster # (2) */ + st_16(buf + BPB_FSInfo32, 1); /* Offset of FSINFO sector (VBR + 1) */ + st_16(buf + BPB_BkBootSec32, 6); /* Offset of backup VBR (VBR + 6) */ buf[BS_DrvNum32] = 0x80; /* Drive number (for int13) */ buf[BS_BootSig32] = 0x29; /* Extended boot signature */ memcpy(buf + BS_VolLab32, "NO NAME " "FAT32 ", 19); /* Volume label, FAT signature */ } else { - st_dword(buf + BS_VolID, vsn); /* VSN */ - st_word(buf + BPB_FATSz16, (WORD)sz_fat); /* FAT size [sector] */ + st_32(buf + BS_VolID, vsn); /* VSN */ + st_16(buf + BPB_FATSz16, (WORD)sz_fat); /* FAT size [sector] */ buf[BS_DrvNum] = 0x80; /* Drive number (for int13) */ buf[BS_BootSig] = 0x29; /* Extended boot signature */ memcpy(buf + BS_VolLab, "NO NAME " "FAT ", 19); /* Volume label, FAT signature */ } - st_word(buf + BS_55AA, 0xAA55); /* Signature (offset is fixed here regardless of sector size) */ + st_16(buf + BS_55AA, 0xAA55); /* Signature (offset is fixed here regardless of sector size) */ if (disk_write(pdrv, buf, b_vol, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Write it to the VBR sector */ /* Create FSINFO record if needed */ if (fsty == FS_FAT32) { disk_write(pdrv, buf, b_vol + 6, 1); /* Write backup VBR (VBR + 6) */ memset(buf, 0, ss); - st_dword(buf + FSI_LeadSig, 0x41615252); - st_dword(buf + FSI_StrucSig, 0x61417272); - st_dword(buf + FSI_Free_Count, n_clst - 1); /* Number of free clusters */ - st_dword(buf + FSI_Nxt_Free, 2); /* Last allocated cluster# */ - st_word(buf + BS_55AA, 0xAA55); + st_32(buf + FSI_LeadSig, 0x41615252); + st_32(buf + FSI_StrucSig, 0x61417272); + st_32(buf + FSI_Free_Count, n_clst - 1); /* Number of free clusters */ + st_32(buf + FSI_Nxt_Free, 2); /* Last allocated cluster# */ + st_16(buf + BS_55AA, 0xAA55); disk_write(pdrv, buf, b_vol + 7, 1); /* Write backup FSINFO (VBR + 7) */ disk_write(pdrv, buf, b_vol + 1, 1); /* Write original FSINFO (VBR + 1) */ } @@ -6230,17 +6375,17 @@ FRESULT f_mkfs ( sect = b_fat; /* FAT start sector */ for (i = 0; i < n_fat; i++) { /* Initialize FATs each */ if (fsty == FS_FAT32) { - st_dword(buf + 0, 0xFFFFFFF8); /* FAT[0] */ - st_dword(buf + 4, 0xFFFFFFFF); /* FAT[1] */ - st_dword(buf + 8, 0x0FFFFFFF); /* FAT[2] (root directory) */ + st_32(buf + 0, 0xFFFFFFF8); /* FAT[0] */ + st_32(buf + 4, 0xFFFFFFFF); /* FAT[1] */ + st_32(buf + 8, 0x0FFFFFFF); /* FAT[2] (root directory at cluster# 2) */ } else { - st_dword(buf + 0, (fsty == FS_FAT12) ? 0xFFFFF8 : 0xFFFFFFF8); /* FAT[0] and FAT[1] */ + st_32(buf + 0, (fsty == FS_FAT12) ? 0xFFFFF8 : 0xFFFFFFF8); /* FAT[0] and FAT[1] */ } nsect = sz_fat; /* Number of FAT sectors */ do { /* Fill FAT sectors */ n = (nsect > sz_buf) ? sz_buf : nsect; if (disk_write(pdrv, buf, sect, (UINT)n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); - memset(buf, 0, ss); /* Rest of FAT all are cleared */ + memset(buf, 0, ss); /* Rest of FAT area is initially zero */ sect += n; nsect -= n; } while (nsect); } @@ -6258,32 +6403,30 @@ FRESULT f_mkfs ( /* Determine system ID in the MBR partition table */ if (FF_FS_EXFAT && fsty == FS_EXFAT) { - sys = 0x07; /* exFAT */ + sys = 0x07; /* exFAT */ + } else if (fsty == FS_FAT32) { + sys = 0x0C; /* FAT32X */ + } else if (sz_vol >= 0x10000) { + sys = 0x06; /* FAT12/16 (large) */ + } else if (fsty == FS_FAT16) { + sys = 0x04; /* FAT16 */ } else { - if (fsty == FS_FAT32) { - sys = 0x0C; /* FAT32X */ - } else { - if (sz_vol >= 0x10000) { - sys = 0x06; /* FAT12/16 (large) */ - } else { - sys = (fsty == FS_FAT16) ? 0x04 : 0x01; /* FAT16 : FAT12 */ - } - } + sys = 0x01; /* FAT12 */ } /* Update partition information */ if (FF_MULTI_PARTITION && ipart != 0) { /* Volume is in the existing partition */ - if (!FF_LBA64 || !(fsopt & 0x80)) { + if (!FF_LBA64 || !(fsopt & 0x80)) { /* Is the partition in MBR? */ /* Update system ID in the partition table */ if (disk_read(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Read the MBR */ buf[MBR_Table + (ipart - 1) * SZ_PTE + PTE_System] = sys; /* Set system ID */ if (disk_write(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Write it back to the MBR */ } } else { /* Volume as a new single partition */ - if (!(fsopt & FM_SFD)) { /* Create partition table if not in SFD */ + if (!(fsopt & FM_SFD)) { /* Create partition table if not in SFD format */ lba[0] = sz_vol; lba[1] = 0; - fr = create_partition(pdrv, lba, sys, buf); - if (fr != FR_OK) LEAVE_MKFS(fr); + res = create_partition(pdrv, lba, sys, buf); + if (res != FR_OK) LEAVE_MKFS(res); } } @@ -6308,17 +6451,22 @@ FRESULT f_fdisk ( { BYTE *buf = (BYTE*)work; DSTATUS stat; + FRESULT res; + /* Initialize the physical drive */ stat = disk_initialize(pdrv); if (stat & STA_NOINIT) return FR_NOT_READY; if (stat & STA_PROTECT) return FR_WRITE_PROTECTED; + #if FF_USE_LFN == 3 if (!buf) buf = ff_memalloc(FF_MAX_SS); /* Use heap memory for working buffer */ #endif if (!buf) return FR_NOT_ENOUGH_CORE; - LEAVE_MKFS(create_partition(pdrv, ptbl, 0x07, buf)); + res = create_partition(pdrv, ptbl, 0x07, buf); /* Create partitions (system ID is temporary setting and determined by f_mkfs) */ + + LEAVE_MKFS(res); } #endif /* FF_MULTI_PARTITION */ @@ -6373,12 +6521,12 @@ TCHAR* f_gets ( #elif FF_STRF_ENCODE == 1 || FF_STRF_ENCODE == 2 /* Read a character in UTF-16LE/BE */ f_read(fp, s, 2, &rc); /* Get a code unit */ if (rc != 2) break; /* EOF? */ - dc = (FF_STRF_ENCODE == 1) ? ld_word(s) : s[0] << 8 | s[1]; + dc = (FF_STRF_ENCODE == 1) ? ld_16(s) : s[0] << 8 | s[1]; if (IsSurrogateL(dc)) continue; /* Broken surrogate pair? */ if (IsSurrogateH(dc)) { /* High surrogate? */ f_read(fp, s, 2, &rc); /* Get low surrogate */ if (rc != 2) break; /* EOF? */ - wc = (FF_STRF_ENCODE == 1) ? ld_word(s) : s[0] << 8 | s[1]; + wc = (FF_STRF_ENCODE == 1) ? ld_16(s) : s[0] << 8 | s[1]; if (!IsSurrogateL(wc)) continue; /* Broken surrogate pair? */ dc = ((dc & 0x3FF) + 0x40) << 10 | (wc & 0x3FF); /* Merge surrogate pair */ } @@ -6388,9 +6536,15 @@ TCHAR* f_gets ( dc = s[0]; if (dc >= 0x80) { /* Multi-byte sequence? */ ct = 0; - if ((dc & 0xE0) == 0xC0) { dc &= 0x1F; ct = 1; } /* 2-byte sequence? */ - if ((dc & 0xF0) == 0xE0) { dc &= 0x0F; ct = 2; } /* 3-byte sequence? */ - if ((dc & 0xF8) == 0xF0) { dc &= 0x07; ct = 3; } /* 4-byte sequence? */ + if ((dc & 0xE0) == 0xC0) { /* 2-byte sequence? */ + dc &= 0x1F; ct = 1; + } + if ((dc & 0xF0) == 0xE0) { /* 3-byte sequence? */ + dc &= 0x0F; ct = 2; + } + if ((dc & 0xF8) == 0xF0) { /* 4-byte sequence? */ + dc &= 0x07; ct = 3; + } if (ct == 0) continue; f_read(fp, s, ct, &rc); /* Get trailing bytes */ if (rc != ct) break; @@ -6402,7 +6556,7 @@ TCHAR* f_gets ( if (rc != ct || dc < 0x80 || IsSurrogate(dc) || dc >= 0x110000) continue; /* Wrong encoding? */ } #endif - /* A code point is avaialble in dc to be output */ + /* A code point is available in dc to be output */ if (FF_USE_STRFUNC == 2 && dc == '\r') continue; /* Strip \r off if needed */ #if FF_LFN_UNICODE == 1 || FF_LFN_UNICODE == 3 /* Output it in UTF-16/32 encoding */ @@ -6417,25 +6571,21 @@ TCHAR* f_gets ( *p++ = (TCHAR)dc; nc++; if (dc == '\n') break; /* End of line? */ - } else { - if (dc < 0x800) { /* 2-byte sequence? */ - *p++ = (TCHAR)(0xC0 | (dc >> 6 & 0x1F)); - *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F)); - nc += 2; - } else { - if (dc < 0x10000) { /* 3-byte sequence? */ - *p++ = (TCHAR)(0xE0 | (dc >> 12 & 0x0F)); - *p++ = (TCHAR)(0x80 | (dc >> 6 & 0x3F)); - *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F)); - nc += 3; - } else { /* 4-byte sequence? */ - *p++ = (TCHAR)(0xF0 | (dc >> 18 & 0x07)); - *p++ = (TCHAR)(0x80 | (dc >> 12 & 0x3F)); - *p++ = (TCHAR)(0x80 | (dc >> 6 & 0x3F)); - *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F)); - nc += 4; - } - } + } else if (dc < 0x800) { /* 2-byte sequence? */ + *p++ = (TCHAR)(0xC0 | (dc >> 6 & 0x1F)); + *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F)); + nc += 2; + } else if (dc < 0x10000) { /* 3-byte sequence? */ + *p++ = (TCHAR)(0xE0 | (dc >> 12 & 0x0F)); + *p++ = (TCHAR)(0x80 | (dc >> 6 & 0x3F)); + *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F)); + nc += 3; + } else { /* 4-byte sequence */ + *p++ = (TCHAR)(0xF0 | (dc >> 18 & 0x07)); + *p++ = (TCHAR)(0x80 | (dc >> 12 & 0x3F)); + *p++ = (TCHAR)(0x80 | (dc >> 6 & 0x3F)); + *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F)); + nc += 4; } #endif } @@ -6471,8 +6621,8 @@ TCHAR* f_gets ( /* Output buffer and work area */ typedef struct { - FIL *fp; /* Ptr to the writing file */ - int idx, nchr; /* Write index of buf[] (-1:error), number of encoding units written */ + FIL *fp; /* Pointer to the writing file */ + int idx, nchr; /* Write index of buf[] (-1:error), number of written encoding units */ #if FF_USE_LFN && FF_LFN_UNICODE == 1 WCHAR hs; #elif FF_USE_LFN && FF_LFN_UNICODE == 2 @@ -6493,7 +6643,7 @@ static void putc_bfd (putbuff* pb, TCHAR c) WCHAR hs, wc; #if FF_LFN_UNICODE == 2 DWORD dc; - const TCHAR *tp; + const TCHAR* tp; #endif #endif @@ -6507,39 +6657,39 @@ static void putc_bfd (putbuff* pb, TCHAR c) #if FF_USE_LFN && FF_LFN_UNICODE #if FF_LFN_UNICODE == 1 /* UTF-16 input */ - if (IsSurrogateH(c)) { /* High surrogate? */ + if (IsSurrogateH(c)) { /* Is this a high-surrogate? */ pb->hs = c; return; /* Save it for next */ } hs = pb->hs; pb->hs = 0; - if (hs != 0) { /* There is a leading high surrogate */ - if (!IsSurrogateL(c)) hs = 0; /* Discard high surrogate if not a surrogate pair */ + if (hs != 0) { /* Is there a leading high-surrogate? */ + if (!IsSurrogateL(c)) hs = 0; /* Discard high-surrogate if a stray high-surrogate */ } else { - if (IsSurrogateL(c)) return; /* Discard stray low surrogate */ + if (IsSurrogateL(c)) return; /* Discard stray low-surrogate */ } wc = c; #elif FF_LFN_UNICODE == 2 /* UTF-8 input */ for (;;) { - if (pb->ct == 0) { /* Out of multi-byte sequence? */ + if (pb->ct == 0) { /* Not in the multi-byte sequence? */ pb->bs[pb->wi = 0] = (BYTE)c; /* Save 1st byte */ - if ((BYTE)c < 0x80) break; /* Single byte? */ + if ((BYTE)c < 0x80) break; /* Single byte code? */ if (((BYTE)c & 0xE0) == 0xC0) pb->ct = 1; /* 2-byte sequence? */ if (((BYTE)c & 0xF0) == 0xE0) pb->ct = 2; /* 3-byte sequence? */ - if (((BYTE)c & 0xF1) == 0xF0) pb->ct = 3; /* 4-byte sequence? */ - return; + if (((BYTE)c & 0xF8) == 0xF0) pb->ct = 3; /* 4-byte sequence? */ + return; /* Invalid leading byte (discard it) */ } else { /* In the multi-byte sequence */ if (((BYTE)c & 0xC0) != 0x80) { /* Broken sequence? */ - pb->ct = 0; continue; + pb->ct = 0; continue; /* Discard the sequence */ } pb->bs[++pb->wi] = (BYTE)c; /* Save the trailing byte */ - if (--pb->ct == 0) break; /* End of multi-byte sequence? */ + if (--pb->ct == 0) break; /* End of the sequence? */ return; } } tp = (const TCHAR*)pb->bs; - dc = tchar2uni(&tp); /* UTF-8 ==> UTF-16 */ + dc = tchar2uni(&tp); /* UTF-8 ==> UTF-16 */ if (dc == 0xFFFFFFFF) return; /* Wrong code? */ - wc = (WCHAR)dc; hs = (WCHAR)(dc >> 16); + wc = (WCHAR)dc; #elif FF_LFN_UNICODE == 3 /* UTF-32 input */ if (IsSurrogate(c) || c >= 0x110000) return; /* Discard invalid code */ if (c >= 0x10000) { /* Out of BMP? */ @@ -6554,11 +6704,11 @@ static void putc_bfd (putbuff* pb, TCHAR c) #if FF_STRF_ENCODE == 1 /* Write a code point in UTF-16LE */ if (hs != 0) { /* Surrogate pair? */ - st_word(&pb->buf[i], hs); + st_16(&pb->buf[i], hs); i += 2; nc++; } - st_word(&pb->buf[i], wc); + st_16(&pb->buf[i], wc); i += 2; #elif FF_STRF_ENCODE == 2 /* Write a code point in UTF-16BE */ if (hs != 0) { /* Surrogate pair? */ @@ -6614,7 +6764,7 @@ static void putc_bfd (putbuff* pb, TCHAR c) } -/* Flush remaining characters in the buffer */ +/* Flush characters left in the buffer and return number of characters written */ static int putc_flush (putbuff* pb) { @@ -6622,7 +6772,9 @@ static int putc_flush (putbuff* pb) if ( pb->idx >= 0 /* Flush buffered characters to the file */ && f_write(pb->fp, pb->buf, (UINT)pb->idx, &nw) == FR_OK - && (UINT)pb->idx == nw) return pb->nchr; + && (UINT)pb->idx == nw) { + return pb->nchr; + } return -1; } @@ -6730,8 +6882,8 @@ static void ftoa ( TCHAR fmt /* Notation */ ) { - int d; - int e = 0, m = 0; + int digit; + int exp = 0, mag = 0; char sign = 0; double w; const char *er = 0; @@ -6742,7 +6894,7 @@ static void ftoa ( er = "NaN"; } else { if (prec < 0) prec = 6; /* Default precision? (6 fractional digits) */ - if (val < 0) { /* Nagative? */ + if (val < 0) { /* Negative? */ val = 0 - val; sign = '-'; } else { sign = '+'; @@ -6752,18 +6904,18 @@ static void ftoa ( } else { if (fmt == 'f') { /* Decimal notation? */ val += i10x(0 - prec) / 2; /* Round (nearest) */ - m = ilog10(val); - if (m < 0) m = 0; - if (m + prec + 3 >= SZ_NUM_BUF) er = "OV"; /* Buffer overflow? */ + mag = ilog10(val); + if (mag < 0) mag = 0; + if (mag + prec + 3 >= SZ_NUM_BUF) er = "OV"; /* Buffer overflow? */ } else { /* E notation */ if (val != 0) { /* Not a true zero? */ val += i10x(ilog10(val) - prec) / 2; /* Round (nearest) */ - e = ilog10(val); - if (e > 99 || prec + 7 >= SZ_NUM_BUF) { /* Buffer overflow or E > +99? */ + exp = ilog10(val); + if (exp > 99 || prec + 7 >= SZ_NUM_BUF) { /* Buffer overflow or E > +99? */ er = "OV"; } else { - if (e < -99) e = -99; - val /= i10x(e); /* Normalize */ + if (exp < -99) exp = -99; + val /= i10x(exp); /* Normalize */ } } } @@ -6771,26 +6923,28 @@ static void ftoa ( if (!er) { /* Not error condition */ if (sign == '-') *buf++ = sign; /* Add a - if negative value */ do { /* Put decimal number */ - if (m == -1) *buf++ = ds; /* Insert a decimal separator when get into fractional part */ - w = i10x(m); /* Snip the highest digit d */ - d = (int)(val / w); val -= d * w; - *buf++ = (char)('0' + d); /* Put the digit */ - } while (--m >= -prec); /* Output all digits specified by prec */ + if (mag == -1) *buf++ = ds; /* Insert a decimal separator when get into fractional part */ + w = i10x(mag); /* Snip the highest digit d */ + digit = (int)(val / w); val -= digit * w; + *buf++ = (char)('0' + digit); /* Put the digit */ + } while (--mag >= -prec); /* Output all digits specified by prec */ if (fmt != 'f') { /* Put exponent if needed */ *buf++ = (char)fmt; - if (e < 0) { - e = 0 - e; *buf++ = '-'; + if (exp < 0) { + exp = 0 - exp; *buf++ = '-'; } else { *buf++ = '+'; } - *buf++ = (char)('0' + e / 10); - *buf++ = (char)('0' + e % 10); + *buf++ = (char)('0' + exp / 10); + *buf++ = (char)('0' + exp % 10); } } } if (er) { /* Error condition */ if (sign) *buf++ = sign; /* Add sign if needed */ - do *buf++ = *er++; while (*er); /* Put error symbol */ + do { /* Put error symbol */ + *buf++ = *er++; + } while (*er); } *buf = 0; /* Term */ } @@ -6806,16 +6960,17 @@ int f_printf ( { va_list arp; putbuff pb; - UINT i, j, w, f, r; + UINT i, j, width, flag, radix; int prec; #if FF_PRINT_LLI && FF_INTDEF == 2 - QWORD v; + QWORD val; #else - DWORD v; + DWORD val; #endif - TCHAR tc, pad, *tp; + TCHAR *tp; + TCHAR chr, pad; TCHAR nul = 0; - char d, str[SZ_NUM_BUF]; + char digit, str[SZ_NUM_BUF]; putc_init(&pb, fp); @@ -6823,122 +6978,132 @@ int f_printf ( va_start(arp, fmt); for (;;) { - tc = *fmt++; - if (tc == 0) break; /* End of format string */ - if (tc != '%') { /* Not an escape character (pass-through) */ - putc_bfd(&pb, tc); + chr = *fmt++; + if (chr == 0) break; /* End of format string */ + if (chr != '%') { /* Not an escape character (pass-through) */ + putc_bfd(&pb, chr); continue; } - f = w = 0; pad = ' '; prec = -1; /* Initialize parms */ - tc = *fmt++; - if (tc == '0') { /* Flag: '0' padded */ - pad = '0'; tc = *fmt++; - } else if (tc == '-') { /* Flag: Left aligned */ - f = 2; tc = *fmt++; + flag = width = 0; pad = ' '; prec = -1; /* Initialize the parameters */ + chr = *fmt++; + if (chr == '0') { /* Flag: '0' padded */ + pad = '0'; chr = *fmt++; + } else if (chr == '-') { /* Flag: Left aligned */ + flag = 2; chr = *fmt++; } - if (tc == '*') { /* Minimum width from an argument */ - w = va_arg(arp, int); - tc = *fmt++; + if (chr == '*') { /* Minimum width from an argument */ + width = (UINT)va_arg(arp, int); + chr = *fmt++; } else { - while (IsDigit(tc)) { /* Minimum width */ - w = w * 10 + tc - '0'; - tc = *fmt++; + while (IsDigit(chr)) { /* Minimum width */ + width = width * 10 + chr - '0'; + chr = *fmt++; } } - if (tc == '.') { /* Precision */ - tc = *fmt++; - if (tc == '*') { /* Precision from an argument */ + if (chr == '.') { /* Precision */ + chr = *fmt++; + if (chr == '*') { /* Precision from an argument */ prec = va_arg(arp, int); - tc = *fmt++; + chr = *fmt++; } else { prec = 0; - while (IsDigit(tc)) { /* Precision */ - prec = prec * 10 + tc - '0'; - tc = *fmt++; + while (IsDigit(chr)) { /* Precision */ + prec = prec * 10 + chr - '0'; + chr = *fmt++; } } } - if (tc == 'l') { /* Size: long int */ - f |= 4; tc = *fmt++; + if (chr == 'l') { /* Size: long int */ + flag |= 4; chr = *fmt++; #if FF_PRINT_LLI && FF_INTDEF == 2 - if (tc == 'l') { /* Size: long long int */ - f |= 8; tc = *fmt++; + if (chr == 'l') { /* Size: long long int */ + flag |= 8; chr = *fmt++; } #endif } - if (tc == 0) break; /* End of format string */ - switch (tc) { /* Atgument type is... */ + if (chr == 0) break; /* End of format string */ + switch (chr) { /* Atgument type is... */ case 'b': /* Unsigned binary */ - r = 2; break; + radix = 2; break; + case 'o': /* Unsigned octal */ - r = 8; break; + radix = 8; break; + case 'd': /* Signed decimal */ - case 'u': /* Unsigned decimal */ - r = 10; break; - case 'x': /* Unsigned hexdecimal (lower case) */ - case 'X': /* Unsigned hexdecimal (upper case) */ - r = 16; break; + case 'u': /* Unsigned decimal */ + radix = 10; break; + + case 'x': /* Unsigned hexadecimal (lower case) */ + case 'X': /* Unsigned hexadecimal (upper case) */ + radix = 16; break; + case 'c': /* Character */ putc_bfd(&pb, (TCHAR)va_arg(arp, int)); continue; + case 's': /* String */ tp = va_arg(arp, TCHAR*); /* Get a pointer argument */ - if (!tp) tp = &nul; /* Null ptr generates a null string */ + if (!tp) tp = &nul; /* Null pointer generates a null string */ for (j = 0; tp[j]; j++) ; /* j = tcslen(tp) */ - if (prec >= 0 && j > (UINT)prec) j = prec; /* Limited length of string body */ - for ( ; !(f & 2) && j < w; j++) putc_bfd(&pb, pad); /* Left pads */ - while (*tp && prec--) putc_bfd(&pb, *tp++); /* Body */ - while (j++ < w) putc_bfd(&pb, ' '); /* Right pads */ + if (prec >= 0 && j > (UINT)prec) j = (UINT)prec; /* Limited length of string body */ + for ( ; !(flag & 2) && j < width; j++) putc_bfd(&pb, pad); /* Left padding */ + while (*tp && prec--) putc_bfd(&pb, *tp++); /* Body */ + while (j++ < width) putc_bfd(&pb, ' '); /* Right padding */ continue; #if FF_PRINT_FLOAT && FF_INTDEF == 2 case 'f': /* Floating point (decimal) */ case 'e': /* Floating point (e) */ case 'E': /* Floating point (E) */ - ftoa(str, va_arg(arp, double), prec, tc); /* Make a flaoting point string */ - for (j = strlen(str); !(f & 2) && j < w; j++) putc_bfd(&pb, pad); /* Left pads */ + ftoa(str, va_arg(arp, double), prec, chr); /* Make a floating point string */ + for (j = strlen(str); !(flag & 2) && j < width; j++) putc_bfd(&pb, pad); /* Leading pads */ for (i = 0; str[i]; putc_bfd(&pb, str[i++])) ; /* Body */ - while (j++ < w) putc_bfd(&pb, ' '); /* Right pads */ + while (j++ < width) putc_bfd(&pb, ' '); /* Trailing pads */ continue; #endif default: /* Unknown type (pass-through) */ - putc_bfd(&pb, tc); continue; + putc_bfd(&pb, chr); + continue; } /* Get an integer argument and put it in numeral */ #if FF_PRINT_LLI && FF_INTDEF == 2 - if (f & 8) { /* long long argument? */ - v = (QWORD)va_arg(arp, LONGLONG); - } else { - if (f & 4) { /* long argument? */ - v = (tc == 'd') ? (QWORD)(LONGLONG)va_arg(arp, long) : (QWORD)va_arg(arp, unsigned long); - } else { /* int/short/char argument */ - v = (tc == 'd') ? (QWORD)(LONGLONG)va_arg(arp, int) : (QWORD)va_arg(arp, unsigned int); - } + if (flag & 8) { /* long long argument? */ + val = (QWORD)va_arg(arp, long long); + } else if (flag & 4) { /* long argument? */ + val = (chr == 'd') ? (QWORD)(long long)va_arg(arp, long) : (QWORD)va_arg(arp, unsigned long); + } else { /* int/short/char argument */ + val = (chr == 'd') ? (QWORD)(long long)va_arg(arp, int) : (QWORD)va_arg(arp, unsigned int); } - if (tc == 'd' && (v & 0x8000000000000000)) { /* Negative value? */ - v = 0 - v; f |= 1; + if (chr == 'd' && (val & 0x8000000000000000)) { /* Negative value? */ + val = 0 - val; flag |= 1; } #else - if (f & 4) { /* long argument? */ - v = (DWORD)va_arg(arp, long); + if (flag & 4) { /* long argument? */ + val = (DWORD)va_arg(arp, long); } else { /* int/short/char argument */ - v = (tc == 'd') ? (DWORD)(long)va_arg(arp, int) : (DWORD)va_arg(arp, unsigned int); + val = (chr == 'd') ? (DWORD)(long)va_arg(arp, int) : (DWORD)va_arg(arp, unsigned int); } - if (tc == 'd' && (v & 0x80000000)) { /* Negative value? */ - v = 0 - v; f |= 1; + if (chr == 'd' && (val & 0x80000000)) { /* Negative value? */ + val = 0 - val; flag |= 1; } #endif i = 0; do { /* Make an integer number string */ - d = (char)(v % r); v /= r; - if (d > 9) d += (tc == 'x') ? 0x27 : 0x07; - str[i++] = d + '0'; - } while (v && i < SZ_NUM_BUF); - if (f & 1) str[i++] = '-'; /* Sign */ + digit = (char)(val % radix) + '0'; val /= radix; + if (digit > '9') digit += (chr == 'x') ? 0x27 : 0x07; + str[i++] = digit; + } while (val && i < SZ_NUM_BUF); + if (flag & 1) str[i++] = '-'; /* Sign */ /* Write it */ - for (j = i; !(f & 2) && j < w; j++) putc_bfd(&pb, pad); /* Left pads */ - do putc_bfd(&pb, (TCHAR)str[--i]); while (i); /* Body */ - while (j++ < w) putc_bfd(&pb, ' '); /* Right pads */ + for (j = i; !(flag & 2) && j < width; j++) { /* Leading pads */ + putc_bfd(&pb, pad); + } + do { /* Body */ + putc_bfd(&pb, (TCHAR)str[--i]); + } while (i); + while (j++ < width) { /* Trailing pads */ + putc_bfd(&pb, ' '); + } } va_end(arp); @@ -6961,12 +7126,12 @@ FRESULT f_setcp ( ) { static const WORD validcp[22] = { 437, 720, 737, 771, 775, 850, 852, 855, 857, 860, 861, 862, 863, 864, 865, 866, 869, 932, 936, 949, 950, 0}; - static const BYTE* const tables[22] = {Ct437, Ct720, Ct737, Ct771, Ct775, Ct850, Ct852, Ct855, Ct857, Ct860, Ct861, Ct862, Ct863, Ct864, Ct865, Ct866, Ct869, Dc932, Dc936, Dc949, Dc950, 0}; + static const BYTE *const tables[22] = {Ct437, Ct720, Ct737, Ct771, Ct775, Ct850, Ct852, Ct855, Ct857, Ct860, Ct861, Ct862, Ct863, Ct864, Ct865, Ct866, Ct869, Dc932, Dc936, Dc949, Dc950, 0}; UINT i; for (i = 0; validcp[i] != 0 && validcp[i] != cp; i++) ; /* Find the code page */ - if (validcp[i] != cp) return FR_INVALID_PARAMETER; /* Not found? */ + if (validcp[i] != cp) return FR_INVALID_PARAMETER; /* Not found? */ CodePage = cp; if (cp >= 900) { /* DBCS */ diff --git a/vendor/fatfs/source/ff.h b/vendor/fatfs/source/ff.h index 64bf79d..dede148 100644 --- a/vendor/fatfs/source/ff.h +++ b/vendor/fatfs/source/ff.h @@ -1,8 +1,8 @@ /*----------------------------------------------------------------------------/ -/ FatFs - Generic FAT Filesystem module R0.14b / +/ FatFs - Generic FAT Filesystem module R0.15b / /-----------------------------------------------------------------------------/ / -/ Copyright (C) 2021, ChaN, all right reserved. +/ Copyright (C) 2025, ChaN, all right reserved. / / FatFs module is an open source software. Redistribution and use of FatFs in / source and binary forms, with or without modification, are permitted provided @@ -20,14 +20,15 @@ #ifndef FF_DEFINED -#define FF_DEFINED 86631 /* Revision ID */ +#define FF_DEFINED 5385 /* Revision ID */ #ifdef __cplusplus extern "C" { #endif +#if !defined(FFCONF_DEF) #include "ffconf.h" /* FatFs configuration options */ - +#endif #if FF_DEFINED != FFCONF_DEF #error Wrong configuration file (ffconf.h). #endif @@ -48,18 +49,18 @@ typedef unsigned __int64 QWORD; #include typedef unsigned int UINT; /* int must be 16-bit or 32-bit */ typedef unsigned char BYTE; /* char must be 8-bit */ -typedef uint16_t WORD; /* 16-bit unsigned integer */ -typedef uint32_t DWORD; /* 32-bit unsigned integer */ -typedef uint64_t QWORD; /* 64-bit unsigned integer */ -typedef WORD WCHAR; /* UTF-16 character type */ +typedef uint16_t WORD; /* 16-bit unsigned */ +typedef uint32_t DWORD; /* 32-bit unsigned */ +typedef uint64_t QWORD; /* 64-bit unsigned */ +typedef WORD WCHAR; /* UTF-16 code unit */ #else /* Earlier than C99 */ #define FF_INTDEF 1 typedef unsigned int UINT; /* int must be 16-bit or 32-bit */ typedef unsigned char BYTE; /* char must be 8-bit */ -typedef unsigned short WORD; /* 16-bit unsigned integer */ -typedef unsigned long DWORD; /* 32-bit unsigned integer */ -typedef WORD WCHAR; /* UTF-16 character type */ +typedef unsigned short WORD; /* short must be 16-bit */ +typedef unsigned long DWORD; /* long must be 32-bit */ +typedef WORD WCHAR; /* UTF-16 code unit */ #endif @@ -113,15 +114,15 @@ typedef char TCHAR; #if FF_MULTI_PARTITION /* Multiple partition configuration */ typedef struct { - BYTE pd; /* Physical drive number */ - BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */ + BYTE pd; /* Associated physical drive */ + BYTE pt; /* Associated partition (0:Auto detect, 1-4:Forced partition) */ } PARTITION; -extern PARTITION VolToPart[]; /* Volume - Partition mapping table */ +extern PARTITION VolToPart[]; /* Volume to partition mapping table */ #endif #if FF_STR_VOLUME_ID #ifndef FF_VOLUME_STRS -extern const char* VolumeStr[FF_VOLUMES]; /* User defied volume ID */ +extern const char* VolumeStr[FF_VOLUMES]; /* User defined volume ID table */ #endif #endif @@ -131,10 +132,11 @@ extern const char* VolumeStr[FF_VOLUMES]; /* User defied volume ID */ typedef struct { BYTE fs_type; /* Filesystem type (0:not mounted) */ - BYTE pdrv; /* Associated physical drive */ + BYTE pdrv; /* Volume hosting physical drive */ + BYTE ldrv; /* Logical drive number (used only when FF_FS_REENTRANT) */ BYTE n_fats; /* Number of FATs (1 or 2) */ - BYTE wflag; /* win[] flag (b0:dirty) */ - BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */ + BYTE wflag; /* win[] status (b0:dirty) */ + BYTE fsi_flag; /* Allocation information control (b7:disabled, b0:dirty) */ WORD id; /* Volume mount ID */ WORD n_rootdir; /* Number of root directory entries (FAT12/16) */ WORD csize; /* Cluster size [sectors] */ @@ -142,36 +144,29 @@ typedef struct { WORD ssize; /* Sector size (512, 1024, 2048 or 4096) */ #endif #if FF_USE_LFN - WCHAR* lfnbuf; /* LFN working buffer */ -#endif -#if FF_FS_EXFAT - BYTE* dirbuf; /* Directory entry block scratchpad buffer for exFAT */ -#endif -#if FF_FS_REENTRANT - FF_SYNC_t sobj; /* Identifier of sync object */ + WCHAR *lfnbuf; /* LFN working buffer */ #endif #if !FF_FS_READONLY - DWORD last_clst; /* Last allocated cluster */ - DWORD free_clst; /* Number of free clusters */ + DWORD last_clst; /* Last allocated cluster (Unknown if >=n_fatent) */ + DWORD free_clst; /* Number of free clusters (Unknown if >=fs->n_fatent-2) */ #endif #if FF_FS_RPATH DWORD cdir; /* Current directory start cluster (0:root) */ -#if FF_FS_EXFAT - DWORD cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */ - DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */ - DWORD cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */ -#endif #endif DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */ - DWORD fsize; /* Size of an FAT [sectors] */ + DWORD fsize; /* Number of sectors per FAT */ + LBA_t winsect; /* Current sector appearing in the win[] */ LBA_t volbase; /* Volume base sector */ LBA_t fatbase; /* FAT base sector */ - LBA_t dirbase; /* Root directory base sector/cluster */ + LBA_t dirbase; /* Root directory base sector (FAT12/16) or cluster (FAT32/exFAT) */ LBA_t database; /* Data base sector */ #if FF_FS_EXFAT LBA_t bitbase; /* Allocation bitmap base sector */ + BYTE *dirbuf; /* Directory entry block scratchpad buffer for exFAT */ + DWORD cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */ + DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */ + DWORD cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */ #endif - LBA_t winsect; /* Current sector appearing in the win[] */ BYTE win[FF_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */ } FATFS; @@ -181,7 +176,7 @@ typedef struct { typedef struct { FATFS* fs; /* Pointer to the hosting volume of this object */ - WORD id; /* Hosting volume mount ID */ + WORD id; /* Hosting volume's mount ID */ BYTE attr; /* Object attribute */ BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous, =3:fragmented in this session, b2:sub-directory stretched) */ DWORD sclust; /* Object data start cluster (0:no cluster or root directory) */ @@ -191,7 +186,7 @@ typedef struct { DWORD n_frag; /* Size of last fragment needs to be written to FAT (valid when not zero) */ DWORD c_scl; /* Containing directory start cluster (valid when sclust != 0) */ DWORD c_size; /* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */ - DWORD c_ofs; /* Offset in the containing directory (valid when file object and sclust != 0) */ + DWORD c_ofs; /* Offset in the containing directory (valid when in file object and sclust != 0) */ #endif #if FF_FS_LOCK UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */ @@ -248,9 +243,13 @@ typedef struct { FSIZE_t fsize; /* File size */ WORD fdate; /* Modified date */ WORD ftime; /* Modified time */ +#if FF_FS_CRTIME + WORD crdate; /* Created date */ + WORD crtime; /* Created time */ +#endif BYTE fattrib; /* File attribute */ #if FF_USE_LFN - TCHAR altname[FF_SFN_BUF + 1];/* Altenative file name */ + TCHAR altname[FF_SFN_BUF + 1];/* Alternative file name */ TCHAR fname[FF_LFN_BUF + 1]; /* Primary file name */ #else TCHAR fname[12 + 1]; /* File name */ @@ -274,32 +273,34 @@ typedef struct { /* File function return code (FRESULT) */ typedef enum { - FR_OK = 0, /* (0) Succeeded */ + FR_OK = 0, /* (0) Function succeeded */ FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */ FR_INT_ERR, /* (2) Assertion failed */ - FR_NOT_READY, /* (3) The physical drive cannot work */ + FR_NOT_READY, /* (3) The physical drive does not work */ FR_NO_FILE, /* (4) Could not find the file */ FR_NO_PATH, /* (5) Could not find the path */ FR_INVALID_NAME, /* (6) The path name format is invalid */ - FR_DENIED, /* (7) Access denied due to prohibited access or directory full */ - FR_EXIST, /* (8) Access denied due to prohibited access */ + FR_DENIED, /* (7) Access denied due to a prohibited access or directory full */ + FR_EXIST, /* (8) Access denied due to a prohibited access */ FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */ FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */ FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */ FR_NOT_ENABLED, /* (12) The volume has no work area */ - FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */ - FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */ - FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */ + FR_NO_FILESYSTEM, /* (13) Could not find a valid FAT volume */ + FR_MKFS_ABORTED, /* (14) The f_mkfs function aborted due to some problem */ + FR_TIMEOUT, /* (15) Could not take control of the volume within defined period */ FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */ - FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */ + FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated or given buffer is insufficient in size */ FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */ FR_INVALID_PARAMETER /* (19) Given parameter is invalid */ } FRESULT; + +/*--------------------------------------------------------------*/ +/* FatFs Module Application Interface */ /*--------------------------------------------------------------*/ -/* FatFs module application interface */ FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */ FRESULT f_close (FIL* fp); /* Close an open file object */ @@ -336,6 +337,8 @@ int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */ int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */ TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */ +/* Some API fucntions are implemented as macro */ + #define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize)) #define f_error(fp) ((fp)->err) #define f_tell(fp) ((fp)->fptr) @@ -349,40 +352,45 @@ TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the fil /*--------------------------------------------------------------*/ -/* Additional user defined functions */ +/* Additional Functions */ +/*--------------------------------------------------------------*/ -/* RTC function */ +/* RTC function (provided by user) */ #if !FF_FS_READONLY && !FF_FS_NORTC -DWORD get_fattime (void); +DWORD get_fattime (void); /* Get current time */ #endif -/* LFN support functions */ -#if FF_USE_LFN >= 1 /* Code conversion (defined in unicode.c) */ + +/* LFN support functions (defined in ffunicode.c) */ + +#if FF_USE_LFN >= 1 WCHAR ff_oem2uni (WCHAR oem, WORD cp); /* OEM code to Unicode conversion */ WCHAR ff_uni2oem (DWORD uni, WORD cp); /* Unicode to OEM code conversion */ DWORD ff_wtoupper (DWORD uni); /* Unicode upper-case conversion */ #endif -#if FF_USE_LFN == 3 /* Dynamic memory allocation */ -void* ff_memalloc (UINT msize); /* Allocate memory block */ -void ff_memfree (void* mblock); /* Free memory block */ -#endif -/* Sync functions */ -#if FF_FS_REENTRANT -int ff_cre_syncobj (BYTE vol, FF_SYNC_t* sobj); /* Create a sync object */ -int ff_req_grant (FF_SYNC_t sobj); /* Lock sync object */ -void ff_rel_grant (FF_SYNC_t sobj); /* Unlock sync object */ -int ff_del_syncobj (FF_SYNC_t sobj); /* Delete a sync object */ + +/* O/S dependent functions (samples available in ffsystem.c) */ + +#if FF_USE_LFN == 3 /* Dynamic memory allocation */ +void* ff_memalloc (UINT msize); /* Allocate memory block */ +void ff_memfree (void* mblock); /* Free memory block */ +#endif +#if FF_FS_REENTRANT /* Sync functions */ +int ff_mutex_create (int vol); /* Create a sync object */ +void ff_mutex_delete (int vol); /* Delete a sync object */ +int ff_mutex_take (int vol); /* Lock sync object */ +void ff_mutex_give (int vol); /* Unlock sync object */ #endif /*--------------------------------------------------------------*/ -/* Flags and offset address */ - +/* Flags and Offset Address */ +/*--------------------------------------------------------------*/ -/* File access mode and open method flags (3rd argument of f_open) */ +/* File access mode and open method flags (3rd argument of f_open function) */ #define FA_READ 0x01 #define FA_WRITE 0x02 #define FA_OPEN_EXISTING 0x00 @@ -391,10 +399,10 @@ int ff_del_syncobj (FF_SYNC_t sobj); /* Delete a sync object */ #define FA_OPEN_ALWAYS 0x10 #define FA_OPEN_APPEND 0x30 -/* Fast seek controls (2nd argument of f_lseek) */ +/* Fast seek controls (2nd argument of f_lseek function) */ #define CREATE_LINKMAP ((FSIZE_t)0 - 1) -/* Format options (2nd argument of f_mkfs) */ +/* Format options (2nd argument of f_mkfs function) */ #define FM_FAT 0x01 #define FM_FAT32 0x02 #define FM_EXFAT 0x04 diff --git a/vendor/fatfs/source/ffconf.h b/vendor/fatfs/source/ffconf.h index 3eefdf4..4377adb 100644 --- a/vendor/fatfs/source/ffconf.h +++ b/vendor/fatfs/source/ffconf.h @@ -1,8 +1,8 @@ /*---------------------------------------------------------------------------/ -/ FatFs Functional Configurations +/ Configurations of FatFs Module /---------------------------------------------------------------------------*/ -#define FFCONF_DEF 86631 /* Revision ID */ +#define FFCONF_DEF 5385 /* Revision ID */ /*---------------------------------------------------------------------------/ / Function Configurations @@ -31,36 +31,36 @@ #define FF_USE_MKFS 0 -/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */ +/* This option switches f_mkfs(). (0:Disable or 1:Enable) */ #define FF_USE_FASTSEEK 0 -/* This option switches fast seek function. (0:Disable or 1:Enable) */ +/* This option switches fast seek feature. (0:Disable or 1:Enable) */ #define FF_USE_EXPAND 0 -/* This option switches f_expand function. (0:Disable or 1:Enable) */ +/* This option switches f_expand(). (0:Disable or 1:Enable) */ #define FF_USE_CHMOD 0 -/* This option switches attribute manipulation functions, f_chmod() and f_utime(). +/* This option switches attribute control API functions, f_chmod() and f_utime(). / (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */ #define FF_USE_LABEL 0 -/* This option switches volume label functions, f_getlabel() and f_setlabel(). +/* This option switches volume label API functions, f_getlabel() and f_setlabel(). / (0:Disable or 1:Enable) */ #define FF_USE_FORWARD 0 -/* This option switches f_forward() function. (0:Disable or 1:Enable) */ +/* This option switches f_forward(). (0:Disable or 1:Enable) */ #define FF_USE_STRFUNC 0 -#define FF_PRINT_LLI 0 -#define FF_PRINT_FLOAT 0 -#define FF_STRF_ENCODE 0 -/* FF_USE_STRFUNC switches string functions, f_gets(), f_putc(), f_puts() and +#define FF_PRINT_LLI 1 +#define FF_PRINT_FLOAT 1 +#define FF_STRF_ENCODE 3 +/* FF_USE_STRFUNC switches string API functions, f_gets(), f_putc(), f_puts() and / f_printf(). / / 0: Disable. FF_PRINT_LLI, FF_PRINT_FLOAT and FF_STRF_ENCODE have no effect. @@ -68,8 +68,8 @@ / 2: Enable with LF-CRLF conversion. / / FF_PRINT_LLI = 1 makes f_printf() support long long argument and FF_PRINT_FLOAT = 1/2 - makes f_printf() support floating point argument. These features want C99 or later. -/ When FF_LFN_UNICODE >= 1 with LFN enabled, string functions convert the character +/ makes f_printf() support floating point argument. These features want C99 or later. +/ When FF_LFN_UNICODE >= 1 with LFN enabled, string API functions convert the character / encoding in it. FF_STRF_ENCODE selects assumption of character encoding ON THE FILE / to be read/written via those functions. / @@ -118,15 +118,15 @@ /* The FF_USE_LFN switches the support for LFN (long file name). / / 0: Disable LFN. FF_MAX_LFN has no effect. -/ 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe. +/ 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe. / 2: Enable LFN with dynamic working buffer on the STACK. / 3: Enable LFN with dynamic working buffer on the HEAP. / -/ To enable the LFN, ffunicode.c needs to be added to the project. The LFN function +/ To enable the LFN, ffunicode.c needs to be added to the project. The LFN feature / requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and / additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled. / The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can -/ be in range of 12 to 255. It is recommended to be set it 255 to fully support LFN +/ be in range of 12 to 255. It is recommended to be set 255 to fully support the LFN / specification. / When use stack for the working buffer, take care on stack overflow. When use heap / memory for the working buffer, memory management functions, ff_memalloc() and @@ -156,9 +156,9 @@ #define FF_FS_RPATH 0 /* This option configures support for relative path. / -/ 0: Disable relative path and remove related functions. +/ 0: Disable relative path and remove related API functions. / 1: Enable relative path. f_chdir() and f_chdrive() are available. -/ 2: f_getcwd() function is available in addition to 1. +/ 2: f_getcwd() is available in addition to 1. */ @@ -175,10 +175,10 @@ /* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings. / When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive / number in the path name. FF_VOLUME_STRS defines the volume ID strings for each -/ logical drives. Number of items must not be less than FF_VOLUMES. Valid +/ logical drive. Number of items must not be less than FF_VOLUMES. Valid / characters for the volume ID strings are A-Z, a-z and 0-9, however, they are / compared in case-insensitive. If FF_STR_VOLUME_ID >= 1 and FF_VOLUME_STRS is -/ not defined, a user defined volume string table needs to be defined as: +/ not defined, a user defined volume string table is needed as: / / const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sd","usb",... */ @@ -188,9 +188,9 @@ /* This option switches support for multiple volumes on the physical drive. / By default (0), each logical drive number is bound to the same physical drive / number and only an FAT volume found on the physical drive will be mounted. -/ When this function is enabled (1), each logical drive number can be bound to +/ When this feature is enabled (1), each logical drive number can be bound to / arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk() -/ funciton will be available. */ +/ will be available. */ #define FF_MIN_SS 512 @@ -198,8 +198,8 @@ /* This set of options configures the range of sector size to be supported. (512, / 1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and / harddisk, but a larger value may be required for on-board flash memory and some -/ type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured -/ for variable sector size mode and disk_ioctl() function needs to implement +/ type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is +/ configured for variable sector size mode and disk_ioctl() needs to implement / GET_SECTOR_SIZE command. */ @@ -209,14 +209,14 @@ #define FF_MIN_GPT 0x10000000 -/* Minimum number of sectors to switch GPT as partitioning format in f_mkfs and -/ f_fdisk function. 0x100000000 max. This option has no effect when FF_LBA64 == 0. */ +/* Minimum number of sectors to switch GPT as partitioning format in f_mkfs() and +/ f_fdisk(). 2^32 sectors maximum. This option has no effect when FF_LBA64 == 0. */ #define FF_USE_TRIM 0 /* This option switches support for ATA-TRIM. (0:Disable or 1:Enable) -/ To enable Trim function, also CTRL_TRIM command should be implemented to the -/ disk_ioctl() function. */ +/ To enable this feature, also CTRL_TRIM command should be implemented to +/ the disk_ioctl(). */ @@ -238,22 +238,27 @@ #define FF_FS_NORTC 0 -#define FF_NORTC_MON 1 +#define FF_NORTC_MON 6 #define FF_NORTC_MDAY 1 -#define FF_NORTC_YEAR 2020 -/* The option FF_FS_NORTC switches timestamp functiton. If the system does not have -/ any RTC function or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable -/ the timestamp function. Every object modified by FatFs will have a fixed timestamp +#define FF_NORTC_YEAR 2025 +/* The option FF_FS_NORTC switches timestamp feature. If the system does not have +/ an RTC or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable the +/ timestamp feature. Every object modified by FatFs will have a fixed timestamp / defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time. -/ To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be -/ added to the project to read current time form real-time clock. FF_NORTC_MON, +/ To enable timestamp function (FF_FS_NORTC = 0), get_fattime() need to be added +/ to the project to read current time form real-time clock. FF_NORTC_MON, / FF_NORTC_MDAY and FF_NORTC_YEAR have no effect. / These options have no effect in read-only configuration (FF_FS_READONLY = 1). */ +#define FF_FS_CRTIME 0 +/* This option enables(1)/disables(0) the timestamp of the file created. When +/ set 1, the file created time is available in FILINFO structure. */ + + #define FF_FS_NOFSINFO 0 -/* If you need to know correct free space on the FAT32 volume, set bit 0 of this -/ option, and f_getfree() function at first time after volume mount will force +/* If you need to know the correct free space on the FAT32 volume, set bit 0 of +/ this option, and f_getfree() on the first time after volume mount will force / a full FAT scan. Bit 1 controls the use of last allocated cluster number. / / bit0=0: Use free cluster count in the FSINFO if available. @@ -275,26 +280,21 @@ / lock control is independent of re-entrancy. */ -/* #include // O/S definitions */ #define FF_FS_REENTRANT 0 #define FF_FS_TIMEOUT 1000 -#define FF_SYNC_t HANDLE /* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs / module itself. Note that regardless of this option, file access to different / volume is always re-entrant and volume control functions, f_mount(), f_mkfs() -/ and f_fdisk() function, are always not re-entrant. Only file/directory access -/ to the same volume is under control of this function. +/ and f_fdisk(), are always not re-entrant. Only file/directory access to +/ the same volume is under control of this featuer. / -/ 0: Disable re-entrancy. FF_FS_TIMEOUT and FF_SYNC_t have no effect. +/ 0: Disable re-entrancy. FF_FS_TIMEOUT have no effect. / 1: Enable re-entrancy. Also user provided synchronization handlers, -/ ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj() -/ function, must be added to the project. Samples are available in -/ option/syscall.c. +/ ff_mutex_create(), ff_mutex_delete(), ff_mutex_take() and ff_mutex_give(), +/ must be added to the project. Samples are available in ffsystem.c. / -/ The FF_FS_TIMEOUT defines timeout period in unit of time tick. -/ The FF_SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*, -/ SemaphoreHandle_t and etc. A header file for O/S definitions needs to be -/ included somewhere in the scope of ff.h. */ +/ The FF_FS_TIMEOUT defines timeout period in unit of O/S time tick. +*/ diff --git a/vendor/fatfs/source/ffsystem.c b/vendor/fatfs/source/ffsystem.c index ad5d737..2657fe2 100644 --- a/vendor/fatfs/source/ffsystem.c +++ b/vendor/fatfs/source/ffsystem.c @@ -1,170 +1,208 @@ /*------------------------------------------------------------------------*/ -/* Sample Code of OS Dependent Functions for FatFs */ -/* (C)ChaN, 2018 */ +/* A Sample Code of User Provided OS Dependent Functions for FatFs */ /*------------------------------------------------------------------------*/ - #include "ff.h" -#if FF_USE_LFN == 3 /* Dynamic memory allocation */ +#if FF_USE_LFN == 3 /* Use dynamic memory allocation */ /*------------------------------------------------------------------------*/ -/* Allocate a memory block */ +/* Allocate/Free a Memory Block */ /*------------------------------------------------------------------------*/ +#include /* with POSIX API */ + + void* ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */ UINT msize /* Number of bytes to allocate */ ) { - return malloc(msize); /* Allocate a new memory block with POSIX API */ + return malloc((size_t)msize); /* Allocate a new memory block */ } -/*------------------------------------------------------------------------*/ -/* Free a memory block */ -/*------------------------------------------------------------------------*/ - void ff_memfree ( - void* mblock /* Pointer to the memory block to free (nothing to do if null) */ + void* mblock /* Pointer to the memory block to free (no effect if null) */ ) { - free(mblock); /* Free the memory block with POSIX API */ + free(mblock); /* Free the memory block */ } #endif -#if FF_FS_REENTRANT /* Mutal exclusion */ +#if FF_FS_REENTRANT /* Mutal exclusion */ /*------------------------------------------------------------------------*/ -/* Create a Synchronization Object */ +/* Definitions of Mutex */ /*------------------------------------------------------------------------*/ -/* This function is called in f_mount() function to create a new -/ synchronization object for the volume, such as semaphore and mutex. -/ When a 0 is returned, the f_mount() function fails with FR_INT_ERR. -*/ -//const osMutexDef_t Mutex[FF_VOLUMES]; /* Table of CMSIS-RTOS mutex */ +#define OS_TYPE 0 /* 0:Win32, 1:uITRON4.0, 2:uC/OS-II, 3:FreeRTOS, 4:CMSIS-RTOS */ + + +#if OS_TYPE == 0 /* Win32 */ +#include +static HANDLE Mutex[FF_VOLUMES + 1]; /* Table of mutex handle */ +#elif OS_TYPE == 1 /* uITRON */ +#include "itron.h" +#include "kernel.h" +static mtxid Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */ -int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */ - BYTE vol, /* Corresponding volume (logical drive number) */ - FF_SYNC_t* sobj /* Pointer to return the created sync object */ +#elif OS_TYPE == 2 /* uc/OS-II */ +#include "includes.h" +static OS_EVENT *Mutex[FF_VOLUMES + 1]; /* Table of mutex pinter */ + +#elif OS_TYPE == 3 /* FreeRTOS */ +#include "FreeRTOS.h" +#include "semphr.h" +static SemaphoreHandle_t Mutex[FF_VOLUMES + 1]; /* Table of mutex handle */ + +#elif OS_TYPE == 4 /* CMSIS-RTOS */ +#include "cmsis_os.h" +static osMutexId Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */ + +#endif + + + +/*------------------------------------------------------------------------*/ +/* Create a Mutex */ +/*------------------------------------------------------------------------*/ +/* This function is called in f_mount function to create a new mutex +/ or semaphore for the volume. When a 0 is returned, the f_mount function +/ fails with FR_INT_ERR. +*/ + +int ff_mutex_create ( /* Returns 1:Function succeeded or 0:Could not create the mutex */ + int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ ) { - /* Win32 */ - *sobj = CreateMutex(NULL, FALSE, NULL); - return (int)(*sobj != INVALID_HANDLE_VALUE); - - /* uITRON */ -// T_CSEM csem = {TA_TPRI,1,1}; -// *sobj = acre_sem(&csem); -// return (int)(*sobj > 0); - - /* uC/OS-II */ -// OS_ERR err; -// *sobj = OSMutexCreate(0, &err); -// return (int)(err == OS_NO_ERR); - - /* FreeRTOS */ -// *sobj = xSemaphoreCreateMutex(); -// return (int)(*sobj != NULL); - - /* CMSIS-RTOS */ -// *sobj = osMutexCreate(&Mutex[vol]); -// return (int)(*sobj != NULL); +#if OS_TYPE == 0 /* Win32 */ + Mutex[vol] = CreateMutex(NULL, FALSE, NULL); + return (int)(Mutex[vol] != INVALID_HANDLE_VALUE); + +#elif OS_TYPE == 1 /* uITRON */ + T_CMTX cmtx = {TA_TPRI,1}; + + Mutex[vol] = acre_mtx(&cmtx); + return (int)(Mutex[vol] > 0); + +#elif OS_TYPE == 2 /* uC/OS-II */ + OS_ERR err; + + Mutex[vol] = OSMutexCreate(0, &err); + return (int)(err == OS_NO_ERR); + +#elif OS_TYPE == 3 /* FreeRTOS */ + Mutex[vol] = xSemaphoreCreateMutex(); + return (int)(Mutex[vol] != NULL); + +#elif OS_TYPE == 4 /* CMSIS-RTOS */ + osMutexDef(cmsis_os_mutex); + + Mutex[vol] = osMutexCreate(osMutex(cmsis_os_mutex)); + return (int)(Mutex[vol] != NULL); + +#endif } /*------------------------------------------------------------------------*/ -/* Delete a Synchronization Object */ +/* Delete a Mutex */ /*------------------------------------------------------------------------*/ -/* This function is called in f_mount() function to delete a synchronization -/ object that created with ff_cre_syncobj() function. When a 0 is returned, -/ the f_mount() function fails with FR_INT_ERR. +/* This function is called in f_mount function to delete a mutex or +/ semaphore of the volume created with ff_mutex_create function. */ -int ff_del_syncobj ( /* 1:Function succeeded, 0:Could not delete due to an error */ - FF_SYNC_t sobj /* Sync object tied to the logical drive to be deleted */ +void ff_mutex_delete ( /* Returns 1:Function succeeded or 0:Could not delete due to an error */ + int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ ) { - /* Win32 */ - return (int)CloseHandle(sobj); +#if OS_TYPE == 0 /* Win32 */ + CloseHandle(Mutex[vol]); - /* uITRON */ -// return (int)(del_sem(sobj) == E_OK); +#elif OS_TYPE == 1 /* uITRON */ + del_mtx(Mutex[vol]); - /* uC/OS-II */ -// OS_ERR err; -// OSMutexDel(sobj, OS_DEL_ALWAYS, &err); -// return (int)(err == OS_NO_ERR); +#elif OS_TYPE == 2 /* uC/OS-II */ + OS_ERR err; - /* FreeRTOS */ -// vSemaphoreDelete(sobj); -// return 1; + OSMutexDel(Mutex[vol], OS_DEL_ALWAYS, &err); - /* CMSIS-RTOS */ -// return (int)(osMutexDelete(sobj) == osOK); +#elif OS_TYPE == 3 /* FreeRTOS */ + vSemaphoreDelete(Mutex[vol]); + +#elif OS_TYPE == 4 /* CMSIS-RTOS */ + osMutexDelete(Mutex[vol]); + +#endif } /*------------------------------------------------------------------------*/ -/* Request Grant to Access the Volume */ +/* Request a Grant to Access the Volume */ /*------------------------------------------------------------------------*/ -/* This function is called on entering file functions to lock the volume. +/* This function is called on enter file functions to lock the volume. / When a 0 is returned, the file function fails with FR_TIMEOUT. */ -int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */ - FF_SYNC_t sobj /* Sync object to wait */ +int ff_mutex_take ( /* Returns 1:Succeeded or 0:Timeout */ + int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ ) { - /* Win32 */ - return (int)(WaitForSingleObject(sobj, FF_FS_TIMEOUT) == WAIT_OBJECT_0); +#if OS_TYPE == 0 /* Win32 */ + return (int)(WaitForSingleObject(Mutex[vol], FF_FS_TIMEOUT) == WAIT_OBJECT_0); + +#elif OS_TYPE == 1 /* uITRON */ + return (int)(tloc_mtx(Mutex[vol], FF_FS_TIMEOUT) == E_OK); + +#elif OS_TYPE == 2 /* uC/OS-II */ + OS_ERR err; - /* uITRON */ -// return (int)(wai_sem(sobj) == E_OK); + OSMutexPend(Mutex[vol], FF_FS_TIMEOUT, &err)); + return (int)(err == OS_NO_ERR); - /* uC/OS-II */ -// OS_ERR err; -// OSMutexPend(sobj, FF_FS_TIMEOUT, &err)); -// return (int)(err == OS_NO_ERR); +#elif OS_TYPE == 3 /* FreeRTOS */ + return (int)(xSemaphoreTake(Mutex[vol], FF_FS_TIMEOUT) == pdTRUE); - /* FreeRTOS */ -// return (int)(xSemaphoreTake(sobj, FF_FS_TIMEOUT) == pdTRUE); +#elif OS_TYPE == 4 /* CMSIS-RTOS */ + return (int)(osMutexWait(Mutex[vol], FF_FS_TIMEOUT) == osOK); - /* CMSIS-RTOS */ -// return (int)(osMutexWait(sobj, FF_FS_TIMEOUT) == osOK); +#endif } + /*------------------------------------------------------------------------*/ -/* Release Grant to Access the Volume */ +/* Release a Grant to Access the Volume */ /*------------------------------------------------------------------------*/ -/* This function is called on leaving file functions to unlock the volume. +/* This function is called on leave file functions to unlock the volume. */ -void ff_rel_grant ( - FF_SYNC_t sobj /* Sync object to be signaled */ +void ff_mutex_give ( + int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ ) { - /* Win32 */ - ReleaseMutex(sobj); +#if OS_TYPE == 0 /* Win32 */ + ReleaseMutex(Mutex[vol]); - /* uITRON */ -// sig_sem(sobj); +#elif OS_TYPE == 1 /* uITRON */ + unl_mtx(Mutex[vol]); - /* uC/OS-II */ -// OSMutexPost(sobj); +#elif OS_TYPE == 2 /* uC/OS-II */ + OSMutexPost(Mutex[vol]); - /* FreeRTOS */ -// xSemaphoreGive(sobj); +#elif OS_TYPE == 3 /* FreeRTOS */ + xSemaphoreGive(Mutex[vol]); - /* CMSIS-RTOS */ -// osMutexRelease(sobj); -} +#elif OS_TYPE == 4 /* CMSIS-RTOS */ + osMutexRelease(Mutex[vol]); #endif +} + +#endif /* FF_FS_REENTRANT */ diff --git a/vendor/fatfs/source/ffunicode.c b/vendor/fatfs/source/ffunicode.c index 784a10e..54ad526 100644 --- a/vendor/fatfs/source/ffunicode.c +++ b/vendor/fatfs/source/ffunicode.c @@ -1,13 +1,13 @@ /*------------------------------------------------------------------------*/ -/* Unicode handling functions for FatFs R0.13+ */ +/* Unicode Handling Functions for FatFs R0.13 and Later */ +/*------------------------------------------------------------------------*/ +/* This module will occupy a huge memory in the .rodata section when the */ +/* FatFs is configured for LFN with DBCS. If the system has a Unicode */ +/* library for the code conversion, this module should be modified to use */ +/* it to avoid silly memory consumption. */ /*------------------------------------------------------------------------*/ -/* This module will occupy a huge memory in the .const section when the / -/ FatFs is configured for LFN with DBCS. If the system has any Unicode / -/ utilitiy for the code conversion, this module should be modified to use / -/ that function to avoid silly memory consumption. / -/-------------------------------------------------------------------------*/ /* -/ Copyright (C) 2014, ChaN, all right reserved. +/ Copyright (C) 2022, ChaN, all right reserved. / / FatFs module is an open source software. Redistribution and use of FatFs in / source and binary forms, with or without modification, are permitted provided @@ -25,7 +25,7 @@ #include "ff.h" -#if FF_USE_LFN /* This module will be blanked if non-LFN configuration */ +#if FF_USE_LFN != 0 /* This module will be blanked if in non-LFN configuration */ #define MERGE2(a, b) a ## b #define CVTBL(tbl, cp) MERGE2(tbl, cp) @@ -15214,8 +15214,8 @@ static const WCHAR uc869[] = { /* CP869(Greek 2) to Unicode conversion table */ /*------------------------------------------------------------------------*/ -/* OEM <==> Unicode conversions for static code page configuration */ -/* SBCS fixed code page */ +/* OEM <==> Unicode Conversions for Static Code Page Configuration with */ +/* SBCS Fixed Code Page */ /*------------------------------------------------------------------------*/ #if FF_CODE_PAGE != 0 && FF_CODE_PAGE < 900 @@ -15225,7 +15225,7 @@ WCHAR ff_uni2oem ( /* Returns OEM code character, zero on error */ ) { WCHAR c = 0; - const WCHAR *p = CVTBL(uc, FF_CODE_PAGE); + const WCHAR* p = CVTBL(uc, FF_CODE_PAGE); if (uni < 0x80) { /* ASCII? */ @@ -15247,7 +15247,7 @@ WCHAR ff_oem2uni ( /* Returns Unicode character in UTF-16, zero on error */ ) { WCHAR c = 0; - const WCHAR *p = CVTBL(uc, FF_CODE_PAGE); + const WCHAR* p = CVTBL(uc, FF_CODE_PAGE); if (oem < 0x80) { /* ASCII? */ @@ -15267,8 +15267,8 @@ WCHAR ff_oem2uni ( /* Returns Unicode character in UTF-16, zero on error */ /*------------------------------------------------------------------------*/ -/* OEM <==> Unicode conversions for static code page configuration */ -/* DBCS fixed code page */ +/* OEM <==> Unicode Conversions for Static Code Page Configuration with */ +/* DBCS Fixed Code Page */ /*------------------------------------------------------------------------*/ #if FF_CODE_PAGE >= 900 @@ -15277,7 +15277,7 @@ WCHAR ff_uni2oem ( /* Returns OEM code character, zero on error */ WORD cp /* Code page for the conversion */ ) { - const WCHAR *p; + const WCHAR* p; WCHAR c = 0, uc; UINT i = 0, n, li, hi; @@ -15313,7 +15313,7 @@ WCHAR ff_oem2uni ( /* Returns Unicode character in UTF-16, zero on error */ WORD cp /* Code page for the conversion */ ) { - const WCHAR *p; + const WCHAR* p; WCHAR c = 0; UINT i = 0, n, li, hi; @@ -15346,7 +15346,7 @@ WCHAR ff_oem2uni ( /* Returns Unicode character in UTF-16, zero on error */ /*------------------------------------------------------------------------*/ -/* OEM <==> Unicode conversions for dynamic code page configuration */ +/* OEM <==> Unicode Conversions for Dynamic Code Page Configuration */ /*------------------------------------------------------------------------*/ #if FF_CODE_PAGE == 0 @@ -15360,7 +15360,7 @@ WCHAR ff_uni2oem ( /* Returns OEM code character, zero on error */ WORD cp /* Code page for the conversion */ ) { - const WCHAR *p; + const WCHAR* p; WCHAR c = 0, uc; UINT i, n, li, hi; @@ -15412,7 +15412,7 @@ WCHAR ff_oem2uni ( /* Returns Unicode character in UTF-16, zero on error */ WORD cp /* Code page for the conversion */ ) { - const WCHAR *p; + const WCHAR* p; WCHAR c = 0; UINT i, n, li, hi; @@ -15458,14 +15458,14 @@ WCHAR ff_oem2uni ( /* Returns Unicode character in UTF-16, zero on error */ /*------------------------------------------------------------------------*/ -/* Unicode up-case conversion */ +/* Unicode Up-case Conversion */ /*------------------------------------------------------------------------*/ DWORD ff_wtoupper ( /* Returns up-converted code point */ DWORD uni /* Unicode code point to be up-converted */ ) { - const WORD *p; + const WORD* p; WORD uc, bc, nc, cmd; static const WORD cvt1[] = { /* Compressed up conversion table for U+0000 - U+0FFF */ /* Basic Latin */ @@ -15590,4 +15590,4 @@ DWORD ff_wtoupper ( /* Returns up-converted code point */ } -#endif /* #if FF_USE_LFN */ +#endif /* #if FF_USE_LFN != 0 */