]> git.ozlabs.org Git - petitboot/blob - lib/pb-protocol/pb-protocol.c
ui/ncurses: Add widget_select_drop_options
[petitboot] / lib / pb-protocol / pb-protocol.c
1
2 #include <assert.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <asm/byteorder.h>
8
9 #include <talloc/talloc.h>
10 #include <list/list.h>
11 #include <log/log.h>
12
13 #include "pb-protocol.h"
14
15
16 /* Message format:
17  *
18  * 4-byte action, determines the remaining message content
19  * 4-byte total payload len
20  *   - not including action and payload len header
21  *
22  * action = 0x1: device add message
23  *  payload:
24  *   4-byte len, id
25  *   1-byte type
26  *   4-byte len, name
27  *   4-byte len, description
28  *   4-byte len, icon_file
29  *
30  *   4-byte option count
31  *   for each option:
32  *    4-byte len, id
33  *    4-byte len, name
34  *    4-byte len, description
35  *    4-byte len, icon_file
36  *    4-byte len, boot_image_file
37  *    4-byte len, initrd_file
38  *    4-byte len, dtb_file
39  *    4-byte len, boot_args
40  *
41  * action = 0x2: device remove message
42  *  payload:
43  *   4-byte len, id
44  *
45  * action = 0x3: boot
46  *  payload:
47  *   4-byte len, boot option id
48  *   4-byte len, boot_image_file
49  *   4-byte len, initrd_file
50  *   4-byte len, dtb_file
51  *   4-byte len, boot_args
52  *
53  */
54
55 void pb_protocol_dump_device(const struct device *dev, const char *text,
56         FILE *stream)
57 {
58         struct boot_option *opt;
59
60         fprintf(stream, "%snew dev:\n", text);
61         fprintf(stream, "%s\tid:   %s\n", text, dev->id);
62         fprintf(stream, "%s\tname: %s\n", text, dev->name);
63         fprintf(stream, "%s\tdesc: %s\n", text, dev->description);
64         fprintf(stream, "%s\ticon: %s\n", text, dev->icon_file);
65         fprintf(stream, "%s\tboot options:\n", text);
66         list_for_each_entry(&dev->boot_options, opt, list) {
67                 fprintf(stream, "%s\t\tid:   %s\n", text, opt->id);
68                 fprintf(stream, "%s\t\tname: %s\n", text, opt->name);
69                 fprintf(stream, "%s\t\tdesc: %s\n", text, opt->description);
70                 fprintf(stream, "%s\t\ticon: %s\n", text, opt->icon_file);
71                 fprintf(stream, "%s\t\tboot: %s\n", text, opt->boot_image_file);
72                 fprintf(stream, "%s\t\tinit: %s\n", text, opt->initrd_file);
73                 fprintf(stream, "%s\t\tdtb:  %s\n", text, opt->dtb_file);
74                 fprintf(stream, "%s\t\targs: %s\n", text, opt->boot_args);
75         }
76 }
77
78 int pb_protocol_device_cmp(const struct device *a, const struct device *b)
79 {
80         return !strcmp(a->id, b->id);
81 }
82
83 int pb_protocol_boot_option_cmp(const struct boot_option *a,
84         const struct boot_option *b)
85 {
86         return !strcmp(a->id, b->id);
87 }
88
89 /* Write a string into the buffer, starting at pos.
90  *
91  * Returns the total length used for the write, including length header.
92  */
93 int pb_protocol_serialise_string(char *pos, const char *str)
94 {
95         int len = 0;
96
97         if (str)
98                 len = strlen(str);
99
100         *(uint32_t *)pos = __cpu_to_be32(len);
101         pos += sizeof(uint32_t);
102
103         memcpy(pos, str, len);
104
105         return len + sizeof(uint32_t);
106 }
107
108 /* Read a string from a buffer, allocating the new string as necessary.
109  *
110  * @param[in] ctx       The talloc context to base the allocation on
111  * @param[in,out] pos   Where to start reading
112  * @param[in,out] len   The amount of data remaining in the buffer
113  * @param[out] str      Pointer to resuling string
114  * @return              zero on success, non-zero on failure
115  */
116 static int read_string(void *ctx, const char **pos, unsigned int *len,
117         char **str)
118 {
119         uint32_t str_len, read_len;
120
121         if (*len < sizeof(uint32_t))
122                 return -1;
123
124         str_len = __be32_to_cpu(*(uint32_t *)(*pos));
125         read_len = sizeof(uint32_t);
126
127         if (read_len + str_len > *len)
128                 return -1;
129
130         if (str_len == 0)
131                 *str = NULL;
132         else
133                 *str = talloc_strndup(ctx, *pos + read_len, str_len);
134
135         read_len += str_len;
136
137         /* all ok, update the caller's pointers */
138         *pos += read_len;
139         *len -= read_len;
140
141         return 0;
142 }
143
144 static int read_u32(const char **pos, unsigned int *len, unsigned int *p)
145 {
146         if (*len < sizeof(uint32_t))
147                 return -1;
148
149         *p = (unsigned int)__be32_to_cpu(*(uint32_t *)(*pos));
150         *pos += sizeof(uint32_t);
151         *len -= sizeof(uint32_t);
152
153         return 0;
154 }
155
156 char *pb_protocol_deserialise_string(void *ctx,
157                 const struct pb_protocol_message *message)
158 {
159         const char *buf;
160         char *str;
161         unsigned int len;
162
163         len = message->payload_len;
164         buf = message->payload;
165
166         if (read_string(ctx, &buf, &len, &str))
167                 return NULL;
168
169         return str;
170 }
171
172 static int optional_strlen(const char *str)
173 {
174         if (!str)
175                 return 0;
176         return strlen(str);
177 }
178
179 int pb_protocol_device_len(const struct device *dev)
180 {
181         return  4 + optional_strlen(dev->id) +
182                 sizeof(dev->type) +
183                 4 + optional_strlen(dev->name) +
184                 4 + optional_strlen(dev->description) +
185                 4 + optional_strlen(dev->icon_file);
186 }
187
188 int pb_protocol_boot_option_len(const struct boot_option *opt)
189 {
190
191         return  4 + optional_strlen(opt->device_id) +
192                 4 + optional_strlen(opt->id) +
193                 4 + optional_strlen(opt->name) +
194                 4 + optional_strlen(opt->description) +
195                 4 + optional_strlen(opt->icon_file) +
196                 4 + optional_strlen(opt->boot_image_file) +
197                 4 + optional_strlen(opt->initrd_file) +
198                 4 + optional_strlen(opt->dtb_file) +
199                 4 + optional_strlen(opt->boot_args) +
200                 sizeof(opt->is_default);
201 }
202
203 int pb_protocol_boot_len(const struct boot_command *boot)
204 {
205         return  4 + optional_strlen(boot->option_id) +
206                 4 + optional_strlen(boot->boot_image_file) +
207                 4 + optional_strlen(boot->initrd_file) +
208                 4 + optional_strlen(boot->dtb_file) +
209                 4 + optional_strlen(boot->boot_args);
210 }
211
212 int pb_protocol_boot_status_len(const struct boot_status *status)
213 {
214         return  4 +
215                 4 + optional_strlen(status->message) +
216                 4 + optional_strlen(status->detail) +
217                 4;
218 }
219
220 int pb_protocol_system_info_len(const struct system_info *sysinfo)
221 {
222         unsigned int len, i;
223
224         len =   4 + optional_strlen(sysinfo->type) +
225                 4 + optional_strlen(sysinfo->identifier) +
226                 4 + 4;
227
228         for (i = 0; i < sysinfo->n_interfaces; i++) {
229                 struct interface_info *if_info = sysinfo->interfaces[i];
230                 len +=  4 + if_info->hwaddr_size +
231                         4 + optional_strlen(if_info->name);
232         }
233
234         for (i = 0; i < sysinfo->n_blockdevs; i++) {
235                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
236                 len +=  4 + optional_strlen(bd_info->name) +
237                         4 + optional_strlen(bd_info->uuid) +
238                         4 + optional_strlen(bd_info->mountpoint);
239         }
240
241         return len;
242 }
243
244 static int pb_protocol_interface_config_len(struct interface_config *conf)
245 {
246         unsigned int len;
247
248         len =   sizeof(conf->hwaddr) +
249                 4 /* conf->ignore */;
250
251         if (conf->ignore)
252                 return len;
253
254         len += 4 /* conf->method */;
255
256         if (conf->method == CONFIG_METHOD_STATIC) {
257                 len += 4 + optional_strlen(conf->static_config.address);
258                 len += 4 + optional_strlen(conf->static_config.gateway);
259         }
260
261         return len;
262 }
263
264 int pb_protocol_config_len(const struct config *config)
265 {
266         unsigned int i, len;
267
268         len =   4 /* config->autoboot_enabled */ +
269                 4 /* config->autoboot_timeout_sec */;
270
271         len += 4;
272         for (i = 0; i < config->network.n_interfaces; i++)
273                 len += pb_protocol_interface_config_len(
274                                 config->network.interfaces[i]);
275
276         len += 4;
277         for (i = 0; i < config->network.n_dns_servers; i++)
278                 len += 4 + optional_strlen(config->network.dns_servers[i]);
279
280         len += 4;
281         len += config->n_boot_priorities * 4;
282
283         return len;
284 }
285
286 int pb_protocol_serialise_device(const struct device *dev,
287                 char *buf, int buf_len)
288 {
289         char *pos = buf;
290
291         pos += pb_protocol_serialise_string(pos, dev->id);
292         *(enum device_type *)pos = dev->type;
293         pos += sizeof(enum device_type);
294         pos += pb_protocol_serialise_string(pos, dev->name);
295         pos += pb_protocol_serialise_string(pos, dev->description);
296         pos += pb_protocol_serialise_string(pos, dev->icon_file);
297
298         assert(pos <= buf + buf_len);
299         (void)buf_len;
300
301         return 0;
302 }
303
304 int pb_protocol_serialise_boot_option(const struct boot_option *opt,
305                 char *buf, int buf_len)
306 {
307         char *pos = buf;
308
309         pos += pb_protocol_serialise_string(pos, opt->device_id);
310         pos += pb_protocol_serialise_string(pos, opt->id);
311         pos += pb_protocol_serialise_string(pos, opt->name);
312         pos += pb_protocol_serialise_string(pos, opt->description);
313         pos += pb_protocol_serialise_string(pos, opt->icon_file);
314         pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
315         pos += pb_protocol_serialise_string(pos, opt->initrd_file);
316         pos += pb_protocol_serialise_string(pos, opt->dtb_file);
317         pos += pb_protocol_serialise_string(pos, opt->boot_args);
318
319         *(bool *)pos = opt->is_default;
320         pos += sizeof(bool);
321
322         assert(pos <= buf + buf_len);
323         (void)buf_len;
324
325         return 0;
326 }
327
328 int pb_protocol_serialise_boot_command(const struct boot_command *boot,
329                 char *buf, int buf_len)
330 {
331         char *pos = buf;
332
333         pos += pb_protocol_serialise_string(pos, boot->option_id);
334         pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
335         pos += pb_protocol_serialise_string(pos, boot->initrd_file);
336         pos += pb_protocol_serialise_string(pos, boot->dtb_file);
337         pos += pb_protocol_serialise_string(pos, boot->boot_args);
338
339         assert(pos <= buf + buf_len);
340         (void)buf_len;
341
342         return 0;
343 }
344
345 int pb_protocol_serialise_boot_status(const struct boot_status *status,
346                 char *buf, int buf_len)
347 {
348         char *pos = buf;
349
350         *(uint32_t *)pos = __cpu_to_be32(status->type);
351         pos += sizeof(uint32_t);
352
353         pos += pb_protocol_serialise_string(pos, status->message);
354         pos += pb_protocol_serialise_string(pos, status->detail);
355
356         *(uint32_t *)pos = __cpu_to_be32(status->type);
357         pos += sizeof(uint32_t);
358
359         assert(pos <= buf + buf_len);
360         (void)buf_len;
361
362         return 0;
363 }
364
365 int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
366                 char *buf, int buf_len)
367 {
368         char *pos = buf;
369         unsigned int i;
370
371         pos += pb_protocol_serialise_string(pos, sysinfo->type);
372         pos += pb_protocol_serialise_string(pos, sysinfo->identifier);
373
374         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_interfaces);
375         pos += sizeof(uint32_t);
376
377         for (i = 0; i < sysinfo->n_interfaces; i++) {
378                 struct interface_info *if_info = sysinfo->interfaces[i];
379
380                 *(uint32_t *)pos = __cpu_to_be32(if_info->hwaddr_size);
381                 pos += sizeof(uint32_t);
382
383                 memcpy(pos, if_info->hwaddr, if_info->hwaddr_size);
384                 pos += if_info->hwaddr_size;
385
386                 pos += pb_protocol_serialise_string(pos, if_info->name);
387         }
388
389         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_blockdevs);
390         pos += sizeof(uint32_t);
391
392         for (i = 0; i < sysinfo->n_blockdevs; i++) {
393                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
394
395                 pos += pb_protocol_serialise_string(pos, bd_info->name);
396                 pos += pb_protocol_serialise_string(pos, bd_info->uuid);
397                 pos += pb_protocol_serialise_string(pos, bd_info->mountpoint);
398         }
399
400         assert(pos <= buf + buf_len);
401         (void)buf_len;
402
403         return 0;
404 }
405
406 static int pb_protocol_serialise_config_interface(char *buf,
407                 struct interface_config *conf)
408 {
409         char *pos = buf;
410
411         memcpy(pos, conf->hwaddr, sizeof(conf->hwaddr));
412         pos += sizeof(conf->hwaddr);
413
414         *(uint32_t *)pos = conf->ignore;
415         pos += 4;
416
417         if (conf->ignore)
418                 return pos - buf;
419
420         *(uint32_t *)pos = __cpu_to_be32(conf->method);
421         pos += 4;
422
423         if (conf->method == CONFIG_METHOD_STATIC) {
424                 pos += pb_protocol_serialise_string(pos,
425                                 conf->static_config.address);
426                 pos += pb_protocol_serialise_string(pos,
427                                 conf->static_config.gateway);
428         }
429
430         return pos - buf;
431 }
432
433 int pb_protocol_serialise_config(const struct config *config,
434                 char *buf, int buf_len)
435 {
436         char *pos = buf;
437         unsigned int i;
438
439         *(uint32_t *)pos = config->autoboot_enabled;
440         pos += 4;
441
442         *(uint32_t *)pos = __cpu_to_be32(config->autoboot_timeout_sec);
443         pos += 4;
444
445         *(uint32_t *)pos = __cpu_to_be32(config->network.n_interfaces);
446         pos += 4;
447         for (i = 0; i < config->network.n_interfaces; i++) {
448                 struct interface_config *iface =
449                         config->network.interfaces[i];
450                 pos += pb_protocol_serialise_config_interface(pos, iface);
451         }
452
453         *(uint32_t *)pos = __cpu_to_be32(config->network.n_dns_servers);
454         pos += 4;
455         for (i = 0; i < config->network.n_dns_servers; i++) {
456                 pos += pb_protocol_serialise_string(pos,
457                                 config->network.dns_servers[i]);
458         }
459
460         *(uint32_t *)pos = __cpu_to_be32(config->n_boot_priorities);
461         pos += 4;
462         for (i = 0; i < config->n_boot_priorities; i++) {
463                 *(uint32_t *)pos = __cpu_to_be32(config->boot_priorities[i].type);
464                 pos += 4;
465         }
466
467         assert(pos <= buf + buf_len);
468         (void)buf_len;
469
470         return 0;
471 }
472
473 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
474 {
475         int total_len, rc;
476         char *pos;
477
478         total_len = sizeof(*message) + message->payload_len;
479
480         message->payload_len = __cpu_to_be32(message->payload_len);
481         message->action = __cpu_to_be32(message->action);
482
483         for (pos = (void *)message; total_len;) {
484                 rc = write(fd, pos, total_len);
485
486                 if (rc <= 0)
487                         break;
488
489                 total_len -= rc;
490                 pos += rc;
491         }
492
493         talloc_free(message);
494
495         if (!total_len)
496                 return 0;
497
498         pb_log("%s: failed: %s\n", __func__, strerror(errno));
499         return -1;
500 }
501
502 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
503                 enum pb_protocol_action action, int payload_len)
504 {
505         struct pb_protocol_message *message;
506
507         if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
508                 pb_log("%s: payload too big %u/%u\n", __func__, payload_len,
509                         PB_PROTOCOL_MAX_PAYLOAD_SIZE);
510                 return NULL;
511         }
512
513         message = talloc_size(ctx, sizeof(*message) + payload_len);
514
515         /* we convert these to big-endian in write_message() */
516         message->action = action;
517         message->payload_len = payload_len;
518
519         return message;
520
521 }
522
523 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
524 {
525         struct pb_protocol_message *message, m;
526         int rc;
527         unsigned int len;
528
529         /* use the stack for the initial 8-byte read */
530
531         rc = read(fd, &m, sizeof(m));
532         if (rc != sizeof(m))
533                 return NULL;
534
535         m.payload_len = __be32_to_cpu(m.payload_len);
536         m.action = __be32_to_cpu(m.action);
537
538         if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
539                 pb_log("%s: payload too big %u/%u\n", __func__, m.payload_len,
540                         PB_PROTOCOL_MAX_PAYLOAD_SIZE);
541                 return NULL;
542         }
543
544         message = talloc_size(ctx, sizeof(m) + m.payload_len);
545         memcpy(message, &m, sizeof(m));
546
547         for (len = 0; len < m.payload_len;) {
548                 rc = read(fd, message->payload + len, m.payload_len - len);
549
550                 if (rc <= 0) {
551                         talloc_free(message);
552                         pb_log("%s: failed (%u): %s\n", __func__, len,
553                                 strerror(errno));
554                         return NULL;
555                 }
556
557                 len += rc;
558         }
559
560         return message;
561 }
562
563
564 int pb_protocol_deserialise_device(struct device *dev,
565                 const struct pb_protocol_message *message)
566 {
567         unsigned int len;
568         const char *pos;
569         int rc = -1;
570
571         len = message->payload_len;
572         pos = message->payload;
573
574         if (read_string(dev, &pos, &len, &dev->id))
575                 goto out;
576
577         if (len < sizeof(enum device_type))
578                 goto out;
579         dev->type = *(enum device_type *)(pos);
580         pos += sizeof(enum device_type);
581         len -= sizeof(enum device_type);
582
583         if (read_string(dev, &pos, &len, &dev->name))
584                 goto out;
585
586         if (read_string(dev, &pos, &len, &dev->description))
587                 goto out;
588
589         if (read_string(dev, &pos, &len, &dev->icon_file))
590                 goto out;
591
592         rc = 0;
593
594 out:
595         return rc;
596 }
597
598 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
599                 const struct pb_protocol_message *message)
600 {
601         unsigned int len;
602         const char *pos;
603         int rc = -1;
604
605         len = message->payload_len;
606         pos = message->payload;
607
608         if (read_string(opt, &pos, &len, &opt->device_id))
609                 goto out;
610
611         if (read_string(opt, &pos, &len, &opt->id))
612                 goto out;
613
614         if (read_string(opt, &pos, &len, &opt->name))
615                 goto out;
616
617         if (read_string(opt, &pos, &len, &opt->description))
618                 goto out;
619
620         if (read_string(opt, &pos, &len, &opt->icon_file))
621                 goto out;
622
623         if (read_string(opt, &pos, &len, &opt->boot_image_file))
624                 goto out;
625
626         if (read_string(opt, &pos, &len, &opt->initrd_file))
627                 goto out;
628
629         if (read_string(opt, &pos, &len, &opt->dtb_file))
630                 goto out;
631
632         if (read_string(opt, &pos, &len, &opt->boot_args))
633                 goto out;
634
635         if (len < sizeof(bool))
636                 goto out;
637         opt->is_default = *(bool *)(pos);
638
639         rc = 0;
640
641 out:
642         return rc;
643 }
644
645 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
646                 const struct pb_protocol_message *message)
647 {
648         unsigned int len;
649         const char *pos;
650         int rc = -1;
651
652         len = message->payload_len;
653         pos = message->payload;
654
655         if (read_string(cmd, &pos, &len, &cmd->option_id))
656                 goto out;
657
658         if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
659                 goto out;
660
661         if (read_string(cmd, &pos, &len, &cmd->initrd_file))
662                 goto out;
663
664         if (read_string(cmd, &pos, &len, &cmd->dtb_file))
665                 goto out;
666
667         if (read_string(cmd, &pos, &len, &cmd->boot_args))
668                 goto out;
669
670         rc = 0;
671
672 out:
673         return rc;
674 }
675
676 int pb_protocol_deserialise_boot_status(struct boot_status *status,
677                 const struct pb_protocol_message *message)
678 {
679         unsigned int len;
680         const char *pos;
681         int rc = -1;
682
683         len = message->payload_len;
684         pos = message->payload;
685
686         /* first up, the type enum... */
687         if (len < sizeof(uint32_t))
688                 goto out;
689
690         status->type = __be32_to_cpu(*(uint32_t *)(pos));
691
692         switch (status->type) {
693         case BOOT_STATUS_ERROR:
694         case BOOT_STATUS_INFO:
695                 break;
696         default:
697                 goto out;
698         }
699
700         pos += sizeof(uint32_t);
701         len -= sizeof(uint32_t);
702
703         /* message and detail strings */
704         if (read_string(status, &pos, &len, &status->message))
705                 goto out;
706
707         if (read_string(status, &pos, &len, &status->detail))
708                 goto out;
709
710         /* and finally, progress */
711         if (len < sizeof(uint32_t))
712                 goto out;
713
714         status->progress = __be32_to_cpu(*(uint32_t *)(pos));
715
716         /* clamp to 100% */
717         if (status->progress > 100)
718                 status->progress = 100;
719
720         rc = 0;
721
722 out:
723         return rc;
724 }
725
726 int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
727                 const struct pb_protocol_message *message)
728 {
729         unsigned int len, i;
730         const char *pos;
731         int rc = -1;
732
733         len = message->payload_len;
734         pos = message->payload;
735
736         /* type and identifier strings */
737         if (read_string(sysinfo, &pos, &len, &sysinfo->type))
738                 goto out;
739
740         if (read_string(sysinfo, &pos, &len, &sysinfo->identifier))
741                 goto out;
742
743         /* number of interfaces */
744         if (read_u32(&pos, &len, &sysinfo->n_interfaces))
745                 goto out;
746
747         sysinfo->interfaces = talloc_array(sysinfo, struct interface_info *,
748                         sysinfo->n_interfaces);
749
750         for (i = 0; i < sysinfo->n_interfaces; i++) {
751                 struct interface_info *if_info = talloc(sysinfo,
752                                                         struct interface_info);
753
754                 if (read_u32(&pos, &len, &if_info->hwaddr_size))
755                         goto out;
756
757                 if (len < if_info->hwaddr_size)
758                         goto out;
759
760                 if_info->hwaddr = talloc_memdup(if_info, pos,
761                                                 if_info->hwaddr_size);
762                 pos += if_info->hwaddr_size;
763                 len -= if_info->hwaddr_size;
764
765                 if (read_string(if_info, &pos, &len, &if_info->name))
766                         goto out;
767
768                 sysinfo->interfaces[i] = if_info;
769         }
770
771         /* number of interfaces */
772         if (read_u32(&pos, &len, &sysinfo->n_blockdevs))
773                 goto out;
774
775         sysinfo->blockdevs = talloc_array(sysinfo, struct blockdev_info *,
776                         sysinfo->n_blockdevs);
777
778         for (i = 0; i < sysinfo->n_blockdevs; i++) {
779                 struct blockdev_info *bd_info = talloc(sysinfo,
780                                                         struct blockdev_info);
781
782                 if (read_string(bd_info, &pos, &len, &bd_info->name))
783                         goto out;
784
785                 if (read_string(bd_info, &pos, &len, &bd_info->uuid))
786                         goto out;
787
788                 if (read_string(bd_info, &pos, &len, &bd_info->mountpoint))
789                         goto out;
790
791                 sysinfo->blockdevs[i] = bd_info;
792         }
793         rc = 0;
794
795 out:
796         return rc;
797 }
798
799 static int pb_protocol_deserialise_config_interface(const char **buf,
800                 unsigned int *len, struct interface_config *iface)
801 {
802         unsigned int tmp;
803
804         if (*len < sizeof(iface->hwaddr))
805                 return -1;
806
807         memcpy(iface->hwaddr, *buf, sizeof(iface->hwaddr));
808         *buf += sizeof(iface->hwaddr);
809         *len -= sizeof(iface->hwaddr);
810
811         if (read_u32(buf, len, &tmp))
812                 return -1;
813         iface->ignore = !!tmp;
814
815         if (iface->ignore)
816                 return 0;
817
818         if (read_u32(buf, len, &iface->method))
819                 return -1;
820
821         if (iface->method == CONFIG_METHOD_STATIC) {
822                 if (read_string(iface, buf, len, &iface->static_config.address))
823                         return -1;
824
825                 if (read_string(iface, buf, len, &iface->static_config.gateway))
826                         return -1;
827         }
828
829         return 0;
830 }
831
832 int pb_protocol_deserialise_config(struct config *config,
833                 const struct pb_protocol_message *message)
834 {
835         unsigned int len, i, tmp;
836         const char *pos;
837         int rc = -1;
838
839         len = message->payload_len;
840         pos = message->payload;
841
842         if (read_u32(&pos, &len, &tmp))
843                 goto out;
844         config->autoboot_enabled = !!tmp;
845
846         if (read_u32(&pos, &len, &config->autoboot_timeout_sec))
847                 goto out;
848
849         if (read_u32(&pos, &len, &config->network.n_interfaces))
850                 goto out;
851
852         config->network.interfaces = talloc_array(config,
853                         struct interface_config *, config->network.n_interfaces);
854
855         for (i = 0; i < config->network.n_interfaces; i++) {
856                 struct interface_config *iface = talloc_zero(
857                                 config->network.interfaces,
858                                 struct interface_config);
859                 if (pb_protocol_deserialise_config_interface(&pos, &len, iface))
860                         goto out;
861                 config->network.interfaces[i] = iface;
862         }
863
864         if (read_u32(&pos, &len, &config->network.n_dns_servers))
865                 goto out;
866         config->network.dns_servers = talloc_array(config, const char *,
867                         config->network.n_dns_servers);
868
869         for (i = 0; i < config->network.n_dns_servers; i++) {
870                 char *tmp;
871                 if (read_string(config->network.dns_servers, &pos, &len, &tmp))
872                         goto out;
873                 config->network.dns_servers[i] = tmp;
874         }
875
876         if (read_u32(&pos, &len, &config->n_boot_priorities))
877                 goto out;
878         config->boot_priorities = talloc_array(config, struct boot_priority,
879                         config->n_boot_priorities);
880
881         for (i = 0; i < config->n_boot_priorities; i++) {
882                 if (read_u32(&pos, &len, &tmp))
883                         goto out;
884                 config->boot_priorities[i].type = tmp;
885         }
886
887         rc = 0;
888
889 out:
890         return rc;
891 }