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
41 /* Deep copy a boot_option.
43 struct boot_option *boot_option_copy(void* ctx, const struct boot_option *opt)
45 struct boot_option *new = talloc(ctx, struct boot_option);
48 new->id = talloc_strdup(new, opt->id);
49 new->name = talloc_strdup(new, opt->name);
50 new->description = talloc_strdup(new, opt->description);
51 new->icon_file = talloc_strdup(new, opt->icon_file);
52 new->boot_image_file = talloc_strdup(new, opt->boot_image_file);
53 new->initrd_file = talloc_strdup(new, opt->initrd_file);
54 new->boot_args = talloc_strdup(new, opt->boot_args);
55 memset(&new->list, 0, sizeof(new->list));
61 /* Write a string into the buffer, starting at pos.
63 * Returns the total length used for the write, including length header.
65 int pb_protocol_serialise_string(char *pos, const char *str)
72 *(uint32_t *)pos = __cpu_to_be32(len);
73 pos += sizeof(uint32_t);
75 memcpy(pos, str, len);
77 return len + sizeof(uint32_t);
80 /* Read a string from a buffer, allocating the new string as necessary.
82 * @param[in] ctx The talloc context to base the allocation on
83 * @param[in,out] pos Where to start reading
84 * @param[in,out] len The amount of data remaining in the buffer
85 * @param[out] str Pointer to resuling string
86 * @return zero on success, non-zero on failure
88 static int read_string(void *ctx, char **pos, unsigned int *len, char **str)
90 uint32_t str_len, read_len;
92 if (*len < sizeof(uint32_t))
95 str_len = __be32_to_cpu(*(uint32_t *)(*pos));
96 read_len = sizeof(uint32_t);
98 if (read_len + str_len > *len)
104 *str = talloc_strndup(ctx, *pos + read_len, str_len);
108 /* all ok, update the caller's pointers */
115 char *pb_protocol_deserialise_string(void *ctx,
116 struct pb_protocol_message *message)
121 len = message->payload_len;
122 buf = message->payload;
124 if (read_string(ctx, &buf, &len, &str))
130 static int optional_strlen(const char *str)
137 int pb_protocol_device_len(struct device *dev)
139 struct boot_option *opt;
142 len = 4 + optional_strlen(dev->id) +
143 4 + optional_strlen(dev->name) +
144 4 + optional_strlen(dev->description) +
145 4 + optional_strlen(dev->icon_file) +
148 list_for_each_entry(&dev->boot_options, opt, list) {
149 len += 4 + optional_strlen(opt->id) +
150 4 + optional_strlen(opt->name) +
151 4 + optional_strlen(opt->description) +
152 4 + optional_strlen(opt->icon_file) +
153 4 + optional_strlen(opt->boot_image_file) +
154 4 + optional_strlen(opt->initrd_file) +
155 4 + optional_strlen(opt->boot_args);
161 int pb_protocol_serialise_device(struct device *dev, char *buf, int buf_len)
163 struct boot_option *opt;
169 /* construct payload into buffer */
170 pos += pb_protocol_serialise_string(pos, dev->id);
171 pos += pb_protocol_serialise_string(pos, dev->name);
172 pos += pb_protocol_serialise_string(pos, dev->description);
173 pos += pb_protocol_serialise_string(pos, dev->icon_file);
175 /* write option count */
178 list_for_each_entry(&dev->boot_options, opt, list)
181 *(uint32_t *)pos = __cpu_to_be32(n);
182 pos += sizeof(uint32_t);
184 /* write each option */
185 list_for_each_entry(&dev->boot_options, opt, list) {
186 pos += pb_protocol_serialise_string(pos, opt->id);
187 pos += pb_protocol_serialise_string(pos, opt->name);
188 pos += pb_protocol_serialise_string(pos, opt->description);
189 pos += pb_protocol_serialise_string(pos, opt->icon_file);
190 pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
191 pos += pb_protocol_serialise_string(pos, opt->initrd_file);
192 pos += pb_protocol_serialise_string(pos, opt->boot_args);
195 assert(pos <= buf + buf_len);
200 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
205 total_len = sizeof(*message) + message->payload_len;
207 message->payload_len = __cpu_to_be32(message->payload_len);
208 message->action = __cpu_to_be32(message->action);
210 for (pos = (void *)message; total_len;) {
211 rc = write(fd, pos, total_len);
220 talloc_free(message);
222 return total_len ? -1 : 0;
225 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
226 enum pb_protocol_action action, int payload_len)
228 struct pb_protocol_message *message;
230 if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE)
233 message = talloc_size(ctx, sizeof(*message) + payload_len);
235 /* we convert these to big-endian in write_message() */
236 message->action = action;
237 message->payload_len = payload_len;
243 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
245 struct pb_protocol_message *message, m;
249 /* use the stack for the initial 8-byte read */
251 rc = read(fd, &m, sizeof(m));
255 m.payload_len = __be32_to_cpu(m.payload_len);
256 m.action = __be32_to_cpu(m.action);
258 if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE)
261 message = talloc_size(ctx, sizeof(m) + m.payload_len);
262 memcpy(message, &m, sizeof(m));
264 for (len = 0; len < m.payload_len;) {
265 rc = read(fd, message->payload + len, m.payload_len - len);
268 talloc_free(message);
279 struct device *pb_protocol_deserialise_device(void *ctx,
280 struct pb_protocol_message *message)
287 len = message->payload_len;
288 pos = message->payload;
290 dev = talloc(ctx, struct device);
292 if (read_string(dev, &pos, &len, &dev->id))
295 if (read_string(dev, &pos, &len, &dev->name))
298 if (read_string(dev, &pos, &len, &dev->description))
301 if (read_string(dev, &pos, &len, &dev->icon_file))
304 n_options = __be32_to_cpu(*(uint32_t *)pos);
305 pos += sizeof(uint32_t);
307 list_init(&dev->boot_options);
309 for (i = 0; i < n_options; i++) {
310 struct boot_option *opt;
312 opt = talloc(dev, struct boot_option);
314 if (read_string(opt, &pos, &len, &opt->id))
316 if (read_string(opt, &pos, &len, &opt->name))
318 if (read_string(opt, &pos, &len,
321 if (read_string(opt, &pos, &len,
324 if (read_string(opt, &pos, &len,
325 &opt->boot_image_file))
327 if (read_string(opt, &pos, &len,
330 if (read_string(opt, &pos, &len,
334 list_add(&dev->boot_options, &opt->list);