Detecting if USB has been removed by examining the .hex file

Can it be determined if a sketch doesn’t contain USB code, by only examining the compiled binary .hex file?

The initial posts in this topic have been moved from here:

Maybe a warning icon on “download hex” or “transfer”. But it would be nice to auto detect the games doing that…

1 Like

Theoretically it’s possible if the USB code either always ends up in the same place and/or always generates the same sequence of instructions.

If it’s always in the same place and always generates the same instruction sequence then you could probably just regex it.

If it stays the same but moves around you’d have to actually parse and read the .hex (which isn’t too difficult).

If the generated machine code can vary, you’re probably screwed.

That is highly unlikely. But checking for the USB interrupt vector may be an option. Will have a look tonight.

Yep checking the USB general interrupt vector works.

When there is no usb code . the vector jumps to the bad interrupt code which is a JMP 0 instruction

USB interrupt vector address is at byte addres 0x0028 and the word address to the interrupt code is located at byte address 0x2A (LSB) and 0x2B (MSB). The bad interrupt code equals the bytes 0x0c, 0x94, 0x00, 0x00

In Python:

  vector_10 = (program[0x2A] << 1) | (program[0x2B]  << 9) #USB General interrupt vector
  if program[vector_10:vector_10+4] == b'\x0c\x94\x00\x00': return True #bad  interrupts jumps to 0

Here’s a python script where you can drag and drop a bunch of hex files to check:

1 Like

This could be used in a batch process to check all hex files in @eried repo (possibly even auto-parsed by the repo itself) so games can be flagged if they remove the USB code as a way to warn users and provide common instructions for uploading other games.

1 Like

That’s assuming a sketch doesn’t use ARDUBOY_NO_USB but then includes it’s own interrupt driven USB handler. Of course, this would be so rare that it wouldn’t be worth worrying about.

I presume code that uses an alternative USB stack for HID, etc. are ok ?
e.g. V-USB, LUFA, etc.
http://www.fourwalledcubicle.com/files/LUFA/Doc/120219/html/_page__alternative_stacks.html

I’m just thinking of the programs that emulate keyboards, mice, etc.

The standard Arduino USB code can emulate a keyboard, mouse, joystick/gamepad, etc.

If you used a different USB stack that hooked into the interrupt but didn’t provide the “virtual reset to invoke the bootloader” functionality (close the virtual serial port at 1200 baud), that would cause the problem I mentioned above.

1 Like

there can be multiple endpoints. When using any of the Arduino USB examples the CDC endpoint is also still present so you can keep uploading as usual.

Ofcourse you could remove the CDC endpoint but more effort to do so.

Yeah when you’re using anyform of custom USB then using ARDUBOY_NO_USB would be kind of pointless.

Though with some caveats. This is from the Arduino Keyboard documentation:

A word of caution on using the Mouse and Keyboard libraries : if the Mouse or Keyboard library is constantly running, it will be difficult to program your board.

I think these refer to the issue where you loose control over your PC cause of constant keyboard and/or mouse input rather then flooding the USB bus.

Whatever the reason, it still means there are situations where you can’t