我正在为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')希望有人能告诉我如何解决这个问题。
发布于 2014-07-29 11:34:19
我忙着解决这个问题。不要通过"textSec.stream.read“手动读取流中的数据,而是使用"textSec.data()”。Internaly (参见"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')坦克为评论!
https://stackoverflow.com/questions/24997541
复制相似问题