Author Topic: How do you deal with stray code?  (Read 5262 times)

0 Members and 1 Guest are viewing this topic.

Offline technixTopic starter

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
How do you deal with stray code?
« on: October 18, 2018, 02:18:37 pm »
What do I mean stray code: when you buy used components with nonvolatile memory online, the previous owner or the seller might not have wiped the chip.

For my old MCU and EEPROM I purchased I always test the chip before marking it as accepted on Taobao using my universal programmer, but from time to time this process reveals code stray on the chip. Usually I save it before wiping the chip, but how should I deal with those saved ROM images? I have no idea what hardware the code is for, often not knowing that architecture the code is for. (There was once that from some old AT28C256 revealed BIOS code for some SCSI card...)
 

Offline Ampera

  • Super Contributor
  • ***
  • Posts: 2578
  • Country: us
    • Ampera's Forums
Re: How do you deal with stray code?
« Reply #1 on: October 18, 2018, 02:24:30 pm »
I usually call up Assembly Control, and have the stray images taken to the local pound for either adoption or euthanasia.

If it's just random code on some random PROM, save it, make sure it's not important, then toss it, unless you want it. It's like buying a used wallet and finding a library card in it, there's really just nothing much it does to you.
I forget who I am sometimes, but then I remember that it's probably not worth remembering.
EEVBlog IRC Admin - Join us on irc.austnet.org #eevblog
 
The following users thanked this post: PointyOintment, tooki, newbrain, NivagSwerdna

Offline taydin

  • Frequent Contributor
  • **
  • Posts: 520
  • Country: tr
Re: How do you deal with stray code?
« Reply #2 on: October 18, 2018, 02:47:57 pm »
I think the OP wants to know how to determine CPU architecture, given only machine code.

I don't think there is an automated way to do this. But what would be possible is to build a list of possible architectures, disassemble for each, and then see if the assembly code generated is sensible. For example, subroutine calls should have proper prologue/epilogue at entry/exit, some registers should be loaded upon entering a subroutine and result should be stored in a register before returning.

A human programmer can easily tell if a given disassembled code is REALLY code or if it's just a random bunch of bytes. This would be quite harder to do automatically using a software.
Real programmers use machine code!

My hobby projects http://mekatronik.org/forum
 

Offline technixTopic starter

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: How do you deal with stray code?
« Reply #3 on: October 18, 2018, 03:05:05 pm »
I think the OP wants to know how to determine CPU architecture, given only machine code.

I don't think there is an automated way to do this. But what would be possible is to build a list of possible architectures, disassemble for each, and then see if the assembly code generated is sensible. For example, subroutine calls should have proper prologue/epilogue at entry/exit, some registers should be loaded upon entering a subroutine and result should be stored in a register before returning.

A human programmer can easily tell if a given disassembled code is REALLY code or if it's just a random bunch of bytes. This would be quite harder to do automatically using a software.
Will I get in trouble if I upload an example for you guys to see?
 

Offline taydin

  • Frequent Contributor
  • **
  • Posts: 520
  • Country: tr
Re: How do you deal with stray code?
« Reply #4 on: October 18, 2018, 03:13:17 pm »
Will I get in trouble if I upload an example for you guys to see?

What you are trying to do is a time consuming process, and you are not likely to find somebody that will do it for free.

But if I were you, I would get the IDA Pro disassembler, which supports disassembly of many different CPU architectures. It can also detect libraries of many toolchains automatically, using FLIRT (  ;D ) technology. I have no idea how much IDA costs, if you are a hobbyist, it might be too expensive.
Real programmers use machine code!

My hobby projects http://mekatronik.org/forum
 

Offline technixTopic starter

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: How do you deal with stray code?
« Reply #5 on: October 18, 2018, 03:22:17 pm »
Will I get in trouble if I upload an example for you guys to see?

What you are trying to do is a time consuming process, and you are not likely to find somebody that will do it for free.

But if I were you, I would get the IDA Pro disassembler, which supports disassembly of many different CPU architectures. It can also detect libraries of many toolchains automatically, using FLIRT (  ;D ) technology. I have no idea how much IDA costs, if you are a hobbyist, it might be too expensive.
I don't have IDA Pro and I am not really interested in finding out what the processor is - that might as well be configuration data for all I know. What I am asking is what is the polite and safe way of handling it.
 

Offline bd139

  • Super Contributor
  • ***
  • Posts: 23018
  • Country: gb
Re: How do you deal with stray code?
« Reply #6 on: October 18, 2018, 03:50:57 pm »
You need cpu_rec: https://github.com/airbus-seclab/cpu_rec ... this is a plugin for binwalk https://github.com/ReFirmLabs/binwalk

Dump the EPROM onto your PC then run it through that

Once you've got the architecture nailed down, find a disassembler for it.

Beware though that you will get false positives. Also some architectures are 16-bit wide so you might only have 8 of those bits at which point you are screwed :)

Also worth running "strings" on it to look for ASCII stuff.

Disclaimer: I've never done this at all. Not once  :-DD
« Last Edit: October 18, 2018, 03:52:48 pm by bd139 »
 

Offline NivagSwerdna

  • Super Contributor
  • ***
  • Posts: 2495
  • Country: gb
Re: How do you deal with stray code?
« Reply #7 on: October 18, 2018, 04:10:00 pm »
I got one recently... all locations had FF.   I wonder what architecture and opcode FF is for?

;)

PS
The ones containing explicit images can be used for blackmail of the original seller
 
The following users thanked this post: bd139

Offline technixTopic starter

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: How do you deal with stray code?
« Reply #8 on: October 18, 2018, 04:49:44 pm »
I got one recently... all locations had FF.   I wonder what architecture and opcode FF is for?

;)

PS
The ones containing explicit images can be used for blackmail of the original seller
That means the chip you bought was new or properly wiped before shipping. Mine wasn't wiped at all and had non-FF code in it.
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9889
  • Country: us
Re: How do you deal with stray code?
« Reply #9 on: October 18, 2018, 04:57:27 pm »
I got one recently... all locations had FF.   I wonder what architecture and opcode FF is for?

;)

PS
The ones containing explicit images can be used for blackmail of the original seller
That means the chip you bought was new or properly wiped before shipping. Mine wasn't wiped at all and had non-FF code in it.

Why mess around?  Trying to analyze bytes is an infinite time sink.  Erase the thing and more on!
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11622
  • Country: my
  • reassessing directives...
Re: How do you deal with stray code?
« Reply #10 on: October 18, 2018, 05:03:53 pm »
yes a good time waster. who knows you might find pamela anderson in it or bill gates' bank account number.
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 8167
  • Country: fi
Re: How do you deal with stray code?
« Reply #11 on: October 18, 2018, 05:18:24 pm »
How about shopping lists you find on shopping mall floors? What do you do with them?  :palm:
 
The following users thanked this post: kony, PointyOintment, newbrain

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21657
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: How do you deal with stray code?
« Reply #12 on: October 18, 2018, 06:44:31 pm »
Well... being an open-ended task, do it the same you'd do anything else.

Stare at it a while.

Is it repeating?  Might be tables of some sort.

Any ASCII (or EBCDIC, or other encoding for that matter) apparent?

As mentioned, if it's slices of a 16 (or more) bit bus, you'll see that more obviously in human-readable formats like ASCII, but less so in others...

Basic stat and crypto checks: do a frequency analysis.  Do an entropy analysis.  Look for sentinel codes, or magic numbers.  Look for checksums or hashes (often at the top or bottom of the image).

If it's very high entropy, it's possible it is compressed or encrypted.  Would be unusual for a ROM I would think, but who knows.  Back in the ROM days, that sort of thing was rather expensive to do, except when absolutely needed (and that, mainly for tape and demo purposes?).

If you aren't familiar with many (or any) instruction sets, binary will just look like gibberish to you.  You'll have to learn a few first -- x86, Z80, 6502 and 68k might be good starting points.  This will take the better part of a year; queue up some projects to use each one, so you have motivation.

Offhand, I know that:
- Z80 machine code is pretty simple, and makes frequent use of middling-range (ASCII readable) codes.  This can make it difficult to eyeball if a passage is code or data!
- x86 tends to be noisier, with occasional patches of recognizable offsets (e.g., load-immediate, absolute address, index indirect..).  That is, groupings of similar numbers keep appearing, often either small addresses or offsets or values (say "01 00" = 0x0001), or addresses to the same region of memory (0x70a4, 0x70a8, ..) suggesting a data segment there.  The most recognizable, and usefully so, opcodes are push reg: you regularly see a "PQR" in the hex dump, for something like PUSH BP / MOV BP, SP / PUSH SI ..., or whatever they actually are.  In other words, normal (Intel ABI) function preamble!
- I haven't worked with other instruction sets in raw format very much, but the other ones that I'm familiar with are unlikely to be found in EPROMs anyway (e.g. AVR).  So, thus ends my flavor text. 8)

If nothing else, feel free to send them to someone who loves archiving stray code, like Jason Scott. :-DD

Tim
« Last Edit: October 18, 2018, 06:53:10 pm by T3sl4co1l »
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline bson

  • Supporter
  • ****
  • Posts: 2269
  • Country: us
Re: How do you deal with stray code?
« Reply #13 on: October 18, 2018, 09:38:27 pm »
Folks, spay your codes or they will go stray!
 
The following users thanked this post: BillyD

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14445
  • Country: fr
Re: How do you deal with stray code?
« Reply #14 on: October 19, 2018, 01:20:46 am »
What I am asking is what is the polite and safe way of handling it.

Polite and safe: just erase the chips and move on. If you're extra polite, you may even warn the seller about existing data. But I wouldn't bother. It's the responsibility of someone selling storage devices to take care of privacy issues and erase them before selling. If they didn't, why would you even care?

Now if you are curious and just want to take a peek at the data - which doesn't seem to be the case - have at it. Guess that would be some rather concrete case of "data mining". ;D
 

Offline technixTopic starter

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: How do you deal with stray code?
« Reply #15 on: October 19, 2018, 05:45:33 am »
How about shopping lists you find on shopping mall floors? What do you do with them?  :palm:
Executable code of unknown architecture is inherently more interesting than shopping lists of library cards.
 
The following users thanked this post: mrpackethead

Offline NivagSwerdna

  • Super Contributor
  • ***
  • Posts: 2495
  • Country: gb
Re: How do you deal with stray code?
« Reply #16 on: October 19, 2018, 03:19:01 pm »
Slightly related.... when identifying ROMs from Arcade machines http://romident.coinopflorida.com/ is very useful.  Basically it runs a CRC and compares it to the known ROM checksums in MAME.

Interestingly the ROM I left by the Window and just checked is the same architecture/instruction set as the other ones I had... all FFs...   ;)
 

Offline technixTopic starter

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: How do you deal with stray code?
« Reply #17 on: October 19, 2018, 03:48:11 pm »
Slightly related.... when identifying ROMs from Arcade machines http://romident.coinopflorida.com/ is very useful.  Basically it runs a CRC and compares it to the known ROM checksums in MAME.

Interestingly the ROM I left by the Window and just checked is the same architecture/instruction set as the other ones I had... all FFs...   ;)
The Sun can erase quartz window EPROMs given enough time.
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: How do you deal with stray code?
« Reply #18 on: October 19, 2018, 05:53:25 pm »
Usually I save it before wiping the chip,
Why?
 

Offline technixTopic starter

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: How do you deal with stray code?
« Reply #19 on: October 19, 2018, 06:11:55 pm »
Usually I save it before wiping the chip,
Why?
Just in case someone might regret losing that chip’s data.
 

Offline NivagSwerdna

  • Super Contributor
  • ***
  • Posts: 2495
  • Country: gb
Re: How do you deal with stray code?
« Reply #20 on: October 19, 2018, 08:42:48 pm »
Usually I save it before wiping the chip,
Why?
Just in case someone might regret losing that chip’s data.
I always save a hex dump of my ROMs that contain all FFs just in case.  ;)
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14445
  • Country: fr
Re: How do you deal with stray code?
« Reply #21 on: October 20, 2018, 12:31:14 am »
I always save a hex dump of my ROMs that contain all FFs just in case.  ;)

 :-DD
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21657
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: How do you deal with stray code?
« Reply #22 on: October 20, 2018, 02:19:07 am »
You should erase your EPROMs with a UV laser pointer, so you have a steady supply of hand-crafted, bespoke artisinal bits and 0xFFs. ;D

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 
The following users thanked this post: NivagSwerdna, bd139

Offline rrinker

  • Super Contributor
  • ***
  • Posts: 2046
  • Country: us
Re: How do you deal with stray code?
« Reply #23 on: October 22, 2018, 03:02:57 pm »
 The images tend to be crude on some of those older low density EEPROMS though, no matter how good your laser pointer is.
The larger ones though - release that inner van Gogh!
 

Offline james_s

  • Super Contributor
  • ***
  • Posts: 21611
  • Country: us
Re: How do you deal with stray code?
« Reply #24 on: October 22, 2018, 03:58:39 pm »
I usually dump any random ROMs I find and browse through it for recognizable strings. Occasionally I find something potentially useful or interesting like the BIOS from some vintage PC or peripheral, other times it's just interesting to see what's there.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf