Example #1
0
        /// <summary>
        /// Read method for filesystem
        /// </summary>
        /// <param name="node">The node</param>
        /// <param name="offset">The offset</param>
        /// <param name="size">The size</param>
        /// <param name="buffer">The buffer</param>
        /// <returns>The amount of bytes read</returns>
        private static unsafe uint readImpl(Node node, uint offset, uint size, byte[] buffer)
        {
            // Only support sizes in magnitudes of 512
            if (size % 512 != 0)
            {
                return(0);
            }

            USBMSCCookie cookie = (USBMSCCookie)node.Cookie;


            byte *bufferPtr = (byte *)Util.ObjectToVoidPtr(buffer);

            int  sizeRead  = 0;
            uint offsetInt = 0;
            uint offsetSec = 0;

            while (offsetInt < size)
            {
                int read = (int)cookie.USBMSC.ReadSector(offset + offsetSec, Util.PtrToArray(bufferPtr + offsetInt), 512);

                if (read == 0)
                {
                    return((uint)sizeRead);
                }

                sizeRead += read;

                offsetInt += 512;
                offsetSec++;
            }

            return((uint)sizeRead);
        }
Example #2
0
        /// <summary>
        /// USB mass storage device initalize
        /// </summary>
        /// <returns>Success?</returns>
        private unsafe bool Initalize()
        {
            _EndPointIn  = 1;
            _EndPointOut = 2;

            //if(!GetEndpoints())
            //{
            //    Console.WriteLine("[USB-MSC] Could not find endpoints");

            //    return false;
            //}

            SCSIInquiryData *inquiryResp = Inquiry();

            if (inquiryResp == null)
            {
                Console.WriteLine("[USB-MSC] Inquiry failed");

                Heap.Free(inquiryResp);
                return(false);
            }

            // Check if device supports Direct access SBC-2 or above
            if ((inquiryResp->PeripheralInfo & 0xF) != INQUIRY_PDT_DIRECT_ACCESS)
            {
                Console.WriteLine("[USB-MSC] Unsupported device type");

                Heap.Free(inquiryResp);
                return(false);
            }

            // @TODO: We should wait till ready here with the test command

            if (!ReadCapacity())
            {
                Console.WriteLine("[USB-MSC] Cannot read capacity");

                return(false);
            }

            if (_BlockLength != SUPPORTED_BLOCK_LENGTH)
            {
                Console.Write("[USB-MSC] Unsupported block length ");
                Console.WriteNum((int)_BlockLength);
                Console.WriteLine("");
            }

            // We can mount it here :D

            int deviceNum = _DeviceNum++;

            char *name = (char *)Heap.Alloc(6);

            name[0] = 'U';
            name[1] = 'S';
            name[2] = 'B';
            name[3] = 'D';
            name[4] = (char)('0' + deviceNum);
            name[5] = '\0';
            string nameStr = Util.CharPtrToString(name);

            Node node = new Node();

            node.Read  = readImpl;
            node.Write = writeImpl;

            USBMSCCookie cookie = new USBMSCCookie(_Device, this);

            node.Cookie = cookie;

            RootPoint dev = new RootPoint(nameStr, node);

            VFS.MountPointDevFS.AddEntry(dev);

            return(true);
        }