7 #include <asm/byteorder.h>
9 #include <talloc/talloc.h>
10 #include <list/list.h>
13 #include "pb-protocol.h"
18 * 4-byte action, determines the remaining message content
19 * 4-byte total payload len
20 * - not including action and payload len header
22 * action = 0x1: device add message
27 * 4-byte len, description
28 * 4-byte len, icon_file
34 * 4-byte len, description
35 * 4-byte len, icon_file
36 * 4-byte len, boot_image_file
37 * 4-byte len, initrd_file
38 * 4-byte len, dtb_file
39 * 4-byte len, boot_args
40 * 4-byte len, args_sig_file
42 * action = 0x2: device remove message
48 * 4-byte len, boot option id
49 * 4-byte len, boot_image_file
50 * 4-byte len, initrd_file
51 * 4-byte len, dtb_file
52 * 4-byte len, boot_args
53 * 4-byte len, args_sig_file
57 void pb_protocol_dump_device(const struct device *dev, const char *text,
60 struct boot_option *opt;
62 fprintf(stream, "%snew dev:\n", text);
63 fprintf(stream, "%s\tid: %s\n", text, dev->id);
64 fprintf(stream, "%s\tname: %s\n", text, dev->name);
65 fprintf(stream, "%s\tdesc: %s\n", text, dev->description);
66 fprintf(stream, "%s\ticon: %s\n", text, dev->icon_file);
67 fprintf(stream, "%s\tboot options:\n", text);
68 list_for_each_entry(&dev->boot_options, opt, list) {
69 fprintf(stream, "%s\t\tid: %s\n", text, opt->id);
70 fprintf(stream, "%s\t\tname: %s\n", text, opt->name);
71 fprintf(stream, "%s\t\tdesc: %s\n", text, opt->description);
72 fprintf(stream, "%s\t\ticon: %s\n", text, opt->icon_file);
73 fprintf(stream, "%s\t\tboot: %s\n", text, opt->boot_image_file);
74 fprintf(stream, "%s\t\tinit: %s\n", text, opt->initrd_file);
75 fprintf(stream, "%s\t\tdtb: %s\n", text, opt->dtb_file);
76 fprintf(stream, "%s\t\targs: %s\n", text, opt->boot_args);
77 fprintf(stream, "%s\t\tasig: %s\n", text, opt->args_sig_file);
78 fprintf(stream, "%s\t\ttype: %s\n", text,
79 opt->type == DISCOVER_BOOT_OPTION ? "boot" : "plugin");
83 int pb_protocol_device_cmp(const struct device *a, const struct device *b)
85 return !strcmp(a->id, b->id);
88 int pb_protocol_boot_option_cmp(const struct boot_option *a,
89 const struct boot_option *b)
91 return !strcmp(a->id, b->id);
94 /* Write a string into the buffer, starting at pos.
96 * Returns the total length used for the write, including length header.
98 int pb_protocol_serialise_string(char *pos, const char *str)
105 *(uint32_t *)pos = __cpu_to_be32(len);
106 pos += sizeof(uint32_t);
108 memcpy(pos, str, len);
110 return len + sizeof(uint32_t);
113 /* Read a string from a buffer, allocating the new string as necessary.
115 * @param[in] ctx The talloc context to base the allocation on
116 * @param[in,out] pos Where to start reading
117 * @param[in,out] len The amount of data remaining in the buffer
118 * @param[out] str Pointer to resuling string
119 * @return zero on success, non-zero on failure
121 static int read_string(void *ctx, const char **pos, unsigned int *len,
124 uint32_t str_len, read_len;
126 if (*len < sizeof(uint32_t))
129 str_len = __be32_to_cpu(*(uint32_t *)(*pos));
130 read_len = sizeof(uint32_t);
132 if (read_len + str_len > *len)
138 *str = talloc_strndup(ctx, *pos + read_len, str_len);
142 /* all ok, update the caller's pointers */
149 static int read_u32(const char **pos, unsigned int *len, unsigned int *p)
151 if (*len < sizeof(uint32_t))
154 *p = (unsigned int)__be32_to_cpu(*(uint32_t *)(*pos));
155 *pos += sizeof(uint32_t);
156 *len -= sizeof(uint32_t);
161 char *pb_protocol_deserialise_string(void *ctx,
162 const struct pb_protocol_message *message)
168 len = message->payload_len;
169 buf = message->payload;
171 if (read_string(ctx, &buf, &len, &str))
177 static int optional_strlen(const char *str)
184 int pb_protocol_device_len(const struct device *dev)
186 return 4 + optional_strlen(dev->id) +
188 4 + optional_strlen(dev->name) +
189 4 + optional_strlen(dev->description) +
190 4 + optional_strlen(dev->icon_file);
193 int pb_protocol_boot_option_len(const struct boot_option *opt)
196 return 4 + optional_strlen(opt->device_id) +
197 4 + optional_strlen(opt->id) +
198 4 + optional_strlen(opt->name) +
199 4 + optional_strlen(opt->description) +
200 4 + optional_strlen(opt->icon_file) +
201 4 + optional_strlen(opt->boot_image_file) +
202 4 + optional_strlen(opt->initrd_file) +
203 4 + optional_strlen(opt->dtb_file) +
204 4 + optional_strlen(opt->boot_args) +
205 4 + optional_strlen(opt->args_sig_file) +
206 sizeof(opt->is_default) +
207 sizeof(opt->is_autoboot_default) +
211 int pb_protocol_boot_len(const struct boot_command *boot)
213 return 4 + optional_strlen(boot->option_id) +
214 4 + optional_strlen(boot->boot_image_file) +
215 4 + optional_strlen(boot->initrd_file) +
216 4 + optional_strlen(boot->dtb_file) +
217 4 + optional_strlen(boot->boot_args) +
218 4 + optional_strlen(boot->args_sig_file) +
219 4 + optional_strlen(boot->console);
222 int pb_protocol_boot_status_len(const struct status *status)
224 return 4 + /* type */
225 4 + optional_strlen(status->message) +
230 int pb_protocol_system_info_len(const struct system_info *sysinfo)
234 len = 4 + optional_strlen(sysinfo->type) +
235 4 + optional_strlen(sysinfo->identifier) +
239 for (i = 0; i < sysinfo->n_primary; i++)
240 len += 4 + optional_strlen(sysinfo->platform_primary[i]);
242 for (i = 0; i < sysinfo->n_other; i++)
243 len += 4 + optional_strlen(sysinfo->platform_other[i]);
246 for (i = 0; i < sysinfo->n_bmc_current; i++)
247 len += 4 + optional_strlen(sysinfo->bmc_current[i]);
249 for (i = 0; i < sysinfo->n_bmc_golden; i++)
250 len += 4 + optional_strlen(sysinfo->bmc_golden[i]);
252 for (i = 0; i < sysinfo->n_interfaces; i++) {
253 struct interface_info *if_info = sysinfo->interfaces[i];
254 len += 4 + if_info->hwaddr_size +
255 4 + optional_strlen(if_info->name) +
256 sizeof(if_info->link) +
257 4 + optional_strlen(if_info->address) +
258 4 + optional_strlen(if_info->address_v6);
261 for (i = 0; i < sysinfo->n_blockdevs; i++) {
262 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
263 len += 4 + optional_strlen(bd_info->name) +
264 4 + optional_strlen(bd_info->uuid) +
265 4 + optional_strlen(bd_info->mountpoint);
274 static int pb_protocol_interface_config_len(struct interface_config *conf)
278 len = sizeof(conf->hwaddr) +
279 4 /* conf->ignore */;
284 len += 4 /* conf->method */;
286 if (conf->method == CONFIG_METHOD_STATIC) {
287 len += 4 + optional_strlen(conf->static_config.address);
288 len += 4 + optional_strlen(conf->static_config.gateway);
289 len += 4 + optional_strlen(conf->static_config.url);
292 len += 4 /* conf->override */;
297 int pb_protocol_config_len(const struct config *config)
301 len = 4 /* config->autoboot_enabled */ +
302 4 /* config->autoboot_timeout_sec */ +
303 4 /* config->safe_mode */;
306 for (i = 0; i < config->network.n_interfaces; i++)
307 len += pb_protocol_interface_config_len(
308 config->network.interfaces[i]);
311 for (i = 0; i < config->network.n_dns_servers; i++)
312 len += 4 + optional_strlen(config->network.dns_servers[i]);
314 len += 4 + optional_strlen(config->http_proxy);
315 len += 4 + optional_strlen(config->https_proxy);
318 for (i = 0; i < config->n_autoboot_opts; i++) {
319 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
323 optional_strlen(config->autoboot_opts[i].uuid);
326 len += 4 + 4; /* ipmi_bootdev, ipmi_bootdev_persistent */
327 len += 4; /* ipmi_bootdev_mailbox */
329 len += 4; /* allow_writes */
331 len += 4; /* n_consoles */
332 for (i = 0; i < config->n_consoles; i++)
333 len += 4 + optional_strlen(config->consoles[i]);
335 len += 4 + optional_strlen(config->boot_console);
336 len += 4; /* manual_console */
338 len += 4 + optional_strlen(config->lang);
343 int pb_protocol_url_len(const char *url)
345 /* url + length field */
346 return 4 + optional_strlen(url);
350 int pb_protocol_plugin_option_len(const struct plugin_option *opt)
352 unsigned int i, len = 0;
354 len += 4 + optional_strlen(opt->id);
355 len += 4 + optional_strlen(opt->name);
356 len += 4 + optional_strlen(opt->vendor);
357 len += 4 + optional_strlen(opt->vendor_id);
358 len += 4 + optional_strlen(opt->version);
359 len += 4 + optional_strlen(opt->date);
360 len += 4 + optional_strlen(opt->plugin_file);
362 len += 4; /* n_executables */
363 for (i = 0; i < opt->n_executables; i++)
364 len += 4 + optional_strlen(opt->executables[i]);
369 int pb_protocol_temp_autoboot_len(const struct autoboot_option *opt)
371 unsigned int len = 0;
376 if (opt->boot_type == BOOT_DEVICE_TYPE)
379 len += optional_strlen(opt->uuid);
384 int pb_protocol_authenticate_len(struct auth_message *msg)
387 case AUTH_MSG_REQUEST:
388 /* enum + password + length */
389 return 4 + 4 + optional_strlen(msg->password);
390 case AUTH_MSG_RESPONSE:
394 /* enum + password + password */
395 return 4 + 4 + optional_strlen(msg->set_password.password) +
396 4 + optional_strlen(msg->set_password.new_password);
398 pb_log("%s: invalid input\n", __func__);
403 int pb_protocol_serialise_device(const struct device *dev,
404 char *buf, int buf_len)
408 pos += pb_protocol_serialise_string(pos, dev->id);
409 *(enum device_type *)pos = dev->type;
410 pos += sizeof(enum device_type);
411 pos += pb_protocol_serialise_string(pos, dev->name);
412 pos += pb_protocol_serialise_string(pos, dev->description);
413 pos += pb_protocol_serialise_string(pos, dev->icon_file);
415 assert(pos <= buf + buf_len);
421 int pb_protocol_serialise_boot_option(const struct boot_option *opt,
422 char *buf, int buf_len)
426 pos += pb_protocol_serialise_string(pos, opt->device_id);
427 pos += pb_protocol_serialise_string(pos, opt->id);
428 pos += pb_protocol_serialise_string(pos, opt->name);
429 pos += pb_protocol_serialise_string(pos, opt->description);
430 pos += pb_protocol_serialise_string(pos, opt->icon_file);
431 pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
432 pos += pb_protocol_serialise_string(pos, opt->initrd_file);
433 pos += pb_protocol_serialise_string(pos, opt->dtb_file);
434 pos += pb_protocol_serialise_string(pos, opt->boot_args);
435 pos += pb_protocol_serialise_string(pos, opt->args_sig_file);
437 *(bool *)pos = opt->is_default;
439 *(bool *)pos = opt->is_autoboot_default;
442 *(uint32_t *)pos = __cpu_to_be32(opt->type);
445 assert(pos <= buf + buf_len);
451 int pb_protocol_serialise_boot_command(const struct boot_command *boot,
452 char *buf, int buf_len)
456 pos += pb_protocol_serialise_string(pos, boot->option_id);
457 pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
458 pos += pb_protocol_serialise_string(pos, boot->initrd_file);
459 pos += pb_protocol_serialise_string(pos, boot->dtb_file);
460 pos += pb_protocol_serialise_string(pos, boot->boot_args);
461 pos += pb_protocol_serialise_string(pos, boot->args_sig_file);
462 pos += pb_protocol_serialise_string(pos, boot->console);
464 assert(pos <= buf + buf_len);
470 int pb_protocol_serialise_boot_status(const struct status *status,
471 char *buf, int buf_len)
475 *(uint32_t *)pos = __cpu_to_be32(status->type);
476 pos += sizeof(uint32_t);
478 pos += pb_protocol_serialise_string(pos, status->message);
480 *(bool *)pos = __cpu_to_be32(status->backlog);
483 *(bool *)pos = __cpu_to_be32(status->boot_active);
486 assert(pos <= buf + buf_len);
492 int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
493 char *buf, int buf_len)
498 pos += pb_protocol_serialise_string(pos, sysinfo->type);
499 pos += pb_protocol_serialise_string(pos, sysinfo->identifier);
501 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_primary);
502 pos += sizeof(uint32_t);
503 for (i = 0; i < sysinfo->n_primary; i++)
504 pos += pb_protocol_serialise_string(pos, sysinfo->platform_primary[i]);
506 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_other);
507 pos += sizeof(uint32_t);
508 for (i = 0; i < sysinfo->n_other; i++)
509 pos += pb_protocol_serialise_string(pos, sysinfo->platform_other[i]);
511 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_bmc_current);
512 pos += sizeof(uint32_t);
513 for (i = 0; i < sysinfo->n_bmc_current; i++)
514 pos += pb_protocol_serialise_string(pos, sysinfo->bmc_current[i]);
516 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_bmc_golden);
517 pos += sizeof(uint32_t);
518 for (i = 0; i < sysinfo->n_bmc_golden; i++)
519 pos += pb_protocol_serialise_string(pos, sysinfo->bmc_golden[i]);
521 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_interfaces);
522 pos += sizeof(uint32_t);
524 for (i = 0; i < sysinfo->n_interfaces; i++) {
525 struct interface_info *if_info = sysinfo->interfaces[i];
527 *(uint32_t *)pos = __cpu_to_be32(if_info->hwaddr_size);
528 pos += sizeof(uint32_t);
530 memcpy(pos, if_info->hwaddr, if_info->hwaddr_size);
531 pos += if_info->hwaddr_size;
533 pos += pb_protocol_serialise_string(pos, if_info->name);
535 *(bool *)pos = if_info->link;
538 pos += pb_protocol_serialise_string(pos, if_info->address);
539 pos += pb_protocol_serialise_string(pos, if_info->address_v6);
542 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_blockdevs);
543 pos += sizeof(uint32_t);
545 for (i = 0; i < sysinfo->n_blockdevs; i++) {
546 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
548 pos += pb_protocol_serialise_string(pos, bd_info->name);
549 pos += pb_protocol_serialise_string(pos, bd_info->uuid);
550 pos += pb_protocol_serialise_string(pos, bd_info->mountpoint);
553 if (sysinfo->bmc_mac)
554 memcpy(pos, sysinfo->bmc_mac, HWADDR_SIZE);
556 memset(pos, 0, HWADDR_SIZE);
559 assert(pos <= buf + buf_len);
565 static int pb_protocol_serialise_config_interface(char *buf,
566 struct interface_config *conf)
570 memcpy(pos, conf->hwaddr, sizeof(conf->hwaddr));
571 pos += sizeof(conf->hwaddr);
573 *(uint32_t *)pos = conf->ignore;
579 *(uint32_t *)pos = __cpu_to_be32(conf->method);
582 if (conf->method == CONFIG_METHOD_STATIC) {
583 pos += pb_protocol_serialise_string(pos,
584 conf->static_config.address);
585 pos += pb_protocol_serialise_string(pos,
586 conf->static_config.gateway);
587 pos += pb_protocol_serialise_string(pos,
588 conf->static_config.url);
591 *(uint32_t *)pos = conf->override;
597 int pb_protocol_serialise_config(const struct config *config,
598 char *buf, int buf_len)
603 *(uint32_t *)pos = config->autoboot_enabled;
606 *(uint32_t *)pos = __cpu_to_be32(config->autoboot_timeout_sec);
609 *(uint32_t *)pos = config->safe_mode;
612 *(uint32_t *)pos = __cpu_to_be32(config->network.n_interfaces);
614 for (i = 0; i < config->network.n_interfaces; i++) {
615 struct interface_config *iface =
616 config->network.interfaces[i];
617 pos += pb_protocol_serialise_config_interface(pos, iface);
620 *(uint32_t *)pos = __cpu_to_be32(config->network.n_dns_servers);
622 for (i = 0; i < config->network.n_dns_servers; i++) {
623 pos += pb_protocol_serialise_string(pos,
624 config->network.dns_servers[i]);
627 pos += pb_protocol_serialise_string(pos, config->http_proxy);
628 pos += pb_protocol_serialise_string(pos, config->https_proxy);
630 *(uint32_t *)pos = __cpu_to_be32(config->n_autoboot_opts);
632 for (i = 0; i < config->n_autoboot_opts; i++) {
634 __cpu_to_be32(config->autoboot_opts[i].boot_type);
636 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
638 __cpu_to_be32(config->autoboot_opts[i].type);
641 pos += pb_protocol_serialise_string(pos,
642 config->autoboot_opts[i].uuid);
646 *(uint32_t *)pos = __cpu_to_be32(config->ipmi_bootdev);
648 *(uint32_t *)pos = config->ipmi_bootdev_persistent;
650 *(uint32_t *)pos = config->ipmi_bootdev_mailbox;
653 *(uint32_t *)pos = config->allow_writes;
656 *(uint32_t *)pos = __cpu_to_be32(config->n_consoles);
658 for (i = 0; i < config->n_consoles; i++)
659 pos += pb_protocol_serialise_string(pos, config->consoles[i]);
661 pos += pb_protocol_serialise_string(pos, config->boot_console);
662 *(uint32_t *)pos = config->manual_console;
665 pos += pb_protocol_serialise_string(pos, config->lang);
667 assert(pos <= buf + buf_len);
673 int pb_protocol_serialise_url(const char *url, char *buf, int buf_len)
677 pos += pb_protocol_serialise_string(pos, url);
679 assert(pos <=buf+buf_len);
685 int pb_protocol_serialise_plugin_option(const struct plugin_option *opt,
686 char *buf, int buf_len)
691 pos += pb_protocol_serialise_string(pos, opt->id);
692 pos += pb_protocol_serialise_string(pos, opt->name);
693 pos += pb_protocol_serialise_string(pos, opt->vendor);
694 pos += pb_protocol_serialise_string(pos, opt->vendor_id);
695 pos += pb_protocol_serialise_string(pos, opt->version);
696 pos += pb_protocol_serialise_string(pos, opt->date);
697 pos += pb_protocol_serialise_string(pos, opt->plugin_file);
699 *(uint32_t *)pos = __cpu_to_be32(opt->n_executables);
702 for (i = 0; i < opt->n_executables; i++)
703 pos += pb_protocol_serialise_string(pos, opt->executables[i]);
705 assert(pos <= buf + buf_len);
711 int pb_protocol_serialise_temp_autoboot(const struct autoboot_option *opt,
712 char *buf, int buf_len)
716 *(uint32_t *)pos = __cpu_to_be32(opt->boot_type);
719 if (opt->boot_type == BOOT_DEVICE_TYPE) {
720 *(uint32_t *)pos = __cpu_to_be32(opt->type);
723 pos += pb_protocol_serialise_string(pos, opt->uuid);
731 int pb_protocol_serialise_authenticate(struct auth_message *msg,
732 char *buf, int buf_len)
736 *(enum auth_msg_type *)pos = msg->op;
737 pos += sizeof(enum auth_msg_type);
740 case AUTH_MSG_REQUEST:
741 pos += pb_protocol_serialise_string(pos, msg->password);
743 case AUTH_MSG_RESPONSE:
744 *(bool *)pos = msg->authenticated;
748 pos += pb_protocol_serialise_string(pos,
749 msg->set_password.password);
750 pos += pb_protocol_serialise_string(pos,
751 msg->set_password.new_password);
754 pb_log("%s: invalid msg\n", __func__);
758 assert(pos <= buf + buf_len);
764 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
769 total_len = sizeof(*message) + message->payload_len;
771 message->payload_len = __cpu_to_be32(message->payload_len);
772 message->action = __cpu_to_be32(message->action);
774 for (pos = (void *)message; total_len;) {
775 rc = write(fd, pos, total_len);
784 talloc_free(message);
789 pb_log_fn("failed: %s\n", strerror(errno));
793 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
794 enum pb_protocol_action action, int payload_len)
796 struct pb_protocol_message *message;
798 if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
799 pb_log_fn("payload too big %u/%u\n", payload_len,
800 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
804 message = talloc_size(ctx, sizeof(*message) + payload_len);
806 /* we convert these to big-endian in write_message() */
807 message->action = action;
808 message->payload_len = payload_len;
814 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
816 struct pb_protocol_message *message, m;
820 /* use the stack for the initial 8-byte read */
822 rc = read(fd, &m, sizeof(m));
826 m.payload_len = __be32_to_cpu(m.payload_len);
827 m.action = __be32_to_cpu(m.action);
829 if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
830 pb_log_fn("payload too big %u/%u\n", m.payload_len,
831 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
835 message = talloc_size(ctx, sizeof(m) + m.payload_len);
836 memcpy(message, &m, sizeof(m));
838 for (len = 0; len < m.payload_len;) {
839 rc = read(fd, message->payload + len, m.payload_len - len);
842 talloc_free(message);
843 pb_log_fn("failed (%u): %s\n", len,
855 int pb_protocol_deserialise_device(struct device *dev,
856 const struct pb_protocol_message *message)
862 len = message->payload_len;
863 pos = message->payload;
865 if (read_string(dev, &pos, &len, &dev->id))
868 if (len < sizeof(enum device_type))
870 dev->type = *(enum device_type *)(pos);
871 pos += sizeof(enum device_type);
872 len -= sizeof(enum device_type);
874 if (read_string(dev, &pos, &len, &dev->name))
877 if (read_string(dev, &pos, &len, &dev->description))
880 if (read_string(dev, &pos, &len, &dev->icon_file))
889 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
890 const struct pb_protocol_message *message)
896 len = message->payload_len;
897 pos = message->payload;
899 if (read_string(opt, &pos, &len, &opt->device_id))
902 if (read_string(opt, &pos, &len, &opt->id))
905 if (read_string(opt, &pos, &len, &opt->name))
908 if (read_string(opt, &pos, &len, &opt->description))
911 if (read_string(opt, &pos, &len, &opt->icon_file))
914 if (read_string(opt, &pos, &len, &opt->boot_image_file))
917 if (read_string(opt, &pos, &len, &opt->initrd_file))
920 if (read_string(opt, &pos, &len, &opt->dtb_file))
923 if (read_string(opt, &pos, &len, &opt->boot_args))
926 if (read_string(opt, &pos, &len, &opt->args_sig_file))
929 if (len < sizeof(bool))
931 opt->is_default = *(bool *)(pos);
934 opt->is_autoboot_default = *(bool *)(pos);
938 if (read_u32(&pos, &len, &opt->type))
947 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
948 const struct pb_protocol_message *message)
954 len = message->payload_len;
955 pos = message->payload;
957 if (read_string(cmd, &pos, &len, &cmd->option_id))
960 if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
963 if (read_string(cmd, &pos, &len, &cmd->initrd_file))
966 if (read_string(cmd, &pos, &len, &cmd->dtb_file))
969 if (read_string(cmd, &pos, &len, &cmd->boot_args))
972 if (read_string(cmd, &pos, &len, &cmd->args_sig_file))
975 if (read_string(cmd, &pos, &len, &cmd->console))
984 int pb_protocol_deserialise_boot_status(struct status *status,
985 const struct pb_protocol_message *message)
991 len = message->payload_len;
992 pos = message->payload;
994 /* first up, the type enum... */
995 if (len < sizeof(uint32_t))
998 status->type = __be32_to_cpu(*(uint32_t *)(pos));
1000 switch (status->type) {
1008 pos += sizeof(uint32_t);
1009 len -= sizeof(uint32_t);
1011 /* message string */
1012 if (read_string(status, &pos, &len, &status->message))
1016 status->backlog = *(bool *)pos;
1017 pos += sizeof(status->backlog);
1020 status->boot_active = *(bool *)pos;
1021 pos += sizeof(status->boot_active);
1029 int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
1030 const struct pb_protocol_message *message)
1032 unsigned int len, i;
1037 len = message->payload_len;
1038 pos = message->payload;
1040 /* type and identifier strings */
1041 if (read_string(sysinfo, &pos, &len, &sysinfo->type))
1044 if (read_string(sysinfo, &pos, &len, &sysinfo->identifier))
1047 /* Platform version strings for openpower platforms */
1048 if (read_u32(&pos, &len, &sysinfo->n_primary))
1050 sysinfo->platform_primary = talloc_array(sysinfo, char *,
1051 sysinfo->n_primary);
1052 for (i = 0; i < sysinfo->n_primary; i++) {
1053 if (read_string(sysinfo, &pos, &len, &tmp))
1055 sysinfo->platform_primary[i] = talloc_strdup(sysinfo, tmp);
1058 if (read_u32(&pos, &len, &sysinfo->n_other))
1060 sysinfo->platform_other = talloc_array(sysinfo, char *,
1062 for (i = 0; i < sysinfo->n_other; i++) {
1063 if (read_string(sysinfo, &pos, &len, &tmp))
1065 sysinfo->platform_other[i] = talloc_strdup(sysinfo, tmp);
1068 /* BMC version strings for openpower platforms */
1069 if (read_u32(&pos, &len, &sysinfo->n_bmc_current))
1071 sysinfo->bmc_current = talloc_array(sysinfo, char *,
1072 sysinfo->n_bmc_current);
1073 for (i = 0; i < sysinfo->n_bmc_current; i++) {
1074 if (read_string(sysinfo, &pos, &len, &tmp))
1076 sysinfo->bmc_current[i] = talloc_strdup(sysinfo, tmp);
1079 if (read_u32(&pos, &len, &sysinfo->n_bmc_golden))
1081 sysinfo->bmc_golden = talloc_array(sysinfo, char *,
1082 sysinfo->n_bmc_golden);
1083 for (i = 0; i < sysinfo->n_bmc_golden; i++) {
1084 if (read_string(sysinfo, &pos, &len, &tmp))
1086 sysinfo->bmc_golden[i] = talloc_strdup(sysinfo, tmp);
1089 /* number of interfaces */
1090 if (read_u32(&pos, &len, &sysinfo->n_interfaces))
1093 sysinfo->interfaces = talloc_array(sysinfo, struct interface_info *,
1094 sysinfo->n_interfaces);
1096 for (i = 0; i < sysinfo->n_interfaces; i++) {
1097 struct interface_info *if_info = talloc(sysinfo,
1098 struct interface_info);
1100 if (read_u32(&pos, &len, &if_info->hwaddr_size))
1103 if (len < if_info->hwaddr_size)
1106 if_info->hwaddr = talloc_memdup(if_info, pos,
1107 if_info->hwaddr_size);
1108 pos += if_info->hwaddr_size;
1109 len -= if_info->hwaddr_size;
1111 if (read_string(if_info, &pos, &len, &if_info->name))
1114 if_info->link = *(bool *)pos;
1115 pos += sizeof(if_info->link);
1117 if (read_string(if_info, &pos, &len, &if_info->address))
1119 if (read_string(if_info, &pos, &len, &if_info->address_v6))
1122 sysinfo->interfaces[i] = if_info;
1125 /* number of interfaces */
1126 if (read_u32(&pos, &len, &sysinfo->n_blockdevs))
1129 sysinfo->blockdevs = talloc_array(sysinfo, struct blockdev_info *,
1130 sysinfo->n_blockdevs);
1132 for (i = 0; i < sysinfo->n_blockdevs; i++) {
1133 struct blockdev_info *bd_info = talloc(sysinfo,
1134 struct blockdev_info);
1136 if (read_string(bd_info, &pos, &len, &bd_info->name))
1139 if (read_string(bd_info, &pos, &len, &bd_info->uuid))
1142 if (read_string(bd_info, &pos, &len, &bd_info->mountpoint))
1145 sysinfo->blockdevs[i] = bd_info;
1148 for (i = 0; i < HWADDR_SIZE; i++) {
1150 sysinfo->bmc_mac = talloc_memdup(sysinfo, pos, HWADDR_SIZE);
1163 static int pb_protocol_deserialise_config_interface(const char **buf,
1164 unsigned int *len, struct interface_config *iface)
1168 if (*len < sizeof(iface->hwaddr))
1171 memcpy(iface->hwaddr, *buf, sizeof(iface->hwaddr));
1172 *buf += sizeof(iface->hwaddr);
1173 *len -= sizeof(iface->hwaddr);
1175 if (read_u32(buf, len, &tmp))
1177 iface->ignore = !!tmp;
1182 if (read_u32(buf, len, &iface->method))
1185 if (iface->method == CONFIG_METHOD_STATIC) {
1186 if (read_string(iface, buf, len, &iface->static_config.address))
1189 if (read_string(iface, buf, len, &iface->static_config.gateway))
1192 if (read_string(iface, buf, len, &iface->static_config.url))
1196 if (read_u32(buf, len, &tmp))
1198 iface->override = !!tmp;
1203 int pb_protocol_deserialise_config(struct config *config,
1204 const struct pb_protocol_message *message)
1206 unsigned int len, i, tmp;
1211 len = message->payload_len;
1212 pos = message->payload;
1214 if (read_u32(&pos, &len, &tmp))
1216 config->autoboot_enabled = !!tmp;
1218 if (read_u32(&pos, &len, &config->autoboot_timeout_sec))
1221 if (read_u32(&pos, &len, &tmp))
1223 config->safe_mode = !!tmp;
1225 if (read_u32(&pos, &len, &config->network.n_interfaces))
1228 config->network.interfaces = talloc_array(config,
1229 struct interface_config *, config->network.n_interfaces);
1231 for (i = 0; i < config->network.n_interfaces; i++) {
1232 struct interface_config *iface = talloc_zero(
1233 config->network.interfaces,
1234 struct interface_config);
1235 if (pb_protocol_deserialise_config_interface(&pos, &len, iface))
1237 config->network.interfaces[i] = iface;
1240 if (read_u32(&pos, &len, &config->network.n_dns_servers))
1242 config->network.dns_servers = talloc_array(config, const char *,
1243 config->network.n_dns_servers);
1245 for (i = 0; i < config->network.n_dns_servers; i++) {
1246 if (read_string(config->network.dns_servers, &pos, &len, &str))
1248 config->network.dns_servers[i] = str;
1251 if (read_string(config, &pos, &len, &str))
1253 config->http_proxy = str;
1254 if (read_string(config, &pos, &len, &str))
1256 config->https_proxy = str;
1258 if (read_u32(&pos, &len, &config->n_autoboot_opts))
1260 config->autoboot_opts = talloc_array(config, struct autoboot_option,
1261 config->n_autoboot_opts);
1263 for (i = 0; i < config->n_autoboot_opts; i++) {
1264 if (read_u32(&pos, &len, &tmp))
1266 config->autoboot_opts[i].boot_type = (int)tmp;
1267 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
1268 if (read_u32(&pos, &len, &tmp))
1270 config->autoboot_opts[i].type = tmp;
1272 if (read_string(config, &pos, &len, &str))
1274 config->autoboot_opts[i].uuid = str;
1278 if (read_u32(&pos, &len, &config->ipmi_bootdev))
1280 if (read_u32(&pos, &len, &tmp))
1282 config->ipmi_bootdev_persistent = !!tmp;
1283 if (read_u32(&pos, &len, &tmp))
1285 config->ipmi_bootdev_mailbox = !!tmp;
1287 if (read_u32(&pos, &len, &tmp))
1289 config->allow_writes = !!tmp;
1291 if (read_u32(&pos, &len, &config->n_consoles))
1294 config->consoles = talloc_array(config, char *, config->n_consoles);
1295 for (i = 0; i < config->n_consoles; i++) {
1296 if (read_string(config->consoles, &pos, &len, &str))
1298 config->consoles[i] = str;
1301 if (read_string(config, &pos, &len, &str))
1304 config->boot_console = str;
1306 if (read_u32(&pos, &len, &tmp))
1308 config->manual_console = !!tmp;
1310 if (read_string(config, &pos, &len, &str))
1321 int pb_protocol_deserialise_plugin_option(struct plugin_option *opt,
1322 const struct pb_protocol_message *message)
1324 unsigned int len, i, tmp;
1329 len = message->payload_len;
1330 pos = message->payload;
1332 if (read_string(opt, &pos, &len, &str))
1336 if (read_string(opt, &pos, &len, &str))
1340 if (read_string(opt, &pos, &len, &str))
1344 if (read_string(opt, &pos, &len, &str))
1346 opt->vendor_id = str;
1348 if (read_string(opt, &pos, &len, &str))
1352 if (read_string(opt, &pos, &len, &str))
1356 if (read_string(opt, &pos, &len, &str))
1358 opt->plugin_file = str;
1360 if (read_u32(&pos, &len, &tmp))
1362 opt->n_executables = tmp;
1364 opt->executables = talloc_zero_array(opt, char *, opt->n_executables);
1365 if (!opt->executables)
1368 for (i = 0; i < opt->n_executables; i++) {
1369 if (read_string(opt, &pos, &len, &str))
1371 opt->executables[i] = talloc_strdup(opt, str);
1379 int pb_protocol_deserialise_temp_autoboot(struct autoboot_option *opt,
1380 const struct pb_protocol_message *message)
1382 unsigned int len, tmp;
1387 len = message->payload_len;
1388 pos = message->payload;
1390 if (read_u32(&pos, &len, &tmp))
1393 opt->boot_type = tmp;
1394 if (opt->boot_type == BOOT_DEVICE_TYPE) {
1395 if (read_u32(&pos, &len, &tmp))
1399 } else if (opt->boot_type == BOOT_DEVICE_UUID) {
1400 if (read_string(opt, &pos, &len, &str))
1414 int pb_protocol_deserialise_authenticate(struct auth_message *msg,
1415 const struct pb_protocol_message *message)
1420 len = message->payload_len;
1421 pos = message->payload;
1423 msg->op = *(enum auth_msg_type *)pos;
1424 pos += sizeof(enum auth_msg_type);
1427 case AUTH_MSG_REQUEST:
1428 if (read_string(msg, &pos, &len, &msg->password))
1431 case AUTH_MSG_RESPONSE:
1432 msg->authenticated = *(bool *)pos;
1433 pos += sizeof(bool);
1436 if (read_string(msg, &pos, &len, &msg->set_password.password))
1438 if (read_string(msg, &pos, &len,
1439 &msg->set_password.new_password))
1443 pb_log("%s: unable to parse\n", __func__);