4 #include <asm/byteorder.h>
6 #include <talloc/talloc.h>
9 #include "pb-protocol.h"
14 * 4-byte action, determines the remaining message content
15 * 4-byte total payload len
16 * - not including action and payload len header
18 * action = 0x1: device add message
22 * 4-byte len, description
23 * 4-byte len, icon_file
29 * 4-byte len, description
30 * 4-byte len, icon_file
31 * 4-byte len, boot_image_file
32 * 4-byte len, initrd_file
33 * 4-byte len, boot_args
35 * action = 0x2: device remove message
41 /* Write a string into the buffer, starting at pos.
43 * Returns the total length used for the write, including length header.
45 int pb_protocol_serialise_string(char *pos, const char *str)
52 *(uint32_t *)pos = __cpu_to_be32(len);
53 pos += sizeof(uint32_t);
55 memcpy(pos, str, len);
57 return len + sizeof(uint32_t);
60 /* Read a string from a buffer, allocating the new string as necessary.
62 * @param[in] ctx The talloc context to base the allocation on
63 * @param[in,out] pos Where to start reading
64 * @param[in,out] len The amount of data remaining in the buffer
65 * @param[out] str Pointer to resuling string
66 * @return zero on success, non-zero on failure
68 static int read_string(void *ctx, char **pos, int *len, char **str)
70 uint32_t str_len, read_len;
72 if (*len < sizeof(uint32_t))
75 str_len = __be32_to_cpu(*(uint32_t *)(*pos));
76 read_len = sizeof(uint32_t);
78 if (read_len + str_len > *len)
84 *str = talloc_strndup(ctx, *pos + read_len, str_len);
88 /* all ok, update the caller's pointers */
95 char *pb_protocol_deserialise_string(void *ctx,
96 struct pb_protocol_message *message)
101 len = message->payload_len;
102 buf = message->payload;
104 if (read_string(ctx, &buf, &len, &str))
110 static int optional_strlen(const char *str)
117 int pb_protocol_device_len(struct device *dev)
119 struct boot_option *opt;
122 len = 4 + optional_strlen(dev->id) +
123 4 + optional_strlen(dev->name) +
124 4 + optional_strlen(dev->description) +
125 4 + optional_strlen(dev->icon_file) +
128 list_for_each_entry(&dev->boot_options, opt, list) {
129 len += 4 + optional_strlen(opt->id) +
130 4 + optional_strlen(opt->name) +
131 4 + optional_strlen(opt->description) +
132 4 + optional_strlen(opt->icon_file) +
133 4 + optional_strlen(opt->boot_image_file) +
134 4 + optional_strlen(opt->initrd_file) +
135 4 + optional_strlen(opt->boot_args);
141 int pb_protocol_serialise_device(struct device *dev, char *buf, int buf_len)
143 struct boot_option *opt;
149 /* construct payload into buffer */
150 pos += pb_protocol_serialise_string(pos, dev->id);
151 pos += pb_protocol_serialise_string(pos, dev->name);
152 pos += pb_protocol_serialise_string(pos, dev->description);
153 pos += pb_protocol_serialise_string(pos, dev->icon_file);
155 /* write option count */
158 list_for_each_entry(&dev->boot_options, opt, list)
161 *(uint32_t *)pos = __cpu_to_be32(n);
162 pos += sizeof(uint32_t);
164 /* write each option */
165 list_for_each_entry(&dev->boot_options, opt, list) {
166 pos += pb_protocol_serialise_string(pos, opt->id);
167 pos += pb_protocol_serialise_string(pos, opt->name);
168 pos += pb_protocol_serialise_string(pos, opt->description);
169 pos += pb_protocol_serialise_string(pos, opt->icon_file);
170 pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
171 pos += pb_protocol_serialise_string(pos, opt->initrd_file);
172 pos += pb_protocol_serialise_string(pos, opt->boot_args);
178 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
183 total_len = sizeof(*message) + message->payload_len;
185 message->payload_len = __cpu_to_be32(message->payload_len);
186 message->action = __cpu_to_be32(message->action);
188 for (pos = (void *)message; total_len;) {
189 rc = write(fd, pos, total_len);
198 talloc_free(message);
200 return total_len ? -1 : 0;
203 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
204 int action, int payload_len)
206 struct pb_protocol_message *message;
208 if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE)
211 message = talloc_size(ctx, sizeof(*message) + payload_len);
213 /* we convert these to big-endian in write_message() */
214 message->action = action;
215 message->payload_len = payload_len;
221 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
223 struct pb_protocol_message *message, m;
226 /* use the stack for the initial 8-byte read */
228 rc = read(fd, &m, sizeof(m));
232 m.payload_len = __be32_to_cpu(m.payload_len);
233 m.action = __be32_to_cpu(m.action);
235 if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE)
238 message = talloc_size(ctx, sizeof(m) + m.payload_len);
239 memcpy(message, &m, sizeof(m));
241 for (len = 0; len < m.payload_len;) {
242 rc = read(fd, message->payload + len, m.payload_len - len);
245 talloc_free(message);
256 struct device *pb_protocol_deserialise_device(void *ctx,
257 struct pb_protocol_message *message)
261 int i, n_options, len;
263 len = message->payload_len;
264 pos = message->payload;
266 dev = talloc(ctx, struct device);
268 if (read_string(dev, &pos, &len, &dev->id))
271 if (read_string(dev, &pos, &len, &dev->name))
274 if (read_string(dev, &pos, &len, &dev->description))
277 if (read_string(dev, &pos, &len, &dev->icon_file))
280 n_options = __be32_to_cpu(*(uint32_t *)pos);
281 pos += sizeof(uint32_t);
283 list_init(&dev->boot_options);
285 for (i = 0; i < n_options; i++) {
286 struct boot_option *opt;
288 opt = talloc(dev, struct boot_option);
290 if (read_string(opt, &pos, &len, &opt->id))
292 if (read_string(opt, &pos, &len, &opt->name))
294 if (read_string(opt, &pos, &len,
297 if (read_string(opt, &pos, &len,
300 if (read_string(opt, &pos, &len,
301 &opt->boot_image_file))
303 if (read_string(opt, &pos, &len,
306 if (read_string(opt, &pos, &len,
310 list_add(&dev->boot_options, &opt->list);