5 #include <asm/byteorder.h>
7 #include <talloc/talloc.h>
10 #include "pb-protocol.h"
15 * 4-byte action, determines the remaining message content
16 * 4-byte total payload len
17 * - not including action and payload len header
19 * action = 0x1: device add message
23 * 4-byte len, description
24 * 4-byte len, icon_file
30 * 4-byte len, description
31 * 4-byte len, icon_file
32 * 4-byte len, boot_image_file
33 * 4-byte len, initrd_file
34 * 4-byte len, boot_args
36 * action = 0x2: device remove message
42 /* Write a string into the buffer, starting at pos.
44 * Returns the total length used for the write, including length header.
46 int pb_protocol_serialise_string(char *pos, const char *str)
53 *(uint32_t *)pos = __cpu_to_be32(len);
54 pos += sizeof(uint32_t);
56 memcpy(pos, str, len);
58 return len + sizeof(uint32_t);
61 /* Read a string from a buffer, allocating the new string as necessary.
63 * @param[in] ctx The talloc context to base the allocation on
64 * @param[in,out] pos Where to start reading
65 * @param[in,out] len The amount of data remaining in the buffer
66 * @param[out] str Pointer to resuling string
67 * @return zero on success, non-zero on failure
69 static int read_string(void *ctx, char **pos, unsigned int *len, char **str)
71 uint32_t str_len, read_len;
73 if (*len < sizeof(uint32_t))
76 str_len = __be32_to_cpu(*(uint32_t *)(*pos));
77 read_len = sizeof(uint32_t);
79 if (read_len + str_len > *len)
85 *str = talloc_strndup(ctx, *pos + read_len, str_len);
89 /* all ok, update the caller's pointers */
96 char *pb_protocol_deserialise_string(void *ctx,
97 struct pb_protocol_message *message)
102 len = message->payload_len;
103 buf = message->payload;
105 if (read_string(ctx, &buf, &len, &str))
111 static int optional_strlen(const char *str)
118 int pb_protocol_device_len(struct device *dev)
120 struct boot_option *opt;
123 len = 4 + optional_strlen(dev->id) +
124 4 + optional_strlen(dev->name) +
125 4 + optional_strlen(dev->description) +
126 4 + optional_strlen(dev->icon_file) +
129 list_for_each_entry(&dev->boot_options, opt, list) {
130 len += 4 + optional_strlen(opt->id) +
131 4 + optional_strlen(opt->name) +
132 4 + optional_strlen(opt->description) +
133 4 + optional_strlen(opt->icon_file) +
134 4 + optional_strlen(opt->boot_image_file) +
135 4 + optional_strlen(opt->initrd_file) +
136 4 + optional_strlen(opt->boot_args);
142 int pb_protocol_serialise_device(struct device *dev, char *buf, int buf_len)
144 struct boot_option *opt;
150 /* construct payload into buffer */
151 pos += pb_protocol_serialise_string(pos, dev->id);
152 pos += pb_protocol_serialise_string(pos, dev->name);
153 pos += pb_protocol_serialise_string(pos, dev->description);
154 pos += pb_protocol_serialise_string(pos, dev->icon_file);
156 /* write option count */
159 list_for_each_entry(&dev->boot_options, opt, list)
162 *(uint32_t *)pos = __cpu_to_be32(n);
163 pos += sizeof(uint32_t);
165 /* write each option */
166 list_for_each_entry(&dev->boot_options, opt, list) {
167 pos += pb_protocol_serialise_string(pos, opt->id);
168 pos += pb_protocol_serialise_string(pos, opt->name);
169 pos += pb_protocol_serialise_string(pos, opt->description);
170 pos += pb_protocol_serialise_string(pos, opt->icon_file);
171 pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
172 pos += pb_protocol_serialise_string(pos, opt->initrd_file);
173 pos += pb_protocol_serialise_string(pos, opt->boot_args);
176 assert(pos <= buf + buf_len);
181 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
186 total_len = sizeof(*message) + message->payload_len;
188 message->payload_len = __cpu_to_be32(message->payload_len);
189 message->action = __cpu_to_be32(message->action);
191 for (pos = (void *)message; total_len;) {
192 rc = write(fd, pos, total_len);
201 talloc_free(message);
203 return total_len ? -1 : 0;
206 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
207 enum pb_protocol_action action, int payload_len)
209 struct pb_protocol_message *message;
211 if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE)
214 message = talloc_size(ctx, sizeof(*message) + payload_len);
216 /* we convert these to big-endian in write_message() */
217 message->action = action;
218 message->payload_len = payload_len;
224 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
226 struct pb_protocol_message *message, m;
230 /* use the stack for the initial 8-byte read */
232 rc = read(fd, &m, sizeof(m));
236 m.payload_len = __be32_to_cpu(m.payload_len);
237 m.action = __be32_to_cpu(m.action);
239 if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE)
242 message = talloc_size(ctx, sizeof(m) + m.payload_len);
243 memcpy(message, &m, sizeof(m));
245 for (len = 0; len < m.payload_len;) {
246 rc = read(fd, message->payload + len, m.payload_len - len);
249 talloc_free(message);
260 struct device *pb_protocol_deserialise_device(void *ctx,
261 struct pb_protocol_message *message)
268 len = message->payload_len;
269 pos = message->payload;
271 dev = talloc(ctx, struct device);
273 if (read_string(dev, &pos, &len, &dev->id))
276 if (read_string(dev, &pos, &len, &dev->name))
279 if (read_string(dev, &pos, &len, &dev->description))
282 if (read_string(dev, &pos, &len, &dev->icon_file))
285 n_options = __be32_to_cpu(*(uint32_t *)pos);
286 pos += sizeof(uint32_t);
288 list_init(&dev->boot_options);
290 for (i = 0; i < n_options; i++) {
291 struct boot_option *opt;
293 opt = talloc(dev, struct boot_option);
295 if (read_string(opt, &pos, &len, &opt->id))
297 if (read_string(opt, &pos, &len, &opt->name))
299 if (read_string(opt, &pos, &len,
302 if (read_string(opt, &pos, &len,
305 if (read_string(opt, &pos, &len,
306 &opt->boot_image_file))
308 if (read_string(opt, &pos, &len,
311 if (read_string(opt, &pos, &len,
315 list_add(&dev->boot_options, &opt->list);