IStorage.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using Ryujinx.HLE.HOS.Ipc;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace Ryujinx.HLE.HOS.Services.FspSrv
  5. {
  6. class IStorage : IpcService
  7. {
  8. private Dictionary<int, ServiceProcessRequest> _commands;
  9. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
  10. private Stream _baseStream;
  11. public IStorage(Stream baseStream)
  12. {
  13. _commands = new Dictionary<int, ServiceProcessRequest>
  14. {
  15. { 0, Read },
  16. { 4, GetSize }
  17. };
  18. _baseStream = baseStream;
  19. }
  20. // Read(u64 offset, u64 length) -> buffer<u8, 0x46, 0> buffer
  21. public long Read(ServiceCtx context)
  22. {
  23. long offset = context.RequestData.ReadInt64();
  24. long size = context.RequestData.ReadInt64();
  25. if (context.Request.ReceiveBuff.Count > 0)
  26. {
  27. IpcBuffDesc buffDesc = context.Request.ReceiveBuff[0];
  28. //Use smaller length to avoid overflows.
  29. if (size > buffDesc.Size)
  30. {
  31. size = buffDesc.Size;
  32. }
  33. byte[] data = new byte[size];
  34. lock (_baseStream)
  35. {
  36. _baseStream.Seek(offset, SeekOrigin.Begin);
  37. _baseStream.Read(data, 0, data.Length);
  38. }
  39. context.Memory.WriteBytes(buffDesc.Position, data);
  40. }
  41. return 0;
  42. }
  43. // GetSize() -> u64 size
  44. public long GetSize(ServiceCtx context)
  45. {
  46. context.ResponseData.Write(_baseStream.Length);
  47. return 0;
  48. }
  49. }
  50. }