Thursday, December 29, 2011

add a default route in Linux

Sometimes, even after cmd "dhclient eth0", the machine still cannot ping outside machines.

Then, I use cmd "route" to check the current route, and get sth like this:

Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 * 255.255.255.0 U 0 0 0 ra0

So there is no default gatway. Next I use the following command to add a default route:

"route add default gw 192.168.2.1 eth0"
(The IP of the gateway could be the current IP address and change the last number to 1.)

Ref:
http://www.cyberciti.biz/faq/linux-setup-default-gateway-with-route-command/

Tuesday, December 27, 2011

A complete guide to RPM and source RPM

Maximum RPM

http://www.rpm.org/max-rpm-snapshot/

List all the rpms, etc:

http://www.cyberciti.biz/faq/howto-list-installed-rpm-package/

Wednesday, December 21, 2011

Miscellaneous Character Drivers

http://www.linuxjournal.com/article/2920?page=0,0

Wednesday, December 14, 2011

Tuesday, December 13, 2011

git discard changes to a file


git checkout -f -- "filename"
Note, there is a space between -- and "filename".

Other methods:
git checkout -f

=========================================
To discard local commits:

git reset --hard origin/master

Tuesday, December 6, 2011

git send-email

http://morefedora.blogspot.com/2009/02/configuring-git-send-email-to-use-gmail.html

http://felipec.wordpress.com/2009/10/25/git-send-email-tricks/

bash commands shortcuts

http://www.gnu.org/software/bash/manual/bashref.html#Commands-For-History

yank-nth-arg (M-C-y)
alt+ctrl+y

Insert the first argument to the previous command (usually the second word on the previous line) at point. With an argument n, insert the nth word from the previous command (the words in the previous command begin with word 0). A negative argument inserts the nth word from the end of the previous command. Once the argument n is computed, the argument is extracted as if the ‘!n’ history expansion had been specified.

yank-last-arg (M-. or M-_)

alt+-
Insert last argument to the previous command (the last word of the previous history entry). With an argument, behave exactly like yank-nth-arg. Successive calls to yank-last-arg move back through the history list, inserting the last argument of each line in turn. The history expansion facilities are used to extract the last argument, as if the ‘!$’ history expansion had been specified.





Monday, December 5, 2011

__releases and __acquires macros in Linux kernel

Is used for sparse (a static analysis tool) to check the lock unlock mismatches.



http://lists.linux-foundation.org/pipermail/linux-pm/2006-May/008346.html


http://marc.info/?l=linux-sparse&m=109865107109043&w=2

use netcat to transfer files between two machines

netcat is a very useful small program to communicate between two machines. If you just want to simply upload a file from one machine to another one, without setting up ftp, ssh server, then you can use netcat (it has many other functions).


commands:

1) listen on a port:

nc -l -p 2222 > log.txt
2) send file:
cat /etc/passwd  nc 192.168.0.50 2222



Thursday, November 10, 2011

vi copy,paste, move cursor around braces

copy: yy
paste: p

move cursor to the corresponding braces: %

move cursor up down for one code block or function: [],{}



ref:



http://www.unix-manuals.com/refs/vi-ref/vi-ref.htm

Wednesday, November 2, 2011

Tuesday, November 1, 2011

Global variable in python

http://docs.python.org/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python

http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them

In short, when modify the global variable in a function, need to explicitly define "global g_sth". This is to avoid using global variables.

Python tutorial and start up script

tutorial:
start up script:

from optparse import OptionParser
import sys
def main():
   usage = "usage: %prog [options] arg"
   parser = OptionParser(usage)
   parser.add_option('-d', '--directory',
           action='store', dest='directory',
           default=None, help='specify directory')
   parser.add_option('-f', '--file',
           action='store', dest='filename',
           default=None, help='specify file')
   parser.add_option('-v', '--version',
                     action="store_true", dest="show_version",
                     default=False, help='displays the version number')
   if len(sys.argv) == 1:
       parser.print_help()
       sys.exit()
   (options, args) = parser.parse_args()
   # rest of program...
if __name__ == '__main__':
   main()

Monday, October 10, 2011

QEMU : change cdrom

http://en.wikibooks.org/wiki/QEMU/Monitor#change

(qemu) info block
ide0-hd0: type=hd removable=0 file=/path/to/winxp.img
ide0-hd1: type=hd removable=0 file=/path/to/pagefile.raw
ide1-hd1: type=hd removable=0 file=/path/to/testing_data.img
ide1-cd0: type=cdrom removable=1 locked=0 file=/dev/sr0 ro=1 drv=host_device
floppy0: type=floppy removable=1 locked=0 [not inserted]
sd0: type=floppy removable=1 locked=0 [not inserted]
(qemu) change ide1-cd0 /path/to/my.iso
(qemu) change ide1-cd0 /dev/sr0 host_device

Ubuntu network restart

sudo /etc/init.d/networking restart

pxe boot on KVM

Fedora:
http://forums.fedoraforum.org/archive/index.php/t-198552.html

Debin:
http://box.matto.nl/kvmpxe.html

Ubuntu Lucid:
http://mrpointy.wordpress.com/2010/05/03/pxe-boot-kvm-guests/

Wednesday, October 5, 2011

A brief introduction to Linux USB drivers (2)

This is the second part for the USB under Linux. The first part is here: http://jianggmulab.blogspot.com/2011/03/brief-introduction-to-linux-usb-drivers.html

Following are excerpts from LDD3.

Endpoints: the most basic form of USB communication. It has four types: control, interrupt, bulk, and isochronous.

Interfaces: may include many endpoints. Each interface handles one logical USB functions, such as a mouse, a keyboard or a audio stream.

Configurations: A USB device can have multiple configurations and switch between them. A single configuration can be enabled only at one point in time.

So to summarize, USB devices are quite complex and are made up of lots of different
logical units. The relationships among these units can be simply described as follows:
• Devices usually have one or more configurations.
• Configurations often have one or more interfaces.
• Interfaces usually have one or more settings.
• Interfaces have zero or more endpoints.

Monday, October 3, 2011

Monday, September 12, 2011

Wednesday, September 7, 2011

list only directories on Linux

cmd:
ls -d /*

ref:
http://embraceubuntu.com/2005/10/19/list-only-the-directories/
http://www.unix-manuals.com/forum/viewtopic.php?t=71

Monday, August 15, 2011

Monday, April 18, 2011

latex tips for big projects

http://cohomo.blogbus.com/logs/23129805.html

http://bbs.ctex.org/viewthread.php?tid=24600

auto wakeup after S3 sleep

On Linux

  • rtcwake -s 30 -m mem
-s means sleep seconds, -m means sleep type. Then the machine will sleep first and wait for 30 seconds and wake up.

On Windows



  • pwrtest /sleep /p:30
The result is the same as Linux.

Friday, April 15, 2011

Ftrace in Linux kernel

Thursday, March 31, 2011

execute windows command line in background

http://serverfault.com/questions/189988/how-i-can-execute-windows-command-line-in-background execute windows command line in background : start /B /B Start application without creating a new window. The application has ^C handling ignored. Unless the application enables ^C processing, ^Break is the only way to interrupt the application.

Sunday, March 27, 2011

Heap (data structure)

http://en.wikipedia.org/wiki/Heap_%28data_structure%29

From Wikipedia, the free encyclopedia
Jump to: navigation, search
Example of a complete binary max-heap

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if B is a child node of A, then key(A) ≥ key(B). This implies that an element with the greatest key is always in the root node, and so such a heap is sometimes called a max-heap. (Alternatively, if the comparison is reversed, the smallest element is always in the root node, which results in a min-heap.) There is no restriction as to how many children each node has in a heap. The heap is one maximally-efficient implementation of an abstract data type called a priority queue. Heaps are crucial in several efficient graph algorithms such as Dijkstra's algorithm.

Heaps are usually implemented in an array, and do not require pointers between elements.

The operations commonly performed with a heap are:

  • find-max or find-min: find the maximum item of a max-heap or a minimum item of a min-heap, respectively
  • delete-max or delete-min: removing the root node of a max- or min-heap, respectively
  • increase-key or decrease-key: updating a key within a max- or min-heap, respectively
  • insert: adding a new key to the heap
  • merge: joining two heaps to form a valid new heap containing all the elements of both.

Heaps are used in the sorting algorithm heapsort.


Master theorem

http://en.wikipedia.org/wiki/Master_theorem

Saturday, March 26, 2011

A brief introduction to Linux USB drivers (1)

USB devices are very popular these days, and I have been curious about USB drivers for a long time. Yesterday, finally, I read the book "Essential Linux Device Drivers", and here is a brief summary about the USB chapter on that book. Moreover, since I am more familiar with PCI devices, I will compare USB with PCI from time to time. The purpose of this article is to help me remember what I just read, and allow me to go back to review it if I meet USB again after a long time.

Unlike PCI, which has only one type of connector, USB has many connector types: type A, B, micro and OTG (on the go). Following are from wiki:

"The Standard-A type of USB plug is a flattened rectangle which inserts into a "downstream-port" receptacle on the USB host, or a hub, and carries both power and data. This plug is frequently seen on cables that are permanently attached to a device, such as one connecting a keyboard or mouse to the computer.

A Standard-B plug—which has a square shape with beveled exterior corners—typically plugs into an "upstream receptacle" on a device that uses a removable cable, e.g. a printer. "

USB mini and micor are for smaller devices, such as PDA.

USB OTG can be used as either host side or device side. In other words, it can be use as both type A and type B connectors.

In addition, there are also many proprietary connectors based on USB, such as those on cell phones. It is very interesting that USB has so many different connectors. I guess one reason is that it has to support many different devices with various sizes. This phenomenon also indicates that USB is a very successful standard.

A device driver for USB on Linux is similar to a PCI driver in some sense. You also need to register the device, allocate memory, fill out many data structures. One difference is that each USB device has an endpoint address in a private name space.

Another difference is that USB uses URB (USB request block) to configure and access the hardware. This is different from PCI, which you can directly read and write to the devices via IO port or IO memory.

USB devices support many classes, such as mass storage and HID (human interactive devices, e.g. keyboard, mouse). For mass storage, it uses SCSI commands to transfer data, which is also a surprise to me.

Thursday, March 24, 2011

关于User-Mode Driver Framework(UMDF)的一点感想

关于User-Mode Driver Framework(UMDF)的一点感想

最近又要在WINDOWS下面写点驱动,于是学习了下目前比较流行的WDF,主要是UMDF。因为以前用过一阵WDM,所以对KERNEL MODE的KMDF兴趣不是特别大。

WDF的全称是Windows Driver Foundation,其中又包括了UMDF和KMDF。前者是USER LEVEL的驱动,后者是KERNEL LEVEL。在WDF出来之前,流行的WINDOWS驱动结构叫做WDM (WINDOWS DRIVER MODEL)。在WDM之前,WIN NT和WIN 95的驱动模式是不一样的。WIN NT的比较规范,什么东西都只能在内核做。NT这一套一直发展下来,变成了后来的WDM, WDF。不知道未来又会出现啥新名词。WIN 95那一套很多地方限制没有那么严,这同时也造成了OS不够稳定。在WIN 95 之前,就是 WIN 3.X 和纯真的DOS年代了。现在的BIOS依然纯真,不过马上就要变成复杂的UEFI了。。。。=。=

回归正题,以前刚学WDM的时候,就被告知一切对硬件的操作都必须要在KERNEL LEVEL做才行。于是形成了一个思维定势,只有KERNEL LEVEL才可以操作硬件。多年以后,开始学习LINUX,惊讶的发现LINUX下面是可以允许USER LEVEL APP直接访问硬件端口的。再看INTEL 手册,发现硬件并没有设计成只能在KERNEL LEVEL才能访问。。。这时才明白,原来WINDOWS是故意那么设计的。。。

LINUX下的USER LEVEL APP也是有ROOT权限才可以访问硬件端口。同时也有一些限制,比如处理中断和DMA.不过能访问端口本身已经提供了很多方便。有的时候,只想写个小程序访问2、3个IO 端口。这个在LINUX下很简单,在WINDOWS下就要自己写驱动,或者借助其他工具和库了。

后来某一天,惊闻WINDOWS最新的UMDF也是在USER LEVEL下的驱动。于是心想,是不是WINDOWS终于想明白,要跟LINUX学,允许USER LEVEL APP 访问端口了呢?本来我以为是这样的,但是一直没仔细看UMDF。今天看了一下后,找了半天,也没有看到类似LINUX那样可以直接访问端口的东西。最后发现,UMDF还是要用FILE OBJECT 去访问那些硬件。。。也就是说WINDOWS下直接在USER LEVEL APP访问IO端口的梦想又泡汤了。。。

看来WINDOWS并没有更改他们的设计观念,UMDF也和LINUX下的东西有很大区别。当然,UMDF本身还是有一些优点的,让某些驱动的编写容易了很多,不用再工作在内核下,不再受很多限制了。另外一个有点惊讶的是,UMDF居然是基于COM的。当然也没有用到COM的全部功能,只是用了一点。不过这个也可以看出,COM这个东西还是比较成功的。感觉UMDF这个东西就是一个APP和DEVICE DRIVER的混合体。UMDF可以提供一些设备的符号链接,接受其他用户程序的调用,这点上比较像个驱动。但是在UMDF真正要访问硬件的时候,它又不能自己干,还要调用底层的KERNEL DEVICE DRIVER。还要用啥FILE OBJECT之类的。从这个角度看,UMDF也很像一个用户程序。

WDF最早也是跟着VISTA出来的。话说VISTA从技术上说还是不错的,可惜用户体验方面做的差了点,变成了一个失败的产品。又一次验证了光有技术是不够的。。。

Function caller in linux kernel

http://stackoverflow.com/questions/4141324/function-caller-in-linux-kernel

Note, printk with %pS is only supported on later version of Linux. Ref:
http://lwn.net/Articles/289064/

Another article about kallsyms_lookup:
http://daydreamer.idv.tw/rewrite.php/read-55.html

Note: when using __builtin_return_address(0), you cannot replace 0 with a variable such as int i; otherwise, you will get some compiling error.

Also, be careful when tracing back too much, such as __builtin_return_address(10). If the call stack is not as deep as 10, the kernel will happily crash.

Wednesday, February 23, 2011

ACPI S3 sleep related oprations

1. Testbed: Dell dimension computer
2. Use "R-W everything" to read the ACPI tables.
3. Find following in the SSDT table:

Scope(\)
{
Name(\_S0, Package(4) {Zero, Zero, Zero, Zero})
Name(\_S3, Package(4) {0x03, 0x03, Zero, Zero})
Name(\_S4, Package(4) {Zero, Zero, Zero, Zero})
Name(\_S5, Package(4) {0x02, 0x02, Zero, Zero})
}

4. Explanation about S4 is in ACPI spec, Section 7.3.4 System \_Sx states

Thursday, February 17, 2011

TCG architecture overview

TCG architecture overview

最近在看TCG (TRUSTED COMPUTING GROUP)相关的东西。目前只看了一个大概,感觉就是一堆东西。如下:

一堆SPEC:
以前看PCI, PCIE, ACPI啥的,基本也就是2,3个相关的SPEC吧。可是在看TCG的时候,发现有一堆的SPEC要看。还好似乎在每个TCG OR TPM 相关的SPEC的前面,都有一个图,告诉了这些SPEC的关系。一般来说按照箭头走,从最上面看起就可以了。不过我跳过了第一个ROAD MAP AND GLOSSARY. 目前只看了TCG architecture overview。理解的不一定全对,欢迎指正。

一堆KEY:
以前就知道TPM里面可以保存KEY. 看了SPEC 之后才发现原来有一堆的KEY. 这样的好处是提高了安全性。因为TPM可以有很多操作,如果全用一个KEY的话,那么被破的机会会大很多。

一堆层:
TPM是最底下的硬件。在上面还有一个OS DRIVER 级别的层,叫TPM Device Driver。然后在上面USER LEVEL 还有3层:TCG Device Driver Library(TDDL) , TSS Core Services (TCS), TCG Service Provider(TSP),每层都有一个接口。

一堆PROTOCOL:
有ADIP, ADCP ,AACP ,OIAP and OSAP。 ADIP, ADCP and AACP are used to create and manage authorization information, which is contained in objects under the control of the TPM. Additionally OIAP and OSAP are used to establish authorized session contexts leveraged by the other protocols.


一堆API:
这些API 是和之前的一堆层对应的。每层都有一个API. 这些函数和COMMAND 的形式都定义好了,厂商去实现就可以了。

当然,TPM 最著名的是PCR,还有BINDING, SIGNING 等操作。这些就不说了,直接看规范吧。

另外,TPM 也考虑到了检查其他硬件,比如VIDEO, NETWORK 状态的情况,也考虑到了升级的情况。不过还没看到具体要求。

如果没有硬件,还可以用一个软件的模拟器来熟悉下TPM。网站如下:
http://tpm-emulator.berlios.de/index.html

Tuesday, January 4, 2011

View the serial console on Linux

use command:
screen /dev/ttyUSB0 115200

/dev/ttyUSB0 should be the device to be watched. 115200 is the Bd rate.

To exit: ctrl+a, then press 'k'


Another method is to use "minicom"

in the minicom window, press 'L' to log to a file.