summaryrefslogtreecommitdiffstats
path: root/utils/ipmi-mailbox-config.py
blob: 1b57ebb90e97bcdfd21dab37782ca6429cbc152d (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3

import argparse
import subprocess

def send_block_read_command(hostname, username, password, index, dry_run):

    if hostname is None and dry_run:
        hostname = "<hostname>"

    cmd = "ipmitool -I lanplus -H " + hostname
    if username is not None:
        cmd = cmd + " -U " + username
    if password is not None:
        cmd = cmd + " -P " + password

    # chassis netfn, get-sys-boot-options, parameter 7, set-sel, block-sel
    cmd = cmd + " raw 0x00 0x09 0x07 " + hex(index) + " 0x00 "

    print(cmd)
    if not dry_run:
        rc = subprocess.call(cmd, shell=True)
        if rc != 0:
            print("Command returned error: {}".format(rc))

def send_block_command(hostname, username, password, block, index, dry_run):

    if hostname is None and dry_run:
        hostname = "<hostname>"

    cmd = "ipmitool -I lanplus -H " + hostname
    if username is not None:
        cmd = cmd + " -U " + username
    if password is not None:
        cmd = cmd + " -P " + password

    # chassis netfn, set-sys-boot-options, parameter 7, set-sel, block-sel
    cmd = cmd + " raw 0x00 0x08 0x07 " + hex(index) + " " + block

    print(cmd)
    if not dry_run:
        rc = subprocess.call(cmd, shell=True)
        if rc != 0:
            print("Command returned error: {}".format(rc))

def construct_buffer(config, max_blocks):

    # Add IBM's IANA prefix
    raw = bytes([0x02, 0x00, 0x00]) + config.encode('ascii')

    n_blocks = int(len(raw) / 16)
    if len(raw) % 16 != 0:
        n_blocks += 1

    if n_blocks > 255:
        print("ERROR: buffer would require {} blocks".format(n_blocks) +
                " which is more than hypothetically possible (255)")
        return None

    if n_blocks > max_blocks:
        print("ERROR: buffer would require {} blocks".format(n_blocks) +
                " which is more than max_blocks ({})".format(max_blocks))
        return None

    if n_blocks > 5:
        print("Warning: buffer would require {} blocks".format(n_blocks) +
                "which is more than some BMCs support")

    blocks = []
    rem = len(raw)
    for i in range(n_blocks):
        block = ""
        if rem >= 16:
            last = 16
        else:
            last = rem

        for j in range(16):
            if j < last:
                block += "{:#02x} ".format(raw[i * 16 + j])
            else:
                # Pad out to 16 bytes
                block += "0x00 "


        blocks.append(block)
        rem -= last

    if n_blocks < max_blocks:
        for i in range(max_blocks - n_blocks):
            blocks.append("0x00 0x00 0x00 0x00 " +
                          "0x00 0x00 0x00 0x00 " +
                          "0x00 0x00 0x00 0x00 " +
                          "0x00 0x00 0x00 0x00")

    return blocks

def construct_empty_buffer(max_blocks):

    blocks = []
    for i in range(max_blocks):
        blocks.append("0x00 0x00 0x00 0x00 " +
                      "0x00 0x00 0x00 0x00 " +
                      "0x00 0x00 0x00 0x00 " +
                      "0x00 0x00 0x00 0x00")

    return blocks

def main():

    parser = argparse.ArgumentParser()
    parser.add_argument("-b", "--bmc-hostname")
    parser.add_argument("-u", "--username")
    parser.add_argument("-p", "--password")
    parser.add_argument("-n", "--dry-run", action="store_true")
    parser.add_argument("-c", "--config")
    parser.add_argument("-x", "--clear", action="store_true")
    parser.add_argument("-d", "--dump", action="store_true")
    parser.add_argument("-m", "--max-blocks")

    args = parser.parse_args()

    if not args.dry_run and args.bmc_hostname is None:
        print("No hostname specified!")
        return -1

    if args.config and args.clear:
        print("Can't specify --config and --clear together")
        return -1

    if args.max_blocks:
        n_blocks = int(args.max_blocks)
    else:
        n_blocks = 16


    if args.config or args.clear:
        if args.config:
            blocks = construct_buffer(args.config, int(args.max_blocks))
        if args.clear:
            blocks = construct_empty_buffer(int(args.max_blocks))
        if blocks is None:
            print("Failed to construct buffer")
            return -1

        print("{} blocks to send".format(len(blocks)))
        print("---------------------------------------")
        for i in range(len(blocks)):
            try:
                send_block_command(args.bmc_hostname, args.username, args.password,
                        blocks[i], i, args.dry_run)
            except Exception as e:
                print(e)
                print("Error sending block {}".format(i))
                return -1
            i += 1

    if args.dump:
        print("\nReading {} blocks".format(n_blocks))
        print("---------------------------------------")
        for i in range(n_blocks):
            send_block_read_command(args.bmc_hostname, args.username,
                    args.password, i, args.dry_run)

if __name__ == "__main__":
    main()
OpenPOWER on IntegriCloud