Ejemplo n.º 1
0
    /// <summary>
    /// Read a page from a memory bank and print in hex
    /// </summary>
    /// <param name="bank">  PagedMemoryBank to read a page from </param>
    /// <param name="pg">  page to read </param>
    public static void dumpBankPage(OneWireContainer33 owd, PagedMemoryBank bank, int pg)
    {
        byte[] read_buf  = new byte [bank.PageLength];
        byte[] extra_buf = new byte [bank.ExtraInfoLength];
        byte[] challenge = new byte [8];
        byte[] secret    = new byte [8];
        byte[] sernum    = new byte [8];
        bool   macvalid  = false;

        try
        {
            // read a page (use the most verbose and secure method)
            if (bank.hasPageAutoCRC())
            {
                Debug.WriteLine("Using device generated CRC");

                if (bank.hasExtraInfo())
                {
                    bank.readPageCRC(pg, false, read_buf, 0, extra_buf);

                    owd.getChallenge(challenge, 0);
                    owd.getContainerSecret(secret, 0);
                    sernum   = owd.Address;
                    macvalid = OneWireContainer33.isMACValid(bank.StartPhysicalAddress + pg * bank.PageLength, sernum, read_buf, extra_buf, challenge, secret);
                }
                else
                {
                    bank.readPageCRC(pg, false, read_buf, 0);
                }
            }
            else
            {
                if (bank.hasExtraInfo())
                {
                    bank.readPage(pg, false, read_buf, 0, extra_buf);
                }
                else
                {
                    bank.readPage(pg, false, read_buf, 0);
                }
            }

            Debug.Write("Page " + pg + ": ");
            hexPrint(read_buf, 0, read_buf.Length);
            Debug.WriteLine("");

            if (bank.hasExtraInfo())
            {
                Debug.Write("Extra: ");
                hexPrint(extra_buf, 0, bank.ExtraInfoLength);
                Debug.WriteLine("");

                if (macvalid)
                {
                    Debug.WriteLine("Data validated with correct MAC.");
                }
                else
                {
                    Debug.WriteLine("Data not validated because incorrect MAC.");
                }
            }
        }
        catch (Exception e)
        {
            Debug.WriteLine(e);
        }
    }
Ejemplo n.º 2
0
        /// <summary> Write  memory in the current bank.  It is recommended that
        /// when writing  data that some structure in the data is created
        /// to provide error free reading back with read().  Or the
        /// method 'writePagePacket()' could be used which automatically
        /// wraps the data in a length and CRC.
        ///
        /// When using on Write-Once devices care must be taken to write into
        /// into empty space.  If write() is used to write over an unlocked
        /// page on a Write-Once device it will fail.  If write verification
        /// is turned off with the method 'setWriteVerification(false)' then
        /// the result will be an 'AND' of the existing data and the new data.
        ///
        /// </summary>
        /// <param name="startAddr">    starting address, relative to the starting physical address
        /// of this memory bank
        /// </param>
        /// <param name="writeBuf">     byte array containing data to write
        /// </param>
        /// <param name="offset">       offset into writeBuf to get data
        /// </param>
        /// <param name="len">          length in bytes to write
        ///
        /// </param>
        /// <throws>  OneWireIOException </throws>
        /// <throws>  OneWireException </throws>
        public virtual void  write(int startAddr, byte[] writeBuf, int offset, int len)
        {
            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
            if (DEBUG)
            {
                Debug.debug("-----------------------------------------------------------");
                Debug.debug("MemoryBankSHAEE.write(int,byte[],int,int) called");
                Debug.debug("  startAddr=0x" + Convert.toHexString((byte)startAddr));
                Debug.debug("  writeBuf", writeBuf, offset, len);
                Debug.debug("  startPhysicalAddress=0x" + Convert.toHexString((byte)startPhysicalAddress));
            }
            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
            int room_left;

            if (!checked_Renamed)
            {
                checked_Renamed = ib.checkStatus();
            }

            // return if nothing to do
            if (len == 0)
            {
                return;
            }

            // attempt to put device at speed
            checkSpeed();

            // check to see if secret is set
            if (!ib.ContainerSecretSet)
            {
                throw new OneWireException("Secret is not set.");
            }

            // check if write exceeds memory
            if ((startAddr + len) > size)
            {
                throw new OneWireException("Write exceeds memory bank end");
            }

            // check if trying to write read only bank
            if (ReadOnly)
            {
                throw new OneWireException("Trying to write read-only memory bank");
            }

            // loop while still have pages to write
            int startx = 0, nextx = 0;             // (start and next index into writeBuf)

            byte[] raw_buf  = new byte[8];
            byte[] memory   = new byte[(size - (startAddr & 0xE0))];           // till end of memory
            int    abs_addr = startPhysicalAddress + startAddr;
            int    pl       = 8;

            read(startAddr & 0xE0, false, memory, 0, memory.Length);

            if (abs_addr >= 128)
            {
                ib.getContainerSecret(memory, 0);
            }

            do
            {
                // calculate room left in current page
                room_left = pl - ((abs_addr + startx) % pl);

                // check if block left will cross end of page
                if ((len - startx) > room_left)
                {
                    nextx = startx + room_left;
                }
                else
                {
                    nextx = len;
                }

                // bug fix, if updating pages two and three in the same write op
                // this used to fail, was (startAddr>=pageLength)
                if ((startx + startAddr) >= pageLength)
                {
                    Array.Copy(memory, (((startx + startAddr) / 8) * 8) - 32, raw_buf, 0, 8);
                }
                else
                {
                    Array.Copy(memory, (((startx + startAddr) / 8) * 8), raw_buf, 0, 8);
                }

                if ((nextx - startx) == 8)
                {
                    Array.Copy(writeBuf, offset + startx, raw_buf, 0, 8);
                }
                else if (((startAddr + nextx) % 8) == 0)
                {
                    Array.Copy(writeBuf, offset + startx, raw_buf, ((startAddr + startx) % 8), 8 - ((startAddr + startx) % 8));
                }
                else
                {
                    Array.Copy(writeBuf, offset + startx, raw_buf, ((startAddr + startx) % 8), ((startAddr + nextx) % 8) - ((startAddr + startx) % 8));
                }

                // write the page of data to scratchpad
                scratchpad.writeScratchpad(abs_addr + startx + room_left - 8, raw_buf, 0, 8);

                // Copy data from scratchpad into memory
                scratchpad.copyScratchpad(abs_addr + startx + room_left - 8, raw_buf, 0, memory, 0);

                // bug fix, if updating pages two and three in the same write op
                // this used to fail, was (startAddr>=pageLength)
                if ((startx + startAddr) >= pageLength)
                {
                    Array.Copy(raw_buf, 0, memory, (((startx + startAddr) / 8) * 8) - 32, 8);
                }
                else
                {
                    Array.Copy(raw_buf, 0, memory, (((startx + startAddr) / 8) * 8), 8);
                }

                // point to next index
                startx = nextx;
            }while (nextx < len);

            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
            if (DEBUG)
            {
                Debug.debug("-----------------------------------------------------------");
            }
            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
        }