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
43 int pb_protocol_device_cmp(const struct device *a, const struct device *b)
45 return !strcmp(a->id, b->id);
48 int pb_protocol_boot_option_cmp(const struct boot_option *a,
49 const struct boot_option *b)
51 return !strcmp(a->id, b->id);
54 /* Write a string into the buffer, starting at pos.
56 * Returns the total length used for the write, including length header.
58 int pb_protocol_serialise_string(char *pos, const char *str)
65 *(uint32_t *)pos = __cpu_to_be32(len);
66 pos += sizeof(uint32_t);
68 memcpy(pos, str, len);
70 return len + sizeof(uint32_t);
73 /* Read a string from a buffer, allocating the new string as necessary.
75 * @param[in] ctx The talloc context to base the allocation on
76 * @param[in,out] pos Where to start reading
77 * @param[in,out] len The amount of data remaining in the buffer
78 * @param[out] str Pointer to resuling string
79 * @return zero on success, non-zero on failure
81 static int read_string(void *ctx, const char **pos, unsigned int *len,
84 uint32_t str_len, read_len;
86 if (*len < sizeof(uint32_t))
89 str_len = __be32_to_cpu(*(uint32_t *)(*pos));
90 read_len = sizeof(uint32_t);
92 if (read_len + str_len > *len)
98 *str = talloc_strndup(ctx, *pos + read_len, str_len);
102 /* all ok, update the caller's pointers */
109 char *pb_protocol_deserialise_string(void *ctx,
110 const struct pb_protocol_message *message)
116 len = message->payload_len;
117 buf = message->payload;
119 if (read_string(ctx, &buf, &len, &str))
125 static int optional_strlen(const char *str)
132 int pb_protocol_device_len(const struct device *dev)
134 struct boot_option *opt;
137 len = 4 + optional_strlen(dev->id) +
138 4 + optional_strlen(dev->name) +
139 4 + optional_strlen(dev->description) +
140 4 + optional_strlen(dev->icon_file) +
143 list_for_each_entry(&dev->boot_options, opt, list) {
144 len += 4 + optional_strlen(opt->id) +
145 4 + optional_strlen(opt->name) +
146 4 + optional_strlen(opt->description) +
147 4 + optional_strlen(opt->icon_file) +
148 4 + optional_strlen(opt->boot_image_file) +
149 4 + optional_strlen(opt->initrd_file) +
150 4 + optional_strlen(opt->boot_args);
156 int pb_protocol_serialise_device(const struct device *dev, char *buf, int buf_len)
158 struct boot_option *opt;
164 /* construct payload into buffer */
165 pos += pb_protocol_serialise_string(pos, dev->id);
166 pos += pb_protocol_serialise_string(pos, dev->name);
167 pos += pb_protocol_serialise_string(pos, dev->description);
168 pos += pb_protocol_serialise_string(pos, dev->icon_file);
170 /* write option count */
173 list_for_each_entry(&dev->boot_options, opt, list)
176 *(uint32_t *)pos = __cpu_to_be32(n);
177 pos += sizeof(uint32_t);
179 /* write each option */
180 list_for_each_entry(&dev->boot_options, opt, list) {
181 pos += pb_protocol_serialise_string(pos, opt->id);
182 pos += pb_protocol_serialise_string(pos, opt->name);
183 pos += pb_protocol_serialise_string(pos, opt->description);
184 pos += pb_protocol_serialise_string(pos, opt->icon_file);
185 pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
186 pos += pb_protocol_serialise_string(pos, opt->initrd_file);
187 pos += pb_protocol_serialise_string(pos, opt->boot_args);
190 assert(pos <= buf + buf_len);
195 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
200 total_len = sizeof(*message) + message->payload_len;
202 message->payload_len = __cpu_to_be32(message->payload_len);
203 message->action = __cpu_to_be32(message->action);
205 for (pos = (void *)message; total_len;) {
206 rc = write(fd, pos, total_len);
215 talloc_free(message);
220 pb_log("%s: failed: %s\n", __func__, strerror(errno));
224 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
225 enum pb_protocol_action action, int payload_len)
227 struct pb_protocol_message *message;
229 if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
230 pb_log("%s: payload too big %u/%u\n", __func__, payload_len,
231 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) {
261 pb_log("%s: payload too big %u/%u\n", __func__, m.payload_len,
262 PB_PROTOCOL_MAX_PAYLOAD_SIZE);
266 message = talloc_size(ctx, sizeof(m) + m.payload_len);
267 memcpy(message, &m, sizeof(m));
269 for (len = 0; len < m.payload_len;) {
270 rc = read(fd, message->payload + len, m.payload_len - len);
273 talloc_free(message);
274 pb_log("%s: failed (%u): %s\n", __func__, len,
286 struct device *pb_protocol_deserialise_device(void *ctx,
287 const struct pb_protocol_message *message)
294 len = message->payload_len;
295 pos = message->payload;
297 dev = talloc(ctx, struct device);
299 if (read_string(dev, &pos, &len, &dev->id))
302 if (read_string(dev, &pos, &len, &dev->name))
305 if (read_string(dev, &pos, &len, &dev->description))
308 if (read_string(dev, &pos, &len, &dev->icon_file))
311 n_options = __be32_to_cpu(*(uint32_t *)pos);
312 pos += sizeof(uint32_t);
314 list_init(&dev->boot_options);
316 for (i = 0; i < n_options; i++) {
317 struct boot_option *opt;
319 opt = talloc(dev, struct boot_option);
321 if (read_string(opt, &pos, &len, &opt->id))
323 if (read_string(opt, &pos, &len, &opt->name))
325 if (read_string(opt, &pos, &len,
328 if (read_string(opt, &pos, &len,
331 if (read_string(opt, &pos, &len,
332 &opt->boot_image_file))
334 if (read_string(opt, &pos, &len,
337 if (read_string(opt, &pos, &len,
341 list_add(&dev->boot_options, &opt->list);