DiskUtil.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /** @file
  2. Disk utilities
  3. Copyright (c) 2021 Pedro Falcato All rights reserved.
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "Ext4Dxe.h"
  7. /**
  8. Reads from the partition's disk using the DISK_IO protocol.
  9. @param[in] Partition Pointer to the opened ext4 partition.
  10. @param[out] Buffer Pointer to a destination buffer.
  11. @param[in] Length Length of the destination buffer.
  12. @param[in] Offset Offset, in bytes, of the location to read.
  13. @return Success status of the disk read.
  14. **/
  15. EFI_STATUS
  16. Ext4ReadDiskIo (
  17. IN EXT4_PARTITION *Partition,
  18. OUT VOID *Buffer,
  19. IN UINTN Length,
  20. IN UINT64 Offset
  21. )
  22. {
  23. return EXT4_DISK_IO (Partition)->ReadDisk (
  24. EXT4_DISK_IO (Partition),
  25. EXT4_MEDIA_ID (Partition),
  26. Offset,
  27. Length,
  28. Buffer
  29. );
  30. }
  31. /**
  32. Reads blocks from the partition's disk using the DISK_IO protocol.
  33. @param[in] Partition Pointer to the opened ext4 partition.
  34. @param[out] Buffer Pointer to a destination buffer.
  35. @param[in] NumberBlocks Length of the read, in filesystem blocks.
  36. @param[in] BlockNumber Starting block number.
  37. @return Success status of the read.
  38. **/
  39. EFI_STATUS
  40. Ext4ReadBlocks (
  41. IN EXT4_PARTITION *Partition,
  42. OUT VOID *Buffer,
  43. IN UINTN NumberBlocks,
  44. IN EXT4_BLOCK_NR BlockNumber
  45. )
  46. {
  47. UINT64 Offset;
  48. UINTN Length;
  49. ASSERT (NumberBlocks != 0);
  50. ASSERT (BlockNumber != EXT4_BLOCK_FILE_HOLE);
  51. Offset = MultU64x32 (BlockNumber, Partition->BlockSize);
  52. Length = NumberBlocks * Partition->BlockSize;
  53. // Check for overflow on the block -> byte conversions.
  54. // Partition->BlockSize is never 0, so we don't need to check for that.
  55. if (DivU64x64Remainder (Offset, BlockNumber, NULL) != Partition->BlockSize) {
  56. return EFI_INVALID_PARAMETER;
  57. }
  58. if (Length / NumberBlocks != Partition->BlockSize) {
  59. return EFI_INVALID_PARAMETER;
  60. }
  61. return Ext4ReadDiskIo (Partition, Buffer, Length, Offset);
  62. }
  63. /**
  64. Allocates a buffer and reads blocks from the partition's disk using the DISK_IO protocol.
  65. This function is deprecated and will be removed in the future.
  66. @param[in] Partition Pointer to the opened ext4 partition.
  67. @param[in] NumberBlocks Length of the read, in filesystem blocks.
  68. @param[in] BlockNumber Starting block number.
  69. @return Buffer allocated by AllocatePool, or NULL if some part of the process
  70. failed.
  71. **/
  72. VOID *
  73. Ext4AllocAndReadBlocks (
  74. IN EXT4_PARTITION *Partition,
  75. IN UINTN NumberBlocks,
  76. IN EXT4_BLOCK_NR BlockNumber
  77. )
  78. {
  79. VOID *Buf;
  80. UINTN Length;
  81. // Check that number of blocks isn't empty, because
  82. // this is incorrect condition for opened partition,
  83. // so we just early-exit
  84. if ((NumberBlocks == 0) || (BlockNumber == EXT4_BLOCK_FILE_HOLE)) {
  85. return NULL;
  86. }
  87. Length = NumberBlocks * Partition->BlockSize;
  88. // Check for integer overflow
  89. if (Length / NumberBlocks != Partition->BlockSize) {
  90. return NULL;
  91. }
  92. Buf = AllocatePool (Length);
  93. if (Buf == NULL) {
  94. return NULL;
  95. }
  96. if (Ext4ReadBlocks (Partition, Buf, NumberBlocks, BlockNumber) != EFI_SUCCESS) {
  97. FreePool (Buf);
  98. return NULL;
  99. }
  100. return Buf;
  101. }