annotate fat.c @ 26:292cd17de2a4

merge
author Matt Johnston <matt@ucc.asn.au>
date Wed, 26 Jun 2013 22:00:08 +0800
parents 5f9a40d6991b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 /*
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 * Copyright (c) 2006-2012 by Roland Riegel <[email protected]>
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 * This file is free software; you can redistribute it and/or modify
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 * it under the terms of either the GNU General Public License version 2
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 * or the GNU Lesser General Public License version 2.1, both as
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 * published by the Free Software Foundation.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 #include "byteordering.h"
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 #include "partition.h"
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 #include "fat.h"
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 #include "fat_config.h"
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 #include "sd-reader_config.h"
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 #include <string.h>
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 #if USE_DYNAMIC_MEMORY
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 #include <stdlib.h>
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 * \addtogroup fat FAT support
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 * This module implements FAT16/FAT32 read and write access.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 * The following features are supported:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 * - File names up to 31 characters long.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 * - Unlimited depth of subdirectories.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 * - Short 8.3 and long filenames.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 * - Creating and deleting files.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 * - Reading and writing from and to files.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 * - File resizing.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 * - File sizes of up to 4 gigabytes.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 * @{
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 * \file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 * FAT implementation (license: GPLv2 or LGPLv2.1)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 * \author Roland Riegel
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 * \addtogroup fat_config FAT configuration
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 * Preprocessor defines to configure the FAT implementation.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 * \addtogroup fat_fs FAT access
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 * Basic functions for handling a FAT filesystem.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 * \addtogroup fat_file FAT file functions
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 * Functions for managing files.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 * \addtogroup fat_dir FAT directory functions
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 * Functions for managing directories.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 * @}
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 #define FAT16_CLUSTER_FREE 0x0000
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 #define FAT16_CLUSTER_RESERVED_MIN 0xfff0
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 #define FAT16_CLUSTER_RESERVED_MAX 0xfff6
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 #define FAT16_CLUSTER_BAD 0xfff7
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 #define FAT16_CLUSTER_LAST_MIN 0xfff8
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 #define FAT16_CLUSTER_LAST_MAX 0xffff
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 #define FAT32_CLUSTER_FREE 0x00000000
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 #define FAT32_CLUSTER_RESERVED_MIN 0x0ffffff0
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 #define FAT32_CLUSTER_RESERVED_MAX 0x0ffffff6
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 #define FAT32_CLUSTER_BAD 0x0ffffff7
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 #define FAT32_CLUSTER_LAST_MIN 0x0ffffff8
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 #define FAT32_CLUSTER_LAST_MAX 0x0fffffff
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 #define FAT_DIRENTRY_DELETED 0xe5
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 #define FAT_DIRENTRY_LFNLAST (1 << 6)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 #define FAT_DIRENTRY_LFNSEQMASK ((1 << 6) - 1)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 /* Each entry within the directory table has a size of 32 bytes
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 * and either contains a 8.3 DOS-style file name or a part of a
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 * long file name, which may consist of several directory table
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 * entries at once.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 * multi-byte integer values are stored little-endian!
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 * 8.3 file name entry:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 * ====================
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 * offset length description
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 * 0 8 name (space padded)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 * 8 3 extension (space padded)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 * 11 1 attributes (FAT_ATTRIB_*)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 * long file name (lfn) entry ordering for a single file name:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 * ===========================================================
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 * LFN entry n
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 * ...
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 * LFN entry 2
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 * LFN entry 1
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 * 8.3 entry (see above)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 * lfn entry:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 * ==========
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 * offset length description
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 * 0 1 ordinal field
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 * 1 2 unicode character 1
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 * 3 3 unicode character 2
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 * 5 3 unicode character 3
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 * 7 3 unicode character 4
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 * 9 3 unicode character 5
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 * 11 1 attribute (always 0x0f)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 * 12 1 type (reserved, always 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 * 13 1 checksum
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 * 14 2 unicode character 6
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 * 16 2 unicode character 7
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 * 18 2 unicode character 8
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 * 20 2 unicode character 9
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 * 22 2 unicode character 10
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 * 24 2 unicode character 11
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 * 26 2 cluster (unused, always 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 * 28 2 unicode character 12
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 * 30 2 unicode character 13
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 * The ordinal field contains a descending number, from n to 1.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 * For the n'th lfn entry the ordinal field is or'ed with 0x40.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134 * For deleted lfn entries, the ordinal field is set to 0xe5.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 struct fat_header_struct
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 offset_t size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 offset_t fat_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 uint32_t fat_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 uint16_t sector_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 uint16_t cluster_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 offset_t cluster_zero_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 offset_t root_dir_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 cluster_t root_dir_cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 };
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155 struct fat_fs_struct
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 struct partition_struct* partition;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 struct fat_header_struct header;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 cluster_t cluster_free;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160 };
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 struct fat_file_struct
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 struct fat_fs_struct* fs;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 struct fat_dir_entry_struct dir_entry;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 offset_t pos;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167 cluster_t pos_cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 };
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 struct fat_dir_struct
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172 struct fat_fs_struct* fs;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 struct fat_dir_entry_struct dir_entry;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 cluster_t entry_cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 uint16_t entry_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 };
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 struct fat_read_dir_callback_arg
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 struct fat_dir_entry_struct* dir_entry;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 uintptr_t bytes_read;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 #if FAT_LFN_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 uint8_t checksum;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 uint8_t finished;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186 };
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 struct fat_usage_count_callback_arg
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 cluster_t cluster_count;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 uintptr_t buffer_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 };
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194 #if !USE_DYNAMIC_MEMORY
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195 static struct fat_fs_struct fat_fs_handles[FAT_FS_COUNT];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196 static struct fat_file_struct fat_file_handles[FAT_FILE_COUNT];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 static struct fat_dir_struct fat_dir_handles[FAT_DIR_COUNT];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200 static uint8_t fat_read_header(struct fat_fs_struct* fs);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 static cluster_t fat_get_next_cluster(const struct fat_fs_struct* fs, cluster_t cluster_num);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202 static offset_t fat_cluster_offset(const struct fat_fs_struct* fs, cluster_t cluster_num);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203 static uint8_t fat_dir_entry_read_callback(uint8_t* buffer, offset_t offset, void* p);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204 #if FAT_LFN_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205 static uint8_t fat_calc_83_checksum(const uint8_t* file_name_83);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208 static uint8_t fat_get_fs_free_16_callback(uint8_t* buffer, offset_t offset, void* p);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
210 static uint8_t fat_get_fs_free_32_callback(uint8_t* buffer, offset_t offset, void* p);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 #if FAT_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214 static cluster_t fat_append_clusters(struct fat_fs_struct* fs, cluster_t cluster_num, cluster_t count);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215 static uint8_t fat_free_clusters(struct fat_fs_struct* fs, cluster_t cluster_num);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 static uint8_t fat_terminate_clusters(struct fat_fs_struct* fs, cluster_t cluster_num);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217 static uint8_t fat_clear_cluster(const struct fat_fs_struct* fs, cluster_t cluster_num);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
218 static uintptr_t fat_clear_cluster_callback(uint8_t* buffer, offset_t offset, void* p);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219 static offset_t fat_find_offset_for_dir_entry(struct fat_fs_struct* fs, const struct fat_dir_struct* parent, const struct fat_dir_entry_struct* dir_entry);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220 static uint8_t fat_write_dir_entry(const struct fat_fs_struct* fs, struct fat_dir_entry_struct* dir_entry);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 #if FAT_DATETIME_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 static void fat_set_file_modification_date(struct fat_dir_entry_struct* dir_entry, uint16_t year, uint8_t month, uint8_t day);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
223 static void fat_set_file_modification_time(struct fat_dir_entry_struct* dir_entry, uint8_t hour, uint8_t min, uint8_t sec);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228 * \ingroup fat_fs
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 * Opens a FAT filesystem.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231 * \param[in] partition Discriptor of partition on which the filesystem resides.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232 * \returns 0 on error, a FAT filesystem descriptor on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233 * \see fat_close
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235 struct fat_fs_struct* fat_open(struct partition_struct* partition)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237 if(!partition ||
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238 #if FAT_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239 !partition->device_write ||
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240 !partition->device_write_interval
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242 0
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244 )
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 #if USE_DYNAMIC_MEMORY
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248 struct fat_fs_struct* fs = malloc(sizeof(*fs));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 if(!fs)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252 struct fat_fs_struct* fs = fat_fs_handles;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253 uint8_t i;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254 for(i = 0; i < FAT_FS_COUNT; ++i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256 if(!fs->partition)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
259 ++fs;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
260 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
261 if(i >= FAT_FS_COUNT)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
262 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
264
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 memset(fs, 0, sizeof(*fs));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
267 fs->partition = partition;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
268 if(!fat_read_header(fs))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
269 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270 #if USE_DYNAMIC_MEMORY
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271 free(fs);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 fs->partition = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
276 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
277
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
278 return fs;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
280
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
281 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
282 * \ingroup fat_fs
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
283 * Closes a FAT filesystem.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
284 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
285 * When this function returns, the given filesystem descriptor
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
286 * will be invalid.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
287 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
288 * \param[in] fs The filesystem to close.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
289 * \see fat_open
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
290 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
291 void fat_close(struct fat_fs_struct* fs)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
292 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
293 if(!fs)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
294 return;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
295
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
296 #if USE_DYNAMIC_MEMORY
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
297 free(fs);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
298 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
299 fs->partition = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
300 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
301 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
302
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
303 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
304 * \ingroup fat_fs
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
305 * Reads and parses the header of a FAT filesystem.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
306 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
307 * \param[in,out] fs The filesystem for which to parse the header.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
308 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
309 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
310 uint8_t fat_read_header(struct fat_fs_struct* fs)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
311 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
312 if(!fs)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
313 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
314
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
315 struct partition_struct* partition = fs->partition;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
316 if(!partition)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
317 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
318
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
319 /* read fat parameters */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
320 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
321 uint8_t buffer[37];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
322 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
323 uint8_t buffer[25];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
324 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
325 offset_t partition_offset = (offset_t) partition->offset * 512;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
326 if(!partition->device_read(partition_offset + 0x0b, buffer, sizeof(buffer)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
327 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
328
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
329 uint16_t bytes_per_sector = read16(&buffer[0x00]);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
330 uint16_t reserved_sectors = read16(&buffer[0x03]);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
331 uint8_t sectors_per_cluster = buffer[0x02];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
332 uint8_t fat_copies = buffer[0x05];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
333 uint16_t max_root_entries = read16(&buffer[0x06]);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
334 uint16_t sector_count_16 = read16(&buffer[0x08]);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
335 uint16_t sectors_per_fat = read16(&buffer[0x0b]);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
336 uint32_t sector_count = read32(&buffer[0x15]);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
337 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
338 uint32_t sectors_per_fat32 = read32(&buffer[0x19]);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
339 uint32_t cluster_root_dir = read32(&buffer[0x21]);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
340 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
341
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
342 if(sector_count == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
343 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
344 if(sector_count_16 == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
345 /* illegal volume size */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
346 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
347 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
348 sector_count = sector_count_16;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
349 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
350 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
351 if(sectors_per_fat != 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
352 sectors_per_fat32 = sectors_per_fat;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
353 else if(sectors_per_fat32 == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
354 /* this is neither FAT16 nor FAT32 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
355 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
356 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
357 if(sectors_per_fat == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
358 /* this is not a FAT16 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
359 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
360 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
361
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
362 /* determine the type of FAT we have here */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
363 uint32_t data_sector_count = sector_count
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
364 - reserved_sectors
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
365 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
366 - sectors_per_fat32 * fat_copies
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
367 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
368 - (uint32_t) sectors_per_fat * fat_copies
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
369 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
370 - ((max_root_entries * 32 + bytes_per_sector - 1) / bytes_per_sector);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
371 uint32_t data_cluster_count = data_sector_count / sectors_per_cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
372 if(data_cluster_count < 4085)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
373 /* this is a FAT12, not supported */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
374 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
375 else if(data_cluster_count < 65525)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
376 /* this is a FAT16 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
377 partition->type = PARTITION_TYPE_FAT16;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
378 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
379 /* this is a FAT32 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
380 partition->type = PARTITION_TYPE_FAT32;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
381
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
382 /* fill header information */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
383 struct fat_header_struct* header = &fs->header;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
384 memset(header, 0, sizeof(*header));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
385
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
386 header->size = (offset_t) sector_count * bytes_per_sector;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
387
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
388 header->fat_offset = /* jump to partition */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
389 partition_offset +
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
390 /* jump to fat */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
391 (offset_t) reserved_sectors * bytes_per_sector;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
392 header->fat_size = (data_cluster_count + 2) * (partition->type == PARTITION_TYPE_FAT16 ? 2 : 4);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
393
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
394 header->sector_size = bytes_per_sector;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
395 header->cluster_size = (uint16_t) bytes_per_sector * sectors_per_cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
396
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
397 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
398 if(partition->type == PARTITION_TYPE_FAT16)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
399 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
400 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
401 header->root_dir_offset = /* jump to fats */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
402 header->fat_offset +
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
403 /* jump to root directory entries */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
404 (offset_t) fat_copies * sectors_per_fat * bytes_per_sector;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
405
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
406 header->cluster_zero_offset = /* jump to root directory entries */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
407 header->root_dir_offset +
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
408 /* skip root directory entries */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
409 (offset_t) max_root_entries * 32;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
410 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
411 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
412 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
413 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
414 header->cluster_zero_offset = /* jump to fats */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
415 header->fat_offset +
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
416 /* skip fats */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
417 (offset_t) fat_copies * sectors_per_fat32 * bytes_per_sector;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
418
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
419 header->root_dir_cluster = cluster_root_dir;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
420 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
421 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
422
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
423 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
424 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
425
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
426 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
427 * \ingroup fat_fs
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
428 * Retrieves the next following cluster of a given cluster.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
429 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
430 * Using the filesystem file allocation table, this function returns
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
431 * the number of the cluster containing the data directly following
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
432 * the data within the cluster with the given number.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
433 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
434 * \param[in] fs The filesystem for which to determine the next cluster.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
435 * \param[in] cluster_num The number of the cluster for which to determine its successor.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
436 * \returns The wanted cluster number, or 0 on error.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
437 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
438 cluster_t fat_get_next_cluster(const struct fat_fs_struct* fs, cluster_t cluster_num)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
439 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
440 if(!fs || cluster_num < 2)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
441 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
442
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
443 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
444 if(fs->partition->type == PARTITION_TYPE_FAT32)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
445 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
446 /* read appropriate fat entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
447 uint32_t fat_entry;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
448 if(!fs->partition->device_read(fs->header.fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
449 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
450
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
451 /* determine next cluster from fat */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
452 cluster_num = ltoh32(fat_entry);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
453
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
454 if(cluster_num == FAT32_CLUSTER_FREE ||
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
455 cluster_num == FAT32_CLUSTER_BAD ||
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
456 (cluster_num >= FAT32_CLUSTER_RESERVED_MIN && cluster_num <= FAT32_CLUSTER_RESERVED_MAX) ||
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
457 (cluster_num >= FAT32_CLUSTER_LAST_MIN && cluster_num <= FAT32_CLUSTER_LAST_MAX))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
458 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
459 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
460 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
461 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
462 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
463 /* read appropriate fat entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
464 uint16_t fat_entry;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
465 if(!fs->partition->device_read(fs->header.fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
466 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
467
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
468 /* determine next cluster from fat */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
469 cluster_num = ltoh16(fat_entry);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
470
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
471 if(cluster_num == FAT16_CLUSTER_FREE ||
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
472 cluster_num == FAT16_CLUSTER_BAD ||
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
473 (cluster_num >= FAT16_CLUSTER_RESERVED_MIN && cluster_num <= FAT16_CLUSTER_RESERVED_MAX) ||
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
474 (cluster_num >= FAT16_CLUSTER_LAST_MIN && cluster_num <= FAT16_CLUSTER_LAST_MAX))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
475 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
476 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
477
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
478 return cluster_num;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
479 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
480
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
481 #if DOXYGEN || FAT_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
482 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
483 * \ingroup fat_fs
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
484 * Appends a new cluster chain to an existing one.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
485 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
486 * Set cluster_num to zero to create a completely new one.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
487 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
488 * \param[in] fs The file system on which to operate.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
489 * \param[in] cluster_num The cluster to which to append the new chain.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
490 * \param[in] count The number of clusters to allocate.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
491 * \returns 0 on failure, the number of the first new cluster on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
492 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
493 cluster_t fat_append_clusters(struct fat_fs_struct* fs, cluster_t cluster_num, cluster_t count)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
494 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
495 if(!fs)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
496 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
497
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
498 device_read_t device_read = fs->partition->device_read;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
499 device_write_t device_write = fs->partition->device_write;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
500 offset_t fat_offset = fs->header.fat_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
501 cluster_t count_left = count;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
502 cluster_t cluster_current = fs->cluster_free;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
503 cluster_t cluster_next = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
504 cluster_t cluster_count;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
505 uint16_t fat_entry16;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
506 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
507 uint32_t fat_entry32;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
508 uint8_t is_fat32 = (fs->partition->type == PARTITION_TYPE_FAT32);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
509
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
510 if(is_fat32)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
511 cluster_count = fs->header.fat_size / sizeof(fat_entry32);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
512 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
513 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
514 cluster_count = fs->header.fat_size / sizeof(fat_entry16);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
515
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
516 fs->cluster_free = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
517 for(cluster_t cluster_left = cluster_count; cluster_left > 0; --cluster_left, ++cluster_current)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
518 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
519 if(cluster_current < 2 || cluster_current >= cluster_count)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
520 cluster_current = 2;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
521
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
522 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
523 if(is_fat32)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
524 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
525 if(!device_read(fat_offset + (offset_t) cluster_current * sizeof(fat_entry32), (uint8_t*) &fat_entry32, sizeof(fat_entry32)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
526 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
527 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
528 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
529 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
530 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
531 if(!device_read(fat_offset + (offset_t) cluster_current * sizeof(fat_entry16), (uint8_t*) &fat_entry16, sizeof(fat_entry16)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
532 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
533 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
534
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
535 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
536 if(is_fat32)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
537 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
538 /* check if this is a free cluster */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
539 if(fat_entry32 != HTOL32(FAT32_CLUSTER_FREE))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
540 continue;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
541
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
542 /* If we don't need this free cluster for the
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
543 * current allocation, we keep it in mind for
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
544 * the next time.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
545 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
546 if(count_left == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
547 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
548 fs->cluster_free = cluster_current;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
549 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
550 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
551
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
552 /* allocate cluster */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
553 if(cluster_next == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
554 fat_entry32 = HTOL32(FAT32_CLUSTER_LAST_MAX);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
555 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
556 fat_entry32 = htol32(cluster_next);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
557
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
558 if(!device_write(fat_offset + (offset_t) cluster_current * sizeof(fat_entry32), (uint8_t*) &fat_entry32, sizeof(fat_entry32)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
559 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
560 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
561 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
562 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
563 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
564 /* check if this is a free cluster */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
565 if(fat_entry16 != HTOL16(FAT16_CLUSTER_FREE))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
566 continue;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
567
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
568 /* If we don't need this free cluster for the
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
569 * current allocation, we keep it in mind for
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
570 * the next time.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
571 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
572 if(count_left == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
573 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
574 fs->cluster_free = cluster_current;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
575 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
576 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
577
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
578 /* allocate cluster */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
579 if(cluster_next == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
580 fat_entry16 = HTOL16(FAT16_CLUSTER_LAST_MAX);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
581 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
582 fat_entry16 = htol16((uint16_t) cluster_next);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
583
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
584 if(!device_write(fat_offset + (offset_t) cluster_current * sizeof(fat_entry16), (uint8_t*) &fat_entry16, sizeof(fat_entry16)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
585 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
586 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
587
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
588 cluster_next = cluster_current;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
589 --count_left;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
590 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
591
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
592 do
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
593 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
594 if(count_left > 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
595 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
596
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
597 /* We allocated a new cluster chain. Now join
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
598 * it with the existing one (if any).
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
599 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
600 if(cluster_num >= 2)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
601 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
602 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
603 if(is_fat32)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
604 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
605 fat_entry32 = htol32(cluster_next);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
606
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
607 if(!device_write(fat_offset + (offset_t) cluster_num * sizeof(fat_entry32), (uint8_t*) &fat_entry32, sizeof(fat_entry32)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
608 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
609 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
610 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
611 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
612 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
613 fat_entry16 = htol16((uint16_t) cluster_next);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
614
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
615 if(!device_write(fat_offset + (offset_t) cluster_num * sizeof(fat_entry16), (uint8_t*) &fat_entry16, sizeof(fat_entry16)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
616 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
617 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
618 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
619
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
620 return cluster_next;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
621
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
622 } while(0);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
623
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
624 /* No space left on device or writing error.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
625 * Free up all clusters already allocated.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
626 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
627 fat_free_clusters(fs, cluster_next);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
628
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
629 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
630 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
631 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
632
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
633 #if DOXYGEN || FAT_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
634 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
635 * \ingroup fat_fs
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
636 * Frees a cluster chain, or a part thereof.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
637 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
638 * Marks the specified cluster and all clusters which are sequentially
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
639 * referenced by it as free. They may then be used again for future
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
640 * file allocations.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
641 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
642 * \note If this function is used for freeing just a part of a cluster
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
643 * chain, the new end of the chain is not correctly terminated
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
644 * within the FAT. Use fat_terminate_clusters() instead.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
645 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
646 * \param[in] fs The filesystem on which to operate.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
647 * \param[in] cluster_num The starting cluster of the chain which to free.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
648 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
649 * \see fat_terminate_clusters
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
650 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
651 uint8_t fat_free_clusters(struct fat_fs_struct* fs, cluster_t cluster_num)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
652 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
653 if(!fs || cluster_num < 2)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
654 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
655
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
656 offset_t fat_offset = fs->header.fat_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
657 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
658 if(fs->partition->type == PARTITION_TYPE_FAT32)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
659 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
660 uint32_t fat_entry;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
661 while(cluster_num)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
662 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
663 if(!fs->partition->device_read(fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
664 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
665
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
666 /* get next cluster of current cluster before freeing current cluster */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
667 uint32_t cluster_num_next = ltoh32(fat_entry);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
668
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
669 if(cluster_num_next == FAT32_CLUSTER_FREE)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
670 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
671 if(cluster_num_next == FAT32_CLUSTER_BAD ||
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
672 (cluster_num_next >= FAT32_CLUSTER_RESERVED_MIN &&
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
673 cluster_num_next <= FAT32_CLUSTER_RESERVED_MAX
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
674 )
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
675 )
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
676 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
677 if(cluster_num_next >= FAT32_CLUSTER_LAST_MIN && cluster_num_next <= FAT32_CLUSTER_LAST_MAX)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
678 cluster_num_next = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
679
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
680 /* We know we will free the cluster, so remember it as
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
681 * free for the next allocation.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
682 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
683 if(!fs->cluster_free)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
684 fs->cluster_free = cluster_num;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
685
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
686 /* free cluster */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
687 fat_entry = HTOL32(FAT32_CLUSTER_FREE);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
688 fs->partition->device_write(fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
689
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
690 /* We continue in any case here, even if freeing the cluster failed.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
691 * The cluster is lost, but maybe we can still free up some later ones.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
692 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
693
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
694 cluster_num = cluster_num_next;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
695 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
696 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
697 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
698 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
699 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
700 uint16_t fat_entry;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
701 while(cluster_num)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
702 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
703 if(!fs->partition->device_read(fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
704 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
705
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
706 /* get next cluster of current cluster before freeing current cluster */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
707 uint16_t cluster_num_next = ltoh16(fat_entry);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
708
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
709 if(cluster_num_next == FAT16_CLUSTER_FREE)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
710 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
711 if(cluster_num_next == FAT16_CLUSTER_BAD ||
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
712 (cluster_num_next >= FAT16_CLUSTER_RESERVED_MIN &&
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
713 cluster_num_next <= FAT16_CLUSTER_RESERVED_MAX
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
714 )
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
715 )
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
716 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
717 if(cluster_num_next >= FAT16_CLUSTER_LAST_MIN && cluster_num_next <= FAT16_CLUSTER_LAST_MAX)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
718 cluster_num_next = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
719
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
720 /* free cluster */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
721 fat_entry = HTOL16(FAT16_CLUSTER_FREE);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
722 fs->partition->device_write(fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
723
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
724 /* We continue in any case here, even if freeing the cluster failed.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
725 * The cluster is lost, but maybe we can still free up some later ones.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
726 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
727
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
728 cluster_num = cluster_num_next;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
729 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
730 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
731
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
732 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
733 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
734 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
735
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
736 #if DOXYGEN || FAT_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
737 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
738 * \ingroup fat_fs
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
739 * Frees a part of a cluster chain and correctly terminates the rest.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
740 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
741 * Marks the specified cluster as the new end of a cluster chain and
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
742 * frees all following clusters.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
743 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
744 * \param[in] fs The filesystem on which to operate.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
745 * \param[in] cluster_num The new end of the cluster chain.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
746 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
747 * \see fat_free_clusters
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
748 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
749 uint8_t fat_terminate_clusters(struct fat_fs_struct* fs, cluster_t cluster_num)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
750 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
751 if(!fs || cluster_num < 2)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
752 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
753
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
754 /* fetch next cluster before overwriting the cluster entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
755 cluster_t cluster_num_next = fat_get_next_cluster(fs, cluster_num);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
756
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
757 /* mark cluster as the last one */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
758 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
759 if(fs->partition->type == PARTITION_TYPE_FAT32)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
760 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
761 uint32_t fat_entry = HTOL32(FAT32_CLUSTER_LAST_MAX);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
762 if(!fs->partition->device_write(fs->header.fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
763 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
764 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
765 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
766 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
767 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
768 uint16_t fat_entry = HTOL16(FAT16_CLUSTER_LAST_MAX);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
769 if(!fs->partition->device_write(fs->header.fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
770 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
771 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
772
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
773 /* free remaining clusters */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
774 if(cluster_num_next)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
775 return fat_free_clusters(fs, cluster_num_next);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
776 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
777 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
778 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
779 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
780
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
781 #if DOXYGEN || FAT_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
782 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
783 * \ingroup fat_fs
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
784 * Clears a single cluster.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
785 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
786 * The complete cluster is filled with zeros.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
787 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
788 * \param[in] fs The filesystem on which to operate.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
789 * \param[in] cluster_num The cluster to clear.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
790 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
791 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
792 uint8_t fat_clear_cluster(const struct fat_fs_struct* fs, cluster_t cluster_num)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
793 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
794 if(cluster_num < 2)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
795 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
796
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
797 offset_t cluster_offset = fat_cluster_offset(fs, cluster_num);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
798
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
799 uint8_t zero[16];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
800 memset(zero, 0, sizeof(zero));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
801 return fs->partition->device_write_interval(cluster_offset,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
802 zero,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
803 fs->header.cluster_size,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
804 fat_clear_cluster_callback,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
805 0
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
806 );
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
807 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
808 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
809
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
810 #if DOXYGEN || FAT_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
811 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
812 * \ingroup fat_fs
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
813 * Callback function for clearing a cluster.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
814 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
815 uintptr_t fat_clear_cluster_callback(uint8_t* buffer, offset_t offset, void* p)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
816 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
817 return 16;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
818 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
819 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
820
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
821 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
822 * \ingroup fat_fs
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
823 * Calculates the offset of the specified cluster.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
824 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
825 * \param[in] fs The filesystem on which to operate.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
826 * \param[in] cluster_num The cluster whose offset to calculate.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
827 * \returns The cluster offset.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
828 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
829 offset_t fat_cluster_offset(const struct fat_fs_struct* fs, cluster_t cluster_num)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
830 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
831 if(!fs || cluster_num < 2)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
832 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
833
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
834 return fs->header.cluster_zero_offset + (offset_t) (cluster_num - 2) * fs->header.cluster_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
835 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
836
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
837 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
838 * \ingroup fat_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
839 * Retrieves the directory entry of a path.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
840 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
841 * The given path may both describe a file or a directory.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
842 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
843 * \param[in] fs The FAT filesystem on which to search.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
844 * \param[in] path The path of which to read the directory entry.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
845 * \param[out] dir_entry The directory entry to fill.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
846 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
847 * \see fat_read_dir
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
848 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
849 uint8_t fat_get_dir_entry_of_path(struct fat_fs_struct* fs, const char* path, struct fat_dir_entry_struct* dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
850 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
851 if(!fs || !path || path[0] == '\0' || !dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
852 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
853
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
854 if(path[0] == '/')
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
855 ++path;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
856
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
857 /* begin with the root directory */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
858 memset(dir_entry, 0, sizeof(*dir_entry));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
859 dir_entry->attributes = FAT_ATTRIB_DIR;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
860
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
861 while(1)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
862 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
863 if(path[0] == '\0')
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
864 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
865
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
866 struct fat_dir_struct* dd = fat_open_dir(fs, dir_entry);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
867 if(!dd)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
868 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
869
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
870 /* extract the next hierarchy we will search for */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
871 const char* sub_path = strchr(path, '/');
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
872 uint8_t length_to_sep;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
873 if(sub_path)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
874 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
875 length_to_sep = sub_path - path;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
876 ++sub_path;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
877 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
878 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
879 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
880 length_to_sep = strlen(path);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
881 sub_path = path + length_to_sep;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
882 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
883
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
884 /* read directory entries */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
885 while(fat_read_dir(dd, dir_entry))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
886 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
887 /* check if we have found the next hierarchy */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
888 if((strlen(dir_entry->long_name) != length_to_sep ||
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
889 strncmp(path, dir_entry->long_name, length_to_sep) != 0))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
890 continue;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
891
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
892 fat_close_dir(dd);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
893 dd = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
894
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
895 if(path[length_to_sep] == '\0')
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
896 /* we iterated through the whole path and have found the file */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
897 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
898
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
899 if(dir_entry->attributes & FAT_ATTRIB_DIR)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
900 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
901 /* we found a parent directory of the file we are searching for */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
902 path = sub_path;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
903 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
904 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
905
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
906 /* a parent of the file exists, but not the file itself */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
907 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
908 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
909
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
910 fat_close_dir(dd);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
911 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
912
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
913 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
914 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
915
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
916 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
917 * \ingroup fat_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
918 * Opens a file on a FAT filesystem.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
919 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
920 * \param[in] fs The filesystem on which the file to open lies.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
921 * \param[in] dir_entry The directory entry of the file to open.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
922 * \returns The file handle, or 0 on failure.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
923 * \see fat_close_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
924 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
925 struct fat_file_struct* fat_open_file(struct fat_fs_struct* fs, const struct fat_dir_entry_struct* dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
926 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
927 if(!fs || !dir_entry || (dir_entry->attributes & FAT_ATTRIB_DIR))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
928 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
929
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
930 #if USE_DYNAMIC_MEMORY
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
931 struct fat_file_struct* fd = malloc(sizeof(*fd));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
932 if(!fd)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
933 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
934 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
935 struct fat_file_struct* fd = fat_file_handles;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
936 uint8_t i;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
937 for(i = 0; i < FAT_FILE_COUNT; ++i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
938 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
939 if(!fd->fs)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
940 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
941
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
942 ++fd;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
943 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
944 if(i >= FAT_FILE_COUNT)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
945 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
946 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
947
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
948 memcpy(&fd->dir_entry, dir_entry, sizeof(*dir_entry));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
949 fd->fs = fs;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
950 fd->pos = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
951 fd->pos_cluster = dir_entry->cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
952
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
953 return fd;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
954 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
955
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
956 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
957 * \ingroup fat_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
958 * Closes a file.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
959 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
960 * \param[in] fd The file handle of the file to close.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
961 * \see fat_open_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
962 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
963 void fat_close_file(struct fat_file_struct* fd)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
964 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
965 if(fd)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
966 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
967 #if FAT_DELAY_DIRENTRY_UPDATE
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
968 /* write directory entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
969 fat_write_dir_entry(fd->fs, &fd->dir_entry);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
970 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
971
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
972 #if USE_DYNAMIC_MEMORY
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
973 free(fd);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
974 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
975 fd->fs = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
976 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
977 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
978 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
979
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
980 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
981 * \ingroup fat_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
982 * Reads data from a file.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
983 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
984 * The data requested is read from the current file location.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
985 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
986 * \param[in] fd The file handle of the file from which to read.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
987 * \param[out] buffer The buffer into which to write.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
988 * \param[in] buffer_len The amount of data to read.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
989 * \returns The number of bytes read, 0 on end of file, or -1 on failure.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
990 * \see fat_write_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
991 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
992 intptr_t fat_read_file(struct fat_file_struct* fd, uint8_t* buffer, uintptr_t buffer_len)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
993 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
994 /* check arguments */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
995 if(!fd || !buffer || buffer_len < 1)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
996 return -1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
997
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
998 /* determine number of bytes to read */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
999 if(fd->pos + buffer_len > fd->dir_entry.file_size)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1000 buffer_len = fd->dir_entry.file_size - fd->pos;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1001 if(buffer_len == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1002 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1003
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1004 uint16_t cluster_size = fd->fs->header.cluster_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1005 cluster_t cluster_num = fd->pos_cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1006 uintptr_t buffer_left = buffer_len;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1007 uint16_t first_cluster_offset = (uint16_t) (fd->pos & (cluster_size - 1));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1008
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1009 /* find cluster in which to start reading */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1010 if(!cluster_num)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1011 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1012 cluster_num = fd->dir_entry.cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1013
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1014 if(!cluster_num)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1015 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1016 if(!fd->pos)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1017 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1018 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1019 return -1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1020 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1021
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1022 if(fd->pos)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1023 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1024 uint32_t pos = fd->pos;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1025 while(pos >= cluster_size)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1026 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1027 pos -= cluster_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1028 cluster_num = fat_get_next_cluster(fd->fs, cluster_num);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1029 if(!cluster_num)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1030 return -1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1031 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1032 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1033 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1034
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1035 /* read data */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1036 do
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1037 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1038 /* calculate data size to copy from cluster */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1039 offset_t cluster_offset = fat_cluster_offset(fd->fs, cluster_num) + first_cluster_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1040 uint16_t copy_length = cluster_size - first_cluster_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1041 if(copy_length > buffer_left)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1042 copy_length = buffer_left;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1043
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1044 /* read data */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1045 if(!fd->fs->partition->device_read(cluster_offset, buffer, copy_length))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1046 return buffer_len - buffer_left;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1047
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1048 /* calculate new file position */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1049 buffer += copy_length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1050 buffer_left -= copy_length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1051 fd->pos += copy_length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1052
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1053 if(first_cluster_offset + copy_length >= cluster_size)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1054 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1055 /* we are on a cluster boundary, so get the next cluster */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1056 if((cluster_num = fat_get_next_cluster(fd->fs, cluster_num)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1057 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1058 first_cluster_offset = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1059 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1060 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1061 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1062 fd->pos_cluster = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1063 return buffer_len - buffer_left;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1064 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1065 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1066
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1067 fd->pos_cluster = cluster_num;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1068
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1069 } while(buffer_left > 0); /* check if we are done */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1070
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1071 return buffer_len;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1072 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1073
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1074 #if DOXYGEN || FAT_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1075 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1076 * \ingroup fat_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1077 * Writes data to a file.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1078 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1079 * The data is written to the current file location.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1080 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1081 * \param[in] fd The file handle of the file to which to write.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1082 * \param[in] buffer The buffer from which to read the data to be written.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1083 * \param[in] buffer_len The amount of data to write.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1084 * \returns The number of bytes written (0 or something less than \c buffer_len on disk full) or -1 on failure.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1085 * \see fat_read_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1086 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1087 intptr_t fat_write_file(struct fat_file_struct* fd, const uint8_t* buffer, uintptr_t buffer_len)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1088 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1089 /* check arguments */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1090 if(!fd || !buffer || buffer_len < 1)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1091 return -1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1092 if(fd->pos > fd->dir_entry.file_size)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1093 return -1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1094
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1095 uint16_t cluster_size = fd->fs->header.cluster_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1096 cluster_t cluster_num = fd->pos_cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1097 uintptr_t buffer_left = buffer_len;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1098 uint16_t first_cluster_offset = (uint16_t) (fd->pos & (cluster_size - 1));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1099
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1100 /* find cluster in which to start writing */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1101 if(!cluster_num)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1102 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1103 cluster_num = fd->dir_entry.cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1104
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1105 if(!cluster_num)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1106 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1107 if(!fd->pos)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1108 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1109 /* empty file */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1110 fd->dir_entry.cluster = cluster_num = fat_append_clusters(fd->fs, 0, 1);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1111 if(!cluster_num)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1112 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1113 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1114 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1115 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1116 return -1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1117 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1118 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1119
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1120 if(fd->pos)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1121 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1122 uint32_t pos = fd->pos;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1123 cluster_t cluster_num_next;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1124 while(pos >= cluster_size)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1125 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1126 pos -= cluster_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1127 cluster_num_next = fat_get_next_cluster(fd->fs, cluster_num);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1128 if(!cluster_num_next)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1129 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1130 if(pos != 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1131 return -1; /* current file position points beyond end of file */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1132
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1133 /* the file exactly ends on a cluster boundary, and we append to it */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1134 cluster_num_next = fat_append_clusters(fd->fs, cluster_num, 1);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1135 if(!cluster_num_next)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1136 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1137 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1138
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1139 cluster_num = cluster_num_next;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1140 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1141 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1142 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1143
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1144 /* write data */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1145 do
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1146 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1147 /* calculate data size to write to cluster */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1148 offset_t cluster_offset = fat_cluster_offset(fd->fs, cluster_num) + first_cluster_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1149 uint16_t write_length = cluster_size - first_cluster_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1150 if(write_length > buffer_left)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1151 write_length = buffer_left;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1152
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1153 /* write data which fits into the current cluster */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1154 if(!fd->fs->partition->device_write(cluster_offset, buffer, write_length))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1155 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1156
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1157 /* calculate new file position */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1158 buffer += write_length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1159 buffer_left -= write_length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1160 fd->pos += write_length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1161
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1162 if(first_cluster_offset + write_length >= cluster_size)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1163 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1164 /* we are on a cluster boundary, so get the next cluster */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1165 cluster_t cluster_num_next = fat_get_next_cluster(fd->fs, cluster_num);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1166 if(!cluster_num_next && buffer_left > 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1167 /* we reached the last cluster, append a new one */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1168 cluster_num_next = fat_append_clusters(fd->fs, cluster_num, 1);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1169 if(!cluster_num_next)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1170 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1171 fd->pos_cluster = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1172 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1173 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1174
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1175 cluster_num = cluster_num_next;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1176 first_cluster_offset = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1177 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1178
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1179 fd->pos_cluster = cluster_num;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1180
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1181 } while(buffer_left > 0); /* check if we are done */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1182
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1183 /* update directory entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1184 if(fd->pos > fd->dir_entry.file_size)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1185 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1186 #if !FAT_DELAY_DIRENTRY_UPDATE
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1187 uint32_t size_old = fd->dir_entry.file_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1188 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1189
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1190 /* update file size */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1191 fd->dir_entry.file_size = fd->pos;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1192
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1193 #if !FAT_DELAY_DIRENTRY_UPDATE
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1194 /* write directory entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1195 if(!fat_write_dir_entry(fd->fs, &fd->dir_entry))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1196 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1197 /* We do not return an error here since we actually wrote
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1198 * some data to disk. So we calculate the amount of data
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1199 * we wrote to disk and which lies within the old file size.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1200 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1201 buffer_left = fd->pos - size_old;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1202 fd->pos = size_old;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1203 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1204 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1205 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1206
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1207 return buffer_len - buffer_left;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1208 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1209 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1210
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1211 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1212 * \ingroup fat_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1213 * Repositions the read/write file offset.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1214 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1215 * Changes the file offset where the next call to fat_read_file()
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1216 * or fat_write_file() starts reading/writing.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1217 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1218 * If the new offset is beyond the end of the file, fat_resize_file()
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1219 * is implicitly called, i.e. the file is expanded.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1220 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1221 * The new offset can be given in different ways determined by
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1222 * the \c whence parameter:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1223 * - \b FAT_SEEK_SET: \c *offset is relative to the beginning of the file.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1224 * - \b FAT_SEEK_CUR: \c *offset is relative to the current file position.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1225 * - \b FAT_SEEK_END: \c *offset is relative to the end of the file.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1226 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1227 * The resulting absolute offset is written to the location the \c offset
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1228 * parameter points to.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1229 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1230 * Calling this function can also be used to retrieve the current file position:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1231 \code
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1232 int32_t file_pos = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1233 if(!fat_seek_file(fd, &file_pos, FAT_SEEK_CUR))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1234 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1235 // error
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1236 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1237 // file_pos now contains the absolute file position
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1238 \endcode
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1239 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1240 * \param[in] fd The file decriptor of the file on which to seek.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1241 * \param[in,out] offset A pointer to the new offset, as affected by the \c whence
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1242 * parameter. The function writes the new absolute offset
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1243 * to this location before it returns.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1244 * \param[in] whence Affects the way \c offset is interpreted, see above.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1245 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1246 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1247 uint8_t fat_seek_file(struct fat_file_struct* fd, int32_t* offset, uint8_t whence)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1248 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1249 if(!fd || !offset)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1250 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1251
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1252 uint32_t new_pos = fd->pos;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1253 switch(whence)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1254 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1255 case FAT_SEEK_SET:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1256 new_pos = *offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1257 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1258 case FAT_SEEK_CUR:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1259 new_pos += *offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1260 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1261 case FAT_SEEK_END:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1262 new_pos = fd->dir_entry.file_size + *offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1263 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1264 default:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1265 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1266 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1267
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1268 if(new_pos > fd->dir_entry.file_size
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1269 #if FAT_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1270 && !fat_resize_file(fd, new_pos)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1271 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1272 )
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1273 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1274
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1275 fd->pos = new_pos;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1276 fd->pos_cluster = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1277
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1278 *offset = (int32_t) new_pos;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1279 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1280 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1281
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1282 #if DOXYGEN || FAT_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1283 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1284 * \ingroup fat_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1285 * Resizes a file to have a specific size.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1286 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1287 * Enlarges or shrinks the file pointed to by the file descriptor to have
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1288 * exactly the specified size.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1289 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1290 * If the file is truncated, all bytes having an equal or larger offset
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1291 * than the given size are lost. If the file is expanded, the additional
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1292 * bytes are allocated.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1293 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1294 * \note Please be aware that this function just allocates or deallocates disk
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1295 * space, it does not explicitely clear it. To avoid data leakage, this
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1296 * must be done manually.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1297 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1298 * \param[in] fd The file decriptor of the file which to resize.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1299 * \param[in] size The new size of the file.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1300 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1301 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1302 uint8_t fat_resize_file(struct fat_file_struct* fd, uint32_t size)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1303 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1304 if(!fd)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1305 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1306
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1307 cluster_t cluster_num = fd->dir_entry.cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1308 uint16_t cluster_size = fd->fs->header.cluster_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1309 uint32_t size_new = size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1310
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1311 do
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1312 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1313 if(cluster_num == 0 && size_new == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1314 /* the file stays empty */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1315 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1316
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1317 /* seek to the next cluster as long as we need the space */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1318 while(size_new > cluster_size)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1319 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1320 /* get next cluster of file */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1321 cluster_t cluster_num_next = fat_get_next_cluster(fd->fs, cluster_num);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1322 if(cluster_num_next)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1323 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1324 cluster_num = cluster_num_next;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1325 size_new -= cluster_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1326 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1327 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1328 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1329 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1330 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1331 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1332
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1333 if(size_new > cluster_size || cluster_num == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1334 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1335 /* Allocate new cluster chain and append
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1336 * it to the existing one, if available.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1337 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1338 cluster_t cluster_count = (size_new + cluster_size - 1) / cluster_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1339 cluster_t cluster_new_chain = fat_append_clusters(fd->fs, cluster_num, cluster_count);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1340 if(!cluster_new_chain)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1341 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1342
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1343 if(!cluster_num)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1344 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1345 cluster_num = cluster_new_chain;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1346 fd->dir_entry.cluster = cluster_num;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1347 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1348 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1349
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1350 /* write new directory entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1351 fd->dir_entry.file_size = size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1352 if(size == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1353 fd->dir_entry.cluster = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1354 if(!fat_write_dir_entry(fd->fs, &fd->dir_entry))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1355 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1356
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1357 if(size == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1358 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1359 /* free all clusters of file */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1360 fat_free_clusters(fd->fs, cluster_num);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1361 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1362 else if(size_new <= cluster_size)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1363 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1364 /* free all clusters no longer needed */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1365 fat_terminate_clusters(fd->fs, cluster_num);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1366 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1367
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1368 } while(0);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1369
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1370 /* correct file position */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1371 if(size < fd->pos)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1372 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1373 fd->pos = size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1374 fd->pos_cluster = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1375 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1376
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1377 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1378 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1379 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1380
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1381 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1382 * \ingroup fat_dir
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1383 * Opens a directory.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1384 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1385 * \param[in] fs The filesystem on which the directory to open resides.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1386 * \param[in] dir_entry The directory entry which stands for the directory to open.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1387 * \returns An opaque directory descriptor on success, 0 on failure.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1388 * \see fat_close_dir
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1389 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1390 struct fat_dir_struct* fat_open_dir(struct fat_fs_struct* fs, const struct fat_dir_entry_struct* dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1391 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1392 if(!fs || !dir_entry || !(dir_entry->attributes & FAT_ATTRIB_DIR))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1393 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1394
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1395 #if USE_DYNAMIC_MEMORY
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1396 struct fat_dir_struct* dd = malloc(sizeof(*dd));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1397 if(!dd)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1398 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1399 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1400 struct fat_dir_struct* dd = fat_dir_handles;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1401 uint8_t i;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1402 for(i = 0; i < FAT_DIR_COUNT; ++i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1403 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1404 if(!dd->fs)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1405 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1406
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1407 ++dd;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1408 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1409 if(i >= FAT_DIR_COUNT)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1410 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1411 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1412
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1413 memcpy(&dd->dir_entry, dir_entry, sizeof(*dir_entry));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1414 dd->fs = fs;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1415 dd->entry_cluster = dir_entry->cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1416 dd->entry_offset = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1417
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1418 return dd;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1419 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1420
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1421 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1422 * \ingroup fat_dir
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1423 * Closes a directory descriptor.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1424 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1425 * This function destroys a directory descriptor which was
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1426 * previously obtained by calling fat_open_dir(). When this
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1427 * function returns, the given descriptor will be invalid.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1428 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1429 * \param[in] dd The directory descriptor to close.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1430 * \see fat_open_dir
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1431 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1432 void fat_close_dir(struct fat_dir_struct* dd)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1433 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1434 if(dd)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1435 #if USE_DYNAMIC_MEMORY
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1436 free(dd);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1437 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1438 dd->fs = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1439 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1440 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1441
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1442 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1443 * \ingroup fat_dir
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1444 * Reads the next directory entry contained within a parent directory.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1445 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1446 * \param[in] dd The descriptor of the parent directory from which to read the entry.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1447 * \param[out] dir_entry Pointer to a buffer into which to write the directory entry information.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1448 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1449 * \see fat_reset_dir
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1450 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1451 uint8_t fat_read_dir(struct fat_dir_struct* dd, struct fat_dir_entry_struct* dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1452 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1453 if(!dd || !dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1454 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1455
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1456 /* get current position of directory handle */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1457 struct fat_fs_struct* fs = dd->fs;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1458 const struct fat_header_struct* header = &fs->header;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1459 uint16_t cluster_size = header->cluster_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1460 cluster_t cluster_num = dd->entry_cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1461 uint16_t cluster_offset = dd->entry_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1462 struct fat_read_dir_callback_arg arg;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1463
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1464 if(cluster_offset >= cluster_size)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1465 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1466 /* The latest call hit the border of the last cluster in
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1467 * the chain, but it still returned a directory entry.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1468 * So we now reset the handle and signal the caller the
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1469 * end of the listing.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1470 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1471 fat_reset_dir(dd);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1472 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1473 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1474
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1475 /* reset callback arguments */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1476 memset(&arg, 0, sizeof(arg));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1477 memset(dir_entry, 0, sizeof(*dir_entry));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1478 arg.dir_entry = dir_entry;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1479
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1480 /* check if we read from the root directory */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1481 if(cluster_num == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1482 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1483 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1484 if(fs->partition->type == PARTITION_TYPE_FAT32)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1485 cluster_num = header->root_dir_cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1486 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1487 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1488 cluster_size = header->cluster_zero_offset - header->root_dir_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1489 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1490
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1491 /* read entries */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1492 uint8_t buffer[32];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1493 while(!arg.finished)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1494 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1495 /* read directory entries up to the cluster border */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1496 uint16_t cluster_left = cluster_size - cluster_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1497 offset_t pos = cluster_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1498 if(cluster_num == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1499 pos += header->root_dir_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1500 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1501 pos += fat_cluster_offset(fs, cluster_num);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1502
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1503 arg.bytes_read = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1504 if(!fs->partition->device_read_interval(pos,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1505 buffer,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1506 sizeof(buffer),
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1507 cluster_left,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1508 fat_dir_entry_read_callback,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1509 &arg)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1510 )
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1511 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1512
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1513 cluster_offset += arg.bytes_read;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1514
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1515 if(cluster_offset >= cluster_size)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1516 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1517 /* we reached the cluster border and switch to the next cluster */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1518
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1519 /* get number of next cluster */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1520 if((cluster_num = fat_get_next_cluster(fs, cluster_num)) != 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1521 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1522 cluster_offset = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1523 continue;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1524 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1525
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1526 /* we are at the end of the cluster chain */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1527 if(!arg.finished)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1528 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1529 /* directory entry not found, reset directory handle */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1530 fat_reset_dir(dd);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1531 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1532 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1533 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1534 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1535 /* The current execution of the function has been successful,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1536 * so we can not signal an end of the directory listing to
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1537 * the caller, but must wait for the next call. So we keep an
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1538 * invalid cluster offset to mark this directory handle's
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1539 * traversal as finished.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1540 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1541 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1542
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1543 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1544 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1545 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1546
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1547 dd->entry_cluster = cluster_num;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1548 dd->entry_offset = cluster_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1549
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1550 return arg.finished;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1551 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1552
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1553 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1554 * \ingroup fat_dir
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1555 * Resets a directory handle.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1556 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1557 * Resets the directory handle such that reading restarts
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1558 * with the first directory entry.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1559 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1560 * \param[in] dd The directory handle to reset.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1561 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1562 * \see fat_read_dir
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1563 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1564 uint8_t fat_reset_dir(struct fat_dir_struct* dd)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1565 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1566 if(!dd)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1567 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1568
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1569 dd->entry_cluster = dd->dir_entry.cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1570 dd->entry_offset = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1571 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1572 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1573
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1574 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1575 * \ingroup fat_fs
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1576 * Callback function for reading a directory entry.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1577 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1578 * Interprets a raw directory entry and puts the contained
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1579 * information into a fat_dir_entry_struct structure.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1580 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1581 * For a single file there may exist multiple directory
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1582 * entries. All except the last one are lfn entries, which
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1583 * contain parts of the long filename. The last directory
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1584 * entry is a traditional 8.3 style one. It contains all
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1585 * other information like size, cluster, date and time.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1586 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1587 * \param[in] buffer A pointer to 32 bytes of raw data.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1588 * \param[in] offset The absolute offset of the raw data.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1589 * \param[in,out] p An argument structure controlling operation.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1590 * \returns 0 on failure or completion, 1 if reading has
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1591 * to be continued
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1592 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1593 uint8_t fat_dir_entry_read_callback(uint8_t* buffer, offset_t offset, void* p)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1594 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1595 struct fat_read_dir_callback_arg* arg = p;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1596 struct fat_dir_entry_struct* dir_entry = arg->dir_entry;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1597
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1598 arg->bytes_read += 32;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1599
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1600 /* skip deleted or empty entries */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1601 if(buffer[0] == FAT_DIRENTRY_DELETED || !buffer[0])
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1602 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1603 #if FAT_LFN_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1604 arg->checksum = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1605 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1606 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1607 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1608
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1609 #if !FAT_LFN_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1610 /* skip lfn entries */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1611 if(buffer[11] == 0x0f)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1612 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1613 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1614
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1615 char* long_name = dir_entry->long_name;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1616 #if FAT_LFN_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1617 if(buffer[11] == 0x0f)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1618 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1619 /* checksum validation */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1620 if(arg->checksum == 0 || arg->checksum != buffer[13])
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1621 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1622 /* reset directory entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1623 memset(dir_entry, 0, sizeof(*dir_entry));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1624
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1625 arg->checksum = buffer[13];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1626 dir_entry->entry_offset = offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1627 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1628
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1629 /* lfn supports unicode, but we do not, for now.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1630 * So we assume pure ascii and read only every
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1631 * second byte.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1632 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1633 uint16_t char_offset = ((buffer[0] & 0x3f) - 1) * 13;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1634 const uint8_t char_mapping[] = { 1, 3, 5, 7, 9, 14, 16, 18, 20, 22, 24, 28, 30 };
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1635 for(uint8_t i = 0; i <= 12 && char_offset + i < sizeof(dir_entry->long_name) - 1; ++i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1636 long_name[char_offset + i] = buffer[char_mapping[i]];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1637
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1638 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1639 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1640 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1641 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1642 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1643 #if FAT_LFN_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1644 /* if we do not have a long name or the previous lfn does not match, take the 8.3 name */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1645 if(long_name[0] == '\0' || arg->checksum != fat_calc_83_checksum(buffer))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1646 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1647 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1648 /* reset directory entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1649 memset(dir_entry, 0, sizeof(*dir_entry));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1650 dir_entry->entry_offset = offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1651
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1652 uint8_t i;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1653 for(i = 0; i < 8; ++i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1654 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1655 if(buffer[i] == ' ')
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1656 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1657 long_name[i] = buffer[i];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1658
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1659 /* Windows NT and later versions do not store lfn entries
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1660 * for 8.3 names which have a lowercase basename, extension
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1661 * or both when everything else is uppercase. They use two
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1662 * extra bits to signal a lowercase basename or extension.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1663 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1664 if((buffer[12] & 0x08) && buffer[i] >= 'A' && buffer[i] <= 'Z')
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1665 long_name[i] += 'a' - 'A';
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1666 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1667 if(long_name[0] == 0x05)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1668 long_name[0] = (char) FAT_DIRENTRY_DELETED;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1669
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1670 if(buffer[8] != ' ')
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1671 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1672 long_name[i++] = '.';
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1673
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1674 uint8_t j = 8;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1675 for(; j < 11; ++j)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1676 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1677 if(buffer[j] == ' ')
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1678 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1679 long_name[i] = buffer[j];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1680
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1681 /* See above for the lowercase 8.3 name handling of
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1682 * Windows NT and later.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1683 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1684 if((buffer[12] & 0x10) && buffer[j] >= 'A' && buffer[j] <= 'Z')
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1685 long_name[i] += 'a' - 'A';
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1686
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1687 ++i;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1688 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1689 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1690
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1691 long_name[i] = '\0';
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1692 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1693
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1694 /* extract properties of file and store them within the structure */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1695 dir_entry->attributes = buffer[11];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1696 dir_entry->cluster = read16(&buffer[26]);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1697 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1698 dir_entry->cluster |= ((cluster_t) read16(&buffer[20])) << 16;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1699 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1700 dir_entry->file_size = read32(&buffer[28]);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1701
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1702 #if FAT_DATETIME_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1703 dir_entry->modification_time = read16(&buffer[22]);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1704 dir_entry->modification_date = read16(&buffer[24]);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1705 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1706
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1707 arg->finished = 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1708 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1709 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1710 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1711
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1712 #if DOXYGEN || FAT_LFN_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1713 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1714 * \ingroup fat_fs
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1715 * Calculates the checksum for 8.3 names used within the
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1716 * corresponding lfn directory entries.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1717 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1718 * \param[in] file_name_83 The 11-byte file name buffer.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1719 * \returns The checksum of the given file name.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1720 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1721 uint8_t fat_calc_83_checksum(const uint8_t* file_name_83)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1722 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1723 uint8_t checksum = file_name_83[0];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1724 for(uint8_t i = 1; i < 11; ++i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1725 checksum = ((checksum >> 1) | (checksum << 7)) + file_name_83[i];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1726
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1727 return checksum;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1728 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1729 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1730
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1731 #if DOXYGEN || FAT_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1732 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1733 * \ingroup fat_fs
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1734 * Searches for space where to store a directory entry.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1735 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1736 * \param[in] fs The filesystem on which to operate.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1737 * \param[in] parent The directory in which to search.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1738 * \param[in] dir_entry The directory entry for which to search space.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1739 * \returns 0 on failure, a device offset on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1740 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1741 offset_t fat_find_offset_for_dir_entry(struct fat_fs_struct* fs, const struct fat_dir_struct* parent, const struct fat_dir_entry_struct* dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1742 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1743 if(!fs || !dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1744 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1745
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1746 /* search for a place where to write the directory entry to disk */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1747 #if FAT_LFN_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1748 uint8_t free_dir_entries_needed = (strlen(dir_entry->long_name) + 12) / 13 + 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1749 uint8_t free_dir_entries_found = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1750 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1751 cluster_t cluster_num = parent->dir_entry.cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1752 offset_t dir_entry_offset = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1753 offset_t offset = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1754 offset_t offset_to = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1755 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1756 uint8_t is_fat32 = (fs->partition->type == PARTITION_TYPE_FAT32);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1757 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1758
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1759 if(cluster_num == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1760 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1761 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1762 if(is_fat32)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1763 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1764 cluster_num = fs->header.root_dir_cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1765 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1766 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1767 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1768 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1769 /* we read/write from the root directory entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1770 offset = fs->header.root_dir_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1771 offset_to = fs->header.cluster_zero_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1772 dir_entry_offset = offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1773 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1774 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1775
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1776 while(1)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1777 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1778 if(offset == offset_to)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1779 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1780 if(cluster_num == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1781 /* We iterated through the whole root directory and
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1782 * could not find enough space for the directory entry.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1783 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1784 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1785
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1786 if(offset)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1787 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1788 /* We reached a cluster boundary and have to
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1789 * switch to the next cluster.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1790 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1791
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1792 cluster_t cluster_next = fat_get_next_cluster(fs, cluster_num);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1793 if(!cluster_next)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1794 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1795 cluster_next = fat_append_clusters(fs, cluster_num, 1);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1796 if(!cluster_next)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1797 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1798
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1799 /* we appended a new cluster and know it is free */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1800 dir_entry_offset = fs->header.cluster_zero_offset +
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1801 (offset_t) (cluster_next - 2) * fs->header.cluster_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1802
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1803 /* clear cluster to avoid garbage directory entries */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1804 fat_clear_cluster(fs, cluster_next);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1805
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1806 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1807 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1808 cluster_num = cluster_next;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1809 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1810
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1811 offset = fat_cluster_offset(fs, cluster_num);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1812 offset_to = offset + fs->header.cluster_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1813 dir_entry_offset = offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1814 #if FAT_LFN_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1815 free_dir_entries_found = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1816 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1817 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1818
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1819 /* read next lfn or 8.3 entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1820 uint8_t first_char;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1821 if(!fs->partition->device_read(offset, &first_char, sizeof(first_char)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1822 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1823
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1824 /* check if we found a free directory entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1825 if(first_char == FAT_DIRENTRY_DELETED || !first_char)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1826 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1827 /* check if we have the needed number of available entries */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1828 #if FAT_LFN_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1829 ++free_dir_entries_found;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1830 if(free_dir_entries_found >= free_dir_entries_needed)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1831 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1832 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1833
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1834 offset += 32;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1835 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1836 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1837 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1838 offset += 32;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1839 dir_entry_offset = offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1840 #if FAT_LFN_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1841 free_dir_entries_found = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1842 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1843 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1844 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1845
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1846 return dir_entry_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1847 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1848 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1849
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1850 #if DOXYGEN || FAT_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1851 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1852 * \ingroup fat_fs
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1853 * Writes a directory entry to disk.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1854 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1855 * \note The file name is not checked for invalid characters.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1856 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1857 * \note The generation of the short 8.3 file name is quite
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1858 * simple. The first eight characters are used for the filename.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1859 * The extension, if any, is made up of the first three characters
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1860 * following the last dot within the long filename. If the
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1861 * filename (without the extension) is longer than eight characters,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1862 * the lower byte of the cluster number replaces the last two
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1863 * characters to avoid name clashes. In any other case, it is your
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1864 * responsibility to avoid name clashes.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1865 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1866 * \param[in] fs The filesystem on which to operate.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1867 * \param[in] dir_entry The directory entry to write.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1868 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1869 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1870 uint8_t fat_write_dir_entry(const struct fat_fs_struct* fs, struct fat_dir_entry_struct* dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1871 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1872 if(!fs || !dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1873 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1874
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1875 #if FAT_DATETIME_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1876 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1877 uint16_t year;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1878 uint8_t month;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1879 uint8_t day;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1880 uint8_t hour;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1881 uint8_t min;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1882 uint8_t sec;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1883
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1884 fat_get_datetime(&year, &month, &day, &hour, &min, &sec);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1885 fat_set_file_modification_date(dir_entry, year, month, day);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1886 fat_set_file_modification_time(dir_entry, hour, min, sec);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1887 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1888 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1889
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1890 device_write_t device_write = fs->partition->device_write;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1891 offset_t offset = dir_entry->entry_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1892 const char* name = dir_entry->long_name;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1893 uint8_t name_len = strlen(name);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1894 #if FAT_LFN_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1895 uint8_t lfn_entry_count = (name_len + 12) / 13;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1896 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1897 uint8_t buffer[32];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1898
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1899 /* write 8.3 entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1900
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1901 /* generate 8.3 file name */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1902 memset(&buffer[0], ' ', 11);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1903 char* name_ext = strrchr(name, '.');
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1904 if(name_ext && *++name_ext)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1905 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1906 uint8_t name_ext_len = strlen(name_ext);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1907 name_len -= name_ext_len + 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1908
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1909 if(name_ext_len > 3)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1910 #if FAT_LFN_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1911 name_ext_len = 3;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1912 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1913 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1914 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1915
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1916 memcpy(&buffer[8], name_ext, name_ext_len);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1917 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1918
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1919 if(name_len <= 8)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1920 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1921 memcpy(buffer, name, name_len);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1922
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1923 #if FAT_LFN_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1924 /* For now, we create lfn entries for all files,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1925 * except the "." and ".." directory references.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1926 * This is to avoid difficulties with capitalization,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1927 * as 8.3 filenames allow uppercase letters only.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1928 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1929 * Theoretically it would be possible to leave
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1930 * the 8.3 entry alone if the basename and the
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1931 * extension have no mixed capitalization.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1932 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1933 if(name[0] == '.' &&
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1934 ((name[1] == '.' && name[2] == '\0') ||
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1935 name[1] == '\0')
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1936 )
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1937 lfn_entry_count = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1938 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1939 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1940 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1941 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1942 #if FAT_LFN_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1943 memcpy(buffer, name, 8);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1944
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1945 /* Minimize 8.3 name clashes by appending
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1946 * the lower byte of the cluster number.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1947 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1948 uint8_t num = dir_entry->cluster & 0xff;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1949
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1950 buffer[6] = (num < 0xa0) ? ('0' + (num >> 4)) : ('a' + (num >> 4));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1951 num &= 0x0f;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1952 buffer[7] = (num < 0x0a) ? ('0' + num) : ('a' + num);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1953 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1954 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1955 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1956 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1957 if(buffer[0] == FAT_DIRENTRY_DELETED)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1958 buffer[0] = 0x05;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1959
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1960 /* fill directory entry buffer */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1961 memset(&buffer[11], 0, sizeof(buffer) - 11);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1962 buffer[0x0b] = dir_entry->attributes;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1963 #if FAT_DATETIME_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1964 write16(&buffer[0x16], dir_entry->modification_time);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1965 write16(&buffer[0x18], dir_entry->modification_date);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1966 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1967 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1968 write16(&buffer[0x14], (uint16_t) (dir_entry->cluster >> 16));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1969 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1970 write16(&buffer[0x1a], dir_entry->cluster);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1971 write32(&buffer[0x1c], dir_entry->file_size);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1972
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1973 /* write to disk */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1974 #if FAT_LFN_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1975 if(!device_write(offset + (uint16_t) lfn_entry_count * 32, buffer, sizeof(buffer)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1976 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1977 if(!device_write(offset, buffer, sizeof(buffer)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1978 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1979 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1980
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1981 #if FAT_LFN_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1982 /* calculate checksum of 8.3 name */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1983 uint8_t checksum = fat_calc_83_checksum(buffer);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1984
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1985 /* write lfn entries */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1986 for(uint8_t lfn_entry = lfn_entry_count; lfn_entry > 0; --lfn_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1987 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1988 memset(buffer, 0xff, sizeof(buffer));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1989
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1990 /* set file name */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1991 const char* long_name_curr = name + (lfn_entry - 1) * 13;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1992 uint8_t i = 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1993 while(i < 0x1f)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1994 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1995 buffer[i++] = *long_name_curr;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1996 buffer[i++] = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1997
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1998 switch(i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1999 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2000 case 0x0b:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2001 i = 0x0e;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2002 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2003 case 0x1a:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2004 i = 0x1c;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2005 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2006 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2007
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2008 if(!*long_name_curr++)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2009 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2010 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2011
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2012 /* set index of lfn entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2013 buffer[0x00] = lfn_entry;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2014 if(lfn_entry == lfn_entry_count)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2015 buffer[0x00] |= FAT_DIRENTRY_LFNLAST;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2016
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2017 /* mark as lfn entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2018 buffer[0x0b] = 0x0f;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2019
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2020 /* set 8.3 checksum */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2021 buffer[0x0d] = checksum;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2022
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2023 /* clear reserved bytes */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2024 buffer[0x0c] = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2025 buffer[0x1a] = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2026 buffer[0x1b] = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2027
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2028 /* write entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2029 device_write(offset, buffer, sizeof(buffer));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2030
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2031 offset += sizeof(buffer);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2032 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2033 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2034
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2035 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2036 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2037 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2038
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2039 #if DOXYGEN || FAT_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2040 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2041 * \ingroup fat_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2042 * Creates a file.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2043 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2044 * Creates a file and obtains the directory entry of the
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2045 * new file. If the file to create already exists, the
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2046 * directory entry of the existing file will be returned
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2047 * within the dir_entry parameter.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2048 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2049 * \note The file name is not checked for invalid characters.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2050 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2051 * \note The generation of the short 8.3 file name is quite
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2052 * simple. The first eight characters are used for the filename.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2053 * The extension, if any, is made up of the first three characters
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2054 * following the last dot within the long filename. If the
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2055 * filename (without the extension) is longer than eight characters,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2056 * the lower byte of the cluster number replaces the last two
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2057 * characters to avoid name clashes. In any other case, it is your
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2058 * responsibility to avoid name clashes.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2059 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2060 * \param[in] parent The handle of the directory in which to create the file.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2061 * \param[in] file The name of the file to create.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2062 * \param[out] dir_entry The directory entry to fill for the new (or existing) file.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2063 * \returns 0 on failure, 1 on success, 2 if the file already existed.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2064 * \see fat_delete_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2065 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2066 uint8_t fat_create_file(struct fat_dir_struct* parent, const char* file, struct fat_dir_entry_struct* dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2067 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2068 if(!parent || !file || !file[0] || !dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2069 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2070
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2071 /* check if the file already exists */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2072 while(1)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2073 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2074 if(!fat_read_dir(parent, dir_entry))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2075 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2076
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2077 if(strcmp(file, dir_entry->long_name) == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2078 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2079 fat_reset_dir(parent);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2080 return 2;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2081 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2082 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2083
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2084 struct fat_fs_struct* fs = parent->fs;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2085
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2086 /* prepare directory entry with values already known */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2087 memset(dir_entry, 0, sizeof(*dir_entry));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2088 strncpy(dir_entry->long_name, file, sizeof(dir_entry->long_name) - 1);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2089
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2090 /* find place where to store directory entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2091 if(!(dir_entry->entry_offset = fat_find_offset_for_dir_entry(fs, parent, dir_entry)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2092 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2093
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2094 /* write directory entry to disk */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2095 if(!fat_write_dir_entry(fs, dir_entry))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2096 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2097
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2098 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2099 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2100 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2101
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2102 #if DOXYGEN || FAT_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2103 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2104 * \ingroup fat_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2105 * Deletes a file or directory.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2106 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2107 * If a directory is deleted without first deleting its
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2108 * subdirectories and files, disk space occupied by these
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2109 * files will get wasted as there is no chance to release
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2110 * it and mark it as free.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2111 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2112 * \param[in] fs The filesystem on which to operate.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2113 * \param[in] dir_entry The directory entry of the file to delete.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2114 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2115 * \see fat_create_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2116 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2117 uint8_t fat_delete_file(struct fat_fs_struct* fs, struct fat_dir_entry_struct* dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2118 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2119 if(!fs || !dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2120 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2121
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2122 /* get offset of the file's directory entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2123 offset_t dir_entry_offset = dir_entry->entry_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2124 if(!dir_entry_offset)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2125 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2126
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2127 #if FAT_LFN_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2128 uint8_t buffer[12];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2129 while(1)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2130 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2131 /* read directory entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2132 if(!fs->partition->device_read(dir_entry_offset, buffer, sizeof(buffer)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2133 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2134
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2135 /* mark the directory entry as deleted */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2136 buffer[0] = FAT_DIRENTRY_DELETED;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2137
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2138 /* write back entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2139 if(!fs->partition->device_write(dir_entry_offset, buffer, sizeof(buffer)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2140 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2141
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2142 /* check if we deleted the whole entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2143 if(buffer[11] != 0x0f)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2144 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2145
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2146 dir_entry_offset += 32;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2147 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2148 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2149 /* mark the directory entry as deleted */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2150 uint8_t first_char = FAT_DIRENTRY_DELETED;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2151 if(!fs->partition->device_write(dir_entry_offset, &first_char, 1))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2152 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2153 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2154
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2155 /* We deleted the directory entry. The next thing to do is
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2156 * marking all occupied clusters as free.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2157 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2158 return (dir_entry->cluster == 0 || fat_free_clusters(fs, dir_entry->cluster));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2159 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2160 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2161
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2162 #if DOXYGEN || FAT_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2163 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2164 * \ingroup fat_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2165 * Moves or renames a file.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2166 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2167 * Changes a file's name, optionally moving it into another
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2168 * directory as well. Before calling this function, the
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2169 * target file name must not exist. Moving a file to a
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2170 * different filesystem (i.e. \a parent_new doesn't lie on
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2171 * \a fs) is not supported.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2172 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2173 * After successfully renaming (and moving) the file, the
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2174 * given directory entry is updated such that it points to
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2175 * the file's new location.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2176 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2177 * \note The notes which apply to fat_create_file() also
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2178 * apply to this function.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2179 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2180 * \param[in] fs The filesystem on which to operate.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2181 * \param[in,out] dir_entry The directory entry of the file to move.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2182 * \param[in] parent_new The handle of the new parent directory of the file.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2183 * \param[in] file_new The file's new name.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2184 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2185 * \see fat_create_file, fat_delete_file, fat_move_dir
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2186 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2187 uint8_t fat_move_file(struct fat_fs_struct* fs, struct fat_dir_entry_struct* dir_entry, struct fat_dir_struct* parent_new, const char* file_new)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2188 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2189 if(!fs || !dir_entry || !parent_new || (file_new && !file_new[0]))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2190 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2191 if(fs != parent_new->fs)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2192 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2193
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2194 /* use existing file name if none has been specified */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2195 if(!file_new)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2196 file_new = dir_entry->long_name;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2197
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2198 /* create file with new file name */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2199 struct fat_dir_entry_struct dir_entry_new;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2200 if(!fat_create_file(parent_new, file_new, &dir_entry_new))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2201 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2202
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2203 /* copy members of directory entry which do not change with rename */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2204 dir_entry_new.attributes = dir_entry->attributes;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2205 #if FAT_DATETIME_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2206 dir_entry_new.modification_time = dir_entry->modification_time;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2207 dir_entry_new.modification_date = dir_entry->modification_date;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2208 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2209 dir_entry_new.cluster = dir_entry->cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2210 dir_entry_new.file_size = dir_entry->file_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2211
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2212 /* make the new file name point to the old file's content */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2213 if(!fat_write_dir_entry(fs, &dir_entry_new))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2214 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2215 fat_delete_file(fs, &dir_entry_new);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2216 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2217 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2218
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2219 /* delete the old file, but not its clusters, which have already been remapped above */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2220 dir_entry->cluster = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2221 if(!fat_delete_file(fs, dir_entry))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2222 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2223
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2224 *dir_entry = dir_entry_new;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2225 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2226 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2227 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2228
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2229 #if DOXYGEN || FAT_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2230 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2231 * \ingroup fat_dir
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2232 * Creates a directory.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2233 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2234 * Creates a directory and obtains its directory entry.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2235 * If the directory to create already exists, its
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2236 * directory entry will be returned within the dir_entry
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2237 * parameter.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2238 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2239 * \note The notes which apply to fat_create_file() also
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2240 * apply to this function.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2241 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2242 * \param[in] parent The handle of the parent directory of the new directory.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2243 * \param[in] dir The name of the directory to create.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2244 * \param[out] dir_entry The directory entry to fill for the new directory.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2245 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2246 * \see fat_delete_dir
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2247 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2248 uint8_t fat_create_dir(struct fat_dir_struct* parent, const char* dir, struct fat_dir_entry_struct* dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2249 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2250 if(!parent || !dir || !dir[0] || !dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2251 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2252
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2253 /* check if the file or directory already exists */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2254 while(fat_read_dir(parent, dir_entry))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2255 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2256 if(strcmp(dir, dir_entry->long_name) == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2257 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2258 fat_reset_dir(parent);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2259 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2260 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2261 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2262
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2263 struct fat_fs_struct* fs = parent->fs;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2264
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2265 /* allocate cluster which will hold directory entries */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2266 cluster_t dir_cluster = fat_append_clusters(fs, 0, 1);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2267 if(!dir_cluster)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2268 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2269
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2270 /* clear cluster to prevent bogus directory entries */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2271 fat_clear_cluster(fs, dir_cluster);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2272
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2273 memset(dir_entry, 0, sizeof(*dir_entry));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2274 dir_entry->attributes = FAT_ATTRIB_DIR;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2275
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2276 /* create "." directory self reference */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2277 dir_entry->entry_offset = fs->header.cluster_zero_offset +
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2278 (offset_t) (dir_cluster - 2) * fs->header.cluster_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2279 dir_entry->long_name[0] = '.';
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2280 dir_entry->cluster = dir_cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2281 if(!fat_write_dir_entry(fs, dir_entry))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2282 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2283 fat_free_clusters(fs, dir_cluster);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2284 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2285 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2286
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2287 /* create ".." parent directory reference */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2288 dir_entry->entry_offset += 32;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2289 dir_entry->long_name[1] = '.';
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2290 dir_entry->cluster = parent->dir_entry.cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2291 if(!fat_write_dir_entry(fs, dir_entry))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2292 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2293 fat_free_clusters(fs, dir_cluster);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2294 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2295 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2296
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2297 /* fill directory entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2298 strncpy(dir_entry->long_name, dir, sizeof(dir_entry->long_name) - 1);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2299 dir_entry->cluster = dir_cluster;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2300
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2301 /* find place where to store directory entry */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2302 if(!(dir_entry->entry_offset = fat_find_offset_for_dir_entry(fs, parent, dir_entry)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2303 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2304 fat_free_clusters(fs, dir_cluster);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2305 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2306 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2307
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2308 /* write directory to disk */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2309 if(!fat_write_dir_entry(fs, dir_entry))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2310 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2311 fat_free_clusters(fs, dir_cluster);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2312 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2313 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2314
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2315 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2316 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2317 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2318
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2319 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2320 * \ingroup fat_dir
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2321 * Deletes a directory.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2322 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2323 * This is just a synonym for fat_delete_file().
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2324 * If a directory is deleted without first deleting its
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2325 * subdirectories and files, disk space occupied by these
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2326 * files will get wasted as there is no chance to release
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2327 * it and mark it as free.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2328 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2329 * \param[in] fs The filesystem on which to operate.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2330 * \param[in] dir_entry The directory entry of the directory to delete.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2331 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2332 * \see fat_create_dir
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2333 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2334 #ifdef DOXYGEN
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2335 uint8_t fat_delete_dir(struct fat_fs_struct* fs, struct fat_dir_entry_struct* dir_entry);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2336 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2337
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2338 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2339 * \ingroup fat_dir
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2340 * Moves or renames a directory.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2341 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2342 * This is just a synonym for fat_move_file().
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2343 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2344 * \param[in] fs The filesystem on which to operate.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2345 * \param[in,out] dir_entry The directory entry of the directory to move.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2346 * \param[in] parent_new The handle of the new parent directory.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2347 * \param[in] dir_new The directory's new name.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2348 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2349 * \see fat_create_dir, fat_delete_dir, fat_move_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2350 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2351 #ifdef DOXYGEN
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2352 uint8_t fat_move_dir(struct fat_fs_struct* fs, struct fat_dir_entry_struct* dir_entry, struct fat_dir_struct* parent_new, const char* dir_new);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2353 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2354
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2355 #if DOXYGEN || FAT_DATETIME_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2356 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2357 * \ingroup fat_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2358 * Returns the modification date of a file.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2359 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2360 * \param[in] dir_entry The directory entry of which to return the modification date.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2361 * \param[out] year The year the file was last modified.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2362 * \param[out] month The month the file was last modified.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2363 * \param[out] day The day the file was last modified.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2364 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2365 void fat_get_file_modification_date(const struct fat_dir_entry_struct* dir_entry, uint16_t* year, uint8_t* month, uint8_t* day)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2366 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2367 if(!dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2368 return;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2369
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2370 *year = 1980 + ((dir_entry->modification_date >> 9) & 0x7f);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2371 *month = (dir_entry->modification_date >> 5) & 0x0f;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2372 *day = (dir_entry->modification_date >> 0) & 0x1f;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2373 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2374 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2375
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2376 #if DOXYGEN || FAT_DATETIME_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2377 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2378 * \ingroup fat_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2379 * Returns the modification time of a file.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2380 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2381 * \param[in] dir_entry The directory entry of which to return the modification time.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2382 * \param[out] hour The hour the file was last modified.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2383 * \param[out] min The min the file was last modified.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2384 * \param[out] sec The sec the file was last modified.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2385 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2386 void fat_get_file_modification_time(const struct fat_dir_entry_struct* dir_entry, uint8_t* hour, uint8_t* min, uint8_t* sec)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2387 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2388 if(!dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2389 return;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2390
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2391 *hour = (dir_entry->modification_time >> 11) & 0x1f;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2392 *min = (dir_entry->modification_time >> 5) & 0x3f;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2393 *sec = ((dir_entry->modification_time >> 0) & 0x1f) * 2;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2394 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2395 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2396
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2397 #if DOXYGEN || (FAT_WRITE_SUPPORT && FAT_DATETIME_SUPPORT)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2398 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2399 * \ingroup fat_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2400 * Sets the modification time of a date.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2401 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2402 * \param[in] dir_entry The directory entry for which to set the modification date.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2403 * \param[in] year The year the file was last modified.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2404 * \param[in] month The month the file was last modified.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2405 * \param[in] day The day the file was last modified.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2406 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2407 void fat_set_file_modification_date(struct fat_dir_entry_struct* dir_entry, uint16_t year, uint8_t month, uint8_t day)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2408 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2409 if(!dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2410 return;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2411
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2412 dir_entry->modification_date =
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2413 ((year - 1980) << 9) |
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2414 ((uint16_t) month << 5) |
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2415 ((uint16_t) day << 0);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2416 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2417 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2418
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2419 #if DOXYGEN || (FAT_WRITE_SUPPORT && FAT_DATETIME_SUPPORT)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2420 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2421 * \ingroup fat_file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2422 * Sets the modification time of a file.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2423 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2424 * \param[in] dir_entry The directory entry for which to set the modification time.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2425 * \param[in] hour The year the file was last modified.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2426 * \param[in] min The month the file was last modified.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2427 * \param[in] sec The day the file was last modified.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2428 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2429 void fat_set_file_modification_time(struct fat_dir_entry_struct* dir_entry, uint8_t hour, uint8_t min, uint8_t sec)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2430 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2431 if(!dir_entry)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2432 return;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2433
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2434 dir_entry->modification_time =
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2435 ((uint16_t) hour << 11) |
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2436 ((uint16_t) min << 5) |
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2437 ((uint16_t) sec >> 1) ;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2438 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2439 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2440
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2441 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2442 * \ingroup fat_fs
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2443 * Returns the amount of total storage capacity of the filesystem in bytes.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2444 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2445 * \param[in] fs The filesystem on which to operate.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2446 * \returns 0 on failure, the filesystem size in bytes otherwise.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2447 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2448 offset_t fat_get_fs_size(const struct fat_fs_struct* fs)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2449 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2450 if(!fs)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2451 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2452
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2453 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2454 if(fs->partition->type == PARTITION_TYPE_FAT32)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2455 return (offset_t) (fs->header.fat_size / 4 - 2) * fs->header.cluster_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2456 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2457 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2458 return (offset_t) (fs->header.fat_size / 2 - 2) * fs->header.cluster_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2459 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2460
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2461 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2462 * \ingroup fat_fs
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2463 * Returns the amount of free storage capacity on the filesystem in bytes.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2464 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2465 * \note As the FAT filesystem is cluster based, this function does not
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2466 * return continuous values but multiples of the cluster size.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2467 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2468 * \param[in] fs The filesystem on which to operate.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2469 * \returns 0 on failure, the free filesystem space in bytes otherwise.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2470 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2471 offset_t fat_get_fs_free(const struct fat_fs_struct* fs)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2472 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2473 if(!fs)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2474 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2475
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2476 uint8_t fat[32];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2477 struct fat_usage_count_callback_arg count_arg;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2478 count_arg.cluster_count = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2479 count_arg.buffer_size = sizeof(fat);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2480
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2481 offset_t fat_offset = fs->header.fat_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2482 uint32_t fat_size = fs->header.fat_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2483 while(fat_size > 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2484 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2485 uintptr_t length = UINTPTR_MAX - 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2486 if(fat_size < length)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2487 length = fat_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2488
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2489 if(!fs->partition->device_read_interval(fat_offset,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2490 fat,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2491 sizeof(fat),
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2492 length,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2493 #if FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2494 (fs->partition->type == PARTITION_TYPE_FAT16) ?
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2495 fat_get_fs_free_16_callback :
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2496 fat_get_fs_free_32_callback,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2497 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2498 fat_get_fs_free_16_callback,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2499 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2500 &count_arg
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2501 )
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2502 )
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2503 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2504
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2505 fat_offset += length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2506 fat_size -= length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2507 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2508
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2509 return (offset_t) count_arg.cluster_count * fs->header.cluster_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2510 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2511
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2512 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2513 * \ingroup fat_fs
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2514 * Callback function used for counting free clusters in a FAT.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2515 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2516 uint8_t fat_get_fs_free_16_callback(uint8_t* buffer, offset_t offset, void* p)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2517 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2518 struct fat_usage_count_callback_arg* count_arg = (struct fat_usage_count_callback_arg*) p;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2519 uintptr_t buffer_size = count_arg->buffer_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2520
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2521 for(uintptr_t i = 0; i < buffer_size; i += 2, buffer += 2)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2522 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2523 uint16_t cluster = read16(buffer);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2524 if(cluster == HTOL16(FAT16_CLUSTER_FREE))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2525 ++(count_arg->cluster_count);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2526 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2527
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2528 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2529 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2530
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2531 #if DOXYGEN || FAT_FAT32_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2532 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2533 * \ingroup fat_fs
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2534 * Callback function used for counting free clusters in a FAT32.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2535 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2536 uint8_t fat_get_fs_free_32_callback(uint8_t* buffer, offset_t offset, void* p)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2537 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2538 struct fat_usage_count_callback_arg* count_arg = (struct fat_usage_count_callback_arg*) p;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2539 uintptr_t buffer_size = count_arg->buffer_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2540
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2541 for(uintptr_t i = 0; i < buffer_size; i += 4, buffer += 4)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2542 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2543 uint32_t cluster = read32(buffer);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2544 if(cluster == HTOL32(FAT32_CLUSTER_FREE))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2545 ++(count_arg->cluster_count);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2546 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2547
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2548 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2549 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2550 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2551