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 

Friday, October 2, 2015

TP-Link TL-WN821N v2 on Raspbian Jessie

Problem -

[  176.283254] usb 1-1.2: new high-speed USB device number 5 using dwc_otg
[  176.431063] usb 1-1.2: New USB device found, idVendor=0cf3, idProduct=1002
[  176.431089] usb 1-1.2: New USB device strings: Mfr=16, Product=32, SerialNumber=48
[  176.431107] usb 1-1.2: Product: USB2.0 WLAN
[  176.431123] usb 1-1.2: Manufacturer: ATHER
[  176.431139] usb 1-1.2: SerialNumber: 12345
[  176.633242] usb 1-1.2: reset high-speed USB device number 5 using dwc_otg
[  176.769865] usbcore: registered new interface driver carl9170
[  176.770955] usb 1-1.2: Direct firmware load for carl9170-1.fw failed with error -2
[  176.770980] usb 1-1.2: firmware not found.

pi@raspberrypi ~/Downloads $ lsusb
...
Bus 001 Device 009: ID 0cf3:1002 Atheros Communications, Inc. TP-Link TL-WN821N v2 802.11n [Atheros AR9170]
...

Solution

pi@raspberrypi ~/Downloads $ sudo aptitude install firmware-linux-free
The following NEW packages will be installed:
  firmware-linux-free
0 packages upgraded, 1 newly installed, 0 to remove and 3 not upgraded.
Need to get 19.1 kB of archives. After unpacking 90.1 kB will be used.
Get: 1 http://mirrordirector.raspbian.org/raspbian/ jessie/main firmware-linux-free all 3.3 [19.1 kB]
Fetched 19.1 kB in 0s (44.0 kB/s)            
Selecting previously unselected package firmware-linux-free.
(Reading database ... 116795 files and directories currently installed.)
Preparing to unpack .../firmware-linux-free_3.3_all.deb ...
Unpacking firmware-linux-free (3.3) ...
Setting up firmware-linux-free (3.3) ...
update-initramfs: deferring update (trigger activated)
Processing triggers for initramfs-tools (0.120) ...
                                       
Result

[14402.310944] usb 1-1.2: new high-speed USB device number 9 using dwc_otg
[14402.459068] usb 1-1.2: New USB device found, idVendor=0cf3, idProduct=1002
[14402.459096] usb 1-1.2: New USB device strings: Mfr=16, Product=32, SerialNumber=48
[14402.459114] usb 1-1.2: Product: USB2.0 WLAN
[14402.459129] usb 1-1.2: Manufacturer: ATHER
[14402.459145] usb 1-1.2: SerialNumber: 12345
[14402.560962] usb 1-1.2: reset high-speed USB device number 9 using dwc_otg
[14402.698503] usb 1-1.2: driver   API: 1.9.7 2012-12-15 [1-1]
[14402.698543] usb 1-1.2: firmware API: 1.9.6 2012-07-07
[14403.050326] ath: EEPROM regdomain: 0x809c
[14403.050351] ath: EEPROM indicates we should expect a country code
[14403.050365] ath: doing EEPROM country->regdmn map search
[14403.050377] ath: country maps to regdmn code: 0x52
[14403.050390] ath: Country alpha2 being used: CN
[14403.050401] ath: Regpair used: 0x52
[14403.080221] ieee80211 phy1: Selected rate control algorithm 'minstrel_ht'
[14403.084478] cfg80211: Calling CRDA to update world regulatory domain
[14403.099237] input: phy1 WPS Button as /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/ieee80211/phy1/input2
[14403.099912] usb 1-1.2: Atheros AR9170 is registered as 'phy1'
[14403.559455] IPv6: ADDRCONF(NETDEV_UP): wlan0: link is not ready

If necessary
Manually update wpa_supplicant as at the link below -
https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md

Oct  2 20:39:21 raspberrypi kernel: [  395.645848] wlan0: authenticate with 00:22:07:37:dd:a7
Oct  2 20:39:21 raspberrypi kernel: [  395.780519] wlan0: send auth to 00:22:07:37:dd:a7 (try 1/3)
Oct  2 20:39:21 raspberrypi kernel: [  395.780780] cfg80211: Calling CRDA to update world regulatory domain
Oct  2 20:39:21 raspberrypi kernel: [  395.782417] wlan0: authenticated
Oct  2 20:39:21 raspberrypi kernel: [  395.786584] wlan0: associate with 00:22:07:37:dd:a7 (try 1/3)
Oct  2 20:39:21 raspberrypi kernel: [  395.790141] wlan0: RX AssocResp from 00:22:07:37:dd:a7 (capab=0x411 status=0 aid=7)
Oct  2 20:39:21 raspberrypi kernel: [  395.799474] wlan0: associated
Oct  2 20:39:21 raspberrypi kernel: [  395.799637] IPv6: ADDRCONF(NETDEV_CHANGE): wlan0: link becomes ready


Tuesday, August 25, 2015

perf on fedora 21


sudo yum install perf



[steve@steve-GA-880GMA-UD2H perf]$ pgrep iperf
2807

[steve@steve-GA-880GMA-UD2H perf]$ perf record -p 2807 -g
^C[ perf record: Woken up 26 times to write data ]
[nf_nat_ipv4] with build id 08e53c9cb720761011036c0f3f3d7db2feb62bc3 not found, continuing without symbols
[nf_conntrack] with build id 6b2d193426fa4e8a93181f77d0622c1425d23af9 not found, continuing without symbols
[iptable_mangle] with build id 3f487227e3da17d0386b8a8b6976ade8311bc0fd not found, continuing without symbols
[nf_conntrack_ipv4] with build id d872b229572d3106b3b415d24a83679191cfd58c not found, continuing without symbols
[iptable_nat] with build id 4a354a31abf978aacb65d3e7c53e13f421c6bdb9 not found, continuing without symbols
[nf_defrag_ipv4] with build id ed79015881b828ed986f78ee7fa06bb62ecf52f3 not found, continuing without symbols
[nf_nat] with build id 9135bac40d6819086edde4816d82c495bbc5640f not found, continuing without symbols
[radeon] with build id 7417d27e130d2e4eecb5d45cc08fb74b4eb003a5 not found, continuing without symbols
[drm] with build id b840649b8536d349cd1525af9dac7892f4b4f5d7 not found, continuing without symbols
[ perf record: Captured and wrote 6.551 MB perf.data (37928 samples) ]


[steve@steve-GA-880GMA-UD2H perf]$ perf report --stdio -g


# Children      Self  Command  Shared Object        Symbol                                         
# ........  ........  .......  ...................  ...............................................
#
    98.75%     0.00%  iperf    libpthread-2.20.so   [.] 0xffff80c11eca95fd                         
              |
              ---0xf5fd
                 |          
                 |--77.16%-- malloc
                 |          0x1d5
                 |          
                 |--15.29%-- Server::Run
                 |          
                 |--6.44%-- 0
                  --1.11%-- [...]

    98.56%     0.08%  iperf    [kernel.kallsyms]    [k] system_call_fastpath                       
              |
              ---system_call_fastpath
                 |          
                 |--99.89%-- 0xf5fd
                 |          |          
                 |          |--78.04%-- malloc
                 |          |          0x1d5



can't get report/annotate to show file/line
maybe related to ?

OK...
I could get it to work by building iperf with
CXXFLAGS='-g3 -gdwarf-2' CFLAGS='-g3 -gdwarf-2'

then some work to understand how threads are represented