用于使用u-boot映像和环境变量进行操作的开源库
uboot的Python项目详细描述
pyuboot是一个基于python的开源库,用于处理 U-boot映像和环境变量。分布有以下内容 命令行实用程序(工具):
- envimg-编辑环境的工具 U-boot映像中的变量
- mkenv-生成/提取u-boot的工具 进入/离开二进制blob的环境变量
- mkimg-使用u-boot进行操作的工具 可执行图像(zimage、脚本等)
安装
$ pip install uboot
要从shell中的master branch execute安装最新版本 以下命令:
$ pip install -r https://raw.githubusercontent.com/molejar/pyUBoot/master/requirements.txt $ pip install -U https://github.com/molejar/pyUBoot/archive/master.zip
如果要进行开发,请从克隆源安装:
$ git clone https://github.com/molejar/pyUBoot.git
$ cd pyUBoot
$ pip install -r requirements.txt
$ pip install -U -e .
注意:运行这些命令可能会遇到权限问题。 以下是一些解决方法:
- 使用sudo运行以全局安装pyuboot和依赖项
- 指定--user选项以本地安装到您的家庭中 目录(如果没有,则将“~/.local/bin”导出到路径变量中)。
- 在 virtualenv本地到 特定项目工作集。
用法
第一个例子展示了如何使用^{tt3}中的^{tt4}类$ 代码中的模块。
importuboot# --------------------------------------------------------------------------------# create env blob# --------------------------------------------------------------------------------env=uboot.EnvBlob(name="U-Boot Variables")env.redundant=Trueenv.set("bootdelay","3")env.set("stdin","serial")env.set("stdout","serial")# --------------------------------------------------------------------------------# save env blob as binary file# --------------------------------------------------------------------------------withopen("env.img",'wb')asf:f.write(env.export())# --------------------------------------------------------------------------------# save env blob in readable format as text file# --------------------------------------------------------------------------------withopen("env.txt",'w')asf:f.write(env.store())# --------------------------------------------------------------------------------# parse env blob from binary file# --------------------------------------------------------------------------------withopen("env.img",'rb')asf:env.parse(f.read())# print env blob infoprint("U-Boot enviroment blob loaded from env.img file:")print(env)print()# --------------------------------------------------------------------------------# load env blob from text file# --------------------------------------------------------------------------------withopen("env.txt",'r')asf:env.load(f.read())# print env blob infoprint("U-Boot enviroment blob loaded from env.txt file:")print(env)
第二个例子展示了如何使用 uboot模块。
importuboot# --------------------------------------------------------------------------------# create dummy firmware image (u-boot executable image)# --------------------------------------------------------------------------------fwimg=uboot.StdImage(bytes([1]*512),name="Firmware Test Image",laddr=0,eaddr=0,arch=uboot.EnumArchType.ARM,os=uboot.EnumOsType.LINUX,image=uboot.EnumImageType.FIRMWARE,compress=uboot.EnumCompressionType.NONE)# --------------------------------------------------------------------------------# create script image (u-boot executable image)# --------------------------------------------------------------------------------scimg=uboot.ScriptImage()scimg.Name="Test Script Image"scimg.OsType=uboot.EnumOsType.LINUXscimg.ArchType=uboot.EnumArchType.ARMscimg.Compression=uboot.EnumCompressionType.NONEscimg.EntryAddress=0scimg.LoadAddress=0scimg.append("echo","'===== U-Boot settings ====='")scimg.append("setenv","stdin serial")scimg.append("setenv","stdout serial")scimg.append("setenv","rootdev mmcblk2p2")# --------------------------------------------------------------------------------# create multi-file image# --------------------------------------------------------------------------------mimg=uboot.MultiImage(name="Multi-File Test Image",laddr=0,eaddr=0,arch=uboot.EnumArchType.ARM,os=uboot.EnumOsType.LINUX,compress=uboot.EnumCompressionType.NONE)mimg.append(fwimg)mimg.append(scimg)# print created image infoprint(mimg)# --------------------------------------------------------------------------------# save created image into file: uboot_mimg.img# --------------------------------------------------------------------------------withopen("uboot_mimg.img","wb")asf:f.write(mimg.export())# --------------------------------------------------------------------------------# open and read image file: uboot_mimg.img# --------------------------------------------------------------------------------withopen("uboot_mimg.img","rb")asf:data=f.read()# --------------------------------------------------------------------------------# parse binary data into new img object of specific image type# --------------------------------------------------------------------------------img=uboot.parse_img(data)# print parsed image infoprint(img)