6 def send_block_read_command(hostname, username, password, index, dry_run):
8 if hostname is None and dry_run:
9 hostname = "<hostname>"
11 cmd = "ipmitool -I lanplus -H " + hostname
12 if username is not None:
13 cmd = cmd + " -U " + username
14 if password is not None:
15 cmd = cmd + " -P " + password
17 # chassis netfn, get-sys-boot-options, parameter 7, set-sel, block-sel
18 cmd = cmd + " raw 0x00 0x09 0x07 " + hex(index) + " 0x00 "
22 rc = subprocess.call(cmd, shell=True)
24 print("Command returned error: {}".format(rc))
26 def send_block_command(hostname, username, password, block, index, dry_run):
28 if hostname is None and dry_run:
29 hostname = "<hostname>"
31 cmd = "ipmitool -I lanplus -H " + hostname
32 if username is not None:
33 cmd = cmd + " -U " + username
34 if password is not None:
35 cmd = cmd + " -P " + password
37 # chassis netfn, set-sys-boot-options, parameter 7, set-sel, block-sel
38 cmd = cmd + " raw 0x00 0x08 0x07 " + hex(index) + " " + block
42 rc = subprocess.call(cmd, shell=True)
44 print("Command returned error: {}".format(rc))
46 def construct_buffer(config, max_blocks):
48 # Add IBM's IANA prefix
49 raw = bytes([0x02, 0x00, 0x00]) + config.encode('ascii')
51 n_blocks = int(len(raw) / 16)
52 if len(raw) % 16 != 0:
56 print("ERROR: buffer would require {} blocks".format(n_blocks) +
57 " which is more than hypothetically possible (255)")
60 if n_blocks > max_blocks:
61 print("ERROR: buffer would require {} blocks".format(n_blocks) +
62 " which is more than max_blocks ({})".format(max_blocks))
66 print("Warning: buffer would require {} blocks".format(n_blocks) +
67 "which is more than some BMCs support")
71 for i in range(n_blocks):
80 block += "{:#02x} ".format(raw[i * 16 + j])
89 if n_blocks < max_blocks:
90 for i in range(max_blocks - n_blocks):
91 blocks.append("0x00 0x00 0x00 0x00 " +
92 "0x00 0x00 0x00 0x00 " +
93 "0x00 0x00 0x00 0x00 " +
94 "0x00 0x00 0x00 0x00")
98 def construct_empty_buffer(max_blocks):
101 for i in range(max_blocks):
102 blocks.append("0x00 0x00 0x00 0x00 " +
103 "0x00 0x00 0x00 0x00 " +
104 "0x00 0x00 0x00 0x00 " +
105 "0x00 0x00 0x00 0x00")
111 parser = argparse.ArgumentParser()
112 parser.add_argument("-b", "--bmc-hostname")
113 parser.add_argument("-u", "--username")
114 parser.add_argument("-p", "--password")
115 parser.add_argument("-n", "--dry-run", action="store_true")
116 parser.add_argument("-c", "--config")
117 parser.add_argument("-x", "--clear", action="store_true")
118 parser.add_argument("-d", "--dump", action="store_true")
119 parser.add_argument("-m", "--max-blocks")
121 args = parser.parse_args()
123 if not args.dry_run and args.bmc_hostname is None:
124 print("No hostname specified!")
127 if args.config and args.clear:
128 print("Can't specify --config and --clear together")
132 n_blocks = int(args.max_blocks)
137 if args.config or args.clear:
139 blocks = construct_buffer(args.config, int(args.max_blocks))
141 blocks = construct_empty_buffer(int(args.max_blocks))
143 print("Failed to construct buffer")
146 print("{} blocks to send".format(len(blocks)))
147 print("---------------------------------------")
148 for i in range(len(blocks)):
150 send_block_command(args.bmc_hostname, args.username, args.password,
151 blocks[i], i, args.dry_run)
152 except Exception as e:
154 print("Error sending block {}".format(i))
159 print("\nReading {} blocks".format(n_blocks))
160 print("---------------------------------------")
161 for i in range(n_blocks):
162 send_block_read_command(args.bmc_hostname, args.username,
163 args.password, i, args.dry_run)
165 if __name__ == "__main__":