Wednesday, April 16, 2014

BeagleBone Device Tree Overlays

(All commands and tests were performed in Debian Wheezy, though in most cases, should also work in various other Linux Distro's)

Real quickly, what is a device tree [DT] overlay? Well, just like a .config file - such as those used in Minecraft Mods - a device tree overlay describes how the kernel is wired in order to take advantage of any new hardware peripherals dynamically - in the case of the bone, it is most commonly used to describe which pins are wire to what device and who has compatibility.

Let's review over the structure of a simple device tree: (Thanks to Adafruit for their valuable detailing)
Unfortunately, Debian and several other distros do not come with device tree source files, just the compiled blobs. This was unfortunate for me as I had four Debian-imaged bones and one Angstrom bone that I forgot the login info to... after several hours I managed to reimage the Angstrom copy to see if the source files were even there, and thankfully they were!



EDIT: While browsing through the raw Linux Kernel source code, I found a number of other *.dts files linking to many other devices. To find specific names and values for these trees, the kernel tree its self has all the answers - it may be intimidating at first but really I feel it is no different from Minecraft: Kernel=Vanilla, Drivers=Mods, Hardware Descriptors=Config Files. The Beaglebone Black uses an OMAP3 [OMAP3630] microprocessor also references as AM33XX in the source code. Linux Source Code



This is the version of the DT, which is 'version 1'
/dts-v1/;

This comes next, tagging the DT as a plugin
/plugin/;

Without specifying compatibility, this plugin would not work
compatible = "ti,beaglebone", "ti,beaglebone-black";

Next is the identification, including the DT name and version - which seems to always be '00A0' (this is also the name of the file in the form NAME-VERSION.dtbo)
part-number = "BB-BONE-DT-NAME";
version = "00A0";

Include any and all resources to be used. (In some cases, just the pins are enough, otherwise it is always good to include the device the pins will be muxed to as well) Here the pin is specified in the form HEADER.PIN, the first pru will also be used:
exclusive-use = "P9.27", "pru0";

And finally the fragments, which can be seen as partitions, or 'structs' in a c-stylized way of looking at things. A device tree can have as many fragments as it needs, but for the simplest layouts, one only needs two: the first for pin muxing and the second for device initialization and pin binding. The syntax goes as follows: fragment@NUM { }; so fragment@0 {}; or fragment@2 {};

A fragment will begin with a target, followed by the __overlay__ block, and within that block resides the settings to be applied to or through the targeted device. For example:
fragment@0 {
    target = <&am33xx_pinmux>;
    __overlay__ {
        token_name: sub_name {
            pinctrl-single,pins = < 
                0x1a4 0x25 
            >;
        };
    };
};
The target is am33xx_pinmux and through the target via 'pinctrl-single,pins,' pin P9.27(0x1a4) is muxed to pin mode 5 as a 'fast-input' with an internal pull-down (0x25) - this prepares the pins for PRU0 output. The 'token_name:' is referenced in other fragments further down.

When initializing a device in the device tree, one must set the status to 'okay.' Then in order to do anything with that device, one must assign the pins created earlier:
fragment@2 {
    target = <&pruss>;
    __overlay__ {
        status = "okay";
        pinctrl-names = "default";
        pinctrl-0 = <&token_name>;
    };
};
The pin names can be left as 'default.' Sometimes there are other names that can be added for state manipulation such as found in the source file: cape-boneblack-hdmi-00A0.dts: pinctrl-names = "default", "off"; which can be seen in action through shell:
# cat /sys/devices/ocp.*/hdmi.*/pinmux_state
default
# echo off > /sys/devices/ocp.*/hdmi.*/pinmux_state
# cat /sys/devices/ocp.*/hdmi.*/pinmux_state
off
# echo "test" > /sys/devices/ocp.*/hdmi.*/pinmux_state
-sh: echo: write error: No such device



The following is a list of devices that can be modified or used via device tree overlays:
am33xx_pinmux    ocp - on chip peripherals
tscadc - analog digital convertermac - media access control
eCAP0_in_PWM0_outeCAP1_in_PWM1_out
eCAP2_in_PWM2_outtps - transaction processing system
mcasp0 - multichannel audio serial portmcasp1
eMMC_RSTngpmc - general purpose memory controller
pruss - programmable real-time unit subsystem pru
pru0pru1
spi0 - serial peripheral interfacespi1
uart1 - universal asynchronous receive transmituart2
uart3uart4
uart5uart6
i2c0 - inter-integrated circuiti2c1
i2c2intc - interrupt controller
ehrpwm0 - enhanced high resolution pwmehrpwm0A
ehrpwm0Behrpwm1
ehrpwm1Aehrpwm1B
ehrpwm2ehrpwm2A
ehrpwm2Bepwmss0
epwmss1 - enhanced pulse-width mod. subsystemepwmss2
ecap0 - enhanced captureecap1
ecap2edma - enhanced direct memory access
lcd - liquid crystal displaylcdc - liquid crystal display controller
aes - advanced encryption standardsham
usb_otg_hstps-bl
dcan0dcan1
clkout2mmc1 - multimedia card
mmc2mmc3
timer1timer2
timer3timer4
timer5timer6
timer7wdt
gpio1 - general purpose input outputgpio2
gpio3gpio4
gpio0_0-31gpio1_0-31
gpio2_031gpio3_0-31
P8.1-P8.46P9.1-P9.46
rtcusb - universal serial bus
sgxmdio
..



To compile the source, run the following shell command:
dtc -O dtb -o $NAME-$VERSION.dtbo -b 0 -@ $NAME-$VERSION.dts

To apply the new overlay, copy the *.dtbo file to /lib/framework and run the command:
echo $NAME > /sys/devices/bone_capemgr.*/slots

Confirm that it has loaded:
cat /sys/devices/bone_capemgr.*/slots

And to remove it, use the slot number where it is found:
echo -$SLOT_NUM > /sys/devices/bone_capemgr.*/slots





Tuesday, April 15, 2014

Pestering Adware

Though I may be a Linux user, and although there are programs such as 'wine' for emulating Windows-based apps in Linux, there is still software out there that can only run in Windows - including various Autodesk, Adobe, and Corel products,  and so I remain in a windows environment. And as every seasoned Windows user knows, no matter how good one's anti-malware,  -spyware, -adware, or anti-virus software is, there is always the rare occasion where it cannot do the job alone.

www.superfish.com, api.jollywallet.com, istatic.datafastguru.info, these are some of the URLs that pestered me for months and none of the programs I had available could find them. Thanks to Chrome's Console (Right Click > Inspect Element > Console) and the step-by-step descriptions provided here, I identified the foreign object and successfully removed it from my machine - no uninstalls and no system wipes!




(The following is for advanced users - a more descriptive approach can be found in the link provided above)
STEP 1:

  • - In the Registry Editor, navigate to:
    HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Policies\Google\Chrome
  • - Delete 'ExtensionInstallForcelist' or delete all the data within.


STEP 2:

  • - Go to chrome://extensions/ and check 'Developer mode'
  • - Search for all extensions that contain the line "Installed by enterprise policy." Save their name and id
    (In my case, there was only one extension with the following information, NAME: YTBBloCkeaRApp ID meajhoiolabnglbkdopdglmmphjckgfp)
  • - Go to chrome://policy/ and click 'Show value'
  • - Next to the ID should be a path to the malicious content. Navigate to that folder destination and remove the respective content.


STEP 3:

  • Go to %AppData%\Local\Google\Chrome\User Data\Default\Extensions
  • Delete the folder with the given ID


STEP 4:

  • Go to C:\Windows\System32\GroupPolicy
  • Delete 'Machine' and 'User' folders (following this step, Chrome will notify that the unwanted extension has been uninstalled)


BONUS:

  • Re-scan to ensure all suspecting evidence is removed.




Back Story:
    When browsing the web, I ensure that I have the following Chrome extensions installed and running: HTTPSEverywhere, AdBlocker, ScriptSafe (or no-script), and Blacklist. ScriptSafe has been a wonderful extension that allows me to be selective in what runs under the hood and what doesn't - it's like disabling JavaScript but only for some websites - saving me the worry of malicious scripts installing bad subjects on my system. One of the only downsides to this is that most sites use third-party scripts to help their website function the way they intended it. Some of the most common third-party scripts is jquery.min.js, three.js, and node.js which are like JavaScript libraries that save a developer a lot of time programming and designing.
    About month or two ago, when attempting to reestablish functionality to a certain site, I mistakenly enabled an unfamiliar script. It fit right in with the others... I later found my self with about 3 trojans and 12 other objects. Malwarebytes, SpyBot, and CCleaner did what they could, but my browser's settings where altered - a new homepage, a new tabpage. Most of this was resolved with a quick reset, nevertheless, I noticed ScriptSafe displayed traces of www.superfish.com, api.jollywallet.com, and istatic.datafastguru.info. A colleague pointed me to the Chrome Extensions; sure enough there was an unauthorized extension. Removing it solved the problem - for a time.

Friday, April 4, 2014

Explorer DVL Wiring

It is unfortunate how much time has past trying to make this device work, and kind of sad to realize how easy it was in the end, but after a few phone calls and several strait hours of effort, our team finally managed to pull data from our Explorer Doppler Velocity Log from RD Instruments:


The problem was the wiring from sensor to computer. The documentation also was a little vague - indeed, a pinout table can be found on page 12 of the manual, but there are no further details about what the pins actually do like an integrated circuits' manuals would. Here are the pins and their values:
  • 1: RX+  Messages received by the DVL Unit it's self.
  • 2: RX-  (Differential pair - RS422)
  • 3: TX+  Messages transmitted by the DVL Unit it's self.
  • 4: TX-  (Differential pair - RS422)
  • 5: COMM 1_2  0 = Com 1 (DVL Unit); 1 = Com 2;
If additional sensors and other devices were to be integrated with this as a full system, these pins would be used. (Our team never had a need as all other sensors were driven with real-time units)
  •  6: RX2+;   7: RX2- (Diff. Pair);
  •  8: TX2+;   9: TX2- (Diff. Pair);
  • 11: RX3+;  12: RX3- (Diff. Pair);
  • 13: TX3+;  14: TX3- (Diff. Pair);
  • 15: COMM 3_4:  0 = Com 3; 1 = Com 4;
  • 16: RX4+;  17: RX4- (Diff. Pair);
  • 18: TX4+;  19: TX4- (Diff. Pair);
In some cases, a vehicle may contain more than one DVL and one cannot ping at the same time another does, otherwise things would end up in complete chaos. So, if one wishes to use say 4 at the same time, they would need a way to synchronize the pings. This can be done with the following pins: (NOTE: the following values are out of speculation, nevertheless their function has been confirmed by the engineers at RDI)
  • 21: TRIG_IN   0 = wait; 1 = ping;
  • 23: TRIG_OUT  0 = busy; 1 = finished;
And the various other pins that are good to have:
  • 33: CHAS_GRND  This would attach to the vehicle chassis
  • 34/35: VDC+  12-24V 3A MAX
  • 36/37: VDC-  0V 0A
Now for the solution to our original problem - how to wire the device to a computer - for that I drew up a valuable schematic:

DB37-1 is connected to DB9-3: RX goes to TX
DB37-3 is connected to DB9-2: TX goes to RX
DB37-5 is connected to DB9-5: COMM 1_2 goes to GND

(I want to note that I happened to mix up pins 1 and 3 causing me grief for several hours. I didn't catch this silly mistake until I used an Arduino Uno to intercept the serial messages. The Arduino was reading data on the TX line but what's funny is that the data it was reading, originally came from the computer and not the DVL, Oops. In the end I ended up with TX to TX and RX to RX - that surely is not going to work, switch 'em around!)

Next, for up-to-date systems, a simple RS232 to USB adapter can be used. RDI recommended us the EasySYNC ES-U-1001, though I've tried another brand and the result was still the same.

With BBTalk (or the Arduino COM port), one can send and receive messages to the DVL.
Project: Success!