Example #1
0
        public long OpenDirectory(ServiceCtx Context)
        {
            long Position = Context.Request.PtrBuff[0].Position;

            int FilterFlags = Context.RequestData.ReadInt32();

            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);

            string DirName = Context.Ns.VFs.GetFullPath(Path, Name);

            if (!Directory.Exists(DirName))
            {
                return(MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist));
            }

            if (IsPathAlreadyInUse(DirName))
            {
                return(MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse));
            }

            IDirectory DirInterface = new IDirectory(DirName, FilterFlags);

            DirInterface.Disposed += RemoveDirectoryInUse;

            lock (OpenPaths)
            {
                OpenPaths.Add(DirName);
            }

            MakeObject(Context, DirInterface);

            return(0);
        }
Example #2
0
        public long GetEntryType(ServiceCtx Context)
        {
            long Position = Context.Request.PtrBuff[0].Position;

            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);

            string FileName = Context.Ns.VFs.GetFullPath(Path, Name);

            if (File.Exists(FileName))
            {
                Context.ResponseData.Write(1);
            }
            else if (Directory.Exists(FileName))
            {
                Context.ResponseData.Write(0);
            }
            else
            {
                Context.ResponseData.Write(0);

                return(MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist));
            }

            return(0);
        }
Example #3
0
        public long OpenFile(ServiceCtx Context)
        {
            long Position = Context.Request.PtrBuff[0].Position;

            int FilterFlags = Context.RequestData.ReadInt32();

            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);

            string FileName = Context.Ns.VFs.GetFullPath(Path, Name);

            if (!File.Exists(FileName))
            {
                return(MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist));
            }

            if (IsPathAlreadyInUse(FileName))
            {
                return(MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse));
            }

            FileStream Stream = new FileStream(FileName, FileMode.Open);

            IFile FileInterface = new IFile(Stream, FileName);

            FileInterface.Disposed += RemoveFileInUse;

            lock (OpenPaths)
            {
                OpenPaths.Add(FileName);
            }

            MakeObject(Context, FileInterface);

            return(0);
        }
Example #4
0
        public static string ReadHbAbiNextLoadPath(AMemory Memory, long Position)
        {
            string FileName = null;

            while (true)
            {
                long Key = Memory.ReadInt64(Position);

                if (Key == 2)
                {
                    long Value0 = Memory.ReadInt64(Position + 0x08);
                    long Value1 = Memory.ReadInt64(Position + 0x10);

                    FileName = AMemoryHelper.ReadAsciiString(Memory, Value0, Value1 - Value0);

                    break;
                }
                else if (Key == 0)
                {
                    break;
                }

                Position += 0x18;
            }

            return(FileName);
        }
Example #5
0
        public long RenameDirectory(ServiceCtx Context)
        {
            long OldPosition = Context.Request.PtrBuff[0].Position;
            long NewPosition = Context.Request.PtrBuff[0].Position;

            string OldName = AMemoryHelper.ReadAsciiString(Context.Memory, OldPosition);
            string NewName = AMemoryHelper.ReadAsciiString(Context.Memory, NewPosition);

            string OldDirName = Context.Ns.VFs.GetFullPath(Path, OldName);
            string NewDirName = Context.Ns.VFs.GetFullPath(Path, NewName);

            if (!Directory.Exists(OldDirName))
            {
                return(MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist));
            }

            if (Directory.Exists(NewDirName))
            {
                return(MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists));
            }

            if (IsPathAlreadyInUse(OldDirName))
            {
                return(MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse));
            }

            Directory.Move(OldDirName, NewDirName);

            return(0);
        }
Example #6
0
        public long CreateDirectory(ServiceCtx Context)
        {
            long Position = Context.Request.PtrBuff[0].Position;

            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);

            string DirName = Context.Ns.VFs.GetFullPath(Path, Name);

            if (DirName == null)
            {
                return(MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist));
            }

            if (Directory.Exists(DirName))
            {
                return(MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists));
            }

            if (IsPathAlreadyInUse(DirName))
            {
                return(MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse));
            }

            Directory.CreateDirectory(DirName);

            return(0);
        }
Example #7
0
        public long OpenDirectory(ServiceCtx Context)
        {
            long Position = Context.Request.PtrBuff[0].Position;

            int FilterFlags = Context.RequestData.ReadInt32();

            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);

            string DirName = Context.Ns.VFs.GetFullPath(Path, Name);

            if (DirName != null)
            {
                if (Directory.Exists(DirName))
                {
                    MakeObject(Context, new IDirectory(DirName, FilterFlags));
                    return(0);
                }
                else
                {
                    // TODO: correct error code.
                    return(-1);
                }
            }

            // TODO: Correct error code.
            return(-1);
        }
Example #8
0
        public long OpenFile(ServiceCtx Context)
        {
            long Position = Context.Request.PtrBuff[0].Position;

            int FilterFlags = Context.RequestData.ReadInt32();

            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);

            string FileName = Context.Ns.VFs.GetFullPath(Path, Name);

            if (FileName == null)
            {
                //TODO: Correct error code.
                return(-1);
            }

            if (File.Exists(FileName))
            {
                FileStream Stream = new FileStream(FileName, FileMode.OpenOrCreate);
                MakeObject(Context, new IFile(Stream));

                return(0);
            }

            //TODO: Correct error code.
            return(-1);
        }
Example #9
0
        public long CreateFile(ServiceCtx Context)
        {
            long Position = Context.Request.PtrBuff[0].Position;

            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);

            long Mode = Context.RequestData.ReadInt64();
            int  Size = Context.RequestData.ReadInt32();

            string FileName = Context.Ns.VFs.GetFullPath(Path, Name);

            if (FileName == null)
            {
                return(MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist));
            }

            if (File.Exists(FileName))
            {
                return(MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists));
            }

            if (IsPathAlreadyInUse(FileName))
            {
                return(MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse));
            }

            using (FileStream NewFile = File.Create(FileName))
            {
                NewFile.SetLength(Size);
            }

            return(0);
        }
Example #10
0
        public void OpenAudioOutMethod(ServiceCtx Context, long SendPosition, long SendSize, long ReceivePosition, long ReceiveSize)
        {
            IAalOutput AudioOut = Context.Ns.AudioOut;

            string DeviceName = AMemoryHelper.ReadAsciiString(
                Context.Memory,
                SendPosition,
                SendSize
                );

            if (DeviceName == string.Empty)
            {
                DeviceName = DefaultAudioOutput;
            }

            byte[] DeviceNameBuffer = Encoding.ASCII.GetBytes(DeviceName + "\0");

            if ((ulong)DeviceNameBuffer.Length <= (ulong)ReceiveSize)
            {
                Context.Memory.WriteBytes(ReceivePosition, DeviceNameBuffer);
            }
            else
            {
                Context.Ns.Log.PrintError(LogClass.ServiceAudio, $"Output buffer size {ReceiveSize} too small!");
            }

            int SampleRate = Context.RequestData.ReadInt32();
            int Channels   = Context.RequestData.ReadInt32();

            Channels = (ushort)(Channels >> 16);

            if (SampleRate == 0)
            {
                SampleRate = 48000;
            }

            if (Channels < 1 || Channels > 2)
            {
                Channels = 2;
            }

            KEvent ReleaseEvent = new KEvent();

            ReleaseCallback Callback = () =>
            {
                ReleaseEvent.WaitEvent.Set();
            };

            int Track = AudioOut.OpenTrack(SampleRate, Channels, Callback, out AudioFormat Format);

            MakeObject(Context, new IAudioOut(AudioOut, ReleaseEvent, Track));

            Context.ResponseData.Write(SampleRate);
            Context.ResponseData.Write(Channels);
            Context.ResponseData.Write((int)Format);
            Context.ResponseData.Write((int)PlaybackState.Stopped);
        }
Example #11
0
        public long OpenAudioOut(ServiceCtx Context)
        {
            IAalOutput AudioOut = Context.Ns.AudioOut;

            string DeviceName = AMemoryHelper.ReadAsciiString(
                Context.Memory,
                Context.Request.SendBuff[0].Position,
                Context.Request.SendBuff[0].Size);

            if (DeviceName == string.Empty)
            {
                DeviceName = "FIXME";
            }

            long DeviceNamePosition = Context.Request.ReceiveBuff[0].Position;
            long DeviceNameSize     = Context.Request.ReceiveBuff[0].Size;

            byte[] DeviceNameBuffer = Encoding.ASCII.GetBytes(DeviceName);

            if (DeviceName.Length <= DeviceNameSize)
            {
                AMemoryHelper.WriteBytes(Context.Memory, DeviceNamePosition, DeviceNameBuffer);
            }

            int SampleRate = Context.RequestData.ReadInt32();
            int Channels   = Context.RequestData.ReadInt32();

            Channels = (ushort)(Channels >> 16);

            if (SampleRate == 0)
            {
                SampleRate = 48000;
            }

            if (Channels < 1 || Channels > 2)
            {
                Channels = 2;
            }

            KEvent ReleaseEvent = new KEvent();

            ReleaseCallback Callback = () =>
            {
                ReleaseEvent.WaitEvent.Set();
            };

            int Track = AudioOut.OpenTrack(SampleRate, Channels, Callback, out AudioFormat Format);

            MakeObject(Context, new IAudioOut(AudioOut, ReleaseEvent, Track));

            Context.ResponseData.Write(SampleRate);
            Context.ResponseData.Write(Channels);
            Context.ResponseData.Write((int)Format);
            Context.ResponseData.Write((int)PlaybackState.Stopped);

            return(0);
        }
Example #12
0
        public long GetTotalSpaceSize(ServiceCtx Context)
        {
            long Position = Context.Request.PtrBuff[0].Position;

            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);

            Context.ResponseData.Write(Context.Ns.VFs.GetDrive().TotalSize);

            return(0);
        }
Example #13
0
        public long SetAudioDeviceOutputVolume(ServiceCtx Context)
        {
            float Volume = Context.RequestData.ReadSingle();

            long Position = Context.Request.SendBuff[0].Position;
            long Size     = Context.Request.SendBuff[0].Size;

            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position, Size);

            return(0);
        }
Example #14
0
        private void SvcOutputDebugString(AThreadState ThreadState)
        {
            long Position = (long)ThreadState.X0;
            long Size     = (long)ThreadState.X1;

            string Str = AMemoryHelper.ReadAsciiString(Memory, Position, Size);

            Device.Log.PrintWarning(LogClass.KernelSvc, Str);

            ThreadState.X0 = 0;
        }
Example #15
0
        private static void SvcOutputDebugString(Switch Ns, ARegisters Registers, AMemory Memory)
        {
            long Position = (long)Registers.X0;
            long Size     = (long)Registers.X1;

            string Str = AMemoryHelper.ReadAsciiString(Memory, Position, (int)Size);

            Console.WriteLine($"SvcOutputDebugString: {Str}");

            Registers.X0 = (int)SvcResult.Success;
        }
Example #16
0
        private void SvcOutputDebugString(AThreadState ThreadState)
        {
            long Position = (long)ThreadState.X0;
            long Size     = (long)ThreadState.X1;

            string Str = AMemoryHelper.ReadAsciiString(Memory, Position, Size);

            Logging.Info($"SvcOutputDebugString: {Str}");

            ThreadState.X0 = 0;
        }
Example #17
0
        private void SvcOutputDebugString(ARegisters Registers)
        {
            long Position = (long)Registers.X0;
            long Size     = (long)Registers.X1;

            string Str = AMemoryHelper.ReadAsciiString(Memory, Position, (int)Size);

            Logging.Info($"SvcOutputDebugString: {Str}");

            Registers.X0 = (int)SvcResult.Success;
        }
Example #18
0
        public long Open(ServiceCtx Context)
        {
            long NamePtr = Context.Request.SendBuff[0].Position;

            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, NamePtr);

            int Fd = Fds.Add(Context.Process, new NvFd(Name));

            Context.ResponseData.Write(Fd);
            Context.ResponseData.Write(0);

            return(0);
        }
Example #19
0
        public long SetAudioDeviceOutputVolume(ServiceCtx Context)
        {
            float Volume = Context.RequestData.ReadSingle();

            long Position = Context.Request.SendBuff[0].Position;
            long Size     = Context.Request.SendBuff[0].Size;

            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position, Size);

            Logging.Stub(LogClass.ServiceAudio, $"Volume = {Volume}, Position = {Position}, Size = {Size}");

            return(0);
        }
Example #20
0
        private static int GetConfig(ServiceCtx Context)
        {
            long InputPosition  = Context.Request.GetBufferType0x21Position();
            long OutputPosition = Context.Request.GetBufferType0x22Position();

            string Nv   = AMemoryHelper.ReadAsciiString(Context.Memory, InputPosition + 0, 0x41);
            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, InputPosition + 0x41, 0x41);

            Context.Memory.WriteByte(OutputPosition + 0x82, 0);

            Context.Ns.Log.PrintStub(LogClass.ServiceNv, "Stubbed.");

            return(NvResult.Success);
        }
Example #21
0
        private static int GetConfig(ServiceCtx Context)
        {
            if (!IsProductionMode)
            {
                long InputPosition  = Context.Request.GetBufferType0x21().Position;
                long OutputPosition = Context.Request.GetBufferType0x22().Position;

                string Domain = AMemoryHelper.ReadAsciiString(Context.Memory, InputPosition + 0, 0x41);
                string Name   = AMemoryHelper.ReadAsciiString(Context.Memory, InputPosition + 0x41, 0x41);

                if (Set.NxSettings.Settings.TryGetValue($"{Domain}!{Name}", out object NvSetting))
                {
                    byte[] SettingBuffer = new byte[0x101];

                    if (NvSetting is string StringValue)
                    {
                        if (StringValue.Length > 0x100)
                        {
                            Logger.PrintError(LogClass.ServiceNv, $"{Domain}!{Name} String value size is too big!");
                        }
                        else
                        {
                            SettingBuffer = Encoding.ASCII.GetBytes(StringValue + "\0");
                        }
                    }

                    if (NvSetting is int IntValue)
                    {
                        SettingBuffer = BitConverter.GetBytes(IntValue);
                    }
                    else if (NvSetting is bool BoolValue)
                    {
                        SettingBuffer[0] = BoolValue ? (byte)1 : (byte)0;
                    }
                    else
                    {
                        throw new NotImplementedException(NvSetting.GetType().Name);
                    }

                    Context.Memory.WriteBytes(OutputPosition + 0x82, SettingBuffer);

                    Logger.PrintDebug(LogClass.ServiceNv, $"Got setting {Domain}!{Name}");
                }

                return(NvResult.Success);
            }

            return(NvResult.NotAvailableInProduction);
        }
Example #22
0
        private void SvcConnectToNamedPort(AThreadState ThreadState)
        {
            long StackPtr = (long)ThreadState.X0;
            long NamePtr  = (long)ThreadState.X1;

            string Name = AMemoryHelper.ReadAsciiString(Memory, NamePtr, 8);

            //TODO: Validate that app has perms to access the service, and that the service
            //actually exists, return error codes otherwise.

            HSession Session = new HSession(Name);

            ThreadState.X1 = (ulong)Ns.Os.Handles.GenerateId(Session);
            ThreadState.X0 = (int)SvcResult.Success;
        }
Example #23
0
        public long DeleteDirectory(ServiceCtx Context)
        {
            long   Position = Context.Request.PtrBuff[0].Position;
            string Name     = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
            string FileName = Context.Ns.VFs.GetFullPath(Path, Name);

            if (FileName != null)
            {
                Directory.Delete(FileName);
                return(0);
            }

            // TODO: Correct error code.
            return(-1);
        }
Example #24
0
        private void SvcConnectToNamedPort(AThreadState ThreadState)
        {
            long StackPtr = (long)ThreadState.X0;
            long NamePtr  = (long)ThreadState.X1;

            string Name = AMemoryHelper.ReadAsciiString(Memory, NamePtr, 8);

            //TODO: Validate that app has perms to access the service, and that the service
            //actually exists, return error codes otherwise.
            KSession Session = new KSession(ServiceFactory.MakeService(Name), Name);

            ulong Handle = (ulong)Process.HandleTable.OpenHandle(Session);

            ThreadState.X0 = 0;
            ThreadState.X1 = Handle;
        }
Example #25
0
        public long RenameDirectory(ServiceCtx Context)
        {
            long   OldPosition = Context.Request.PtrBuff[0].Position;
            long   NewPosition = Context.Request.PtrBuff[0].Position;
            string OldName     = AMemoryHelper.ReadAsciiString(Context.Memory, OldPosition);
            string NewName     = AMemoryHelper.ReadAsciiString(Context.Memory, NewPosition);
            string OldDirName  = Context.Ns.VFs.GetFullPath(Path, OldName);
            string NewDirName  = Context.Ns.VFs.GetFullPath(Path, NewName);

            if (OldDirName != null && NewDirName != null)
            {
                Directory.Move(OldDirName, NewDirName);
                return(0);
            }

            // TODO: Correct error code.
            return(-1);
        }
Example #26
0
        public long CreateFile(ServiceCtx Context)
        {
            long   Position = Context.Request.PtrBuff[0].Position;
            string Name     = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
            ulong  Mode     = Context.RequestData.ReadUInt64();
            uint   Size     = Context.RequestData.ReadUInt32();
            string FileName = Context.Ns.VFs.GetFullPath(Path, Name);

            if (FileName != null)
            {
                FileStream NewFile = File.Create(FileName);
                NewFile.SetLength(Size);
                NewFile.Close();
                return(0);
            }

            //TODO: Correct error code.
            return(-1);
        }
Example #27
0
        private void SvcConnectToNamedPort(AThreadState ThreadState)
        {
            long StackPtr = (long)ThreadState.X0;
            long NamePtr  = (long)ThreadState.X1;

            string Name = AMemoryHelper.ReadAsciiString(Memory, NamePtr, 8);

            //TODO: Validate that app has perms to access the service, and that the service
            //actually exists, return error codes otherwise.
            KSession Session = new KSession(ServiceFactory.MakeService(System, Name), Name);

            if (Process.HandleTable.GenerateHandle(Session, out int Handle) != KernelResult.Success)
            {
                throw new InvalidOperationException("Out of handles!");
            }

            ThreadState.X0 = 0;
            ThreadState.X1 = (uint)Handle;
        }
Example #28
0
        public long GetEntryType(ServiceCtx Context)
        {
            long Position = Context.Request.PtrBuff[0].Position;

            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);

            string FileName = Context.Ns.VFs.GetFullPath(Path, Name);

            if (FileName == null)
            {
                //TODO: Correct error code.
                return(-1);
            }

            bool IsFile = File.Exists(FileName);

            Context.ResponseData.Write(IsFile ? 1 : 0);

            return(0);
        }
Example #29
0
        private long DeleteDirectory(ServiceCtx Context, bool Recursive)
        {
            long Position = Context.Request.PtrBuff[0].Position;

            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);

            string DirName = Context.Ns.VFs.GetFullPath(Path, Name);

            if (!Directory.Exists(DirName))
            {
                return(MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist));
            }

            if (IsPathAlreadyInUse(DirName))
            {
                return(MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse));
            }

            Directory.Delete(DirName, Recursive);

            return(0);
        }
Example #30
0
        public long DeleteFile(ServiceCtx Context)
        {
            long Position = Context.Request.PtrBuff[0].Position;

            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);

            string FileName = Context.Ns.VFs.GetFullPath(Path, Name);

            if (!File.Exists(FileName))
            {
                return(MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist));
            }

            if (IsPathAlreadyInUse(FileName))
            {
                return(MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse));
            }

            File.Delete(FileName);

            return(0);
        }