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