2009年12月8日 星期二

Electronic circuits

Pull-up resistor

Pull-up resistors are used in electronic logic circuits to ensure that inputs to logic systems settle at expected logic levels if external devices are disconnected.

tri-state (vt)

When outputs are tri-stated (in the Hi-Z state) their influence on the rest of the circuit is removed, and the circuit node will be "floating" if no other circuit element determines its state.

連結

PLL (phase-locked loop)

A PLL is a control system that generates a signal that has a fixed relation to the phase of a "reference" signal.

vim 啟用 syntax color (eg. for bash script)

1. 先試式能否在 vim 中動態切換 color:
按:進入command mode 試試
:syntax on
:syntax off

如果沒有顏色,可能副檔名沒有比對成功,手動設定 syntax filetype:
(例如使用 shell script 的 syntax)
:set syn=sh

2. 編輯 /etc/vim/vimrc
把 syntax on 註解打開
或是只想動到個人設定,只在~/.vimrc 加入"syntax on"

2009年12月5日 星期六

LPC bus (Low Pin Count)

What's LPC ? (from LPC bus in Plug-and-Play-HOWTO)
* a type of slow bus, used for slow "legacy" devices such as serial ports, floppy devices, etc.
* replace old ISA bus: LPC needs fewer pins, and uses a faster clock speed
* its bus for data/address and control is only 4 bits wide (so "low pin count")
* all LPC devices are on-board; there are no LPC slots
* a major chip on the LPC bus is the superio chip


2009年11月23日 星期一

Moschip linux driver for EMT PCI Express to port RS232 (serial)

Driver:
Moschip download
(need to register)

Patch for 2.6.28:
starex_2.6.28.patch.gz
(source:http://forums.linuxmint.com/viewtopic.php?f=49&t=29576/MCS9901_Linux/version1.6)

Usage:
@Drivers/MCS9901_Linux/version1.6 $ gzip -d starex_2.6.28.patch.gz
@Drivers/MCS9901_Linux/version1.6 $ ls
MCS9901_Linux_UM_Ver0.4.pdf starex starex_2.6.28.patch starex.tar.gz
@Drivers/MCS9901_Linux/version1.6 $ patch -p0 <>make
->sudo make install
->ls /dev/ttyD*
->lspci -v (After install)
03:00.0 Serial controller: NetMos Technology PCIe 9901 Multi-I/O Controller (prog-if 02)
Subsystem: Device a000:1000
Flags: bus master, fast devsel, latency 0, IRQ 17
I/O ports at ec00 [size=8]
Memory at febff000 (32-bit, non-prefetchable) [size=4K]
Memory at febfe000 (32-bit, non-prefetchable) [size=4K]
Capabilities: [80] Power Management version 3
Capabilities: [88] Message Signalled Interrupts: Mask- 64bit+ Queue=0/5 Enable-
Capabilities: [c0] Express Legacy Endpoint, MSI 00
Capabilities: [100] Power Budgeting
Capabilities: [200] Device Serial Number aa-bb-cc-dd-ee-ff-99-88
Kernel driver in use: starex-serial
Kernel modules: 9900-isa


You may need to modify Makefile before calling "make install":
(My OS is Ubuntu 9.04)
#ln -s /etc/init.d/mcs9900 /etc/rc.d/rc3.d/Smcs9900 || true
#ln -s /etc/init.d/mcs9900 /etc/rc.d/rc5.d/Smcs9900 || true
ln -s /etc/init.d/mcs9900 /etc/rc2.d/Smcs9900 || true
ln -s /etc/init.d/mcs9900 /etc/rc3.d/Smcs9900 || true
ln -s /etc/init.d/mcs9900 /etc/rc5.d/Smcs9900 || true

so that the driver is loaded when OS starts.

2009年11月17日 星期二

使用 od 觀察(dump) binary file

od -x -j 5 -N 10 file
->0000005 5478 1245 4eae 0842 4082
->0000017

-x: 以16進位印出, 兩個bytes為單位 (因此此例由小位址起dump值為 78 54 45 12 ae 4e ... 82 40)
-j: 跳過幾個bytes
-N: 輸出幾個bytes
file: 檔名

2009年11月10日 星期二

file nodes in /proc (relevant to driver)

看Major number
robert@robert-desktop ~/Test $ cat /proc/devices
Character devices:
1 mem
4 /dev/vc/0
4 tty
4 ttyS
...

看device node, Major number, minor number range
robert@robert-desktop ~/Test $ cat /proc/tty/drivers

/dev/tty /dev/tty 5 0 system:/dev/tty
/dev/console /dev/console 5 1 system:console
/dev/ptmx /dev/ptmx 5 2 system
/dev/vc/0 /dev/vc/0 4 0 system:vtmaster
rfcomm /dev/rfcomm 216 0-255 serial
serial /dev/ttyS 4 64-111 serial
...

看IO port
robert@robert-desktop ~/Test $ cat /proc/ioports
0000-001f : dma1
0020-0021 : pic1
0040-0043 : timer0
0050-0053 : timer1
0060-0060 : keyboard
0064-0064 : keyboard
...

看Memory Map IO
robert@robert-desktop ~/Test $ cat /proc/iomem
00000000-0000ffff : reserved
00010000-0009fbff : System RAM
0009fc00-0009ffff : reserved
000a0000-000bffff : Video RAM area
000c0000-000c7fff : Video ROM
000cc800-000cd7ff : Adapter ROM
...

看serial port tty* 的 IO port 和 IRQ
robert@robert-desktop ~/Test $ setserial -g /dev/tty*
/dev/ttyD0, UART: 16550A, Port: 0xec00, IRQ: 17
/dev/ttyS0, UART: unknown, Port: 0x03f8, IRQ: 4
/dev/ttyS1, UART: unknown, Port: 0x02f8, IRQ: 3
/dev/ttyS2, UART: unknown, Port: 0x03e8, IRQ: 4
/dev/ttyS3, UART: unknown, Port: 0x02e8, IRQ: 3

看 IRQ 次數
robert@robert-desktop ~/Test $ cat /proc/interrupts
CPU0 CPU1 CPU2 CPU3
0: 3855694 3885186 3836114 3824345 IO-APIC-edge timer
1: 0 1 0 1 IO-APIC-edge i8042
...

2009年10月26日 星期一

Use ntfs-clone to backup to / restore from an image

==== save image ======
robert@robert-desktop ~/Test $ sudo ntfsclone --save-image --output clonentfs.img /dev/sdb2
ntfsclone v2.0.0 (libntfs 10:0:0)
NTFS volume version: 3.1
Cluster size : 4096 bytes
Current volume size: 8044322816 bytes (8045 MB)
Current device size: 8044323840 bytes (8045 MB)
Scanning volume ...
100.00 percent completed
Accounting clusters ...
Space in use : 864 MB (10.7%)
Saving NTFS to image ...
100.00 percent completed
Syncing ...

====== restore =========
robert@robert-desktop ~/Test $ sudo ntfsclone --restore-image --overwrite /dev/sdb2 clonentfs.img
ntfsclone v2.0.0 (libntfs 10:0:0)
Ntfsclone image version: 10.0
Cluster size : 4096 bytes
Image volume size : 8044322816 bytes (8045 MB)
Image device size : 8044323840 bytes
Space in use : 864 MB (10.7%)
Offset to image data : 56 (0x38) bytes
Restoring NTFS from image ...
100.00 percent completed
Syncing ...

==== Error if you restore a smaller partition =====
robert@robert-desktop ~/Test $ sudo ntfsclone --restore-image --overwrite /dev/sdb2 clonentfs.img
ntfsclone v2.0.0 (libntfs 10:0:0)
ERROR: Output device is too small (3708230144) to fit the NTFS image (8044323328).

2009年10月20日 星期二

安裝與設定 apache 2.2 with WebDAV and AuthType=Digest

如果你要設定把Apache的AuthType設為Digest,要注意:
1. 你的Apache 版本,不同版本設定很不一樣: http://httpd.apache.org/docs/ 查mod_auth_digest
2. 非常tricky的地方(文件沒提到): AuthName 要設得跟 realm 一樣,不然會總是認證失敗!

=====

安裝:
sudo apt-get update
sudo apt-get install apache2 php5 libapache2-mod-php5 apache2-utils

設定
1) 啟用WebDAV
sudo a2enmod dav_fs
sudo a2enmod dav
sudo a2enmod dav_lock
sudo a2enmod auth_digest

2) 建立 WebDAV 使用者 (舉例:使用者 robertDAV)
sudo htdigest -c /etc/apache2/.digestpwd.dav /davshared/ robertdav
(Basic認證才用 htpasswd )
(如果有兩組以上user要設定,可能要手動填第二行喔!不然第二次使用htdigest會把第一組的資料"切掉")

3) 建立共享的 WebDAV 目錄
sudo mkdir /var/www/davshared/
sudo chown www-data:www-data /var/www/davshared/

4) 設定apache config 關於 WebDAV 目錄位置、權限等
vim /etc/apache2/sites-available/default
插入:

(請自動把以下的( )換成< >,因打< >在此秀不出來)
Alias /davshared/ "/var/www/davshared/"
(Directory "/var/www/davshared/")
       Options Indexes FollowSymLinks MultiViews
       AllowOverride None
       Order allow,deny
       allow from all
       #Allow from 192.168.0.0/255.0.0.0 ::1/128
(/Directory)
(Location /davshared/)
       DAV On
       AuthName "/davshared/"
       #AuthType Basic
       #AuthUserFile /etc/apache2/.passwd.dav
       AuthType Digest
       AuthDigestProvider file
       AuthDigestDomain /davshared/
       AuthUserFile /etc/apache2/.digestpwd.dav
       DavMinTimeout 600
       #(LimitExcept GET PUT HEAD OPTIONS POST)
       (LimitExcept GET HEAD OPTIONS)
               Require valid-user
       (/LimitExcept)
(/Location)



5) 重啟 apache
sudo /etc/init.d/apache2 restart

========================
Reference:
Setting up a WebDAV server
http://www.linuxquestions.org/questions/linux-server-73/digest-authentication-not-working-on-default-apache-installation-on-slackware-12.1-730785/
http://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html
http://samiux.wordpress.com/2009/06/27/howto-webdav-on-ubuntu-9-04-server/

2009年10月18日 星期日

認識 initrd 與 initramfs

查wiki的initrd有很好的說明
這裡提供最簡易的認知

Linux系統開機時,除了載入kernel,還需掛載根檔案系統(rootfs)
由於根檔案系統並不一定能很容易的被掛載,(例如rootfs不位在本機硬碟,需要使用其它工具才能從遠端掛載它;又例如你的rootfs在特殊的儲存裝置,需要先載入它的driver才能正常使用)。

為了提供足夠掛載真正 rootfs 能力,所以設計成兩階段掛載rootfs,先掛載容易取得的 initrd檔為rootfs,有了足夠的能力,再掛載真正的rootfs,這樣的設計就很有彈性。

當然initrd檔要放在系統很容易就能存取到的地方,例如kernel有支援的硬碟中的一個檔案。

initrd檔案有兩種:
1. (old) Ram disk image : (optionally gzip-compressed) file system image (例如一個 ext2 file system 的壓縮檔)
2. (new) Initramfs image: gzip-compressed cpio archive (直接將開機所需檔丟給 cpio,再經 gzip 壓縮)

kernel能自行判斷是哪一種 initrd 檔,成功解壓縮形成在 ram 中的 rootfs。如果是 Ram disk image,就會把原本的 file system (如ext2) 還原成 ram disk;如果是 initramfs image,則會使用它cpio中包含的檔案形成 tmpfs file system(或是較舊的 ramfs file system)

雖然也是在 ram 中運作,但 tmpfs 並不是 ram disk,ram disk 指的是把一般的 disk file system 放到 ram 中運作,而 tmpfs就是設計在ram中運作,並不是 disk file system,也比它有效率。


My note on creating initramfs:

To extract initramfs:
gunzip < /boot/initrd.img-2.6.28-11-generic | cpio -i -d -H newc --no-absolute-filenames

To create initramfs:
cd myinitrd/
find . | cpio -o -H newc | gzip > ../myinitrd.gz

Note 1: Makesure all files in myinitrd/ are belong to root (chown -R 0:0 *). Otherwise, init script may fail to mount proc and show you "permission denied (are you root )" errors.

Note 2: use gzip -9 ... may lead to kernel panic "invalid compressed format" when booting !! Guess it's because some vmlinuz doesn't support that compression level.


Reference:

2009年10月1日 星期四

Sed : remove empty lines, remove beginning spaces, tabs

Remove empty lines:
cat file|sed /^$/d

Remove beginning spaces:
cat file|sed s/^\ *//g

Remove beginning tabs:
cat file|sed s/^\\t*/d

(Remove=Replace with nothing)

2009年9月29日 星期二

2009年9月24日 星期四

Use syslinux to boot Windows in the second partition

If your Windows is in the 2nd partition, and syslinux is in the 1st partition,
you want to first boot to syslinux and then choose"Windows" item and go there, how do you do that?

1. Edit your windows C:\BOOT.ini
chang partition(1) to partition(2)

2. Edit your 1st partition's syslinux.cfg
LABEL Windows
MENU LABEL ^Windows
KERNEL chain.c32
APPEND hd0 2
TEXT HELP
Boot to Windows
ENDTEXT

The keypoint is "hd0 2". This is very very tricky!! Since we use "hd0 1" in Lilo and Grub. But we use "hd0 2" in syslinux.

2009年9月23日 星期三

How to build ntfs-3g (2009.11.14) ?

How to build ntfs-3g (2009.11.14) ?

If you make and have an error about secaudit.o

Then you need to install package libattr1-dev.

(Also it was not designed to be mandatory. See http://tuxera.com/forum/viewtopic.php?f=3&t=1151&p=5191&hilit=secaudit+secaudit&sid=94d2d635f4de0e2f0acb7962b6459aa0#p5191)

2009年9月17日 星期四

Bash kill a process with a specified name

kill `ps x | awk '$5=="process_name" {printf $1}'`

============

For example, to kill "epiphany-browser":
ps x
->23243 ? Sl 0:57 epiphany-browser

kill `ps x | awk '$5=="epiphany-browser" {printf $1}'`

,where
$1=23243 (pid)
$5=epiphany-browser

2009年9月16日 星期三

Bash string compare greater less

比較字串順序大小用 \> 及 \<,大於等於或小於等於要用 ! 配合來實作。

if [ $str \> $str2 ]; then
echo "greater"
fi

if [ ! $str \< $str2 ]; then
echo "greater or equal"
fi

實例應用:
g_host_os_ver=`uname -r`
if [ ! $g_host_os_ver \< "2.6.28" ]; then
ko=$(echo $ko|sed 's/^\///g') # remove the first /
fi

2009年8月14日 星期五

vi編輯完以su權限存檔 (:w !sudo tee %)

若以普通使用者身份用vi開檔編輯後想以root身份存檔,可以使用如下命令:

:w !sudo tee %
==> Press any key
==> Press L

這樣就存檔成功 可以離開vi了