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
41 * action = 0x2: device remove message
47 * 4-byte len, boot option id
48 * 4-byte len, boot_image_file
49 * 4-byte len, initrd_file
50 * 4-byte len, dtb_file
51 * 4-byte len, boot_args
55 void pb_protocol_dump_device(const struct device *dev, const char *text,
58 struct boot_option *opt;
60 fprintf(stream, "%snew dev:\n", text);
61 fprintf(stream, "%s\tid: %s\n", text, dev->id);
62 fprintf(stream, "%s\tname: %s\n", text, dev->name);
63 fprintf(stream, "%s\tdesc: %s\n", text, dev->description);
64 fprintf(stream, "%s\ticon: %s\n", text, dev->icon_file);
65 fprintf(stream, "%s\tboot options:\n", text);
66 list_for_each_entry(&dev->boot_options, opt, list) {
67 fprintf(stream, "%s\t\tid: %s\n", text, opt->id);
68 fprintf(stream, "%s\t\tname: %s\n", text, opt->name);
69 fprintf(stream, "%s\t\tdesc: %s\n", text, opt->description);
70 fprintf(stream, "%s\t\ticon: %s\n", text, opt->icon_file);
71 fprintf(stream, "%s\t\tboot: %s\n", text, opt->boot_image_file);
72 fprintf(stream, "%s\t\tinit: %s\n", text, opt->initrd_file);
73 fprintf(stream, "%s\t\tdtb: %s\n", text, opt->dtb_file);
74 fprintf(stream, "%s\t\targs: %s\n", text, opt->boot_args);
78 int pb_protocol_device_cmp(const struct device *a, const struct device *b)
80 return !strcmp(a->id, b->id);
83 int pb_protocol_boot_option_cmp(const struct boot_option *a,
84 const struct boot_option *b)
86 return !strcmp(a->id, b->id);
89 /* Write a string into the buffer, starting at pos.
91 * Returns the total length used for the write, including length header.
93 int pb_protocol_serialise_string(char *pos, const char *str)
100 *(uint32_t *)pos = __cpu_to_be32(len);
101 pos += sizeof(uint32_t);
103 memcpy(pos, str, len);
105 return len + sizeof(uint32_t);
108 /* Read a string from a buffer, allocating the new string as necessary.
110 * @param[in] ctx The talloc context to base the allocation on
111 * @param[in,out] pos Where to start reading
112 * @param[in,out] len The amount of data remaining in the buffer
113 * @param[out] str Pointer to resuling string
114 * @return zero on success, non-zero on failure
116 static int read_string(void *ctx, const char **pos, unsigned int *len,
119 uint32_t str_len, read_len;
121 if (*len < sizeof(uint32_t))
124 str_len = __be32_to_cpu(*(uint32_t *)(*pos));
125 read_len = sizeof(uint32_t);
127 if (read_len + str_len > *len)
133 *str = talloc_strndup(ctx, *pos + read_len, str_len);
137 /* all ok, update the caller's pointers */
144 static int read_u32(const char **pos, unsigned int *len, unsigned int *p)
146 if (*len < sizeof(uint32_t))
149 *p = (unsigned int)__be32_to_cpu(*(uint32_t *)(*pos));
150 *pos += sizeof(uint32_t);
151 *len -= sizeof(uint32_t);
156 char *pb_protocol_deserialise_string(void *ctx,
157 const struct pb_protocol_message *message)
163 len = message->payload_len;
164 buf = message->payload;
166 if (read_string(ctx, &buf, &len, &str))
172 static int optional_strlen(const char *str)
179 int pb_protocol_device_len(const struct device *dev)
181 return 4 + optional_strlen(dev->id) +
183 4 + optional_strlen(dev->name) +
184 4 + optional_strlen(dev->description) +
185 4 + optional_strlen(dev->icon_file);
188 int pb_protocol_boot_option_len(const struct boot_option *opt)
191 return 4 + optional_strlen(opt->device_id) +
192 4 + optional_strlen(opt->id) +
193 4 + optional_strlen(opt->name) +
194 4 + optional_strlen(opt->description) +
195 4 + optional_strlen(opt->icon_file) +
196 4 + optional_strlen(opt->boot_image_file) +
197 4 + optional_strlen(opt->initrd_file) +
198 4 + optional_strlen(opt->dtb_file) +
199 4 + optional_strlen(opt->boot_args) +
200 sizeof(opt->is_default);
203 int pb_protocol_boot_len(const struct boot_command *boot)
205 return 4 + optional_strlen(boot->option_id) +
206 4 + optional_strlen(boot->boot_image_file) +
207 4 + optional_strlen(boot->initrd_file) +
208 4 + optional_strlen(boot->dtb_file) +
209 4 + optional_strlen(boot->boot_args);
212 int pb_protocol_boot_status_len(const struct boot_status *status)
215 4 + optional_strlen(status->message) +
216 4 + optional_strlen(status->detail) +
220 int pb_protocol_system_info_len(const struct system_info *sysinfo)
224 len = 4 + optional_strlen(sysinfo->type) +
225 4 + optional_strlen(sysinfo->identifier) +
228 for (i = 0; i < sysinfo->n_interfaces; i++) {
229 struct interface_info *if_info = sysinfo->interfaces[i];
230 len += 4 + if_info->hwaddr_size +
231 4 + optional_strlen(if_info->name) +
232 sizeof(if_info->link);
235 for (i = 0; i < sysinfo->n_blockdevs; i++) {
236 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
237 len += 4 + optional_strlen(bd_info->name) +
238 4 + optional_strlen(bd_info->uuid) +
239 4 + optional_strlen(bd_info->mountpoint);
248 static int pb_protocol_interface_config_len(struct interface_config *conf)
252 len = sizeof(conf->hwaddr) +
253 4 /* conf->ignore */;
258 len += 4 /* conf->method */;
260 if (conf->method == CONFIG_METHOD_STATIC) {
261 len += 4 + optional_strlen(conf->static_config.address);
262 len += 4 + optional_strlen(conf->static_config.gateway);
263 len += 4 + optional_strlen(conf->static_config.url);
269 int pb_protocol_config_len(const struct config *config)
273 len = 4 /* config->autoboot_enabled */ +
274 4 /* config->autoboot_timeout_sec */ +
275 4 /* config->safe_mode */;
278 for (i = 0; i < config->network.n_interfaces; i++)
279 len += pb_protocol_interface_config_len(
280 config->network.interfaces[i]);
283 for (i = 0; i < config->network.n_dns_servers; i++)
284 len += 4 + optional_strlen(config->network.dns_servers[i]);
287 for (i = 0; i < config->n_autoboot_opts; i++) {
288 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
292 optional_strlen(config->autoboot_opts[i].uuid);
295 len += 4 + 4; /* ipmi_bootdev, ipmi_bootdev_persistent */
297 len += 4; /* allow_writes */
299 len += 4 + optional_strlen(config->lang);
304 int pb_protocol_url_len(const char *url)
306 /* url + length field */
307 return 4 + optional_strlen(url);
310 int pb_protocol_serialise_device(const struct device *dev,
311 char *buf, int buf_len)
315 pos += pb_protocol_serialise_string(pos, dev->id);
316 *(enum device_type *)pos = dev->type;
317 pos += sizeof(enum device_type);
318 pos += pb_protocol_serialise_string(pos, dev->name);
319 pos += pb_protocol_serialise_string(pos, dev->description);
320 pos += pb_protocol_serialise_string(pos, dev->icon_file);
322 assert(pos <= buf + buf_len);
328 int pb_protocol_serialise_boot_option(const struct boot_option *opt,
329 char *buf, int buf_len)
333 pos += pb_protocol_serialise_string(pos, opt->device_id);
334 pos += pb_protocol_serialise_string(pos, opt->id);
335 pos += pb_protocol_serialise_string(pos, opt->name);
336 pos += pb_protocol_serialise_string(pos, opt->description);
337 pos += pb_protocol_serialise_string(pos, opt->icon_file);
338 pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
339 pos += pb_protocol_serialise_string(pos, opt->initrd_file);
340 pos += pb_protocol_serialise_string(pos, opt->dtb_file);
341 pos += pb_protocol_serialise_string(pos, opt->boot_args);
343 *(bool *)pos = opt->is_default;
346 assert(pos <= buf + buf_len);
352 int pb_protocol_serialise_boot_command(const struct boot_command *boot,
353 char *buf, int buf_len)
357 pos += pb_protocol_serialise_string(pos, boot->option_id);
358 pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
359 pos += pb_protocol_serialise_string(pos, boot->initrd_file);
360 pos += pb_protocol_serialise_string(pos, boot->dtb_file);
361 pos += pb_protocol_serialise_string(pos, boot->boot_args);
363 assert(pos <= buf + buf_len);
369 int pb_protocol_serialise_boot_status(const struct boot_status *status,
370 char *buf, int buf_len)
374 *(uint32_t *)pos = __cpu_to_be32(status->type);
375 pos += sizeof(uint32_t);
377 pos += pb_protocol_serialise_string(pos, status->message);
378 pos += pb_protocol_serialise_string(pos, status->detail);
380 *(uint32_t *)pos = __cpu_to_be32(status->type);
381 pos += sizeof(uint32_t);
383 assert(pos <= buf + buf_len);
389 int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
390 char *buf, int buf_len)
395 pos += pb_protocol_serialise_string(pos, sysinfo->type);
396 pos += pb_protocol_serialise_string(pos, sysinfo->identifier);
398 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_interfaces);
399 pos += sizeof(uint32_t);
401 for (i = 0; i < sysinfo->n_interfaces; i++) {
402 struct interface_info *if_info = sysinfo->interfaces[i];
404 *(uint32_t *)pos = __cpu_to_be32(if_info->hwaddr_size);
405 pos += sizeof(uint32_t);
407 memcpy(pos, if_info->hwaddr, if_info->hwaddr_size);
408 pos += if_info->hwaddr_size;
410 pos += pb_protocol_serialise_string(pos, if_info->name);
412 *(bool *)pos = if_info->link;
416 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_blockdevs);
417 pos += sizeof(uint32_t);
419 for (i = 0; i < sysinfo->n_blockdevs; i++) {
420 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
422 pos += pb_protocol_serialise_string(pos, bd_info->name);
423 pos += pb_protocol_serialise_string(pos, bd_info->uuid);
424 pos += pb_protocol_serialise_string(pos, bd_info->mountpoint);
427 memcpy(pos, sysinfo->bmc_mac, HWADDR_SIZE);
430 assert(pos <= buf + buf_len);
436 static int pb_protocol_serialise_config_interface(char *buf,
437 struct interface_config *conf)
441 memcpy(pos, conf->hwaddr, sizeof(conf->hwaddr));
442 pos += sizeof(conf->hwaddr);
444 *(uint32_t *)pos = conf->ignore;
450 *(uint32_t *)pos = __cpu_to_be32(conf->method);
453 if (conf->method == CONFIG_METHOD_STATIC) {
454 pos += pb_protocol_serialise_string(pos,
455 conf->static_config.address);
456 pos += pb_protocol_serialise_string(pos,
457 conf->static_config.gateway);
458 pos += pb_protocol_serialise_string(pos,
459 conf->static_config.url);
465 int pb_protocol_serialise_config(const struct config *config,
466 char *buf, int buf_len)
471 *(uint32_t *)pos = config->autoboot_enabled;
474 *(uint32_t *)pos = __cpu_to_be32(config->autoboot_timeout_sec);
477 *(uint32_t *)pos = config->safe_mode;
480 *(uint32_t *)pos = __cpu_to_be32(config->network.n_interfaces);
482 for (i = 0; i < config->network.n_interfaces; i++) {
483 struct interface_config *iface =
484 config->network.interfaces[i];
485 pos += pb_protocol_serialise_config_interface(pos, iface);
488 *(uint32_t *)pos = __cpu_to_be32(config->network.n_dns_servers);
490 for (i = 0; i < config->network.n_dns_servers; i++) {
491 pos += pb_protocol_serialise_string(pos,
492 config->network.dns_servers[i]);
495 *(uint32_t *)pos = __cpu_to_be32(config->n_autoboot_opts);
497 for (i = 0; i < config->n_autoboot_opts; i++) {
499 __cpu_to_be32(config->autoboot_opts[i].boot_type);
501 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
503 __cpu_to_be32(config->autoboot_opts[i].type);
506 pos += pb_protocol_serialise_string(pos,
507 config->autoboot_opts[i].uuid);
511 *(uint32_t *)pos = __cpu_to_be32(config->ipmi_bootdev);
513 *(uint32_t *)pos = config->ipmi_bootdev_persistent;
516 *(uint32_t *)pos = config->allow_writes;
519 pos += pb_protocol_serialise_string(pos, config->lang);
521 assert(pos <= buf + buf_len);
527 int pb_protocol_serialise_url(const char *url, char *buf, int buf_len)
531 pos += pb_protocol_serialise_string(pos, url);
533 assert(pos <=buf+buf_len);
539 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
544 total_len = sizeof(*message) + message->payload_len;
546 message->payload_len = __cpu_to_be32(message->payload_len);
547 message->action = __cpu_to_be32(message->action);
549 for (pos = (void *)message; total_len;) {
550 rc = write(fd, pos, total_len);
559 talloc_free(message);
564 pb_log("%s: failed: %s\n", __func__, strerror(errno));
568 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
569 enum pb_protocol_action action, int payload_len)
571 struct pb_protocol_message *message;
573 if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
574 pb_log("%s: payload too big %u/%u\n", __func__, payload_len,
575 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
579 message = talloc_size(ctx, sizeof(*message) + payload_len);
581 /* we convert these to big-endian in write_message() */
582 message->action = action;
583 message->payload_len = payload_len;
589 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
591 struct pb_protocol_message *message, m;
595 /* use the stack for the initial 8-byte read */
597 rc = read(fd, &m, sizeof(m));
601 m.payload_len = __be32_to_cpu(m.payload_len);
602 m.action = __be32_to_cpu(m.action);
604 if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
605 pb_log("%s: payload too big %u/%u\n", __func__, m.payload_len,
606 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
610 message = talloc_size(ctx, sizeof(m) + m.payload_len);
611 memcpy(message, &m, sizeof(m));
613 for (len = 0; len < m.payload_len;) {
614 rc = read(fd, message->payload + len, m.payload_len - len);
617 talloc_free(message);
618 pb_log("%s: failed (%u): %s\n", __func__, len,
630 int pb_protocol_deserialise_device(struct device *dev,
631 const struct pb_protocol_message *message)
637 len = message->payload_len;
638 pos = message->payload;
640 if (read_string(dev, &pos, &len, &dev->id))
643 if (len < sizeof(enum device_type))
645 dev->type = *(enum device_type *)(pos);
646 pos += sizeof(enum device_type);
647 len -= sizeof(enum device_type);
649 if (read_string(dev, &pos, &len, &dev->name))
652 if (read_string(dev, &pos, &len, &dev->description))
655 if (read_string(dev, &pos, &len, &dev->icon_file))
664 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
665 const struct pb_protocol_message *message)
671 len = message->payload_len;
672 pos = message->payload;
674 if (read_string(opt, &pos, &len, &opt->device_id))
677 if (read_string(opt, &pos, &len, &opt->id))
680 if (read_string(opt, &pos, &len, &opt->name))
683 if (read_string(opt, &pos, &len, &opt->description))
686 if (read_string(opt, &pos, &len, &opt->icon_file))
689 if (read_string(opt, &pos, &len, &opt->boot_image_file))
692 if (read_string(opt, &pos, &len, &opt->initrd_file))
695 if (read_string(opt, &pos, &len, &opt->dtb_file))
698 if (read_string(opt, &pos, &len, &opt->boot_args))
701 if (len < sizeof(bool))
703 opt->is_default = *(bool *)(pos);
711 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
712 const struct pb_protocol_message *message)
718 len = message->payload_len;
719 pos = message->payload;
721 if (read_string(cmd, &pos, &len, &cmd->option_id))
724 if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
727 if (read_string(cmd, &pos, &len, &cmd->initrd_file))
730 if (read_string(cmd, &pos, &len, &cmd->dtb_file))
733 if (read_string(cmd, &pos, &len, &cmd->boot_args))
742 int pb_protocol_deserialise_boot_status(struct boot_status *status,
743 const struct pb_protocol_message *message)
749 len = message->payload_len;
750 pos = message->payload;
752 /* first up, the type enum... */
753 if (len < sizeof(uint32_t))
756 status->type = __be32_to_cpu(*(uint32_t *)(pos));
758 switch (status->type) {
759 case BOOT_STATUS_ERROR:
760 case BOOT_STATUS_INFO:
766 pos += sizeof(uint32_t);
767 len -= sizeof(uint32_t);
769 /* message and detail strings */
770 if (read_string(status, &pos, &len, &status->message))
773 if (read_string(status, &pos, &len, &status->detail))
776 /* and finally, progress */
777 if (len < sizeof(uint32_t))
780 status->progress = __be32_to_cpu(*(uint32_t *)(pos));
783 if (status->progress > 100)
784 status->progress = 100;
792 int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
793 const struct pb_protocol_message *message)
799 len = message->payload_len;
800 pos = message->payload;
802 /* type and identifier strings */
803 if (read_string(sysinfo, &pos, &len, &sysinfo->type))
806 if (read_string(sysinfo, &pos, &len, &sysinfo->identifier))
809 /* number of interfaces */
810 if (read_u32(&pos, &len, &sysinfo->n_interfaces))
813 sysinfo->interfaces = talloc_array(sysinfo, struct interface_info *,
814 sysinfo->n_interfaces);
816 for (i = 0; i < sysinfo->n_interfaces; i++) {
817 struct interface_info *if_info = talloc(sysinfo,
818 struct interface_info);
820 if (read_u32(&pos, &len, &if_info->hwaddr_size))
823 if (len < if_info->hwaddr_size)
826 if_info->hwaddr = talloc_memdup(if_info, pos,
827 if_info->hwaddr_size);
828 pos += if_info->hwaddr_size;
829 len -= if_info->hwaddr_size;
831 if (read_string(if_info, &pos, &len, &if_info->name))
834 if_info->link = *(bool *)pos;
835 pos += sizeof(if_info->link);
837 sysinfo->interfaces[i] = if_info;
840 /* number of interfaces */
841 if (read_u32(&pos, &len, &sysinfo->n_blockdevs))
844 sysinfo->blockdevs = talloc_array(sysinfo, struct blockdev_info *,
845 sysinfo->n_blockdevs);
847 for (i = 0; i < sysinfo->n_blockdevs; i++) {
848 struct blockdev_info *bd_info = talloc(sysinfo,
849 struct blockdev_info);
851 if (read_string(bd_info, &pos, &len, &bd_info->name))
854 if (read_string(bd_info, &pos, &len, &bd_info->uuid))
857 if (read_string(bd_info, &pos, &len, &bd_info->mountpoint))
860 sysinfo->blockdevs[i] = bd_info;
863 for (i = 0; i < HWADDR_SIZE; i++) {
865 sysinfo->bmc_mac = talloc_memdup(sysinfo, pos, HWADDR_SIZE);
878 static int pb_protocol_deserialise_config_interface(const char **buf,
879 unsigned int *len, struct interface_config *iface)
883 if (*len < sizeof(iface->hwaddr))
886 memcpy(iface->hwaddr, *buf, sizeof(iface->hwaddr));
887 *buf += sizeof(iface->hwaddr);
888 *len -= sizeof(iface->hwaddr);
890 if (read_u32(buf, len, &tmp))
892 iface->ignore = !!tmp;
897 if (read_u32(buf, len, &iface->method))
900 if (iface->method == CONFIG_METHOD_STATIC) {
901 if (read_string(iface, buf, len, &iface->static_config.address))
904 if (read_string(iface, buf, len, &iface->static_config.gateway))
907 if (read_string(iface, buf, len, &iface->static_config.url))
914 int pb_protocol_deserialise_config(struct config *config,
915 const struct pb_protocol_message *message)
917 unsigned int len, i, tmp;
922 len = message->payload_len;
923 pos = message->payload;
925 if (read_u32(&pos, &len, &tmp))
927 config->autoboot_enabled = !!tmp;
929 if (read_u32(&pos, &len, &config->autoboot_timeout_sec))
932 if (read_u32(&pos, &len, &tmp))
934 config->safe_mode = !!tmp;
936 if (read_u32(&pos, &len, &config->network.n_interfaces))
939 config->network.interfaces = talloc_array(config,
940 struct interface_config *, config->network.n_interfaces);
942 for (i = 0; i < config->network.n_interfaces; i++) {
943 struct interface_config *iface = talloc_zero(
944 config->network.interfaces,
945 struct interface_config);
946 if (pb_protocol_deserialise_config_interface(&pos, &len, iface))
948 config->network.interfaces[i] = iface;
951 if (read_u32(&pos, &len, &config->network.n_dns_servers))
953 config->network.dns_servers = talloc_array(config, const char *,
954 config->network.n_dns_servers);
956 for (i = 0; i < config->network.n_dns_servers; i++) {
957 if (read_string(config->network.dns_servers, &pos, &len, &str))
959 config->network.dns_servers[i] = str;
962 if (read_u32(&pos, &len, &config->n_autoboot_opts))
964 config->autoboot_opts = talloc_array(config, struct autoboot_option,
965 config->n_autoboot_opts);
967 for (i = 0; i < config->n_autoboot_opts; i++) {
968 if (read_u32(&pos, &len, &tmp))
970 config->autoboot_opts[i].boot_type = (int)tmp;
971 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
972 if (read_u32(&pos, &len, &tmp))
974 config->autoboot_opts[i].type = tmp;
976 if (read_string(config, &pos, &len, &str))
978 config->autoboot_opts[i].uuid = str;
982 if (read_u32(&pos, &len, &config->ipmi_bootdev))
984 if (read_u32(&pos, &len, &tmp))
986 config->ipmi_bootdev_persistent = !!tmp;
988 if (read_u32(&pos, &len, &tmp))
990 config->allow_writes = !!tmp;
992 if (read_string(config, &pos, &len, &str))