[メモ] Raspberry Pi2 - buildrootで、wifiを使うまで。

# [メモ] Raspberry Pi 2で、buildrootを使う

- Buildは、Ubuntu 14.04(x86_64)
- Raspberry Pi 2
- UART console (UARTケーブルでWinと接続、TeraTermでコンソール使用)
- `buildroot-2015.11.1`
- Realtek8192cuベースのUSBドングル

## ダウンロード、展開、デフォルトのコンフィグレーション

```
wget  http://buildroot.uclibc.org/downloads/buildroot-2015.11.1.tar.bz2
tar xf ./buildroot-2015.11.1.tar.bz2
cd buildroot-2015.11.1
make raspberrypi2_defconfig
```
> `make raspberrypi_defconfig` で、RasberryPi1用ができるハズ。


### Configuration(buildroot)
`% make menuconfig`

- UARTを使う。(ttyAMA0)
  - `System configuration  --->` => `[*] Run a getty (login prompt) after boot  ---> ` から

```text
  │ │               --- Run a getty (login prompt) after boot                                            │ │  
  │ │               (ttyAMA0) TTY port                                                                   │ │  
  │ │                     Baudrate (115200)  --->                                                        │ │  
  │ │               (vt100) TERM environment variable                                                    │ │  
  │ │               ()    other options to pass to getty                                                 │ │  
```

- initramfsはカーネルに含めてしまう。
  - `Filesystem images  ---> `から

```
  │ │               [ ] axfs root filesystem                                                             │ │  
  │ │               [ ] cloop root filesystem for the target device                                      │ │  
  │ │               -*- cpio the root filesystem (for use as an initial RAM filesystem)                  │ │  
  │ │                     Compression method (bzip2)  --->                                               │ │  
  │ │               [ ]   Create U-Boot image of the root filesystem                                     │ │  
  │ │               [ ] cramfs root filesystem                                                           │ │  
  │ │               [ ] ext2/3/4 root filesystem                                                         │ │  
  │ │               [*] initial RAM filesystem linked into linux kernel                                  │ │  
  │ │               [ ] jffs2 root filesystem                                                            │ │  
  │ │               [ ] romfs root filesystem                                                            │ │  
  │ │               [ ] squashfs root filesystem                                                         │ │  
  │ │               [ ] tar the root filesystem                                                          │ │  
  │ │               [ ] ubifs root filesystem                                                            │ │  
  │ │               [ ] yaffs2 root filesystem                                                           │ │  
```

### Configuration (linux kernel)
`% make linux-menuconfig`
=> カーネルの設定ができる

### make ... 待つ。
- binutilsとか、gccとかダウンロードしてきて、ビルドする。
- => `buildroot-2015.11.1-1/output/images/` フォルダの下に生成物ができる。

```
% tree buildroot-2015.11.1/output/images/
buildroot-2015.11.1/output/images/
├── bcm2709-rpi-2-b.dtb
├── rootfs.cpio
├── rootfs.cpio.bz2
├── rpi-firmware
│   ├── bootcode.bin
│   ├── cmdline.txt
│   ├── config.txt
│   ├── fixup.dat
│   └── start.elf
└── zImage
```

### 起動SDカードの準備
- [SDカードフォーマッター](https://www.sdcard.org/jp/downloads/formatter_4/)でフォーマット。=>FAT(FAT32)のみでOK.
- SDカードにイメージをコピー
  - `rpi-firmware/*` を SDのルートへ
  - `bcm2709-rpi-2-b.dtb` を SDのルートへ
  - `zImage` を SDのルートへ
  - `rootfs.cpio`と`rootfs.cpio.bz2`は<font color='red'>コピーしない</font>

```
SD:/
├── bcm2709-rpi-2-b.dtb
├── bootcode.bin
├── cmdline.txt
├── config.txt
├── fixup.dat
├── start.elf
└── zImage

```
- `cmdline.txt`を編集
  - consoleをUARTへ出力。
  - 起動後のルートは、カーネルにくっついてるイメージがramに展開されて、使用されるので、`root=`は指定してない。

```cmdline.txt
console=ttyAMA0,115200
```
 


###　実機でブート
- Windows機にUARTでつないで、TeraTerm-115200bpsで待機。
- SDをいれて、PowerON
- `root` パスなしで、ログイン。

```
ncompressing Linux... done, booting the kernel.
[    0.000000] Booting Linux on physical CPU 0xf00
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct
[    0.000000] Linux version 4.1.5-v7 (user01@adas-sandbox) (gcc version 4.9.3 (Buildroot 2015.11.1) ) #2 SMP PREEMPT Fri Dec 18 16:39:25 PST 2015
[    0.000000] CPU: ARMv7 Processor [410fc075] revision 5 (ARMv7), cr=10c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: BCM2709
...
...
[    3.622694] uart-pl011 dev:f1: no DMA platform data
[    3.631491] mmc0: host does not support reading read-only switch, assuming write-enable
[    3.641587] mmc0: new high speed SDHC card at address 0001
[    3.647762] mmcblk0: mmc0:0001 00000 29.8 GiB
[    3.652824] Freeing unused kernel memory: 15264K (80788000 - 81670000)
[    3.654373] Indeed it is in host mode hprt0 = 00021501
[    3.664260]  mmcblk0: p1
Starting logging: OK
Initializing random number generator... [    3.746711] random: dd urandom read with 4 bits of entropy available
done.
Starting network...
[    3.834216] usb 1-1: new high-speed USB device number 2 using dwc_otg
[    3.840852] Indeed it is in host mode hprt0 = 00001101

Welcome to Buildroot
buildroot login: [    4.044583] usb 1-1: New USB device found, idVendor=0424, idProduct=9514
[    4.051291] usb 1-1: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[    4.059347] hub 1-1:1.0: USB hub found
[    4.063217] hub 1-1:1.0: 5 ports detected
[    4.344194] usb 1-1.1: new high-speed USB device number 3 using dwc_otg
[    4.464528] usb 1-1.1: New USB device found, idVendor=0424, idProduct=ec00
[    4.471404] usb 1-1.1: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[    4.481756] smsc95xx v1.0.4
[    4.548378] smsc95xx 1-1.1:1.0 eth0: register 'smsc95xx' at usb-bcm2708_usb-1.1, smsc95xx USB 2.0 Ethernet, b8:27:eb:e0:02:5a

Welcome to Buildroot
buildroot login: root
# ls -l /
total 4
drwxr-xr-x    2 root     root          1420 Dec 19  2015 bin
drwxr-xr-x    7 root     root          2560 Jan  1 00:00 dev
drwxr-xr-x    6 root     root           460 Jan  1 00:00 etc
-rwxr-xr-x    1 root     root           176 Dec 19  2015 init
drwxr-xr-x    4 root     root           600 Dec 19  2015 lib
lrwxrwxrwx    1 root     root             3 Dec 19  2015 lib32 -> lib
lrwxrwxrwx    1 root     root            11 Dec 19  2015 linuxrc -> bin/busybox
drwxr-xr-x    2 root     root            40 Dec  5  2015 media
drwxr-xr-x    2 root     root            40 Dec  5  2015 mnt
drwxr-xr-x    2 root     root            40 Dec  5  2015 opt
dr-xr-xr-x   67 root     root             0 Jan  1 00:00 proc
drwx------    2 root     root            60 Jan  1 00:00 root
drwxr-xr-x    3 root     root           140 Jan  1 00:00 run
drwxr-xr-x    2 root     root          1040 Dec 19  2015 sbin
dr-xr-xr-x   11 root     root             0 Jan  1 00:00 sys
drwxrwxrwt    2 root     root            60 Jan  1 00:00 tmp
drwxr-xr-x    6 root     root           140 Dec 19  2015 usr
drwxr-xr-x    4 root     root           200 Dec 19  2015 var
#
```

### 追加パッケージ
`% make menuconfig`から、`Target packages  --->`へ、いくとパッケージの追加ができる。

### wifi を使う。
- Realtek8192cuベースのUSBドングル
- wifi関連のtoolをいれる。<br>
- `Target packages  --->`　`Networking applications  --->` から、`iw`,` wireless tools`, `wpa_supplicant`関連を選択。

```
  | |               [ ] iputils                                                                          | |   
  | |                   *** irssi needs a toolchain w/ wchar, threads ***                                | |   
  | |               [*] iw                                                                               | |   
  | |                   *** janus-gateway needs a toolchain w/ dynamic library, threads, wchar ***       | |   
  | |               [ ] kismet                                                                           | |   
...     
...     
  | |               [*] wireless tools                                                                   | |   
  | |               [ ]   Install shared library                                                         | |   
  | |                   *** wireshark needs a toolchain w/ wchar, threads ***                            | |   
  | |               [*] wpa_supplicant                                                                   | |   
  | |               [*]   Enable nl80211 support                                                         | |   
  | |               [*]   Enable AP mode                                                                 | |   
  | |               [*]   Enable EAP                                                                     | |   
  | |               [*]   Enable HS20                                                                    | |   
  | |               [*]   Enable syslog support                                                          | |   
  | |               [*]   Enable WPS                                                                     | |   
  | |               [*]   Install wpa_cli binary                                                         | |   
  | |               [*]   Install wpa_passphrase binary                                                  | |   
  | |               [ ] wvdial                                                                           | |   

```

- `make`したら、`zImage`のみSDへコピーする。
 
1. モジュールのロード `/lib/modules/`から検索。
2. `ifconfig wlan0 up`して、`iwlist scan`
3. `wpa_passphrase`で、`/etc/wpa_supplicant.conf`に情報追加。
4. `wpa_supplicant`で、接続
5. `udhcp`で ip取得

```shell-session
Welcome to Buildroot
buildroot login: root

# find . /lib/modules/ | grep 8192cu.ko
/lib/modules/4.1.5-v7/kernel/drivers/net/wireless/rtl8192cu/8192cu.ko
# insmod /lib/modules/4.1.5-v7/kernel/drivers/net/wireless/rtl8192cu/8192cu.ko
[   80.737115] usbcore: registered new interface driver rtl8192cu
# lsmod
Module                  Size  Used by    Not tainted
8192cu                509748  0
# iwlist scan
wlan0     No scan results

lo        Interface doesn't support scanning.

eth0      Interface doesn't support scanning.

# ifconfig wlan0 up
# iwlist scan
...ずらずらでてくる...

# wpa_passphrase 'SSID' 'Password' >> /etc/wpa_supplicant.conf

# wpa_supplicant -B w -D wext -i wlan0 -c /etc/wpa_supplicant.conf -dd
...ずらずらでてくる...

# iwconfig
wlan0     IEEE 802.11bgn  ESSID:"SSIDがでてる"  Nickname:"<WIFI@REALTEK>"

# udhcpc -i wlan0
udhcpc (v1.24.1) started
Sending discover...
...ずらずら...

# ping www.google.com
PING www.google.com (74.125.239.48): 56 data bytes
64 bytes from 74.125.239.48: seq=0 ttl=44 time=17.263 ms
64 bytes from 74.125.239.48: seq=1 ttl=44 time=44.928 ms
64 bytes from 74.125.239.48: seq=2 ttl=44 time=11.501 ms
^C
--- www.google.com ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 11.501/24.564/44.928 ms
#
```