xref: /illumos-gate/usr/src/boot/libsa/dosfs.h (revision 9164a50bf932130cbb5097a16f6986873ce0e6e5)
1 /*
2  * Copyright (c) 1996, 1998 Robert Nordier
3  * Copyright 2024 MNX Cloud, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef DOSIO_H
30 #define	DOSIO_H
31 
32 /*
33  * DOS file attributes
34  */
35 
36 #define	FA_RDONLY	001	/* read-only */
37 #define	FA_HIDDEN	002	/* hidden file */
38 #define	FA_SYSTEM	004	/* system file */
39 #define	FA_LABEL	010	/* volume label */
40 #define	FA_DIR		020	/* directory */
41 #define	FA_ARCH		040	/* archive (file modified) */
42 #define	FA_XDE		017	/* extended directory entry */
43 #define	FA_MASK		077	/* all attributes */
44 
45 /*
46  * Macros to convert DOS-format 16-bit and 32-bit quantities
47  */
48 
49 #define	cv2(p)  ((uint16_t)(p)[0] |         \
50 		((uint16_t)(p)[1] << 010))
51 #define	cv4(p)  ((uint32_t)(p)[0] |          \
52 		((uint32_t)(p)[1] << 010) |  \
53 		((uint32_t)(p)[2] << 020) |  \
54 		((uint32_t)(p)[3] << 030))
55 
56 /*
57  * Directory, filesystem, and file structures.
58  */
59 
60 typedef struct {
61     uchar_t x_case;		/* case */
62     uchar_t c_hsec;		/* created: secs/100 */
63     uchar_t c_time[2];		/* created: time */
64     uchar_t c_date[2];		/* created: date */
65     uchar_t a_date[2];		/* accessed: date */
66     uchar_t h_clus[2];		/* clus[hi] */
67 } DOS_DEX;
68 
69 typedef struct {
70     uchar_t name[8];		/* name */
71     uchar_t ext[3];		/* extension */
72     uchar_t attr;		/* attributes */
73     DOS_DEX dex;		/* VFAT/FAT32 only */
74     uchar_t time[2];		/* modified: time */
75     uchar_t date[2];		/* modified: date */
76     uchar_t clus[2];		/* starting cluster */
77     uchar_t size[4];		/* size */
78 } DOS_DE;
79 
80 typedef struct {
81     uchar_t seq;		/* flags */
82     uchar_t name1[5][2];	/* 1st name area */
83     uchar_t attr;		/* (see fat_de) */
84     uchar_t res;		/* reserved */
85     uchar_t chk;		/* checksum */
86     uchar_t name2[6][2];	/* 2nd name area */
87     uchar_t clus[2];		/* (see fat_de) */
88     uchar_t name3[2][2];	/* 3rd name area */
89 } DOS_XDE;
90 
91 typedef union {
92     DOS_DE de;			/* standard directory entry */
93     DOS_XDE xde;		/* extended directory entry */
94 } DOS_DIR;
95 
96 typedef struct {
97     struct open_file *fd;	/* file descriptor */
98     uchar_t *fatbuf;		/* FAT cache buffer */
99     uint_t fatbuf_blknum;	/* number of 128K block in FAT cache buffer */
100     uint_t links;		/* active links to structure */
101     uint_t spc;			/* sectors per cluster */
102     uint_t bsize;		/* cluster size in bytes */
103     uint_t bshift;		/* cluster conversion shift */
104     uint_t dirents;		/* root directory entries */
105     uint_t spf;			/* sectors per fat */
106     uint_t rdcl;		/* root directory start cluster */
107     daddr_t lsnfat;		/* start of fat */
108     daddr_t lsndir;		/* start of root dir */
109     daddr_t lsndta;		/* start of data area */
110     uint_t fatsz;		/* FAT entry size */
111     uint_t xclus;		/* maximum cluster number */
112     DOS_DE root;
113 } DOS_FS;
114 
115 typedef struct {
116     DOS_FS *fs;			/* associated filesystem */
117     DOS_DE de;			/* directory entry */
118     uint_t offset;		/* current offset */
119     uint_t c;			/* last cluster read */
120 } DOS_FILE;
121 
122 #endif	/* !DOSIO_H */
123