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 */;
273 for (i = 0; i < config->network.n_interfaces; i++)
274 len += pb_protocol_interface_config_len(
275 config->network.interfaces[i]);
278 for (i = 0; i < config->network.n_dns_servers; i++)
279 len += 4 + optional_strlen(config->network.dns_servers[i]);
282 len += config->n_boot_priorities * 8;
287 int pb_protocol_serialise_device(const struct device *dev,
288 char *buf, int buf_len)
292 pos += pb_protocol_serialise_string(pos, dev->id);
293 *(enum device_type *)pos = dev->type;
294 pos += sizeof(enum device_type);
295 pos += pb_protocol_serialise_string(pos, dev->name);
296 pos += pb_protocol_serialise_string(pos, dev->description);
297 pos += pb_protocol_serialise_string(pos, dev->icon_file);
299 assert(pos <= buf + buf_len);
305 int pb_protocol_serialise_boot_option(const struct boot_option *opt,
306 char *buf, int buf_len)
310 pos += pb_protocol_serialise_string(pos, opt->device_id);
311 pos += pb_protocol_serialise_string(pos, opt->id);
312 pos += pb_protocol_serialise_string(pos, opt->name);
313 pos += pb_protocol_serialise_string(pos, opt->description);
314 pos += pb_protocol_serialise_string(pos, opt->icon_file);
315 pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
316 pos += pb_protocol_serialise_string(pos, opt->initrd_file);
317 pos += pb_protocol_serialise_string(pos, opt->dtb_file);
318 pos += pb_protocol_serialise_string(pos, opt->boot_args);
320 *(bool *)pos = opt->is_default;
323 assert(pos <= buf + buf_len);
329 int pb_protocol_serialise_boot_command(const struct boot_command *boot,
330 char *buf, int buf_len)
334 pos += pb_protocol_serialise_string(pos, boot->option_id);
335 pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
336 pos += pb_protocol_serialise_string(pos, boot->initrd_file);
337 pos += pb_protocol_serialise_string(pos, boot->dtb_file);
338 pos += pb_protocol_serialise_string(pos, boot->boot_args);
340 assert(pos <= buf + buf_len);
346 int pb_protocol_serialise_boot_status(const struct boot_status *status,
347 char *buf, int buf_len)
351 *(uint32_t *)pos = __cpu_to_be32(status->type);
352 pos += sizeof(uint32_t);
354 pos += pb_protocol_serialise_string(pos, status->message);
355 pos += pb_protocol_serialise_string(pos, status->detail);
357 *(uint32_t *)pos = __cpu_to_be32(status->type);
358 pos += sizeof(uint32_t);
360 assert(pos <= buf + buf_len);
366 int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
367 char *buf, int buf_len)
372 pos += pb_protocol_serialise_string(pos, sysinfo->type);
373 pos += pb_protocol_serialise_string(pos, sysinfo->identifier);
375 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_interfaces);
376 pos += sizeof(uint32_t);
378 for (i = 0; i < sysinfo->n_interfaces; i++) {
379 struct interface_info *if_info = sysinfo->interfaces[i];
381 *(uint32_t *)pos = __cpu_to_be32(if_info->hwaddr_size);
382 pos += sizeof(uint32_t);
384 memcpy(pos, if_info->hwaddr, if_info->hwaddr_size);
385 pos += if_info->hwaddr_size;
387 pos += pb_protocol_serialise_string(pos, if_info->name);
389 *(bool *)pos = if_info->link;
393 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_blockdevs);
394 pos += sizeof(uint32_t);
396 for (i = 0; i < sysinfo->n_blockdevs; i++) {
397 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
399 pos += pb_protocol_serialise_string(pos, bd_info->name);
400 pos += pb_protocol_serialise_string(pos, bd_info->uuid);
401 pos += pb_protocol_serialise_string(pos, bd_info->mountpoint);
404 assert(pos <= buf + buf_len);
410 static int pb_protocol_serialise_config_interface(char *buf,
411 struct interface_config *conf)
415 memcpy(pos, conf->hwaddr, sizeof(conf->hwaddr));
416 pos += sizeof(conf->hwaddr);
418 *(uint32_t *)pos = conf->ignore;
424 *(uint32_t *)pos = __cpu_to_be32(conf->method);
427 if (conf->method == CONFIG_METHOD_STATIC) {
428 pos += pb_protocol_serialise_string(pos,
429 conf->static_config.address);
430 pos += pb_protocol_serialise_string(pos,
431 conf->static_config.gateway);
437 int pb_protocol_serialise_config(const struct config *config,
438 char *buf, int buf_len)
443 *(uint32_t *)pos = config->autoboot_enabled;
446 *(uint32_t *)pos = __cpu_to_be32(config->autoboot_timeout_sec);
449 *(uint32_t *)pos = __cpu_to_be32(config->network.n_interfaces);
451 for (i = 0; i < config->network.n_interfaces; i++) {
452 struct interface_config *iface =
453 config->network.interfaces[i];
454 pos += pb_protocol_serialise_config_interface(pos, iface);
457 *(uint32_t *)pos = __cpu_to_be32(config->network.n_dns_servers);
459 for (i = 0; i < config->network.n_dns_servers; i++) {
460 pos += pb_protocol_serialise_string(pos,
461 config->network.dns_servers[i]);
464 *(uint32_t *)pos = __cpu_to_be32(config->n_boot_priorities);
466 for (i = 0; i < config->n_boot_priorities; i++) {
468 __cpu_to_be32(config->boot_priorities[i].type);
471 __cpu_to_be32(config->boot_priorities[i].priority);
475 assert(pos <= buf + buf_len);
481 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
486 total_len = sizeof(*message) + message->payload_len;
488 message->payload_len = __cpu_to_be32(message->payload_len);
489 message->action = __cpu_to_be32(message->action);
491 for (pos = (void *)message; total_len;) {
492 rc = write(fd, pos, total_len);
501 talloc_free(message);
506 pb_log("%s: failed: %s\n", __func__, strerror(errno));
510 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
511 enum pb_protocol_action action, int payload_len)
513 struct pb_protocol_message *message;
515 if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
516 pb_log("%s: payload too big %u/%u\n", __func__, payload_len,
517 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
521 message = talloc_size(ctx, sizeof(*message) + payload_len);
523 /* we convert these to big-endian in write_message() */
524 message->action = action;
525 message->payload_len = payload_len;
531 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
533 struct pb_protocol_message *message, m;
537 /* use the stack for the initial 8-byte read */
539 rc = read(fd, &m, sizeof(m));
543 m.payload_len = __be32_to_cpu(m.payload_len);
544 m.action = __be32_to_cpu(m.action);
546 if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
547 pb_log("%s: payload too big %u/%u\n", __func__, m.payload_len,
548 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
552 message = talloc_size(ctx, sizeof(m) + m.payload_len);
553 memcpy(message, &m, sizeof(m));
555 for (len = 0; len < m.payload_len;) {
556 rc = read(fd, message->payload + len, m.payload_len - len);
559 talloc_free(message);
560 pb_log("%s: failed (%u): %s\n", __func__, len,
572 int pb_protocol_deserialise_device(struct device *dev,
573 const struct pb_protocol_message *message)
579 len = message->payload_len;
580 pos = message->payload;
582 if (read_string(dev, &pos, &len, &dev->id))
585 if (len < sizeof(enum device_type))
587 dev->type = *(enum device_type *)(pos);
588 pos += sizeof(enum device_type);
589 len -= sizeof(enum device_type);
591 if (read_string(dev, &pos, &len, &dev->name))
594 if (read_string(dev, &pos, &len, &dev->description))
597 if (read_string(dev, &pos, &len, &dev->icon_file))
606 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
607 const struct pb_protocol_message *message)
613 len = message->payload_len;
614 pos = message->payload;
616 if (read_string(opt, &pos, &len, &opt->device_id))
619 if (read_string(opt, &pos, &len, &opt->id))
622 if (read_string(opt, &pos, &len, &opt->name))
625 if (read_string(opt, &pos, &len, &opt->description))
628 if (read_string(opt, &pos, &len, &opt->icon_file))
631 if (read_string(opt, &pos, &len, &opt->boot_image_file))
634 if (read_string(opt, &pos, &len, &opt->initrd_file))
637 if (read_string(opt, &pos, &len, &opt->dtb_file))
640 if (read_string(opt, &pos, &len, &opt->boot_args))
643 if (len < sizeof(bool))
645 opt->is_default = *(bool *)(pos);
653 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
654 const struct pb_protocol_message *message)
660 len = message->payload_len;
661 pos = message->payload;
663 if (read_string(cmd, &pos, &len, &cmd->option_id))
666 if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
669 if (read_string(cmd, &pos, &len, &cmd->initrd_file))
672 if (read_string(cmd, &pos, &len, &cmd->dtb_file))
675 if (read_string(cmd, &pos, &len, &cmd->boot_args))
684 int pb_protocol_deserialise_boot_status(struct boot_status *status,
685 const struct pb_protocol_message *message)
691 len = message->payload_len;
692 pos = message->payload;
694 /* first up, the type enum... */
695 if (len < sizeof(uint32_t))
698 status->type = __be32_to_cpu(*(uint32_t *)(pos));
700 switch (status->type) {
701 case BOOT_STATUS_ERROR:
702 case BOOT_STATUS_INFO:
708 pos += sizeof(uint32_t);
709 len -= sizeof(uint32_t);
711 /* message and detail strings */
712 if (read_string(status, &pos, &len, &status->message))
715 if (read_string(status, &pos, &len, &status->detail))
718 /* and finally, progress */
719 if (len < sizeof(uint32_t))
722 status->progress = __be32_to_cpu(*(uint32_t *)(pos));
725 if (status->progress > 100)
726 status->progress = 100;
734 int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
735 const struct pb_protocol_message *message)
741 len = message->payload_len;
742 pos = message->payload;
744 /* type and identifier strings */
745 if (read_string(sysinfo, &pos, &len, &sysinfo->type))
748 if (read_string(sysinfo, &pos, &len, &sysinfo->identifier))
751 /* number of interfaces */
752 if (read_u32(&pos, &len, &sysinfo->n_interfaces))
755 sysinfo->interfaces = talloc_array(sysinfo, struct interface_info *,
756 sysinfo->n_interfaces);
758 for (i = 0; i < sysinfo->n_interfaces; i++) {
759 struct interface_info *if_info = talloc(sysinfo,
760 struct interface_info);
762 if (read_u32(&pos, &len, &if_info->hwaddr_size))
765 if (len < if_info->hwaddr_size)
768 if_info->hwaddr = talloc_memdup(if_info, pos,
769 if_info->hwaddr_size);
770 pos += if_info->hwaddr_size;
771 len -= if_info->hwaddr_size;
773 if (read_string(if_info, &pos, &len, &if_info->name))
776 if_info->link = *(bool *)pos;
777 pos += sizeof(if_info->link);
779 sysinfo->interfaces[i] = if_info;
782 /* number of interfaces */
783 if (read_u32(&pos, &len, &sysinfo->n_blockdevs))
786 sysinfo->blockdevs = talloc_array(sysinfo, struct blockdev_info *,
787 sysinfo->n_blockdevs);
789 for (i = 0; i < sysinfo->n_blockdevs; i++) {
790 struct blockdev_info *bd_info = talloc(sysinfo,
791 struct blockdev_info);
793 if (read_string(bd_info, &pos, &len, &bd_info->name))
796 if (read_string(bd_info, &pos, &len, &bd_info->uuid))
799 if (read_string(bd_info, &pos, &len, &bd_info->mountpoint))
802 sysinfo->blockdevs[i] = bd_info;
810 static int pb_protocol_deserialise_config_interface(const char **buf,
811 unsigned int *len, struct interface_config *iface)
815 if (*len < sizeof(iface->hwaddr))
818 memcpy(iface->hwaddr, *buf, sizeof(iface->hwaddr));
819 *buf += sizeof(iface->hwaddr);
820 *len -= sizeof(iface->hwaddr);
822 if (read_u32(buf, len, &tmp))
824 iface->ignore = !!tmp;
829 if (read_u32(buf, len, &iface->method))
832 if (iface->method == CONFIG_METHOD_STATIC) {
833 if (read_string(iface, buf, len, &iface->static_config.address))
836 if (read_string(iface, buf, len, &iface->static_config.gateway))
843 int pb_protocol_deserialise_config(struct config *config,
844 const struct pb_protocol_message *message)
846 unsigned int len, i, tmp;
850 len = message->payload_len;
851 pos = message->payload;
853 if (read_u32(&pos, &len, &tmp))
855 config->autoboot_enabled = !!tmp;
857 if (read_u32(&pos, &len, &config->autoboot_timeout_sec))
860 if (read_u32(&pos, &len, &config->network.n_interfaces))
863 config->network.interfaces = talloc_array(config,
864 struct interface_config *, config->network.n_interfaces);
866 for (i = 0; i < config->network.n_interfaces; i++) {
867 struct interface_config *iface = talloc_zero(
868 config->network.interfaces,
869 struct interface_config);
870 if (pb_protocol_deserialise_config_interface(&pos, &len, iface))
872 config->network.interfaces[i] = iface;
875 if (read_u32(&pos, &len, &config->network.n_dns_servers))
877 config->network.dns_servers = talloc_array(config, const char *,
878 config->network.n_dns_servers);
880 for (i = 0; i < config->network.n_dns_servers; i++) {
882 if (read_string(config->network.dns_servers, &pos, &len, &tmp))
884 config->network.dns_servers[i] = tmp;
887 if (read_u32(&pos, &len, &config->n_boot_priorities))
889 config->boot_priorities = talloc_array(config, struct boot_priority,
890 config->n_boot_priorities);
892 for (i = 0; i < config->n_boot_priorities; i++) {
893 if (read_u32(&pos, &len, &tmp))
895 config->boot_priorities[i].type = tmp;