Ubuntu - Geek went Freak!

Ubuntu

Ubuntu: Cross-compile baremetal Cortex-M assembly program

In this post, we will cross-compile a small baremetal program for ARM processor on an Ubuntu machine.

ARM cross-compile toolchain

First step is to install the ARM cross-compiler toolchain. Luckily Ubuntu already has it in its software repository. Execute the following command in the terminal to install ARM EABI compatible tool chain:

sudo apt install gcc-arm-none-eabi

Check the version of the installed compiler using the following command:

arm-none-eabi-gcc --version

Sample baremetal program

Now, we need a sample baremetal program to compile. I have choosen a very simple assembly program.

startup.S

.global _start
_start:
  B _reset /* Reset */
  B . /* Undefined */
  B . /* SWI */
  B . /* Prefetch Abort */
  B . /* Data Abort */
  B . /* reserved */
  B . /* IRQ */
  B . /* FIQ */

_reset:
  mov r1, #10
  ldr r0, =0x20000000
  str r1, [r0]
  ldr r2, [r0]
  B .

Assemble

Lets assemble the assembly file using GCC assembler.

arm-none-eabi-as -mcpu=cortex-m3 -g startup.S -o startup.out

Link

Finally lets link the object file startup.out generated by the assembler.

arm-none-eabi-ld -Ttext=0x0 -o startup.elf startup.out

Note: Since the program is very simple, I haven’t used any linker script here.

-Ttext=0x0 option instructs the linker to use 0x0 as the starting address of the instructions.

Ubuntu: Emulate baremetal Cortex-M program

In this post, we will emulate a baremetal program for Cortex-M on Ubuntu PC.

Installation

We will need

  1. QEMU emulator for ARM
  2. GDB

Fortunately both of them are available through Ubuntu software repository.

Install them using the following command:

sudo apt install qemu-system-arm
sudo apt install gdb-arm-none-eabi

Emulation

We will use QEMU for emulation. GDB is used to control and inspect QEMU.

Launch QEMU

qemu-system-arm -monitor stdio -machine lm3s811evb -cpu cortex-m3 -s -S -kernel startup.elf
  • -monitor stdio
    Access QEMU HMI monitor from terminal
  • -machine lm3s811evb -cpu cortex-m3
    Select machine lm3s811evb and CPU cortex-m3
  • -s
    Start GDB server on localhost:1234
  • -S
    Don’t start execution. This is used so we can start and control execution from GDB
  • -kernel startup.elf
    The executable file to execute

Launch GDB client

arm-none-eabi-gdb startup.elf

You should now be in GDB interactive console.

Connect to QEMU

Lets connect to GDB server hosted by QEMU from the GDB client

target remote localhost:1234
load

Run the program

continue

Inspect

Press <Ctrl-c> to stop execution.

Check registers

In lines 13, 14 and 16, we update registers r1, r0 and r2 respectively. They should hold values 0x20000000, 10 and 10 respectively.

info reg r0 r1 r2

Should print:

r0 0x20000000 536870912
r1 0xa 10
r2 0xa 10

Check memory

We write value 10 to memory address 0x20000000. Lets check if that worked correctly:

x/4wx 0x20000000

0x20000000: 0x0000000a 0x00000000 0x00000000 0x00000000

Crack and install Modelsim on linux

Note: Piracy is crime. Please don’t use or encourage pirated software. If you would like to evaluate Modelsim, try the evaluation version. If you still haven’t changed your mind, read on :P.

1) Install the required dependencies

2) Download modelsim 10.1c for linux and its corresponding crack.

3) Mount the downloaded modelsim iso and install modelsim.

mkdir /tmp/modelsim/
mount -o loop path-to-iso /tmp/modelsim
cd /tmp/modelsim
./install.linux

Note:Install modelsim under ~/modelsim/ directory. If you choose another location, please make sure you exchange the default location with your preferred location in the steps below.

4) Execute this script to crack modelsim

Comment the first two lines in the generated license.dat

#SERVER xxxx xxxxxxxxxxxx 27001
#VENDOR mgcld D:FEATURE mgc_s mgcld 2020.00 1-jan-2021 999 0 TS_OK

5) Fix libfreetype problem (Only required if you face this problem) Try to start vsim

cd ~/modelsim/modeltech/linux_x86_64/
./vsim

If vsim reports the following error, your distro’s freetype library doesn’t play well with modelsim.

Fatal: Read failure in vlm process (0,0)
Segmentation fault (core dumped)

Use the following script to compile custom freetype

6) Modelsim is ready to use! One last step, add these commands to ~/.bashrc to avoid executing every time you start a new terminal

Linux and Modelsim!!!! Ain’t it fun?

Ubuntu: Compiling ffmpeg from source

Ubuntu uses avconv instead of ffmpeg. It also doesn’t have a package for libswresample. This can be problematic at times. So I resorted to installing ffmpeg from source. You need the following packages to compile ffmpeg from source.

sudo apt-get install yasm pkg-config

Lets clone the ffmpeg repository and compile it.

git clone https://github.com/FFmpeg/FFmpeg.git FFmpeg
cd FFmpeg
./configure --prefix=/usr/local/ffmpeg --enable-shared
make
sudo make install

After the above step successfully completes, ffmpeg should be installed in /usr/local/ffmpeg directory. Since we have installed ffmpeg in non standard location, we have to set PKG_CONFIG_PATH and LD_LIBRARY_PATH correctly. Add these lines at the end of your ~/.bashrc.

export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/ffmpeg/lib/pkgconfig/
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/ffmpeg/lib/

Install ffmpeg/avconv dev packages in Ubuntu

In Debian and its derivatives, the dev package for ffmpeg/avconv is split into several sub packages:

  • libavdevice-dev
  • libavformat-dev
  • libavfilter-dev
  • libavcodec-dev
  • libswscale-dev
  • libavutil-dev

This command will install all of them:

sudo apt-get install libavdevice-dev libavformat-dev libavfilter-dev libavcodec-dev libswscale-dev libavutil-dev

Firefox: play MP4, MOV, H.264 videos on Linux(Ubuntu, Mint, Debian)

Firefox doesn’t support MP4, MOV and H.264 videos on Linux. This is because these formats are protected by patents and hence incur fee. In linux, like always, you can circumvent this and install the necessary plugins to play these video formats.

Install the necessary plugins and video decoders

sudo apt-get install ubuntu-restricted-extras

Request Firefox to use gStreamer for video decoding

In Firefox, access the following page: about:config. You will be presented with a list of flags. In the search box type: media.gstreamer.enabled. Enable the flag by double clicking it.

Testing that H.264 format works

Access the following page to see if H.264 is enabled: Youtube HTML5 video test You can test some test videos from this site: HTML5 test videos

mount virtualbox image( vdi ) on linux

I was using windows on virtual box. Once i had to shutdown the system abruptly. The next time i booted, i got this famous “blue screen of death”. All my precious data were inside the virtual box image. Here is my journey to retrieve the data hidden in virtual box’s vdi format.

First, backup your vdi file. If virtual box is installed in your system, you must have a command called VBoxManage. Lets use it to convert our vdi image into raw image, which can later be mount.

VBoxManage internalcommands converttoraw your_backup_vdi.vdi recover.raw

Substitute the backup vdi file in the above command. The raw image created is recover.raw. Now we have to mount it in order to access the files in it. Execute the following command to mount your raw image,

losetup /dev/loop1 recover.raw

Now query the partition information from fdisk,

fdisk -lu /dev/loop1
Disk /dev/loop7: 44.1 GB, 44108349440 bytes
255 heads, 63 sectors/track, 5362 cylinders, total 86149120 sectors
Units = sectors of 1 * 512 = 512 bytes

Device Boot Start End Blocks Id System
/dev/loop7p1 * 16065 43070264 21527100 7 HPFS/NTFS
/dev/loop7p2 43070265 86140529 21535132+ 7 HPFS/NTFS

In my case, I had two partitions C and D. To mount one of these partitions, You need the exact address where the partition starts. You can get the exact address by multiplying start sector of the partition with 512(Since a partition is 512 bytes each). So in my case, the first partition starts at 16065 * 512. Now let us mount our partition,

losetup /dev/loop2 recover.raw -o $((16065 * 512))
mount /dev/loop2 /mnt
cd /mnt

Bingo! You have recovered your files.

Note:
If the filesystem type of your partition is ntfs, you have to install fuse and ntfs-3g tools to be able to mount your partition.

Alternative methods: 1) the easiest method is using nbd module and qemu-nbd program. But i couldn’t find those packages in my company’s rpm repository.