0%

从零开构建树莓派64位操作系统

1. 首次安装树莓派

1.1 安装树莓派

1.1.1 下载树莓派镜像(可跳过)

https://www.raspberrypi.org/software/operating-systems/ 下载树莓派镜像,为了快速下载,这里下载的是 Raspberry Pi OS Lite ,体积最小,后面主要在命令行环境使用。这里下载的版本是 2021-03-04-raspios-buster-armhf-lite.zip
准备2张SD卡,一张安装树莓派系统(这里取代号A卡),另外一张(B卡)用于我们自己的构建系统。

1.1.2 安装树莓派系统

在Ubuntu主机上安装树莓派烧写工具 rpi-imager

1
sudo apt install rpi-imager

在命令行执行 rpi-imager 后会出现工具

1
rpi-imager


选择刚才下载的zip文件:

或者直接选择需要安装的系统在线安装

选择A卡,点击烧录即可完成安装。

1.1.3 无屏幕键盘初始化树莓派

如果没有屏幕,可通过配置WIFI和SSH,通过SSH来连接树莓派。在烧录系统到A卡后,A卡上有2个分区 root rootfs , root 分区上是包含树莓派引导文件已经Linux Kerne、设备数文件等等,具体的内容后面会详细介绍。
配置网络
如果有网线,直接插线就可以。连接WIFI需要以下操作:
root 分区新建 wpa_supplicant.conf 文件

1
2
3
4
5
6
7
8
9
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US

network={
ssid="WIFI名称"
psk="WIFI密码"
key_mgmt=WPA-PSK
}

配置SSH
root 分区新建名为SSH的文件即可。
将A卡插入树莓派即可启动树莓派系统。

1.2 树莓派系统初始化

1.2.1 SSH连接

通过SSH连接树莓派:

1
ssh [email protected]

默认密码为:raspberry
到此即可通过SSH来使用树莓派。
修改密码:

1
2
3
4
5
$ passwd
Changing password for pi.
Current password:
New password:
Retype new password:

输入默认密码 raspberry 和自己的密码。

1.2.2 树莓派系统配置和VNC连接(可选)

通过raspi-config可对树莓派进行设置,比如打来GPIO 端口,开启VNC 服务等

1
sudo raspi-config


3 Interface Options / P3 VNC 中开启VNC
在Ubuntu中安装VNC客户端:
https://www.realvnc.com/en/connect/download/viewer/linux/ 中下载VNC View https://www.realvnc.com/download/file/viewer.files/VNC-Viewer-6.20.529-Linux-x64.deb
安装:

1
sudo dpkg -i VNC-Viewer-6.20.529-Linux-x64.deb

打开vnc view,新建Connection 输入树莓派IP地址和用户名密码即可进入远程桌面。

至此完成了树莓派系统的初步安装。

2 从零构建树莓派

2.1 串口连接树莓派

这一步需要使用到USB转TTL串口的线,淘宝和京东有售。

2.1.2 串口接线

USB 串口工具

树莓派GPIO借口

1,2号pin是远离树莓派USB的那一端。

接线:
usb转串口工具的TX连接树莓派的RX,RX连接树莓派的TX,VCC与GND正常连接

通过 raspi-config 设置 3 Interface Options / P6 Serial Port 开启串口

2.1.2 Ubuntu上安装串口工具

1
sudo apt-get install minicom

启动minicom

1
sudo minicom -D /dev/ttyUSB0

-D 参数Device,ttyUSB0 即为电脑连接上的USB串口。

在接好线,插好USB,启动minicom后,接通树莓派电源,可看到minicom打印内核启动过程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
sudo minicom -D /dev/ttyUSB0                  


Welcome to minicom 2.7.1

OPTIONS: I18n
Compiled on Dec 23 2019, 02:06:26.
Port /dev/ttyUSB0, 20:27:33

Press CTRL-A Z for help on special keys

[ 0.000000] Booting Linux on physical CPU 0x0
[ 0.000000] Linux version 5.10.11-v7l+ (dom@buildbot) (arm-linux-gnueabihf-gcc-8 (Ubuntu/Linaro 8.4.0-3ubuntu1) 8.4.0, GNU ld (GNU Binutils f1
[ 0.000000] CPU: ARMv7 Processor [410fd083] revision 3 (ARMv7), cr=30c5383d
[ 0.000000] CPU: div instructions available: patching division code
[ 0.000000] CPU: PIPT / VIPT nonaliasing data cache, PIPT instruction cache
[ 0.000000] OF: fdt: Machine model: Raspberry Pi 4 Model B Rev 1.2
[ 0.000000] Memory policy: Data cache writealloc
[ 0.000000] Reserved memory: created CMA memory pool at 0x000000001ec00000, size 256 MiB
[ 0.000000] OF: reserved mem: initialized node linux,cma, compatible id shared-dma-pool
[ 0.000000] Zone ranges:
[ 0.000000] DMA [mem 0x0000000000000000-0x000000002fffffff]
[ 0.000000] Normal empty
[ 0.000000] HighMem [mem 0x0000000030000000-0x00000000fbffffff]
[ 0.000000] Movable zone start for each node
[ 0.000000] Early memory node ranges
[ 0.000000] node 0: [mem 0x0000000000000000-0x0000000037ffffff]
[ 0.000000] node 0: [mem 0x0000000040000000-0x00000000fbffffff]
[ 0.000000] Initmem setup node 0 [mem 0x0000000000000000-0x00000000fbffffff]
[ 0.000000] percpu: Embedded 20 pages/cpu s50636 r8192 d23092 u81920
[ 0.000000] Built 1 zonelists, mobility grouping on. Total pages: 997120
[ 0.000000] Kernel command line: coherent_pool=1M 8250.nr_uarts=1 snd_bcm2835.enable_compat_alsa=0 snd_bcm2835.enable_hdmi=1 video=HDMI-A-1:1t
[ 0.000000] Kernel parameter elevator= does not have any effect anymore.
[ 0.000000] Please use sysfs to set IO scheduler for individual devices.
[ 0.000000] Dentry cache hash table entries: 131072 (order: 7, 524288 bytes, linear)
[ 0.000000] Inode-cache hash table entries: 65536 (order: 6, 262144 bytes, linear)
[ 0.000000] mem auto-init: stack:off, heap alloc:off, heap free:off
[ 0.000000] software IO TLB: mapped [mem 0x0000000017cc0000-0x000000001bcc0000] (64MB)
[ 0.000000] Memory: 3602320K/3997696K available (10240K kernel code, 1354K rwdata, 3152K rodata, 2048K init, 890K bss, 133232K reserved, 2621)
[ 0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=4, Nodes=1
[ 0.000000] ftrace: allocating 33824 entries in 67 pages
[ 0.000000] ftrace: allocated 67 pages with 3 groups
[ 0.000000] rcu: Hierarchical RCU implementation.
[ 0.000000] Rude variant of Tasks RCU enabled.
[ 0.000000] Tracing variant of Tasks RCU enabled.
[ 0.000000] rcu: RCU calculated value of scheduler-enlistment delay is 10 jiffies.
[ 0.000000] NR_IRQS: 16, nr_irqs: 16, preallocated irqs: 16
[ 0.000000] GIC: Using split EOI/Deactivate mode
[ 0.000000] random: get_random_bytes called from start_kernel+0x3c8/0x59c with crng_init=0
[ 0.000008] sched_clock: 32 bits at 1000kHz, resolution 1000ns, wraps every 2147483647500ns
[ 0.000035] clocksource: timer: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 1911260446275 ns
[ 0.000099] bcm2835: system timer (irq = 25)
[ 0.000743] arch_timer: cp15 timer(s) running at 54.00MHz (phys).
[ 0.000765] clocksource: arch_sys_counter: mask: 0xffffffffffffff max_cycles: 0xc743ce346, max_idle_ns: 440795203123 ns
[ 0.000786] sched_clock: 56 bits at 54MHz, resolution 18ns, wraps every 4398046511102ns
[ 0.000804] Switching to timer-based delay loop, resolution 18ns
[ 0.001051] Console: colour dummy device 80x30
[ 0.001772] printk: console [tty1] enabled
[ 0.001840] Calibrating delay loop (skipped), value calculated using timer frequency.. 108.00 BogoMIPS (lpj=540000)
[ 0.001894] pid_max: default: 32768 minimum: 301
[ 0.002074] LSM: Security Framework initializing
[ 0.002266] Mount-cache hash table entries: 2048 (order: 1, 8192 bytes, linear)
[ 0.002311] Mountpoint-cache hash table entries: 2048 (order: 1, 8192 bytes, linear)
[ 0.003882] Disabling memory control group subsystem
[ 0.004016] CPU: Testing write buffer coherency: ok
[ 0.004476] CPU0: thread -1, cpu 0, socket 0, mpidr 80000000
[ 0.005683] Setting up static identity map for 0x200000 - 0x20003c
[ 0.005896] rcu: Hierarchical SRCU implementation.
[ 0.006824] smp: Bringing up secondary CPUs ...
[ 0.008018] CPU1: thread -1, cpu 1, socket 0, mpidr 80000001
[ 0.009364] CPU2: thread -1, cpu 2, socket 0, mpidr 80000002
[ 0.010646] CPU3: thread -1, cpu 3, socket 0, mpidr 80000003
[ 0.010895] smp: Brought up 1 node, 4 CPUs
[ 0.010927] SMP: Total of 4 processors activated (432.00 BogoMIPS).
[ 0.010957] CPU: All CPU(s) started in HYP mode.
[ 0.010984] CPU: Virtualization extensions available.
[ 0.011809] devtmpfs: initialized
[ 0.025623] VFP support v0.3: implementor 41 architecture 3 part 40 variant 8 rev 0
[ 0.025866] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns
[ 0.025919] futex hash table entries: 1024 (order: 4, 65536 bytes, linear)
[ 0.032638] pinctrl core: initialized pinctrl subsystem
[ 0.033713] NET: Registered protocol family 16
[ 0.037476] DMA: preallocated 1024 KiB pool for atomic coherent allocations
[ 0.038273] audit: initializing netlink subsys (disabled)
[ 0.038568] audit: type=2000 audit(0.030:1): state=initialized audit_enabled=0 res=1
[ 0.039138] thermal_sys: Registered thermal governor 'step_wise'
[ 0.039819] hw-breakpoint: found 5 (+1 reserved) breakpoint and 4 watchpoint registers.
[ 0.039873] hw-breakpoint: maximum watchpoint size is 8 bytes.
[ 0.040268] Serial: AMBA PL011 UART driver
[ 0.072212] bcm2835-mbox fe00b880.mailbox: mailbox enabled
[ 0.090908] raspberrypi-firmware soc:firmware: Attached to firmware from 2021-01-27T22:19:57, variant start
[ 0.100922] raspberrypi-firmware soc:firmware: Firmware hash is 99d9a48302e4553cff3688692bb7e9ac760a03fa
[ 0.145578] bcm2835-dma fe007000.dma: DMA legacy API manager, dmachans=0x1
[ 0.149725] vgaarb: loaded
[ 0.150191] SCSI subsystem initialized
[ 0.150419] usbcore: registered new interface driver usbfs
[ 0.150507] usbcore: registered new interface driver hub
[ 0.150595] usbcore: registered new device driver usb
[ 0.151041] usb_phy_generic phy: supply vcc not found, using dummy regulator
[ 0.153196] clocksource: Switched to clocksource arch_sys_counter
[ 1.180263] VFS: Disk quotas dquot_6.6.0
[ 1.180389] VFS: Dquot-cache hash table entries: 1024 (order 0, 4096 bytes)
[ 1.180588] FS-Cache: Loaded
[ 1.180789] CacheFiles: Loaded
[ 1.181786] simple-framebuffer 3e6c7000.framebuffer: framebuffer at 0x3e6c7000, 0x500000 bytes, mapped to 0x(ptrval)
[ 1.181830] simple-framebuffer 3e6c7000.framebuffer: format=a8r8g8b8, mode=1280x1024x32, linelength=5120
[ 1.188675] Console: switching to colour frame buffer device 160x64
[ 1.194941] simple-framebuffer 3e6c7000.framebuffer: fb0: simplefb registered!
[ 1.204680] NET: Registered protocol family 2
[ 1.205611] tcp_listen_portaddr_hash hash table entries: 512 (order: 0, 6144 bytes, linear)
[ 1.205834] TCP established hash table entries: 8192 (order: 3, 32768 bytes, linear)
[ 1.205979] TCP bind hash table entries: 8192 (order: 4, 65536 bytes, linear)
[ 1.206114] TCP: Hash tables configured (established 8192 bind 8192)
[ 1.206324] UDP hash table entries: 512 (order: 2, 16384 bytes, linear)
[ 1.206423] UDP-Lite hash table entries: 512 (order: 2, 16384 bytes, linear)
[ 1.206743] NET: Registered protocol family 1
[ 1.207511] RPC: Registered named UNIX socket transport module.
[ 1.207589] RPC: Registered udp transport module.
[ 1.207652] RPC: Registered tcp transport module.
[ 1.207714] RPC: Registered tcp NFSv4.1 backchannel transport module.
[ 1.207799] PCI: CLS 0 bytes, default 64
[ 1.210729] Initialise system trusted keyrings
[ 1.211026] workingset: timestamp_bits=14 max_order=20 bucket_order=6
[ 1.219381] zbud: loaded
[ 1.221358] FS-Cache: Netfs 'nfs' registered for caching
[ 1.222193] NFS: Registering the id_resolver key type
[ 1.222304] Key type id_resolver registered
[ 1.222364] Key type id_legacy registered
[ 1.222547] nfs4filelayout_init: NFSv4 File Layout Driver Registering...
[ 1.223725] Key type asymmetric registered
[ 1.223789] Asymmetric key parser 'x509' registered
[ 1.224032] bounce: pool size: 64 pages
[ 1.224117] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 249)
[ 1.224399] io scheduler mq-deadline registered
[ 1.224465] io scheduler kyber registered
[ 1.228738] brcm-pcie fd500000.pcie: host bridge /scb/pcie@7d500000 ranges:
[ 1.231271] brcm-pcie fd500000.pcie: No bus range found for /scb/pcie@7d500000, using [bus 00-ff]
[ 1.233925] brcm-pcie fd500000.pcie: MEM 0x0600000000..0x063fffffff -> 0x00c0000000
[ 1.236493] brcm-pcie fd500000.pcie: IB MEM 0x0000000000..0x00bfffffff -> 0x0400000000
[ 1.295300] brcm-pcie fd500000.pcie: link up, 5.0 GT/s PCIe x1 (SSC)
[ 1.298093] brcm-pcie fd500000.pcie: PCI host bridge to bus 0000:00
[ 1.300511] pci_bus 0000:00: root bus resource [bus 00-ff]
[ 1.302938] pci_bus 0000:00: root bus resource [mem 0x600000000-0x63fffffff] (bus address [0xc0000000-0xffffffff])
[ 1.305532] pci 0000:00:00.0: [14e4:2711] type 01 class 0x060400
[ 1.308205] pci 0000:00:00.0: PME# supported from D0 D3hot
[ 1.314106] PCI: bus0: Fast back to back transfers disabled
[ 1.316839] pci 0000:01:00.0: [1106:3483] type 00 class 0x0c0330
[ 1.319343] pci 0000:01:00.0: reg 0x10: [mem 0x00000000-0x00000fff 64bit]
[ 1.322172] pci 0000:01:00.0: PME# supported from D0 D3cold
[ 1.328028] PCI: bus1: Fast back to back transfers disabled
[ 1.330422] pci 0000:00:00.0: BAR 8: assigned [mem 0x600000000-0x6000fffff]
[ 1.332810] pci 0000:01:00.0: BAR 0: assigned [mem 0x600000000-0x600000fff 64bit]
[ 1.335291] pci 0000:00:00.0: PCI bridge to [bus 01]
[ 1.337675] pci 0000:00:00.0: bridge window [mem 0x600000000-0x6000fffff]
[ 1.340468] pcieport 0000:00:00.0: enabling device (0140 -> 0142)
[ 1.343081] pcieport 0000:00:00.0: PME: Signaling with IRQ 63
[ 1.351160] Serial: 8250/16550 driver, 1 ports, IRQ sharing enabled
[ 1.354458] bcm2835-aux-uart fe215040.serial: there is not valid maps for state default
[ 1.359294] iproc-rng200 fe104000.rng: hwrng registered
[ 1.361981] vc-mem: phys_addr:0x00000000 mem_base=0x3ec00000 mem_size:0x40000000(1024 MiB)
[ 1.365371] gpiomem-bcm2835 fe200000.gpiomem: Initialised: Registers at 0xfe200000
[ 1.380748] brd: module loaded
[ 1.395318] loop: module loaded
[ 1.399348] Loading iSCSI transport class v2.0-870.
[ 1.404157] libphy: Fixed MDIO Bus: probed
[ 1.407776] bcmgenet fd580000.ethernet: GENET 5.0 EPHY: 0x0000
[ 1.423223] libphy: bcmgenet MII bus: probed
[ 1.503347] unimac-mdio unimac-mdio.-19: Broadcom UniMAC MDIO bus
[ 1.506805] usbcore: registered new interface driver r8152
[ 1.509183] usbcore: registered new interface driver lan78xx
[ 1.511502] usbcore: registered new interface driver smsc95xx
[ 1.515385] xhci_hcd 0000:01:00.0: enabling device (0140 -> 0142)
[ 1.517778] xhci_hcd 0000:01:00.0: xHCI Host Controller
[ 1.520019] xhci_hcd 0000:01:00.0: new USB bus registered, assigned bus number 1
[ 1.525563] xhci_hcd 0000:01:00.0: hcc params 0x002841eb hci version 0x100 quirks 0x0000030000000890
[ 1.529114] usb usb1: New USB device found, idVendor=1d6b, idProduct=0002, bcdDevice= 5.10
[ 1.531401] usb usb1: New USB device strings: Mfr=3, Product=2, SerialNumber=1
[ 1.533729] usb usb1: Product: xHCI Host Controller
[ 1.536026] usb usb1: Manufacturer: Linux 5.10.11-v7l+ xhci-hcd
[ 1.538319] usb usb1: SerialNumber: 0000:01:00.0
[ 1.541333] hub 1-0:1.0: USB hub found
[ 1.543716] hub 1-0:1.0: 1 port detected
[ 1.546630] xhci_hcd 0000:01:00.0: xHCI Host Controller
[ 1.548897] xhci_hcd 0000:01:00.0: new USB bus registered, assigned bus number 2
[ 1.551175] xhci_hcd 0000:01:00.0: Host supports USB 3.0 SuperSpeed
[ 1.553989] usb usb2: New USB device found, idVendor=1d6b, idProduct=0003, bcdDevice= 5.10
[ 1.556309] usb usb2: New USB device strings: Mfr=3, Product=2, SerialNumber=1
[ 1.558613] usb usb2: Product: xHCI Host Controller
[ 1.560891] usb usb2: Manufacturer: Linux 5.10.11-v7l+ xhci-hcd
[ 1.563206] usb usb2: SerialNumber: 0000:01:00.0
[ 1.566198] hub 2-0:1.0: USB hub found
[ 1.568559] hub 2-0:1.0: 4 ports detected
[ 1.572456] dwc_otg: version 3.00a 10-AUG-2012 (platform bus)
[ 1.575615] usbcore: registered new interface driver uas
[ 1.577961] usbcore: registered new interface driver usb-storage
[ 1.580371] mousedev: PS/2 mouse device common for all mice
[ 1.584260] bcm2835-wdt bcm2835-wdt: Broadcom BCM2835 watchdog timer
[ 1.589977] sdhci: Secure Digital Host Controller Interface driver
[ 1.592240] sdhci: Copyright(c) Pierre Ossman
[ 1.595223] sdhci-pltfm: SDHCI platform and OF driver helper
[ 1.600204] ledtrig-cpu: registered to indicate activity on CPUs
[ 1.602781] hid: raw HID events driver (C) Jiri Kosina
[ 1.605191] usbcore: registered new interface driver usbhid
[ 1.607409] usbhid: USB HID core driver
[ 1.614965] Initializing XFRM netlink socket
[ 1.617362] NET: Registered protocol family 17
[ 1.619680] Key type dns_resolver registered
[ 1.622214] Registering SWP/SWPB emulation handler
[ 1.624581] registered taskstats version 1
[ 1.626751] Loading compiled-in X.509 certificates
[ 1.629732] Key type ._fscrypt registered
[ 1.631899] Key type .fscrypt registered
[ 1.634124] Key type fscrypt-provisioning registered
[ 1.647476] uart-pl011 fe201000.serial: there is not valid maps for state default
[ 1.649974] uart-pl011 fe201000.serial: cts_event_workaround enabled
[ 1.652213] fe201000.serial: ttyAMA0 at MMIO 0xfe201000 (irq = 36, base_baud = 0) is a PL011 rev2
[ 1.661041] bcm2835-aux-uart fe215040.serial: there is not valid maps for state default
[ 1.664051] printk: console [ttyS0] disabled
[ 1.666328] fe215040.serial: ttyS0 at MMIO 0xfe215040 (irq = 37, base_baud = 62500000) is a 16550
[ 2.967163] printk: console [ttyS0] enabled
[ 2.976253] bcm2835-power bcm2835-power: Broadcom BCM2835 power domains driver
[ 2.989191] of_cfs_init
[ 2.994060] of_cfs_init: OK
[ 3.037147] mmc0: SDHCI controller on fe340000.emmc2 [fe340000.emmc2] using ADMA
[ 3.048226] Waiting for root device PARTUUID=37dc5df3-02...
[ 3.123239] usb 1-1: new high-speed USB device number 2 using xhci_hcd
[ 3.148244] mmc0: new ultra high speed DDR50 SDHC card at address aaaa
[ 3.157974] mmcblk0: mmc0:aaaa SC16G 14.8 GiB
[ 3.170257] mmcblk0: p1 p2
[ 3.213492] EXT4-fs (mmcblk0p2): mounted filesystem with ordered data mode. Opts: (null)
[ 3.224119] VFS: Mounted root (ext4 filesystem) readonly on device 179:2.
[ 3.242393] devtmpfs: mounted
[ 3.256691] Freeing unused kernel memory: 2048K
[ 3.263934] Run /sbin/init as init process
[ 3.305845] usb 1-1: New USB device found, idVendor=2109, idProduct=3431, bcdDevice= 4.21
[ 3.316511] usb 1-1: New USB device strings: Mfr=0, Product=1, SerialNumber=0
[ 3.326113] usb 1-1: Product: USB2.0 Hub
[ 3.334293] hub 1-1:1.0: USB hub found
[ 3.340665] hub 1-1:1.0: 4 ports detected
[ 3.357666] random: fast init done
[ 3.709677] systemd[1]: System time before build time, advancing clock.
[ 3.820447] NET: Registered protocol family 10
[ 3.828660] Segment Routing with IPv6
[ 3.894632] systemd[1]: systemd 241 running in system mode. (+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYP)
[ 3.922400] systemd[1]: Detected architecture arm.
[ 4.013351] systemd[1]: Set hostname to <raspberrypi>.
[ 4.784874] random: systemd: uninitialized urandom read (16 bytes read)
[ 4.807407] random: systemd: uninitialized urandom read (16 bytes read)
[ 4.821131] systemd[1]: Created slice system-systemd\x2dfsck.slice.
[ 4.833750] random: systemd: uninitialized urandom read (16 bytes read)
[ 4.843600] systemd[1]: Listening on udev Kernel Socket.
[ 4.855139] systemd[1]: Reached target Swap.
[ 4.866153] systemd[1]: Listening on Journal Socket.
[ 4.886705] systemd[1]: Starting Restore / save the current clock...
[ 4.900496] systemd[1]: Condition check resulted in Huge Pages File System being skipped.
[ 4.912097] systemd[1]: Listening on initctl Compatibility Named Pipe.
[ 5.179601] i2c /dev entries driver

Raspbian GNU/Linux 10 raspberrypi ttyS0

raspberrypi login:

从日志中我们可以看到,Linux内核为5.10.11-v7l 使用的是armv7l架构的版本,编译的gcc为arm,32位

1
Linux version 5.10.11-v7l+ (dom@buildbot) (arm-linux-gnueabihf-gcc-8 (Ubuntu/Linaro 8.4.0-3ubuntu1) 8.4.0

在串口里面我们可以直接登录。
到这里,串口的部分结束了,为了和后面做对比,我们登录后,看一下/boot 下的这个2个文件。
**cmdline.txt**

1
console=serial0,115200 console=tty1 root=PARTUUID=37dc5df3-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

cmdline.txt 是树莓派BootLoader启动Linux内核时,传给内核的启动参数
console=serial0,115200 设置串口控制台,波特率为115200
console=tty1 设置tty1控制台
root=PARTUUID=37dc5df3-02 rootfs目录挂在的位置,这里的PARTUUID=37dc5df3-02直接制定了分区号
rootfstype=ext4 rootfs的分区类型为ext4
elevator=``deadline 磁盘扫描算法为deadline
fsck.repair=true 开启磁盘修复能力
rootwait 等待root设备加载完毕,如果不设置,会出现加载完内核后,无法找到磁盘的情况

Linux启动参数后面会有更多介绍,这里暂时列出现有的。

**config.txt**

1
2
3
4
[all]
start_x=0
gpu_mem=128
enable_uart=1

start_x 是否启动桌面
enable_uart 开启串口
gpu_mem 设置GPU内存
其他的配置暂时没列。

2.2 构建64位Linux系统

在准备完串口工具后,我们正式开始构建系统

2.2.1 编译U-boot(可选)

在树莓派有自己的BootLoader,可以直接加载Kernel,也可以先加载U-Boot再启动Kernel,这里选择编译一下U-Boot。
64位构建过程参考 https://www.cwiki.cn/archives/u-boot安装到树莓派

2.2.2 编译Linux Kernel

kernel 建议使用树莓派的,里面已经配置好了各种选项。

1
2
3
4
5
6
git clone https://github.com/raspberrypi/linux.git
cd linux
make distclean
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcm2711_defconfig
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig # 可选择自己定制
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j12 # 自己设置编译进程数

构建完成后,在arch/arm64/boot 目录下,可找到 Image Image.gz 镜像文件,其中Image 是未经压缩的Kernel文件,Image.gz 是gzip压缩过的镜像文件。64位的树莓派需要使用未经压缩的内核,即Image

1
2
$ file Image 
Image: MS-DOS executable PE32+ executable (EFI application) Aarch64 (stripped to external PDB), for MS Windows
1
2
$ file Image.gz 
Image.gz: gzip compressed data, max compression, from Unix, original size modulo 2^32 21043712

2.2.3 通过BusyBox构建文件系统(可选)

下载BusyBox https://busybox.net/ ,解压后,进入Busybox目录

1
2
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j12

选择静态编译 Settings  ---> / --- Build Options 下的 Build static binary (no shared libs) ,如果不选择静态编译,会带上很多so库,静态编译会方便一些。
编译后会有下面的显示,这个可以不用管,编译已经成功。

1
2
3
4
5
6
7
8
9
Static linking against glibc, can't use --gc-sections
Trying libraries: crypt m resolv rt
Library crypt is not needed, excluding it
Library m is needed, can't exclude it (yet)
Library resolv is needed, can't exclude it (yet)
Library rt is not needed, excluding it
Library m is needed, can't exclude it (yet)
Library resolv is needed, can't exclude it (yet)
Final link with: m resolv

构建Busybox文件系统:

1
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- install

在busybox目录下生成 _install 目录。

1
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-  PREFIX=/path/sdcark/rootfs install

通过PREFIX参数可以制定安装位置,建议直接安装到需要的地方。具体参考 https://git.busybox.net/busybox/tree/INSTALL
在将BusyBox生成的install文件拷贝到rootfs目录后,进入rootfs目录,补充完所需目录。

1
2
3
4
5
mkdir root dev etc bin sbin mnt sys proc lib home tmp var usr
mkdir usr/sbin usr/bin usr/lib usr/modules
mkdir mnt/usb mnt/nfs mnt/etc mnt/etc/init.d
mkdir lib/modules
sudo chmod 1777 tmp

创建必要设备

1
2
3
cd dev
sudo mknod -m 660 console c 5 1
sudo mknod -m 660 null c 1 3
1
2
3
4
5
6
7
cd etc
vim fstab

proc /proc proc defaults 0 0
none /tmp ramfs defaults 0 0
mdev /dev ramfs defaults 0 0
sysfs /sys sysfs defaults 0 0
1
2
3
4
5
6
cd etc/init.d
vim rcS

#! /bin/sh

/bin/mount -a
1
2
3
4
5
6
7
8
9
cd etc
vim inittab

::sysinit:/etc/init.d/rcS
::respawn:-/bin/sh
::restart:/sbin/init
::ctrlaltdel:/bin/umount -a -r
::shutdown:/bin/umount -a -r
::shutdown:/sbin/swapoff –a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
cd etc
vim group

root:*:0:
daemon:*:1:
bin:*:2:
sys:*:3:
adm:*:4:
tty:*:5:
disk:*:6:
lp:*:7:lp
mail:*:8:
news:*:9:
uucp:*:10:
proxy:*:13:
kmem:*:15:
dialout:*:20:
fax:*:21:
voice:*:22:
cdrom:*:24:
floppy:*:25:
tape:*:26:
sudo:*:27:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cd etc 
vim profile

# /etc/profile: system-wide .profile file for the Bourne shells

echo
echo "FileSystem is Ready ..."
echo

USER="`id -un`"
LOGNAME=$USER
PS1='[\u@\h \W]\# '
PATH=$PATH
HOSTNAME=`/bin/hostname`

export USER LOGNAME PS1 PATH

至此完成了文件系统制作。也可以将这个文件系统打包成镜像,在Kernel启动时制定initrd

2.2.4 通过debootstrap构建基于Debian的文件系统(建议)

通过debootstrap制作文件系统就相当简单,建议以这种方式。debootstrap制作的文件系统包含了Debian所需的软件,文件系统完善,可直接使用apt等命令等。
在为了避免跨平台到来的麻烦,我们直接在树莓派现有系统上操作。
在树莓派上通过读卡器插入B卡

1
sudo fdisk -l

找到B的的rootfs分区

1
sudo mount /dev/sdb2 /mnt
1
2
3
4
sudo apt install debootstrap
sudo debootstrap --arch=arm64 --foreign buster /mnt/ http://ftp.cn.debian.org/debian/
sudo chroot /mnt/
debootstrap/debootstrap --second-stage

设置清华源

1
2
3
4
5
6
7
8
9
10
vim.tiny /etc/apt/sources.list

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free

配置时区、串口、SSH、Vimd等等

1
2
3
4
5
6
7
8
9
10
11
apt-get update
passwd
# localtime
ln -sf /usr/slocaltimehare/zoneinfo/Asia/Shanghai /etc/localtime
# serial port
ln -s /lib/systemd/system/serial-getty\@.service /etc/systemd/system/getty.target.wants/[email protected]

apt-get install vim ssh
apt-get install ifupdown net-tools
apt-get install udev sudo wget curl
apt-get install alsa-utils libasound2-dev

添加用户、设置网络、设置中文

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
USER=pi
HOST=raspberry
useradd -G sudo -m -s /bin/bash $USER
passwd $USER

echo $HOST > /etc/hostname
echo "127.0.0.1 localhost.localdomain localhost" > /etc/hosts
echo "127.0.0.1 $HOST" >> /etc/hosts

cat <<EOF > /etc/network/interfaces.d/eth0
auto eth0
iface eth0 inet static
address 192.168.1.9
gateway 192.168.1.1
netmask 255.255.255.0
EOF

apt-get install locales
dpkg-reconfigure locales

2.3 配置引导

拷贝编译好的Linux Kernel文件Image 到root分区,如果需要使用U-Boot引用,好需要将u-boot.bin 拷贝到root分区。

2.3.1 配置树莓派基本引导文件

https://github.com/raspberrypi/firmware.git 中root目录下的文件拷贝到root分区,参考https://www.cwiki.cn/archives/u-boot安装到树莓派

2.3.2 配置启动内核Linux

修改cmdline.txt,这个文件里面是Linux启动参数

1
console=ttyAMA0,115200 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

修改config.txt为

1
2
3
kernel=kernel8.img # 启动内核
arm_64bit=1 # 64位
enable_uart=1 # 打开串口

当设置位U-Boot启动时,设置为

1
2
3
kernel=u-boot.bin  # 启动内核
arm_64bit=1 # 64位
enable_uart=1 # 打开串口

由于uboot编译的是树莓派的rpi_4_defconfig ,启动后自动加载配置,包括 cmdline.txt config.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
MESS:00:00:05.410376:0: arasan: arasan_emmc_open                                                         
MESS:00:00:05.578211:0: brfs: File read: /mfs/sd/config.txt
MESS:00:00:05.581017:0: brfs: File read: 257 bytes
MESS:00:00:05.648806:0: brfs: File read: /mfs/sd/config.txt
MESS:00:00:05.667051:0: brfs: File read: 257 bytes
MESS:00:00:06.147877:0: gpioman: gpioman_get_pin_num: pin DISPLAY_DSI_PORT not defined
MESS:00:00:06.155166:0: *** Restart logging
MESS:00:00:06.160428:0: hdmi: HDMI:hdmi_get_state is deprecated, use hdmi_get_display_state instead
MESS:00:00:06.169785:0: hdmi: HDMI:hdmi_get_state is deprecated, use hdmi_get_display_state instead
MESS:00:00:06.175719:0: HDMI0: hdmi_pixel_encoding: 300000000
MESS:00:00:06.181188:0: HDMI1: hdmi_pixel_encoding: 300000000
MESS:00:00:06.191390:0: dtb_file 'bcm2711-rpi-4-b.dtb'
MESS:00:00:06.198297:0: brfs: File read: /mfs/sd/bcm2711-rpi-4-b.dtb
MESS:00:00:06.201544:0: Loading 'bcm2711-rpi-4-b.dtb' to 0x100 size 0xbd2d
MESS:00:00:06.220694:0: brfs: File read: 48429 bytes
MESS:00:00:06.232960:0: brfs: File read: /mfs/sd/overlays/overlay_map.dtb
MESS:00:00:06.298769:0: brfs: File read: 1523 bytes
MESS:00:00:06.301317:0: brfs: File read: /mfs/sd/config.txt
MESS:00:00:06.311815:0: brfs: File read: 257 bytes
MESS:00:00:06.315176:0: brfs: File read: /mfs/sd/cmdline.txt
MESS:00:00:06.318896:0: Read command line from file 'cmdline.txt':
MESS:00:00:06.324775:0: 'console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait'
MESS:00:00:07.446637:0: brfs: File read: 115 bytes
MESS:00:00:08.020689:0: brfs: File read: /mfs/sd/kernel8.img
MESS:00:00:08.023242:0: Loading 'kernel8.img' to 0x80000 size 0x76f687
MESS:00:00:09.138881:0: Kernel relocated to 0x200000
MESS:00:00:09.140734:0: Device tree loaded to 0x2eff3d00 (size 0xc203)
MESS:00:00:09.148754:0: uart: Set PL011 baud rate to 103448.300000 Hz
MESS:00:00:09.156064:0: uart: Baud rate change done...
MESS:00:00:09.158083:0:[ 0.000000] Booting Linux on physical CPU 0x0000000000 [0x410fd083]
[ 0.000000] Linux version 5.10.25-v8+ (zauther@zauther-MS-7B98) (aarch64-linux-gnu-gcc (GNU Toolchain for the A-profile Architecture 8.3-201

从串口中启动打印可以知道,u-boot启动时,会依次读取config.txt 配置,加载bcm2711-rpi-4-b.dtb 设备树描述文件, overlay_map.dtb 等。从cmdline.txt 中读取Linux Kernel启动参数,加载kernel8.img 默认文件名。加载kernel到内存位置 0x80000 ,重定位到0x200000 ;加载设备树到0x2eff3d00,设置串口波特率,并启动Kernel。
至此完成了Kernel的加载,并启动文件系统。
如果遇到文件系统出现损坏,为只读文件系统,可尝试切换到root用户,重新挂载根文件系统:

1
mount -o remount rw /

由于之前配置的是静态IP,通过SSH即可连接到树莓派。

DONE.