2016年8月19日 星期五

git觀念, ssh-keygen的使用方式, github註冊金鑰

今天要紀錄的是git觀念以及ssh-keygen的使用方式

git分成Server端與client端,基本上以往用常常用到git指令去github抓取open source的code,使用git抓取的人就是client端,把source code分享到他自己的github上就是server端。

當你要去抓取別人的github裡的source code時,你必須先在自己的電腦端使用ssh-keygen產生一組key,然後把這組key給server端的人,server端要幫你把這組key註冊到他的github裡,這樣你才會有權限去抓取(git clone)這個source code.

下圖是沒有註冊key去抓取github上的東西,所回應的錯誤訊息(白色是我打馬賽克的部分@@)






以上是觀念的部分,接下來是實際操作的部分



================  分隔線 ==================
首先在你的電腦端(client) 新增ssh key,輸入指令:
           ssh-keygen -t rsa -C "your_email@example.com"
 
    會出現下圖的情形,他會告知你將要產生id_rsa的金鑰檔案,請按Enter跳過




    再來會請你輸入密碼,這個密碼是當你要用git clone抓東西時,所要輸入的密碼:







   這樣就可以成功產生一個ssh key了:
















下圖是key的內容,也就是待會要向github作註冊的key






================  分隔線 ==================
接下來是github端的要做的部分

首先進入github網站,你可以自行註冊帳號,這樣你也可以放一些東西到github上,也可以用git指令去下載。

下圖是我們要把剛剛產生出來的key註冊到github server端,讓client端有權限可以作git clone.
請點選右上角的部分,然後選擇settings



















接著選擇左邊的ssh and GPG keys選項



















接著請點選New SSH key來註冊一組新的key,我們也可以看到,已經有註冊了兩組ssh key




















接著填入Title以及key的內容,按下Add SSH key即完成註冊key的動作


















    我們可以看到,現在已經註冊了三組key

















   這樣你就可以順利在你的電腦端(client)執行git clone的下載動作了。

2016年8月16日 星期二

交叉編譯 wpa_supplicant 移植到ARMv7板子

要交叉編譯wpa_supplicant,首先必須先編譯出for ARMv7的openssl

所以第一步先去抓最新版的openssl,到下面的官網下載,我這邊抓的是openssl-1.0.1t

https://www.openssl.org/

抓完之後就準備開始做openssl的cross compile了

=========== cross compile openssl for arm ================
1. ./config --prefix=/home/danny/ARMv7/

2.成功config完成之後,手動進入Makefile更改toolchain的路徑
      #++++++ Add+++
      CROSS_COMPILE=/home/danny/x-tool/armv7-rpi2-linux-gnueabihf/bin/armv7-rpi2-linux-gnueabihf-
      #======Modify =====
      CC=$(CROSS_COMPILE)gcc
      AR= $(CROSS_COMPILE)ar $(ARFLAGS) r
      RANLIB= $(CROSS_COMPILE)ranlib
      NM= $(CROSS_COMPILE)nm

3. make
    在這個步驟之後,我遇到的情形,會出現如下面所秀出的一堆ERROR訊息
     x86cpuid.s: Assembler messages:
     x86cpuid.s:4: Error: unrecognized symbol type ""
     x86cpuid.s:5: Error: alignment too large: 15 assumed
     x86cpuid.s:8: Error: bad instruction `pushl %ebp'
     x86cpuid.s:9: Error: bad instruction `pushl %ebx'
     x86cpuid.s:10: Error: bad instruction `pushl %esi'
     x86cpuid.s:11: Error: bad instruction `pushl %edi'
     x86cpuid.s:12: Error: bad instruction `xorl %edx,%edx'
     x86cpuid.s:13: Error: bad instruction `pushfl'
     x86cpuid.s:14: Error: bad instruction `popl %eax'
     x86cpuid.s:15: Error: bad instruction `movl %eax,%ecx'
     x86cpuid.s:16: Error: bad instruction `xorl $2097152,%eax'
     x86cpuid.s:17: Error: bad instruction `pushl %eax'
     x86cpuid.s:18: Error: bad instruction `popfl'
     x86cpuid.s:19: Error: bad instruction `pushfl'
     x86cpuid.s:20: Error: bad instruction `popl %eax'
     x86cpuid.s:21: Error: bad instruction `xorl %eax,%ecx'
     x86cpuid.s:22: Error: bad instruction `xorl %eax,%eax'      ..................

     請在重新config一次並在後面加入一些參數如下面指令

     ./config --prefix=/home/danny/ARMv7/ shared no-zlib no-asm no-threads no-sse2 no-gmp no-rfc3779 no-krb5 no-rc5 no-zlib-dynamic no-hw no-cipher no-md2 no-mdc2 no-rc2 no-idea no-camellia no-ec no-ripemd no-rc4 no-bf no-cast no-perlasm

     config完成之後,請記得再遵照步驟2,手動修改一下Makefile裡的toolchain路徑



4. make
    make install

這樣openssl就編譯完成,並安裝在你指定的路徑了,openssl會編譯出兩個library  分別是openssl與crypt,而wpa_supplicant還需要另一個libnl
=================================================================


============== cross compile libnl for arm ==========================
libnl我是下載 libnl-3.2.28 載點如下
http://www.linuxfromscratch.org/blfs/view/svn/basicnet/libnl.html

1. ./configure ARCH=arm --prefix=/home/danny/ARMv7/ CC=/home/danny/x-tool/armv7-rpi2-linux-gnueabihf/bin/armv7-rpi2-linux-gnueabihf-gcc --host=arm-linux LD=/home/danny/x-tool/armv7-rpi2-linux-gnueabihf/bin/armv7-rpi2-linux-gnueabihf-ld

2. make
    make install

這邊比較不會有甚麼問題,也可以成功編譯出libnl的library

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




最後是編譯wpa_supplicant的部分
=============== cross compile wpa_supplicant for arm =================
首先新進入到下面的資料夾當中

1. cd wpa_supplicant-2.5/wpa_supplicant
 
2. cp defconfig .config

3. 進入Makefile做修改
    CROSS_COMPILE=/home/danny/x-tools/armv7-rpi2-linux-gnueabihf/bin/armv7-rpi2-linux-gnueabihf-
    CC=$(CROSS_COMPILE)gcc

    export LIBDIR ?= /home/danny/ARMv7/wpa_supplicant/lib/
    export INCDIR ?= /home/danny/ARMv7/wpa_supplicant/include/
    export BINDIR ?= /home/danny/ARMv7/wpa_supplicant/bin/
    LIBS += -L/home/danny/ARMv7/lib -lssl -lcrypto

4. make


Q1. 首先會出現錯誤訊息如下所示,這是因為找不到openssl的header檔
../src/crypto/tls_openssl.c:19:25: fatal error: openssl/ssl.h: No such file or directory

請進入Makefile修改增加下面這一行,路徑是先前openssl的安裝路徑
CFLAGS += -I/home/danny/ARMv7/include



Q2. 接著會出現錯誤訊息如下所示,這是因為找不到libnl的header檔
../src/drivers/driver_nl80211.c:17:31: fatal error: netlink/genl/genl.h: No such file or directory

請進入Makefile修改增加下面這一行,路徑是先前libnl安裝的include檔路徑
CFLAGS += -I/home/danny/ARMv7/include/libnl3



Q3. 接著會出現如下的undefined reference訊息
        ../src/drivers/driver_nl80211.o: In function `get_channel_width':
        /home/danny/wpa_supplicant-2.5/wpa_supplicant/../src/drivers/driver_nl80211.c:6492: undefined reference to `genlmsg_attrdata'
        /home/danny/wpa_supplicant-2.5/wpa_supplicant/../src/drivers/driver_nl80211.c:6492: undefined reference to `genlmsg_attrlen'
        ../src/drivers/driver_nl80211.o: In function `netdev_info_handler':
        /home/danny/wpa_supplicant-2.5/wpa_supplicant/../src/drivers/driver_nl80211.c:519: undefined reference to `genlmsg_attrdata'

遇到這個問題,請修改.config檔,把原本被註解掉的這兩個項目打開
CONFIG_LIBNL20=y
CONFIG_LIBNL32=y


Q4. 遇到如下的錯誤訊息
      Package libnl-3.0 was not found in the pkg-config search path.
      Perhaps you should add the directory containing `libnl-3.0.pc'
      to the PKG_CONFIG_PATH environment variable
      No package 'libnl-3.0' found

請在terminal底下,設定如下環境變數,路徑為libnl安裝的資料夾底下lib有一個pkgconfig資料夾
export PKG_CONFIG_PATH=/home/danny/ARMv7/lib/pkgconfig


Q5. 遇到找不到libcrypto的問題,錯誤訊息如下
     ...armv7-rpi2-linux-gnueabihf/bin/ld.bfd: cannot find -lcrypto collect2: error: ld returned 1 exit status

請在Makefile中搜尋 -lcrypto,把找到的項目 前面都指定 先前編譯好安裝的openssl路徑,例如
LIBS += -L/home/danny/ARMv7/lib -lcrypto
LIBS_p += -L/home/danny/ARMv7/lib -lcrypto
總共有兩個地方要加



最後就可以順利make成功了!!!


       

  

apt-file search 的使用

當你在Ubuntu上做一些open source的編譯時

如果遇到類似這樣的訊息 ===> fatal error: openssl/ssl.h: No such file or directory

有可能是你host端(PC)缺少了一些套件

可以先使用apt-file search /openssl/ssl.h 他會跟你說你缺少的套件名稱

你就可以使用apt-get install去做安裝

2016年7月28日 星期四

如何撰寫Linux Kernel中的Timer機制

想要在kernel中寫Timer的機制,首先要些對一些專有名詞有一些初步的了解,分別是HZ, Tick, Jiffies.

HZ:Linux kernel 每隔固定週期會發出timer interrupt,HZ就是用來定義每一秒有幾次timer interrupts。例如:500 HZ 就是CPU每一秒會發出500個interrupt的意思。這個值是可以在Kernel的menuconfig當中來做設定.

Tick:Tick是HZ的倒數,意即timer interrupt每發生一次中斷所花的時間。如HZ為500時,tick為2毫秒 (millisecond)。

Jiffies:Jiffies為Linux核心變數(32位元變數,unsigned long),它被用來紀錄系統自開幾以來,已經過多少的tick。每發生一次timer interrupt,Jiffies變數會被加一。


接下來開始介紹Timer API的做法,我們先來看以下定義所代表的意義:

首先先宣告一個Timer的Struct:
struct timer_list timer;

這個Struct當中有幾個比較重要的值,我們要去做設定分別是:
timer.function    <===== timer 啟動後所設定的expires時間到後,會去做此參數所指定的function
timer.data             <===== timer 傳入function的參數值
timer.expires      <===== timer 延遲的時間,也就是延遲多久後去做 timer.function 所指定的函式

而下面這部分則是想要timer delay多久的寫法
Jiffies + HZ;   <=====  1秒之後 

Jiffies  + HZ/2;       <=====   半秒之後

Jiffies  + 20*HZ;     <=====  20秒之後


接下來就是實做一個Timer的Code.
/**************** Kernel_Timer *************/
struct timer_list danny_timer;

static int danny_do(void)
{
    danny_timer.expires = jiffies + HZ;
    add_timer(&danny_timer);
}

static void danny_timer_init(void)
{
    /* Timer 初始化 */
   init_timer(&danny_timer);

   /* define timer 要執行之函式 */
  danny_timer.function = danny_do;

  /* define timer 傳入函式之 Data */
  danny_timer.data = ((unsigned long) 0);

  /* define timer Delay 1秒的時間 */
  danny_timer.expires = jiffies + HZ;

  /* 啟動 Timer*/
  add_timer(&danny_timer);
}
/*********************************************/

結論:

所以當呼叫 danny_timer_init();之後,delay 1秒之後,會去做danny_do();

而danny_do()裡面也加了add_timer()的函式,所以會不斷的迴圈,每隔1秒鐘都會去做danny_do()的函式.



 

2016年5月19日 星期四

Linux_Driver入門-寫一個HelloWorld module

因為是driver的初學者,所以先練習寫一支HelloWorld的module,這個module的行為是當insmod這個module的時候,會印出"Hello World!"的字眼,而在rmmod的時候會印出"Goodbye, hello world"

首先描述一下整個編譯環境,以及要掛這個module的平台:
首先說明要掛這個module的平台
- arm64平台(DB410C),系統是linux

編譯平台
- PC,系統是ubuntu,DB410C的kernel與toolchain都準備好了

這邊先說明一下,當你要作交叉編譯時,一定要有Target Board(DB410C)的toolchain,以這邊的例子為例,我在PC上面編譯,我利用toolchain把程式編譯成Target Board可讀的檔案

另外要注意的是,要編譯driver,必須要拿到與Target Board相同的Kernel Source Code(版本要相同),Source code必須整包放在要進行編譯的PC端,編譯時必須參考到裡面的一些header檔

接著就來看我們的HelloWorld.c

///////////////////////////////////////////  HelloWorld.c  ///////////////////////////////////////////
#include <linux/module.h>
#include <linux/init.h>

MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)
{
    printk("Hello world!\n");
    return 0;
}

static void hello_exit(void)
{
    printk("Goodbye, hello world\n");
}

module_init(hello_init);
module_exit(hello_exit);
///////////////////////////////////////////////////////////////////////////////////////////////////////////

這程式很單純,在module被insert的時候就會執行hello_init的function
在module被remove時會執行hello_exit的function



接著是Makefile的部分

obj-m += hello.o

KERNEL=/home/danny/DB410C/kernel/kernel

all:
make -C $(KERNEL) M=$(shell pwd) modules ARCH=arm64

clean:
make -C $(KERNEL) M=$(shell pwd) clean


編譯之前這邊要注意兩個部分,首先是必須給系統CROSS_COMPILE環境變數,必須給他toolchain的完整路徑,請使用export指令:
export CROSS_COMPILE=/home/Danny/DB410C/toolchain/gcc-linaro-4.9-2014.11-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-
(請依照你的toolchain給定CROSS_COMPILE的值)

另外一部分是Makefile中KERNEL這個參數,必須要指定到PC上,Target Board(DB410C)的Kernel Source Code,這樣編譯時才不會出錯!


接著只需下make,hello.ko就會被產生出來,接著把hello.ko複製到Target Board上,接著執行
sudo insmod hello.ko

sudo rmmod hello.ko

輸入dmesg就可以看到driver印出的訊息囉!






Linux中設定root的密碼

當要使用su指令切換成root的時候 ,會請求你輸入root的密碼,但是都沒有設定過root密碼要怎麼處裡呢?

請直接輸入下面的指令:
sudo passwd root

他會要求你設定密碼,設定完成後就可以順利使用su切換root了!

2016年5月13日 星期五

Linux中設定環境變數的方法

當你需要作cross-compiler時,須要先安裝Toolchain在你的工作電腦上(Ubuntu, Fedora, Debiab etc.), 與其說安裝其實就是把Toolchain包解壓縮到某個路徑。接著要作交叉編譯時,需要指定編譯工具的路徑,此時為了不用每次都輸入一整串的路徑來指定編譯工具,就會去設定PATH。例如:我的編譯器arm-linux-gnueabihf-gcc路徑是在 "home/danny/workspace/DB410C/Toolchain/gcc-linaro-4.9-2014.11-x86_64_arm-linux-gnueabihf/bin",此時有三個方法來設定環境變數:

1.用export指令
export PATH=$PATH:/home/danny/workspace/DB410C/Toolchain/gcc-linaro-4.9-2014.11-x86_64_arm-linux-gnueabihf/bin
輸入之後可以使用export指令來查看環境變數是否有輸入進去。

*此修改重開機後,就必須再作一次

2.修改profile
profile的路徑是在 "/etc/profile"
直接修改profile這個檔案在裡面加入
export PATH=$PATH:/home/danny/workspace/DB410C/Toolchain/gcc-linaro-4.9-2014.11-x86_64_arm-linux-gnueabihf/bin

*此修改必須在重開機之後,才會有作用


3.修改.bashrc
.bashrc的路徑是在"/home/danny/.bashrc"
在檔案最後面加入
export PATH=$PATH:/home/danny/workspace/DB410C/Toolchain/gcc-linaro-4.9-2014.11-x86_64_arm-linux-gnueabihf/bin

*此修改只需關掉Terminal在開啟後,就都會被設定



***
另外發現也可以直接去修改 /etc/enviroment  這檔案裡面包含原本PATH變數的資料, 要增加請在最後面用:加上你要加入的路徑即可, 例如:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/danny/workspace/DB410C/Toolchain/gcc-linaro-4.9-2014.11-x86_64_arm-linux-gnueabihf/bin"