OpenHarmony的allinone.bin固件格式解析 原创

再见南丫岛
发布于 2022-3-22 17:35
浏览
2收藏

在开发完Hi3861的固件之后,需要对固件进行烧录。官方推荐直接使用allinone.bin固件。通过HiBurn软件加载该固件之后,会解析出三个bin文件。猜测allinone.bin中包含了这三个固件,以及一些配置参数。
OpenHarmony的allinone.bin固件格式解析-鸿蒙开发者社区
带着这个猜测,分析了一下openharmony关于编译部分的代码。在build\scripts目录下发现了packet_create.py文件。

device\hisilicon\hispark_pegasus\sdk_liteos\build\scripts\packet_create.py

于是与源码对比分析,发现确实该文件是打包的py文件。

def packet_bin(outputPath, inputList):
    pathList = []
    burnAddrList = []
    burnSizeList = []
    imageSizeList = []
    typeList = []
    for item in inputList:
        path, burnAddr, burnSize, type = item.split("|")
        imageSize = os.path.getsize(path)
        pathList.append(path)
        burnAddrList.append(int(burnAddr))
        burnSizeList.append(int(burnSize))
        imageSizeList.append(imageSize)
        typeList.append(int(type))

    print(pathList)
    print(burnAddrList)
    print(burnSizeList)
    print(imageSizeList)
    print(typeList)

    flag = 0xefbeaddf
    print(flag)
    crc = 0
    imageNum = len(pathList)
    headLen = imageNum*52 + 12
    totalFileSize = sum(imageSizeList) + headLen

    with open(outputPath, 'wb+') as file:
        file.write(struct.pack('IHHI', flag, crc, imageNum, totalFileSize))
        startIndex = headLen
        times = 0
        for path in pathList:
            pathName = os.path.basename(path)
            file.write(
                struct.pack('32sIIIII', bytes(pathName, 'ascii'), startIndex, imageSizeList[times], burnAddrList[times],
                            burnSizeList[times], typeList[times]))
            startIndex = startIndex + imageSizeList[times] + 16
            times += 1

        for path in pathList:
            with  open(path, 'rb+') as subfile:
                data = subfile.read()
                file.write(data)
                file.write(struct.pack('IIII', 0, 0, 0, 0))

        file.flush()
        file.seek(6)
        newdata = file.read(headLen - 6)
        crc16 = t.crcb(newdata)
        file.seek(4)
        file.write(struct.pack('H', crc16))

allinone.bin文件内容分为两部分,数据头和帧内容。
OpenHarmony的allinone.bin固件格式解析-鸿蒙开发者社区
以上为数据头内容。对比python的代码,可以分析出数据头中包含的内容为

名称 类型 内容
标志头 unsigned int 0xefbeaddf
CRC unsigned short
镜头个数 unsigned short 3
文件大小 unsigned int
文件名 字符串32s Hi3861_loader_signed.bin
索引地址 unsigned int
文件大小 unsigned int
Burn地址 unsigned int
xx unsigned int
xx unsigned int
文件名 字符串32s Hi3861_wifiiot_app_burn.bin
索引地址 unsigned int
文件大小 unsigned int
Burn地址 unsigned int
xx unsigned int
xx unsigned int
文件名 字符串32s Hi3861_boot_signed_B.bin
索引地址 unsigned int
文件大小 unsigned int
Burn地址 unsigned int
xx unsigned int
xx unsigned int

数据内容既对应文件的内容,然后每个文件后面有16个字节的0x00作为分割。
分析这个文件的原因是希望可以自己开发一个HiBurn工具,这样可以更加灵活的实现固件的烧录。后续会对HiBurn的工作原理,协议交互逻辑进行分析。

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
已于2022-7-13 18:04:31修改
5
收藏 2
回复
举报
1条回复
按时间正序
/
按时间倒序
红叶亦知秋
红叶亦知秋

跟着楼主探究了一遍

回复
2022-3-22 18:15:18
回复
    相关推荐