ReadCapacity10Parameter.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* Copyright (C) 2012-2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using Utilities;
  11. namespace SCSI
  12. {
  13. public class ReadCapacity10Parameter
  14. {
  15. public const int Length = 8;
  16. public uint ReturnedLBA; // the LBA of the last logical block on the direct-access block device
  17. public uint BlockLengthInBytes; // block size
  18. public ReadCapacity10Parameter()
  19. {
  20. }
  21. public ReadCapacity10Parameter(byte[] buffer)
  22. {
  23. ReturnedLBA = BigEndianConverter.ToUInt32(buffer, 0);
  24. BlockLengthInBytes = BigEndianConverter.ToUInt32(buffer, 4);
  25. }
  26. public ReadCapacity10Parameter(long diskSize, uint blockSizeInBytes)
  27. {
  28. // If the number of logical blocks exceeds the maximum value that is able to be specified in the RETURNED LOGICAL BLOCK ADDRESS field,
  29. // the device server shall set the RETURNED LOGICAL BLOCK ADDRESS field to 0xFFFFFFFF
  30. long returnedLBA = diskSize / blockSizeInBytes - 1; // zero-based LBA of the last logical block
  31. if (returnedLBA <= UInt32.MaxValue)
  32. {
  33. ReturnedLBA = (uint)returnedLBA;
  34. }
  35. else
  36. {
  37. ReturnedLBA = 0xFFFFFFFF;
  38. }
  39. BlockLengthInBytes = blockSizeInBytes;
  40. }
  41. public byte[] GetBytes()
  42. {
  43. byte[] buffer = new byte[Length];
  44. BigEndianWriter.WriteUInt32(buffer, 0, ReturnedLBA);
  45. BigEndianWriter.WriteUInt32(buffer, 4, BlockLengthInBytes);
  46. return buffer;
  47. }
  48. }
  49. }