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);
237 int pb_protocol_serialise_device(const struct device *dev,
238 char *buf, int buf_len)
242 pos += pb_protocol_serialise_string(pos, dev->id);
243 *(enum device_type *)pos = dev->type;
244 pos += sizeof(enum device_type);
245 pos += pb_protocol_serialise_string(pos, dev->name);
246 pos += pb_protocol_serialise_string(pos, dev->description);
247 pos += pb_protocol_serialise_string(pos, dev->icon_file);
249 assert(pos <= buf + buf_len);
255 int pb_protocol_serialise_boot_option(const struct boot_option *opt,
256 char *buf, int buf_len)
260 pos += pb_protocol_serialise_string(pos, opt->device_id);
261 pos += pb_protocol_serialise_string(pos, opt->id);
262 pos += pb_protocol_serialise_string(pos, opt->name);
263 pos += pb_protocol_serialise_string(pos, opt->description);
264 pos += pb_protocol_serialise_string(pos, opt->icon_file);
265 pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
266 pos += pb_protocol_serialise_string(pos, opt->initrd_file);
267 pos += pb_protocol_serialise_string(pos, opt->dtb_file);
268 pos += pb_protocol_serialise_string(pos, opt->boot_args);
270 *(bool *)pos = opt->is_default;
273 assert(pos <= buf + buf_len);
279 int pb_protocol_serialise_boot_command(const struct boot_command *boot,
280 char *buf, int buf_len)
284 pos += pb_protocol_serialise_string(pos, boot->option_id);
285 pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
286 pos += pb_protocol_serialise_string(pos, boot->initrd_file);
287 pos += pb_protocol_serialise_string(pos, boot->dtb_file);
288 pos += pb_protocol_serialise_string(pos, boot->boot_args);
290 assert(pos <= buf + buf_len);
296 int pb_protocol_serialise_boot_status(const struct boot_status *status,
297 char *buf, int buf_len)
301 *(uint32_t *)pos = __cpu_to_be32(status->type);
302 pos += sizeof(uint32_t);
304 pos += pb_protocol_serialise_string(pos, status->message);
305 pos += pb_protocol_serialise_string(pos, status->detail);
307 *(uint32_t *)pos = __cpu_to_be32(status->type);
308 pos += sizeof(uint32_t);
310 assert(pos <= buf + buf_len);
316 int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
317 char *buf, int buf_len)
322 pos += pb_protocol_serialise_string(pos, sysinfo->type);
323 pos += pb_protocol_serialise_string(pos, sysinfo->identifier);
325 *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_interfaces);
326 pos += sizeof(uint32_t);
328 for (i = 0; i < sysinfo->n_interfaces; i++) {
329 struct interface_info *if_info = sysinfo->interfaces[i];
331 *(uint32_t *)pos = __cpu_to_be32(if_info->hwaddr_size);
332 pos += sizeof(uint32_t);
334 memcpy(pos, if_info->hwaddr, if_info->hwaddr_size);
335 pos += if_info->hwaddr_size;
337 pos += pb_protocol_serialise_string(pos, if_info->name);
340 assert(pos <= buf + buf_len);
346 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
351 total_len = sizeof(*message) + message->payload_len;
353 message->payload_len = __cpu_to_be32(message->payload_len);
354 message->action = __cpu_to_be32(message->action);
356 for (pos = (void *)message; total_len;) {
357 rc = write(fd, pos, total_len);
366 talloc_free(message);
371 pb_log("%s: failed: %s\n", __func__, strerror(errno));
375 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
376 enum pb_protocol_action action, int payload_len)
378 struct pb_protocol_message *message;
380 if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
381 pb_log("%s: payload too big %u/%u\n", __func__, payload_len,
382 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
386 message = talloc_size(ctx, sizeof(*message) + payload_len);
388 /* we convert these to big-endian in write_message() */
389 message->action = action;
390 message->payload_len = payload_len;
396 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
398 struct pb_protocol_message *message, m;
402 /* use the stack for the initial 8-byte read */
404 rc = read(fd, &m, sizeof(m));
408 m.payload_len = __be32_to_cpu(m.payload_len);
409 m.action = __be32_to_cpu(m.action);
411 if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
412 pb_log("%s: payload too big %u/%u\n", __func__, m.payload_len,
413 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
417 message = talloc_size(ctx, sizeof(m) + m.payload_len);
418 memcpy(message, &m, sizeof(m));
420 for (len = 0; len < m.payload_len;) {
421 rc = read(fd, message->payload + len, m.payload_len - len);
424 talloc_free(message);
425 pb_log("%s: failed (%u): %s\n", __func__, len,
437 int pb_protocol_deserialise_device(struct device *dev,
438 const struct pb_protocol_message *message)
444 len = message->payload_len;
445 pos = message->payload;
447 if (read_string(dev, &pos, &len, &dev->id))
450 if (len < sizeof(enum device_type))
452 dev->type = *(enum device_type *)(pos);
453 pos += sizeof(enum device_type);
454 len -= sizeof(enum device_type);
456 if (read_string(dev, &pos, &len, &dev->name))
459 if (read_string(dev, &pos, &len, &dev->description))
462 if (read_string(dev, &pos, &len, &dev->icon_file))
471 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
472 const struct pb_protocol_message *message)
478 len = message->payload_len;
479 pos = message->payload;
481 if (read_string(opt, &pos, &len, &opt->device_id))
484 if (read_string(opt, &pos, &len, &opt->id))
487 if (read_string(opt, &pos, &len, &opt->name))
490 if (read_string(opt, &pos, &len, &opt->description))
493 if (read_string(opt, &pos, &len, &opt->icon_file))
496 if (read_string(opt, &pos, &len, &opt->boot_image_file))
499 if (read_string(opt, &pos, &len, &opt->initrd_file))
502 if (read_string(opt, &pos, &len, &opt->dtb_file))
505 if (read_string(opt, &pos, &len, &opt->boot_args))
508 if (len < sizeof(bool))
510 opt->is_default = *(bool *)(pos);
518 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
519 const struct pb_protocol_message *message)
525 len = message->payload_len;
526 pos = message->payload;
528 if (read_string(cmd, &pos, &len, &cmd->option_id))
531 if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
534 if (read_string(cmd, &pos, &len, &cmd->initrd_file))
537 if (read_string(cmd, &pos, &len, &cmd->dtb_file))
540 if (read_string(cmd, &pos, &len, &cmd->boot_args))
549 int pb_protocol_deserialise_boot_status(struct boot_status *status,
550 const struct pb_protocol_message *message)
556 len = message->payload_len;
557 pos = message->payload;
559 /* first up, the type enum... */
560 if (len < sizeof(uint32_t))
563 status->type = __be32_to_cpu(*(uint32_t *)(pos));
565 switch (status->type) {
566 case BOOT_STATUS_ERROR:
567 case BOOT_STATUS_INFO:
573 pos += sizeof(uint32_t);
574 len -= sizeof(uint32_t);
576 /* message and detail strings */
577 if (read_string(status, &pos, &len, &status->message))
580 if (read_string(status, &pos, &len, &status->detail))
583 /* and finally, progress */
584 if (len < sizeof(uint32_t))
587 status->progress = __be32_to_cpu(*(uint32_t *)(pos));
590 if (status->progress > 100)
591 status->progress = 100;
599 int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
600 const struct pb_protocol_message *message)
606 len = message->payload_len;
607 pos = message->payload;
609 /* type and identifier strings */
610 if (read_string(sysinfo, &pos, &len, &sysinfo->type))
613 if (read_string(sysinfo, &pos, &len, &sysinfo->identifier))
616 /* number of interfaces */
617 if (read_u32(&pos, &len, &sysinfo->n_interfaces))
620 sysinfo->interfaces = talloc_array(sysinfo, struct interface_info *,
621 sysinfo->n_interfaces);
623 for (i = 0; i < sysinfo->n_interfaces; i++) {
624 struct interface_info *if_info = talloc(sysinfo,
625 struct interface_info);
627 if (read_u32(&pos, &len, &if_info->hwaddr_size))
630 if (len < if_info->hwaddr_size)
633 if_info->hwaddr = talloc_memdup(if_info, pos,
634 if_info->hwaddr_size);
635 pos += if_info->hwaddr_size;
636 len -= if_info->hwaddr_size;
638 if (read_string(if_info, &pos, &len, &if_info->name))
641 sysinfo->interfaces[i] = if_info;