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