private void ReadMemoryBank(MemoryBank mb, out byte[] readBuf) { int size = mb.Size; readBuf = new byte[size]; byte[][] extraInfo = null; try { Debug.WriteLine("Reading memory..."); if (mb is PagedMemoryBank) { PagedMemoryBank pmb = (PagedMemoryBank)mb; int len = pmb.PageLength; int numPgs = (size / len) + (size % len > 0 ? 1 : 0); bool hasExtra = pmb.hasExtraInfo(); int extraSize = pmb.ExtraInfoLength; if (hasExtra) { extraInfo[numPgs] = new byte[numPgs]; for (var i = 0; i < numPgs; i++) { extraInfo[0] = new byte[extraSize]; } } int retryCnt = 0; for (int i = 0; i < numPgs;) { try { bool readContinue = (i > 0) && (retryCnt == 0); if (hasExtra) { pmb.readPage(i, readContinue, readBuf, i * len, extraInfo[i]); Debug.WriteLine("Read Extra Info!"); } else { pmb.readPage(i, readContinue, readBuf, i * len); } i++; retryCnt = 0; } catch (Exception e) { if (++retryCnt > 15) { throw e; } } } } else { int retryCnt = 0; while (true) { try { mb.read(0, false, readBuf, 0, size); break; } catch (Exception e) { if (++retryCnt > 15) { throw e; } } } } } catch (Exception e) { Debug.WriteLine(e.ToString()); Debug.Write(e.StackTrace); return; } Debug.WriteLine("Done Reading memory..."); }
/// <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); } }
/// <summary> /// Dump pages from memory. /// in the provided owd instance. /// /// @parameter owd device to check for memory banks. /// @parameter showContents flag to indicate if the packet memory bank contents will /// be displayed /// </summary> public static void dumpDevicePages(OneWireContainer owd, bool showContents) { byte[] read_buf, extra_buf; int reps, i, pg, numberPages; bool found_bank = false, hasExtraInfo, hasPageAutoCRC, readContinue; Stopwatch stopWatch = new Stopwatch(); // get the port names we can use and try to open, test and close each for (System.Collections.IEnumerator bank_enum = owd.MemoryBanks; bank_enum.MoveNext();) { // get the next memory bank MemoryBank mb = (MemoryBank)bank_enum.Current; // check if has paged services if (!(mb is PagedMemoryBank)) { continue; } // cast to page bank PagedMemoryBank bank = (PagedMemoryBank)mb; // found a memory bank found_bank = true; // display bank information displayBankInformation(bank); read_buf = new byte [bank.PageLength]; extra_buf = new byte [bank.ExtraInfoLength]; // get bank flags hasPageAutoCRC = bank.hasPageAutoCRC(); hasExtraInfo = bank.hasExtraInfo(); numberPages = bank.NumberPages; // get overdrive going so not a factor in time tests try { bank.read(0, false, read_buf, 0, 1); } catch (Exception) { } // dynamically change number of reps reps = 1000 / (read_buf.Length * bank.NumberPages); if (owd.MaxSpeed == DSPortAdapter.SPEED_OVERDRIVE) { reps *= 2; } if ((reps == 0) || showContents) { reps = 1; } if (!showContents) { Debug.Write("[" + reps + "]"); } // start timer to time the dump of the bank contents stopWatch.Start(); for (i = 0; i < reps; i++) { // loop to read all of the pages in bank readContinue = false; for (pg = 0; pg < numberPages; pg++) { try { // read a page (use the most verbose and secure method) if (hasPageAutoCRC) { if (hasExtraInfo) { bank.readPageCRC(pg, readContinue, read_buf, 0, extra_buf); } else { bank.readPageCRC(pg, readContinue, read_buf, 0); } } else { if (hasExtraInfo) { bank.readPage(pg, readContinue, read_buf, 0, extra_buf); } else { bank.readPage(pg, readContinue, read_buf, 0); } } readContinue = true; if (showContents) { 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(""); } } } catch (Exception e) { Debug.WriteLine("Exception in reading page: " + e + "TRACE: "); Debug.WriteLine(e.ToString()); Debug.Write(e.StackTrace); readContinue = false; } } } stopWatch.Stop(); Debug.WriteLine(" (time to read PAGES = " + (stopWatch.ElapsedMilliseconds / reps).ToString() + "ms)"); } if (!found_bank) { Debug.WriteLine("XXXX Does not contain any general-purpose non-volatile page memory bank's"); } }