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 len += config->n_boot_priorities * 8;
285 len += 4 + optional_strlen(config->boot_device);
287 len += 4 + 4; /* ipmi_bootdev, ipmi_bootdev_persistent */
289 len += 4 + optional_strlen(config->lang);
294 int pb_protocol_url_len(const char *url)
296 /* url + length field */
297 return 4 + optional_strlen(url);
300 int pb_protocol_serialise_device(const struct device *dev,
301 char *buf, int buf_len)
305 pos += pb_protocol_serialise_string(pos, dev->id);
306 *(enum device_type *)pos = dev->type;
307 pos += sizeof(enum device_type);
308 pos += pb_protocol_serialise_string(pos, dev->name);
309 pos += pb_protocol_serialise_string(pos, dev->description);
310 pos += pb_protocol_serialise_string(pos, dev->icon_file);
312 assert(pos <= buf + buf_len);
318 int pb_protocol_serialise_boot_option(const struct boot_option *opt,
319 char *buf, int buf_len)
323 pos += pb_protocol_serialise_string(pos, opt->device_id);
324 pos += pb_protocol_serialise_string(pos, opt->id);
325 pos += pb_protocol_serialise_string(pos, opt->name);
326 pos += pb_protocol_serialise_string(pos, opt->description);
327 pos += pb_protocol_serialise_string(pos, opt->icon_file);
328 pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
329 pos += pb_protocol_serialise_string(pos, opt->initrd_file);
330 pos += pb_protocol_serialise_string(pos, opt->dtb_file);
331 pos += pb_protocol_serialise_string(pos, opt->boot_args);
333 *(bool *)pos = opt->is_default;
336 assert(pos <= buf + buf_len);
342 int pb_protocol_serialise_boot_command(const struct boot_command *boot,
343 char *buf, int buf_len)
347 pos += pb_protocol_serialise_string(pos, boot->option_id);
348 pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
349 pos += pb_protocol_serialise_string(pos, boot->initrd_file);
350 pos += pb_protocol_serialise_string(pos, boot->dtb_file);
351 pos += pb_protocol_serialise_string(pos, boot->boot_args);
353 assert(pos <= buf + buf_len);
359 int pb_protocol_serialise_boot_status(const struct boot_status *status,
360 char *buf, int buf_len)
364 *(uint32_t *)pos = __cpu_to_be32(status->type);
365 pos += sizeof(uint32_t);
367 pos += pb_protocol_serialise_string(pos, status->message);
368 pos += pb_protocol_serialise_string(pos, status->detail);
370 *(uint32_t *)pos = __cpu_to_be32(status->type);
371 pos += sizeof(uint32_t);
373 assert(pos <= buf + buf_len);
379 int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
380 char *buf, int buf_len)
385 pos += pb_protocol_serialise_string(pos, sysinfo->type);
386 pos += pb_protocol_serialise_string(pos, sysinfo->identifier);
388 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_interfaces);
389 pos += sizeof(uint32_t);
391 for (i = 0; i < sysinfo->n_interfaces; i++) {
392 struct interface_info *if_info = sysinfo->interfaces[i];
394 *(uint32_t *)pos = __cpu_to_be32(if_info->hwaddr_size);
395 pos += sizeof(uint32_t);
397 memcpy(pos, if_info->hwaddr, if_info->hwaddr_size);
398 pos += if_info->hwaddr_size;
400 pos += pb_protocol_serialise_string(pos, if_info->name);
402 *(bool *)pos = if_info->link;
406 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_blockdevs);
407 pos += sizeof(uint32_t);
409 for (i = 0; i < sysinfo->n_blockdevs; i++) {
410 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
412 pos += pb_protocol_serialise_string(pos, bd_info->name);
413 pos += pb_protocol_serialise_string(pos, bd_info->uuid);
414 pos += pb_protocol_serialise_string(pos, bd_info->mountpoint);
417 assert(pos <= buf + buf_len);
423 static int pb_protocol_serialise_config_interface(char *buf,
424 struct interface_config *conf)
428 memcpy(pos, conf->hwaddr, sizeof(conf->hwaddr));
429 pos += sizeof(conf->hwaddr);
431 *(uint32_t *)pos = conf->ignore;
437 *(uint32_t *)pos = __cpu_to_be32(conf->method);
440 if (conf->method == CONFIG_METHOD_STATIC) {
441 pos += pb_protocol_serialise_string(pos,
442 conf->static_config.address);
443 pos += pb_protocol_serialise_string(pos,
444 conf->static_config.gateway);
450 int pb_protocol_serialise_config(const struct config *config,
451 char *buf, int buf_len)
456 *(uint32_t *)pos = config->autoboot_enabled;
459 *(uint32_t *)pos = __cpu_to_be32(config->autoboot_timeout_sec);
462 *(uint32_t *)pos = config->safe_mode;
465 *(uint32_t *)pos = __cpu_to_be32(config->network.n_interfaces);
467 for (i = 0; i < config->network.n_interfaces; i++) {
468 struct interface_config *iface =
469 config->network.interfaces[i];
470 pos += pb_protocol_serialise_config_interface(pos, iface);
473 *(uint32_t *)pos = __cpu_to_be32(config->network.n_dns_servers);
475 for (i = 0; i < config->network.n_dns_servers; i++) {
476 pos += pb_protocol_serialise_string(pos,
477 config->network.dns_servers[i]);
480 *(uint32_t *)pos = __cpu_to_be32(config->n_boot_priorities);
482 for (i = 0; i < config->n_boot_priorities; i++) {
484 __cpu_to_be32(config->boot_priorities[i].type);
487 __cpu_to_be32(config->boot_priorities[i].priority);
491 pos += pb_protocol_serialise_string(pos, config->boot_device);
493 *(uint32_t *)pos = __cpu_to_be32(config->ipmi_bootdev);
495 *(uint32_t *)pos = config->ipmi_bootdev_persistent;
498 pos += pb_protocol_serialise_string(pos, config->lang);
500 assert(pos <= buf + buf_len);
506 int pb_protocol_serialise_url(const char *url, char *buf, int buf_len)
510 pos += pb_protocol_serialise_string(pos, url);
512 assert(pos <=buf+buf_len);
518 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
523 total_len = sizeof(*message) + message->payload_len;
525 message->payload_len = __cpu_to_be32(message->payload_len);
526 message->action = __cpu_to_be32(message->action);
528 for (pos = (void *)message; total_len;) {
529 rc = write(fd, pos, total_len);
538 talloc_free(message);
543 pb_log("%s: failed: %s\n", __func__, strerror(errno));
547 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
548 enum pb_protocol_action action, int payload_len)
550 struct pb_protocol_message *message;
552 if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
553 pb_log("%s: payload too big %u/%u\n", __func__, payload_len,
554 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
558 message = talloc_size(ctx, sizeof(*message) + payload_len);
560 /* we convert these to big-endian in write_message() */
561 message->action = action;
562 message->payload_len = payload_len;
568 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
570 struct pb_protocol_message *message, m;
574 /* use the stack for the initial 8-byte read */
576 rc = read(fd, &m, sizeof(m));
580 m.payload_len = __be32_to_cpu(m.payload_len);
581 m.action = __be32_to_cpu(m.action);
583 if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
584 pb_log("%s: payload too big %u/%u\n", __func__, m.payload_len,
585 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
589 message = talloc_size(ctx, sizeof(m) + m.payload_len);
590 memcpy(message, &m, sizeof(m));
592 for (len = 0; len < m.payload_len;) {
593 rc = read(fd, message->payload + len, m.payload_len - len);
596 talloc_free(message);
597 pb_log("%s: failed (%u): %s\n", __func__, len,
609 int pb_protocol_deserialise_device(struct device *dev,
610 const struct pb_protocol_message *message)
616 len = message->payload_len;
617 pos = message->payload;
619 if (read_string(dev, &pos, &len, &dev->id))
622 if (len < sizeof(enum device_type))
624 dev->type = *(enum device_type *)(pos);
625 pos += sizeof(enum device_type);
626 len -= sizeof(enum device_type);
628 if (read_string(dev, &pos, &len, &dev->name))
631 if (read_string(dev, &pos, &len, &dev->description))
634 if (read_string(dev, &pos, &len, &dev->icon_file))
643 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
644 const struct pb_protocol_message *message)
650 len = message->payload_len;
651 pos = message->payload;
653 if (read_string(opt, &pos, &len, &opt->device_id))
656 if (read_string(opt, &pos, &len, &opt->id))
659 if (read_string(opt, &pos, &len, &opt->name))
662 if (read_string(opt, &pos, &len, &opt->description))
665 if (read_string(opt, &pos, &len, &opt->icon_file))
668 if (read_string(opt, &pos, &len, &opt->boot_image_file))
671 if (read_string(opt, &pos, &len, &opt->initrd_file))
674 if (read_string(opt, &pos, &len, &opt->dtb_file))
677 if (read_string(opt, &pos, &len, &opt->boot_args))
680 if (len < sizeof(bool))
682 opt->is_default = *(bool *)(pos);
690 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
691 const struct pb_protocol_message *message)
697 len = message->payload_len;
698 pos = message->payload;
700 if (read_string(cmd, &pos, &len, &cmd->option_id))
703 if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
706 if (read_string(cmd, &pos, &len, &cmd->initrd_file))
709 if (read_string(cmd, &pos, &len, &cmd->dtb_file))
712 if (read_string(cmd, &pos, &len, &cmd->boot_args))
721 int pb_protocol_deserialise_boot_status(struct boot_status *status,
722 const struct pb_protocol_message *message)
728 len = message->payload_len;
729 pos = message->payload;
731 /* first up, the type enum... */
732 if (len < sizeof(uint32_t))
735 status->type = __be32_to_cpu(*(uint32_t *)(pos));
737 switch (status->type) {
738 case BOOT_STATUS_ERROR:
739 case BOOT_STATUS_INFO:
745 pos += sizeof(uint32_t);
746 len -= sizeof(uint32_t);
748 /* message and detail strings */
749 if (read_string(status, &pos, &len, &status->message))
752 if (read_string(status, &pos, &len, &status->detail))
755 /* and finally, progress */
756 if (len < sizeof(uint32_t))
759 status->progress = __be32_to_cpu(*(uint32_t *)(pos));
762 if (status->progress > 100)
763 status->progress = 100;
771 int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
772 const struct pb_protocol_message *message)
778 len = message->payload_len;
779 pos = message->payload;
781 /* type and identifier strings */
782 if (read_string(sysinfo, &pos, &len, &sysinfo->type))
785 if (read_string(sysinfo, &pos, &len, &sysinfo->identifier))
788 /* number of interfaces */
789 if (read_u32(&pos, &len, &sysinfo->n_interfaces))
792 sysinfo->interfaces = talloc_array(sysinfo, struct interface_info *,
793 sysinfo->n_interfaces);
795 for (i = 0; i < sysinfo->n_interfaces; i++) {
796 struct interface_info *if_info = talloc(sysinfo,
797 struct interface_info);
799 if (read_u32(&pos, &len, &if_info->hwaddr_size))
802 if (len < if_info->hwaddr_size)
805 if_info->hwaddr = talloc_memdup(if_info, pos,
806 if_info->hwaddr_size);
807 pos += if_info->hwaddr_size;
808 len -= if_info->hwaddr_size;
810 if (read_string(if_info, &pos, &len, &if_info->name))
813 if_info->link = *(bool *)pos;
814 pos += sizeof(if_info->link);
816 sysinfo->interfaces[i] = if_info;
819 /* number of interfaces */
820 if (read_u32(&pos, &len, &sysinfo->n_blockdevs))
823 sysinfo->blockdevs = talloc_array(sysinfo, struct blockdev_info *,
824 sysinfo->n_blockdevs);
826 for (i = 0; i < sysinfo->n_blockdevs; i++) {
827 struct blockdev_info *bd_info = talloc(sysinfo,
828 struct blockdev_info);
830 if (read_string(bd_info, &pos, &len, &bd_info->name))
833 if (read_string(bd_info, &pos, &len, &bd_info->uuid))
836 if (read_string(bd_info, &pos, &len, &bd_info->mountpoint))
839 sysinfo->blockdevs[i] = bd_info;
847 static int pb_protocol_deserialise_config_interface(const char **buf,
848 unsigned int *len, struct interface_config *iface)
852 if (*len < sizeof(iface->hwaddr))
855 memcpy(iface->hwaddr, *buf, sizeof(iface->hwaddr));
856 *buf += sizeof(iface->hwaddr);
857 *len -= sizeof(iface->hwaddr);
859 if (read_u32(buf, len, &tmp))
861 iface->ignore = !!tmp;
866 if (read_u32(buf, len, &iface->method))
869 if (iface->method == CONFIG_METHOD_STATIC) {
870 if (read_string(iface, buf, len, &iface->static_config.address))
873 if (read_string(iface, buf, len, &iface->static_config.gateway))
880 int pb_protocol_deserialise_config(struct config *config,
881 const struct pb_protocol_message *message)
883 unsigned int len, i, tmp;
888 len = message->payload_len;
889 pos = message->payload;
891 if (read_u32(&pos, &len, &tmp))
893 config->autoboot_enabled = !!tmp;
895 if (read_u32(&pos, &len, &config->autoboot_timeout_sec))
898 if (read_u32(&pos, &len, &tmp))
900 config->safe_mode = !!tmp;
902 if (read_u32(&pos, &len, &config->network.n_interfaces))
905 config->network.interfaces = talloc_array(config,
906 struct interface_config *, config->network.n_interfaces);
908 for (i = 0; i < config->network.n_interfaces; i++) {
909 struct interface_config *iface = talloc_zero(
910 config->network.interfaces,
911 struct interface_config);
912 if (pb_protocol_deserialise_config_interface(&pos, &len, iface))
914 config->network.interfaces[i] = iface;
917 if (read_u32(&pos, &len, &config->network.n_dns_servers))
919 config->network.dns_servers = talloc_array(config, const char *,
920 config->network.n_dns_servers);
922 for (i = 0; i < config->network.n_dns_servers; i++) {
923 if (read_string(config->network.dns_servers, &pos, &len, &str))
925 config->network.dns_servers[i] = str;
928 if (read_u32(&pos, &len, &config->n_boot_priorities))
930 config->boot_priorities = talloc_array(config, struct boot_priority,
931 config->n_boot_priorities);
933 for (i = 0; i < config->n_boot_priorities; i++) {
934 if (read_u32(&pos, &len, &tmp))
936 config->boot_priorities[i].priority = (int)tmp;
937 if (read_u32(&pos, &len, &tmp))
939 config->boot_priorities[i].type = tmp;
942 if (read_string(config, &pos, &len, &str))
944 config->boot_device = str;
946 if (read_u32(&pos, &len, &config->ipmi_bootdev))
948 if (read_u32(&pos, &len, &tmp))
950 config->ipmi_bootdev_persistent = !!tmp;
952 if (read_string(config, &pos, &len, &str))