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, const char **pos, unsigned int *len,
91 uint32_t str_len, read_len;
93 if (*len < sizeof(uint32_t))
96 str_len = __be32_to_cpu(*(uint32_t *)(*pos));
97 read_len = sizeof(uint32_t);
99 if (read_len + str_len > *len)
105 *str = talloc_strndup(ctx, *pos + read_len, str_len);
109 /* all ok, update the caller's pointers */
116 char *pb_protocol_deserialise_string(void *ctx,
117 const struct pb_protocol_message *message)
123 len = message->payload_len;
124 buf = message->payload;
126 if (read_string(ctx, &buf, &len, &str))
132 static int optional_strlen(const char *str)
139 int pb_protocol_device_len(const struct device *dev)
141 struct boot_option *opt;
144 len = 4 + optional_strlen(dev->id) +
145 4 + optional_strlen(dev->name) +
146 4 + optional_strlen(dev->description) +
147 4 + optional_strlen(dev->icon_file) +
150 list_for_each_entry(&dev->boot_options, opt, list) {
151 len += 4 + optional_strlen(opt->id) +
152 4 + optional_strlen(opt->name) +
153 4 + optional_strlen(opt->description) +
154 4 + optional_strlen(opt->icon_file) +
155 4 + optional_strlen(opt->boot_image_file) +
156 4 + optional_strlen(opt->initrd_file) +
157 4 + optional_strlen(opt->boot_args);
163 int pb_protocol_serialise_device(const struct device *dev, char *buf, int buf_len)
165 struct boot_option *opt;
171 /* construct payload into buffer */
172 pos += pb_protocol_serialise_string(pos, dev->id);
173 pos += pb_protocol_serialise_string(pos, dev->name);
174 pos += pb_protocol_serialise_string(pos, dev->description);
175 pos += pb_protocol_serialise_string(pos, dev->icon_file);
177 /* write option count */
180 list_for_each_entry(&dev->boot_options, opt, list)
183 *(uint32_t *)pos = __cpu_to_be32(n);
184 pos += sizeof(uint32_t);
186 /* write each option */
187 list_for_each_entry(&dev->boot_options, opt, list) {
188 pos += pb_protocol_serialise_string(pos, opt->id);
189 pos += pb_protocol_serialise_string(pos, opt->name);
190 pos += pb_protocol_serialise_string(pos, opt->description);
191 pos += pb_protocol_serialise_string(pos, opt->icon_file);
192 pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
193 pos += pb_protocol_serialise_string(pos, opt->initrd_file);
194 pos += pb_protocol_serialise_string(pos, opt->boot_args);
197 assert(pos <= buf + buf_len);
202 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
207 total_len = sizeof(*message) + message->payload_len;
209 message->payload_len = __cpu_to_be32(message->payload_len);
210 message->action = __cpu_to_be32(message->action);
212 for (pos = (void *)message; total_len;) {
213 rc = write(fd, pos, total_len);
222 talloc_free(message);
224 return total_len ? -1 : 0;
227 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
228 enum pb_protocol_action action, int payload_len)
230 struct pb_protocol_message *message;
232 if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE)
235 message = talloc_size(ctx, sizeof(*message) + payload_len);
237 /* we convert these to big-endian in write_message() */
238 message->action = action;
239 message->payload_len = payload_len;
245 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
247 struct pb_protocol_message *message, m;
251 /* use the stack for the initial 8-byte read */
253 rc = read(fd, &m, sizeof(m));
257 m.payload_len = __be32_to_cpu(m.payload_len);
258 m.action = __be32_to_cpu(m.action);
260 if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE)
263 message = talloc_size(ctx, sizeof(m) + m.payload_len);
264 memcpy(message, &m, sizeof(m));
266 for (len = 0; len < m.payload_len;) {
267 rc = read(fd, message->payload + len, m.payload_len - len);
270 talloc_free(message);
281 struct device *pb_protocol_deserialise_device(void *ctx,
282 const struct pb_protocol_message *message)
289 len = message->payload_len;
290 pos = message->payload;
292 dev = talloc(ctx, struct device);
294 if (read_string(dev, &pos, &len, &dev->id))
297 if (read_string(dev, &pos, &len, &dev->name))
300 if (read_string(dev, &pos, &len, &dev->description))
303 if (read_string(dev, &pos, &len, &dev->icon_file))
306 n_options = __be32_to_cpu(*(uint32_t *)pos);
307 pos += sizeof(uint32_t);
309 list_init(&dev->boot_options);
311 for (i = 0; i < n_options; i++) {
312 struct boot_option *opt;
314 opt = talloc(dev, struct boot_option);
316 if (read_string(opt, &pos, &len, &opt->id))
318 if (read_string(opt, &pos, &len, &opt->name))
320 if (read_string(opt, &pos, &len,
323 if (read_string(opt, &pos, &len,
326 if (read_string(opt, &pos, &len,
327 &opt->boot_image_file))
329 if (read_string(opt, &pos, &len,
332 if (read_string(opt, &pos, &len,
336 list_add(&dev->boot_options, &opt->list);