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)
108 *(uint32_t *)pos = __cpu_to_be32(len);
109 pos += sizeof(uint32_t);
112 memcpy(pos, str, len);
114 return len + sizeof(uint32_t);
117 /* Read a string from a buffer, allocating the new string as necessary.
119 * @param[in] ctx The talloc context to base the allocation on
120 * @param[in,out] pos Where to start reading
121 * @param[in,out] len The amount of data remaining in the buffer
122 * @param[out] str Pointer to resuling string
123 * @return zero on success, non-zero on failure
125 static int read_string(void *ctx, const char **pos, unsigned int *len,
128 uint32_t str_len, read_len;
130 if (*len < sizeof(uint32_t))
133 str_len = __be32_to_cpu(*(uint32_t *)(*pos));
134 read_len = sizeof(uint32_t);
136 if (read_len + str_len > *len)
142 *str = talloc_strndup(ctx, *pos + read_len, str_len);
146 /* all ok, update the caller's pointers */
153 static int read_u32(const char **pos, unsigned int *len, unsigned int *p)
155 if (*len < sizeof(uint32_t))
158 *p = (unsigned int)__be32_to_cpu(*(uint32_t *)(*pos));
159 *pos += sizeof(uint32_t);
160 *len -= sizeof(uint32_t);
165 char *pb_protocol_deserialise_string(void *ctx,
166 const struct pb_protocol_message *message)
172 len = message->payload_len;
173 buf = message->payload;
175 if (read_string(ctx, &buf, &len, &str))
181 static int optional_strlen(const char *str)
188 int pb_protocol_device_len(const struct device *dev)
190 return 4 + optional_strlen(dev->id) +
192 4 + optional_strlen(dev->name) +
193 4 + optional_strlen(dev->description) +
194 4 + optional_strlen(dev->icon_file);
197 int pb_protocol_boot_option_len(const struct boot_option *opt)
200 return 4 + optional_strlen(opt->device_id) +
201 4 + optional_strlen(opt->id) +
202 4 + optional_strlen(opt->name) +
203 4 + optional_strlen(opt->description) +
204 4 + optional_strlen(opt->icon_file) +
205 4 + optional_strlen(opt->boot_image_file) +
206 4 + optional_strlen(opt->initrd_file) +
207 4 + optional_strlen(opt->dtb_file) +
208 4 + optional_strlen(opt->boot_args) +
209 4 + optional_strlen(opt->args_sig_file) +
210 sizeof(opt->is_default) +
211 sizeof(opt->is_autoboot_default) +
215 int pb_protocol_boot_len(const struct boot_command *boot)
217 return 4 + optional_strlen(boot->option_id) +
218 4 + optional_strlen(boot->boot_image_file) +
219 4 + optional_strlen(boot->initrd_file) +
220 4 + optional_strlen(boot->dtb_file) +
221 4 + optional_strlen(boot->boot_args) +
222 4 + optional_strlen(boot->args_sig_file) +
223 4 + optional_strlen(boot->console);
226 int pb_protocol_boot_status_len(const struct status *status)
228 return 4 + /* type */
229 4 + optional_strlen(status->message) +
234 int pb_protocol_system_info_len(const struct system_info *sysinfo)
238 len = 4 + optional_strlen(sysinfo->type) +
239 4 + optional_strlen(sysinfo->identifier) +
243 for (i = 0; i < sysinfo->n_primary; i++)
244 len += 4 + optional_strlen(sysinfo->platform_primary[i]);
246 for (i = 0; i < sysinfo->n_other; i++)
247 len += 4 + optional_strlen(sysinfo->platform_other[i]);
250 for (i = 0; i < sysinfo->n_bmc_current; i++)
251 len += 4 + optional_strlen(sysinfo->bmc_current[i]);
253 for (i = 0; i < sysinfo->n_bmc_golden; i++)
254 len += 4 + optional_strlen(sysinfo->bmc_golden[i]);
256 for (i = 0; i < sysinfo->n_interfaces; i++) {
257 struct interface_info *if_info = sysinfo->interfaces[i];
258 len += 4 + if_info->hwaddr_size +
259 4 + optional_strlen(if_info->name) +
260 sizeof(if_info->link) +
261 4 + optional_strlen(if_info->address) +
262 4 + optional_strlen(if_info->address_v6);
265 for (i = 0; i < sysinfo->n_blockdevs; i++) {
266 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
267 len += 4 + optional_strlen(bd_info->name) +
268 4 + optional_strlen(bd_info->uuid) +
269 4 + optional_strlen(bd_info->mountpoint);
278 static int pb_protocol_interface_config_len(struct interface_config *conf)
282 len = sizeof(conf->hwaddr) +
283 4 /* conf->ignore */;
288 len += 4 /* conf->method */;
290 if (conf->method == CONFIG_METHOD_STATIC) {
291 len += 4 + optional_strlen(conf->static_config.address);
292 len += 4 + optional_strlen(conf->static_config.gateway);
293 len += 4 + optional_strlen(conf->static_config.url);
296 len += 4 /* conf->override */;
301 int pb_protocol_config_len(const struct config *config)
305 len = 4 /* config->autoboot_enabled */ +
306 4 /* config->autoboot_timeout_sec */ +
307 4 /* config->safe_mode */;
310 for (i = 0; i < config->network.n_interfaces; i++)
311 len += pb_protocol_interface_config_len(
312 config->network.interfaces[i]);
315 for (i = 0; i < config->network.n_dns_servers; i++)
316 len += 4 + optional_strlen(config->network.dns_servers[i]);
318 len += 4 + optional_strlen(config->http_proxy);
319 len += 4 + optional_strlen(config->https_proxy);
322 for (i = 0; i < config->n_autoboot_opts; i++) {
323 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
327 optional_strlen(config->autoboot_opts[i].uuid);
330 len += 4 + 4; /* ipmi_bootdev, ipmi_bootdev_persistent */
331 len += 4; /* ipmi_bootdev_mailbox */
333 len += 4; /* allow_writes */
335 len += 4; /* n_consoles */
336 for (i = 0; i < config->n_consoles; i++)
337 len += 4 + optional_strlen(config->consoles[i]);
339 len += 4 + optional_strlen(config->boot_console);
340 len += 4; /* manual_console */
342 len += 4 + optional_strlen(config->lang);
347 int pb_protocol_url_len(const char *url)
349 /* url + length field */
350 return 4 + optional_strlen(url);
354 int pb_protocol_plugin_option_len(const struct plugin_option *opt)
356 unsigned int i, len = 0;
358 len += 4 + optional_strlen(opt->id);
359 len += 4 + optional_strlen(opt->name);
360 len += 4 + optional_strlen(opt->vendor);
361 len += 4 + optional_strlen(opt->vendor_id);
362 len += 4 + optional_strlen(opt->version);
363 len += 4 + optional_strlen(opt->date);
364 len += 4 + optional_strlen(opt->plugin_file);
366 len += 4; /* n_executables */
367 for (i = 0; i < opt->n_executables; i++)
368 len += 4 + optional_strlen(opt->executables[i]);
373 int pb_protocol_temp_autoboot_len(const struct autoboot_option *opt)
375 unsigned int len = 0;
380 if (opt->boot_type == BOOT_DEVICE_TYPE)
383 len += optional_strlen(opt->uuid);
388 int pb_protocol_authenticate_len(struct auth_message *msg)
391 case AUTH_MSG_REQUEST:
392 /* enum + password + length */
393 return 4 + 4 + optional_strlen(msg->password);
394 case AUTH_MSG_RESPONSE:
398 /* enum + password + password */
399 return 4 + 4 + optional_strlen(msg->set_password.password) +
400 4 + optional_strlen(msg->set_password.new_password);
401 case AUTH_MSG_DECRYPT:
402 /* enum + password + device id */
403 return 4 + 4 + optional_strlen(msg->decrypt_dev.password) +
404 4 + optional_strlen(msg->decrypt_dev.device_id);
406 pb_log("%s: invalid input\n", __func__);
411 int pb_protocol_serialise_device(const struct device *dev,
412 char *buf, int buf_len)
416 pos += pb_protocol_serialise_string(pos, dev->id);
417 *(enum device_type *)pos = dev->type;
418 pos += sizeof(enum device_type);
419 pos += pb_protocol_serialise_string(pos, dev->name);
420 pos += pb_protocol_serialise_string(pos, dev->description);
421 pos += pb_protocol_serialise_string(pos, dev->icon_file);
423 assert(pos <= buf + buf_len);
425 return (pos <= buf + buf_len) ? 0 : -1;
428 int pb_protocol_serialise_boot_option(const struct boot_option *opt,
429 char *buf, int buf_len)
433 pos += pb_protocol_serialise_string(pos, opt->device_id);
434 pos += pb_protocol_serialise_string(pos, opt->id);
435 pos += pb_protocol_serialise_string(pos, opt->name);
436 pos += pb_protocol_serialise_string(pos, opt->description);
437 pos += pb_protocol_serialise_string(pos, opt->icon_file);
438 pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
439 pos += pb_protocol_serialise_string(pos, opt->initrd_file);
440 pos += pb_protocol_serialise_string(pos, opt->dtb_file);
441 pos += pb_protocol_serialise_string(pos, opt->boot_args);
442 pos += pb_protocol_serialise_string(pos, opt->args_sig_file);
444 *(bool *)pos = opt->is_default;
446 *(bool *)pos = opt->is_autoboot_default;
449 *(uint32_t *)pos = __cpu_to_be32(opt->type);
452 assert(pos <= buf + buf_len);
454 return (pos <= buf + buf_len) ? 0 : -1;
457 int pb_protocol_serialise_boot_command(const struct boot_command *boot,
458 char *buf, int buf_len)
462 pos += pb_protocol_serialise_string(pos, boot->option_id);
463 pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
464 pos += pb_protocol_serialise_string(pos, boot->initrd_file);
465 pos += pb_protocol_serialise_string(pos, boot->dtb_file);
466 pos += pb_protocol_serialise_string(pos, boot->boot_args);
467 pos += pb_protocol_serialise_string(pos, boot->args_sig_file);
468 pos += pb_protocol_serialise_string(pos, boot->console);
470 assert(pos <= buf + buf_len);
472 return (pos <= buf + buf_len) ? 0 : -1;
475 int pb_protocol_serialise_boot_status(const struct status *status,
476 char *buf, int buf_len)
480 *(uint32_t *)pos = __cpu_to_be32(status->type);
481 pos += sizeof(uint32_t);
483 pos += pb_protocol_serialise_string(pos, status->message);
485 *(bool *)pos = __cpu_to_be32(status->backlog);
488 *(bool *)pos = __cpu_to_be32(status->boot_active);
491 assert(pos <= buf + buf_len);
493 return (pos <= buf + buf_len) ? 0 : -1;
496 int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
497 char *buf, int buf_len)
502 pos += pb_protocol_serialise_string(pos, sysinfo->type);
503 pos += pb_protocol_serialise_string(pos, sysinfo->identifier);
505 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_primary);
506 pos += sizeof(uint32_t);
507 for (i = 0; i < sysinfo->n_primary; i++)
508 pos += pb_protocol_serialise_string(pos, sysinfo->platform_primary[i]);
510 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_other);
511 pos += sizeof(uint32_t);
512 for (i = 0; i < sysinfo->n_other; i++)
513 pos += pb_protocol_serialise_string(pos, sysinfo->platform_other[i]);
515 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_bmc_current);
516 pos += sizeof(uint32_t);
517 for (i = 0; i < sysinfo->n_bmc_current; i++)
518 pos += pb_protocol_serialise_string(pos, sysinfo->bmc_current[i]);
520 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_bmc_golden);
521 pos += sizeof(uint32_t);
522 for (i = 0; i < sysinfo->n_bmc_golden; i++)
523 pos += pb_protocol_serialise_string(pos, sysinfo->bmc_golden[i]);
525 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_interfaces);
526 pos += sizeof(uint32_t);
528 for (i = 0; i < sysinfo->n_interfaces; i++) {
529 struct interface_info *if_info = sysinfo->interfaces[i];
531 *(uint32_t *)pos = __cpu_to_be32(if_info->hwaddr_size);
532 pos += sizeof(uint32_t);
534 memcpy(pos, if_info->hwaddr, if_info->hwaddr_size);
535 pos += if_info->hwaddr_size;
537 pos += pb_protocol_serialise_string(pos, if_info->name);
539 *(bool *)pos = if_info->link;
542 pos += pb_protocol_serialise_string(pos, if_info->address);
543 pos += pb_protocol_serialise_string(pos, if_info->address_v6);
546 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_blockdevs);
547 pos += sizeof(uint32_t);
549 for (i = 0; i < sysinfo->n_blockdevs; i++) {
550 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
552 pos += pb_protocol_serialise_string(pos, bd_info->name);
553 pos += pb_protocol_serialise_string(pos, bd_info->uuid);
554 pos += pb_protocol_serialise_string(pos, bd_info->mountpoint);
557 if (sysinfo->bmc_mac)
558 memcpy(pos, sysinfo->bmc_mac, HWADDR_SIZE);
560 memset(pos, 0, HWADDR_SIZE);
563 assert(pos <= buf + buf_len);
565 return (pos <= buf + buf_len) ? 0 : -1;
568 static int pb_protocol_serialise_config_interface(char *buf,
569 struct interface_config *conf)
573 memcpy(pos, conf->hwaddr, sizeof(conf->hwaddr));
574 pos += sizeof(conf->hwaddr);
576 *(uint32_t *)pos = conf->ignore;
582 *(uint32_t *)pos = __cpu_to_be32(conf->method);
585 if (conf->method == CONFIG_METHOD_STATIC) {
586 pos += pb_protocol_serialise_string(pos,
587 conf->static_config.address);
588 pos += pb_protocol_serialise_string(pos,
589 conf->static_config.gateway);
590 pos += pb_protocol_serialise_string(pos,
591 conf->static_config.url);
594 *(uint32_t *)pos = conf->override;
600 int pb_protocol_serialise_config(const struct config *config,
601 char *buf, int buf_len)
606 *(uint32_t *)pos = config->autoboot_enabled;
609 *(uint32_t *)pos = __cpu_to_be32(config->autoboot_timeout_sec);
612 *(uint32_t *)pos = config->safe_mode;
615 *(uint32_t *)pos = __cpu_to_be32(config->network.n_interfaces);
617 for (i = 0; i < config->network.n_interfaces; i++) {
618 struct interface_config *iface =
619 config->network.interfaces[i];
620 pos += pb_protocol_serialise_config_interface(pos, iface);
623 *(uint32_t *)pos = __cpu_to_be32(config->network.n_dns_servers);
625 for (i = 0; i < config->network.n_dns_servers; i++) {
626 pos += pb_protocol_serialise_string(pos,
627 config->network.dns_servers[i]);
630 pos += pb_protocol_serialise_string(pos, config->http_proxy);
631 pos += pb_protocol_serialise_string(pos, config->https_proxy);
633 *(uint32_t *)pos = __cpu_to_be32(config->n_autoboot_opts);
635 for (i = 0; i < config->n_autoboot_opts; i++) {
637 __cpu_to_be32(config->autoboot_opts[i].boot_type);
639 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
641 __cpu_to_be32(config->autoboot_opts[i].type);
644 pos += pb_protocol_serialise_string(pos,
645 config->autoboot_opts[i].uuid);
649 *(uint32_t *)pos = __cpu_to_be32(config->ipmi_bootdev);
651 *(uint32_t *)pos = config->ipmi_bootdev_persistent;
653 *(uint32_t *)pos = config->ipmi_bootdev_mailbox;
656 *(uint32_t *)pos = config->allow_writes;
659 *(uint32_t *)pos = __cpu_to_be32(config->n_consoles);
661 for (i = 0; i < config->n_consoles; i++)
662 pos += pb_protocol_serialise_string(pos, config->consoles[i]);
664 pos += pb_protocol_serialise_string(pos, config->boot_console);
665 *(uint32_t *)pos = config->manual_console;
668 pos += pb_protocol_serialise_string(pos, config->lang);
670 assert(pos <= buf + buf_len);
672 return (pos <= buf + buf_len) ? 0 : -1;
675 int pb_protocol_serialise_url(const char *url, char *buf, int buf_len)
679 pos += pb_protocol_serialise_string(pos, url);
681 assert(pos <=buf+buf_len);
683 return (pos <= buf + buf_len) ? 0 : -1;
686 int pb_protocol_serialise_plugin_option(const struct plugin_option *opt,
687 char *buf, int buf_len)
692 pos += pb_protocol_serialise_string(pos, opt->id);
693 pos += pb_protocol_serialise_string(pos, opt->name);
694 pos += pb_protocol_serialise_string(pos, opt->vendor);
695 pos += pb_protocol_serialise_string(pos, opt->vendor_id);
696 pos += pb_protocol_serialise_string(pos, opt->version);
697 pos += pb_protocol_serialise_string(pos, opt->date);
698 pos += pb_protocol_serialise_string(pos, opt->plugin_file);
700 *(uint32_t *)pos = __cpu_to_be32(opt->n_executables);
703 for (i = 0; i < opt->n_executables; i++)
704 pos += pb_protocol_serialise_string(pos, opt->executables[i]);
706 assert(pos <= buf + buf_len);
708 return (pos <= buf + buf_len) ? 0 : -1;
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);
726 assert(pos <= buf + buf_len);
728 return (pos <= buf + buf_len) ? 0 : -1;
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);
753 case AUTH_MSG_DECRYPT:
754 pos += pb_protocol_serialise_string(pos,
755 msg->decrypt_dev.password);
756 pos += pb_protocol_serialise_string(pos,
757 msg->decrypt_dev.device_id);
760 pb_log("%s: invalid msg\n", __func__);
764 assert(pos <= buf + buf_len);
766 return (pos <= buf + buf_len) ? 0 : -1;
769 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
774 total_len = sizeof(*message) + message->payload_len;
776 message->payload_len = __cpu_to_be32(message->payload_len);
777 message->action = __cpu_to_be32(message->action);
779 for (pos = (void *)message; total_len;) {
780 rc = write(fd, pos, total_len);
789 talloc_free(message);
794 pb_log_fn("failed: %s\n", strerror(errno));
798 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
799 enum pb_protocol_action action, int payload_len)
801 struct pb_protocol_message *message;
803 if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
804 pb_log_fn("payload too big %u/%u\n", payload_len,
805 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
809 message = talloc_size(ctx, sizeof(*message) + payload_len);
811 /* we convert these to big-endian in write_message() */
812 message->action = action;
813 message->payload_len = payload_len;
819 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
821 struct pb_protocol_message *message, m;
825 /* use the stack for the initial 8-byte read */
827 rc = read(fd, &m, sizeof(m));
831 m.payload_len = __be32_to_cpu(m.payload_len);
832 m.action = __be32_to_cpu(m.action);
834 if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
835 pb_log_fn("payload too big %u/%u\n", m.payload_len,
836 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
840 message = talloc_size(ctx, sizeof(m) + m.payload_len);
841 memcpy(message, &m, sizeof(m));
843 for (len = 0; len < m.payload_len;) {
844 rc = read(fd, message->payload + len, m.payload_len - len);
847 talloc_free(message);
848 pb_log_fn("failed (%u): %s\n", len,
860 int pb_protocol_deserialise_device(struct device *dev,
861 const struct pb_protocol_message *message)
867 len = message->payload_len;
868 pos = message->payload;
870 if (read_string(dev, &pos, &len, &dev->id))
873 if (len < sizeof(enum device_type))
875 dev->type = *(enum device_type *)(pos);
876 pos += sizeof(enum device_type);
877 len -= sizeof(enum device_type);
879 if (read_string(dev, &pos, &len, &dev->name))
882 if (read_string(dev, &pos, &len, &dev->description))
885 if (read_string(dev, &pos, &len, &dev->icon_file))
894 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
895 const struct pb_protocol_message *message)
901 len = message->payload_len;
902 pos = message->payload;
904 if (read_string(opt, &pos, &len, &opt->device_id))
907 if (read_string(opt, &pos, &len, &opt->id))
910 if (read_string(opt, &pos, &len, &opt->name))
913 if (read_string(opt, &pos, &len, &opt->description))
916 if (read_string(opt, &pos, &len, &opt->icon_file))
919 if (read_string(opt, &pos, &len, &opt->boot_image_file))
922 if (read_string(opt, &pos, &len, &opt->initrd_file))
925 if (read_string(opt, &pos, &len, &opt->dtb_file))
928 if (read_string(opt, &pos, &len, &opt->boot_args))
931 if (read_string(opt, &pos, &len, &opt->args_sig_file))
934 if (len < sizeof(bool))
936 opt->is_default = *(bool *)(pos);
939 opt->is_autoboot_default = *(bool *)(pos);
943 if (read_u32(&pos, &len, &opt->type))
946 rc = (pos <= message->payload + message->payload_len) ? 0 : -1;
952 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
953 const struct pb_protocol_message *message)
959 len = message->payload_len;
960 pos = message->payload;
962 if (read_string(cmd, &pos, &len, &cmd->option_id))
965 if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
968 if (read_string(cmd, &pos, &len, &cmd->initrd_file))
971 if (read_string(cmd, &pos, &len, &cmd->dtb_file))
974 if (read_string(cmd, &pos, &len, &cmd->boot_args))
977 if (read_string(cmd, &pos, &len, &cmd->args_sig_file))
980 if (read_string(cmd, &pos, &len, &cmd->console))
989 int pb_protocol_deserialise_boot_status(struct status *status,
990 const struct pb_protocol_message *message)
996 len = message->payload_len;
997 pos = message->payload;
999 /* first up, the type enum... */
1000 if (len < sizeof(uint32_t))
1003 status->type = __be32_to_cpu(*(uint32_t *)(pos));
1005 switch (status->type) {
1013 pos += sizeof(uint32_t);
1014 len -= sizeof(uint32_t);
1016 /* message string */
1017 if (read_string(status, &pos, &len, &status->message))
1021 status->backlog = *(bool *)pos;
1022 pos += sizeof(status->backlog);
1025 status->boot_active = *(bool *)pos;
1026 pos += sizeof(status->boot_active);
1034 int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
1035 const struct pb_protocol_message *message)
1037 unsigned int len, i;
1042 len = message->payload_len;
1043 pos = message->payload;
1045 /* type and identifier strings */
1046 if (read_string(sysinfo, &pos, &len, &sysinfo->type))
1049 if (read_string(sysinfo, &pos, &len, &sysinfo->identifier))
1052 /* Platform version strings for openpower platforms */
1053 if (read_u32(&pos, &len, &sysinfo->n_primary))
1055 sysinfo->platform_primary = talloc_array(sysinfo, char *,
1056 sysinfo->n_primary);
1057 for (i = 0; i < sysinfo->n_primary; i++) {
1058 if (read_string(sysinfo, &pos, &len, &tmp))
1060 sysinfo->platform_primary[i] = talloc_strdup(sysinfo, tmp);
1063 if (read_u32(&pos, &len, &sysinfo->n_other))
1065 sysinfo->platform_other = talloc_array(sysinfo, char *,
1067 for (i = 0; i < sysinfo->n_other; i++) {
1068 if (read_string(sysinfo, &pos, &len, &tmp))
1070 sysinfo->platform_other[i] = talloc_strdup(sysinfo, tmp);
1073 /* BMC version strings for openpower platforms */
1074 if (read_u32(&pos, &len, &sysinfo->n_bmc_current))
1076 sysinfo->bmc_current = talloc_array(sysinfo, char *,
1077 sysinfo->n_bmc_current);
1078 for (i = 0; i < sysinfo->n_bmc_current; i++) {
1079 if (read_string(sysinfo, &pos, &len, &tmp))
1081 sysinfo->bmc_current[i] = talloc_strdup(sysinfo, tmp);
1084 if (read_u32(&pos, &len, &sysinfo->n_bmc_golden))
1086 sysinfo->bmc_golden = talloc_array(sysinfo, char *,
1087 sysinfo->n_bmc_golden);
1088 for (i = 0; i < sysinfo->n_bmc_golden; i++) {
1089 if (read_string(sysinfo, &pos, &len, &tmp))
1091 sysinfo->bmc_golden[i] = talloc_strdup(sysinfo, tmp);
1094 /* number of interfaces */
1095 if (read_u32(&pos, &len, &sysinfo->n_interfaces))
1098 sysinfo->interfaces = talloc_array(sysinfo, struct interface_info *,
1099 sysinfo->n_interfaces);
1101 for (i = 0; i < sysinfo->n_interfaces; i++) {
1102 struct interface_info *if_info = talloc(sysinfo,
1103 struct interface_info);
1105 if (read_u32(&pos, &len, &if_info->hwaddr_size))
1108 if (len < if_info->hwaddr_size)
1111 if_info->hwaddr = talloc_memdup(if_info, pos,
1112 if_info->hwaddr_size);
1113 pos += if_info->hwaddr_size;
1114 len -= if_info->hwaddr_size;
1116 if (read_string(if_info, &pos, &len, &if_info->name))
1119 if_info->link = *(bool *)pos;
1120 pos += sizeof(if_info->link);
1122 if (read_string(if_info, &pos, &len, &if_info->address))
1124 if (read_string(if_info, &pos, &len, &if_info->address_v6))
1127 sysinfo->interfaces[i] = if_info;
1130 /* number of interfaces */
1131 if (read_u32(&pos, &len, &sysinfo->n_blockdevs))
1134 sysinfo->blockdevs = talloc_array(sysinfo, struct blockdev_info *,
1135 sysinfo->n_blockdevs);
1137 for (i = 0; i < sysinfo->n_blockdevs; i++) {
1138 struct blockdev_info *bd_info = talloc(sysinfo,
1139 struct blockdev_info);
1141 if (read_string(bd_info, &pos, &len, &bd_info->name))
1144 if (read_string(bd_info, &pos, &len, &bd_info->uuid))
1147 if (read_string(bd_info, &pos, &len, &bd_info->mountpoint))
1150 sysinfo->blockdevs[i] = bd_info;
1153 for (i = 0; i < HWADDR_SIZE; i++) {
1155 sysinfo->bmc_mac = talloc_memdup(sysinfo, pos, HWADDR_SIZE);
1168 static int pb_protocol_deserialise_config_interface(const char **buf,
1169 unsigned int *len, struct interface_config *iface)
1173 if (*len < sizeof(iface->hwaddr))
1176 memcpy(iface->hwaddr, *buf, sizeof(iface->hwaddr));
1177 *buf += sizeof(iface->hwaddr);
1178 *len -= sizeof(iface->hwaddr);
1180 if (read_u32(buf, len, &tmp))
1182 iface->ignore = !!tmp;
1187 if (read_u32(buf, len, &iface->method))
1190 if (iface->method == CONFIG_METHOD_STATIC) {
1191 if (read_string(iface, buf, len, &iface->static_config.address))
1194 if (read_string(iface, buf, len, &iface->static_config.gateway))
1197 if (read_string(iface, buf, len, &iface->static_config.url))
1201 if (read_u32(buf, len, &tmp))
1203 iface->override = !!tmp;
1208 int pb_protocol_deserialise_config(struct config *config,
1209 const struct pb_protocol_message *message)
1211 unsigned int len, i, tmp;
1216 len = message->payload_len;
1217 pos = message->payload;
1219 if (read_u32(&pos, &len, &tmp))
1221 config->autoboot_enabled = !!tmp;
1223 if (read_u32(&pos, &len, &config->autoboot_timeout_sec))
1226 if (read_u32(&pos, &len, &tmp))
1228 config->safe_mode = !!tmp;
1230 if (read_u32(&pos, &len, &config->network.n_interfaces))
1233 config->network.interfaces = talloc_array(config,
1234 struct interface_config *, config->network.n_interfaces);
1236 for (i = 0; i < config->network.n_interfaces; i++) {
1237 struct interface_config *iface = talloc_zero(
1238 config->network.interfaces,
1239 struct interface_config);
1240 if (pb_protocol_deserialise_config_interface(&pos, &len, iface))
1242 config->network.interfaces[i] = iface;
1245 if (read_u32(&pos, &len, &config->network.n_dns_servers))
1247 config->network.dns_servers = talloc_array(config, const char *,
1248 config->network.n_dns_servers);
1250 for (i = 0; i < config->network.n_dns_servers; i++) {
1251 if (read_string(config->network.dns_servers, &pos, &len, &str))
1253 config->network.dns_servers[i] = str;
1256 if (read_string(config, &pos, &len, &str))
1258 config->http_proxy = str;
1259 if (read_string(config, &pos, &len, &str))
1261 config->https_proxy = str;
1263 if (read_u32(&pos, &len, &config->n_autoboot_opts))
1265 config->autoboot_opts = talloc_array(config, struct autoboot_option,
1266 config->n_autoboot_opts);
1268 for (i = 0; i < config->n_autoboot_opts; i++) {
1269 if (read_u32(&pos, &len, &tmp))
1271 config->autoboot_opts[i].boot_type = (int)tmp;
1272 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
1273 if (read_u32(&pos, &len, &tmp))
1275 config->autoboot_opts[i].type = tmp;
1277 if (read_string(config, &pos, &len, &str))
1279 config->autoboot_opts[i].uuid = str;
1283 if (read_u32(&pos, &len, &config->ipmi_bootdev))
1285 if (read_u32(&pos, &len, &tmp))
1287 config->ipmi_bootdev_persistent = !!tmp;
1288 if (read_u32(&pos, &len, &tmp))
1290 config->ipmi_bootdev_mailbox = !!tmp;
1292 if (read_u32(&pos, &len, &tmp))
1294 config->allow_writes = !!tmp;
1296 if (read_u32(&pos, &len, &config->n_consoles))
1299 config->consoles = talloc_array(config, char *, config->n_consoles);
1300 for (i = 0; i < config->n_consoles; i++) {
1301 if (read_string(config->consoles, &pos, &len, &str))
1303 config->consoles[i] = str;
1306 if (read_string(config, &pos, &len, &str))
1309 config->boot_console = str;
1311 if (read_u32(&pos, &len, &tmp))
1313 config->manual_console = !!tmp;
1315 if (read_string(config, &pos, &len, &str))
1326 int pb_protocol_deserialise_plugin_option(struct plugin_option *opt,
1327 const struct pb_protocol_message *message)
1329 unsigned int len, i, tmp;
1334 len = message->payload_len;
1335 pos = message->payload;
1337 if (read_string(opt, &pos, &len, &str))
1341 if (read_string(opt, &pos, &len, &str))
1345 if (read_string(opt, &pos, &len, &str))
1349 if (read_string(opt, &pos, &len, &str))
1351 opt->vendor_id = str;
1353 if (read_string(opt, &pos, &len, &str))
1357 if (read_string(opt, &pos, &len, &str))
1361 if (read_string(opt, &pos, &len, &str))
1363 opt->plugin_file = str;
1365 if (read_u32(&pos, &len, &tmp))
1367 opt->n_executables = tmp;
1369 opt->executables = talloc_zero_array(opt, char *, opt->n_executables);
1370 if (!opt->executables)
1373 for (i = 0; i < opt->n_executables; i++) {
1374 if (read_string(opt, &pos, &len, &str))
1376 opt->executables[i] = talloc_strdup(opt, str);
1384 int pb_protocol_deserialise_temp_autoboot(struct autoboot_option *opt,
1385 const struct pb_protocol_message *message)
1387 unsigned int len, tmp;
1392 len = message->payload_len;
1393 pos = message->payload;
1395 if (read_u32(&pos, &len, &tmp))
1398 opt->boot_type = tmp;
1399 if (opt->boot_type == BOOT_DEVICE_TYPE) {
1400 if (read_u32(&pos, &len, &tmp))
1404 } else if (opt->boot_type == BOOT_DEVICE_UUID) {
1405 if (read_string(opt, &pos, &len, &str))
1419 int pb_protocol_deserialise_authenticate(struct auth_message *msg,
1420 const struct pb_protocol_message *message)
1425 len = message->payload_len;
1426 pos = message->payload;
1428 msg->op = *(enum auth_msg_type *)pos;
1429 pos += sizeof(enum auth_msg_type);
1432 case AUTH_MSG_REQUEST:
1433 if (read_string(msg, &pos, &len, &msg->password))
1436 case AUTH_MSG_RESPONSE:
1437 msg->authenticated = *(bool *)pos;
1438 pos += sizeof(bool);
1441 if (read_string(msg, &pos, &len, &msg->set_password.password))
1443 if (read_string(msg, &pos, &len,
1444 &msg->set_password.new_password))
1447 case AUTH_MSG_DECRYPT:
1448 if (read_string(msg, &pos, &len, &msg->decrypt_dev.password))
1450 if (read_string(msg, &pos, &len,
1451 &msg->decrypt_dev.device_id))
1455 pb_log("%s: unable to parse\n", __func__);