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 char *pb_protocol_deserialise_string(void *ctx,
145 const struct pb_protocol_message *message)
151 len = message->payload_len;
152 buf = message->payload;
154 if (read_string(ctx, &buf, &len, &str))
160 static int optional_strlen(const char *str)
167 int pb_protocol_device_len(const struct device *dev)
169 return 4 + optional_strlen(dev->id) +
171 4 + optional_strlen(dev->name) +
172 4 + optional_strlen(dev->description) +
173 4 + optional_strlen(dev->icon_file);
176 int pb_protocol_boot_option_len(const struct boot_option *opt)
179 return 4 + optional_strlen(opt->device_id) +
180 4 + optional_strlen(opt->id) +
181 4 + optional_strlen(opt->name) +
182 4 + optional_strlen(opt->description) +
183 4 + optional_strlen(opt->icon_file) +
184 4 + optional_strlen(opt->boot_image_file) +
185 4 + optional_strlen(opt->initrd_file) +
186 4 + optional_strlen(opt->dtb_file) +
187 4 + optional_strlen(opt->boot_args) +
188 sizeof(opt->is_default);
191 int pb_protocol_boot_len(const struct boot_command *boot)
193 return 4 + optional_strlen(boot->option_id) +
194 4 + optional_strlen(boot->boot_image_file) +
195 4 + optional_strlen(boot->initrd_file) +
196 4 + optional_strlen(boot->dtb_file) +
197 4 + optional_strlen(boot->boot_args);
200 int pb_protocol_boot_status_len(const struct boot_status *status)
203 4 + optional_strlen(status->message) +
204 4 + optional_strlen(status->detail) +
208 int pb_protocol_serialise_device(const struct device *dev,
209 char *buf, int buf_len)
213 pos += pb_protocol_serialise_string(pos, dev->id);
214 *(enum device_type *)pos = dev->type;
215 pos += sizeof(enum device_type);
216 pos += pb_protocol_serialise_string(pos, dev->name);
217 pos += pb_protocol_serialise_string(pos, dev->description);
218 pos += pb_protocol_serialise_string(pos, dev->icon_file);
220 assert(pos <= buf + buf_len);
226 int pb_protocol_serialise_boot_option(const struct boot_option *opt,
227 char *buf, int buf_len)
231 pos += pb_protocol_serialise_string(pos, opt->device_id);
232 pos += pb_protocol_serialise_string(pos, opt->id);
233 pos += pb_protocol_serialise_string(pos, opt->name);
234 pos += pb_protocol_serialise_string(pos, opt->description);
235 pos += pb_protocol_serialise_string(pos, opt->icon_file);
236 pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
237 pos += pb_protocol_serialise_string(pos, opt->initrd_file);
238 pos += pb_protocol_serialise_string(pos, opt->dtb_file);
239 pos += pb_protocol_serialise_string(pos, opt->boot_args);
241 *(bool *)pos = opt->is_default;
244 assert(pos <= buf + buf_len);
250 int pb_protocol_serialise_boot_command(const struct boot_command *boot,
251 char *buf, int buf_len)
255 pos += pb_protocol_serialise_string(pos, boot->option_id);
256 pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
257 pos += pb_protocol_serialise_string(pos, boot->initrd_file);
258 pos += pb_protocol_serialise_string(pos, boot->dtb_file);
259 pos += pb_protocol_serialise_string(pos, boot->boot_args);
261 assert(pos <= buf + buf_len);
267 int pb_protocol_serialise_boot_status(const struct boot_status *status,
268 char *buf, int buf_len)
272 *(uint32_t *)pos = __cpu_to_be32(status->type);
273 pos += sizeof(uint32_t);
275 pos += pb_protocol_serialise_string(pos, status->message);
276 pos += pb_protocol_serialise_string(pos, status->detail);
278 *(uint32_t *)pos = __cpu_to_be32(status->type);
279 pos += sizeof(uint32_t);
281 assert(pos <= buf + buf_len);
287 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
292 total_len = sizeof(*message) + message->payload_len;
294 message->payload_len = __cpu_to_be32(message->payload_len);
295 message->action = __cpu_to_be32(message->action);
297 for (pos = (void *)message; total_len;) {
298 rc = write(fd, pos, total_len);
307 talloc_free(message);
312 pb_log("%s: failed: %s\n", __func__, strerror(errno));
316 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
317 enum pb_protocol_action action, int payload_len)
319 struct pb_protocol_message *message;
321 if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
322 pb_log("%s: payload too big %u/%u\n", __func__, payload_len,
323 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
327 message = talloc_size(ctx, sizeof(*message) + payload_len);
329 /* we convert these to big-endian in write_message() */
330 message->action = action;
331 message->payload_len = payload_len;
337 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
339 struct pb_protocol_message *message, m;
343 /* use the stack for the initial 8-byte read */
345 rc = read(fd, &m, sizeof(m));
349 m.payload_len = __be32_to_cpu(m.payload_len);
350 m.action = __be32_to_cpu(m.action);
352 if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
353 pb_log("%s: payload too big %u/%u\n", __func__, m.payload_len,
354 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
358 message = talloc_size(ctx, sizeof(m) + m.payload_len);
359 memcpy(message, &m, sizeof(m));
361 for (len = 0; len < m.payload_len;) {
362 rc = read(fd, message->payload + len, m.payload_len - len);
365 talloc_free(message);
366 pb_log("%s: failed (%u): %s\n", __func__, len,
378 int pb_protocol_deserialise_device(struct device *dev,
379 const struct pb_protocol_message *message)
385 len = message->payload_len;
386 pos = message->payload;
388 if (read_string(dev, &pos, &len, &dev->id))
391 if (len < sizeof(enum device_type))
393 dev->type = *(enum device_type *)(pos);
394 pos += sizeof(enum device_type);
395 len -= sizeof(enum device_type);
397 if (read_string(dev, &pos, &len, &dev->name))
400 if (read_string(dev, &pos, &len, &dev->description))
403 if (read_string(dev, &pos, &len, &dev->icon_file))
412 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
413 const struct pb_protocol_message *message)
419 len = message->payload_len;
420 pos = message->payload;
422 if (read_string(opt, &pos, &len, &opt->device_id))
425 if (read_string(opt, &pos, &len, &opt->id))
428 if (read_string(opt, &pos, &len, &opt->name))
431 if (read_string(opt, &pos, &len, &opt->description))
434 if (read_string(opt, &pos, &len, &opt->icon_file))
437 if (read_string(opt, &pos, &len, &opt->boot_image_file))
440 if (read_string(opt, &pos, &len, &opt->initrd_file))
443 if (read_string(opt, &pos, &len, &opt->dtb_file))
446 if (read_string(opt, &pos, &len, &opt->boot_args))
449 if (len < sizeof(bool))
451 opt->is_default = *(bool *)(pos);
459 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
460 const struct pb_protocol_message *message)
466 len = message->payload_len;
467 pos = message->payload;
469 if (read_string(cmd, &pos, &len, &cmd->option_id))
472 if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
475 if (read_string(cmd, &pos, &len, &cmd->initrd_file))
478 if (read_string(cmd, &pos, &len, &cmd->dtb_file))
481 if (read_string(cmd, &pos, &len, &cmd->boot_args))
490 int pb_protocol_deserialise_boot_status(struct boot_status *status,
491 const struct pb_protocol_message *message)
497 len = message->payload_len;
498 pos = message->payload;
500 /* first up, the type enum... */
501 if (len < sizeof(uint32_t))
504 status->type = __be32_to_cpu(*(uint32_t *)(pos));
506 switch (status->type) {
507 case BOOT_STATUS_ERROR:
508 case BOOT_STATUS_INFO:
514 pos += sizeof(uint32_t);
515 len -= sizeof(uint32_t);
517 /* message and detail strings */
518 if (read_string(status, &pos, &len, &status->message))
521 if (read_string(status, &pos, &len, &status->detail))
524 /* and finally, progress */
525 if (len < sizeof(uint32_t))
528 status->progress = __be32_to_cpu(*(uint32_t *)(pos));
531 if (status->progress > 100)
532 status->progress = 100;