|
OpenWrt是一個高度模塊化、高度自動化的嵌入式Linux系統(tǒng),擁有強(qiáng)大的網(wǎng)絡(luò)組件,常常被用于工控設(shè)備、電話、小型機(jī)器人、智能家居、路由器以及VOIP設(shè)備中。OpenWrt支持各種處理器架構(gòu),無論是對ARM,X86,PowerPC或者M(jìn)IPS都有很好的支持。 其多達(dá)3000多種軟件包,囊括從工具鏈(toolchain),到內(nèi)核(linux kernel),到軟件包(packages),再到根文件系統(tǒng)(rootfs)整個體系,使得用戶只需簡單的一個make命令即可方便快速地定制一個具有特定功能的嵌入式系統(tǒng)來制作固件。其模塊化設(shè)計(jì)也可以方便的移植各類功能到OpenWrt下,加快開發(fā)速度。 對于開發(fā)人員,OpenWrt 是使用框架來構(gòu)建應(yīng)用程序,而無需建立一個完整的固件來支持;對于用戶來說,這意味著其擁有完全定制的能力,可以用前所未有的方式使用該設(shè)備。 OpenWrt介紹
2013年12月19日小米路由器公測版正式發(fā)售,也意味著OpenWrt進(jìn)入國內(nèi)主流科技企業(yè)的眼球。然而OpenWrt到底是一款什么樣的操作系統(tǒng)呢?對于創(chuàng)客來講,怎么才能融入創(chuàng)客的設(shè)計(jì),下面就從零介紹如果在pcDuino上開發(fā)OpenWrt。 OpenWrt項(xiàng)目始于2004年1月. 最早的OpenWrt版本基于Linksys為遵守GPL而放出的、為WRT54G所編寫的代碼,以及uclibc項(xiàng)目的buildroot。 這個版本以O(shè)penWrt “stable release”(“穩(wěn)定版”)之名為人所知,使用廣泛。仍有許多OpenWrt應(yīng)用程序是基于這一版的,例如Freifunk-Firmware或Sip@Home。 2005年初,一些新的開發(fā)者進(jìn)入了團(tuán)隊(duì)。在封閉開發(fā)了數(shù)月之后,團(tuán)隊(duì)決定發(fā)布OpenWrt的第一個“實(shí)驗(yàn)”(experimental)版本。這個實(shí)驗(yàn)版本使用的build系統(tǒng)是基于buildroot2大改而成的,而buildroot2來自于uclibc項(xiàng)目。 OpenWrt使用官方版GNU/Linux內(nèi)核代碼,只是額外添加了片上系統(tǒng)(SoC,System on Chip)的補(bǔ)丁和網(wǎng)絡(luò)接口的驅(qū)動。開發(fā)團(tuán)隊(duì)嘗試重新實(shí)現(xiàn)GPL tarball中不同開發(fā)商的絕大多數(shù)專有代碼。其中有:將新固件鏡像文件直接寫入閃存的自由工具(mtd)、配置無線局域網(wǎng)(wlcompat/wificonf)、通過proc文件系統(tǒng)對支持VLAN的switch(交換機(jī)?)進(jìn)行編程。最初發(fā)布的OpenWrt的代號是“White Russian”,來自于著名雞尾酒的名稱。在OpenWrt發(fā)布0.9版的時候,White Russian的生命周期結(jié)束。 下一個版本的開發(fā)正在我們的subversion(SVN) repository(倉庫)中進(jìn)行。下面一張圖將很清晰的反應(yīng)OpenWrt的版本史。從圖上可以看出最新的穩(wěn)定版本代號為Backfire,早期的穩(wěn)定版本為Kamikaze,開發(fā)版本一直都是trunk。各個版本的官方下載地址為 還存在另一種版本最新測試版代號為Attitude_Adjustment OpenWrt移植(一)下載編譯OpenWrt
1,下載依賴包
在官方trunk版本中已經(jīng)支持pcDuino硬件了。這里只需要配置OpenWrt系統(tǒng),使系統(tǒng)支持pcDuino,具體配置如下: A,配置目標(biāo)系統(tǒng)(Target System) Target System (Allwinner A1x/A20/A3x) —> B,配置目標(biāo)硬件(Target Profile) Target Profile (pcDuino) —> C,配置編譯出來的image,配置rootfs文件系統(tǒng)的格式這里選擇ext4,rootfs文件系統(tǒng)的大小這里設(shè)置(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,選擇編譯交叉編譯器,還有開發(fā)SDK [*] Build the OpenWrt Image Builder [*] Build the OpenWrt SDK E,配置無線網(wǎng)卡,V2/V3都是用的rtl8188cus無線網(wǎng)卡 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-rtlwifi……………………………. Realtek common driver part F, LucI系統(tǒng)快速配置接口 LuCI —> {*} luci
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系統(tǒng) make –j 8 V=s 由于OpenWrt整個系統(tǒng)非常龐大,編譯很慢?!?j 8 “表示用8線程進(jìn)行編譯,”V=s”編譯的時候顯示編譯信息。如果你的電腦是4核建議你用8線程進(jìn)行編譯,雙核建議你使用4線程。這里測試8線程編譯需要一個小時才能編譯完成。 OpenWrt移植(二)構(gòu)建pcDuino BSP 由于OpenWrt的u-boot用的是u-boot-2013的版本,目前只支持SD卡啟動,而且內(nèi)核用的是3.12.5版本。另外我們的3.4.29的內(nèi)核用的是全志fex,而且3.12.5用的是linux官方的kernel使用的是dts設(shè)備樹。這樣的話我們就不能用之前的BSP方案,我們要自己做一個從SD卡啟動的系統(tǒng)。 pcDuino從SD卡啟動順序是A10—>u-boot–>uImage–>OpenWrt. 根據(jù)全志官網(wǎng)的說明,這些軟件都必須放在SD卡固定的地址。那么首先要對A10進(jìn)行分區(qū)。根據(jù)全志芯片的說明,需要對SD卡進(jìn)行下表固定分區(qū)。 1, 先格式化TF卡前面的1M空間,這里是將TF卡通過讀卡器插入到PC的虛擬機(jī)??梢钥闯鯰F卡的設(shè)備是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的生成的二進(jìn)制文件都在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已經(jīng)完全啟動了,上面的時間是編譯的時間。上面的信息還可以看到我們的環(huán)境變量沒有設(shè)置,它使用的是默認(rèn)的環(huán)境變量。前面介紹,系統(tǒng)建立在分區(qū)表不同的地方,但是我們現(xiàn)在SD卡還沒有分區(qū)表,我們需要先建立分區(qū)表再做環(huán)境變量。 3, 建立分區(qū)表。重新把SD卡插回到電腦的虛擬機(jī)里面,使用fdisk創(chuàng)建分區(qū)表。具體的分區(qū)見下操作,步驟的說明請看# 后面的注釋。 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 #創(chuàng)建分區(qū) o create a new empty DOS partition table p print the partition table #查看分區(qū) q quit without saving changes s create a new empty Sun disklabel t change a partition’s system id #改變分區(qū)類型 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 #查看分區(qū) 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 #沒有分區(qū) Command (m for help): n #創(chuàng)建分區(qū) Partition type: p primary (0 primary, 0 extended, 4 free) #主分區(qū) e extended #擴(kuò)展分區(qū) Select (default p): #選擇默認(rèn)主分區(qū) Using default response p Partition number (1-4, default 1): #分區(qū)號為1 Using default value 1 First sector (2048-7866367, default 2048): #選擇默認(rèn)值 Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-7866367, default 7866367): 34815 #這個根據(jù)全志的手冊來第一個分區(qū)必須這么大 Command (m for help): p #查看分區(qū) 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 #創(chuàng)建的第一個分區(qū) Command (m for help): n #再創(chuàng)建一個分區(qū) Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): #主分區(qū) Using default response p Partition number (1-4, default 2): #第二個主分區(qū) Using default value 2 First sector (34816-7866367, default 34816): #默認(rèn)大小從34816開始 Using default value 34816 Last sector, +sectors or +size{K,M,G} (34816-7866367, default 7866367): #默認(rèn)全部分到第二分區(qū) Using default value 7866367 Command (m for help): p #再一次查看分區(qū) 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 #可以看出創(chuàng)建了兩個分區(qū)都為linux類型,但是u-boot只能識別第一個分區(qū)為FAT32分區(qū) Command (m for help): t #修改分區(qū)類型 Partition number (1-4): 1 #選擇修改哪個分區(qū) 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 #再一次查看分區(qū) 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)#已經(jīng)修改過了了 /dev/sdb2 34816 7866367 3915776 83 Linux Command (m for help): w #保存分區(qū)表 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, 格式化分區(qū)。剛才創(chuàng)建了分區(qū),但是沒有格式化,我們還是不能使用。 pillar@monster :~/openwrt/trunk/bin/sunxi$ ls /dev/sdb #查看已經(jīng)分好的分區(qū) sdb sdb1 sdb2 pillar@monster :~/openwrt/trunk/bin/sunxi$ mkf #查看有哪些分區(qū)類型 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 #第一個分區(qū)格式化為fat分區(qū) [sudo] password for pillar: mkfs.vfat 3.0.12 (29 Oct 2011) pillar@monster :~/openwrt/trunk/bin/sunxi$ sudo mkfs.ext4 /dev/sdb2 #第二個分區(qū)格式化為ext4分區(qū),這里需要幾分鐘 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, 掛載分區(qū)。 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環(huán)境變量文件。剛剛創(chuàng)建了分區(qū),這里只需要將環(huán)境變量文件,還有uImage拷貝到第一分區(qū)讓u-boot讀取,就可以引導(dǎo)系統(tǒng)了。下面開始制作u-boot環(huán)境變量文件。 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, 將系統(tǒng)文件拷貝到第一和第二分區(qū)。 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 #第一分區(qū)文件 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 #拷貝第二分區(qū)文件 好了,現(xiàn)在整個的從SD啟動的BSP已經(jīng)最好了。 8, 發(fā)布并燒寫系統(tǒng)?,F(xiàn)在把系統(tǒng)做好了,你可以發(fā)布你制作的系統(tǒng),然后別人可以通過win32diskimager來把你的系統(tǒng)寫入到他的SD卡,他就可以和你一起玩OpenWrt。 pillar@monster :/media$ sudo dd if=/dev/sdc of=OpenWrt.img bs=4M 現(xiàn)在把OpenWrt.img拷貝到windows上,把你新的SD卡插到電腦開始用win32diskimager寫入
1, 讓系統(tǒng)上網(wǎng) vim /etc/config/network config interface ‘net’ option ifname ‘eth0′ option proto ‘dhcp’ 2,設(shè)置固定的mac地址 當(dāng)系統(tǒng)的啟動的時候發(fā)現(xiàn)mac地址老實(shí)在變,這就會出現(xiàn)一個問題,有時候能獲取到ip,有時候獲取不到ip。這里可以做一個系統(tǒng)服務(wù),讓系統(tǒng)開機(jī)保存mac地址,然后再開機(jī)的時候恢復(fù)之前的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>, 指定運(yùn)行的模式 /etc/rc.d/rc則根據(jù)其參數(shù)指定的運(yùn)行模式(運(yùn)行級別,你在inittab文件中可以設(shè)置)來執(zhí)行相應(yīng)目錄下的腳本。凡是以Kxx開頭的,都以stop為參數(shù)來調(diào)用;凡是以Sxx開頭的,都以start為參數(shù)來調(diào)用。調(diào)用的順序按xx 從小到大來執(zhí)行。例如,假設(shè)缺省的運(yùn)行模式是3,/etc/rc.d/rc就會按上述方式調(diào)用 。 由于設(shè)定mac地址要在network之前。所以要創(chuàng)建鏈接: 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密碼是沒有設(shè)置,你需要從serial debug進(jìn)入系統(tǒng)設(shè)置root密碼,設(shè)置方法如下: passwd root 然后在同一個局域網(wǎng)內(nèi)你的PC的瀏覽器上輸入:ethX的ip,這里是192.168.1.119.就會出現(xiàn)下面界面: 輸入你剛才設(shè)置的密碼,進(jìn)入系統(tǒng)管理界面,默認(rèn)是進(jìn)入狀態(tài)標(biāo)簽,這里你可以看到整個系統(tǒng)的運(yùn)行的狀態(tài)。 如果你對當(dāng)前頁面不太習(xí)慣,而且在使用上語言上也有些困難,你可以進(jìn)入system標(biāo)簽,在System Properties里面設(shè)置language and style如下圖所示。設(shè)置完之后save & apply,重新刷新一下瀏覽器就可以使用你設(shè)置的語言和主題。 下面進(jìn)入網(wǎng)絡(luò)標(biāo)簽欄設(shè)置wifi節(jié)點(diǎn),這個部分是openwrt比較復(fù)雜的一個部分,這個部分的設(shè)置直接決定著你的openwrt能不能使用。 添加新接口,選擇靜態(tài)ip,新接口的名稱,你需要用英文自定義一個名字,在包括一下接口里面選擇無線網(wǎng)絡(luò)。設(shè)置完之后提交,進(jìn)入下一個頁面繼續(xù)設(shè)置。 基本設(shè)置設(shè)置完成之后,進(jìn)入防火墻設(shè)置,這里wifi必須選擇為lan口。設(shè)置完成之后保存應(yīng)用。這個時候你電腦就可以連接使用openwrt這個路由器了。 點(diǎn)擊修改后進(jìn)入防火墻設(shè)置標(biāo)簽欄,分配防火墻區(qū)域?yàn)閣an,設(shè)置完成之后保存&應(yīng)用。
1, 清空恢復(fù)上一個全新的內(nèi)核 make target/linux/{clean,prepare} V=s QUILT=1 2, 到內(nèi)核源碼目錄 cd build_dir/target-*/linux-*/linux-3.* 3, 建立git代碼倉庫,當(dāng)然你要事先建立git環(huán)境,如果使用Git請參見附錄一。 git init git add * -f git commit -a -m “first add” 4, 修改你的代碼,這里我給我代碼添加rtl8188cus驅(qū)動 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/* drivers/net/wireless/rtl8192cus/ -rf vim drivers/net/wireless/Kconfig 284 source “drivers/net/wireless/rtl8192cus/Kconfig” vim drivers/net/wireless/rtl8192cus/Kconfig 1 config RTL8192CU_SW 2 tristate “Realtek 8192C USB WiFi for SW” 3 depends on USB 4 select WIRELESS_EXT 5 select WEXT_PRIV vim drivers/net/wireless/Makefile 28 obj-$(CONFIG_RTL8192CU_SW) += rtl8192cus/ vim drivers/net/wireless/rtl8192cus/Makefile 575 obj-$(CONFIG_RTL8192CU_SW) := $(MODULE_NAME).o 579 export CONFIG_RTL8192CU_SW = m 5,建立git分支,并制作補(bǔ)丁 git branch rtl8192 git checkout rtl8192 git add * -f git commit -a -m “add rtl8192cus for pcDuino” git format-patch -M master #會生成0002-add-rtl8192cus-for-pcDuino.patch cp 0001-add-rtl8192cus-for-pcDuino.patch patches/ cd ../../../../ make target/linux/update package/index V=s cp build_dir/target-arm_cortex-a8+vfpv3_uClibc-0.9.33.2_eabi/linux-sunxi/linux-3.12.5/patches/0001-add-rtl8192cus-for-pcDuino.patch target/linux/sunxi/patches-3.12/ 5, 檢測是否生效,執(zhí)行完之后就是rtl8192分支的代碼了 make target/linux/{clean,prepare} V=s QUILT=1 6, 配置內(nèi)核應(yīng)用選項(xiàng) make kernel_menuconfig Device Drivers —> [*] Network device support —> [*] Wireless LAN —> <*> Realtek 8192C USB WiFi 其他的patch的制作方法請參考: http://wiki./doc/devel/patches OpenWrt系統(tǒng)開發(fā)(二)建立App服務(wù)器
OpenWrt通過opkg來管理安裝整個系統(tǒng)的軟件。目前有很多OpenWrt的軟件源,但是哪些都是針對于MIPS平臺的,pcDuino使用的ARM平臺,我們必須自己搭建軟件源。查看了一下MIPS平臺的服務(wù)器,其實(shí)很簡單的,就是一個apache服務(wù)器,而且OpenWrt編譯完成之后,在openwrt/trunk/bin/sunxi/packages下面已經(jīng)生成了軟件源。我們只需要將他們聯(lián)系起來就行了,這里是在我的PC的虛擬機(jī)上搭建的。 # sudo apt-get install apache2 修改https的根目錄 pillar@monster :~/openwrt$ vim /etc/apache2/sites-available/default 4 DocumentRoot /home/pillar/openwrt/trunk/bin/sunxi/ 重啟服務(wù)器使修改過的配置生效 pillar@monster :~/openwrt$ sudo /etc/init.d/apache2 restart 修改pcDuino上面的OpenWrt上面的系統(tǒng)配置 root@OpenWrt :/# vim /etc/opkg.conf src/gz barrier_breaker http://192.168.1.125/packages dest root / dest ram /tmp lists_dir ext /var/opkg-lists option overlay_root /overlay 上面的IP為我們電腦虛擬機(jī)的IP,下面更新一下軟件源。 $opkg update opkg常用命令: opkg update 更新可以獲取的軟件包列表 opkg upgrade 對已經(jīng)安裝的軟件包升級 opkg list 獲取軟件列表 opkg install 安裝指定的軟件包 opkg remove 卸載已經(jīng)安裝的指定的軟件包 OpenWrt系統(tǒng)開發(fā)(三)如何使用Git
1, 什么是GIT Git是一個強(qiáng)調(diào)速度的分布式版本控制軟件和源代碼管理系統(tǒng)(SCM,source code management)。Git最初是由Linus Torvalds為內(nèi)核開發(fā)而設(shè)計(jì)的管理軟件。自從Git推出以來,已經(jīng)被很多開源項(xiàng)目所采納。每一個Git工作目錄是一個帶有完全歷史記錄和版本信息的倉庫,不依賴于網(wǎng)絡(luò)和中央服務(wù)器。Git是一個免費(fèi)的開源軟件,遵從GNU v2協(xié)議。 Git這個詞在英語中的原意是很笨拙,沒用的人。Linus自嘲說:“我是一個任性的笨蛋,所以我把我的所有的項(xiàng)目的名字都和我很相似。第一個是Linux,現(xiàn)在是Git。”Git的幫助文檔中描述Git為:笨拙的內(nèi)容跟蹤者(the stupid content tracker)。 2, 為什么要用GIT ü 更順暢的工作流程,開發(fā)過程中,完全可以離線操作 ü 快速,Git分布式架構(gòu)使得本地倉庫包含所有的歷史版本信息,你可以在不同的版本之間快速切換 ü 彈性的本地分支,在svn下,你建一個分支需要把源代碼復(fù)制到另外一個文件夾,而在Git下,創(chuàng)建分支的代價是非常小的,只需一條命令 ü 倉庫目錄結(jié)構(gòu)簡潔,用Git復(fù)制一個項(xiàng)目,只會在項(xiàng)目根目錄創(chuàng)建一個.git的目錄,而其他目錄很干凈 ü 內(nèi)容按元數(shù)據(jù)方式存儲,所有的版本信息都位于.git目錄下 ü 完整性好,更易于協(xié)作開發(fā) ü 用戶群大,現(xiàn)在已經(jīng)有成千上萬個開源項(xiàng)目采用Git來做項(xiàng)目管理,github上更是有無數(shù)個代碼倉庫 3, git 安裝 #sudo apt-get install git git-core 4, 第一次使用git。在你安裝好Git之后,你需要修改一些配置,才能正常使用Git。 Git通過“git config”命令來配置Git,這個命令有2個選項(xiàng):–system, –global, 加上默認(rèn)選項(xiàng),分別對應(yīng)Git上3級配置文件。第一個是/etc/gitconfig文件,和–system對應(yīng),這是全局配置文件,修改這個文件,將會影響系統(tǒng)上所有的用戶,所有的倉庫。第二個是你家目錄下的/.gitconfig文件,與–global對應(yīng),修改它會對你當(dāng)前用戶的所有倉庫產(chǎn)生影響。第三個是你倉庫中的.git/.gitconfig文件,這是“git config”默認(rèn)修改的配置文件,它只會對你當(dāng)前倉庫產(chǎn)生影響。 在第一次使用Git時,你需要告訴你的協(xié)同開發(fā)者,你是誰以及你的郵箱,在你提交的時候,Git需要這兩個信息。具體通過以下命令設(shè)置: #git config –global user.name “Pillar Zuo” #git config –global user.email “pillar.zuo@qq.com” 當(dāng)然你也可以不用–global選項(xiàng),但這意味這你在每一個倉庫中都要這樣設(shè)置。 同時,你也可以指定你的編輯器,你的Diff工具: #git config –global core.editor vim #git config –global merge.tool vimdiff 你還可以通過”git config –list”命令來查看你的設(shè)置。 pillar@monster :~$ git config –list user.name=Pillar user.email=pillar.zuo@qq.com credential.helper=cache –timeout 86400 github.user=Pillar1989 當(dāng)你把Git設(shè)置好之后,如果你要和從Git服務(wù)器上獲得倉庫,或者向Git服務(wù)器提交你的代碼(比如github),你可能需要生成你自己的ssh密鑰對。Git支持4種與服務(wù)器端通信的協(xié)議:git、http、ssh和https。其中g(shù)it只是一個只讀協(xié)議,也就是說你只可以從服務(wù)器端獲取倉庫,但是你不能提交你自己的代碼。而http和https用的很少,大部分都只支持ssh協(xié)議和Git協(xié)議。 當(dāng)你通過ssh協(xié)議與遠(yuǎn)端服務(wù)器進(jìn)行通信的時候,你可以通過以下命令生成ssh密鑰對: ssh-keygen -t rsa 如果你沒有指定密鑰名稱和存放路徑的話,它默認(rèn)把兩個不對稱密鑰放在你的家目錄下的.ssh目錄下,密鑰文件默認(rèn)名稱為id_rsa和id_rsa.pub,前者是私鑰,后者是公鑰。中間可能會要你設(shè)定訪問密鑰密碼,這個可以設(shè),可以不設(shè),但為了安全考慮,還是建議你設(shè)一個訪問密碼。否則,意味著任何持有你密鑰的人都可以使用該密鑰。 然后把你的公鑰發(fā)給Git倉庫管理員,然后你就可以通過ssh協(xié)議來訪問服務(wù)器端,期間程序會自動進(jìn)行密鑰對匹配,如果你設(shè)了訪問密碼,你可能需要輸入密碼。 這些設(shè)定完之后,你可以通過獲得任何一個公開的代碼倉庫來檢測你的git是否工作正常。比如下面這個: #git clone git://git2.kernel.org/pub/scm/git/git.git 4、GIT倉庫 Git做為一個資源管理和跟蹤系統(tǒng),如果想要把自己的文件托管在Git上,那么首先你得讓Git知道你需要管理的文件在哪。比如說現(xiàn)在我有一個項(xiàng)目,它在test文件夾里,我想讓Git管理這個項(xiàng)目,這個時候你需進(jìn)入到這個目錄,然后運(yùn)行“git init”命令。這個時候Git就會在該目錄下生成一個.git的隱藏目錄,Git用來進(jìn)行版本控制和內(nèi)容跟蹤的所有文件都在該文件夾下。 處于git跟蹤下的文件只具有三種狀態(tài): Modified(working directory):被修改過的文件 Staged(staging area):通過git add添加到暫存區(qū)域的文件 Committed(git directory):通過git commit提交到倉庫的文件 所以,一般的git工作流程可能是這樣:修改過某些文件,然后把這些文件添加都暫緩區(qū),再提交到倉庫中形成一個版本或快照,最后提交到git服務(wù)器上。而在中間,可能伴隨著分支管理,分支切換,撤消與合并。 可能有些人會覺得很奇怪,為什么git會有暫存區(qū)域這個概念,直接提交到倉庫中不就ok了。其實(shí)這是git為了做版本控制用的,試想如果沒有暫存區(qū)域,每修改一個文件,就會形成一個版本,太過頻繁,不易于管理。暫存區(qū)域其實(shí)就是下一個版本的文件清單,你可以自由控制該往倉庫中提交什么文件,這也可以避免在一個版本中包含一些中間文件,比如編譯后的文件。具體的怎么做,例如,你可以在你的代碼下面建一個隱藏文件為.gitignore 里面的內(nèi)容就是你需要過濾的文件,這里提供一個OpenWrt里面linux-3.12.5的過濾文件。 1 # 2 # NOTE! Don’t add files that are generated in specific 3 # subdirectories here. Add them in the “.gitignore” file 4 # in that subdirectory instead. 5 # 6 # NOTE! Please use ‘git ls-files -i –exclude-standard’ 7 # command after changing this file, to see if there are 8 # any tracked files which get ignored after the change. 9 # 10 # Normal rules 11 # 12 .* 13 *.o 14 *.o.* 15 *.a 16 *.s 17 *.ko 18 *.so 19 *.so.dbg 20 *.mod.c 21 *.i 22 *.lst 23 *.symtypes 24 *.order 25 modules.builtin 26 *.elf 27 *.bin 28 *.gz 29 *.bz2 30 *.lzma 31 *.xz 32 *.lz4 33 *.lzo 34 *.patch 35 *.gcno 36 37 # 38 # Top-level generic files 39 # 40 /tags 41 /TAGS 42 /linux 43 /vmlinux 44 /vmlinuz 45 /System.map 46 /Module.markers 47 /Module.symvers 48 49 # 50 # Debian directory (make deb-pkg) 51 # 52 /debian/ 53 54 # 55 # git files that we don’t want to ignore even it they are dot-files 56 # 57 !.gitignore 58 !.mailmap 59 60 # 多的,我就不再一一列出了,可以看出里面每一行都代表一種過濾方式,用通配符*代替一些文件。 5, git倉庫使用的基本流程 1> 初始化git倉庫 初始化倉庫有兩種情況,一種是直接在一個空目錄里建立一個項(xiàng)目,這時候你可以這樣干: #git init 另一種是從其他機(jī)器復(fù)制一個倉庫,比如這樣: #git clone git://git2.kernel.org/pub/scm/git/git.git (遠(yuǎn)程倉庫) #git clone /home/oss/test.git (本地倉庫) 第一次從服務(wù)器上復(fù)制一個倉庫,可能比較慢,因?yàn)間it要把所有的歷史記錄和版本全部復(fù)制下來,這也算git的一個弊端吧! 復(fù)制完后,就會在當(dāng)前目錄下生成一個工作目錄,名字以倉庫名字命名。如果你不想指定目錄,那就在上面的命令后加一個目錄就ok了。比如我想把test倉庫放到oss倉庫中: #git clone /home/oss/test.git oss 之后,你就可以開始你的工作啦! 2> 添加文件 在編輯了幾個文檔之后,你可能突然想起來,好像文件還沒有讓git跟蹤。Git并不會實(shí)時的跟蹤你的文件,只在你明確讓它記錄你的文件時,它才會把指定的文件的當(dāng)前狀態(tài)記錄到倉庫中去,然后又撒手不管了。我想這就是說git笨的原因吧。這個時候,你需要手動添加你的文件當(dāng)暫存區(qū)域: #git add filename1 filename2 如果你懶得一個一個加,你可以試試這個: #git add -A 它會把當(dāng)前目錄下所有的文件都添加到暫存區(qū)域。 3>推送更變 在你commit完之后,你可能想把自己的代碼提交到github或者其他git服務(wù)器上,與他人交流共享,這時候就需要和遠(yuǎn)程服務(wù)器打交道了。 如果你是在本地建立起的倉庫,默認(rèn)情況下是沒有任何服務(wù)器地址的,如果你是從其他服務(wù)器復(fù)制過來的倉庫,這個服務(wù)器地址會自動添加到你的倉庫中,你可以這樣查看: pillar@monster :~/u-boot-sunxi$ git remote -v origin https://github.com/linux-sunxi/u-boot-sunxi.git (fetch) origin https://github.com/linux-sunxi/u-boot-sunxi.git (push) 如果只輸入”git remote”,就只會列出服務(wù)器端的別名,不會列出地址來。 一個倉庫可以有多個服務(wù)器地址,這就意味著,你可以從不同的人手中復(fù)制同一個倉庫,但這并不會打亂你自己的分支,哪怕雙方的分支名字都一樣。假如你現(xiàn)在在和另外兩個人做同一個項(xiàng)目中的同一個分支,你發(fā)現(xiàn)A的一個模塊正是你想要的,你想把他的代碼合并到你現(xiàn)在的版本中,這時候你可以這樣做: #git remote add code_a git://url/test.git //添加對方的地址,code_a是別名 #git fetch code_a //復(fù)制對方的倉庫到本地,但不合并,git pull會自動合并 #git merge code_a/master //把對方master分支合并到自己當(dāng)前版本下 合并完之后,你可能想提交你的代碼到其他的服務(wù)器上,這時候你可以先把要提交的服務(wù)器地址添加進(jìn)來,然后這樣做: #git push origin master 上面的命令就是把自己master的分支提交到名字為origin的服務(wù)器上。 3> 創(chuàng)建并管理分支 在做項(xiàng)目的時候,你可能會想寫一些擴(kuò)展性的功能,或者做一些小實(shí)驗(yàn),但是你又不想影響你現(xiàn)在的項(xiàng)目。這時候,你可以創(chuàng)建一個分支,然后在這個分支里寫東西,當(dāng)覺得不好的時候,你可以把這個分支刪除掉,對你之前的主分支沒有任何影響?;蛘吣阌X得這個新特性超出了自己的預(yù)想,可以合并到主分支里,這時候你只要把工作轉(zhuǎn)回主分支,然后合并分支,最后刪除分支,然后就跟那個分支沒創(chuàng)建一樣。具體操作如下: git branch test //創(chuàng)建一個test分支 git checkout test //轉(zhuǎn)到test分支 edit something…commit something… git checkout master //轉(zhuǎn)到master分支 git merge test //合并test分支 git checkout -b test2 //創(chuàng)建test2分支,并轉(zhuǎn)到test2分支 git branch -d test //刪除test分支 git branch //列出分支列表 git branch -v //列出分支列表和當(dāng)前commit Git merge的實(shí)質(zhì)是把兩個版本合在一起,然后在當(dāng)前分支創(chuàng)建一個新的commit,如果你在兩個分支的同一個文件的同一個地方都做了修改,這時候merge就會失敗,git就不會自動創(chuàng)建一個新的commit,而是直接停住。你需要手動修改這些沖突的文件,選擇這兩個分支中的一個版本,或者自己重寫這個部分,然后手動添加這些文件到暫存區(qū)域,再commit一下就ok了。要查看哪些文件沖突了,可以用”git status”查看。 6>撤消改動 是人就會犯錯。當(dāng)你執(zhí)行某個命令之后,突然發(fā)現(xiàn),自己寫錯了,或者漏了一個文件,這時候怎么辦? 如果你提交得太早,忘了添加某些文件,你可以這樣做: git commit -m ‘a(chǎn)dd something ’ git add file1 git commit –amend 最后一個命令會把你當(dāng)前暫存區(qū)域最為上一次的commit。如果你commit以后,馬上amend,這時候git會直接跳到編輯commit備注里面,這樣你可以修改你上次commit的備注。 如果你添加了不該添加的文件,你可以這樣挽回: git add . //把所有的文件都添加進(jìn)去 git reset HEAD readme //把readme文件從暫存區(qū)域去除 如果你發(fā)現(xiàn)你編輯錯了一個文件,你想把它恢復(fù)到上一個版本的狀態(tài),這時候你可以這樣: git checkout — filename1 //只撤消這一個文件 如果你覺得這個版本糟糕透了,想完全回滾到上一個版本,你可以干如下事情: git reset –hard HEAD^ HEAD是指向當(dāng)前版本,^指當(dāng)前版本的父版本,這個操作無法撤消。你可以把–hard換成–soft,這只會回退commit信息。還有一個–mixed默認(rèn)選項(xiàng),大家可以參考官方文檔,查看這3個選項(xiàng)的具體區(qū)別。 6, git常用命令 Git add Git clone Git commit Git push Git checkout Git reset Git pull Git status Git branch OpenWrt系統(tǒng)開發(fā)(四)應(yīng)用程序開發(fā)
OpenWrt上面應(yīng)用程序開發(fā)有兩種方式,一種是利用OpenWrt SDK,一種是利用OpenWrt源碼。這里主要介紹利用OpenWrt源碼,進(jìn)行開發(fā)應(yīng)用程序,制作成ipk軟件可以安裝。 1,進(jìn)入package目錄,創(chuàng)建軟件目錄 #cd /home/pillar/openwrt/trunk/package #mkdir example1 2,進(jìn)入example1目錄,創(chuàng)建Makefile文件和代碼路徑 #cd example1 #touch Makefile #mkdir src 1, 該Makefile具體內(nèi)容如下: #User mode tool example include $(TOPDIR)/rules.mk include $(INCLUDE_DIR)/kernel.mk PKG_NAME:=example1 PKG_RELEASE:=1 PKG_BUILD_DIR := $(KERNEL_BUILD_DIR)/$(PKG_NAME) include $(INCLUDE_DIR)/package.mk define Package/example1 SECTION:=utils CATEGORY:=Base system TITLE:=Build for example1 commands endef define Package/example1/description This package contains an utility useful to use example1 commands. endef define Build/Prepare mkdir -p $(PKG_BUILD_DIR) $(CP) ./src/* $(PKG_BUILD_DIR)/ endef target=$(firstword $(subst -, ,$(BOARD))) MAKE_FLAGS += TARGET=”$(target)” TARGET_CFLAGS += -Dtarget_$(target)=1 -Wall define Build/example1/compile $(MAKE) -C “$(LINUX_DIR)” \ CROSS_COMPILE=”$(TARGET_CROSS)” \ ARCH=”$(LINUX_KARCH)” \ SUBDIRS=”$(PKG_BUILD_DIR)” \ EXTRA_CFLAGS=”$(BUILDFLAGS)” endef define Package/example1/install $(INSTALL_DIR) $(1)/sbin $(INSTALL_BIN) $(PKG_BUILD_DIR)/example1 $(1)/sbin/ endef $(eval $(call BuildPackage,example1)) 3.進(jìn)入src目錄,創(chuàng)建相關(guān)源文件 cd src touch example1.c Makefile example1.c 具體內(nèi)容如下 #include int main(void) { printf(“Hello, world\n”); return 0; } Makefile文件具體內(nèi)容如下: .NOTPARALLEL: #OCTEON_ROOT=$(PWD)/src/ CC=~/openwrt/main/staging_dir/toolchain-mips64_gcc-4.4.1_eglibc-2.10.1/usr/bin/mips64-openwrt-linux-gnu-gcc CFLAGS=-mips64r2 -mabi=64 -march=octeon -mtune=octeon LFLAGS= .PHONY: all all: example1 example1:example1.c ${CC} ${CFLAGS} ${LFLAGS} -W -g -Wall -Wno-unused-parameter -DUSE_RUNTIME_MODEL_CHECKS=1 \ -o $@ example1.c 4.回到主路徑 /home/pillar/openwrt/trunk /,編譯選項(xiàng)配置保存并編譯 make menuconfig Base system —> example1 選項(xiàng)設(shè)置為M,保存退出 然后編譯該模塊: make package/example1/compile 5,更新package make package/ example1/install make package/index OpenWrt系統(tǒng)開發(fā)(五)內(nèi)核驅(qū)動開發(fā)
OpenWrt開發(fā)內(nèi)核驅(qū)動有多種方式,前面講到的制作內(nèi)核補(bǔ)丁也是一種開發(fā)方法。這里介紹直接在OpenWrt系統(tǒng)上開發(fā)內(nèi)核驅(qū)動,把內(nèi)核驅(qū)動做成ipk軟件包的形式。 1, 建立工作目錄 cd openwrt/trunk/package mkdir example 2, 進(jìn)入example目錄,創(chuàng)建Makefile文件和代碼路徑 cd example mkdir src vim Makefile # Kernel module example include $(TOPDIR)/rules.mk include $(INCLUDE_DIR)/kernel.mk PKG_NAME:=example PKG_RELEASE:=1 include $(INCLUDE_DIR)/package.mk define KernelPackage/example SUBMENU:=Other modules DEPENDS:=@TARGET_octeon TITLE:=Support Module for example AUTOLOAD:=$(call AutoLoad,81,example) FILES:=$(PKG_BUILD_DIR)/example/example.$(LINUX_KMOD_SUFFIX) endef define Build/Prepare mkdir -p $(PKG_BUILD_DIR) $(CP) -R ./src/* $(PKG_BUILD_DIR)/ endef define Build/Compile $(MAKE) -C “$(LINUX_DIR)” \ CROSS_COMPILE=”$(TARGET_CROSS)” \ ARCH=”$(LINUX_KARCH)” \ SUBDIRS=”$(PKG_BUILD_DIR)/example” \ EXTRA_CFLAGS=”-g $(BUILDFLAGS)” \ modules endef $(eval $(call KernelPackage,example)) 3, 進(jìn)入src目錄,創(chuàng)建代碼路徑和相關(guān)源文件 cd src mkdir example cd example vim example.c #include #include #include /* hello_init —- 初始化函數(shù),當(dāng)模塊裝載時被調(diào)用,如果成功裝載返回0 否則返回非0值 */ static int __init hello_init(void) { printk(“I bear a charmed life.\n”); return 0; } / * hello_exit —- 退出函數(shù),當(dāng)模塊卸載時被調(diào)用 */ 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 選項(xiàng)設(shè)置為M,保存退出 然后編譯該模塊: make package/example/compile make package/index 5,在OpenWrt系統(tǒng)里面就可以用opkg下載使用了。 OpenWrt系統(tǒng)開發(fā)(六)使用OpenWrt SDK
OpenWrt為了避免每次都重新編譯系統(tǒng),引入了SDK機(jī)制。我們在發(fā)布系統(tǒng)的時候也需要發(fā)布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 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,修改軟件源根目錄。如果你不想每次都拷貝,你可以把軟件源的根目錄下設(shè)置在 OpenWrt-SDK-sunxi-for-linux-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2/bin/sunxi/packages 然后你可以在OpenWrt系統(tǒng)里面下載安裝。 |
|
|