file_windows.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * - The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <ext4_config.h>
  29. #include <ext4_blockdev.h>
  30. #include <ext4_errno.h>
  31. #include <stdio.h>
  32. #include <stdbool.h>
  33. #include <string.h>
  34. #ifdef WIN32
  35. #include <windows.h>
  36. #include <winioctl.h>
  37. /**@brief Default filename.*/
  38. static const char *fname = "ExtFs.img";
  39. /**@brief IO block size.*/
  40. #define EXT4_IORAW_BSIZE 512
  41. /**@brief Image file descriptor.*/
  42. static HANDLE dev_file;
  43. /**********************BLOCKDEV INTERFACE**************************************/
  44. static int file_open(struct ext4_blockdev *bdev);
  45. static int file_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
  46. uint32_t blk_cnt);
  47. static int file_bwrite(struct ext4_blockdev *bdev, const void *buf,
  48. uint64_t blk_id, uint32_t blk_cnt);
  49. static int file_close(struct ext4_blockdev *bdev);
  50. /******************************************************************************/
  51. EXT4_BLOCKDEV_STATIC_INSTANCE(_filedev, EXT4_IORAW_BSIZE, 0, file_open,
  52. file_bread, file_bwrite, file_close, 0, 0);
  53. /******************************************************************************/
  54. static int file_open(struct ext4_blockdev *bdev)
  55. {
  56. char path[64];
  57. DISK_GEOMETRY pdg;
  58. uint64_t disk_size;
  59. BOOL bResult = FALSE;
  60. DWORD junk;
  61. sprintf(path, "\\\\.\\%s", fname);
  62. printf("%s\n",path);
  63. dev_file =
  64. CreateFile(path, GENERIC_READ | GENERIC_WRITE,
  65. FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING,
  66. FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH, NULL);
  67. if (dev_file == INVALID_HANDLE_VALUE) {
  68. printf("open failed\n");
  69. return EIO;
  70. }
  71. bResult =
  72. DeviceIoControl(dev_file, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
  73. &pdg, sizeof(pdg), &junk, (LPOVERLAPPED)NULL);
  74. if (bResult == FALSE) {
  75. CloseHandle(dev_file);
  76. return EIO;
  77. }
  78. disk_size = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *
  79. (ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
  80. _filedev.bdif->ph_bsize = pdg.BytesPerSector;
  81. _filedev.bdif->ph_bcnt = disk_size / pdg.BytesPerSector;
  82. _filedev.part_offset = 0;
  83. _filedev.part_size = disk_size;
  84. return EOK;
  85. }
  86. /******************************************************************************/
  87. static int file_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
  88. uint32_t blk_cnt)
  89. {
  90. long hipart = blk_id >> (32 - 9);
  91. long lopart = blk_id << 9;
  92. long err;
  93. SetLastError(0);
  94. lopart = SetFilePointer(dev_file, lopart, &hipart, FILE_BEGIN);
  95. if (lopart == -1 && NO_ERROR != (err = GetLastError())) {
  96. return EIO;
  97. }
  98. DWORD n;
  99. if (!ReadFile(dev_file, buf, blk_cnt * 512, &n, NULL)) {
  100. err = GetLastError();
  101. return EIO;
  102. }
  103. return EOK;
  104. }
  105. /******************************************************************************/
  106. static int file_bwrite(struct ext4_blockdev *bdev, const void *buf,
  107. uint64_t blk_id, uint32_t blk_cnt)
  108. {
  109. long hipart = blk_id >> (32 - 9);
  110. long lopart = blk_id << 9;
  111. long err;
  112. SetLastError(0);
  113. lopart = SetFilePointer(dev_file, lopart, &hipart, FILE_BEGIN);
  114. if (lopart == -1 && NO_ERROR != (err = GetLastError())) {
  115. return EIO;
  116. }
  117. DWORD n;
  118. if (!WriteFile(dev_file, buf, blk_cnt * 512, &n, NULL)) {
  119. err = GetLastError();
  120. return EIO;
  121. }
  122. return EOK;
  123. }
  124. /******************************************************************************/
  125. static int file_close(struct ext4_blockdev *bdev)
  126. {
  127. CloseHandle(dev_file);
  128. return EOK;
  129. }
  130. /******************************************************************************/
  131. struct ext4_blockdev *file_windows_dev_get(void)
  132. {
  133. return &_filedev;
  134. }
  135. /******************************************************************************/
  136. void file_windows_name_set(const char *n)
  137. {
  138. fname = n;
  139. }
  140. /******************************************************************************/
  141. #endif