# 2019q1 Homework4 (riscv)
contributed by < `tony2037` >
###### tags: `riscv`
# Set up tinyemu
- [ ] Makefile
```
install:
sudo apt install libsdl2-dev libssl-dev libcurl4-openssl-dev
git clone https://github.com/sysprog21/riscv-emu.git
```
但是這邊我是直接從[官方網站](https://bellard.org/tinyemu/)下載
- [ ] Result
```shell=
$>temu
temu version 2019-02-10, Copyright (c) 2016-2018 Fabrice Bellard
usage: riscvemu [options] config_file
options are:
-m ram_size set the RAM size in MB
-rw allow write access to the disk image (default=snapshot)
-ctrlc the C-c key stops the emulator instead of being sent to the
emulated software
-append cmdline append cmdline to the kernel command line
Console keys:
Press C-a x to exit the emulator, C-a h to get some help.
```
- [ ] 將啟動寫進 `Makefile` 以後方便多
```
TEMU = temu
activate:
echo "Press CTRL+A release then X to exit"
$(TEMU) https://bellard.org/jslinux/buildroot-riscv64.cfg
```
# Download diskimage
- [ ] 老師這邊給了下載的 url
```
get-image:
wget https://bellard.org/tinyemu/diskimage-linux-riscv-2018-09-23.tar.gz
tar zxvf diskimage-linux-riscv-2018-09-23.tar.gz
$(RM) diskimage-linux-riscv-2018-09-23.tar.gz
```
- [ ] Result
```
$> temu root-riscv64.cfg
[ 0.186079] NET: Registered protocol family 17
[ 0.186360] 9pnet: Installing 9P2000 support
[ 0.188580] EXT4-fs (vda): couldn't mount as ext3 due to feature incompatibilities
[ 0.189088] EXT4-fs (vda): mounting ext2 file system using the ext4 subsystem
[ 0.193411] EXT4-fs (vda): mounted filesystem without journal. Opts: (null)
[ 0.193571] VFS: Mounted root (ext2 filesystem) on device 254:0.
[ 0.193974] devtmpfs: mounted
[ 0.194201] Freeing unused kernel memory: 80K
[ 0.194291] This architecture does not have kernel memory protection.
~ #
```
# Buildroot
根據 [TinyEMU RISC-V Buildroot](https://bellard.org/tinyemu/buildroot.html) 設置環境
According to [wiki](https://en.wikipedia.org/wiki/Buildroot):
Buildroot is a set of Makefiles and patches that simplifies and automates the process of building a complete and bootable Linux environment for an embedded system, while using cross-compilation to allow building for multiple target platforms on a single Linux-based development system. Buildroot can automatically build the required cross-compilation toolchain, create a root file system, compile a Linux kernel image, and generate a boot loader for the targeted embedded system, or it can perform any independent combination of these steps. For example, an already installed cross-compilation toolchain can be used independently, while Buildroot only creates the root file system.
:::info
用來建置 Linux environment for for embedded system, using cross-compilation.
支援大量 CPU 指令集及架構
:::
## 使用 `make menuconfig` 配置設定

可[參考](https://www.youtube.com/watch?v=84T-P5ye864)
## 修改 *E2FSPROGS* 版本號

## 將 `kilo` 加入 buildroot
可以從 `menu` 的 `Target packages> Text editors and viewer` 看到原本是沒有 `kilo` editor

參考 [documentation](https://buildroot.org/downloads/manual/manual.html#adding-packages)
* 17.1 says: First of all, **create a directory** under the package directory for your software, for example libfoo.
```shell=
cd package
mkdir kilo
```
* 17.2: There are two types: **Config.in** and **Config.in.host**
* [17.23.4. How to add a package from GitHub](https://buildroot.org/downloads/manual/manual.html#github-download-url) 提到如何從 github 新增包
* [17.5. Infrastructure for packages with specific build systems](https://buildroot.org/downloads/manual/manual.html#github-download-url): 提到如何寫 `.mk`
```
01: ################################################################################
02: #
03: # libfoo
04: #
05: ################################################################################
06:
07: LIBFOO_VERSION = 1.0
08: LIBFOO_SOURCE = libfoo-$(LIBFOO_VERSION).tar.gz
09: LIBFOO_SITE = http://www.foosoftware.org/download
10: LIBFOO_LICENSE = GPL-3.0+
11: LIBFOO_LICENSE_FILES = COPYING
12: LIBFOO_INSTALL_STAGING = YES
13: LIBFOO_CONFIG_SCRIPTS = libfoo-config
14: LIBFOO_DEPENDENCIES = host-libaaa libbbb
15:
16: define LIBFOO_BUILD_CMDS
17: $(MAKE) $(TARGET_CONFIGURE_OPTS) -C $(@D) all
18: endef
19:
20: define LIBFOO_INSTALL_STAGING_CMDS
21: $(INSTALL) -D -m 0755 $(@D)/libfoo.a $(STAGING_DIR)/usr/lib/libfoo.a
22: $(INSTALL) -D -m 0644 $(@D)/foo.h $(STAGING_DIR)/usr/include/foo.h
23: $(INSTALL) -D -m 0755 $(@D)/libfoo.so* $(STAGING_DIR)/usr/lib
24: endef
25:
26: define LIBFOO_INSTALL_TARGET_CMDS
27: $(INSTALL) -D -m 0755 $(@D)/libfoo.so* $(TARGET_DIR)/usr/lib
28: $(INSTALL) -d -m 0755 $(TARGET_DIR)/etc/foo.d
29: endef
30:
31: define LIBFOO_USERS
32: foo -1 libfoo -1 * - - - LibFoo daemon
33: endef
34:
35: define LIBFOO_DEVICES
36: /dev/foo c 666 0 0 42 0 - - -
37: endef
38:
39: define LIBFOO_PERMISSIONS
40: /bin/foo f 4755 foo libfoo - - - - -
41: endef
42:
43: $(eval $(generic-package))
```
最後我的 `package/kilo/kilo.mk` 是這麼寫的
- [ ] `package/kilo/kilo.mk`
```
KILO_VERSION = 62b099af00b542bdb08471058d527af258a349cf
# KILO_SOURCE = git://github.com/antirez/kilo.git
# FOO_SITE = $(call github,<user>,<package>,$(FOO_VERSION))
# KILO_SITE = $(call github, antirez, kilo,$(FOO_VERSION))
KILO_SITE = git://github.com/antirez/kilo.git
# KILO_SOURCE =
KILO_SITE_METHOD = git
# $(@D): the path to the build directory
define KILO_BUILD_CMDS
$(MAKE) CC="$(TARGET_CC)" LD="$(TARGET_LD)" -C $(@D)
endef
# See https://stackoverflow.com/questions/8014991/how-do-i-add-a-a-package-to-buildroot-which-is-available-in-a-git-repository
# See
define KILO_INSTALL_TARGET_CMDS
$(INSTALL) -D -m 0755 $(@D)/kilo $(TARGET_DIR)/usr/bin
endef
$(eval $(generic-package))
```
:::info
問題: 根據 buildroot 官方 [17.23.4. How to add a package from GitHub](https://buildroot.org/downloads/manual/manual.html#github-download-url)
```
# Use a tag or a full commit ID
FOO_VERSION = v1.0
FOO_SITE = $(call github,<user>,<package>,$(FOO_VERSION))
```
我的 `.mk` file 原本是這樣寫的
```
KILO_VERSION = 62b099af00b542bdb08471058d527af258a349cf
# FOO_SITE = $(call github,<user>,<package>,$(FOO_VERSION))
KILO_SITE = $(call github, antirez, kilo,$(FOO_VERSION))
```
但是 url 也就是 git clone 的址是不正確的,
url: github.com
我之後再回去重新 make 看看探討個
:::
### menu
最後再回去看 `menu`, **kilo** 出現在選單囉

### 補充一下
* [local package](http://vh21.github.io/linux/2016/05/07/add-customized-buildroot-package.html): 加一個自己的 local package,與原生 package 不同的地方,不需要去 official site 抓 tar ball。
* [How do I Add a A Package To Buildroot Which Is Available In A Git Repository?](https://stackoverflow.com/questions/8014991/how-do-i-add-a-a-package-to-buildroot-which-is-available-in-a-git-repository)
* [kilo github-repo](https://github.com/antirez/kilo)
* [buildroot的使用简介](https://blog.csdn.net/flfihpv259/article/details/51996204)
## `Make`
- [ ] Result
```
checking for bison... no
checking if /home/parallels/Desktop/riscv-tinyemu/buildroot-riscv-2018-10-20/output/host/usr/bin/riscv64-buildroot-linux-gnu-gcc is sufficient to build libc... yes
checking for riscv64-buildroot-linux-gnu-nm... /home/parallels/Desktop/riscv-tinyemu/buildroot-riscv-2018-10-20/output/host/usr/bin/riscv64-buildroot-linux-gnu-nm
checking for python3... python3
configure: error:
*** These critical programs are missing or too old: bison
*** Check the INSTALL file for required versions.
package/pkg-generic.mk:185: recipe for target '/home/parallels/Desktop/riscv-tinyemu/buildroot-riscv-2018-10-20/output/build/glibc-2.27/.stamp_configured' failed
make[1]: *** [/home/parallels/Desktop/riscv-tinyemu/buildroot-riscv-2018-10-20/output/build/glibc-2.27/.stamp_configured] Error 1
Makefile:36: recipe for target '_all' failed
make: *** [_all] Error 2
```
[mailing list](http://lists.busybox.net/pipermail/buildroot/2018-February/213360.html) 寫到了相關的問題
修改 `package/glibc/glibc.mk`
```
-GLIBC_DEPENDENCIES = host-gcc-initial linux-headers host-gawk
+GLIBC_DEPENDENCIES = host-gcc-initial linux-headers host-bison host-gawk
GLIBC_SUBDIR = build
```
### Build done
```
$> file output/images/rootfs.ext2
output/images/rootfs.ext2: Linux rev 1.0 ext2 filesystem data, UUID=25a89310-3e51-47c9-81f3-abfdac39f0f0
```
## Reult
```
Welcome to Buildroot
localhost login: root
Jan 1 00:00:04 localhost auth.info login[74]: root login on 'console'
[root@localhost ~]# ls /usr/bin/
[ du lsof scp truncate
[[ eject lspci seq tty
ar env lsusb setkeycodes uniq
awk expr lzcat setsid unix2dos
basename find lzma sha1sum unlink
bunzip2 flock man sha256sum unlzma
bzcat fold md5sum sha3sum unxz
chrt free mesg sha512sum unzip
chvt fuser microcom sort uptime
cksum head mkfifo ssh uudecode
clear hexdump nohup strace uuencode
cmp hostid nslookup strace-log-merge vlock
crontab id od strings wc
cut install openvt tail wget
dbclient ipcrm passwd tee which
dc ipcs patch telnet who
deallocvt killall printf test whoami
diff **kilo** readlink tftp xargs
dirname last realpath time xz
dos2unix less renice top xzcat
dropbearconvert logger reset tr yes
dropbearkey logname resize traceroute
[root@localhost ~]#
```
看到 **kilo** 了!!! 我真的哭出來了
- [ ] **kilo** editor

:::warning
kilo 要用指定的 [repository](https://github.com/sysprog21/kilo),裡頭有些關鍵的修改,請留意
:notes: jserv
:::
> copy that