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