Skip to content

Instantly share code, notes, and snippets.

@abelardojarab
Created February 1, 2018 07:14
Show Gist options
  • Select an option

  • Save abelardojarab/49f5226f67cf5ececd59357bc7ba2152 to your computer and use it in GitHub Desktop.

Select an option

Save abelardojarab/49f5226f67cf5ececd59357bc7ba2152 to your computer and use it in GitHub Desktop.
Dump device feature list (Python code) - list PCI feature devices
#!/usr/bin/python
"""Dump Device Feature List"""
# Copyright(c) 2015-2017, Intel Corporation
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Intel Corporation nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import mmap, sys, binascii, struct, os
MAPMASK = mmap.PAGESIZE-1
def decode_dfh(data):
retval = {
"Raw DFH " : data,
"Feature Type " : (data >> 60) & 0xF,
"AFU Minor Revision " : (data >> 48) & 0xF,
"End of List " : (data >> 40) & 0x1,
"Next DFH Byte Offset " : (data >> 16) & 0xFFFFFF,
"AFU Major Revision / Feature Revision " : (data >> 12) & 0xF,
"Feature ID " : data & 0xFFF
}
return retval
def print_dfh(fields, offset):
for key, value in fields.iteritems():
print key + ": " + hex(value)
def walk_dfl(mm, base, offset):
print "==== DFH at " + hex(base + offset) + "===="
# read data (64 bit)
data_mm = mm[offset:offset+8]
data_bin = struct.unpack('<Q', data_mm)[0]
fields = decode_dfh(data_bin)
print_dfh(fields, offset)
eol = (data_bin >> 40) & 0x1
next_dfh = (data_bin >> 16) & 0xFFFFFF
if eol != 1 and next_dfh > 0:
walk_dfl(mm, base, offset+next_dfh)
if __name__ == "__main__":
if len(sys.argv) < 3:
sys.stderr.write("USAGE: dump_dfl.py <resource> <offset>\n")
sys.exit(-1)
address = int(sys.argv[2], 0)
base = address & ~MAPMASK
offset = address & MAPMASK
# mmap PCI resource
with open(sys.argv[1], "rb", 0) as f:
size = os.path.getsize(sys.argv[1])
mm = mmap.mmap(f.fileno(), size-base, mmap.MAP_SHARED, mmap.PROT_READ, 0, base)
walk_dfl(mm, base, offset)
# close mapping
mm.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment