244e4f4f45bb6e854beee0132f71823fbae7887f
[librecmc/librecmc.git] / tools / dosfstools / patches / 0013-Add-compatible-ioctl-calls-for-OSX-and-FreeBSD.patch
1 From 50897e9d43f61b4ab238d3ebff62cc45d505206a Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?=C3=81lvaro=20Fern=C3=A1ndez=20Rojas?= <noltari@gmail.com>
3 Date: Sat, 7 Mar 2015 15:55:32 +0100
4 Subject: [PATCH 13/14] Add compatible ioctl calls for OSX and FreeBSD
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
10 ---
11  src/io.c       |   2 --
12  src/mkfs.fat.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
13  2 files changed, 96 insertions(+), 7 deletions(-)
14
15 diff --git a/src/io.c b/src/io.c
16 index 450432c..7d0d49a 100644
17 --- a/src/io.c
18 +++ b/src/io.c
19 @@ -36,10 +36,8 @@
20  #include <string.h>
21  #include <unistd.h>
22  #include <sys/stat.h>
23 -#include <sys/ioctl.h>
24  #include <errno.h>
25  #include <fcntl.h>
26 -#include <linux/fd.h>
27  
28  #include "fsck.fat.h"
29  #include "common.h"
30 diff --git a/src/mkfs.fat.c b/src/mkfs.fat.c
31 index 76d40d8..02e0918 100644
32 --- a/src/mkfs.fat.c
33 +++ b/src/mkfs.fat.c
34 @@ -47,17 +47,13 @@
35  #include "version.h"
36  
37  #include <fcntl.h>
38 -#include <linux/hdreg.h>
39  #include <sys/mount.h>
40 -#include <linux/fs.h>
41 -#include <linux/fd.h>
42  #include <endian.h>
43  #include <mntent.h>
44  #include <signal.h>
45  #include <string.h>
46  #include <stdio.h>
47  #include <stdlib.h>
48 -#include <sys/ioctl.h>
49  #include <sys/stat.h>
50  #include <sys/time.h>
51  #include <unistd.h>
52 @@ -67,6 +63,40 @@
53  #include <stdint.h>
54  #include <endian.h>
55  
56 +#if defined(__linux__)
57 +    #include <linux/hdreg.h>
58 +    #include <linux/fs.h>
59 +    #include <linux/fd.h>
60 +#elif defined(__FreeBSD__) || defined(__APPLE__)
61 +    #include <sys/disk.h>
62 +
63 +    #define BLOCK_SIZE_BITS 10
64 +    #define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
65 +
66 +    struct floppy_struct {
67 +        unsigned int size;     /* nr of sectors total */
68 +        unsigned int sect;     /* sectors per track */
69 +        unsigned int head;     /* nr of heads */
70 +        unsigned int track;    /* nr of tracks */
71 +        unsigned int stretch;  /* bit 0 !=0 means double track steps */
72 +                                       /* bit 1 != 0 means swap sides */
73 +                                       /* bits 2..9 give the first sector */
74 +                                       /*  number (the LSB is flipped) */
75 +        unsigned char gap;     /* gap1 size */
76 +        unsigned char rate;    /* data rate. |= 0x40 for perpendicular */
77 +        unsigned char spec1;   /* stepping rate, head unload time */
78 +        unsigned char fmt_gap; /* gap2 size */
79 +        const char * name;     /* used only for predefined formats */
80 +    };
81 +
82 +    struct hd_geometry {
83 +        unsigned char heads;
84 +        unsigned char sectors;
85 +        unsigned short cylinders;
86 +        unsigned long start;
87 +    };
88 +#endif
89 +
90  #include "msdos_fs.h"
91  
92  /* In earlier versions, an own llseek() was used, but glibc lseek() is
93 @@ -511,7 +541,9 @@ static void check_mount(char *device_name)
94  static void establish_params(int device_num, int size)
95  {
96      long loop_size;
97 +#if defined(__linux__) || defined(__FreeBSD__)
98      struct hd_geometry geometry;
99 +#endif
100      struct floppy_struct param;
101      int def_root_dir_entries = 512;
102  
103 @@ -549,9 +581,12 @@ static void establish_params(int device_num, int size)
104             }
105  
106         } else {                /* is a floppy diskette */
107 -
108 +#if defined(__linux__)
109             if (ioctl(dev, FDGETPRM, &param))   /*  Can we get the diskette geometry? */
110                 die("unable to get diskette geometry for '%s'");
111 +#else
112 +               die("unable to get diskette geometry for '%s'");
113 +#endif
114         }
115         bs.secs_track = htole16(param.sect);    /*  Set up the geometry information */
116         bs.heads = htole16(param.head);
117 @@ -594,8 +629,18 @@ floppy_default:
118                 goto floppy_default;
119         }
120      } else if ((device_num & 0xff00) == 0x0700) {      /* This is a loop device */
121 +#if defined(__FreeBSD__)
122 +       if (ioctl(dev, DIOCGSECTORSIZE, &loop_size))
123 +           die("unable to get loop device size");
124 +#elif defined(__linux__)
125         if (ioctl(dev, BLKGETSIZE, &loop_size))
126             die("unable to get loop device size");
127 +#elif defined(__APPLE__)
128 +       if (ioctl(dev, DKIOCGETBLOCKSIZE, &loop_size))
129 +           die("unable to get loop device size");
130 +#else
131 +       die("unable to get loop device size");
132 +#endif
133  
134         switch (loop_size) {    /* Assuming the loop device -> floppy later */
135         case 720:               /* 5.25", 2, 9, 40 - 360K */
136 @@ -651,6 +696,17 @@ floppy_default:
137      {
138         /* Can we get the drive geometry? (Note I'm not too sure about */
139         /* whether to use HDIO_GETGEO or HDIO_REQ) */
140 +#if defined(__FreeBSD__)
141 +       if (ioctl(dev, DIOCGFWSECTORS, &geometry.sectors) || ioctl(dev, DIOCGFWHEADS, &geometry.heads) || geometry.sectors == 0
142 +           || geometry.heads == 0) {
143 +           printf("unable to get drive geometry, using default 255/63\n");
144 +           bs.secs_track = htole16(63);
145 +           bs.heads = htole16(255);
146 +       } else {
147 +           bs.secs_track = htole16(geometry.sectors);  /* Set up the geometry information */
148 +           bs.heads = htole16(geometry.heads);
149 +       }
150 +#elif defined(__linux__)
151         if (ioctl(dev, HDIO_GETGEO, &geometry) || geometry.sectors == 0
152             || geometry.heads == 0) {
153             printf("unable to get drive geometry, using default 255/63\n");
154 @@ -662,6 +718,11 @@ floppy_default:
155             if (!hidden_sectors_by_user)
156                 hidden_sectors = htole32(geometry.start);
157         }
158 +#else
159 +           printf("unable to get drive geometry, using default 255/63\n");
160 +           bs.secs_track = htole16(63);
161 +           bs.heads = htole16(255);
162 +#endif
163  def_hd_params:
164         bs.media = (char)0xf8;  /* Set up the media descriptor for a hard drive */
165         if (!size_fat && blocks * SECTORS_PER_BLOCK > 1064960) {
166 @@ -1693,6 +1754,15 @@ int main(int argc, char **argv)
167         die("Device partition expected, not making filesystem on entire device '%s' (use -I to override)");
168  
169      if (sector_size_set) {
170 +#if defined(__FreeBSD__)
171 +       if (ioctl(dev, DIOCGSECTORSIZE, &min_sector_size) >= 0)
172 +           if (sector_size < min_sector_size) {
173 +               sector_size = min_sector_size;
174 +               fprintf(stderr,
175 +                       "Warning: sector size was set to %d (minimal for this device)\n",
176 +                       sector_size);
177 +           }
178 +#elif defined(__linux__)
179         if (ioctl(dev, BLKSSZGET, &min_sector_size) >= 0)
180             if (sector_size < min_sector_size) {
181                 sector_size = min_sector_size;
182 @@ -1700,11 +1770,32 @@ int main(int argc, char **argv)
183                         "Warning: sector size was set to %d (minimal for this device)\n",
184                         sector_size);
185             }
186 +#elif defined(__APPLE__)
187 +       if (ioctl(dev, DKIOCGETPHYSICALBLOCKSIZE, &min_sector_size) >= 0)
188 +           if (sector_size < min_sector_size) {
189 +               sector_size = min_sector_size;
190 +               fprintf(stderr,
191 +                       "Warning: sector size was set to %d (minimal for this device)\n",
192 +                       sector_size);
193 +           }
194 +#endif
195      } else {
196 +#if defined(__FreeBSD__)
197 +       if (ioctl(dev, DIOCGSECTORSIZE, &min_sector_size) >= 0) {
198 +           sector_size = min_sector_size;
199 +           sector_size_set = 1;
200 +       }
201 +#elif defined(__linux__)
202         if (ioctl(dev, BLKSSZGET, &min_sector_size) >= 0) {
203             sector_size = min_sector_size;
204             sector_size_set = 1;
205         }
206 +#elif defined(__APPLE__)
207 +       if (ioctl(dev, DKIOCGETPHYSICALBLOCKSIZE, &min_sector_size) >= 0) {
208 +           sector_size = min_sector_size;
209 +           sector_size_set = 1;
210 +       }
211 +#endif
212      }
213  
214      if (sector_size > 4096)
215 -- 
216 1.9.1
217