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