2

我正在为 ATXmega128A4U 创建自己的引导加载程序。要使用引导加载程序,我想将固件的 ELF 文件转换为 ATXmega 中使用的内存映射。为此,我使用 python 和模块“pyelftools”。它的文档很差,所以我遇到了一个问题:我不知道我可以使用什么信息从这些部分的数据中获取地址、偏移量等。我的目标是创建一个字节数组,将数据/代码复制到其中并将其传输到引导程序。下面是我的代码:

import sys

# If pyelftools is not installed, the example can also run from the root or
# examples/ dir of the source distribution.
sys.path[0:0] = ['.', '..']

from elftools.common.py3compat import bytes2str
from elftools.elf.elffile import ELFFile

# 128k flash for the ATXmega128a4u
flashsize = 128 * 1024


def process_file(filename):
    with open(filename, 'rb') as f:
        # get the data
        elffile = ELFFile(f)
        dataSec = elffile.get_section_by_name(b'.data')        
        textSec = elffile.get_section_by_name(b'.text')
        # prepare the memory
        flashMemory = bytearray(flashsize)
        # the data section
        startAddr = dataSec.header.sh_offset
        am = dataSec.header.sh_size
        i = 0
        while i < am:
            val = dataSec.stream.read(1)
            flashMemory[startAddr] = val[0]
            startAddr += 1
            i += 1
        # the text section
        startAddr = textSec.header.sh_offset
        am = textSec.header.sh_size
        i = 0
        while i < am:
            print(str(startAddr) + ' : ' + str(i))
            val = textSec.stream.read(1)
            flashMemory[startAddr] = val[0]
            startAddr += 1
            i += 1
    print('finished')

if __name__ == '__main__':
    process_file('firmware.elf')

希望有人能告诉我如何解决这个问题。

4

1 回答 1

1

我设法解决了这个问题。不要通过“textSec.stream.read”从流中手动读取数据,而是使用“textSec.data()”。内部(参见“sections.py”)文件中的查找操作已完成。之后读取数据。结果将是有效的数据块。以下代码读取 atxmega 固件的代码(文本)部分并将其复制到具有 atxmega128a4u 设备闪存布局的字节数组中。@vlas_tepesch:不需要十六进制对话,避免了 64k 陷阱。

sys.path[0:0] = ['.', '..']

from elftools.common.py3compat import bytes2str
from elftools.elf.elffile import ELFFile

# 128k flash for the ATXmega128a4u
flashsize = 128 * 1024


def __printSectionInfo (s):
    print ('[{nr}] {name} {type} {addr} {offs} {size}'.format(
                nr = s.header['sh_name'],
                name = s.name,
                type = s.header['sh_type'],
                addr = s.header['sh_addr'],
                offs = s.header['sh_offset'],
                size = s.header['sh_size']
                                                              )
           )

def process_file(filename):
    print('In file: ' + filename)
    with open(filename, 'rb') as f:
        # get the data
        elffile = ELFFile(f)
        print ('sections:')
        for s in elffile.iter_sections():
            __printSectionInfo(s)
        print ('get the code from the .text section')
        textSec = elffile.get_section_by_name(b'.text')
        # prepare the memory
        flashMemory = bytearray(flashsize)
        # the text section
        startAddr = textSec.header['sh_addr']
        val = textSec.data()

        flashMemory[startAddr:startAddr+len(val)] = val

        # print memory

        print('finished')

if __name__ == '__main__':
    process_file('firmware.elf')

坦克评论!

于 2014-07-29T11:34:19.027 回答