io_raw.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 = "ext2";
  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 io_raw_open(struct ext4_blockdev *bdev);
  45. static int io_raw_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
  46. uint32_t blk_cnt);
  47. static int io_raw_bwrite(struct ext4_blockdev *bdev, const void *buf,
  48. uint64_t blk_id, uint32_t blk_cnt);
  49. static int io_raw_close(struct ext4_blockdev *bdev);
  50. /******************************************************************************/
  51. EXT4_BLOCKDEV_STATIC_INSTANCE(_filedev, EXT4_IORAW_BSIZE, 0, io_raw_open,
  52. io_raw_bread, io_raw_bwrite, io_raw_close, 0, 0);
  53. /******************************************************************************/
  54. static int io_raw_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. dev_file =
  63. CreateFile(path, GENERIC_READ | GENERIC_WRITE,
  64. FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING,
  65. FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH, NULL);
  66. if (dev_file == INVALID_HANDLE_VALUE) {
  67. return EIO;
  68. }
  69. bResult =
  70. DeviceIoControl(dev_file, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
  71. &pdg, sizeof(pdg), &junk, (LPOVERLAPPED)NULL);
  72. if (bResult == FALSE) {
  73. CloseHandle(dev_file);
  74. return EIO;
  75. }
  76. disk_size = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *
  77. (ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
  78. _filedev.bdif->ph_bsize = pdg.BytesPerSector;
  79. _filedev.bdif->ph_bcnt = disk_size / pdg.BytesPerSector;
  80. _filedev.part_offset = 0;
  81. _filedev.part_size = disk_size;
  82. return EOK;
  83. }
  84. /******************************************************************************/
  85. static int io_raw_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
  86. uint32_t blk_cnt)
  87. {
  88. long hipart = blk_id >> (32 - 9);
  89. long lopart = blk_id << 9;
  90. long err;
  91. SetLastError(0);
  92. lopart = SetFilePointer(dev_file, lopart, &hipart, FILE_BEGIN);
  93. if (lopart == -1 && NO_ERROR != (err = GetLastError())) {
  94. return EIO;
  95. }
  96. DWORD n;
  97. if (!ReadFile(dev_file, buf, blk_cnt * 512, &n, NULL)) {
  98. err = GetLastError();
  99. return EIO;
  100. }
  101. return EOK;
  102. }
  103. /******************************************************************************/
  104. static int io_raw_bwrite(struct ext4_blockdev *bdev, const void *buf,
  105. uint64_t blk_id, uint32_t blk_cnt)
  106. {
  107. long hipart = blk_id >> (32 - 9);
  108. long lopart = blk_id << 9;
  109. long err;
  110. SetLastError(0);
  111. lopart = SetFilePointer(dev_file, lopart, &hipart, FILE_BEGIN);
  112. if (lopart == -1 && NO_ERROR != (err = GetLastError())) {
  113. return EIO;
  114. }
  115. DWORD n;
  116. if (!WriteFile(dev_file, buf, blk_cnt * 512, &n, NULL)) {
  117. err = GetLastError();
  118. return EIO;
  119. }
  120. return EOK;
  121. }
  122. /******************************************************************************/
  123. static int io_raw_close(struct ext4_blockdev *bdev)
  124. {
  125. CloseHandle(dev_file);
  126. return EOK;
  127. }
  128. /******************************************************************************/
  129. struct ext4_blockdev *ext4_io_raw_dev_get(void) { return &_filedev; }
  130. /******************************************************************************/
  131. void ext4_io_raw_filename(const char *n) { fname = n; }
  132. /******************************************************************************/
  133. #endif