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