6 #include <asm/byteorder.h>
8 #include <talloc/talloc.h>
12 #include "pb-protocol.h"
17 * 4-byte action, determines the remaining message content
18 * 4-byte total payload len
19 * - not including action and payload len header
21 * action = 0x1: device add message
25 * 4-byte len, description
26 * 4-byte len, icon_file
32 * 4-byte len, description
33 * 4-byte len, icon_file
34 * 4-byte len, boot_image_file
35 * 4-byte len, initrd_file
36 * 4-byte len, boot_args
38 * action = 0x2: device remove message
44 * 4-byte len, boot option id
45 * 4-byte len, boot_image_file
46 * 4-byte len, initrd_file
47 * 4-byte len, boot_args
51 void pb_protocol_dump_device(const struct device *dev, const char *text,
54 struct boot_option *opt;
56 fprintf(stream, "%snew dev:\n", text);
57 fprintf(stream, "%s\tid: %s\n", text, dev->id);
58 fprintf(stream, "%s\tname: %s\n", text, dev->name);
59 fprintf(stream, "%s\tdesc: %s\n", text, dev->description);
60 fprintf(stream, "%s\ticon: %s\n", text, dev->icon_file);
61 fprintf(stream, "%s\tboot options:\n", text);
62 list_for_each_entry(&dev->boot_options, opt, list) {
63 fprintf(stream, "%s\t\tid: %s\n", text, opt->id);
64 fprintf(stream, "%s\t\tname: %s\n", text, opt->name);
65 fprintf(stream, "%s\t\tdesc: %s\n", text, opt->description);
66 fprintf(stream, "%s\t\ticon: %s\n", text, opt->icon_file);
67 fprintf(stream, "%s\t\tboot: %s\n", text, opt->boot_image_file);
68 fprintf(stream, "%s\t\tinit: %s\n", text, opt->initrd_file);
69 fprintf(stream, "%s\t\targs: %s\n", text, opt->boot_args);
73 int pb_protocol_device_cmp(const struct device *a, const struct device *b)
75 return !strcmp(a->id, b->id);
78 int pb_protocol_boot_option_cmp(const struct boot_option *a,
79 const struct boot_option *b)
81 return !strcmp(a->id, b->id);
84 /* Write a string into the buffer, starting at pos.
86 * Returns the total length used for the write, including length header.
88 int pb_protocol_serialise_string(char *pos, const char *str)
95 *(uint32_t *)pos = __cpu_to_be32(len);
96 pos += sizeof(uint32_t);
98 memcpy(pos, str, len);
100 return len + sizeof(uint32_t);
103 /* Read a string from a buffer, allocating the new string as necessary.
105 * @param[in] ctx The talloc context to base the allocation on
106 * @param[in,out] pos Where to start reading
107 * @param[in,out] len The amount of data remaining in the buffer
108 * @param[out] str Pointer to resuling string
109 * @return zero on success, non-zero on failure
111 static int read_string(void *ctx, const char **pos, unsigned int *len,
114 uint32_t str_len, read_len;
116 if (*len < sizeof(uint32_t))
119 str_len = __be32_to_cpu(*(uint32_t *)(*pos));
120 read_len = sizeof(uint32_t);
122 if (read_len + str_len > *len)
128 *str = talloc_strndup(ctx, *pos + read_len, str_len);
132 /* all ok, update the caller's pointers */
139 char *pb_protocol_deserialise_string(void *ctx,
140 const struct pb_protocol_message *message)
146 len = message->payload_len;
147 buf = message->payload;
149 if (read_string(ctx, &buf, &len, &str))
155 static int optional_strlen(const char *str)
162 int pb_protocol_device_len(const struct device *dev)
164 return 4 + optional_strlen(dev->id) +
165 4 + optional_strlen(dev->name) +
166 4 + optional_strlen(dev->description) +
167 4 + optional_strlen(dev->icon_file);
170 int pb_protocol_boot_option_len(const struct boot_option *opt)
173 return 4 + optional_strlen(opt->device_id) +
174 4 + optional_strlen(opt->id) +
175 4 + optional_strlen(opt->name) +
176 4 + optional_strlen(opt->description) +
177 4 + optional_strlen(opt->icon_file) +
178 4 + optional_strlen(opt->boot_image_file) +
179 4 + optional_strlen(opt->initrd_file) +
180 4 + optional_strlen(opt->boot_args);
183 int pb_protocol_boot_len(const struct boot_command *boot)
185 return 4 + optional_strlen(boot->option_id) +
186 4 + optional_strlen(boot->boot_image_file) +
187 4 + optional_strlen(boot->initrd_file) +
188 4 + optional_strlen(boot->boot_args);
191 int pb_protocol_serialise_device(const struct device *dev,
192 char *buf, int buf_len)
196 pos += pb_protocol_serialise_string(pos, dev->id);
197 pos += pb_protocol_serialise_string(pos, dev->name);
198 pos += pb_protocol_serialise_string(pos, dev->description);
199 pos += pb_protocol_serialise_string(pos, dev->icon_file);
201 assert(pos <= buf + buf_len);
207 int pb_protocol_serialise_boot_option(const struct boot_option *opt,
208 char *buf, int buf_len)
212 pos += pb_protocol_serialise_string(pos, opt->device_id);
213 pos += pb_protocol_serialise_string(pos, opt->id);
214 pos += pb_protocol_serialise_string(pos, opt->name);
215 pos += pb_protocol_serialise_string(pos, opt->description);
216 pos += pb_protocol_serialise_string(pos, opt->icon_file);
217 pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
218 pos += pb_protocol_serialise_string(pos, opt->initrd_file);
219 pos += pb_protocol_serialise_string(pos, opt->boot_args);
221 assert(pos <= buf + buf_len);
227 int pb_protocol_serialise_boot_command(const struct boot_command *boot,
228 char *buf, int buf_len)
232 pos += pb_protocol_serialise_string(pos, boot->option_id);
233 pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
234 pos += pb_protocol_serialise_string(pos, boot->initrd_file);
235 pos += pb_protocol_serialise_string(pos, boot->boot_args);
237 assert(pos <= buf + buf_len);
243 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
248 total_len = sizeof(*message) + message->payload_len;
250 message->payload_len = __cpu_to_be32(message->payload_len);
251 message->action = __cpu_to_be32(message->action);
253 for (pos = (void *)message; total_len;) {
254 rc = write(fd, pos, total_len);
263 talloc_free(message);
268 pb_log("%s: failed: %s\n", __func__, strerror(errno));
272 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
273 enum pb_protocol_action action, int payload_len)
275 struct pb_protocol_message *message;
277 if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
278 pb_log("%s: payload too big %u/%u\n", __func__, payload_len,
279 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
283 message = talloc_size(ctx, sizeof(*message) + payload_len);
285 /* we convert these to big-endian in write_message() */
286 message->action = action;
287 message->payload_len = payload_len;
293 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
295 struct pb_protocol_message *message, m;
299 /* use the stack for the initial 8-byte read */
301 rc = read(fd, &m, sizeof(m));
305 m.payload_len = __be32_to_cpu(m.payload_len);
306 m.action = __be32_to_cpu(m.action);
308 if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
309 pb_log("%s: payload too big %u/%u\n", __func__, m.payload_len,
310 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
314 message = talloc_size(ctx, sizeof(m) + m.payload_len);
315 memcpy(message, &m, sizeof(m));
317 for (len = 0; len < m.payload_len;) {
318 rc = read(fd, message->payload + len, m.payload_len - len);
321 talloc_free(message);
322 pb_log("%s: failed (%u): %s\n", __func__, len,
334 int pb_protocol_deserialise_device(struct device *dev,
335 const struct pb_protocol_message *message)
341 len = message->payload_len;
342 pos = message->payload;
344 if (read_string(dev, &pos, &len, &dev->id))
347 if (read_string(dev, &pos, &len, &dev->name))
350 if (read_string(dev, &pos, &len, &dev->description))
353 if (read_string(dev, &pos, &len, &dev->icon_file))
362 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
363 const struct pb_protocol_message *message)
369 len = message->payload_len;
370 pos = message->payload;
372 if (read_string(opt, &pos, &len, &opt->device_id))
375 if (read_string(opt, &pos, &len, &opt->id))
378 if (read_string(opt, &pos, &len, &opt->name))
381 if (read_string(opt, &pos, &len, &opt->description))
384 if (read_string(opt, &pos, &len, &opt->icon_file))
387 if (read_string(opt, &pos, &len, &opt->boot_image_file))
390 if (read_string(opt, &pos, &len, &opt->initrd_file))
393 if (read_string(opt, &pos, &len, &opt->boot_args))
402 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
403 const struct pb_protocol_message *message)
409 len = message->payload_len;
410 pos = message->payload;
412 if (read_string(cmd, &pos, &len, &cmd->option_id))
415 if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
418 if (read_string(cmd, &pos, &len, &cmd->initrd_file))
421 if (read_string(cmd, &pos, &len, &cmd->boot_args))