summaryrefslogtreecommitdiffstats
path: root/cpukit/libfs/src/dosfs/msdos.h
blob: 57865d515398d57d2351631e6b6b13998532344c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/**
 * @file
 *
 * @brief The MSDOS Filesystem Constants/Data Structures/Prototypes
 *
 * @ingroup libfs_msdos
 */

/*
 *  Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
 *  Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
 *
 *  Modifications to support UTF-8 in the file system are
 *  Copyright (c) 2013 embedded brains GmbH.
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 */

#ifndef __DOSFS_MSDOS_H__
#define __DOSFS_MSDOS_H__

#include <rtems.h>
#include <rtems/libio_.h>
#include <rtems/dosfs.h>

#include "fat.h"
#include "fat_file.h"

/**
 *  @defgroup libfs_msdos MSDOS FileSystem
 *
 *  @ingroup libfs
 */
/**@{*/
#ifdef __cplusplus
extern "C" {
#endif

#define MSDOS_NAME_NOT_FOUND_ERR  0x7D01

/*
 * This structure identifies the instance of the filesystem on the MSDOS
 * level.
 */
typedef struct msdos_fs_info_s
{
    fat_fs_info_t                     fat;                /*
                                                           * volume
                                                           * description
                                                           */
    const rtems_filesystem_file_handlers_r *directory_handlers; /*
                                                                 * a set of routines
                                                                 * that handles the
                                                                 * nodes of directory
                                                                 * type
                                                                 */
    const rtems_filesystem_file_handlers_r *file_handlers; /*
                                                            * a set of routines
                                                            * that handles the
                                                            * nodes of file
                                                            * type
                                                            */
    rtems_id                          vol_sema;           /*
                                                           * semaphore
                                                           * associated with
                                                           * the volume
                                                           */
    uint8_t                          *cl_buf;              /*
                                                            * just placeholder
                                                            * for anything
                                                            */

    rtems_dosfs_convert_control      *converter;
} msdos_fs_info_t;

/* a set of routines that handle the nodes which are directories */
extern const rtems_filesystem_file_handlers_r  msdos_dir_handlers;

/* a set of routines that handle the nodes which are files */
extern const rtems_filesystem_file_handlers_r  msdos_file_handlers;

/* Volume semaphore timeout value. This value can be changed to a number
 * of ticks to help debugging or if you need such a  */
#define MSDOS_VOLUME_SEMAPHORE_TIMEOUT    RTEMS_NO_TIMEOUT

/* Node types */
#define MSDOS_DIRECTORY     RTEMS_FILESYSTEM_DIRECTORY
#define MSDOS_REGULAR_FILE  RTEMS_FILESYSTEM_MEMORY_FILE
#define MSDOS_HARD_LINK     RTEMS_FILESYSTEM_HARD_LINK /* pseudo type */

/**
 *  @brief Type of node that loc refers to.
 *
 *  The following returns the type of node that the loc refers to.
 *
 */
typedef rtems_filesystem_node_types_t msdos_node_type_t;

/*
 * Macros for fetching fields from 32 bytes long FAT Directory Entry
 * Structure
 */
#define MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE    32 /* 32 bytes */

#define MSDOS_DIR_NAME(x)                 (char     *)((x) + 0)
#define MSDOS_DIR_ENTRY_TYPE(x)           (uint8_t  *)((x) + 0)
#define MSDOS_DIR_ATTR(x)                 (uint8_t  *)((x) + 11)
#define MSDOS_DIR_NT_RES(x)               (uint8_t  *)((x) + 12)
#define MSDOS_DIR_LFN_CHECKSUM(x)         (uint8_t  *)((x) + 13)
#define MSDOS_DIR_CRT_TIME_TENTH(x)       (uint8_t  *)((x) + 13)
#define MSDOS_DIR_CRT_TIME(x)             (uint16_t *)((x) + 14)
#define MSDOS_DIR_CRT_DATE(x)             (uint16_t *)((x) + 16)
#define MSDOS_DIR_LAST_ACCESS_DATE(x)     (uint16_t *)((x) + 18)
#define MSDOS_DIR_FIRST_CLUSTER_HI(x)     (uint16_t *)((x) + 20)
#define MSDOS_DIR_WRITE_TIME(x)           (uint16_t *)((x) + 22)
#define MSDOS_DIR_WRITE_DATE(x)           (uint16_t *)((x) + 24)
#define MSDOS_DIR_FIRST_CLUSTER_LOW(x)    (uint16_t *)((x) + 26)
#define MSDOS_DIR_FILE_SIZE(x)            (uint32_t *)((x) + 28)

#define MSDOS_EXTRACT_CLUSTER_NUM(p)                                         \
            (uint32_t)( (CF_LE_W(*MSDOS_DIR_FIRST_CLUSTER_LOW(p))) |       \
                          ((uint32_t)(CF_LE_W((*MSDOS_DIR_FIRST_CLUSTER_HI(p))))<<16) )

/*
 * Fields offset in 32 bytes long FAT Directory Entry
 * Structure
 */
#define MSDOS_FILE_SIZE_OFFSET            28
#define MSDOS_FILE_NAME_OFFSET             0
#define MSDOS_FIRST_CLUSTER_HI_OFFSET     20
#define MSDOS_FIRST_CLUSTER_LOW_OFFSET    26
#define MSDOS_FILE_WDATE_OFFSET           24
#define MSDOS_FILE_WTIME_OFFSET           22
#define MSDOS_FILE_ADATE_OFFSET           18

/*
 * Possible values of DIR_Attr field of 32 bytes long FAT Directory Entry
 * Structure
 */
#define MSDOS_ATTR_READ_ONLY    0x01
#define MSDOS_ATTR_HIDDEN       0x02
#define MSDOS_ATTR_SYSTEM       0x04
#define MSDOS_ATTR_VOLUME_ID    0x08
#define MSDOS_ATTR_DIRECTORY    0x10
#define MSDOS_ATTR_ARCHIVE      0x20
#define MSDOS_ATTR_LFN          (MSDOS_ATTR_READ_ONLY | \
                                 MSDOS_ATTR_HIDDEN | \
                                 MSDOS_ATTR_SYSTEM | \
                                 MSDOS_ATTR_VOLUME_ID)
#define MSDOS_ATTR_LFN_MASK     (MSDOS_ATTR_READ_ONLY | \
                                 MSDOS_ATTR_HIDDEN | \
                                 MSDOS_ATTR_SYSTEM | \
                                 MSDOS_ATTR_VOLUME_ID | \
                                 MSDOS_ATTR_DIRECTORY | \
                                 MSDOS_ATTR_ARCHIVE)

#define MSDOS_LAST_LONG_ENTRY         0x40
#define MSDOS_LAST_LONG_ENTRY_MASK    0x3F

#define MSDOS_DT_2SECONDS_MASK        0x1F    /* seconds divided by 2 */
#define MSDOS_DT_2SECONDS_SHIFT       0
#define MSDOS_DT_MINUTES_MASK         0x7E0   /* minutes */
#define MSDOS_DT_MINUTES_SHIFT        5
#define MSDOS_DT_HOURS_MASK           0xF800  /* hours */
#define MSDOS_DT_HOURS_SHIFT          11

#define MSDOS_DD_DAY_MASK             0x1F    /* day of month */
#define MSDOS_DD_DAY_SHIFT            0
#define MSDOS_DD_MONTH_MASK           0x1E0   /* month */
#define MSDOS_DD_MONTH_SHIFT          5
#define MSDOS_DD_YEAR_MASK            0xFE00  /* year - 1980 */
#define MSDOS_DD_YEAR_SHIFT           9


/*
 * Possible values of DIR_Name[0] field of 32 bytes long FAT Directory Entry
 * Structure
 */
#define MSDOS_THIS_DIR_ENTRY_EMPTY             0xE5
#define MSDOS_THIS_DIR_ENTRY_AND_REST_EMPTY    0x00

/*
 * Number of characters per directory entry for a long filename.
 */
#define MSDOS_LFN_LEN_PER_ENTRY (13)

/*
 *  Macros for names parsing and formatting
 */
#define MSDOS_NAME_MAX_UTF8_BYTES_PER_CHAR  4
#define MSDOS_NAME_MIN_UTF8_BYTES_PER_CHAR  1

#define MSDOS_SHORT_BASE_LEN             8  /* 8 characters */
#define MSDOS_SHORT_EXT_LEN              3  /* 3 characters */
#define MSDOS_SHORT_NAME_LEN             (MSDOS_SHORT_BASE_LEN+\
                                          MSDOS_SHORT_EXT_LEN) /* 11 chars */
#define MSDOS_NAME_MAX_LNF_LEN           (255)
#define MSDOS_NAME_MAX                   MSDOS_SHORT_NAME_LEN
#define MSDOS_NAME_MAX_UTF8_SFN_BYTES    (MSDOS_NAME_MAX *\
                                         MSDOS_NAME_MAX_UTF8_BYTES_PER_CHAR)
#define MSDOS_NAME_MAX_WITH_DOT          (MSDOS_NAME_MAX + 1)
#define MSDOS_SFN_MAX_WITH_DOT_UTF8_BYTES (MSDOS_NAME_MAX_WITH_DOT *\
                                           MSDOS_NAME_MAX_UTF8_BYTES_PER_CHAR)
#define MSDOS_NAME_MAX_LFN_WITH_DOT      (260)

#define MSDOS_NAME_LFN_BYTES_PER_CHAR    (2)
#define MSDOS_NAME_MAX_LFN_BYTES         (MSDOS_NAME_MAX_LFN_WITH_DOT *\
                                          MSDOS_NAME_LFN_BYTES_PER_CHAR)
#define MSDOS_NAME_MAX_UTF8_LFN_BYTES    (MSDOS_NAME_MAX_LFN_WITH_DOT *\
                                          MSDOS_NAME_MAX_UTF8_BYTES_PER_CHAR)
#define MSDOS_ENTRY_LFN_UTF8_BYTES       (MSDOS_LFN_LEN_PER_ENTRY *\
                                          MSDOS_NAME_MAX_UTF8_BYTES_PER_CHAR)

extern const char *const MSDOS_DOT_NAME;    /* ".", padded to MSDOS_NAME chars */
extern const char *const MSDOS_DOTDOT_NAME; /* ".", padded to MSDOS_NAME chars */

typedef enum msdos_name_types_e
{
    MSDOS_NAME_INVALID = 0, /* Unknown name type. Has invalid characters. */
    MSDOS_NAME_SHORT,       /* Name can be short. */
    MSDOS_NAME_LONG         /* Name is long; cannot be short. */
} msdos_name_type_t;

typedef enum msdos_token_types_e
{
    MSDOS_NO_MORE_PATH,
    MSDOS_CURRENT_DIR,
    MSDOS_UP_DIR,
    MSDOS_NAME,
    MSDOS_INVALID_TOKEN
} msdos_token_types_t;

/* Others macros */
#define MSDOS_RES_NT_VALUE     0x00
#define MSDOS_INIT_DIR_SIZE    0x00

/* "dot" entry offset in a directory */
#define MSDOS_DOT_DIR_ENTRY_OFFSET       0x00 /* first entry in directory */

/* "dotdot" entry offset in a directory */
#define MSDOS_DOTDOT_DIR_ENTRY_OFFSET    0x20 /* second entry in directory */

/* 'p' should be char* */
#define DOT_NODE_P(p)     ((char *)(p))
#define DOTDOT_NODE_P(p)  ((char *)((p) + MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE))

/* Size limits for files and directories */
#define MSDOS_MAX_DIR_LENGHT               0x200000   /* 2,097,152 bytes */
#define MSDOS_MAX_FILE_SIZE                0xFFFFFFFF /* 4 Gb */

/*
 * The number of 32 bytes long FAT Directory Entry
 * Structures per 512 bytes sector
 */
#define MSDOS_DPS512_NUM    16

/**
 *  @brief Shut down the MSDOS filesystem.
 *
 *  MSDOS shut down handler implementation
 */
void msdos_shut_down(rtems_filesystem_mount_table_entry_t *temp_mt_entry);

void msdos_eval_path(rtems_filesystem_eval_path_context_t *ctx);

/**
 *  @brief Call the Fat-File close routine.
 *
 *  Free node handler implementation for the filesystem operations table.
 */
void msdos_free_node_info(const rtems_filesystem_location_info_t *pathloc);

rtems_filesystem_node_types_t msdos_node_type(
  const rtems_filesystem_location_info_t *loc
);

/**
 * @brief Routine for node creation in a MSDOS filesystem.
 *
 * MSDOS Directory Handlers Implementation
 */
int msdos_mknod(
  const rtems_filesystem_location_info_t *loc,
  const char *name,
  size_t namelen,
  mode_t mode,
  dev_t dev
);

/**
 * @brief Remove node from MSDOS directory.
 *
 * MSDOS Directory Handlers Implementation
 */
int msdos_rmnod(
  const rtems_filesystem_location_info_t *parentloc,
  const rtems_filesystem_location_info_t *loc
);

/**
 * @brief Rename a MSDOS filesystem node.
 *
 * Routine to rename a MSDOS filesystem node
 */
int msdos_rename(
  const rtems_filesystem_location_info_t *old_parent_loc,
  const rtems_filesystem_location_info_t *old_loc,
  const rtems_filesystem_location_info_t *new_parent_loc,
  const char *new_name,
  size_t new_namelen
);

int msdos_statvfs(
  const rtems_filesystem_location_info_t *root_loc,
  struct statvfs *sb);

void msdos_lock(const rtems_filesystem_mount_table_entry_t *mt_entry);

void msdos_unlock(const rtems_filesystem_mount_table_entry_t *mt_entry);

/**
 *  @brief MSDOS filesystem initialization routine.
 *
 *  MSDOS Initialization support routine implementation
 */
int msdos_initialize_support(
  rtems_filesystem_mount_table_entry_t    *temp_mt_entry,
  const rtems_filesystem_operations_table *op_table,
  const rtems_filesystem_file_handlers_r  *file_handlers,
  const rtems_filesystem_file_handlers_r  *directory_handlers,
  rtems_dosfs_convert_control             *converter
);

int msdos_file_close(rtems_libio_t *iop /* IN  */);

ssize_t msdos_file_read(
  rtems_libio_t *iop,              /* IN  */
  void          *buffer,           /* IN  */
  size_t         count             /* IN  */
);

ssize_t msdos_file_write(
  rtems_libio_t *iop,             /* IN  */
  const void    *buffer,          /* IN  */
  size_t         count            /* IN  */
);

int msdos_file_stat(
  const rtems_filesystem_location_info_t *loc,
  struct stat *buf
);

int
msdos_file_ftruncate(
  rtems_libio_t *iop,               /* IN  */
  off_t          length            /* IN  */
);

int msdos_file_sync(rtems_libio_t *iop);

int msdos_file_datasync(rtems_libio_t *iop);

ssize_t msdos_dir_read(
  rtems_libio_t *iop,              /* IN  */
  void          *buffer,           /* IN  */
  size_t         count             /* IN  */
);

int msdos_dir_sync(rtems_libio_t *iop);

int msdos_dir_stat(
  const rtems_filesystem_location_info_t *loc,
  struct stat *buf
);

/**
 * @brief Implements wake up version of the "signal" operation.
 *
 * Routine to create a new MSDOS filesystem node
 *
 */
int msdos_creat_node(const rtems_filesystem_location_info_t *parent_loc,
                     msdos_node_type_t                       type,
                     const char                             *name,
                     int                                     name_len,
                     mode_t                                  mode,
                     const fat_file_fd_t                    *link_fd);

/* Misc prototypes */

int msdos_find_name(
  rtems_filesystem_location_info_t *parent_loc,
  const char                       *name,
  int                               name_len
);

int msdos_get_name_node(
  const rtems_filesystem_location_info_t *parent_loc,
  bool                                    create_node,
  const char                             *name,
  int                                     name_len,
  msdos_name_type_t                       name_type,
  fat_dir_pos_t                          *dir_pos,
  char                                   *name_dir_entry
);

int msdos_dir_info_remove(rtems_filesystem_location_info_t *pathloc);

ssize_t
msdos_format_dirent_with_dot(char *dst,const char *src);

msdos_name_type_t msdos_long_to_short(rtems_dosfs_convert_control *converter,
                                      const char *lfn, int lfn_len,
                                      char* sfn, int sfn_len);

ssize_t
msdos_filename_utf8_to_short_name_for_compare (
    rtems_dosfs_convert_control     *converter,
    const uint8_t                   *utf8_name,
    const size_t                     utf8_name_size,
    void                            *short_name,
    const size_t                     short_name_size);

ssize_t
msdos_filename_utf8_to_short_name_for_save (
    rtems_dosfs_convert_control     *converter,
    const uint8_t                   *utf8_name,
    const size_t                     utf8_name_size,
    void                            *short_name,
    const size_t                     short_name_size);

ssize_t
msdos_filename_utf8_to_long_name_for_compare (
    rtems_dosfs_convert_control     *converter,
    const uint8_t                   *utf8_name,
    const size_t                     utf8_name_size,
    uint8_t                         *long_name,
    const size_t                     long_name_size);

ssize_t
msdos_filename_utf8_to_long_name_for_save (
    rtems_dosfs_convert_control     *converter,
    const uint8_t                   *utf8_name,
    const size_t                     utf8_name_size,
    uint16_t                        *long_name,
    const size_t                     long_name_size);

ssize_t
msdos_get_utf16_string_from_long_entry (
  const char                 *entry,
  uint16_t                   *entry_string_buf,
  const size_t                buf_size,
  bool                        is_first_entry
);

void msdos_date_unix2dos(
  unsigned int tsp, uint16_t *ddp,
  uint16_t *dtp);

unsigned int msdos_date_dos2unix(unsigned int dd, unsigned int dt);

int msdos_set_first_cluster_num(
  rtems_filesystem_mount_table_entry_t *mt_entry,
  fat_file_fd_t                        *fat_fd
);

int msdos_set_file_size(
  rtems_filesystem_mount_table_entry_t *mt_entry,
  fat_file_fd_t                        *fat_fd
);

int msdos_set_first_char4file_name(
  rtems_filesystem_mount_table_entry_t *mt_entry,
  fat_dir_pos_t                        *dir_pos,
  unsigned char                         first_char
);

int msdos_set_dir_wrt_time_and_date(
    rtems_filesystem_mount_table_entry_t *mt_entry,
    fat_file_fd_t                        *fat_fd
);


int msdos_dir_is_empty(
  rtems_filesystem_mount_table_entry_t *mt_entry,
  fat_file_fd_t                        *fat_fd,
  bool                                 *ret_val
);

int msdos_find_name_in_fat_file(
    rtems_filesystem_mount_table_entry_t *mt_entry,
    fat_file_fd_t                        *fat_fd,
    bool                                  create_node,
    const uint8_t                        *name_utf8,
    int                                   name_len,
    msdos_name_type_t                     name_type,
    fat_dir_pos_t                        *dir_pos,
    char                                 *name_dir_entry
);

int msdos_find_node_by_cluster_num_in_fat_file(
    rtems_filesystem_mount_table_entry_t *mt_entry,
    fat_file_fd_t                        *fat_fd,
    uint32_t                              cl4find,
    fat_dir_pos_t                        *dir_pos,
    char                                 *dir_entry
);

int msdos_get_dotdot_dir_info_cluster_num_and_offset(
    rtems_filesystem_mount_table_entry_t *mt_entry,
    uint32_t                              cln,
    fat_dir_pos_t                        *dir_pos,
    char                                 *dir_entry
);

int msdos_sync(rtems_libio_t *iop);

#ifdef __cplusplus
}
#endif
/**@}*/
#endif /* __DOSFS_MSDOS_H__ */