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