├── .gitignore ├── LICENSE ├── README.md ├── devmem ├── __init__.py └── __main__.py ├── examples └── phymdio.py └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | /dist 3 | /devmem.egg-info 4 | __pycache__ 5 | *.swp 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Kyle Manna 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Python DevMem 2 | ============= 3 | 4 | [![](https://img.shields.io/pypi/v/devmem.svg?maxAge=3600)](https://pypi.org/project/devmem/) 5 | 6 | This is designed primarily for use with accessing /dev/mem on OMAP platforms. 7 | It should work on other platforms and work to mmap() files rather then just 8 | /dev/mem, but these use cases aren't well tested. 9 | 10 | All file accesses are aligned to DevMem.word bytes, which is 4 bytes on ARM 11 | platforms to avoid data abort faults when accessing peripheral registers. 12 | 13 | Usage 14 | ----- 15 | 16 | Usage: devmem.py [options] 17 | 18 | Options: 19 | -h, --help show this help message and exit 20 | -r ADDR, --read=ADDR read a value 21 | -w ADDR VALUE, --write=ADDR VALUE 22 | write a value 23 | -n NUM, --num=NUM number of words to read 24 | -s WORD_SIZE, --word-size=WORD_SIZE 25 | size of word when displayed 26 | -m FILE, --mmap=FILE file to open with mmap() 27 | -v provide more information regarding operation 28 | -d provide debugging information 29 | 30 | 31 | Speed 32 | ----- 33 | 34 | Initial testing on a BeagleBoard-xM (Cortex-A8 in a TI DM3730) shows that 35 | starting up the python interpreter is pretty slow: 36 | 37 | # time (echo | python) 38 | 39 | real 0m0.859s 40 | user 0m0.750s 41 | sys 0m0.102s 42 | 43 | # time python ./pydevmem.py -r 0x4830a204 -n 8 44 | 0x4830a204: 1b89102f 00000000 00000000 000000f0 45 | 0x4830a214: cafeb891 0c030016 015739eb 1ff00000 46 | 47 | real 0m1.109s 48 | user 0m0.977s 49 | sys 0m0.133s 50 | 51 | # time python -S ./pydevmem.py -r 0x4830a204 -n 8 52 | 0x4830a204: 1b89102f 00000000 00000000 000000f0 53 | 0x4830a214: cafeb891 0c030016 015739eb 1ff00000 54 | 55 | real 0m0.659s 56 | user 0m0.602s 57 | sys 0m0.047s 58 | 59 | # time python -S ./pydevmem.pyc -r 0x4830a204 -n 8 60 | 0x4830a204: 1b89102f 00000000 00000000 000000f0 61 | 0x4830a214: cafeb891 0c030016 015739eb 1ff00000 62 | 63 | real 0m0.647s 64 | user 0m0.508s 65 | sys 0m0.133s 66 | 67 | 68 | System information for those tests: 69 | 70 | Linux omap 3.0.6-x3 #1 SMP Wed Oct 5 07:19:24 UTC 2011 armv7l GNU/Linux 71 | 72 | python 2.7.2-7ubuntu2 73 | python-configobj 4.7.2+ds-3 74 | python-minimal 2.7.2-7ubuntu2 75 | python2.7 2.7.2-5ubuntu1 76 | python2.7-minimal 2.7.2-5ubuntu1 77 | 78 | Something needs to be sped up to make python start-up in a reasonable amount 79 | of time. 80 | -------------------------------------------------------------------------------- /devmem/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """ 3 | This is designed primarily for use with accessing /dev/mem on OMAP platforms. 4 | It should work on other platforms and work to mmap() files rather then just 5 | /dev/mem, but these use cases aren't well tested. 6 | 7 | All file accesses are aligned to DevMem.word bytes, which is 4 bytes on ARM 8 | platforms to avoid data abort faults when accessing peripheral registers. 9 | 10 | References: 11 | http://wiki.python.org/moin/PythonSpeed/PerformanceTips 12 | http://www.python.org/dev/peps/pep-0008/ 13 | 14 | """ 15 | 16 | import os 17 | import sys 18 | import mmap 19 | import struct 20 | 21 | class DevMemBuffer: 22 | """This class holds data for objects returned from DevMem class. It allows an easy way to print hex data""" 23 | 24 | def __init__(self, base_addr, data): 25 | self.data = data 26 | self.base_addr = base_addr 27 | 28 | def __len__(self): 29 | return len(self.data) 30 | 31 | def __getitem__(self, key): 32 | return self.data[key] 33 | 34 | def __setitem__(self, key, value): 35 | self.data[key] = value 36 | 37 | def hexdump(self, word_size = 4, words_per_row = 4): 38 | # Build a list of strings and then join them in the last step. 39 | # This is more efficient then concat'ing immutable strings. 40 | 41 | d = self.data 42 | dump = [] 43 | 44 | word = 0 45 | while (word < len(d)): 46 | dump.append('0x{0:02x}: '.format(self.base_addr 47 | + word_size * word)) 48 | 49 | max_col = word + words_per_row 50 | if max_col > len(d): max_col = len(d) 51 | 52 | while word < max_col: 53 | # If the word is 4 bytes, then handle it and continue the 54 | # loop, this should be the normal case 55 | if word_size == 4: 56 | dump.append(" {0:08x} ".format(d[word])) 57 | 58 | # Otherwise the word_size is not an int, pack it so it can be 59 | # un-packed to the desired word size. This should blindly 60 | # handle endian problems (Verify?) 61 | elif word_size == 2: 62 | for halfword in struct.unpack('HH', struct.pack('I',(d[word]))): 63 | dump.append(" {0:04x}".format(halfword)) 64 | 65 | elif word_size == 1: 66 | for byte in struct.unpack('BBBB', struct.pack('I',(d[word]))): 67 | dump.append(" {0:02x}".format(byte)) 68 | 69 | word += 1 70 | 71 | dump.append('\n') 72 | 73 | # Chop off the last new line character and join the list of strings 74 | # in to a single string 75 | return ''.join(dump[:-1]) 76 | 77 | def __str__(self): 78 | return self.hexdump() 79 | 80 | 81 | class DevMem: 82 | """Class to read and write data aligned to word boundaries of /dev/mem""" 83 | 84 | # Size of a word that will be used for reading/writing 85 | word = 4 86 | mask = ~(word - 1) 87 | f = None 88 | 89 | def __init__(self, base_addr, length = 1, filename = '/dev/mem', 90 | debug = 0): 91 | 92 | if base_addr < 0 or length < 0: raise AssertionError 93 | self._debug = debug 94 | 95 | self.base_addr = base_addr & ~(mmap.PAGESIZE - 1) 96 | self.base_addr_offset = base_addr - self.base_addr 97 | 98 | stop = base_addr + length * self.word 99 | if (stop % self.mask): 100 | stop = (stop + self.word) & ~(self.word - 1) 101 | 102 | self.length = stop - self.base_addr 103 | self.fname = filename 104 | 105 | # Check filesize (doesn't work with /dev/mem) 106 | #filesize = os.stat(self.fname).st_size 107 | #if (self.base_addr + self.length) > filesize: 108 | # self.length = filesize - self.base_addr 109 | 110 | self.debug('init with base_addr = {0} and length = {1} on {2}'. 111 | format(hex(self.base_addr), hex(self.length), self.fname)) 112 | 113 | # Open file and mmap 114 | self.f = os.open(self.fname, os.O_RDWR | os.O_SYNC) 115 | self.mem = mmap.mmap(self.f, self.length, mmap.MAP_SHARED, 116 | mmap.PROT_READ | mmap.PROT_WRITE, 117 | offset=self.base_addr) 118 | 119 | def __del__(self): 120 | if self.f: 121 | os.close(self.f) 122 | 123 | def read(self, offset, length): 124 | """Read length number of words from offset""" 125 | 126 | if offset < 0 or length < 0: raise AssertionError 127 | 128 | # Make reading easier (and faster... won't resolve dot in loops) 129 | mem = self.mem 130 | 131 | self.debug('reading {0} bytes from offset {1}'. 132 | format(length * self.word, hex(offset))) 133 | 134 | # Compensate for the base_address not being what the user requested 135 | # and then seek to the aligned offset. 136 | virt_base_addr = self.base_addr_offset & self.mask 137 | mem.seek(virt_base_addr + offset) 138 | 139 | # Read length words of size self.word and return it 140 | data = [] 141 | for i in range(length): 142 | data.append(struct.unpack('I', mem.read(self.word))[0]) 143 | 144 | abs_addr = self.base_addr + virt_base_addr 145 | return DevMemBuffer(abs_addr + offset, data) 146 | 147 | 148 | def write(self, offset, din): 149 | """Write length number of words to offset""" 150 | 151 | if offset < 0 or len(din) <= 0: raise AssertionError 152 | 153 | self.debug('writing {0} bytes to offset {1}'. 154 | format(len(din) * self.word, hex(offset))) 155 | 156 | # Make reading easier (and faster... won't resolve dot in loops) 157 | mem = self.mem 158 | 159 | # Compensate for the base_address not being what the user requested 160 | # fix double plus offset 161 | #offset += self.base_addr_offset 162 | 163 | # Check that the operation is going write to an aligned location 164 | if (offset & ~self.mask): raise AssertionError 165 | 166 | # Seek to the aligned offset 167 | virt_base_addr = self.base_addr_offset & self.mask 168 | mem.seek(virt_base_addr + offset) 169 | 170 | # Read until the end of our aligned address 171 | for i in range(len(din)): 172 | self.debug('writing at position = {0}: 0x{1:x}'. 173 | format(self.mem.tell(), din[i])) 174 | # Write one word at a time 175 | mem.write(struct.pack('I', din[i])) 176 | 177 | def debug_set(self, value): 178 | self._debug = value 179 | 180 | def debug(self, debug_str): 181 | if self._debug: print('DevMem Debug: {0}'.format(debug_str)) 182 | -------------------------------------------------------------------------------- /devmem/__main__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import argparse 4 | import os 5 | import sys 6 | 7 | from . import DevMem 8 | 9 | """If this is run as a script (rather then imported as a module) it provides some basic functionality out of the box""" 10 | 11 | # Need to fix up argparse's prog value as it's broken and returns `__main__.py` 12 | # Upstream bug: 13 | # * https://bugs.python.org/issue22240 14 | # pip workaround: 15 | # * https://github.com/pypa/pip/blob/08c99b6e00135ca8df2e98db58aa0b701b971c64/src/pip/_internal/utils/misc.py#L124-L134 16 | def get_prog() -> str: 17 | """Determine the program name if invoked directly or as a module""" 18 | 19 | name = ( 20 | sys.argv[0] 21 | if globals().get("__spec__") is None 22 | else __spec__.name.partition(".")[0] 23 | ) 24 | try: 25 | prog = os.path.basename(sys.argv[0]) 26 | if prog in ("__main__.py", "-c"): 27 | return "{} -m {}".format(sys.executable, name) 28 | else: 29 | return prog 30 | except (AttributeError, TypeError, IndexError): 31 | pass 32 | 33 | return name 34 | 35 | 36 | def main() -> int: 37 | """Main function with useful demo application""" 38 | 39 | parser = argparse.ArgumentParser(prog=get_prog()) 40 | 41 | group = parser.add_mutually_exclusive_group(required=True) 42 | group.add_argument( 43 | "-r", "--read", metavar="ADDR", type=lambda x: int(x, 0), help="read a value" 44 | ) 45 | group.add_argument( 46 | "-w", 47 | "--write", 48 | help="write a value", 49 | nargs=2, 50 | type=lambda x: int(x, 0), 51 | metavar=("ADDR", "VALUE"), 52 | ) 53 | 54 | parser.add_argument( 55 | "-n", 56 | "--num", 57 | help="number of words to read", 58 | type=lambda x: int(x, 0), 59 | default=1, 60 | ) 61 | 62 | parser.add_argument( 63 | "-s", 64 | "--word-size", 65 | help="size of word when displayed", 66 | type=int, 67 | default=4, 68 | choices=[1, 2, 4], 69 | ) 70 | 71 | parser.add_argument( 72 | "-m", 73 | "--mmap", 74 | metavar="FILE", 75 | help="file to open with mmap()", 76 | type=str, 77 | default="/dev/mem", 78 | ) 79 | 80 | parser.add_argument( 81 | "-v", 82 | "--verbose", 83 | action="store_true", 84 | help="provide more information regarding operation", 85 | ) 86 | 87 | parser.add_argument( 88 | "-d", "--debug", action="store_true", help="provide debugging information" 89 | ) 90 | 91 | args = parser.parse_args() 92 | 93 | # Check for sane arguments 94 | if args.num < 0: 95 | parser.print_help() 96 | print("\nError: Invalid num of words specified") 97 | return 1 98 | 99 | # Only support writing one word at a time, force this 100 | if args.write is not None and args.num != 1: 101 | print("Warning: Forcing number of words to 1 for set operation\n") 102 | args.num = 1 103 | 104 | # Determine base address to operate on 105 | addr = args.write[0] if args.write else args.read 106 | 107 | # Create the Dev Mem object that does the magic 108 | mem = DevMem(addr, length=args.num, filename=args.mmap, debug=args.debug) 109 | 110 | # Perform the actual read or write 111 | if args.write is not None: 112 | if args.verbose: 113 | print( 114 | "Value before write:\t{0}".format( 115 | mem.read(0x0, args.num).hexdump(args.word_size) 116 | ) 117 | ) 118 | 119 | mem.write(0x0, [args.write[1]]) 120 | 121 | if args.verbose: 122 | print( 123 | "Value after write:\t{0}".format( 124 | mem.read(0x0, args.num).hexdump(args.word_size) 125 | ) 126 | ) 127 | else: 128 | print(mem.read(0x0, args.num).hexdump(args.word_size)) 129 | 130 | 131 | if __name__ == "__main__": 132 | sys.exit(main()) 133 | -------------------------------------------------------------------------------- /examples/phymdio.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """ 3 | This tool dumps the MDIO register on systems similar to the Beaglebone and 4 | TI814x platforms. 5 | """ 6 | 7 | import sys 8 | import devmem 9 | 10 | class PhyMDIO: 11 | def __init__(self, base_addr): 12 | self.base_addr = base_addr 13 | self.mem = devmem.DevMem(base_addr, 0x100, "/dev/mem", 0) 14 | 15 | def get_mdio(self, offset): 16 | reg_go = 1 << 31 17 | reg_regaddr = offset << 21 18 | reg = 0 19 | reg = reg_go | reg_regaddr 20 | self.mem.write(0x80, [reg]) 21 | 22 | buf = self.mem.read(0x80, 1) 23 | while buf[0] & reg_go: 24 | buf = self.mem.read(0x80, 1) 25 | 26 | buf.base_addr = offset 27 | buf[0] &= 0xffff 28 | 29 | return buf 30 | 31 | def get_cpsw(self, offset): 32 | return self.mem.read(offset, 1) 33 | 34 | def dump_mdio(base_addr): 35 | phy = PhyMDIO(base_addr) 36 | 37 | cpsw_name = ["MDIO Version", 38 | "MDIO Control", 39 | "PHY Alive Status", 40 | "PHY Link Status"] 41 | 42 | mii_name = ["MII Basic Mode Control", 43 | "MII Basic Mode Status", 44 | "MII PHY ID 1", 45 | "MII PHY ID 2", 46 | "MII Advertisement Control", 47 | "MII Link Parner Ability", 48 | "MII Expansion", 49 | "MII Manf Specific", 50 | "MII Manf Specific", 51 | "MII 1000BASE-T Control", 52 | "MII 1000BASE-T Status", 53 | "MII Manf Specific", 54 | "MII Manf Specific", 55 | "MII Manf Specific", 56 | "MII Manf Specific", 57 | "MII Extended Status", 58 | "MII Manf Specific", 59 | "MII Manf Specific", 60 | "MII Disconnect Counter", 61 | "MII False Carrier Counter", 62 | "MII N-way Auto Neg Test", 63 | "MII Receive Error Counter", 64 | "MII Silicon Revision", 65 | "MII TPI Status for 10 Mbps", 66 | "MII Network Interface Config", 67 | "MII Manf Specific", 68 | "MII Manf Specific", 69 | "MII Manf Specific", 70 | "MII Manf Specific", 71 | "MII Manf Specific", 72 | "MII Manf Specific", 73 | "MII Manf Specific"] 74 | 75 | for i in range(4): 76 | print("{0!s:38} {1}".format(cpsw_name[i] + ' Register:', 77 | phy.get_cpsw(i << 2))) 78 | 79 | # Blank line 80 | print() 81 | 82 | for i in range(0x20): 83 | print("{0!s:38} {1}".format(mii_name[i] + ' Register:', 84 | phy.get_mdio(i).hexdump(2, 2))) 85 | 86 | if __name__ == '__main__': 87 | 88 | sys.exit(dump_mdio(0x4a101000)) 89 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import pathlib 4 | from setuptools import setup 5 | 6 | # The text of the README file 7 | README = (pathlib.Path(__file__).parent / "README.md").read_text() 8 | 9 | setup( 10 | name="devmem", 11 | version="0.1.0", 12 | description="Python devmem clone", 13 | long_description=README, 14 | long_description_content_type="text/markdown", 15 | url="https://github.com/kylemanna/pydevmem", 16 | author="Kyle Manna", 17 | author_email="kyle@kylemanna.com", 18 | license="MIT", 19 | classifiers=[ 20 | "License :: OSI Approved :: MIT License", 21 | "Programming Language :: Python :: 3", 22 | "Programming Language :: Python :: 3.8", 23 | ], 24 | packages=["devmem"], 25 | include_package_data=True, 26 | entry_points={ 27 | "console_scripts": [ 28 | "pydevmem=devmem.__main__:main", 29 | ] 30 | }, 31 | ) 32 | --------------------------------------------------------------------------------