IStorage.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. };
  17. _baseStream = baseStream;
  18. }
  19. // Read(u64 offset, u64 length) -> buffer<u8, 0x46, 0> buffer
  20. public long Read(ServiceCtx context)
  21. {
  22. long offset = context.RequestData.ReadInt64();
  23. long size = context.RequestData.ReadInt64();
  24. if (context.Request.ReceiveBuff.Count > 0)
  25. {
  26. IpcBuffDesc buffDesc = context.Request.ReceiveBuff[0];
  27. //Use smaller length to avoid overflows.
  28. if (size > buffDesc.Size)
  29. {
  30. size = buffDesc.Size;
  31. }
  32. byte[] data = new byte[size];
  33. lock (_baseStream)
  34. {
  35. _baseStream.Seek(offset, SeekOrigin.Begin);
  36. _baseStream.Read(data, 0, data.Length);
  37. }
  38. context.Memory.WriteBytes(buffDesc.Position, data);
  39. }
  40. return 0;
  41. }
  42. }
  43. }