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);
81 int pb_protocol_device_cmp(const struct device *a, const struct device *b)
83 return !strcmp(a->id, b->id);
86 int pb_protocol_boot_option_cmp(const struct boot_option *a,
87 const struct boot_option *b)
89 return !strcmp(a->id, b->id);
92 /* Write a string into the buffer, starting at pos.
94 * Returns the total length used for the write, including length header.
96 int pb_protocol_serialise_string(char *pos, const char *str)
103 *(uint32_t *)pos = __cpu_to_be32(len);
104 pos += sizeof(uint32_t);
106 memcpy(pos, str, len);
108 return len + sizeof(uint32_t);
111 /* Read a string from a buffer, allocating the new string as necessary.
113 * @param[in] ctx The talloc context to base the allocation on
114 * @param[in,out] pos Where to start reading
115 * @param[in,out] len The amount of data remaining in the buffer
116 * @param[out] str Pointer to resuling string
117 * @return zero on success, non-zero on failure
119 static int read_string(void *ctx, const char **pos, unsigned int *len,
122 uint32_t str_len, read_len;
124 if (*len < sizeof(uint32_t))
127 str_len = __be32_to_cpu(*(uint32_t *)(*pos));
128 read_len = sizeof(uint32_t);
130 if (read_len + str_len > *len)
136 *str = talloc_strndup(ctx, *pos + read_len, str_len);
140 /* all ok, update the caller's pointers */
147 static int read_u32(const char **pos, unsigned int *len, unsigned int *p)
149 if (*len < sizeof(uint32_t))
152 *p = (unsigned int)__be32_to_cpu(*(uint32_t *)(*pos));
153 *pos += sizeof(uint32_t);
154 *len -= sizeof(uint32_t);
159 char *pb_protocol_deserialise_string(void *ctx,
160 const struct pb_protocol_message *message)
166 len = message->payload_len;
167 buf = message->payload;
169 if (read_string(ctx, &buf, &len, &str))
175 static int optional_strlen(const char *str)
182 int pb_protocol_device_len(const struct device *dev)
184 return 4 + optional_strlen(dev->id) +
186 4 + optional_strlen(dev->name) +
187 4 + optional_strlen(dev->description) +
188 4 + optional_strlen(dev->icon_file);
191 int pb_protocol_boot_option_len(const struct boot_option *opt)
194 return 4 + optional_strlen(opt->device_id) +
195 4 + optional_strlen(opt->id) +
196 4 + optional_strlen(opt->name) +
197 4 + optional_strlen(opt->description) +
198 4 + optional_strlen(opt->icon_file) +
199 4 + optional_strlen(opt->boot_image_file) +
200 4 + optional_strlen(opt->initrd_file) +
201 4 + optional_strlen(opt->dtb_file) +
202 4 + optional_strlen(opt->boot_args) +
203 4 + optional_strlen(opt->args_sig_file) +
204 sizeof(opt->is_default);
207 int pb_protocol_boot_len(const struct boot_command *boot)
209 return 4 + optional_strlen(boot->option_id) +
210 4 + optional_strlen(boot->boot_image_file) +
211 4 + optional_strlen(boot->initrd_file) +
212 4 + optional_strlen(boot->dtb_file) +
213 4 + optional_strlen(boot->boot_args) +
214 4 + optional_strlen(boot->args_sig_file) +
215 4 + optional_strlen(boot->console);
218 int pb_protocol_boot_status_len(const struct boot_status *status)
221 4 + optional_strlen(status->message) +
222 4 + optional_strlen(status->detail) +
226 int pb_protocol_system_info_len(const struct system_info *sysinfo)
230 len = 4 + optional_strlen(sysinfo->type) +
231 4 + optional_strlen(sysinfo->identifier) +
235 for (i = 0; i < sysinfo->n_primary; i++)
236 len += 4 + optional_strlen(sysinfo->platform_primary[i]);
238 for (i = 0; i < sysinfo->n_other; i++)
239 len += 4 + optional_strlen(sysinfo->platform_other[i]);
242 for (i = 0; i < sysinfo->n_bmc_current; i++)
243 len += 4 + optional_strlen(sysinfo->bmc_current[i]);
245 for (i = 0; i < sysinfo->n_bmc_golden; i++)
246 len += 4 + optional_strlen(sysinfo->bmc_golden[i]);
248 for (i = 0; i < sysinfo->n_interfaces; i++) {
249 struct interface_info *if_info = sysinfo->interfaces[i];
250 len += 4 + if_info->hwaddr_size +
251 4 + optional_strlen(if_info->name) +
252 sizeof(if_info->link);
255 for (i = 0; i < sysinfo->n_blockdevs; i++) {
256 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
257 len += 4 + optional_strlen(bd_info->name) +
258 4 + optional_strlen(bd_info->uuid) +
259 4 + optional_strlen(bd_info->mountpoint);
268 static int pb_protocol_interface_config_len(struct interface_config *conf)
272 len = sizeof(conf->hwaddr) +
273 4 /* conf->ignore */;
278 len += 4 /* conf->method */;
280 if (conf->method == CONFIG_METHOD_STATIC) {
281 len += 4 + optional_strlen(conf->static_config.address);
282 len += 4 + optional_strlen(conf->static_config.gateway);
283 len += 4 + optional_strlen(conf->static_config.url);
286 len += 4 /* conf->override */;
291 int pb_protocol_config_len(const struct config *config)
295 len = 4 /* config->autoboot_enabled */ +
296 4 /* config->autoboot_timeout_sec */ +
297 4 /* config->safe_mode */;
300 for (i = 0; i < config->network.n_interfaces; i++)
301 len += pb_protocol_interface_config_len(
302 config->network.interfaces[i]);
305 for (i = 0; i < config->network.n_dns_servers; i++)
306 len += 4 + optional_strlen(config->network.dns_servers[i]);
308 len += 4 + optional_strlen(config->http_proxy);
309 len += 4 + optional_strlen(config->https_proxy);
312 for (i = 0; i < config->n_autoboot_opts; i++) {
313 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
317 optional_strlen(config->autoboot_opts[i].uuid);
320 len += 4 + 4; /* ipmi_bootdev, ipmi_bootdev_persistent */
322 len += 4; /* allow_writes */
324 len += 4; /* n_consoles */
325 for (i = 0; i < config->n_consoles; i++)
326 len += 4 + optional_strlen(config->consoles[i]);
328 len += 4 + optional_strlen(config->boot_console);
329 len += 4; /* manual_console */
331 len += 4 + optional_strlen(config->lang);
336 int pb_protocol_url_len(const char *url)
338 /* url + length field */
339 return 4 + optional_strlen(url);
342 int pb_protocol_serialise_device(const struct device *dev,
343 char *buf, int buf_len)
347 pos += pb_protocol_serialise_string(pos, dev->id);
348 *(enum device_type *)pos = dev->type;
349 pos += sizeof(enum device_type);
350 pos += pb_protocol_serialise_string(pos, dev->name);
351 pos += pb_protocol_serialise_string(pos, dev->description);
352 pos += pb_protocol_serialise_string(pos, dev->icon_file);
354 assert(pos <= buf + buf_len);
360 int pb_protocol_serialise_boot_option(const struct boot_option *opt,
361 char *buf, int buf_len)
365 pos += pb_protocol_serialise_string(pos, opt->device_id);
366 pos += pb_protocol_serialise_string(pos, opt->id);
367 pos += pb_protocol_serialise_string(pos, opt->name);
368 pos += pb_protocol_serialise_string(pos, opt->description);
369 pos += pb_protocol_serialise_string(pos, opt->icon_file);
370 pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
371 pos += pb_protocol_serialise_string(pos, opt->initrd_file);
372 pos += pb_protocol_serialise_string(pos, opt->dtb_file);
373 pos += pb_protocol_serialise_string(pos, opt->boot_args);
374 pos += pb_protocol_serialise_string(pos, opt->args_sig_file);
376 *(bool *)pos = opt->is_default;
379 assert(pos <= buf + buf_len);
385 int pb_protocol_serialise_boot_command(const struct boot_command *boot,
386 char *buf, int buf_len)
390 pos += pb_protocol_serialise_string(pos, boot->option_id);
391 pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
392 pos += pb_protocol_serialise_string(pos, boot->initrd_file);
393 pos += pb_protocol_serialise_string(pos, boot->dtb_file);
394 pos += pb_protocol_serialise_string(pos, boot->boot_args);
395 pos += pb_protocol_serialise_string(pos, boot->args_sig_file);
396 pos += pb_protocol_serialise_string(pos, boot->console);
398 assert(pos <= buf + buf_len);
404 int pb_protocol_serialise_boot_status(const struct boot_status *status,
405 char *buf, int buf_len)
409 *(uint32_t *)pos = __cpu_to_be32(status->type);
410 pos += sizeof(uint32_t);
412 pos += pb_protocol_serialise_string(pos, status->message);
413 pos += pb_protocol_serialise_string(pos, status->detail);
415 *(uint32_t *)pos = __cpu_to_be32(status->type);
416 pos += sizeof(uint32_t);
418 assert(pos <= buf + buf_len);
424 int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
425 char *buf, int buf_len)
430 pos += pb_protocol_serialise_string(pos, sysinfo->type);
431 pos += pb_protocol_serialise_string(pos, sysinfo->identifier);
433 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_primary);
434 pos += sizeof(uint32_t);
435 for (i = 0; i < sysinfo->n_primary; i++)
436 pos += pb_protocol_serialise_string(pos, sysinfo->platform_primary[i]);
438 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_other);
439 pos += sizeof(uint32_t);
440 for (i = 0; i < sysinfo->n_other; i++)
441 pos += pb_protocol_serialise_string(pos, sysinfo->platform_other[i]);
443 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_bmc_current);
444 pos += sizeof(uint32_t);
445 for (i = 0; i < sysinfo->n_bmc_current; i++)
446 pos += pb_protocol_serialise_string(pos, sysinfo->bmc_current[i]);
448 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_bmc_golden);
449 pos += sizeof(uint32_t);
450 for (i = 0; i < sysinfo->n_bmc_golden; i++)
451 pos += pb_protocol_serialise_string(pos, sysinfo->bmc_golden[i]);
453 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_interfaces);
454 pos += sizeof(uint32_t);
456 for (i = 0; i < sysinfo->n_interfaces; i++) {
457 struct interface_info *if_info = sysinfo->interfaces[i];
459 *(uint32_t *)pos = __cpu_to_be32(if_info->hwaddr_size);
460 pos += sizeof(uint32_t);
462 memcpy(pos, if_info->hwaddr, if_info->hwaddr_size);
463 pos += if_info->hwaddr_size;
465 pos += pb_protocol_serialise_string(pos, if_info->name);
467 *(bool *)pos = if_info->link;
471 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_blockdevs);
472 pos += sizeof(uint32_t);
474 for (i = 0; i < sysinfo->n_blockdevs; i++) {
475 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
477 pos += pb_protocol_serialise_string(pos, bd_info->name);
478 pos += pb_protocol_serialise_string(pos, bd_info->uuid);
479 pos += pb_protocol_serialise_string(pos, bd_info->mountpoint);
482 if (sysinfo->bmc_mac)
483 memcpy(pos, sysinfo->bmc_mac, HWADDR_SIZE);
485 memset(pos, 0, HWADDR_SIZE);
488 assert(pos <= buf + buf_len);
494 static int pb_protocol_serialise_config_interface(char *buf,
495 struct interface_config *conf)
499 memcpy(pos, conf->hwaddr, sizeof(conf->hwaddr));
500 pos += sizeof(conf->hwaddr);
502 *(uint32_t *)pos = conf->ignore;
508 *(uint32_t *)pos = __cpu_to_be32(conf->method);
511 if (conf->method == CONFIG_METHOD_STATIC) {
512 pos += pb_protocol_serialise_string(pos,
513 conf->static_config.address);
514 pos += pb_protocol_serialise_string(pos,
515 conf->static_config.gateway);
516 pos += pb_protocol_serialise_string(pos,
517 conf->static_config.url);
520 *(uint32_t *)pos = conf->override;
526 int pb_protocol_serialise_config(const struct config *config,
527 char *buf, int buf_len)
532 *(uint32_t *)pos = config->autoboot_enabled;
535 *(uint32_t *)pos = __cpu_to_be32(config->autoboot_timeout_sec);
538 *(uint32_t *)pos = config->safe_mode;
541 *(uint32_t *)pos = __cpu_to_be32(config->network.n_interfaces);
543 for (i = 0; i < config->network.n_interfaces; i++) {
544 struct interface_config *iface =
545 config->network.interfaces[i];
546 pos += pb_protocol_serialise_config_interface(pos, iface);
549 *(uint32_t *)pos = __cpu_to_be32(config->network.n_dns_servers);
551 for (i = 0; i < config->network.n_dns_servers; i++) {
552 pos += pb_protocol_serialise_string(pos,
553 config->network.dns_servers[i]);
556 pos += pb_protocol_serialise_string(pos, config->http_proxy);
557 pos += pb_protocol_serialise_string(pos, config->https_proxy);
559 *(uint32_t *)pos = __cpu_to_be32(config->n_autoboot_opts);
561 for (i = 0; i < config->n_autoboot_opts; i++) {
563 __cpu_to_be32(config->autoboot_opts[i].boot_type);
565 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
567 __cpu_to_be32(config->autoboot_opts[i].type);
570 pos += pb_protocol_serialise_string(pos,
571 config->autoboot_opts[i].uuid);
575 *(uint32_t *)pos = __cpu_to_be32(config->ipmi_bootdev);
577 *(uint32_t *)pos = config->ipmi_bootdev_persistent;
580 *(uint32_t *)pos = config->allow_writes;
583 *(uint32_t *)pos = __cpu_to_be32(config->n_consoles);
585 for (i = 0; i < config->n_consoles; i++)
586 pos += pb_protocol_serialise_string(pos, config->consoles[i]);
588 pos += pb_protocol_serialise_string(pos, config->boot_console);
589 *(uint32_t *)pos = config->manual_console;
592 pos += pb_protocol_serialise_string(pos, config->lang);
594 assert(pos <= buf + buf_len);
600 int pb_protocol_serialise_url(const char *url, char *buf, int buf_len)
604 pos += pb_protocol_serialise_string(pos, url);
606 assert(pos <=buf+buf_len);
612 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
617 total_len = sizeof(*message) + message->payload_len;
619 message->payload_len = __cpu_to_be32(message->payload_len);
620 message->action = __cpu_to_be32(message->action);
622 for (pos = (void *)message; total_len;) {
623 rc = write(fd, pos, total_len);
632 talloc_free(message);
637 pb_log("%s: failed: %s\n", __func__, strerror(errno));
641 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
642 enum pb_protocol_action action, int payload_len)
644 struct pb_protocol_message *message;
646 if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
647 pb_log("%s: payload too big %u/%u\n", __func__, payload_len,
648 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
652 message = talloc_size(ctx, sizeof(*message) + payload_len);
654 /* we convert these to big-endian in write_message() */
655 message->action = action;
656 message->payload_len = payload_len;
662 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
664 struct pb_protocol_message *message, m;
668 /* use the stack for the initial 8-byte read */
670 rc = read(fd, &m, sizeof(m));
674 m.payload_len = __be32_to_cpu(m.payload_len);
675 m.action = __be32_to_cpu(m.action);
677 if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
678 pb_log("%s: payload too big %u/%u\n", __func__, m.payload_len,
679 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
683 message = talloc_size(ctx, sizeof(m) + m.payload_len);
684 memcpy(message, &m, sizeof(m));
686 for (len = 0; len < m.payload_len;) {
687 rc = read(fd, message->payload + len, m.payload_len - len);
690 talloc_free(message);
691 pb_log("%s: failed (%u): %s\n", __func__, len,
703 int pb_protocol_deserialise_device(struct device *dev,
704 const struct pb_protocol_message *message)
710 len = message->payload_len;
711 pos = message->payload;
713 if (read_string(dev, &pos, &len, &dev->id))
716 if (len < sizeof(enum device_type))
718 dev->type = *(enum device_type *)(pos);
719 pos += sizeof(enum device_type);
720 len -= sizeof(enum device_type);
722 if (read_string(dev, &pos, &len, &dev->name))
725 if (read_string(dev, &pos, &len, &dev->description))
728 if (read_string(dev, &pos, &len, &dev->icon_file))
737 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
738 const struct pb_protocol_message *message)
744 len = message->payload_len;
745 pos = message->payload;
747 if (read_string(opt, &pos, &len, &opt->device_id))
750 if (read_string(opt, &pos, &len, &opt->id))
753 if (read_string(opt, &pos, &len, &opt->name))
756 if (read_string(opt, &pos, &len, &opt->description))
759 if (read_string(opt, &pos, &len, &opt->icon_file))
762 if (read_string(opt, &pos, &len, &opt->boot_image_file))
765 if (read_string(opt, &pos, &len, &opt->initrd_file))
768 if (read_string(opt, &pos, &len, &opt->dtb_file))
771 if (read_string(opt, &pos, &len, &opt->boot_args))
774 if (read_string(opt, &pos, &len, &opt->args_sig_file))
777 if (len < sizeof(bool))
779 opt->is_default = *(bool *)(pos);
787 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
788 const struct pb_protocol_message *message)
794 len = message->payload_len;
795 pos = message->payload;
797 if (read_string(cmd, &pos, &len, &cmd->option_id))
800 if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
803 if (read_string(cmd, &pos, &len, &cmd->initrd_file))
806 if (read_string(cmd, &pos, &len, &cmd->dtb_file))
809 if (read_string(cmd, &pos, &len, &cmd->boot_args))
812 if (read_string(cmd, &pos, &len, &cmd->args_sig_file))
815 if (read_string(cmd, &pos, &len, &cmd->console))
824 int pb_protocol_deserialise_boot_status(struct boot_status *status,
825 const struct pb_protocol_message *message)
831 len = message->payload_len;
832 pos = message->payload;
834 /* first up, the type enum... */
835 if (len < sizeof(uint32_t))
838 status->type = __be32_to_cpu(*(uint32_t *)(pos));
840 switch (status->type) {
841 case BOOT_STATUS_ERROR:
842 case BOOT_STATUS_INFO:
848 pos += sizeof(uint32_t);
849 len -= sizeof(uint32_t);
851 /* message and detail strings */
852 if (read_string(status, &pos, &len, &status->message))
855 if (read_string(status, &pos, &len, &status->detail))
858 /* and finally, progress */
859 if (len < sizeof(uint32_t))
862 status->progress = __be32_to_cpu(*(uint32_t *)(pos));
865 if (status->progress > 100)
866 status->progress = 100;
874 int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
875 const struct pb_protocol_message *message)
882 len = message->payload_len;
883 pos = message->payload;
885 /* type and identifier strings */
886 if (read_string(sysinfo, &pos, &len, &sysinfo->type))
889 if (read_string(sysinfo, &pos, &len, &sysinfo->identifier))
892 /* Platform version strings for openpower platforms */
893 if (read_u32(&pos, &len, &sysinfo->n_primary))
895 sysinfo->platform_primary = talloc_array(sysinfo, char *,
897 for (i = 0; i < sysinfo->n_primary; i++) {
898 if (read_string(sysinfo, &pos, &len, &tmp))
900 sysinfo->platform_primary[i] = talloc_strdup(sysinfo, tmp);
903 if (read_u32(&pos, &len, &sysinfo->n_other))
905 sysinfo->platform_other = talloc_array(sysinfo, char *,
907 for (i = 0; i < sysinfo->n_other; i++) {
908 if (read_string(sysinfo, &pos, &len, &tmp))
910 sysinfo->platform_other[i] = talloc_strdup(sysinfo, tmp);
913 /* BMC version strings for openpower platforms */
914 if (read_u32(&pos, &len, &sysinfo->n_bmc_current))
916 sysinfo->bmc_current = talloc_array(sysinfo, char *,
917 sysinfo->n_bmc_current);
918 for (i = 0; i < sysinfo->n_bmc_current; i++) {
919 if (read_string(sysinfo, &pos, &len, &tmp))
921 sysinfo->bmc_current[i] = talloc_strdup(sysinfo, tmp);
924 if (read_u32(&pos, &len, &sysinfo->n_bmc_golden))
926 sysinfo->bmc_golden = talloc_array(sysinfo, char *,
927 sysinfo->n_bmc_golden);
928 for (i = 0; i < sysinfo->n_bmc_golden; i++) {
929 if (read_string(sysinfo, &pos, &len, &tmp))
931 sysinfo->bmc_golden[i] = talloc_strdup(sysinfo, tmp);
934 /* number of interfaces */
935 if (read_u32(&pos, &len, &sysinfo->n_interfaces))
938 sysinfo->interfaces = talloc_array(sysinfo, struct interface_info *,
939 sysinfo->n_interfaces);
941 for (i = 0; i < sysinfo->n_interfaces; i++) {
942 struct interface_info *if_info = talloc(sysinfo,
943 struct interface_info);
945 if (read_u32(&pos, &len, &if_info->hwaddr_size))
948 if (len < if_info->hwaddr_size)
951 if_info->hwaddr = talloc_memdup(if_info, pos,
952 if_info->hwaddr_size);
953 pos += if_info->hwaddr_size;
954 len -= if_info->hwaddr_size;
956 if (read_string(if_info, &pos, &len, &if_info->name))
959 if_info->link = *(bool *)pos;
960 pos += sizeof(if_info->link);
962 sysinfo->interfaces[i] = if_info;
965 /* number of interfaces */
966 if (read_u32(&pos, &len, &sysinfo->n_blockdevs))
969 sysinfo->blockdevs = talloc_array(sysinfo, struct blockdev_info *,
970 sysinfo->n_blockdevs);
972 for (i = 0; i < sysinfo->n_blockdevs; i++) {
973 struct blockdev_info *bd_info = talloc(sysinfo,
974 struct blockdev_info);
976 if (read_string(bd_info, &pos, &len, &bd_info->name))
979 if (read_string(bd_info, &pos, &len, &bd_info->uuid))
982 if (read_string(bd_info, &pos, &len, &bd_info->mountpoint))
985 sysinfo->blockdevs[i] = bd_info;
988 for (i = 0; i < HWADDR_SIZE; i++) {
990 sysinfo->bmc_mac = talloc_memdup(sysinfo, pos, HWADDR_SIZE);
1003 static int pb_protocol_deserialise_config_interface(const char **buf,
1004 unsigned int *len, struct interface_config *iface)
1008 if (*len < sizeof(iface->hwaddr))
1011 memcpy(iface->hwaddr, *buf, sizeof(iface->hwaddr));
1012 *buf += sizeof(iface->hwaddr);
1013 *len -= sizeof(iface->hwaddr);
1015 if (read_u32(buf, len, &tmp))
1017 iface->ignore = !!tmp;
1022 if (read_u32(buf, len, &iface->method))
1025 if (iface->method == CONFIG_METHOD_STATIC) {
1026 if (read_string(iface, buf, len, &iface->static_config.address))
1029 if (read_string(iface, buf, len, &iface->static_config.gateway))
1032 if (read_string(iface, buf, len, &iface->static_config.url))
1036 if (read_u32(buf, len, &tmp))
1038 iface->override = !!tmp;
1043 int pb_protocol_deserialise_config(struct config *config,
1044 const struct pb_protocol_message *message)
1046 unsigned int len, i, tmp;
1051 len = message->payload_len;
1052 pos = message->payload;
1054 if (read_u32(&pos, &len, &tmp))
1056 config->autoboot_enabled = !!tmp;
1058 if (read_u32(&pos, &len, &config->autoboot_timeout_sec))
1061 if (read_u32(&pos, &len, &tmp))
1063 config->safe_mode = !!tmp;
1065 if (read_u32(&pos, &len, &config->network.n_interfaces))
1068 config->network.interfaces = talloc_array(config,
1069 struct interface_config *, config->network.n_interfaces);
1071 for (i = 0; i < config->network.n_interfaces; i++) {
1072 struct interface_config *iface = talloc_zero(
1073 config->network.interfaces,
1074 struct interface_config);
1075 if (pb_protocol_deserialise_config_interface(&pos, &len, iface))
1077 config->network.interfaces[i] = iface;
1080 if (read_u32(&pos, &len, &config->network.n_dns_servers))
1082 config->network.dns_servers = talloc_array(config, const char *,
1083 config->network.n_dns_servers);
1085 for (i = 0; i < config->network.n_dns_servers; i++) {
1086 if (read_string(config->network.dns_servers, &pos, &len, &str))
1088 config->network.dns_servers[i] = str;
1091 if (read_string(config, &pos, &len, &str))
1093 config->http_proxy = str;
1094 if (read_string(config, &pos, &len, &str))
1096 config->https_proxy = str;
1098 if (read_u32(&pos, &len, &config->n_autoboot_opts))
1100 config->autoboot_opts = talloc_array(config, struct autoboot_option,
1101 config->n_autoboot_opts);
1103 for (i = 0; i < config->n_autoboot_opts; i++) {
1104 if (read_u32(&pos, &len, &tmp))
1106 config->autoboot_opts[i].boot_type = (int)tmp;
1107 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
1108 if (read_u32(&pos, &len, &tmp))
1110 config->autoboot_opts[i].type = tmp;
1112 if (read_string(config, &pos, &len, &str))
1114 config->autoboot_opts[i].uuid = str;
1118 if (read_u32(&pos, &len, &config->ipmi_bootdev))
1120 if (read_u32(&pos, &len, &tmp))
1122 config->ipmi_bootdev_persistent = !!tmp;
1124 if (read_u32(&pos, &len, &tmp))
1126 config->allow_writes = !!tmp;
1128 if (read_u32(&pos, &len, &config->n_consoles))
1131 config->consoles = talloc_array(config, char *, config->n_consoles);
1132 for (i = 0; i < config->n_consoles; i++) {
1133 if (read_string(config->consoles, &pos, &len, &str))
1135 config->consoles[i] = str;
1138 if (read_string(config, &pos, &len, &str))
1141 config->boot_console = str;
1143 if (read_u32(&pos, &len, &tmp))
1145 config->manual_console = !!tmp;
1147 if (read_string(config, &pos, &len, &str))