xref: /xnu-11215.1.10/tools/lldbmacros/nvram.py (revision 8d741a5de7ff4191bf97d57b9f54c2f6d4a15585)
1from ioreg import *
2
3@lldb_command("shownvram")
4def PrintNvramVars(cmd_args=[]):
5    """
6    Print NVRAM variables.
7    """
8    dt_plane = GetRegistryPlane("IODeviceTree")
9    if dt_plane is None:
10        raise ValueError("Couldn't obtain a pointer to IODeviceTree")
11
12    # Registry API functions operate on "plane" global variable
13    global plane
14    prev_plane = plane
15    plane = dt_plane
16    options = FindRegistryObjectRecurse(kern.globals.gRegistryRoot, "options")
17    # Restore original plane after getting options
18    plane = prev_plane
19    if options is None:
20        print("Couldn't obtain options IORegistryEntry")
21        return
22
23    var_dict = kern.GetValueFromAddress('((IODTNVRAM*)' + hex(options) + ')->_varDict', 'OSDictionary *')
24    if var_dict is None:
25        print("Couldn't obtain varDict")
26        return
27
28    for x in range(var_dict.count):
29        name = var_dict.dictionary[x].key.string
30        value = var_dict.dictionary[x].value
31
32        # get value type
33        value_info = GetObjectTypeStr(value)
34        if value_info is None:
35            print("Couldn't obtain object type for name:", name, "value:", value)
36            continue
37        srch = re.search(r'vtable for ([A-Za-z].*)', value_info)
38        if not srch:
39            print("Couldn't find type in value_info:", value_info)
40            continue
41        value_type = srch.group(1)
42
43        if (value_type == 'OSString'):
44            print(name, '=', GetString(value))
45        elif (value_type == 'OSData'):
46            data_ptr = Cast(value.data, 'uint8_t *')
47            print (name, '= ', end ='')
48            data_buffer = ""
49            for i in range(value.length):
50                if ((data_ptr[i] >= 0x20 and data_ptr[i] <= 0x7e) and chr(data_ptr[i]) != '%'):
51                    data_buffer += chr(data_ptr[i])
52                else:
53                    data_buffer += "%%%02x" % data_ptr[i]
54            print (data_buffer)
55        elif (value_type == 'OSNumber'):
56            print(name, '=', GetNumber(value))
57        elif (value_type == 'OSBoolean'):
58            print(name, '=', GetBoolean(value))
59        else:
60            print("Invalid type:", value_type)