Plattan i Mattan

/dev/null

Wednesday, February 19, 2014

Adding Bluetooth as console connection on Raspberry Pi

Hacking Raspberry Pi is fun. But as with all hacking activities it sometimes goes wrong — lost IP address, wireless LAN do not connect anymore, disks that are offline, etc. When it goes wrong it is always good to have access to the Linux console. Diagnose and to restore the system to a known state is much simpler with such access. But hacking and building Pi's sometimes leads to Pi's located in hard to reach places such as the roof of the garage, the attic, or deep in the basement. Places where attaching a serial console is hard or even sometimes impossible. For those scenarios it would be perfect if the Linux console always is accessible and preferably so wirelessly. A wireless cable replacement technology that replaces a serial cable, and is supported by standard computer hardware, would be perfect for this task.

Enter Bluetooth SPP

There is one technology that fulfill all of the requirements above and more and that is Bluetooth Classic and the Bluetooth Serial Port Profile. Bluetooth Classic and the serial port profile is supported by most modern PC and hardware making it easy to operate. Besides being widely supported, it also has the stability and range needed for a reliable console connection. A Bluetooth Classic module of today has an operational range of up to 300 meters and with a speed of ~1Mbps. Very useful when the system is located on the roof top!

Adding a wireless connection

To make the console wireless a ready-to-use Bluetooth Classic device with the serial port profile is needed. In this guide the connectBlue cB-OBS419i serial port adapter is used. It is a ready-to-use Classic Bluetooth serial port module with a simple configuration interface.
The cB-OBS419i can be found on e.g. DigiKey.

Note; Serial console connectivity can be done using a Bluetooth USB dongle and some Bluez configuration. However, by using a serial port module you separate Linux config/setup/hacking from the default console and thus minimizing the risk of not reaching the console when all is lost.

Initial configuration

The default serial configuration of connectBlue's cB-OBS419i is 57600 baud and hardware flow control on. This config do not match the Raspberry Pi's console settings at all so some reconfiguration is needed. To change settings the Bluetooth module needs to be made accessible to a computer. This is easiest by using the connectBlue USB module adapter cB-ACC-34. An alternative is to use the FTDI TTL-232R-3V3 cable.

Once attached to a PC, Mac, or other hardware, connect to the module using a terminal application such as minicom or TeraTerm and issue the escape sequence '///' to enter command mode. 

First the serial baud rate needs to be increased to 115200 baud and flow control needs to be shut off. Both are changed using one single command called AT*AMRS. (All of the configuration commands are documented in the connectBlue Bluetooth Serial Port Adapter AT Commands.) 

Before changing the serial setting it is wise to reset all settings to factory defaults. Use the AT&F command to reset all settings to factory defaults.

AT&F
OK

Now, change serial baud rate and turn off flow control.

AT*AMRS=9,1,1,1,2,0,1
OK

Finally, set a more descriptive name so the console port is easier to find.

AT*AGLN="Raspberry Pi Console",1

All settings are stored in non-volatile memory so this configuration is not needed to be done again.

Connect to Raspberry Pi console pins

The Raspberry Pi console interface is located on the GPIO pin header P1, pin 8 (Tx) and 10 (Rx). These two pins needs to be wired to the Rx and Tx pins of the Bluetooth module in a crossed manner. That is, Rx on the Pi connects to Tx on OBS419i and vice versa. Besides Rx and Tx, power (VCC) and GND are needed for operating the Bluetooth module. The connectBlue module is capable of handling VCC input up to 6VDC meaning that the module can be powered directly from the +5VDC pin. 

cB-OBS419i has several host interface alternatives. For this guide host interface J6 is used. 4 cables are directly soldered on OBS419i J6 connector. Pins used are J6-3 (GND), J6-4 (VCC), J6-11 (Tx), and J6-13 (Rx).

Connection list:

P1-4 (+5VDC) ->  J6-4  (VCC)
P1-6 (GND)   ->  J6-3  (GND)
P-8  (Tx)    ->  J6-13 (Rx)
P-10 (Rx)    ->  J6-11 (Tx)

See the cB-OBS419 Electrical Mechanical Data Sheet for details about pin out etc.


The Raspberry Pi Bluetooth Linux console adapter is now ready for use.

Test

Connect the 4x1 header to the Raspberry Pi GPIO pin list. Power on and the Bluetooth console connection will be ready for a connection. Scan for the device and use your Bluetooth connection manager to create a virtual serial port. Below shows how it look like on a Mac.



The Bluetooth Manager on Mac will automatically create a TTY in /dev/ that can be used to initiate a connection. The name is derived from Bluetooth service name. Simply call e.g. minicom with the TTY as an argument like this:


Now the console is connected and ready to use. Login using your ordinary credentials. 


Finally

Do not forget to change the default Raspberry Pi password. Adding a wireless connectivity will make it easy for others to connect too. Best is to enable Bluetooth link security but this is out of scope for this short guide. The Bluetooth Serial Port Adapter Security document is a good read on Bluetooth security and how to enable it.

Thursday, January 31, 2013

MacPorts broken after OS X upgrade

XCode command line tools will be removed when upgrading Mac OS X to Mountain Lion. This causes the port command in MacPorts to exit and return the following error message:
Unable to execute port: can't read "build.cmd": Failed to locate 'make' in path: '/opt/local/bin:/opt/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin'
To re-install command line tools open Xcode, Select menu "Xcode->Preferences", go to tab "Downloads" and click on the Install button to download and install the command line tools. When installation is complete, port command will work as expected.

Note; an upgrade of Mac OS X will also remove the X11 package. This needs to be re-installed too. Navigate to xquartz.macosforge.org to find a download of X11.


Sunday, May 27, 2012

Recovering Lost Mail Accounts in OS X

My Mac panicked and crashed and when I started it up again all of my mail accounts were gone. And when Mail started it presented the initial "setup up a new account" wizard.

Looking into the Mail archive, ~/Library/Mail, the mailboxes seemed intact. There were files there and Spotlight was capable of both displaying and listing my mails. But in the "MailData" directory the Accounts.plist file had lost all accounts. After recovering Accounts.plist from a Time Machine backup, Mail started ok and all mails and accounts were intact.

To recover Accounts.plist do the following:
  1. Use Finder to navigate to ~/Library/Mail/V2/MailData.
  2. Ensure "MailData" is selected.
  3. Enter Time Machine and move back in time to a date/time you know Mail worked OK.
  4. Recover "Accounts.plist".
  5. Exit Time Machine.
  6. Start Mail.
Note; I use Mail v5.2.

Friday, June 4, 2010

Slow Linux console window in VirtualBox?

This is probably due to poor 2D acceleration. Per default Ubuntu uses the framebuffer for console and this is badly emulated on VirtualBox/MAC. To speed-up the console disable/blacklist the VGA frame buffer in /etc/modprobe.d/blacklist-framebuffer.conf. To do this just add the following line: "blacklist vga16fb" and then reboot. Voila! the console should now be fine.

Thursday, April 15, 2010

Cross compiling Linux on MAC OS X

This is a quick guide how to compile Linux kernels on Mac OSX.
First of all, get MacPorts (http://www.macports.org/) and install the following packages: git-core and libelf. These ports provide tools for download of Linux source code and libraries for handling of ELF files. (needed since Mac OSX uses Mach-O)
$ sudo port install libelf git-core

Then get Linux sources by cloning the linux source tree using git. Note: The volume used for Linux kernel compilation need to have a case sensitive file system. If this is not the case, create a new virtual volume with case sensitive fs on and mount it.
$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git linux-2.6

Change to the new directory linux-2.6 and update.
$ git pull

Install a cross compiler. In this post I use the cross arm-eabi- compiler created in the post below. MacPorts has an arm-elf- cross compiler that can be used if you do not want to compile one by yourself. Search for "arm-elf".

With the build infrastructure is in place it is time to configure a kernel. Start with coping a well known kernel configuration for your target to ”.config”. I use an ARM9 based development board that is close to KwikByte's KB9202 development board and thus I do the following:
$ cp arch/arm/configs/kb9202_defconfig .config

Try to build kernel. Don't forget to specify architecture and path to cross compiler.
$ make ARCH=arm CROSS_COMPILE=/opt/arm-none-eabi/bin/arm-none-eabi-

The build will fail due to missing "malloc.h" file. This header file is deprecated and not available on Mac. Replace all occurrences of this filename in all files in scripts/genksyms/ with "stdlib.h.".
$ sed -i .backup 's/malloc.h/stdlib.h/' *.c* 

If you try to build again it will still fail due to a missing elf.h file. A GNU alternative to this elf.h file can be found in /opt/local/include and is called "gelf.h". (installed by MacPorts libelf port.) Since the compiler has no search path set to the include directory of MacPorts per default it is required to alter ”HOSTCFLAGS” to include search path to MacPorts include dir. The missing file wasn't gelf.h but elf.h. Create a dummy ”elf.h” file that just includes ”gelf.h” + defines some missing ELF definitions. Like this:
/* ELF library in MAC ports is called gelf.h */
#include < gelf.h >

/* The following defines are needed to compensate for the following errors:
HOSTCC  scripts/mod/modpost.o
scripts/mod/modpost.c: In function ‘addend_386_rel’:
scripts/mod/modpost.c:1330: error: ‘R_386_32’ undeclared (first use in this function)
scripts/mod/modpost.c:1330: error: (Each undeclared identifier is reported only once
scripts/mod/modpost.c:1330: error: for each function it appears in.)
scripts/mod/modpost.c:1333: error: ‘R_386_PC32’ undeclared (first use in this function)
scripts/mod/modpost.c: In function ‘addend_arm_rel’:
scripts/mod/modpost.c:1348: error: ‘R_ARM_ABS32’ undeclared (first use in this function)
scripts/mod/modpost.c:1353: error: ‘R_ARM_PC24’ undeclared (first use in this function)
scripts/mod/modpost.c: In function ‘addend_mips_rel’:
scripts/mod/modpost.c:1371: error: ‘R_MIPS_HI16’ undeclared (first use in this function)
scripts/mod/modpost.c:1375: error: ‘R_MIPS_LO16’ undeclared (first use in this function)
scripts/mod/modpost.c:1378: error: ‘R_MIPS_26’ undeclared (first use in this function)
scripts/mod/modpost.c:1381: error: ‘R_MIPS_32’ undeclared (first use in this function)
make[2]: *** [scripts/mod/modpost.o] Error 1
make[1]: *** [scripts/mod] Error 2
*/

#define R_386_NONE        0
#define R_386_32          1
#define R_386_PC32        2
#define R_ARM_NONE        0
#define R_ARM_PC24        1
#define R_ARM_ABS32       2
#define R_MIPS_NONE       0
#define R_MIPS_16         1
#define R_MIPS_32         2
#define R_MIPS_REL32      3
#define R_MIPS_26         4
#define R_MIPS_HI16       5
#define R_MIPS_LO16       6

Add the path of this file to list of include directories. For this guide I put elf.h in the Linux top directory. The final build command now look like this:
$ make ARCH=arm CROSS_COMPILE=/opt/arm-none-eabi/bin/arm-none-eabi- HOSTCFLAGS="-I/opt/local/include/ -I."

Monday, March 22, 2010

ARM-Cortex cross compiler on MAC

Use MacPorts and add:
  • gmp (GNU multiple precision arithmetic library)
  • mpfr (C library for multiple-precision floating-point computations)
Download latest versions of gcc, binutils, and newlib. Note that GCC has to be of version 4.3 or newer to include support for newer Cortex CPU's.

$ cd binutils-2.20
$ mkdir build
$ cd build/
$ ../configure --target=arm-none-eabi --prefix=/opt/arm-none-eabi --enable-interwork --enable-multilib --with-gnu-as --with-gnu-ld --disable-nls --disable-werror
…
$ make -j2
…
$ sudo make install
…
$ cd ../..
$ PATH=/opt/arm-none-eabi/bin/:$PATH
$ cd gcc-4.4.3
$ mkdir build
$ cd build/
$ ../configure --with-libiconv-prefix=/opt/local --target=arm-none-eabi --prefix=/opt/arm-none-eabi --enable-interwork --enable-multilib --enable-languages="c" --with-newlib --without-headers --disable-shared --with-gnu-as --with-gnu-ld --with-gmp=/opt/local --with-mpfr=/opt/local --disable-werror
…
$ make -j2 all-gcc
…
$ sudo make install-gcc
…
$ cd ../..
$ cd newlib-1.18.0
$ mkdir build
$ cd build/
$ ../configure --target=arm-none-eabi --prefix=/opt/arm-none-eabi --enable-interwork --enable-multilib --with-gnu-as --with-gnu-ld --disable-nls
…
$ make -j2
…
$ sudo make install
…
$ cd ../..
$ cd gcc-4.4.3
$ cd build
$ ../configure --with-libiconv-prefix=/opt/local --target=arm-none-eabi --prefix=/opt/arm-none-eabi --enable-interwork --enable-multilib --enable-languages="c" --with-newlib --disable-shared --with-gnu-as --with-gnu-ld --with-gmp=/opt/local --with-mpfr=/opt/local --disable-werror
…
$ make
 -j2
…
$ sudo make install

…

Sunday, March 14, 2010

No valid VPN secrets


The VPN connection 'xxxxx' failed because there were no valid VPN secrets

You get this message if you have installed openvpn or vpnc in Ubuntu and haven't restarted the network manager. The error is due to GNOME keyring manager cannot find your password in the keyring due to an incomplete configuration. Just restart network manager and you are all set.

hl@braoha:~$ sudo aptitude install network-manager-vpnc
hl@braoha:~$ sudo /etc/init.d/network-manager restart