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);
245 static int pb_protocol_interface_config_len(struct interface_config *conf)
249 len = sizeof(conf->hwaddr) +
250 4 /* conf->ignore */;
255 len += 4 /* conf->method */;
257 if (conf->method == CONFIG_METHOD_STATIC) {
258 len += 4 + optional_strlen(conf->static_config.address);
259 len += 4 + optional_strlen(conf->static_config.gateway);
265 int pb_protocol_config_len(const struct config *config)
269 len = 4 /* config->autoboot_enabled */ +
270 4 /* config->autoboot_timeout_sec */ +
271 4 /* config->safe_mode */;
274 for (i = 0; i < config->network.n_interfaces; i++)
275 len += pb_protocol_interface_config_len(
276 config->network.interfaces[i]);
279 for (i = 0; i < config->network.n_dns_servers; i++)
280 len += 4 + optional_strlen(config->network.dns_servers[i]);
283 for (i = 0; i < config->n_autoboot_opts; i++) {
284 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
288 optional_strlen(config->autoboot_opts[i].uuid);
291 len += 4 + 4; /* ipmi_bootdev, ipmi_bootdev_persistent */
293 len += 4 + optional_strlen(config->lang);
298 int pb_protocol_url_len(const char *url)
300 /* url + length field */
301 return 4 + optional_strlen(url);
304 int pb_protocol_serialise_device(const struct device *dev,
305 char *buf, int buf_len)
309 pos += pb_protocol_serialise_string(pos, dev->id);
310 *(enum device_type *)pos = dev->type;
311 pos += sizeof(enum device_type);
312 pos += pb_protocol_serialise_string(pos, dev->name);
313 pos += pb_protocol_serialise_string(pos, dev->description);
314 pos += pb_protocol_serialise_string(pos, dev->icon_file);
316 assert(pos <= buf + buf_len);
322 int pb_protocol_serialise_boot_option(const struct boot_option *opt,
323 char *buf, int buf_len)
327 pos += pb_protocol_serialise_string(pos, opt->device_id);
328 pos += pb_protocol_serialise_string(pos, opt->id);
329 pos += pb_protocol_serialise_string(pos, opt->name);
330 pos += pb_protocol_serialise_string(pos, opt->description);
331 pos += pb_protocol_serialise_string(pos, opt->icon_file);
332 pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
333 pos += pb_protocol_serialise_string(pos, opt->initrd_file);
334 pos += pb_protocol_serialise_string(pos, opt->dtb_file);
335 pos += pb_protocol_serialise_string(pos, opt->boot_args);
337 *(bool *)pos = opt->is_default;
340 assert(pos <= buf + buf_len);
346 int pb_protocol_serialise_boot_command(const struct boot_command *boot,
347 char *buf, int buf_len)
351 pos += pb_protocol_serialise_string(pos, boot->option_id);
352 pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
353 pos += pb_protocol_serialise_string(pos, boot->initrd_file);
354 pos += pb_protocol_serialise_string(pos, boot->dtb_file);
355 pos += pb_protocol_serialise_string(pos, boot->boot_args);
357 assert(pos <= buf + buf_len);
363 int pb_protocol_serialise_boot_status(const struct boot_status *status,
364 char *buf, int buf_len)
368 *(uint32_t *)pos = __cpu_to_be32(status->type);
369 pos += sizeof(uint32_t);
371 pos += pb_protocol_serialise_string(pos, status->message);
372 pos += pb_protocol_serialise_string(pos, status->detail);
374 *(uint32_t *)pos = __cpu_to_be32(status->type);
375 pos += sizeof(uint32_t);
377 assert(pos <= buf + buf_len);
383 int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
384 char *buf, int buf_len)
389 pos += pb_protocol_serialise_string(pos, sysinfo->type);
390 pos += pb_protocol_serialise_string(pos, sysinfo->identifier);
392 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_interfaces);
393 pos += sizeof(uint32_t);
395 for (i = 0; i < sysinfo->n_interfaces; i++) {
396 struct interface_info *if_info = sysinfo->interfaces[i];
398 *(uint32_t *)pos = __cpu_to_be32(if_info->hwaddr_size);
399 pos += sizeof(uint32_t);
401 memcpy(pos, if_info->hwaddr, if_info->hwaddr_size);
402 pos += if_info->hwaddr_size;
404 pos += pb_protocol_serialise_string(pos, if_info->name);
406 *(bool *)pos = if_info->link;
410 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_blockdevs);
411 pos += sizeof(uint32_t);
413 for (i = 0; i < sysinfo->n_blockdevs; i++) {
414 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
416 pos += pb_protocol_serialise_string(pos, bd_info->name);
417 pos += pb_protocol_serialise_string(pos, bd_info->uuid);
418 pos += pb_protocol_serialise_string(pos, bd_info->mountpoint);
421 assert(pos <= buf + buf_len);
427 static int pb_protocol_serialise_config_interface(char *buf,
428 struct interface_config *conf)
432 memcpy(pos, conf->hwaddr, sizeof(conf->hwaddr));
433 pos += sizeof(conf->hwaddr);
435 *(uint32_t *)pos = conf->ignore;
441 *(uint32_t *)pos = __cpu_to_be32(conf->method);
444 if (conf->method == CONFIG_METHOD_STATIC) {
445 pos += pb_protocol_serialise_string(pos,
446 conf->static_config.address);
447 pos += pb_protocol_serialise_string(pos,
448 conf->static_config.gateway);
454 int pb_protocol_serialise_config(const struct config *config,
455 char *buf, int buf_len)
460 *(uint32_t *)pos = config->autoboot_enabled;
463 *(uint32_t *)pos = __cpu_to_be32(config->autoboot_timeout_sec);
466 *(uint32_t *)pos = config->safe_mode;
469 *(uint32_t *)pos = __cpu_to_be32(config->network.n_interfaces);
471 for (i = 0; i < config->network.n_interfaces; i++) {
472 struct interface_config *iface =
473 config->network.interfaces[i];
474 pos += pb_protocol_serialise_config_interface(pos, iface);
477 *(uint32_t *)pos = __cpu_to_be32(config->network.n_dns_servers);
479 for (i = 0; i < config->network.n_dns_servers; i++) {
480 pos += pb_protocol_serialise_string(pos,
481 config->network.dns_servers[i]);
484 *(uint32_t *)pos = __cpu_to_be32(config->n_autoboot_opts);
486 for (i = 0; i < config->n_autoboot_opts; i++) {
488 __cpu_to_be32(config->autoboot_opts[i].boot_type);
490 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
492 __cpu_to_be32(config->autoboot_opts[i].type);
495 pos += pb_protocol_serialise_string(pos,
496 config->autoboot_opts[i].uuid);
500 *(uint32_t *)pos = __cpu_to_be32(config->ipmi_bootdev);
502 *(uint32_t *)pos = config->ipmi_bootdev_persistent;
505 pos += pb_protocol_serialise_string(pos, config->lang);
507 assert(pos <= buf + buf_len);
513 int pb_protocol_serialise_url(const char *url, char *buf, int buf_len)
517 pos += pb_protocol_serialise_string(pos, url);
519 assert(pos <=buf+buf_len);
525 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
530 total_len = sizeof(*message) + message->payload_len;
532 message->payload_len = __cpu_to_be32(message->payload_len);
533 message->action = __cpu_to_be32(message->action);
535 for (pos = (void *)message; total_len;) {
536 rc = write(fd, pos, total_len);
545 talloc_free(message);
550 pb_log("%s: failed: %s\n", __func__, strerror(errno));
554 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
555 enum pb_protocol_action action, int payload_len)
557 struct pb_protocol_message *message;
559 if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
560 pb_log("%s: payload too big %u/%u\n", __func__, payload_len,
561 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
565 message = talloc_size(ctx, sizeof(*message) + payload_len);
567 /* we convert these to big-endian in write_message() */
568 message->action = action;
569 message->payload_len = payload_len;
575 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
577 struct pb_protocol_message *message, m;
581 /* use the stack for the initial 8-byte read */
583 rc = read(fd, &m, sizeof(m));
587 m.payload_len = __be32_to_cpu(m.payload_len);
588 m.action = __be32_to_cpu(m.action);
590 if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
591 pb_log("%s: payload too big %u/%u\n", __func__, m.payload_len,
592 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
596 message = talloc_size(ctx, sizeof(m) + m.payload_len);
597 memcpy(message, &m, sizeof(m));
599 for (len = 0; len < m.payload_len;) {
600 rc = read(fd, message->payload + len, m.payload_len - len);
603 talloc_free(message);
604 pb_log("%s: failed (%u): %s\n", __func__, len,
616 int pb_protocol_deserialise_device(struct device *dev,
617 const struct pb_protocol_message *message)
623 len = message->payload_len;
624 pos = message->payload;
626 if (read_string(dev, &pos, &len, &dev->id))
629 if (len < sizeof(enum device_type))
631 dev->type = *(enum device_type *)(pos);
632 pos += sizeof(enum device_type);
633 len -= sizeof(enum device_type);
635 if (read_string(dev, &pos, &len, &dev->name))
638 if (read_string(dev, &pos, &len, &dev->description))
641 if (read_string(dev, &pos, &len, &dev->icon_file))
650 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
651 const struct pb_protocol_message *message)
657 len = message->payload_len;
658 pos = message->payload;
660 if (read_string(opt, &pos, &len, &opt->device_id))
663 if (read_string(opt, &pos, &len, &opt->id))
666 if (read_string(opt, &pos, &len, &opt->name))
669 if (read_string(opt, &pos, &len, &opt->description))
672 if (read_string(opt, &pos, &len, &opt->icon_file))
675 if (read_string(opt, &pos, &len, &opt->boot_image_file))
678 if (read_string(opt, &pos, &len, &opt->initrd_file))
681 if (read_string(opt, &pos, &len, &opt->dtb_file))
684 if (read_string(opt, &pos, &len, &opt->boot_args))
687 if (len < sizeof(bool))
689 opt->is_default = *(bool *)(pos);
697 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
698 const struct pb_protocol_message *message)
704 len = message->payload_len;
705 pos = message->payload;
707 if (read_string(cmd, &pos, &len, &cmd->option_id))
710 if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
713 if (read_string(cmd, &pos, &len, &cmd->initrd_file))
716 if (read_string(cmd, &pos, &len, &cmd->dtb_file))
719 if (read_string(cmd, &pos, &len, &cmd->boot_args))
728 int pb_protocol_deserialise_boot_status(struct boot_status *status,
729 const struct pb_protocol_message *message)
735 len = message->payload_len;
736 pos = message->payload;
738 /* first up, the type enum... */
739 if (len < sizeof(uint32_t))
742 status->type = __be32_to_cpu(*(uint32_t *)(pos));
744 switch (status->type) {
745 case BOOT_STATUS_ERROR:
746 case BOOT_STATUS_INFO:
752 pos += sizeof(uint32_t);
753 len -= sizeof(uint32_t);
755 /* message and detail strings */
756 if (read_string(status, &pos, &len, &status->message))
759 if (read_string(status, &pos, &len, &status->detail))
762 /* and finally, progress */
763 if (len < sizeof(uint32_t))
766 status->progress = __be32_to_cpu(*(uint32_t *)(pos));
769 if (status->progress > 100)
770 status->progress = 100;
778 int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
779 const struct pb_protocol_message *message)
785 len = message->payload_len;
786 pos = message->payload;
788 /* type and identifier strings */
789 if (read_string(sysinfo, &pos, &len, &sysinfo->type))
792 if (read_string(sysinfo, &pos, &len, &sysinfo->identifier))
795 /* number of interfaces */
796 if (read_u32(&pos, &len, &sysinfo->n_interfaces))
799 sysinfo->interfaces = talloc_array(sysinfo, struct interface_info *,
800 sysinfo->n_interfaces);
802 for (i = 0; i < sysinfo->n_interfaces; i++) {
803 struct interface_info *if_info = talloc(sysinfo,
804 struct interface_info);
806 if (read_u32(&pos, &len, &if_info->hwaddr_size))
809 if (len < if_info->hwaddr_size)
812 if_info->hwaddr = talloc_memdup(if_info, pos,
813 if_info->hwaddr_size);
814 pos += if_info->hwaddr_size;
815 len -= if_info->hwaddr_size;
817 if (read_string(if_info, &pos, &len, &if_info->name))
820 if_info->link = *(bool *)pos;
821 pos += sizeof(if_info->link);
823 sysinfo->interfaces[i] = if_info;
826 /* number of interfaces */
827 if (read_u32(&pos, &len, &sysinfo->n_blockdevs))
830 sysinfo->blockdevs = talloc_array(sysinfo, struct blockdev_info *,
831 sysinfo->n_blockdevs);
833 for (i = 0; i < sysinfo->n_blockdevs; i++) {
834 struct blockdev_info *bd_info = talloc(sysinfo,
835 struct blockdev_info);
837 if (read_string(bd_info, &pos, &len, &bd_info->name))
840 if (read_string(bd_info, &pos, &len, &bd_info->uuid))
843 if (read_string(bd_info, &pos, &len, &bd_info->mountpoint))
846 sysinfo->blockdevs[i] = bd_info;
854 static int pb_protocol_deserialise_config_interface(const char **buf,
855 unsigned int *len, struct interface_config *iface)
859 if (*len < sizeof(iface->hwaddr))
862 memcpy(iface->hwaddr, *buf, sizeof(iface->hwaddr));
863 *buf += sizeof(iface->hwaddr);
864 *len -= sizeof(iface->hwaddr);
866 if (read_u32(buf, len, &tmp))
868 iface->ignore = !!tmp;
873 if (read_u32(buf, len, &iface->method))
876 if (iface->method == CONFIG_METHOD_STATIC) {
877 if (read_string(iface, buf, len, &iface->static_config.address))
880 if (read_string(iface, buf, len, &iface->static_config.gateway))
887 int pb_protocol_deserialise_config(struct config *config,
888 const struct pb_protocol_message *message)
890 unsigned int len, i, tmp;
895 len = message->payload_len;
896 pos = message->payload;
898 if (read_u32(&pos, &len, &tmp))
900 config->autoboot_enabled = !!tmp;
902 if (read_u32(&pos, &len, &config->autoboot_timeout_sec))
905 if (read_u32(&pos, &len, &tmp))
907 config->safe_mode = !!tmp;
909 if (read_u32(&pos, &len, &config->network.n_interfaces))
912 config->network.interfaces = talloc_array(config,
913 struct interface_config *, config->network.n_interfaces);
915 for (i = 0; i < config->network.n_interfaces; i++) {
916 struct interface_config *iface = talloc_zero(
917 config->network.interfaces,
918 struct interface_config);
919 if (pb_protocol_deserialise_config_interface(&pos, &len, iface))
921 config->network.interfaces[i] = iface;
924 if (read_u32(&pos, &len, &config->network.n_dns_servers))
926 config->network.dns_servers = talloc_array(config, const char *,
927 config->network.n_dns_servers);
929 for (i = 0; i < config->network.n_dns_servers; i++) {
930 if (read_string(config->network.dns_servers, &pos, &len, &str))
932 config->network.dns_servers[i] = str;
935 if (read_u32(&pos, &len, &config->n_autoboot_opts))
937 config->autoboot_opts = talloc_array(config, struct autoboot_option,
938 config->n_autoboot_opts);
940 for (i = 0; i < config->n_autoboot_opts; i++) {
941 if (read_u32(&pos, &len, &tmp))
943 config->autoboot_opts[i].boot_type = (int)tmp;
944 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
945 if (read_u32(&pos, &len, &tmp))
947 config->autoboot_opts[i].type = tmp;
949 if (read_string(config, &pos, &len, &str))
951 config->autoboot_opts[i].uuid = str;
955 if (read_u32(&pos, &len, &config->ipmi_bootdev))
957 if (read_u32(&pos, &len, &tmp))
959 config->ipmi_bootdev_persistent = !!tmp;
961 if (read_string(config, &pos, &len, &str))