天天看點

openwrt 基礎知識

penWrt是一個高度子產品化、高度自動化的嵌入式Linux系統,擁有強大的網絡元件,常常被用于工控裝置、電話、小型機器人、智能家居、路由器以及VOIP裝置中。OpenWrt支援各種處理器架構,無論是對ARM,X86,PowerPC或者MIPS都有很好的支援。 其多達3000多種軟體包,囊括從工具鍊(toolchain),到核心(linux kernel),到軟體包(packages),再到根檔案系統(rootfs)整個體系,使得使用者隻需簡單的一個make指令即可友善快速地定制一個具有特定功能的嵌入式系統來制作固件。其子產品化設計也可以友善的移植各類功能到OpenWrt下,加快開發速度。

對于開發人員,OpenWrt 是使用架構來建構應用程式,而無需建立一個完整的固件來支援;對于使用者來說,這意味着其擁有完全定制的能力,可以用前所未有的方式使用該裝置。

openwrt 基礎知識

OpenWrt介紹

2013年12月19日小米路由器公測版正式發售,也意味着OpenWrt進入國内主流科技企業的眼球。然而OpenWrt到底是一款什麼樣的作業系統呢?對于創客來講,怎麼才能融入創客的設計,下面就從零介紹如果在pcDuino上開發OpenWrt。

OpenWrt項目始于2004年1月. 最早的OpenWrt版本基于Linksys為遵守GPL而放出的、為WRT54G所編寫的代碼,以及uclibc項目的buildroot。 這個版本以OpenWrt “stable release”(“穩定版”)之名為人所知,使用廣泛。仍有許多OpenWrt應用程式是基于這一版的,例如Freifunk-Firmware或[email protected]。

2005年初,一些新的開發者進入了團隊。在封閉開發了數月之後,團隊決定釋出OpenWrt的第一個“實驗”(experimental)版本。這個實驗版本使用的build系統是基于buildroot2大改而成的,而buildroot2來自于uclibc項目。 OpenWrt使用官方版GNU/Linux核心代碼,隻是額外添加了片上系統(SoC,System on Chip)的更新檔和網絡接口的驅動。開發團隊嘗試重新實作GPL tarball中不同開發商的絕大多數專有代碼。其中有:将新固件鏡像檔案直接寫入閃存的自由工具(mtd)、配置無線區域網路(wlcompat/wificonf)、通過proc檔案系統對支援VLAN的switch(交換機?)進行程式設計。最初釋出的OpenWrt的代号是“White Russian”,來自于著名雞尾酒的名稱。在OpenWrt釋出0.9版的時候,White Russian的生命周期結束。

下一個版本的開發正在我們的subversion(SVN) repository(倉庫)中進行。下面一張圖将很清晰的反應OpenWrt的版本史。從圖上可以看出最新的穩定版本代号為Backfire,早期的穩定版本為Kamikaze,開發版本一直都是trunk。各個版本的官方下載下傳位址為

https://dev.openwrt.org/wiki/GetSource

還存在另一種版本最新測試版代号為Attitude_Adjustment

openwrt 基礎知識

OpenWrt移植(一)下載下傳編譯OpenWrt

1,下載下傳依賴包

sudo apt-get install libncurses5-dev zlib1g-dev gawk flex patch git-core g++ subversion

2,OpenWrt的源代碼管理預設用的是SVN,當然你還可以用Git,本教程中使用最新的trunk版本,用SVN工具下載下傳源碼

svn co svn://svn.openwrt.org/openwrt/trunk/ openwrt-pcDuino

你還可以用Git下載下傳

git clone git://git.openwrt.org/openwrt.git

git clone git://git.openwrt.org/packages.git

3,擴充軟體包package feeds,feeds即為包含到你的OpenWrt環境中的額外軟體包的索引之類的。目前常用的feeds有:

src-git packages git://git.openwrt.org/packages.git

src-svn xwrt http://x-wrt.googlecode.com/svn/trunk/package

src-git luci git://nbd.name/luci.git

src-git routing git://github.com/openwrt-routing/packages.git

src-git telephony http://feeds.openwrt.nanl.de/openwrt/telephony.git

src-svn phone svn://svn.openwrt.org/openwrt/feeds/phone

src-svn efl svn://svn.openwrt.org/openwrt/feeds/efl

src-svn xorg svn://svn.openwrt.org/openwrt/feeds/xorg

src-svn desktop svn://svn.openwrt.org/openwrt/feeds/desktop

src-svn xfce svn://svn.openwrt.org/openwrt/feeds/xfce

src-svn lxde svn://svn.openwrt.org/openwrt/feeds/lxde

src-link custom /usr/src/openwrt/custom-feed

一般情況,你至少需要含packages feeds,其他可根據需求下載下傳、安裝feeds。

* packages – 提供衆多庫、工具等基本功能;也是其他feed所依賴的軟體源,是以在安裝其他feed前一定要先安裝packages!

* luci – OpenWrt預設的GUI(WEB管理界面)

* xwrt – 另一種可替換LuCI的GUI

* qpe – DreamBox維護的基于Qt的圖形界面,包含Qt2,Qt4,Qtopia,OPIE,SMPlayer等衆多圖形界面。

* device – DreamBox維護與硬體密切相關的軟體,如uboot,qemu等。

* dreambox_packages – DreamBox維護的國内常用網絡工具,如oh3c,njit8021xclient等

* desktop -

* xfce -基于Xorg的著名輕量級桌面環境。Xfce建基在GTK+2.x之上,它使用Xfwm作為視窗管理器。

* efl -針對enlightenment

* phone -針對fso, paroli

Trunk中預設的feeds下載下傳有packages、xwrt、luci、routing、telephony。如果你需要其他的軟體包,你隻需要打開源碼根目錄下面的feeds.conf.default文去掉你需要的軟體包前面的#号,本教程中使用預設的軟體,确定了軟體源之後,更新源:

./scripts/feeds update –a

安裝下載下傳好的包:

./scripts/feeds install -a

4,OpenWrt源碼目錄結構,執行上面指令之後你就可以得到全部的Openwrt源碼。

pillar@monster :~/openwrt/trunk$ ls

bin config docs include Makefile rules.mk target tools BSDmakefile Config.in feeds LICENSE package scripts tmp build_dir dl feeds.conf.default logs README staging_dir toolchain

tools和toolchain包含了一些通用指令,用來生成固件、編譯器、和C庫;

build dir/host是一個臨時目錄,用來儲存不依賴于目标平台的工具;

build dir/toolchain-<arch>*用來儲存依賴于指定平台的編譯鍊,隻是編譯檔案存放目錄無需修改;

build dir/ target-<arch>*用來儲存依賴于指定平台的軟體包的編譯檔案,其中包括linux核心,u-boot,packages,隻是編譯檔案存放目錄無需修改;

staging dir是編譯目标的最終安裝位置,其中包括rootfs,package, toolchain;

package——軟體包的下載下傳編譯規則,在OpenWrt固件中,幾乎所有東西都是.ipk,這樣就可以很友善的安裝和解除安裝;

target目标系統指嵌入式裝置,針對不同的平台有不同的特性,針對這些特性,”target/linux”目錄下按照平台<arch>進行目錄劃分,裡面包括了針對标準核心的更新檔,特殊配置等。

bin 編譯完OpenWrt的二進制檔案生成目錄,其中包括sdk,uImage,u-boot,dts,rootfs建構一個嵌入式系統完整的二進制檔案;

config存放着整個系統的的配置檔案;

docs裡面不斷包含了整個主控端的檔案源碼的介紹,裡面還有Makefile為目标系統生成docs

include裡面包括了整個系統的編譯需要的頭檔案,但是是以Make進行連接配接的;

feeds 擴充軟體包索引目錄;

scripts組織編譯整個OpenWrt的規則;

tmp編譯檔案夾,一般情況為空;

dl所有軟體的下載下傳目錄,包括u-boot,kernel;

logs如果編譯出錯,可以在這裡找到編譯出錯的log。

5,配置OpenWrt系統

make menuconfig

這時候會彈出和linux一樣的配置界面。

openwrt 基礎知識

在官方trunk版本中已經支援pcDuino硬體了。這裡隻需要配置OpenWrt系統,使系統支援pcDuino,具體配置如下:

A,配置目标系統(Target System)

Target System (Allwinner A1x/A20/A3x)  —>

B,配置目标硬體(Target Profile)

Target Profile (pcDuino)  —>

C,配置編譯出來的image,配置rootfs檔案系統的格式這裡選擇ext4,rootfs檔案系統的大小這裡設定(48M)。

Target Images  —>

[ ] ramdisk  —>

*** Root filesystem archives ***

[ ] cpio.gz

[*] tar.gz

*** Root filesystem images ***

[*] ext4

[ ] jffs2

[ ] squashfs

[*] GZip images

*** Image Options ***

(48) Root filesystem partition size (in MB)

(6000) Maximum number of inodes in root filesystem

(0) Percentage of reserved blocks in root filesystem

[ ] Include kernel in root filesystem  —>

[ ] Include DTB in root filesystem

D,選擇編譯交叉編譯器,還有開發SDK

[*] Build the OpenWrt Image Builder

[*] Build the OpenWrt SDK

E,配置無線網卡,V2/V3都是用的rtl8188cus無線網卡

Kernel modules  —>

Wireless Drivers  —>

-*- kmod-cfg80211…………………. cfg80211 – wireless configuration API

<*> kmod-lib80211……………………………… 802.11 Networking stack

{M} kmod-mac80211………………… Linux 802.11 Wireless Networking Stack

<M> kmod-rtl8192cu………………….. Realtek RTL8192CU/RTL8188CU support

{M} kmod-rtlwifi……………………………. Realtek common driver part

F, LucI系統快速配置接口

LuCI  —>

  1. Collections  —>

{*} luci

<M> luci-ssl……………………. Standard OpenWrt set with HTTPS

4. Themes  —>

-*- luci-theme-base…………………………. Common base for all

-*- luci-theme-bootstrap……………………… Bootstrap Theme

<*> luci-theme-freifunk-bno……………….. Freifunk Berlin Nordost Theme

<*> luci-theme-freifunk-generic………………….. Freifunk Generic Theme

<*> luci-theme-openwrt……………………………………. OpenWrt.org

5. Translations  —>

<*> luci-i18n-chinese………………….. Chinese (by Chinese Translators)

-*- luci-i18n-english………………………………………… English

6,編譯OpenWrt系統

make –j 8 V=s

由于OpenWrt整個系統非常龐大,編譯很慢。“-j 8 “表示用8線程進行編譯,”V=s”編譯的時候顯示編譯資訊。如果你的電腦是4核建議你用8線程進行編譯,雙核建議你使用4線程。這裡測試8線程編譯需要一個小時才能編譯完成。

OpenWrt移植(二)建構pcDuino BSP

由于OpenWrt的u-boot用的是u-boot-2013的版本,目前隻支援SD卡啟動,而且核心用的是3.12.5版本。另外我們的3.4.29的核心用的是全志fex,而且3.12.5用的是linux官方的kernel使用的是dts裝置樹。這樣的話我們就不能用之前的BSP方案,我們要自己做一個從SD卡啟動的系統。

pcDuino從SD卡啟動順序是A10—>u-boot–>uImage–>OpenWrt.

根據全志官網的說明,這些軟體都必須放在SD卡固定的位址。那麼首先要對A10進行分區。根據全志晶片的說明,需要對SD卡進行下表固定分區。

openwrt 基礎知識

具體的存放順序請看右邊的布局圖 ,先就開始給SD進行分區。

1,  先格式化TF卡前面的1M空間,這裡是将TF卡通過讀卡器插入到PC的虛拟機。可以看出TF卡的裝置是sdb

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda1       195G   60G  126G  33% /

udev            989M  4.0K  989M   1% /dev

tmpfs           400M  940K  399M   1% /run

none            5.0M     0  5.0M   0% /run/lock

none            998M   76K  998M   1% /run/shm

/dev/sdb1       3.8G   12K  3.8G   1% /media/0005-559B

sudo  dd  if=/dev/zero of=/dev/sdb  bs=1M  count=1

2,  寫入u-boot-spl.bin和u-boot.bin。OpenWrt的生成的二進制檔案都在openwrt/trunk/bin/sunxi目錄下,這裡OpenWrt做了一些工作将u-boot-spl.bin和u-boot.bin合在了一起,隻需要把openwrt-sunxi-pcDuino-sunxi-with-spl.bin寫到

cd  uboot-sunxi-pcDuino

pillar@monster :~/openwrt/trunk/bin/sunxi/uboot-sunxi-pcDuino$ ls

openwrt-sunxi-pcDuino-sunxi-spl.bin       openwrt-sunxi-pcDuino-u-boot.bin

openwrt-sunxi-pcDuino-sunxi-with-spl.bin

sudo  dd  if=openwrt-sunxi-pcDuino-sunxi-with-spl.bin of=/dev/sdb bs=1024 seek=8

這時候把SD卡插到闆子上,重新上電就會看到下面列印資訊

U-Boot 2013.10-rc2 (Jan 15 2014 – 17:48:38) Allwinner Technology

CPU:   Allwinner A10 (SUN4I)

Board: pcDuino

I2C:   ready

DRAM:  1 GiB

MMC:   SUNXI SD/MMC: 0

*** Warning – bad CRC, using default environment

In:    serial

Out:   serial

Err:   serial

Net:   emac

Hit any key to stop autoboot:  0

sun4i#

從上面可以看到u-boot已經完全啟動了,上面的時間是編譯的時間。上面的資訊還可以看到我們的環境變量沒有設定,它使用的是預設的環境變量。前面介紹,系統建立在分區表不同的地方,但是我們現在SD卡還沒有分區表,我們需要先建立分區表再做環境變量。

3,  建立分區表。重新把SD卡插回到電腦的虛拟機裡面,使用fdisk建立分區表。具體的分區見下操作,步驟的說明請看# 後面的注釋。

pillar@monster :~/openwrt/trunk/bin/sunxi$ sudo fdisk /dev/sdb

[sudo] password for pillar:

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel with disk identifier 0x97bf3019.

Changes will remain in memory only, until you decide to write them.

After that, of course, the previous content won’t be recoverable.

Warning: invalid flag 0×0000 of partition table 4 will be corrected by w(rite)

Command (m for help): m   #幫助

Command action

   a   toggle a bootable flag 

   b   edit bsd disklabel

   c   toggle the dos compatibility flag

   d   delete a partition

   l   list known partition types

   m   print this menu

   n   add a new partition   #建立分區

   o   create a new empty DOS partition table

   p   print the partition table  #檢視分區

   q   quit without saving changes

   s   create a new empty Sun disklabel

   t   change a partition’s system id   #改變分區類型

   u   change display/entry units

   v   verify the partition table

   w   write table to disk and exit

   x   extra functionality (experts only)

Command (m for help): p             #檢視分區

Disk /dev/sdb: 4027 MB, 4027580416 bytes

124 heads, 62 sectors/track, 1023 cylinders, total 7866368 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x97bf3019

   Device Boot      Start         End      Blocks   Id  System

                                                                                                                                            #沒有分區

Command (m for help): n                                                               #建立分區

Partition type:

   p   primary (0 primary, 0 extended, 4 free)    #主分區

   e   extended                            #擴充分區

Select (default p):                            #選擇預設主分區

Using default response p

Partition number (1-4, default 1):                #分區号為1

Using default value 1

First sector (2048-7866367, default 2048):         #選擇預設值

Using default value 2048

Last sector, +sectors or +size{K,M,G} (2048-7866367, default 7866367): 34815 #這個根據全志的手冊來第一個分區必須這麼大

Command (m for help): p                       #檢視分區

Disk /dev/sdb: 4027 MB, 4027580416 bytes

124 heads, 62 sectors/track, 1023 cylinders, total 7866368 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x97bf3019

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048       34815       16384   83  Linux  #建立的第一個分區

Command (m for help): n                    #再建立一個分區

Partition type:

   p   primary (1 primary, 0 extended, 3 free)

   e   extended

Select (default p):                          #主分區

Using default response p

Partition number (1-4, default 2):              #第二個主分區

Using default value 2

First sector (34816-7866367, default 34816):      #預設大小從34816開始

Using default value 34816

Last sector, +sectors or +size{K,M,G} (34816-7866367, default 7866367): #預設全部分到第二分區

Using default value 7866367

Command (m for help): p                       #再一次檢視分區

Disk /dev/sdb: 4027 MB, 4027580416 bytes

124 heads, 62 sectors/track, 1023 cylinders, total 7866368 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x97bf3019

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048       34815       16384   83  Linux

/dev/sdb2           34816     7866367     3915776   83  Linux

#可以看出建立了兩個分區都為linux類型,但是u-boot隻能識别第一個分區為FAT32分區

Command (m for help): t            #修改分區類型

Partition number (1-4): 1            #選擇修改哪個分區

Hex code (type L to list codes): L      #列出所有類型,

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris       

 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-

 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-

 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-

 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx        

 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data   

 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .

 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility  

 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt        

 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access    

 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O       

 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor     

 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs       

 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT           

 f  W95 Ext’d (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/

10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b

11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor     

12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor     

14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary 

16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS   

17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE

18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto

1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep       

1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT           

1e  Hidden W95 FAT1 80  Old Minix     

Hex code (type L to list codes): c             #選擇FAT32

Changed system type of partition 1 to c (W95 FAT32 (LBA))

Command (m for help): p                  #再一次檢視分區

Disk /dev/sdb: 4027 MB, 4027580416 bytes

124 heads, 62 sectors/track, 1023 cylinders, total 7866368 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x97bf3019

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1        2048       34815     16384    c  W95 FAT32 (LBA)#已經修改過了了

/dev/sdb2           34816     7866367     3915776   83  Linux

Command (m for help): w                                   #儲存分區表

The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: If you have created or modified any DOS 6.x

partitions, please see the fdisk manual page for additional

information.

Syncing disks.

4,  格式化分區。剛才建立了分區,但是沒有格式化,我們還是不能使用。

pillar@monster :~/openwrt/trunk/bin/sunxi$ ls /dev/sdb  #檢視已經分好的分區

sdb   sdb1  sdb2 

pillar@monster :~/openwrt/trunk/bin/sunxi$ mkf        #檢視有哪些分區類型

mkfifo        mkfontscale   mkfs.bfs      mkfs.ext2     mkfs.ext4     mkfs.minix    mkfs.ntfs

mkfontdir     mkfs          mkfs.cramfs   mkfs.ext3     mkfs.ext4dev  mkfs.msdos    mkfs.vfat

pillar@monster :~/openwrt/trunk/bin/sunxi$ sudo mkfs.vfat /dev/sdb1 #第一個分區格式化為fat分區

[sudo] password for pillar:

mkfs.vfat 3.0.12 (29 Oct 2011)

pillar@monster :~/openwrt/trunk/bin/sunxi$ sudo mkfs.ext4 /dev/sdb2 #第二個分區格式化為ext4分區,這裡需要幾分鐘

mke2fs 1.42 (29-Nov-2011)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

244800 inodes, 978944 blocks

48947 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=1002438656

30 block groups

32768 blocks per group, 32768 fragments per group

8160 inodes per group

Superblock backups stored on blocks:

         32768, 98304, 163840, 229376, 294912, 819200, 884736

Allocating group tables: done                           

Writing inode tables: done                           

Creating journal (16384 blocks): done

Writing superblocks and filesystem accounting information: done

5,  挂載分區。

pillar@monster :~/openwrt/trunk/bin/sunxi$ sudo mount /dev/sdb1 /media/1

pillar@monster :~/openwrt/trunk/bin/sunxi$ sudo mount /dev/sdb2 /media/2

pillar@monster :~/openwrt/trunk/bin/sunxi$ df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda1       195G   60G  126G  33% /

udev            989M  4.0K  989M   1% /dev

tmpfs           400M  944K  399M   1% /run

none            5.0M     0  5.0M   0% /run/lock

none            998M   76K  998M   1% /run/shm

/dev/sdb1        16M     0   16M   0% /media/1

/dev/sdb2       3.7G  7.5M  3.5G   1% /media/2

6,  制作u-boot環境變量檔案。剛剛建立了分區,這裡隻需要将環境變量檔案,還有uImage拷貝到第一分區讓u-boot讀取,就可以引導系統了。下面開始制作u-boot環境變量檔案。

pillar@monster :/media/1$ vim boot.cmd

  1 setenv bootargs console=ttyS0,115200 root=/dev/mmcblk0p2 rootwait panic=10 ${extra}

  2 fatload mmc 0 0×46000000 uImage

  3 fatload mmc 0 0×49000000 sun4i-a10-pcduino.dtb

  4 fdt_high ffffffff

  5 bootm 0×46000000 – 0×49000000

pillar@monster :/media/1$mkimage -C none -A arm -T script -d boot.cmd boot.scr

7,  将系統檔案拷貝到第一和第二分區。

pillar@monster :/media/1$ cp ~/openwrt/trunk/bin/sunxi/sun4i-a10-pcduino.dtb  .

pillar@monster :/media/1$ cp ~/openwrt/trunk/bin/sunxi/openwrt-sunxi-uImage  uImage

pillar@monster :/media/1$ ls           #第一分區檔案

boot.scr  sun4i-a10-pcduino.dtb    uImage

pillar@monster :/media$ sudo dd if=~/openwrt/trunk/bin/sunxi/openwrt-sunxi-root.ext4 of=/dev/sdb2 bs=1M                   #拷貝第二分區檔案

好了,現在整個的從SD啟動的BSP已經最好了。

8,  釋出并燒寫系統。現在把系統做好了,你可以釋出你制作的系統,然後别人可以通過win32diskimager來把你的系統寫入到他的SD卡,他就可以和你一起玩OpenWrt。

pillar@monster :/media$ sudo dd if=/dev/sdc of=OpenWrt.img  bs=4M

現在把OpenWrt.img拷貝到windows上,把你新的SD卡插到電腦開始用win32diskimager寫入

openwrt 基礎知識

OpenWrt移植(三) 配置系統

1,  讓系統上網

vim /etc/config/network

config interface ‘net’    

        option ifname ‘eth0′                  

        option proto ‘dhcp’

2,設定固定的mac位址

當系統的啟動的時候發現mac位址老實在變,這就會出現一個問題,有時候能擷取到ip,有時候擷取不到ip。這裡可以做一個系統服務,讓系統開機儲存mac位址,然後再開機的時候恢複之前的mac位址。

1>,  在/etc/init.d/mac裡面編寫如下腳本

#!/bin/sh /etc/rc.common

START=18

STOP=91

start() {

if [ -f /mac ]; then

dd if=/mac bs=1 count=17 of=/tmp/mac >/dev/null 2>&1

mac_addr=`cat /tmp/mac`

else

mac_file=/sys/class/net/eth0/address

dd if=$mac_file bs=1 of=/mac count=17 >/dev/null 2>&1

mac_addr=`cat /tmp/mac`

fi

ifconfig eth0 down

ifconfig eth0 hw ether $mac_addr

#if failed, save current mac address

if [ $? -ne 0 ]; then

mac_file=/sys/class/net/eth0/address

dd if=$mac_file bs=1 of=/mac count=17 >/dev/null 2>&1

fi

}

2>,  指定運作的模式

/etc/rc.d/rc則根據其參數指定的運作模式(運作級别,你在inittab檔案中可以設定)來執行相應目錄下的腳本。凡是以Kxx開頭的,都以stop為參數來調用;凡是以Sxx開頭的,都以start為參數來調用。調用的順序按xx 從小到大來執行。例如,假設預設的運作模式是3,/etc/rc.d/rc就會按上述方式調用 。

由于設定mac位址要在network之前。是以要建立連結:

ln  -s   /etc/init.d/mac   /etc/rc.d/S18mac

3,開啟wifi

openwrt啟動之後輸入:

root@OpenWrt :/# ifconfig

eth0      Link encap:Ethernet  HWaddr AE:DB:9A:D9:31:DE

inet addr:192.168.1.119  Bcast:192.168.1.255  Mask:255.255.255.0

inet6 addr: fe80::acdb:9aff:fed9:31de/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:696 errors:0 dropped:0 overruns:0 frame:0

TX packets:640 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:86028 (84.0 KiB)  TX bytes:377264 (368.4 KiB)

Interrupt:17 Base address:0×4000

lo        Link encap:Local Loopback

inet addr:127.0.0.1  Mask:255.0.0.0

inet6 addr: ::1/128 Scope:Host

UP LOOPBACK RUNNING  MTU:65536  Metric:1

RX packets:16 errors:0 dropped:0 overruns:0 frame:0

TX packets:16 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:1786 (1.7 KiB)  TX bytes:1786 (1.7 KiB)

wlan0     Link encap:Ethernet  HWaddr 00:7A:03:00:29:F4

inet6 addr: fe80::27a:3ff:fe00:29f4/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:0 errors:0 dropped:0 overruns:0 frame:0

TX packets:7 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:0 (0.0 B)  TX bytes:864 (864.0 B)

確定ethX和wlanX都有。openwrt的root密碼是沒有設定,你需要從serial debug進入系統設定root密碼,設定方法如下:

passwd  root

然後在同一個區域網路内你的PC的浏覽器上輸入:ethX的ip,這裡是192.168.1.119.就會出現下面界面:

openwrt 基礎知識

輸入你剛才設定的密碼,進入系統管理界面,預設是進入狀态标簽,這裡你可以看到整個系統的運作的狀态。

openwrt 基礎知識

如果你對目前頁面不太習慣,而且在使用上語言上也有些困難,你可以進入system标簽,在System Properties裡面設定language and style如下圖所示。設定完之後save & apply,重新重新整理一下浏覽器就可以使用你設定的語言和主題。

openwrt 基礎知識

下面進入網絡标簽欄設定wifi節點,這個部分是openwrt比較複雜的一個部分,這個部分的設定直接決定着你的openwrt能不能使用。

openwrt 基礎知識

添加新接口,選擇靜态ip,新接口的名稱,你需要用英文自定義一個名字,在包括一下接口裡面選擇無線網絡。設定完之後送出,進入下一個頁面繼續設定。

openwrt 基礎知識

基本設定設定完成之後,進入防火牆設定,這裡wifi必須選擇為lan口。設定完成之後儲存應用。這個時候你電腦就可以連接配接使用openwrt這個路由器了。

openwrt 基礎知識
openwrt 基礎知識

點選修改後進入防火牆設定标簽欄,配置設定防火牆區域為wan,設定完成之後儲存&應用。

openwrt 基礎知識

OpenWrt系統開發(一)制作核心更新檔

1,  清空恢複上一個全新的核心

make target/linux/{clean,prepare} V=s QUILT=1

2,  到核心源碼目錄

cd build_dir/target-*/linux-*/linux-3.*

3,  建立git代碼倉庫,當然你要事先建立git環境,如果使用Git請參見附錄一。

git init

git add * -f

git commit -a -m “first add”

4,  修改你的代碼,這裡我給我代碼添加rtl8188cus驅動

mkdir drivers/net/wireless/rtl8192cus

cp  /home/pillar/openwrt/openwrt-pcDuino/RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/driver/rtl8188C_8192C_usb_linux_v4.0.2_9000.20130911

static int __init hello_init(void)

{

   printk(“I bear a charmed life.\n”);

   return 0;

}

/ * hello_exit —- 退出函數,當子產品解除安裝時被調用 */

static void __exit hello_exit(void)

{

   printk(“Out, out, brief candle\n”);

}

module_init(hello_init);

module_exit(hello_exit);

MODULE_LICENSE(“GPL”);

MODULE_AUTHOR(“Pillar_zuo”);

vim Kconfig

config EXAMPLE

  tristate “Just a example”

  default n

  help

   This is a example, for debugging kernel model.

   If unsure, say N.

vim Makefile

obj-m := example.o

4,  回到OpenWrt源碼根目錄下

make menuconfig

  Kernel modules —>

    Other modules —>

      kmod-example

選項設定為M,儲存退出

然後編譯該子產品:

make package/example/compile

make package/index

5,在OpenWrt系統裡面就可以用opkg下載下傳使用了。

OpenWrt系統開發(六)使用OpenWrt SDK

OpenWrt為了避免每次都重新編譯系統,引入了SDK機制。我們在釋出系統的時候也需要釋出SDK,具體的使用方法請下面例子。

1,解壓SDK

pillar@monster :~/openwrt/trunk/bin/sunxi$ tar xvf

 OpenWrt-SDK-sunxi-for-linux-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2

cd  OpenWrt-SDK-sunxi-for-linux-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2

1,  建立軟體工作目錄

cd package

mkdir helloworld

vim Makefile    #這個Makefile可以作為模闆

##############################################

# OpenWrt Makefile for helloworld program

#

#

# Most of the variables used here are defined in

# the include directives below. We just need to

# specify a basic description of the package,

# where to build our program, where to find

# the source files, and where to install the

# compiled program on the router.

#

# Be very careful of spacing in this file.

# Indents should be tabs, not spaces, and

# there should be no trailing whitespace in

# lines that are not commented.

#

##############################################

include $(TOPDIR)/rules.mk

# Name and release number of this package

PKG_NAME:=helloworld

PKG_RELEASE:=1

# This specifies the directory where we’re going to build the program.

# The root build directory, $(BUILD_DIR), is by default the build_mipsel

# directory in your OpenWrt SDK directory

PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)

include $(INCLUDE_DIR)/package.mk

# Specify package information for this program.

# The variables defined here should be self explanatory.

# If you are running Kamikaze, delete the DESCRIPTION

# variable below and uncomment the Kamikaze define

# directive for the description below

define Package/helloworld

    SECTION:=utils

    CATEGORY:=Utilities

    TITLE:=Helloworld — prints a snarky message

endef

# Uncomment portion below for Kamikaze and delete DESCRIPTION variable above

define Package/helloworld/description

        If you can’t figure out what this program does, you’re probably

        brain-dead and need immediate medical attention.

endef

# Specify what needs to be done to prepare for building the package.

# In our case, we need to copy the source files to the build directory.

# This is NOT the default.  The default uses the PKG_SOURCE_URL and the

# PKG_SOURCE which is not defined here to download the source from the web.

# In order to just build a simple program that we have just written, it is

# much easier to do it this way.

define Build/Prepare

    mkdir -p $(PKG_BUILD_DIR)

    $(CP) ./src/* $(PKG_BUILD_DIR)/

endef

# We do not need to define Build/Configure or Build/Compile directives

# The defaults are appropriate for compiling a simple program such as this one

# Specify where and how to install the program. Since we only have one file,

# the helloworld executable, install it by copying it to the /bin directory on

# the router. The $(1) variable represents the root directory on the router running

# OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install

# directory if it does not already exist.  Likewise $(INSTALL_BIN) contains the

# command to copy the binary file from its current location (in our case the build

# directory) to the install directory.

define Package/helloworld/install

    $(INSTALL_DIR) $(1)/bin

    $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/

endef

# This line executes the necessary commands to compile our program.

# The above define directives specify all the information needed, but this

# line calls BuildPackage which in turn actually uses this information to

# build a package.

$(eval $(call BuildPackage,helloworld))

mkdir src

cd src

2,  編寫自己的軟體,這裡以helloworld為例。

vim helloworld.c

  1 #include<stdio.h>

  2 int main(void)

  3 {

  4      printf(“Hell! O’ world, why won’t my code compile?\n\n”);

  5      return 0;

  6 }

vim Makefile

  1 # build helloworld executable when user executes “make”

  2 helloworld: helloworld.o

  3     $(CC) $(LDFLAGS) helloworld.o -o helloworld

  4 helloworld.o: helloworld.c

  5     $(CC) $(CFLAGS) -c helloworld.c

  6 # remove object files and executable when user executes “make clean”

  7 clean:

  8     rm *.o helloworld

3,  編譯軟體,回到SDK根目錄下。

cd ../../

make V=s

pillar@monster :~/openwrt/trunk/bin/sunxi/OpenWrt-SDK-sunxi-for-linux-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2$ ls bin/sunxi/packages/

helloworld_1_sunxi.ipk  Packages  Packages.gz

5,修改軟體源根目錄。如果你不想每次都拷貝,你可以把軟體源的根目錄下設定在

OpenWrt-SDK-sunxi-for-linux-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2/bin/sunxi/packages

然後你可以在OpenWrt系統裡面下載下傳安裝。