]> git.ozlabs.org Git - petitboot/blob - lib/pb-protocol/pb-protocol.c
discover: Only update our config if the config_set succeeded.
[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                         sizeof(if_info->link);
233         }
234
235         for (i = 0; i < sysinfo->n_blockdevs; i++) {
236                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
237                 len +=  4 + optional_strlen(bd_info->name) +
238                         4 + optional_strlen(bd_info->uuid) +
239                         4 + optional_strlen(bd_info->mountpoint);
240         }
241
242         return len;
243 }
244
245 static int pb_protocol_interface_config_len(struct interface_config *conf)
246 {
247         unsigned int len;
248
249         len =   sizeof(conf->hwaddr) +
250                 4 /* conf->ignore */;
251
252         if (conf->ignore)
253                 return len;
254
255         len += 4 /* conf->method */;
256
257         if (conf->method == CONFIG_METHOD_STATIC) {
258                 len += 4 + optional_strlen(conf->static_config.address);
259                 len += 4 + optional_strlen(conf->static_config.gateway);
260         }
261
262         return len;
263 }
264
265 int pb_protocol_config_len(const struct config *config)
266 {
267         unsigned int i, len;
268
269         len =   4 /* config->autoboot_enabled */ +
270                 4 /* config->autoboot_timeout_sec */ +
271                 4 /* config->safe_mode */;
272
273         len += 4;
274         for (i = 0; i < config->network.n_interfaces; i++)
275                 len += pb_protocol_interface_config_len(
276                                 config->network.interfaces[i]);
277
278         len += 4;
279         for (i = 0; i < config->network.n_dns_servers; i++)
280                 len += 4 + optional_strlen(config->network.dns_servers[i]);
281
282         len += 4;
283         len += config->n_boot_priorities * 8;
284
285         len += 4 + optional_strlen(config->boot_device);
286
287         return len;
288 }
289
290 int pb_protocol_serialise_device(const struct device *dev,
291                 char *buf, int buf_len)
292 {
293         char *pos = buf;
294
295         pos += pb_protocol_serialise_string(pos, dev->id);
296         *(enum device_type *)pos = dev->type;
297         pos += sizeof(enum device_type);
298         pos += pb_protocol_serialise_string(pos, dev->name);
299         pos += pb_protocol_serialise_string(pos, dev->description);
300         pos += pb_protocol_serialise_string(pos, dev->icon_file);
301
302         assert(pos <= buf + buf_len);
303         (void)buf_len;
304
305         return 0;
306 }
307
308 int pb_protocol_serialise_boot_option(const struct boot_option *opt,
309                 char *buf, int buf_len)
310 {
311         char *pos = buf;
312
313         pos += pb_protocol_serialise_string(pos, opt->device_id);
314         pos += pb_protocol_serialise_string(pos, opt->id);
315         pos += pb_protocol_serialise_string(pos, opt->name);
316         pos += pb_protocol_serialise_string(pos, opt->description);
317         pos += pb_protocol_serialise_string(pos, opt->icon_file);
318         pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
319         pos += pb_protocol_serialise_string(pos, opt->initrd_file);
320         pos += pb_protocol_serialise_string(pos, opt->dtb_file);
321         pos += pb_protocol_serialise_string(pos, opt->boot_args);
322
323         *(bool *)pos = opt->is_default;
324         pos += sizeof(bool);
325
326         assert(pos <= buf + buf_len);
327         (void)buf_len;
328
329         return 0;
330 }
331
332 int pb_protocol_serialise_boot_command(const struct boot_command *boot,
333                 char *buf, int buf_len)
334 {
335         char *pos = buf;
336
337         pos += pb_protocol_serialise_string(pos, boot->option_id);
338         pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
339         pos += pb_protocol_serialise_string(pos, boot->initrd_file);
340         pos += pb_protocol_serialise_string(pos, boot->dtb_file);
341         pos += pb_protocol_serialise_string(pos, boot->boot_args);
342
343         assert(pos <= buf + buf_len);
344         (void)buf_len;
345
346         return 0;
347 }
348
349 int pb_protocol_serialise_boot_status(const struct boot_status *status,
350                 char *buf, int buf_len)
351 {
352         char *pos = buf;
353
354         *(uint32_t *)pos = __cpu_to_be32(status->type);
355         pos += sizeof(uint32_t);
356
357         pos += pb_protocol_serialise_string(pos, status->message);
358         pos += pb_protocol_serialise_string(pos, status->detail);
359
360         *(uint32_t *)pos = __cpu_to_be32(status->type);
361         pos += sizeof(uint32_t);
362
363         assert(pos <= buf + buf_len);
364         (void)buf_len;
365
366         return 0;
367 }
368
369 int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
370                 char *buf, int buf_len)
371 {
372         char *pos = buf;
373         unsigned int i;
374
375         pos += pb_protocol_serialise_string(pos, sysinfo->type);
376         pos += pb_protocol_serialise_string(pos, sysinfo->identifier);
377
378         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_interfaces);
379         pos += sizeof(uint32_t);
380
381         for (i = 0; i < sysinfo->n_interfaces; i++) {
382                 struct interface_info *if_info = sysinfo->interfaces[i];
383
384                 *(uint32_t *)pos = __cpu_to_be32(if_info->hwaddr_size);
385                 pos += sizeof(uint32_t);
386
387                 memcpy(pos, if_info->hwaddr, if_info->hwaddr_size);
388                 pos += if_info->hwaddr_size;
389
390                 pos += pb_protocol_serialise_string(pos, if_info->name);
391
392                 *(bool *)pos = if_info->link;
393                 pos += sizeof(bool);
394         }
395
396         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_blockdevs);
397         pos += sizeof(uint32_t);
398
399         for (i = 0; i < sysinfo->n_blockdevs; i++) {
400                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
401
402                 pos += pb_protocol_serialise_string(pos, bd_info->name);
403                 pos += pb_protocol_serialise_string(pos, bd_info->uuid);
404                 pos += pb_protocol_serialise_string(pos, bd_info->mountpoint);
405         }
406
407         assert(pos <= buf + buf_len);
408         (void)buf_len;
409
410         return 0;
411 }
412
413 static int pb_protocol_serialise_config_interface(char *buf,
414                 struct interface_config *conf)
415 {
416         char *pos = buf;
417
418         memcpy(pos, conf->hwaddr, sizeof(conf->hwaddr));
419         pos += sizeof(conf->hwaddr);
420
421         *(uint32_t *)pos = conf->ignore;
422         pos += 4;
423
424         if (conf->ignore)
425                 return pos - buf;
426
427         *(uint32_t *)pos = __cpu_to_be32(conf->method);
428         pos += 4;
429
430         if (conf->method == CONFIG_METHOD_STATIC) {
431                 pos += pb_protocol_serialise_string(pos,
432                                 conf->static_config.address);
433                 pos += pb_protocol_serialise_string(pos,
434                                 conf->static_config.gateway);
435         }
436
437         return pos - buf;
438 }
439
440 int pb_protocol_serialise_config(const struct config *config,
441                 char *buf, int buf_len)
442 {
443         char *pos = buf;
444         unsigned int i;
445
446         *(uint32_t *)pos = config->autoboot_enabled;
447         pos += 4;
448
449         *(uint32_t *)pos = __cpu_to_be32(config->autoboot_timeout_sec);
450         pos += 4;
451
452         *(uint32_t *)pos = config->safe_mode;
453         pos += 4;
454
455         *(uint32_t *)pos = __cpu_to_be32(config->network.n_interfaces);
456         pos += 4;
457         for (i = 0; i < config->network.n_interfaces; i++) {
458                 struct interface_config *iface =
459                         config->network.interfaces[i];
460                 pos += pb_protocol_serialise_config_interface(pos, iface);
461         }
462
463         *(uint32_t *)pos = __cpu_to_be32(config->network.n_dns_servers);
464         pos += 4;
465         for (i = 0; i < config->network.n_dns_servers; i++) {
466                 pos += pb_protocol_serialise_string(pos,
467                                 config->network.dns_servers[i]);
468         }
469
470         *(uint32_t *)pos = __cpu_to_be32(config->n_boot_priorities);
471         pos += 4;
472         for (i = 0; i < config->n_boot_priorities; i++) {
473                 *(uint32_t *)pos =
474                         __cpu_to_be32(config->boot_priorities[i].type);
475                 pos += 4;
476                 *(uint32_t *)pos =
477                         __cpu_to_be32(config->boot_priorities[i].priority);
478                 pos += 4;
479         }
480
481         pos += pb_protocol_serialise_string(pos, config->boot_device);
482
483         assert(pos <= buf + buf_len);
484         (void)buf_len;
485
486         return 0;
487 }
488
489 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
490 {
491         int total_len, rc;
492         char *pos;
493
494         total_len = sizeof(*message) + message->payload_len;
495
496         message->payload_len = __cpu_to_be32(message->payload_len);
497         message->action = __cpu_to_be32(message->action);
498
499         for (pos = (void *)message; total_len;) {
500                 rc = write(fd, pos, total_len);
501
502                 if (rc <= 0)
503                         break;
504
505                 total_len -= rc;
506                 pos += rc;
507         }
508
509         talloc_free(message);
510
511         if (!total_len)
512                 return 0;
513
514         pb_log("%s: failed: %s\n", __func__, strerror(errno));
515         return -1;
516 }
517
518 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
519                 enum pb_protocol_action action, int payload_len)
520 {
521         struct pb_protocol_message *message;
522
523         if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
524                 pb_log("%s: payload too big %u/%u\n", __func__, payload_len,
525                         PB_PROTOCOL_MAX_PAYLOAD_SIZE);
526                 return NULL;
527         }
528
529         message = talloc_size(ctx, sizeof(*message) + payload_len);
530
531         /* we convert these to big-endian in write_message() */
532         message->action = action;
533         message->payload_len = payload_len;
534
535         return message;
536
537 }
538
539 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
540 {
541         struct pb_protocol_message *message, m;
542         int rc;
543         unsigned int len;
544
545         /* use the stack for the initial 8-byte read */
546
547         rc = read(fd, &m, sizeof(m));
548         if (rc != sizeof(m))
549                 return NULL;
550
551         m.payload_len = __be32_to_cpu(m.payload_len);
552         m.action = __be32_to_cpu(m.action);
553
554         if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
555                 pb_log("%s: payload too big %u/%u\n", __func__, m.payload_len,
556                         PB_PROTOCOL_MAX_PAYLOAD_SIZE);
557                 return NULL;
558         }
559
560         message = talloc_size(ctx, sizeof(m) + m.payload_len);
561         memcpy(message, &m, sizeof(m));
562
563         for (len = 0; len < m.payload_len;) {
564                 rc = read(fd, message->payload + len, m.payload_len - len);
565
566                 if (rc <= 0) {
567                         talloc_free(message);
568                         pb_log("%s: failed (%u): %s\n", __func__, len,
569                                 strerror(errno));
570                         return NULL;
571                 }
572
573                 len += rc;
574         }
575
576         return message;
577 }
578
579
580 int pb_protocol_deserialise_device(struct device *dev,
581                 const struct pb_protocol_message *message)
582 {
583         unsigned int len;
584         const char *pos;
585         int rc = -1;
586
587         len = message->payload_len;
588         pos = message->payload;
589
590         if (read_string(dev, &pos, &len, &dev->id))
591                 goto out;
592
593         if (len < sizeof(enum device_type))
594                 goto out;
595         dev->type = *(enum device_type *)(pos);
596         pos += sizeof(enum device_type);
597         len -= sizeof(enum device_type);
598
599         if (read_string(dev, &pos, &len, &dev->name))
600                 goto out;
601
602         if (read_string(dev, &pos, &len, &dev->description))
603                 goto out;
604
605         if (read_string(dev, &pos, &len, &dev->icon_file))
606                 goto out;
607
608         rc = 0;
609
610 out:
611         return rc;
612 }
613
614 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
615                 const struct pb_protocol_message *message)
616 {
617         unsigned int len;
618         const char *pos;
619         int rc = -1;
620
621         len = message->payload_len;
622         pos = message->payload;
623
624         if (read_string(opt, &pos, &len, &opt->device_id))
625                 goto out;
626
627         if (read_string(opt, &pos, &len, &opt->id))
628                 goto out;
629
630         if (read_string(opt, &pos, &len, &opt->name))
631                 goto out;
632
633         if (read_string(opt, &pos, &len, &opt->description))
634                 goto out;
635
636         if (read_string(opt, &pos, &len, &opt->icon_file))
637                 goto out;
638
639         if (read_string(opt, &pos, &len, &opt->boot_image_file))
640                 goto out;
641
642         if (read_string(opt, &pos, &len, &opt->initrd_file))
643                 goto out;
644
645         if (read_string(opt, &pos, &len, &opt->dtb_file))
646                 goto out;
647
648         if (read_string(opt, &pos, &len, &opt->boot_args))
649                 goto out;
650
651         if (len < sizeof(bool))
652                 goto out;
653         opt->is_default = *(bool *)(pos);
654
655         rc = 0;
656
657 out:
658         return rc;
659 }
660
661 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
662                 const struct pb_protocol_message *message)
663 {
664         unsigned int len;
665         const char *pos;
666         int rc = -1;
667
668         len = message->payload_len;
669         pos = message->payload;
670
671         if (read_string(cmd, &pos, &len, &cmd->option_id))
672                 goto out;
673
674         if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
675                 goto out;
676
677         if (read_string(cmd, &pos, &len, &cmd->initrd_file))
678                 goto out;
679
680         if (read_string(cmd, &pos, &len, &cmd->dtb_file))
681                 goto out;
682
683         if (read_string(cmd, &pos, &len, &cmd->boot_args))
684                 goto out;
685
686         rc = 0;
687
688 out:
689         return rc;
690 }
691
692 int pb_protocol_deserialise_boot_status(struct boot_status *status,
693                 const struct pb_protocol_message *message)
694 {
695         unsigned int len;
696         const char *pos;
697         int rc = -1;
698
699         len = message->payload_len;
700         pos = message->payload;
701
702         /* first up, the type enum... */
703         if (len < sizeof(uint32_t))
704                 goto out;
705
706         status->type = __be32_to_cpu(*(uint32_t *)(pos));
707
708         switch (status->type) {
709         case BOOT_STATUS_ERROR:
710         case BOOT_STATUS_INFO:
711                 break;
712         default:
713                 goto out;
714         }
715
716         pos += sizeof(uint32_t);
717         len -= sizeof(uint32_t);
718
719         /* message and detail strings */
720         if (read_string(status, &pos, &len, &status->message))
721                 goto out;
722
723         if (read_string(status, &pos, &len, &status->detail))
724                 goto out;
725
726         /* and finally, progress */
727         if (len < sizeof(uint32_t))
728                 goto out;
729
730         status->progress = __be32_to_cpu(*(uint32_t *)(pos));
731
732         /* clamp to 100% */
733         if (status->progress > 100)
734                 status->progress = 100;
735
736         rc = 0;
737
738 out:
739         return rc;
740 }
741
742 int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
743                 const struct pb_protocol_message *message)
744 {
745         unsigned int len, i;
746         const char *pos;
747         int rc = -1;
748
749         len = message->payload_len;
750         pos = message->payload;
751
752         /* type and identifier strings */
753         if (read_string(sysinfo, &pos, &len, &sysinfo->type))
754                 goto out;
755
756         if (read_string(sysinfo, &pos, &len, &sysinfo->identifier))
757                 goto out;
758
759         /* number of interfaces */
760         if (read_u32(&pos, &len, &sysinfo->n_interfaces))
761                 goto out;
762
763         sysinfo->interfaces = talloc_array(sysinfo, struct interface_info *,
764                         sysinfo->n_interfaces);
765
766         for (i = 0; i < sysinfo->n_interfaces; i++) {
767                 struct interface_info *if_info = talloc(sysinfo,
768                                                         struct interface_info);
769
770                 if (read_u32(&pos, &len, &if_info->hwaddr_size))
771                         goto out;
772
773                 if (len < if_info->hwaddr_size)
774                         goto out;
775
776                 if_info->hwaddr = talloc_memdup(if_info, pos,
777                                                 if_info->hwaddr_size);
778                 pos += if_info->hwaddr_size;
779                 len -= if_info->hwaddr_size;
780
781                 if (read_string(if_info, &pos, &len, &if_info->name))
782                         goto out;
783
784                 if_info->link = *(bool *)pos;
785                 pos += sizeof(if_info->link);
786
787                 sysinfo->interfaces[i] = if_info;
788         }
789
790         /* number of interfaces */
791         if (read_u32(&pos, &len, &sysinfo->n_blockdevs))
792                 goto out;
793
794         sysinfo->blockdevs = talloc_array(sysinfo, struct blockdev_info *,
795                         sysinfo->n_blockdevs);
796
797         for (i = 0; i < sysinfo->n_blockdevs; i++) {
798                 struct blockdev_info *bd_info = talloc(sysinfo,
799                                                         struct blockdev_info);
800
801                 if (read_string(bd_info, &pos, &len, &bd_info->name))
802                         goto out;
803
804                 if (read_string(bd_info, &pos, &len, &bd_info->uuid))
805                         goto out;
806
807                 if (read_string(bd_info, &pos, &len, &bd_info->mountpoint))
808                         goto out;
809
810                 sysinfo->blockdevs[i] = bd_info;
811         }
812         rc = 0;
813
814 out:
815         return rc;
816 }
817
818 static int pb_protocol_deserialise_config_interface(const char **buf,
819                 unsigned int *len, struct interface_config *iface)
820 {
821         unsigned int tmp;
822
823         if (*len < sizeof(iface->hwaddr))
824                 return -1;
825
826         memcpy(iface->hwaddr, *buf, sizeof(iface->hwaddr));
827         *buf += sizeof(iface->hwaddr);
828         *len -= sizeof(iface->hwaddr);
829
830         if (read_u32(buf, len, &tmp))
831                 return -1;
832         iface->ignore = !!tmp;
833
834         if (iface->ignore)
835                 return 0;
836
837         if (read_u32(buf, len, &iface->method))
838                 return -1;
839
840         if (iface->method == CONFIG_METHOD_STATIC) {
841                 if (read_string(iface, buf, len, &iface->static_config.address))
842                         return -1;
843
844                 if (read_string(iface, buf, len, &iface->static_config.gateway))
845                         return -1;
846         }
847
848         return 0;
849 }
850
851 int pb_protocol_deserialise_config(struct config *config,
852                 const struct pb_protocol_message *message)
853 {
854         unsigned int len, i, tmp;
855         const char *pos;
856         int rc = -1;
857         char *str;
858
859         len = message->payload_len;
860         pos = message->payload;
861
862         if (read_u32(&pos, &len, &tmp))
863                 goto out;
864         config->autoboot_enabled = !!tmp;
865
866         if (read_u32(&pos, &len, &config->autoboot_timeout_sec))
867                 goto out;
868
869         if (read_u32(&pos, &len, &tmp))
870                 goto out;
871         config->safe_mode = !!tmp;
872
873         if (read_u32(&pos, &len, &config->network.n_interfaces))
874                 goto out;
875
876         config->network.interfaces = talloc_array(config,
877                         struct interface_config *, config->network.n_interfaces);
878
879         for (i = 0; i < config->network.n_interfaces; i++) {
880                 struct interface_config *iface = talloc_zero(
881                                 config->network.interfaces,
882                                 struct interface_config);
883                 if (pb_protocol_deserialise_config_interface(&pos, &len, iface))
884                         goto out;
885                 config->network.interfaces[i] = iface;
886         }
887
888         if (read_u32(&pos, &len, &config->network.n_dns_servers))
889                 goto out;
890         config->network.dns_servers = talloc_array(config, const char *,
891                         config->network.n_dns_servers);
892
893         for (i = 0; i < config->network.n_dns_servers; i++) {
894                 if (read_string(config->network.dns_servers, &pos, &len, &str))
895                         goto out;
896                 config->network.dns_servers[i] = str;
897         }
898
899         if (read_u32(&pos, &len, &config->n_boot_priorities))
900                 goto out;
901         config->boot_priorities = talloc_array(config, struct boot_priority,
902                         config->n_boot_priorities);
903
904         for (i = 0; i < config->n_boot_priorities; i++) {
905                 if (read_u32(&pos, &len, &tmp))
906                         goto out;
907                 config->boot_priorities[i].priority = (int)tmp;
908                 if (read_u32(&pos, &len, &tmp))
909                         goto out;
910                 config->boot_priorities[i].type = tmp;
911         }
912
913         if (read_string(config, &pos, &len, &str))
914                 goto out;
915
916         config->boot_device = str;
917
918         rc = 0;
919
920 out:
921         return rc;
922 }