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