# Embedded (Linux) System Design
###### tags: `LINUX KERNEL` `rootfs` `busybox` `embedded Linux`
<style>
.blue {
color: blue;
}
.bgblue {
color: blue;
font-size: 24px;
font-weight: bold;
}
.red {
color: red;
font-size: 24px;
font-weight: bold;
}
h1 {text-align: center;}
h2 {text-align: center;}
</style>
Authors: WhoAmI, CrazyMonkey
email: kccddb@gmail.com
Date: 2023-10-11
Copyright: CC BY-NC-SA
<h1>千里之行始於足下 ~老子</h1>
**故不積跬步,無以至千里;不積小流,無以成江海**~荀子
**學而不思則罔 思而不學則殆**~論語

偉大的 GNU GPL 前輩 [**Richard Matthew Stallman (rms)**](https://en.wikipedia.org/wiki/Richard_Stallman)
---


<center><span class="red">**BUG 除不盡 春風吹又生**</span></center>
The course covers the basic and advanced issues of Embedded Linux System such as boot loader, toolchain, linux kernel source code, root file system, kernel module and device driver.
Stability is an important issue.

<h2>站在巨人的肩膀上</h2>
先修科目 (Prerequisites) : C (or C++) Language, Linux, Network programming and OS
教室 資電 515 星期二 13:10-16:00
c Language:
[ANSI C for Programmers on UNIX Systems, Tim Love](https://www.cs.utexas.edu/~dahlin/Classes/UGOS/reading/loveC.pdf)
[C 也可以寫的很OOP 簡單範例與簡易Makefile, 務必要懂](https://hackmd.io/@pingulinux/c-oop)
[在 linux kernel 中的 OOP 設計思維](https://haogroot.com/2021/10/16/oop_in_linux_kernel/)
Win32 c 有 [cygwin](https://www.cygwin.com/) 可以練習 c 語言 (使用 gcc)
[LINUX:
Rute User's Tutorial and Exposition
(Version 1.0.0), by Paul Sheer](https://paginas.fe.up.pt/~pfs/recursos/unix/rute/)
[Object-oriented design patterns in the kernel, part 1](https://lwn.net/Articles/444910/)
[Object-oriented design patterns in the kernel, part 2](https://lwn.net/Articles/446317/)
shell (bash): [OnlineGDB ](https://https://www.onlinegdb.com/) 可以練習, 還是用自己的 Linux 方便
Linux network commands: ip addr xxxxx, ip route xxxxx, ifconfig xxxxx, ...
embedded system 軟體設計中 有遞迴(Recursive) 設計時 要特別注意 stack overflow

:::success
<span class="blue">善用 ramdisk, tmpfs 降低對 flash 的讀寫次數: [How to Create a Ramdisk in Linux](https://linuxhint.com/create-ramdisk-linux/)
</span>
[Linux Tmpfs](https://www.kernel.org/doc/html/latest/filesystems/tmpfs.html)
**如果記憶體 夠大~用 Ramdisk 放網頁等也很適合**
:::
VirtualBox: Create Ubuntu 16.04.7 (最好有**40G**, two NICs (NAT Mode and Bridged Mode))
:::info
[Get Linux Kernel Version in Ubuntu Command Line](https://learnubuntu.com/get-kernel-version/)
:::
[MobaXterm](https://mobaxterm.mobatek.net/download.html) or [putty](https://www.putty.org/)
Text editor: vim, nano, gedit(need X-window),...
[Makefile範例教學](https://jasonblog.github.io/note/gunmake/makefilefan_li_jiao_xue.html?fbclid=IwAR3hJJwfQAKMR9x8Be90rjTQn3g5BPMMjb_BKC7_DCyrl7BZ_xTZNRs1pTc)
練習 看 8051 datasheet,[ 8390D datasheet]
(https://pdf1.alldatasheet.com/datasheet-pdf/view/8365/NSC/DP8390D.html)
[Linux Command Cheat Sheet](https://www.golinuxcloud.com/linux-commands-cheat-sheet/)
:::success
Futher Reading (宣老師 提供):
[Linux Kernel Teaching](https://linux-kernel-labs.github.io/refs/heads/master/index.html?fbclid=IwAR2wL2-TSqF7b-nXXMi3YUcGJ8BnJFLadpOwCugrnP-jMmg09hTB9qhXWmQ)
[Linux Network Programming, by WhoAmI](https://hackmd.io/@pingulinux/Linux-Network-Programming)
:::
[e.g., BeagleBone Black](https://hackmd.io/@pingulinux/beaglebone)
[Linux 核心模組運作原理](https://hackmd.io/@sysprog/linux-kernel-module)
**這課程大都是 原理的理解與實作~相信是很花時間的課程**
原理老師們會教也給實例, **但要學生自己練習做過, 就像開車也要自己練習一樣, 只是會背名詞考試沒有大用, 基礎要好**~計概(含 c, 資料結構) , 微處理機, OS, 要紮實, 基礎要學好
早期 吳大猷院長 也說 "基礎" 很重要, 因為是日後自我學習的基礎
我看過一本書~有讀者問 blog 作者一個問題, 結果 作者 <span class="blue">回的是 請 複習 成語辭典 "囫圇吞棗" </span>
我看了都笑出來~
:::info
PC817 Photocoupler 可隔離電源
A3144 LINEAR HALL-EFFECT SENSORS, 可用磁鐵當開關
2SD313 NPN (3A) 可換電壓
請看 datasheet 複習 電子學 電路學
:::
---
[Container (abstract data type)](https://en.wikipedia.org/wiki/Container_(abstract_data_type))
e.g., The $<div>$ HTML element is the generic container for flow content.
Ref. [w3schools ](https://www.w3schools.com/default.asp)
---
另外 還有 Boot Loader 部分 例如[ [U-Boot], 因需硬體設備例如 樹莓派(英語:Raspberry Pi)或 Banana Pi ,本課程不含 這部分](https://www.denx.de/wiki/U-Boot)
[What is a bootloader and how does it work?](https://www.ionos.com/digitalguide/server/configuration/what-is-a-bootloader/)
---

[stackoverflow](https://stackoverflow.com/) 可以查到 很多 好心軟體系統等專家幫你解答疑惑, 是您進步的明燈
[Linux Command Cheat Sheet](https://www.golinuxcloud.com/linux-commands-cheat-sheet/)
[fcu embedded system (大三)](https://www.facebook.com/groups/733324005014456)
[Linux 的 ls 指令教學與常用範例整理](https://blog.gtwang.org/linux/linux-ls-command-tutorial/)
---
<span class="bgblue">Course Outlines</span>
---
<h1>基本觀念</h1>
[1. Embedded Linux and how the system works:
Boot Loader, Linux OS, root file system (rootfs) and init](https://hackmd.io/@pingulinux/emlinux)
[2. Linux System Calls, VFS, and How it works](https://hackmd.io/@pingulinux/emlinux)
<h1>
四大步驟 運用QEMU (Quick EMUlator) 完成 Linux ARM-xxx 成功開機的方法實例 (這裡皆用 source code 重新編譯而成)
</h1>
**以下就是 實作的基礎** **請讀者 務必實作一遍, 用 QEMU 代替 實際的 embedded system, 可以方便學習與測試**
**本課程 與 kernel 相關 都由 source code 做起**
**第一步驟 能夠用 toolchain 建造 linux kernel image 與 rootfs.**
3. Build QEMU ( https://www.qemu.org/), [QEMU 5.0.0 and QEMU 2.x.x](https://hackmd.io/@pingulinux/QEMU5)
[4. Busybox, build x86 kernel (linux-[4.4.50]) and rootfs (busybox 1.22.0)](https://hackmd.io/@pingulinux/QEMU5)

[PXE Linux](https://wiki.syslinux.org/wiki/index.php?title=PXELINUX)
[ How to enable PXE boot with VirtualBox, by Jack Wallen](https://www.techrepublic.com/article/enable-pxe-boot-virtualbox/)
[ Setting up a ‘PXE Network Boot Server’ for Multiple Linux Distribution Installations in RHEL/CentOS 7](https://www.tecmint.com/install-pxe-network-boot-server-in-centos-7/)
[Build SYSLINUX 6.03](https://hackmd.io/@pingulinux/syslinux6_03)
[A bootloader for Linux using the PXE network booting protocol](https://wiki.syslinux.org/wiki/index.php?title=Doc/pxelinux)
[master boot record (MBR)](https://en.wikipedia.org/wiki/Master_boot_record#:~:text=A%20master%20boot%20record%20%28MBR%29%20is%20a%20special,publicly%20introduced%20in%201983%20with%20PC%20DOS%202.0.)
[UEFI (Unified Extensible Firmware Interface)](https://zh.wikipedia.org/zh-tw/%E7%B5%B1%E4%B8%80%E5%8F%AF%E5%BB%B6%E4%BC%B8%E9%9F%8C%E9%AB%94%E4%BB%8B%E9%9D%A2)
[UEFI 是什麼以及如何在 Windows 中使用它?【完整教學】](https://tw.easeus.com/diskmanager/what-is-uefi.html)
[DHCP server and TFTP server](https://hackmd.io/@pingulinux/dhcp-tftp)
**讀者有空時 可用 真正的電腦測試, 這種方式 可以 避免 每個人都要 裝 toolchain...等等 方便 版本維護**
---
:::success
5. [toolchain: Sourcery G++ Lite 2011.03-42
Build linux kernel: linux-2.6.33 ](https://hackmd.io/@pingulinux/QEMUARM) (kernel source: http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.33.tar.bz2)
:::
:::success
6. [重點: Build rootfs (busybox 1.22.0) and understand rootfs](https://hackmd.io/@pingulinux/ARMBusybox)
wget https://www.busybox.net/downloads/busybox-1.22.0.tar.bz2 --no-check-certificate
:::
:::warning
**HW (期中考後一星期完成, 同一組只須交一份紙本, 期末口試):**
1. 運用 QEMU 與 linux kernel source 添加 一個 system call, 例如 sys_hello() ( ARM version, 練習 cross compiler, Makefile)
[Ref. Add a system call (in x86 QEMU Environment)](https://hackmd.io/@pingulinux/add-system-call)
2. Tech. Report: Build your linux kernel and rootfs (ARM version, 可以用 QEMU 啟動)
:::
[Linux Kernel: 簡介HZ, tick and jiffies
](https://drianhuang.blogspot.com/2007/10/linux-kernel-hz-tick-and-jiffies.html)
[sysfs - a filesystem for exporting kernel objects](https://man7.org/linux/man-pages/man5/sysfs.5.html)
[proc - process information, system information, and sysctl pseudo-filesystem](https://man7.org/linux/man-pages/man5/proc.5.html)
enum
{
HI_SOFTIRQ=0,
TIMER_SOFTIRQ,
NET_TX_SOFTIRQ,
NET_RX_SOFTIRQ,
BLOCK_SOFTIRQ,
IRQ_POLL_SOFTIRQ,
TASKLET_SOFTIRQ,
SCHED_SOFTIRQ,
HRTIMER_SOFTIRQ,
RCU_SOFTIRQ, /* Preferable RCU should always be the last softirq */
NR_SOFTIRQS
};
softirqs : deferred work runs in interrupt context
tasklets : deferred work runs in interrupt context
work queues : deferred work runs in process context
softirqs : Softirqs of the same type can run concurrently on several CPUs.
tasklets : Tasklets of different types can run concurrently on several CPUs, but tasklets of the same type cannot.
work queues : can run simultaneously on different CPU's
softirqs : cannot go to sleep
tasklets : cannot go to sleep
work queues : can go to sleep
softirqs : cannot be preempted/schedule
tasklets : cannot be preempted/schedule
work queues : maybe be preempted/schedule
softirqs : not easy to use
tasklets : easy to use
work queues : easy to use
[work queue, kthread和timer对比](https://blog.csdn.net/njuitjf/article/details/108454786)
[Character device drivers](https://linux-kernel-labs.github.io/refs/heads/master/labs/device_drivers.html)
[Linux Kernel Teaching](https://linux-kernel-labs.github.io/refs/heads/master/index.html)
:::info
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)
register a range of char device numbers
[udev - Dynamic device management](https://man7.org/linux/man-pages/man7/udev.7.html)
busybox: mdev
:::
Parameters
dev_t *dev
output parameter for first assigned number
unsigned baseminor
first of the requested range of minor numbers
unsigned count
the number of minor numbers required
const char *name
the name of the associated device or driver
Description
Allocates a range of char device numbers. The major number will be chosen dynamically, and returned (along with the first minor number) in dev. Returns zero or a negative error code.
[How to initialize device's class when register device in linux-6.0](https://stackoverflow.com/questions/76302629/how-to-initialize-devices-class-when-register-device-in-linux-6-0)
[通过 Linux sysfs 玩转硬件](https://blog.inoki.cc/2021/10/20/linux-sysfs-playground/)
[Linux下通过sysfs方式控制GPIO(/sys/class/gpio)](https://blog.csdn.net/qq_37858386/article/details/85343380)
<h1>
Linux Kernel Module Programming
</h1>
---
7. [Linux Device Driver Tutorial Programming – Linux Device Driver Tutorial Part 7](https://embetronicx.com/tutorials/linux/device-drivers/linux-device-driver-tutorial-programming/)
8. [sysfs, procfs](https://hackmd.io/@pingulinux/sysfs-procfs) ([Sysfs in Linux Kernel – Linux Device Driver Tutorial Part 11](https://embetronicx.com/tutorials/linux/device-drivers/sysfs-in-linux-kernel/))
9. [Linux kernel module programming, by Dr. Derek Molloy, interrupt 需要硬體板子, 沒硬體的看 14](http://derekmolloy.ie/category/general/linux/)
10. [Workqueue in Linux Kernel Part 2 – Linux Device Driver Tutorial Part 15](https://embetronicx.com/tutorials/linux/device-drivers/workqueue-in-linux-dynamic-creation/)
11. [Kernel Thread – Linux Device Driver Tutorial Part 19](https://embetronicx.com/tutorials/linux/device-drivers/linux-device-drivers-tutorial-kernel-thread/)
12. [Tasklets in Linux kernel | Dynamic Method – Linux Device Driver Tutorial Part 21](https://embetronicx.com/tutorials/linux/device-drivers/tasklets-dynamic-method/)
13. [Using Kernel Timer In Linux Device Driver – Linux](https://embetronicx.com/tutorials/linux/device-drivers/using-kernel-timer-in-linux-device-driver/)
14. [Poll Linux Example Device Driver – Linux Device Driver Tutorial Part 42](https://embetronicx.com/tutorials/linux/device-drivers/poll-linux-example-device-driver/)
15. I[nterrupts Example Program in Linux Kernel – Linux Device Driver Tutorial Part 13, by SLR 作者 巧妙用 x86-> asm("int $0x3B"); 產生 irq 11 ](https://embetronicx.com/tutorials/linux/device-drivers/linux-device-driver-tutorial-part-13-interrupt-example-program-in-linux-kernel/)
[The Linux Kernel Module Programming Guide, by Pe Jay Salzman, Michael Burian , Ori Pomerantz, Bob Mottram, Jim Huang](https://sysprog21.github.io/lkmpg/#interrupt-handlers)
<span class="blue">
Cultivate teamwork to complete homework in groups of 2-3 people
Final oral test
No interim written test
Class situation</span>
---
[TEXTBOOKS AND ON-LINE EDUCATIONAL MATERIAL](https://hackmd.io/@pingulinux/books)
Install:
Ubuntu-16.04.7 server version
install openssh-server
**modify sshd_config**
putty or MobaXterm
Learn iproute2
ip addr
ip route
Learn commands: (請看 man page)
vim, nano, wget, curl, sudo, cd, ls, cp, cat, more, find, ln, grep, ps, chmod, chown, tar, mkdir, rm, rmdir, df, du, kill, echo, which, man, top, ping, touch, pstree, strace, strip, file, tcpdump, mount, umount, dd, objdump, gzip, mke2fs, sync, cpio, losetup, strip -g (Remove debugging symbols only), rsync, gzip, gunzip, file, chroot, tee, tail, dd, uname, uname -r, [mdev (busybox 代替 udev)](https://www.cnblogs.com/wmate/p/13518444.html), udevadm - udev management tool, [ gawk, awk 這比較難且與 Regular Expressions 有關, 您有空與需要再學](https://www.gnu.org/software/gawk/manual/html_node/index.html), ...
[簡明 Linux Shell Script 入門教學](https://blog.techbridge.cc/2019/11/15/linux-shell-script-tutorial/)
了解 [linux 檔案架構與主要用途](https://linyunwen.github.io/2020/10/01/linux-fs/)
[Linux Command Cheat Sheet](https://www.golinuxcloud.com/linux-commands-cheat-sheet/)
[30+ awk examples for beginners / awk command tutorial in Linux/Unix](https://www.golinuxcloud.com/awk-examples-with-command-tutorial-unix-linux/)
[ntpd - Network Time Protocol (NTP) daemon](https://doc.ntp.org/documentation/4.2.8-series/ntpd/)
[Learn linux pipe](https://blog.gtwang.org/linux/linux-io-input-output-redirection-operators/)
[Linux 使用 wget 指令自動下載網頁檔案教學與範例, by G. T. Wang](https://blog.gtwang.org/linux/linux-wget-command-download-web-pages-and-files-tutorial-examples/)
[Shell Scripting for Beginners – How to Write Bash Scripts in Linux](https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/)
grep: [Linux 匹配文字 grep 指令用法教學與範例, by G. T. Wang](https://blog.gtwang.org/linux/linux-grep-command-tutorial-examples/)
:::info
Read Around:
[How to Set Environment Variables in Linux](https://phoenixnap.com/kb/linux-set-environment-variable)
[Environment Variables in Linux/Unix](https://www.geeksforgeeks.org/environment-variables-in-linux-unix/)
[Elixir Cross Referencer](https://elixir.bootlin.com/busybox/latest/source)
:::
[Ubuntu-16.04搭建DHCP服務](https://www.796t.com/content/1528114960.html)
Linux 2.6.33
```c=
Documentation
arch
block
crypto
drivers
firmware
fs
include
init
ipc
kernel
lib
mm
net
samples
scripts
security
sound
tools
usr
virt
.gitignore 843 2010-02-24 18:52:17
.mailmap 4021 2010-02-24 18:52:17
COPYING 18693 2010-02-24 18:52:17
CREDITS 94031 2010-02-24 18:52:17
Kbuild 2440 2010-02-24 18:52:17
MAINTAINERS 164165 2010-02-24 18:52:17
Makefile 53199 2010-02-24 18:52:17
README 17459 2010-02-24 18:52:17
REPORTING-BUGS 3371 2010-02-24 18:52:17
Th
```
| [Embedded Linux System, Boot, Linux VFS, System Call, SPI, GPIO, … 課程基礎原理](https://hackmd.io/@pingulinux/emlinux) |
| --- |
| | |
|
| [C 也可以寫的很OOP 簡單範例與簡易Makefile](https://hackmd.io/@pingulinux/c-oop) |[ c language: function pointer, callback](https://hackmd.io/@pingulinux/function-pointer-callback)|
| -------- | -------- |
| [busybox-1.22.0 and linux-4.4.50](https://hackmd.io/@pingulinux/b1-22-0-linux4-4-50)| [QEMU 5.0.0 ](https://hackmd.io/@pingulinux/QEMU5) |
---
[System calls in the Linux kernel. Part 1.](https://0xax.gitbooks.io/linux-insides/content/SysCall/linux-syscall-1.html)
| [使用 Sourcery G++ Lite 2011.03-42 編譯 Linux Kernel for QEMU ARM Version](https://hackmd.io/@pingulinux/QEMUARM) | [編譯 ARM Busybox 1.22.0 當 Rootfs for QEMU Linux](https://hackmd.io/@pingulinux/busybox-rfs)|
| -------- | -------- |
| [Syslinux 6.03](https://hackmd.io/@pingulinux/syslinux6_03)| [Linux simple sysfs, procfs and character device driver (Linux kernel >3.2)](https://hackmd.io/@pingulinux/sysfs-procfs)
[Introduction to deferred interrupts (Softirq, Tasklets and Workqueues)](https://0xax.gitbooks.io/linux-insides/content/Interrupts/linux-interrupts-9.html)
[软中断与软中断的排查](https://www.cnblogs.com/rdchenxi/p/13552582.html)
---
Appendix
練習 看 8051 datasheet 與
[DP8390 Datasheet~NIC controller](https://www.alldatasheet.com/datasheet-pdf/pdf/8368/NSC/DP8390.html)
[Sleeping in the Kernel and Frame Reception/Transmission](https://hackmd.io/@pingulinux/sniffframe)
**[linux-insides](https://0xax.gitbooks.io/linux-insides/content/)**
[Linux Network Programming, by WhoAmI](https://hackmd.io/@pingulinux/Linux-Network-Programming)
[getloadavg - get system load averages](https://www.man7.org/linux/man-pages/man3/getloadavg.3.html)
[Interface statistics](https://www.kernel.org/doc/html/latest/networking/statistics.html)
https://www.gnu.org/software/libc/manual/html_node/Integers.html
[山不在高 有仙則名](https://hackmd.io/@pingulinux/books)
---
水不在深 有龍則靈
有內功(基礎理論知識, 演算法, OS, 網路知識, ...) 沒 外功 (程式設計, 微處理機, 網路程式設計...) 無法打架 例如 覺遠大師
有外功 沒內功 進步有限, 除非 您是 **洪七公**
如果 經濟上許可 (**課程上 不要求有**)
[樹莓派(英語:Raspberry Pi)](https://https://zh.wikipedia.org/zh-tw/%E6%A0%91%E8%8E%93%E6%B4%BE)
或
[ Banana Pi ](https://www.banana-pi.org/)
如果對 單晶片+Wifi 或 物聯網專題 有興趣可以參考 [ESP8266](https://en.wikipedia.org/wiki/ESP8266) (可以用 Arduino IDE 開發軟體)
有 硬體 可以練習 實作經驗與技巧
**千里之行 始於足下**