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