【树莓派学习笔记】树莓派4B上运行uboot并从网络启动linux内核(上)
【树莓派学习笔记】树莓派4B上运行uboot并从网络启动linux内核(上)
文章目录
- 【树莓派学习笔记】树莓派4B上运行uboot并从网络启动linux内核(上)
- 前言
- 1. 硬件需求与软件版本汇总
- 1.1 硬件需求
- 1.2 软件版本汇总
- 2. 前期准备
- 2.1 查找SD卡的设备名
- 2.2 删除原有的分区
- 2.2.1 fdisk命令如下:
- 2.2.2 删除原有分区:
- 2.2.3 直接回车确认
- 2.2.4 继续删除:
- 2.2.5 查看SD卡基本信息:
- 2.2.6 保存并退出:
- 2.3 增加两个分区
- 2.3.1 fdisk命令如下:
- 2.3.2 新建第一个分区:
- 2.3.3 新建第二个分区全部回车默认选项:
- 2.4 格式化分区
- 2.5 挂载分区
- 3. 构建交叉工具链
- 3.1 安装必要组件
- 3.2 下载 crosstool-NG源码
- 3.3 构建并安装crosstool-NG
- 3.4 配置corsstool-NG
- 3.5 构建工具链
- 4. u-boot
- 4.1 下载u-boot源码
- 4.2 配置并编译u-boot
- 未完待续
参考:https://hechao.li/2021/12/20/Boot-Raspberry-Pi-4-Using-uboot-and-Initramfs/
前言
树莓派的多数教程上,都是将linux系统下载进入SD,并从SD卡启动。但是对于编写linux驱动这种特定场景来说,每次更改都要把改动拷贝进入U盘实在太过繁琐,严重打击学习热情。所以本文通过在树莓派上运行uboot,并通过uboot网络启动linux内核的方式解决上述问题。
1. 硬件需求与软件版本汇总
1.1 硬件需求
- 树莓派4B(内存大小不限)
- linux桌面端运行环境,本文使用虚拟机安装ubuntu20.04.
- SD卡和读卡器
- CH340 USB转TTL模块(可选)
- 杜邦线(至少三根)(可选)
1.2 软件版本汇总
- crosstool-NG 1.24.0
- u-boot v2021.10
- linux rpi-5.17.y
- busybox-1.33.2
2. 前期准备
SD卡仅用来存储树莓派的启动引导文件,uboot的执行文件以及设备树文件。
2.1 查找SD卡的设备名
linux命令如下:
$ dmesg | tail
命令窗口输出如下:
[ 2719.343068] sd 33:0:0:1: [sdc] 60751872 512-byte logical blocks: (31.1 GB/29.0 GiB)
[ 2719.345290] sd 33:0:0:1: [sdc] Write Protect is off
[ 2719.345294] sd 33:0:0:1: [sdc] Mode Sense: 21 00 00 00
[ 2719.353076] sd 33:0:0:1: [sdc] Write cache: disabled, read cache: enabled, doesn’t support DPO or FUA
[ 2719.359180] sd 33:0:0:0: [sdb] Attached SCSI removable disk
[ 2719.587175] sdc: sdc1 sdc2
[ 2719.612387] sd 33:0:0:1: [sdc] Attached SCSI removable disk
[ 2720.761882] FAT-fs (sdc1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.
[ 2735.040898] EXT4-fs (sdc2): recovery complete
[ 2735.049551] EXT4-fs (sdc2): mounted filesystem with ordered data mode. Opts: (null). Quota mode: none.
从上图可知,新插入的SD的卡设备为sdc,接下来我们将对SD卡进行格式化及分区。
2.2 删除原有的分区
2.2.1 fdisk命令如下:
$ sudo fdisk /dev/sdc
注:其中sdc为2.1.1章节中获取的设备名称,需要更换为自己的设备名称
命令窗口输出:
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help):
2.2.2 删除原有分区:
Command (m for help): d
Command (m for help): d
Partition number (1,2, default 2):
2.2.3 直接回车确认
Partition 2 has been deleted.
2.2.4 继续删除:
Command (m for help): d
仅剩一个分区,不会询问Partition number:
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.
2.2.5 查看SD卡基本信息:
Command (m for help): p
Command (m for help): p
Disk /dev/sdc: 28.99 GiB, 31104958464 bytes, 60751872 sectors
Disk model: MassStorageClass
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x638274e3
2.2.6 保存并退出:
Command (m for help): w
- 如果出现如下信息,则未删除成功,需要先将挂载的设备卸载。
Command (m for help): w
The partition table has been altered.
Failed to remove partition 1 from system: Device or resource busy
Failed to remove partition 2 from system: Device or resource busy
The kernel still uses the old partitions. The new table will be used at the next reboot.
Syncing disks.
查找挂载的设备:
lsblk -f | grep sdc
注:其中sdc为2.1.1章节中获取的设备名称,需要更换为自己的设备名称
sdc
├─sdc1 vfat system-boot B084-2C85 147.5M 41% /media/fyh/system-boot
└─sdc2 ext4 writable a6ba7311-aab8-46ea-aad2-6a42bf57300d 24.9G 9% /media/fyh/writable
其中/media/fyh/***/字段为挂载设备,本例中为/media/fyh/system-boot与/media/fyh/writable。
卸载设备:
sudo umount /media/fyh/system-boot /media/fyh/writable
注:其中**/fyh/system-boot与/fyh/writable**为本小节中获取的文件名称,需要更换为自己的
2.3 增加两个分区
2.3.1 fdisk命令如下:
$ sudo fdisk /dev/sdc
注:其中sdc为2.1.1章节中获取的设备名称,需要更换为自己的设备名称
命令窗口输出:
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help):
2.3.2 新建第一个分区:
Command (m for help): n
一路回车:
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (1-4, default 1):
First sector (2048-60751871, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-60751871, default 60751871):
分区大小100M,下一个选项Y:
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-60751871, default 60751871): +100M
…
Do you want to remove the signature? [Y]es/[N]o: Y
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-60751871, default 60751871): +100M
Created a new partition 1 of type ‘Linux’ and of size 100 MiB.
Partition #1 contains a vfat signature.
Do you want to remove the signature? [Y]es/[N]o: Y
The signature will be removed by a write command.
2.3.3 新建第二个分区全部回车默认选项:
Command (m for help): n
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (2-4, default 2):
First sector (206848-60751871, default 206848):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (206848-60751871, default 60751871):
Created a new partition 2 of type ‘Linux’ and of size 28.9 GiB.
保存并退出:
Command (m for help): w
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
2.4 格式化分区
# 第一个分区初始化为fat32格式
sudo mkfs.vfat -F 32 -n boot /dev/sdc1
# 第二个分区初始化为ext4格式
sudo mkfs.ext4 -L root /dev/sdc2
注:其中sdc为2.1.1章节中获取的设备名称,需要更换为自己的设备名称
2.5 挂载分区
sudo mount /dev/sdc1 /mnt/boot
sudo mount /dev/sdc2 /mnt/root
注:其中sdc为2.1.1章节中获取的设备名称,需要更换为自己的设备名称。
如果boot和root目录不存在请事先创建一下
sudo mkdir /mnt/boot
sudo mkdir /mnt/root
3. 构建交叉工具链
通常我们的PC机一般是X86架构的,而树莓派4B是arm架构的,由于架构的差异导致在PC上编译生成的可执行文件无法在树莓派上直接运行。虽然我们也可以直接在树莓派4B上编译,但是通常PC相对树莓派来说有性能优势。交叉编译工具就是可以用PC的性能编译出树莓派上可直接运行的工具。
本例中使用crosstool-NG构建适用于树莓派ARM的交叉工具链。
建议在开始前一个树莓派专用的文件夹,用于存放所有工具及文件。
#在用户目录建立文件夹
mkdir ~/raspberry4B
#进入该文件夹
cd ~/raspberry4B
3.1 安装必要组件
# 安装必要的组件
$ sudo apt install gperf bison flex texinfo help2man gawk automake libncurses5-dev python3-dev -y
3.2 下载 crosstool-NG源码
运行以下命令时,可先进入想下载安装目录。
$ git clone https://github.com/crosstool-ng/crosstool-ng
$ cd crosstool-ng/
# 切换到1.25.0分支
$ git checkout crosstool-ng-1.25.0 -b 1.25.0
3.3 构建并安装crosstool-NG
$ ./bootstrap
$ ./configure --prefix=${PWD}
$ make
$ make install
# 将编译工具加入环境变量
$ export PATH="${PWD}/bin:${PATH}"
3.4 配置corsstool-NG
# 查看可构建的交叉编译器
$ ct-ng list-samples
# 使用aarch64-rpi4-linux-gnu编译器
$ ct-ng aarch64-rpi4-linux-gnu
3.5 构建工具链
$ ct-ng build
4. u-boot
u-boot是一种bootloader,类似于PC中的BIOS,用于引导linux内核的运行。本章主要介绍u-boot的源码下载,配置,编译与生成可执行文件的过程。
4.1 下载u-boot源码
# 回到树莓派目录
$ cd ~/raspberry4B
$ git clone git://git.denx.de/u-boot.git
$ cd u-boot
# 切换到v2021.10分支
$ git checkout v2021.10 -b v2021.10
4.2 配置并编译u-boot
# 将aarch64-rpi4-linux-gnu交叉编译器的路径作为环境变量
$ export PATH=${HOME}/x-tools/aarch64-rpi4-linux-gnu/bin/:$PATH
# 选择树莓派4的参数做配置
$ make CROSS_COMPILE=aarch64-rpi4-linux-gnu- rpi_4_defconfig
# 编译生成可执行文件
$ make CROSS_COMPILE=aarch64-rpi4-linux-gnu- -j4
未完待续
下半篇内容:
- u-boot在树莓派上的运行
- linux内核的配置与编译
- 根文件系统的构建
- u-boot启动变量的设置
- 内核启动与串口控制台
【树莓派学习笔记】树莓派4B上运行uboot并从网络启动linux内核(上)相关推荐
- 树莓派学习笔记——GPIO功能学习
0.前言 树莓派现在越来越火,网上树莓派的资料也越来越多.树莓派源自英国,国外嵌入式开源领域具有良好的分享精神,树莓派各种集成库也层出不穷,下面推荐几个. [ python GPIO ] [开发语言] ...
- 树莓派学习笔记——定时向yeelink上传树莓派CPU温度
0 前言 本文通过python文件IO操作获得树莓派CPU温度信息,通过python request库周期性向yeelink平台上传温度,修改rc.local脚本使得该python脚本在开机时便在后台 ...
- 树莓派可以移动linux,树莓派学习笔记 篇三:树莓派4B 与移动存储设备的那些事儿...
树莓派学习笔记 篇三:树莓派4B 与移动存储设备的那些事儿 2019-11-24 23:03:55 39点赞 286收藏 14评论 本文是「树莓派学习笔记」系列的第三篇,将学习下 Linux 系统分区 ...
- 树莓派学习笔记(五)——烟雾浓度检测(ONENET云平台收发数据代码)
文章目录 前言 一.硬件连接 MQ-2 PCF8591 二.Onenet平台数据收发程序 onenetsub.py onenetget.py 三.程序 树莓派开启iic功能 完整程序 OneNet界面 ...
- 树莓派学习笔记—— 源代码方式安装opencv
0.前言 本文介绍如何在树莓派中通过编译源代码的方式安装opencv,并通过一个简单的例子说明如何使用opencv. 更多内容请参考--[树莓派学习笔记--索引博文] 1.下载若干依赖项 在开始安装之 ...
- 树莓派学习笔记(5):成功实现NAS家庭服务器(流媒体播放、文件共享及下载机)
一.家庭服务器实现的主要功能 http://www.cnblogs.com/xiaowuyi/p/4051238.html 1.流媒体播放服务:利用DLNA实现电视.手机.电脑播放其上面的媒体文件. ...
- 树莓派学习笔记——Python SQLite插入温度记录
0 前言 本文使用python向SQLite数据库中插入树莓派温度数据,SQLite数据库中包含一张只包含三个字段的记录表--参数名称,时间和温度值.本文重点解释Python操作SQlite的具体方法 ...
- 树莓派学习笔记——获取树莓派CPU温度
0 前言 本文通过文件操作读取树莓派CPU温度,在linux系统中任何设备的操作都被抽象成为文件读写,通过读取/sys/class/thermal/thermal_zone0/temp文件中的内容便获 ...
- 树莓派学习笔记——yeelink 远程控制LED
1.前言 前段时间玩了树莓派的GPIO,串口和I2C接口,把树莓派当成单片机来玩,期间深入分析了wiringPi.相对于单片机,以Linux为操作系统的树莓派在网络功能方面要强大的多,下面就结合当下流 ...
最新文章
- sockaddr_in 结构体
- 设备驱动模型之device-driver
- Oracle-批量修改语句及相关知识点
- 架构师成长之路:如何保证消息队列的高可用
- vs2010 sp1 安装 Silverlight4_Tools 提示 错误 解决办法
- python传文件_Python实现Windows和Linux之间互相传输文件(文件夹)的方法
- Vue:不使用webpack情况下将Cesium引入到Vue项目中
- winserve2016 万能驱动网卡_万能网卡驱动win10
- 解决notepad++ php代码美化
- 打破双亲委派么,怎么打破_打破统一垃圾收集规则
- 理解shallow heap 和 retained heap
- SpringBoot整合极光推送
- 你若安好便是晴天nbsp;---------…
- 无线传感器网络路由协议
- 下载 线代 薛威_考研线代李永乐真的首选吗?
- GD32VF103_定时器中断
- 我们都是代码接盘侠,请做一位好前任!
- 张艾迪(创始人): 趣味励志
- springmvc+ajaxfileupload异步上传
- blender 贴图导入与连接 学习日志