File: ata_struct_2_c_dump.py

package info (click to toggle)
diskscan 0.21-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,656 kB
  • sloc: ansic: 11,136; python: 338; xml: 138; sh: 41; makefile: 34
file content (79 lines) | stat: -rwxr-xr-x 2,312 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python

import sys
import yaml

def emit_func_bit(name, field, params):
	bit_params = dict(name=name, field=field, word=int(params[0]), bit=int(params[1]))
	print('printf("%%-40s: %%s\\n", "%(field)s", ata_get_%(name)s_%(field)s(buf) ? "true" : "false");' % bit_params)

def emit_func_string(name, field, params):
	bit_params = dict(name=name, field=field, word_start=int(params[0]), word_end=int(params[1]))
	print('{')
	print('char outbuf[1024];')
	print('ata_get_%(name)s_%(field)s(buf, outbuf);' % bit_params)
	print('printf("%%-40s: %%s\\n", "%(field)s", outbuf);' % bit_params)
	print('}')

def emit_func_bits(name, field, params):
	bit_params = dict(name=name, field=field, word_start=int(params[0]))
	print('printf("%%-40s: %%u\\n", "%(field)s", ata_get_%(name)s_%(field)s(buf));' % bit_params)

def emit_func_longword(name, field, params):
	bit_params = dict(name=name, field=field, word_start=int(params))
	print('printf("%%-40s: %%u\\n", "%(field)s", ata_get_%(name)s_%(field)s(buf));' % bit_params)

def emit_func_qword(name, field, params):
	bit_params = dict(name=name, field=field, word_start=int(params))
	print('printf("%%-40s: %%"PRIu64"\\n", "%(field)s", ata_get_%(name)s_%(field)s(buf));' % bit_params)

kinds = {
    'bit': emit_func_bit,
    'bits': emit_func_bits,
	'string': emit_func_string,
	'longword': emit_func_longword,
    'qword': emit_func_qword,
}

def emit_header_single(name, struct):
	field_names = list(struct.keys())
	field_names.sort()

	for field in field_names:
		info = struct[field]
		keys = list(info.keys())
		assert(len(keys) == 1)
		kind = keys[0]
		params = info[kind]

		kinds[kind](name, field, params)

def emit_header(structs):
	for name, struct in list(structs.items()):
		print('void dump_%s(const unsigned char *buf)' % name)
		print('{')
		emit_header_single(name, struct)
		print('}')

def emit_prefix():
	print('#include "ata.h"')
	print('#include "ata_parse.h"')
	print('#include "ata_identify_dump.h"')
	print('#include <stdio.h>')
	print('#include <inttypes.h>')

def emit_suffix():
	print('')

def convert_def(filename):
	f = file(filename)
	structs = yaml.load(f)
	f.close()
	emit_header(structs)

if __name__ == '__main__':
	emit_prefix()
	filenames = sys.argv[1:]
	for filename in filenames:
		convert_def(filename)
	emit_suffix()