Saturday, December 31, 2016

Learning linux driver development with qemu


Linux Drivers

PCI

Based on instructions here -
http://nairobi-embedded.org/linux_pci_device_driver.html

Build yocto guest OS

$ git clone git://git.yoctoproject.org/poky
poky]$ git checkout morty
poky]$ source oe-init-build-env

#for those of us with non-US keyboards (you can also skip this step if using 'nographic' mode for qemu
build]$ echo "IMAGE_INSTALL_append = \" kbd keymaps kbd-keymaps\"" >> conf/local.conf

#this will take a while
build]$ bitbake core-image-sato-sdk

#now run it in qemu
build]$ runqemu qemux86-64 core-image-sato-sdk ext4 nographic qemuparams="-device ivshmem,shm=ivshmem,size=1"

#change keyboard layout if needed
root@qemux86-64:~# loadkeys /usr/share/keymaps/i386/qwerty/sv-latin1.map.gz 

#build guest module
#copy code to guest
[host pci]$ scp -r guest/ root@192.168.7.2:~/

ssh root@192.168.7.2
root@qemux86-64:~# cd /usr/src/kernel
root@qemux86-64:/usr/src/kernel# make modules_prepare

root@qemux86-64:/usr/src/kernel# cd ~/guest
root@qemux86-64:guest # make
root@qemux86-64:~/guest# sudo insmod ne_ivshmem_ldd_basic.ko
root@qemux86-64:~/guest# ls -l /dev/ivshmem0
root@qemux86-64:~/guest# sudo ./ne_ivshmem_shm_guest_usr -w "Dunia, vipi?"
main:169:: writing "Dunia, vipi?"

[host pci]$ hexdump -C /dev/shm/ivshmem
00000000  44 75 6e 69 61 2c 20 76  69 70 69 3f 00 00 00 00  |Dunia, vipi?....|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|


User mode
Now for user-mode, based on instructions here

Sunday, October 9, 2016

Fritzing on Fedora 24

Fritzing fails to start as below, when run on Fedora 24.
See also - https://github.com/fritzing/fritzing-app/issues/3245

tar xvf Downloads/fritzing-0.9.3b.linux.AMD64.tar.bz2 
cd ~/fritzing-0.9.3b.linux.AMD64
./install_fritzing.sh
./Fritzing
fritzing-0.9.3b.linux.AMD64/lib/Fritzing: error while loading shared libraries: libssl.so.1.0.0: cannot open shared object file: No such file or directory

This can be resolved by replacing libgit2 as below -

cd lib
rm libgit*
ln -s /usr/lib64/libgit2.so.24 libgit2.so.23 
cd ~/fritzing-0.9.3b.linux.AMD64
./Fritzing

Friday, March 11, 2016

Enlarging an ext2 partition (and filesystem) without data loss on Centos 6

I have tested the procedure using qemu-kvm.

qemu-kvm -boot once=d -cdrom CentOS-6.7-i386-LiveCD.iso -hda centos_2G.img -m 2G

Using a live CD image, to do the resizing, and a USB flash disk image as the test subject.

The general procedure described for doing this is as below -

Check that filesystem is OK
e2fsck /dev/sda1

Use fdisk to delete and recreate the partition, in place, but with increased size
fdisk /dev/sda
p - to show table, note details
d - to delete partition
n - to create new partition, using details from before, increasing the end block as needed
a - to toggle boot flag
w - to write partition table back to disk

Re-check that filesystem is OK
e2fsck /dev/sda1

At this point the process fails for me, with the error -
Superblock invalid
Bad magic number in super-block

and from here I have not found a way to recover the filesystem.
I have tried a similar process using parted, and also failed.

Using gdisk (GPT) instead of fdisk

[root@livecd ~]# e2fsck /dev/sda1
e2fsck 1.41.12 (17-May-2010)
/dev/sda1: clean, 18823/65536 files, 209783/261888 blocks

[root@livecd ~]# yum install gdisk
[root@livecd ~]# gdisk /dev/sda
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: not present


***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************

Command (? for help): p
Disk /dev/sda: 3913728 sectors, 1.9 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): F2EA2B62-4D1C-4D0A-8274-24009DC5353D
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 3913694
Partitions will be aligned on 2048-sector boundaries
Total free space is 1818557 sectors (888.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         2097151   1023.0 MiB  8300  Linux filesystem

Command (? for help): d
Using 1

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-3913694, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-3913694, default = 3913694) or {+-}size{KMGTP}: 1700M
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): x

Expert command (? for help): a
Using 1
Known attributes are:
0: system partition
1: hide from EFI
2: legacy BIOS bootable
60: read-only
62: hidden
63: do not automount

Attribute value is 0000000000000000. Set fields are:
  No fields set

Toggle which attribute field (0-63, 64 or to exit): 2
Have enabled the 'legacy BIOS bootable' attribute.
Attribute value is 0000000000000004. Set fields are:
2 (legacy BIOS bootable)

Toggle which attribute field (0-63, 64 or to exit): 

Expert command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sda.
The operation has completed successfully.
[root@livecd ~]# e2fsck -f /dev/sda1
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 3A: Optimizing directories
Pass 4: Checking reference counts
Pass 5: Checking group summary information

/dev/sda1: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sda1: 18823/65536 files (0.7% non-contiguous), 209783/261888 blocks


[root@livecd ~]# resize2fs /dev/sda1
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/sda1 to 434944 (4k) blocks.
The filesystem on /dev/sda1 is now 434944 blocks long.

[root@livecd ~]# e2fsck /dev/sda1
e2fsck 1.41.12 (17-May-2010)
/dev/sda1: clean, 18823/114688 files, 212932/434944 blocks

All good, now reboot, and see that the image boots successfully.

[root@usbhda ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       1.7G  804M  783M  51% /
tmpfs           947M     0  947M   0% /dev/shm


Sunday, February 28, 2016

Irulu U2 malware removal


Use usb debugging to 'backup' the malware
adb pull /system/app/

Download original firmware

wget http://img.irulu.com/driver/U2_Q500H_V101En20141223.rar
unrar e U2_Q500H_V101En20141223.rar
$ file system.img
system.img: Android sparse image, version: 1.0, Total of 256000 4096-byte output blocks in 1684 input chunks.

Now to check if the original firmware is clean

Get simg2img tool
git clone https://github.com/anestisb/android-simg2img.git
make

Mount the system image

simg2img system.img system.raw.img
$ file system.raw.img
system.raw.img: Linux rev 1.0 ext4 filesystem data, UUID=57f8f4bc-abf4-655f-bf67-946fc0f9f25b (needs journal recovery) (extents) (large files)
sudo mount -o loop system.raw.img /mnt/loop/

Compare with that found on the Ali Express phone reveals



diff sys_app_mal.ls sys_app_irulu.ls  
1d0
< abc.apk
24d22
< cd89920009
29d26
< com.andr0id.lauchinmg.apk
31,43d27
< com.as.youtube.downloader.a5.apk
< com.chaidongqiang.beautyvideo.apk
< com.example.homeof01111beauty.apk
< com.example.homeof01192beauty.apk
< com.free.all.mp3.music.a6.apk
< com.free.all.mptree.music.b.apk
< com.haodu.sexyhotvideo.apk
< com.json.lutu.apk
< com.sailer.coolbrowser.apk
< com.sms.server.socialgraphop.db
< com.swiping.whale.apk
< com.system.cap.hm.hupdater.apk
< com.system.update.apk
45,46d28
< CustomICON.apk
< CustomICON.odex
75c57
< frozenkeyboard.apk
---
> Frozen_Keyboard.apk
82d63
< GloablBCServiceInfo.apk
89d69
< hll_videoplayer_pop.apk
92a73
> HTMLViewer.odex
125d105
< obs.apk
130d109
< OPBKEY_4c16012fb8540b4619b866cb3dde30aecf5f
141c120
< playApp.apk
---
> playApp_0830.apk
165d143
< system.bin.apk
178d155
< v5_function_video.mp4
180a158
> videoplayer.apk

So, the original firmware looks OK - now, to install it

I was unable to unlock the fastboot, so I used this procedure instead -


Download SP Flash Tool for Linux from - 
(or elsewhere. Note that the Windows version I grabbed, included Malware)

Uninstall ModemManager

sudo yum remove ModemManager
sudo udevadm control --reload

Power off the phone
Start the tool
sudo SP_Flash_Tool_v5.1548_Linux/flash_tool.sh

On the Download tab, select the scatter file from the extracted Irulu file (MT6582_Android_scatter.txt)

Click download

Now, power on the phone

Download should progress and show OK

Disconnect and power cycle the phone, and all should be good!

Friday, February 5, 2016

DisplayLink on Fedora

Here's a surprising story of a piece of unusual hardware working with less effort on Linux, than on Windows.

I bought one of these of ebay -

DELTACO USB 2.0 till DVI/HDMI/VGA-adapter

Planning to use it on a custom x86 board I have, that doesn't have a graphics adapter. First trying it on Windows 7 - where it worked fine, after downloading and installing the drivers.

I then booted Linux, expecting to again download drivers, and likely need to compile them, with the usual dependency nightmares...

However, on boot, the display came up by itself :)


[   16.823553] udl 5-2:1.0: fb1: udldrmfb frame buffer device
[   16.823559] [drm] Supports vblank timestamp caching Rev 2 (21.10.2013).
[   16.823561] [drm] No driver support for vblank timestamp query.
[   16.823563] [drm] Initialized udl on minor 1
[   16.823599] usbcore: registered new interface driver udl

Bus 005 Device 002: ID 17e9:0198 DisplayLink