Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- 'extract and write nvram variables to CFE image'
- import sys
- NVRAM_OFFSET = 4096
- NVRAM_SIZE = 4096
- NVRAM_HEADER_SIZE = 20
- NVRAM_DATA_OFFSET = NVRAM_OFFSET + NVRAM_HEADER_SIZE
- def extract(filename):
- data = read(filename)
- nvram = data[NVRAM_OFFSET:NVRAM_OFFSET + NVRAM_SIZE]
- return filter(None, nvram[NVRAM_HEADER_SIZE:].split('\0'))
- def modify(cfefile, changesfile):
- values = [line.split('=', 1) for line in extract(cfefile)]
- valuesdict = dict(values)
- changes = dict([line.split('=', 1) for line in readlines(changesfile)])
- valuesdict.update(changes)
- for index in range(len(values)):
- key, value = values[index]
- if valuesdict[key] != value:
- print >>sys.stderr, 'changing value of "%s" from "%s" to "%s"' % (
- key, value, valuesdict[key])
- values[index][1] = valuesdict[key]
- del valuesdict[key]
- if valuesdict:
- print >>sys.stderr, 'failed! remaining data: %s' % valuesdict
- sys.exit(1)
- nvram_data = '\0'.join(['='.join([k, v]) for [k, v] in values])
- data = read(cfefile)
- padding = '\0' * (NVRAM_SIZE - NVRAM_HEADER_SIZE)
- data = data[:NVRAM_DATA_OFFSET] + \
- (nvram_data + padding)[:len(padding)] + \
- data[NVRAM_OFFSET + NVRAM_SIZE:]
- write(cfefile, data)
- def read(filename):
- input = open(filename)
- data = input.read()
- input.close()
- return data
- def readlines(filename):
- input = open(filename)
- data = input.readlines()
- input.close()
- return map(str.rstrip, data)
- def write(filename, data):
- output = open(filename, 'w')
- output.write(data)
- output.close()
- if __name__ == '__main__':
- if len(sys.argv) > 1:
- if sys.argv[1] == 'extract':
- for line in extract(sys.argv[2]):
- print line
- elif sys.argv[1] == 'modify':
- modify(*sys.argv[2:])
- else:
- print >>sys.stderr, 'only "extract" or "modify" accepted'
- else:
- print >> sys.stderr, \
- 'usage: %s extract binfile | modify binfile changesfile' % sys.argv[0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement