0013-Add-compatible-ioctl-calls-for-OSX-and-FreeBSD.patch 6.8 KB

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