前言
之前我们成功的将U-Boot进行了编译和移植,这章主要讲解linux内核的编译和移植。
说道linux内核,我便会想起linus这个人,拜他所赐,使得全世界拥有了这么个操作系统。同时由于目前硬件设备的急剧成熟化和规模化,使得在一块不到30块钱的设备上运行移植过的linux也不是什么难事儿了。所以在后面的物联网发展道路上,linux注定会一路高歌。
作为linux的喜好者,我向来不吝啬把自己的赞美送给这个已经比自己年纪还大的操作系统。不仅仅因为他的便捷移植性,更因为他的开源,以及由开源所体现出来的各种合作精神。相信拥抱过开源后的人,一定也会成为开源的布道者,而linux则值得让我们这样做。
linux内核编译
编译linux内核前,我们需要设置交叉编译链,但是由于第一章中,已经设置好了交叉编译链,所以这里无需重复设置。而linux内核的下载,可以在这里进行:
git clone https://github.com/Icenowy/linux.git -b f1c100s-480272lcd-test
下载下来后,传到虚拟机上解压。
之后,我们需要进入到linux内核目录来配置menuconfig,命令如下:
make ARCH=arm menuconfig
这条命令执行后会打开linux内核裁剪的menuconfig,这里我们可以根据自己的需要进行特性的裁剪。选择好后,保存之后,会在源码根目录下将我们的配置写入到.config文件中备用。需要说明的是,由于荔枝派nano这里已经为我们配置好了.config,所以我们直接点击链接下载,并将此config文件拷贝到源码根目录, 然后利用如下命令将config文件改为.config即可。
mv config .config
之后,我们就开始进行正式的编译流程了,在根目录中执行如下命令:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j16
此时屏幕会进行输出,开始进行正式的编译流程。期间可能会遇到若干问题,目前通过百度基本上都可以解决。
比如,编译过程中我遇到的一个问题,记录在了挖坑网上,通过百度,修改源码后得以解决,这里我不准备贴具体的错误信息了,大家可以点开自行翻阅。
漫长的等待,一般需要30分钟左右的编译时间,看到如下的结果,就代表编译成功了:
zImage is ready 代表着linux内核编译成功,并且已经生成到了/arch/arm/boot/目录下,我们接下来只需要将其拷贝到第一分区中即可。
制作分区
由于这里我们还没有制作分区,这里需要制作一下,总共需要制作两个分区,其中zImage文件和dtb设备树文件放到第一个分区,而根文件系统rootfs.tar需要放到第二个分区。
首先,利用命令打开gparted工具,然后分别创建第一个,第二个分区,第一个分区为fat16格式,给16mb大小,第二个分区为ext4格式,给4gb大小:
分区点好后,点击上面的对勾,即可把两个分区正确的做出来了。从图上,我们可以看出,第一分区为/dev/sdb1, 第二分区为/dev/sdb2.
进入到zImage所在的目录(arch/arm/boot),然后执行如下命令,将其拷贝到第一分区(由于sdb没法直接访问,需要先挂载,才能访问):
sudo mkdir /mnt/sdb1 //创建一个临时目录 sudo mount /dev/sdb1 /mnt/sdb1 //将sdb1挂载到此临时目录 sudo cp zImage /mnt/sdb1 //拷贝到sdb1中,此时sdb1中已经能看到文件 sudo sync sudo umount /dev/sdb1
设备树文件
之后进入目录/linux-mi/linux-f1c100s-480272lcd-test/arch/arm/boot/dts中,可以看到存在着suniv-f1c100s-licheepi-nano.dts文件,我们使用如下命令对其进行修改:
nano suniv-f1c100s-licheepi-nano.dts
整体的修改方法,这里有提示,我们可以按照我们的需求进行更改,改完之后保存,然后利用如下命令进行编译:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- dtbs -j16
编译完毕,会在当前目录生成suniv-f1c100s-licheepi-nano.dtb文件,将此文件也拷贝到第一分区即可,命令如下:
sudo mkdir /mnt/sdb1 //创建一个临时目录 sudo mount /dev/sdb1 /mnt/sdb1 //将sdb1挂载到此临时目录 sudo cp suniv-f1c100s-licheepi-nano.dtb /mnt/sdb1 //拷贝到sdb1中,此时sdb1中已经能看到文件 sudo sync sudo umount /dev/sdb1
整体操作完毕后,可以看到俩文件都拷贝到第一分区来了:
需要注意的是,如果执行umount操作,提示busy:umount: /mnt/sdb2: target is busy.,无法卸载的时候,可以尝试将当前cmd窗口关闭,然后在新窗口中进行umount即可,或者退出当前/mnt目录。
将卡插入荔枝派nano,得到的输出结果如下:
[ 7.408921] Reboot failed -- System halted U-Boot SPL 2018.01 (Jun 07 2021 - 14:56:21) DRAM: 32 MiB Trying to boot from MMC1 U-Boot 2018.01 (Jun 07 2021 - 14:56:21 -0700) Allwinner Technology CPU: Allwinner F Series (SUNIV) Model: Lichee Pi Nano DRAM: 32 MiB MMC: SUNXI SD/MMC: 0 *** Warning - bad CRC, using default environment In: serial@1c25000 Out: serial@1c25000 Err: serial@1c25000 Net: No ethernet found. starting USB... No controllers found Hit any key to stop autoboot: 0 reading zImage 3844528 bytes read in 200 ms (18.3 MiB/s) reading suniv-f1c100s-licheepi-nano.dtb 7583 bytes read in 25 ms (295.9 KiB/s) ## Flattened Device Tree blob at 83000000 Booting using the fdt blob at 0x83000000 Loading Device Tree to 816fb000, end 816ffd9e ... OK Starting kernel ... [ 0.000000] Booting Linux on physical CPU 0x0 [ 0.000000] Linux version 4.15.0-rc8-licheepi-nano (scy@ubuntu) (gcc version 6.1.1 20160711 (Linaro GCC 6.1-2016.08)) #11 [ 0.000000] CPU: ARM926EJ-S [41069265] revision 5 (ARMv5TEJ), cr=0005317f [ 0.000000] CPU: VIVT data cache, VIVT instruction cache [ 0.000000] OF: fdt: Machine model: Lichee Pi Nano [ 0.000000] Memory policy: Data cache writeback [ 0.000000] Built 1 zonelists, mobility grouping on. Total pages: 8128 [ 0.000000] Kernel command line: console=ttyS0,115200 panic=5 root=/dev/mmcblk0p2 rootwait rw [ 0.000000] Dentry cache hash table entries: 4096 (order: 2, 16384 bytes) [ 0.000000] Inode-cache hash table entries: 2048 (order: 1, 8192 bytes) [ 0.000000] Memory: 22684K/32768K available (6144K kernel code, 236K rwdata, 1388K rodata, 1024K init, 244K bss, 10084K ) [ 0.000000] Virtual kernel memory layout: [ 0.000000] vector : 0xffff0000 - 0xffff1000 ( 4 kB) [ 0.000000] fixmap : 0xffc00000 - 0xfff00000 (3072 kB) [ 0.000000] vmalloc : 0xc2800000 - 0xff800000 ( 976 MB) [ 0.000000] lowmem : 0xc0000000 - 0xc2000000 ( 32 MB) [ 0.000000] pkmap : 0xbfe00000 - 0xc0000000 ( 2 MB) [ 0.000000] modules : 0xbf000000 - 0xbfe00000 ( 14 MB) [ 0.000000] .text : 0x(ptrval) - 0x(ptrval) (7136 kB) [ 0.000000] .init : 0x(ptrval) - 0x(ptrval) (1024 kB) [ 0.000000] .data : 0x(ptrval) - 0x(ptrval) ( 237 kB) [ 0.000000] .bss : 0x(ptrval) - 0x(ptrval) ( 245 kB) [ 0.000000] SLUB: HWalign=32, Order=0-3, MinObjects=0, CPUs=1, Nodes=1 [ 0.000000] NR_IRQS: 16, nr_irqs: 16, preallocated irqs: 16 [ 0.000046] sched_clock: 32 bits at 24MHz, resolution 41ns, wraps every 89478484971ns [ 0.000109] clocksource: timer: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 79635851949 ns [ 0.000614] Console: colour dummy device 80x30 [ 0.000703] Calibrating delay loop... 203.16 BogoMIPS (lpj=1015808) [ 0.070226] pid_max: default: 32768 minimum: 301 [ 0.070532] Mount-cache hash table entries: 1024 (order: 0, 4096 bytes) [ 0.070571] Mountpoint-cache hash table entries: 1024 (order: 0, 4096 bytes) [ 0.072012] CPU: Testing write buffer coherency: ok [ 0.073656] Setting up static identity map for 0x80100000 - 0x80100058 [ 0.076119] devtmpfs: initialized [ 0.082271] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns [ 0.082333] futex hash table entries: 256 (order: -1, 3072 bytes) [ 0.082588] pinctrl core: initialized pinctrl subsystem [ 0.084455] random: get_random_u32 called from bucket_table_alloc+0x80/0x1bc with crng_init=0 [ 0.084696] NET: Registered protocol family 16 [ 0.085971] DMA: preallocated 256 KiB pool for atomic coherent allocations [ 0.087791] cpuidle: using governor menu [ 0.113370] SCSI subsystem initialized [ 0.113698] usbcore: registered new interface driver usbfs [ 0.113843] usbcore: registered new interface driver hub [ 0.114051] usbcore: registered new device driver usb [ 0.114483] pps_core: LinuxPPS API ver. 1 registered [ 0.114508] pps_core: Software ver. 5.3.6 - Copyright 2005-2007 Rodolfo Giometti <giometti@linux.it> [ 0.114567] PTP clock support registered [ 0.115019] Advanced Linux Sound Architecture Driver Initialized. [ 0.115884] random: fast init done [ 0.116479] clocksource: Switched to clocksource timer [ 0.142440] NET: Registered protocol family 2 [ 0.143849] TCP established hash table entries: 1024 (order: 0, 4096 bytes) [ 0.143924] TCP bind hash table entries: 1024 (order: 0, 4096 bytes) [ 0.143975] TCP: Hash tables configured (established 1024 bind 1024) [ 0.144244] UDP hash table entries: 256 (order: 0, 4096 bytes) [ 0.144301] UDP-Lite hash table entries: 256 (order: 0, 4096 bytes) [ 0.144764] NET: Registered protocol family 1 [ 0.145966] RPC: Registered named UNIX socket transport module. [ 0.146008] RPC: Registered udp transport module. [ 0.146025] RPC: Registered tcp transport module. [ 0.146041] RPC: Registered tcp NFSv4.1 backchannel transport module. [ 0.148277] NetWinder Floating Point Emulator V0.97 (double precision) [ 0.150120] Initialise system trusted keyrings [ 0.150651] workingset: timestamp_bits=30 max_order=13 bucket_order=0 [ 0.168147] NFS: Registering the id_resolver key type [ 0.168228] Key type id_resolver registered [ 0.168250] Key type id_legacy registered [ 0.181411] Key type asymmetric registered [ 0.181450] Asymmetric key parser 'x509' registered [ 0.181647] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 251) [ 0.181683] io scheduler noop registered [ 0.181702] io scheduler deadline registered [ 0.182448] io scheduler cfq registered (default) [ 0.182483] io scheduler mq-deadline registered [ 0.182503] io scheduler kyber registered [ 0.183580] sun4i-usb-phy 1c13400.phy: Couldn't request ID GPIO [ 0.193407] suniv-pinctrl 1c20800.pinctrl: initialized sunXi PIO driver [ 0.360992] Serial: 8250/16550 driver, 8 ports, IRQ sharing disabled [ 0.367696] console [ttyS0] disabled [ 0.387937] 1c25000.serial: ttyS0 at MMIO 0x1c25000 (irq = 23, base_baud = 6250000) is a 16550A [ 0.862083] console [ttyS0] enabled [ 0.872528] panel-simple panel: panel supply power not found, using dummy regulator [ 0.881892] SCSI Media Changer driver v0.25 [ 0.889643] m25p80 spi0.0: unrecognized JEDEC id bytes: 0b, 40, 18 [ 0.896382] ehci_hcd: USB 2.0 'Enhanced' Host Controller (EHCI) Driver [ 0.903033] ehci-platform: EHCI generic platform driver [ 0.908590] ohci_hcd: USB 1.1 'Open' Host Controller (OHCI) Driver [ 0.914814] ohci-platform: OHCI generic platform driver [ 0.920498] usbcore: registered new interface driver usb-storage [ 0.927511] udc-core: couldn't find an available UDC - added [g_cdc] to list of pending drivers [ 0.936453] i2c /dev entries driver [ 0.996612] sunxi-mmc 1c0f000.mmc: base:0x(ptrval) irq:19 [ 1.004004] usbcore: registered new interface driver usbhid [ 1.009706] usbhid: USB HID core driver [ 1.031574] NET: Registered protocol family 17 [ 1.036270] Key type dns_resolver registered [ 1.042881] Loading compiled-in X.509 certificates [ 1.059259] sun4i-drm display-engine: bound 1e60000.display-backend (ops 0xc07394f8) [ 1.068148] sun4i-drm display-engine: bound 1c0c000.lcd-controller (ops 0xc07387dc) [ 1.075813] [drm] Supports vblank timestamp caching Rev 2 (21.10.2013). [ 1.082532] [drm] No driver support for vblank timestamp query. [ 1.138992] Console: switching to colour frame buffer device 60x34 [ 1.142223] mmc0: host does not support reading read-only switch, assuming write-enable [ 1.145893] mmc0: new high speed SDXC card at address aaaa [ 1.147151] mmcblk0: mmc0:aaaa SN64G 59.5 GiB [ 1.162578] mmcblk0: p1 p2 [ 1.185816] sun4i-drm display-engine: fb0: frame buffer device [ 1.193003] [drm] Initialized sun4i-drm 1.0.0 20150629 for display-engine on minor 0 [ 1.202242] usb_phy_generic usb_phy_generic.0.auto: usb_phy_generic.0.auto supply vcc not found, using dummy regulator [ 1.214157] musb-hdrc musb-hdrc.1.auto: MUSB HDRC host driver [ 1.220133] musb-hdrc musb-hdrc.1.auto: new USB bus registered, assigned bus number 1 [ 1.230248] hub 1-0:1.0: USB hub found [ 1.234246] hub 1-0:1.0: 1 port detected [ 1.239923] using random self ethernet address [ 1.244392] using random host ethernet address [ 1.250846] usb0: HOST MAC da:9f:cf:9e:80:f1 [ 1.255224] usb0: MAC 52:86:68:f9:fd:1b [ 1.259282] g_cdc gadget: CDC Composite Gadget, version: King Kamehameha Day 2008 [ 1.266828] g_cdc gadget: g_cdc ready [ 1.271378] cfg80211: Loading compiled-in X.509 certificates for regulatory database [ 1.289660] cfg80211: Loaded X.509 cert 'sforshee: 00b28ddf47aef9cea7' [ 1.296407] ALSA device list: [ 1.299516] #0: Loopback 1 [ 1.303359] platform regulatory.0: Direct firmware load for regulatory.db failed with error -2 [ 1.312086] cfg80211: failed to load regulatory.db [ 1.318923] EXT4-fs (mmcblk0p2): couldn't mount as ext3 due to feature incompatibilities [ 1.339949] EXT4-fs (mmcblk0p2): recovery complete [ 1.344794] EXT4-fs (mmcblk0p2): mounted filesystem with ordered data mode. Opts: (null) [ 1.353177] VFS: Mounted root (ext4 filesystem) on device 179:2. [ 1.360977] devtmpfs: error mounting -2 [ 1.369651] Freeing unused kernel memory: 1024K [ 1.374418] Kernel panic - not syncing: No working init found. Try passing init= option to kernel. See Linux Documentat. [ 1.388565] CPU: 0 PID: 1 Comm: swapper Not tainted 4.15.0-rc8-licheepi-nano #1 [ 1.395851] Hardware name: Allwinner suniv Family [ 1.400640] [<c010e73c>] (unwind_backtrace) from [<c010b8a8>] (show_stack+0x10/0x14) [ 1.408395] [<c010b8a8>] (show_stack) from [<c01168d8>] (panic+0xb8/0x230) [ 1.415275] [<c01168d8>] (panic) from [<c0656d6c>] (kernel_init+0xd4/0x10c) [ 1.422238] [<c0656d6c>] (kernel_init) from [<c0107f60>] (ret_from_fork+0x14/0x34) [ 1.429800] Rebooting in 5 seconds.. [ 7.409034] Reboot failed -- System halted
可以看到,成功进入了内核,由于没有根文件系统,所以系统暂停了,接下来一章,我们就来讲解如何制作根文件系统。
参考
http://nano.lichee.pro/build_sys/kernel.html